Skip to content

Commit

Permalink
refactor of sample
Browse files Browse the repository at this point in the history
  • Loading branch information
Amazingkivas committed Feb 17, 2024
1 parent 3253ada commit a70a061
Showing 1 changed file with 57 additions and 44 deletions.
101 changes: 57 additions & 44 deletions samples/sample.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,74 @@
#define _USE_MATH_DEFINES
//#define __DEBUG__

#include <iostream>
#include <iomanip>
#include <fstream>
#include <iterator>

#include "test_FDTD.h"
#include "Writer.h"

#ifndef __DEBUG__
char get_axis(Component field_E, Component field_B)
{
char selected_axis;
if (field_E == Component::EY && field_B == Component::BZ ||
field_E == Component::EZ && field_B == Component::BY)
{
selected_axis = 'x';
}
else if (field_E == Component::EX && field_B == Component::BZ ||
field_E == Component::EZ && field_B == Component::BX)
{
selected_axis = 'y';
}
else
{
std::cout << "ERROR" << std::endl;
exit(1);
}
}

int main(int argc, char* argv[])
#else
int main()
#endif
{
std::vector<double> numbers;
std::ifstream source_fin;
char* outfile_path;

#ifndef __DEBUG__
source_fin.open("Source.txt");
#else
source_fin.open("../../PlotScript/Source.txt");
#endif
std::vector<char*> arguments(argv, argv + argc);
if (argc == 1)
{
source_fin.open("../../PlotScript/Source.txt");
outfile_path = "OutFile.csv";

const size_t size_tmp = 5;
char* tmp[size_tmp]{ "1", "2", "3", "2", "1" };

arguments.clear();
arguments.reserve(size_tmp);
std::copy(std::begin(tmp), std::end(tmp), std::back_inserter(arguments));
}
else
{
source_fin.open("Source.txt");
outfile_path = "../../PlotScript/OutFile.csv";
}
if (!source_fin.is_open())
{
std::cout << "ERROR: Failed to open Source.txt" << std::endl;
return 1;
}

#ifdef __DEBUG__
const char* argv[5] = { "1", "2", "3", "2", "1" };
#endif

// Saving data from the console
Component selected_field_1 = static_cast<Component>(std::atoi(argv[1]));
Component selected_field_2 = static_cast<Component>(std::atoi(argv[2]));
Component fld_tested = static_cast<Component>(std::atoi(argv[3]));
bool version_flag = static_cast<bool>(std::atoi(argv[4]));
Component selected_field_1 = static_cast<Component>(std::atoi(arguments[1]));
Component selected_field_2 = static_cast<Component>(std::atoi(arguments[2]));
Component fld_tested = static_cast<Component>(std::atoi(arguments[3]));
bool version_flag = static_cast<bool>(std::atoi(arguments[4]));

Component flds_selected[2] = { selected_field_1, selected_field_2 };


// Saving data from the txt file
std::vector<double> numbers;
double number;
while (source_fin >> number)
{
Expand All @@ -58,6 +85,7 @@ int main()

source_fin.close();


// Initialization of the initializing function and the true solution function
std::function<double(double, double[2])> initial_func =
[](double x, double size[2]) {
Expand All @@ -68,32 +96,21 @@ int main()
return sin(2.0 * M_PI * (x - size[0] - FDTD_Const::C * t) / (size[1] - size[0]));
};


// Determination of the wave propagation axis
char selected_axis;
if (flds_selected[0] == Component::EY && flds_selected[1] == Component::BZ ||
flds_selected[0] == Component::EZ && flds_selected[1] == Component::BY)
{
selected_axis = 'x';
}
else if (flds_selected[0] == Component::EX && flds_selected[1] == Component::BZ ||
flds_selected[0] == Component::EZ && flds_selected[1] == Component::BX)
{
selected_axis = 'y';
}
else
{
std::cout << "ERROR" << std::endl;
exit(1);
}
char axis = get_axis(flds_selected[0], flds_selected[1]);


// Meaningful calculations
FDTD test_1(grid_sizes, sizes_x, sizes_y, time_step);
SelectedFields current_fields{
SelectedFields current_fields
{
flds_selected[0],
flds_selected[1],
fld_tested
};
Parameters params{
Parameters params
{
sizes_x[0],
sizes_x[1],
sizes_y[0],
Expand All @@ -103,21 +120,17 @@ int main()
time,
iterations_num
};
Functions funcs{
Functions funcs
{
initial_func,
true_func
};
Test_FDTD test(test_1, current_fields, params, funcs, version_flag);
std::cout << test.get_max_abs_error() << std::endl;


// Writing the results to a file
char* file_path;
#ifndef __DEBUG__
file_path = "OutFile.csv";
#else
file_path = "../../PlotScript/OutFile.csv";
#endif
write_all(test_1, selected_axis, file_path);
write_all(test_1, axis, outfile_path);

return 0;
}

0 comments on commit a70a061

Please sign in to comment.