From a70a0617f9b79c16af5da48d7bfa1bfb55e13857 Mon Sep 17 00:00:00 2001 From: Saveliy Date: Sat, 17 Feb 2024 18:15:57 +0300 Subject: [PATCH] refactor of sample --- samples/sample.cpp | 101 +++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 44 deletions(-) diff --git a/samples/sample.cpp b/samples/sample.cpp index c483c0d..d34869c 100644 --- a/samples/sample.cpp +++ b/samples/sample.cpp @@ -1,47 +1,74 @@ #define _USE_MATH_DEFINES -//#define __DEBUG__ #include #include #include +#include #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 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 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(std::atoi(argv[1])); - Component selected_field_2 = static_cast(std::atoi(argv[2])); - Component fld_tested = static_cast(std::atoi(argv[3])); - bool version_flag = static_cast(std::atoi(argv[4])); + Component selected_field_1 = static_cast(std::atoi(arguments[1])); + Component selected_field_2 = static_cast(std::atoi(arguments[2])); + Component fld_tested = static_cast(std::atoi(arguments[3])); + bool version_flag = static_cast(std::atoi(arguments[4])); Component flds_selected[2] = { selected_field_1, selected_field_2 }; + // Saving data from the txt file + std::vector numbers; double number; while (source_fin >> number) { @@ -58,6 +85,7 @@ int main() source_fin.close(); + // Initialization of the initializing function and the true solution function std::function initial_func = [](double x, double size[2]) { @@ -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], @@ -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; }