Skip to content

Commit

Permalink
Merge pull request #12 from Amazingkivas/optim_fdtd
Browse files Browse the repository at this point in the history
Optimization FDTD
  • Loading branch information
Amazingkivas authored Oct 10, 2024
2 parents e3776d1 + 181f783 commit 79c8256
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 540 deletions.
Binary file modified PlotScript/Animations/animation_Ex.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 4 additions & 24 deletions include/FDTD.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,11 @@
#include <omp.h>
#include <functional>

#include "Structures.h"
#include "Writer.h"
#include "Field.h"

using namespace FDTDstruct;

class Field
{
private:
int Ni, Nj, Nk;
std::vector<double> field;

public:
Field(const int _Ni, const int _Nj, const int _Nk);
Field& operator= (const Field& other);

double& operator() (int i, int j, int k);

int get_Ni() { return Ni; }
int get_Nj() { return Nj; }
int get_Nk() { return Nk; }
};

class FDTD
{
private:
Expand Down Expand Up @@ -59,16 +43,12 @@ class FDTD
void set_sigma_z(int bounds_i[2], int bounds_j[2], int bounds_k[2],
double SGm, std::function<int(int, int, int)> dist);

double calc_max_value(Field& field);

public:
FDTD(Parameters _parameters, double _dt, double _pml_percent);

Field& get_field(Component);
std::vector<Field>& get_current(Component);

std::vector<std::vector<Field>> update_fields(const int time);

double calc_reflection(Field E_start[3], Field B_start[3],
Field E_final[3], Field B_final[3]);
std::vector<Field> update_fields(const int time, bool write_result = false,
Axis write_axis = Axis::X, std::string base_path = "");
};
20 changes: 20 additions & 0 deletions include/Field.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <vector>

class Field
{
private:
int Ni, Nj, Nk;
std::vector<double> field;

public:
Field(const int _Ni = 1, const int _Nj = 1, const int _Nk = 1);
Field& operator= (const Field& other);

double& operator() (int i, int j, int k);

int get_Ni() { return Ni; }
int get_Nj() { return Nj; }
int get_Nk() { return Nk; }
};
174 changes: 9 additions & 165 deletions include/Writer.h
Original file line number Diff line number Diff line change
@@ -1,172 +1,16 @@
#pragma once

#include "Structures.h"
#include "Field.h"

#include <iostream>
#include <fstream>
#include <string>

#include "FDTD.h"

void write_x(Field& this_field, std::ofstream& fout)
{
for (int k = 0; k < this_field.get_Nk(); ++k)
{
for (int j = 0; j < this_field.get_Nj(); ++j)
{
for (int i = 0; i < this_field.get_Ni(); ++i)
{
fout << this_field(i, j, k);
if (i == this_field.get_Ni() - 1)
{
fout << std::endl;
}
else
{
fout << ";";
}
}
}
}
fout << std::endl << std::endl;
}
void write_y(Field& this_field, std::ofstream& fout)
{
for (int k = 0; k < this_field.get_Nk(); ++k)
{
for (int i = 0; i < this_field.get_Ni(); ++i)
{
for (int j = 0; j < this_field.get_Nj(); ++j)
{
fout << this_field(i, j, k);
if (j == this_field.get_Nj() - 1)
{
fout << std::endl;
}
else
{
fout << ";";
}
}
}
}
fout << std::endl << std::endl;
}
void write_z(Field& this_field, std::ofstream& fout)
{
for (int i = 0; i < this_field.get_Ni(); ++i)
{
for (int j = 0; j < this_field.get_Nj(); ++j)
{
for (int k = 0; k < this_field.get_Nk(); ++k)
{
fout << this_field(i, j, k);
if (k == this_field.get_Nk() - 1)
{
fout << std::endl;
}
else
{
fout << ";";
}
}
}
}
fout << std::endl << std::endl;
}

void write_spherical(std::vector<std::vector<Field>> data, Axis axis, int iteration, std::string base_path)
{
std::ofstream test_fout;

int it = 0;
for (std::vector<Field> fields : data)
{
it++;
for (int c = static_cast<int>(Component::EX); c <= static_cast<int>(Component::BZ); ++c)
{
std::cout << "Write : " << it << " -- " << c << std::endl;
if (axis == Axis::X)
{
test_fout.open(base_path + "OutFiles_" + std::to_string(c + 1) + "/" + std::to_string(it) + ".csv");
Field field = fields[c];
for (int k = 0; k < field.get_Nk(); ++k)
{
for (int j = 0; j < field.get_Nj(); ++j)
{
test_fout << field(field.get_Ni() / 2, j, k);
if (j == field.get_Nj() - 1)
{
test_fout << std::endl;
}
else
{
test_fout << ";";
}
}
}
test_fout.close();
}
else if (axis == Axis::Y)
{
test_fout.open(base_path + "OutFiles_" + std::to_string(c + 1) + "/" + std::to_string(it) + ".csv");
Field field = fields[c];
for (int i = 0; i < field.get_Nj(); ++i)
{
for (int k = 0; k < field.get_Nk(); ++k)
{
test_fout << field(i, field.get_Nj() / 2, k);
if (k == field.get_Nk() - 1)
{
test_fout << std::endl;
}
else
{
test_fout << ";";
}
}
}
test_fout.close();
}
else
{
test_fout.open(base_path + "OutFiles_" + std::to_string(c + 1) + "/" + std::to_string(it) + ".csv");
Field field = fields[c];
for (int i = 0; i < field.get_Nj(); ++i)
{
for (int j = 0; j < field.get_Nk(); ++j)
{
test_fout << field(i, j, field.get_Nk() / 2);
if (j == field.get_Nk() - 1)
{
test_fout << std::endl;
}
else
{
test_fout << ";";
}
}
}
test_fout.close();
}
}
}
}
using namespace FDTDstruct;

void write_plane(FDTD& test, Axis axis, char* file_path)
{
std::ofstream test_fout;
test_fout.open(file_path);
void write_x(Field& this_field, std::ofstream& fout);
void write_y(Field& this_field, std::ofstream& fout);
void write_z(Field& this_field, std::ofstream& fout);

if (!test_fout.is_open())
{
throw std::runtime_error("ERROR: Failed to open " + static_cast<std::string>(file_path));
}
for (int i = static_cast<int>(Component::EX); i <= static_cast<int>(Component::BZ); ++i)
{
if (axis == Axis::X)
write_x(test.get_field(static_cast<Component>(i)), test_fout);
else if (axis == Axis::Y)
write_y(test.get_field(static_cast<Component>(i)), test_fout);
else
write_z(test.get_field(static_cast<Component>(i)), test_fout);
}
test_fout.close();
}
void write_spherical(std::vector<Field> fields, Axis axis, std::string base_path, int it);
Loading

0 comments on commit 79c8256

Please sign in to comment.