From 3253ada67a2e9ede25de89f69ea458b799d51023 Mon Sep 17 00:00:00 2001 From: Saveliy Date: Tue, 6 Feb 2024 23:46:53 +0300 Subject: [PATCH] fix test_FDTD: add strustures, fix convergence --- PlotScript/convergence.py | 6 +- PlotScript/legacy.py | 102 ------------------- PlotScript/src/Release/sample.exe | Bin 44032 -> 44544 bytes include/FDTD.h | 2 + include/Writer.h | 71 ++++++++++++++ include/test_FDTD.h | 33 ++++++- samples/sample.cpp | 157 +++++++++++------------------- src/FDTD.cpp | 2 +- src/test_FDTD.cpp | 34 +++---- 9 files changed, 181 insertions(+), 226 deletions(-) delete mode 100644 PlotScript/legacy.py create mode 100644 include/Writer.h diff --git a/PlotScript/convergence.py b/PlotScript/convergence.py index d1de015..4914dcb 100644 --- a/PlotScript/convergence.py +++ b/PlotScript/convergence.py @@ -35,12 +35,12 @@ def update_sources(): if not (select_update == 0 or select_update == 1): print("Invalid input") exit(1) - - if (select_update): + elif (select_update == 1): with open("Source.txt", "w") as file: for component in input_list: file.write(input(component + ": ") + "\n") + def save_source_into_reserve(): with open('Source.txt', 'r') as file1, open('Reserve.txt', 'w') as file2: numbers = [float(line.strip()) for line in file1] @@ -92,7 +92,7 @@ def analyze_convergence(numbers, mult_2, iterations, shifts): if __name__ == '__main__': update_sources() loaded_numbers = save_source_into_reserve() - nums, convergence = analyze_convergence(loaded_numbers, 4, 4, False) + nums, convergence = analyze_convergence(loaded_numbers, 4, 5, False) reload_source() print(convergence) diff --git a/PlotScript/legacy.py b/PlotScript/legacy.py deleted file mode 100644 index 4f847f9..0000000 --- a/PlotScript/legacy.py +++ /dev/null @@ -1,102 +0,0 @@ -import matplotlib.pyplot as plt -import csv -import subprocess - - -def get_plot(num, data, size_N, size_x, time): - components = {1: "Ex", 2: "Ey", 3: "Ez", 4: "Bx", 5: "By", 6: "Bz"} - x = 0. - X = [] - V = [] - dx = (float(size_x[1]) - float(size_x[0])) / float(size_N[0]) - cnt = 0 - while cnt < size_N[0]: - X.append(x) - x += dx - cnt += 1 - - with open(data, 'r') as datafile: - plotting = list(csv.reader(datafile, delimiter=';')) - - V.extend([float(value) for value in plotting[(num - 1) * (size_N[1] + 2)]]) - - plt.plot(X, V) - plt.xlabel('X') - plt.ylabel(components[num]) - plt.title('Plot ' + components[num] + " (Time: " + str(time) + ")") - plt.grid(True) - plt.show() - - -if __name__ == '__main__': - input_list = ["Ni", "Nj", "ax", "bx", "ay", "by", "dt", "t"] - - print("Update parameters? \n \ - 1 - Yes \n \ - 2 - No") - select_update = int(input("Number: ")) * (-1) + 2 - if not (select_update == 0 or select_update == 1): - print("Invalid input") - exit(1) - - if (select_update): - with open("Source.txt", "w") as file: - for component in input_list: - file.write(input(component + ": ") + "\n") - else: - with open("Source.txt", "r+") as file: - lines = file.readlines() - lines[-1] = input("t : ") + "\n" - file.seek(0) - file.writelines(lines) - file.truncate() - - print("Select the active fields: \n \ - 1 - Ex \n \ - 2 - Ey \n \ - 3 - Ez \n \ - 4 - Bx \n \ - 5 - By \n \ - 6 - Bz") - field_1 = -1 + int(input("Number: ")) - field_2 = -1 + int(input("Number: ")) - if not (0 <= field_1 < 6 and 0 <= field_2 < 6): - print("Invalid input") - exit(1) - - print("Select field for plotting: \n \ - 1 - Ex \n \ - 2 - Ey \n \ - 3 - Ez \n \ - 4 - Bx \n \ - 5 - By \n \ - 6 - Bz") - select_to_plot = -1 + int(input("Number: ")) - if not (0 <= select_to_plot < 6 and (select_to_plot in (field_1, field_2))): - print("Invalid input") - exit(1) - - with open('Source.txt', 'r') as file: - numbers = [float(line.strip()) for line in file] - - if (field_1 == 0 and field_2 == 5 or field_1 == 2 and field_2 == 3): - arr_N = (int(numbers[1]), int(numbers[0])) - arr_x = (float(numbers[4]), float(numbers[5])) - elif (field_1 == 1 and field_2 == 5 or field_1 == 2 and field_2 == 4): - arr_N = (int(numbers[0]), int(numbers[1])) - arr_x = (float(numbers[2]), float(numbers[3])) - else: - print("Invalid selected fields") - exit(1) - - cpp_executable = "src/sample.exe" - args = [cpp_executable, str(field_1), str(field_2), str(select_to_plot)] - try: - subprocess.run(args, check=True) - except subprocess.CalledProcessError: - print("Error when starting a C++ project") - except FileNotFoundError: - print("sample.exe not found") - - file = 'OutFile.csv' - get_plot(select_to_plot + 1, file, arr_N, arr_x, numbers[7]) \ No newline at end of file diff --git a/PlotScript/src/Release/sample.exe b/PlotScript/src/Release/sample.exe index e1c67b32c165dc8569d8f5e8379deeecb2b3b223..3a786bc389eeee9a77f6eb202ad55be6bf4f264a 100644 GIT binary patch delta 11122 zcmeHMeOOf0*59+4!2yLcDubYiqYMh7h=7J)IFmDauz?Uk$WK)KDnGaeg=x+xQjDiD zbZghTdLyqHb+a(c$E;f@UcsXB_F-ySR9^28){ADPeR$sAKIia}_x|}l&->5idG`LT zwf5R;uf6v=d#^os&QZ^2wCcu?_jaFHLbShjGKBP`p#w<~1TIzhSrO~6TdLd7HtF2K z0gE9tp%k%T8XdYj1*8Wh6=gXaONZ(9u=%L>u}A1I_A%|l-lE~U7wgW_k-8qwEFz>e zD!(pR|G18RoyGoS_>RuZVp8A`Ix36395~H2-+C7Fm6-HMDga8y%8Q ztERE7Av5TtY3yQ%NWY)ThJ`xl!KrK&VA)hw7rK(Jo5=fOYQRnT?ApD92z=>S^7RRV)4U#QCwDw|HCXk!SXAj%J%t5mPMcWm(KROEcR{R zQFOMAMTbSx@ivwo_A_mn%*LB{(VOXY|1dvA>G4T*t0E2?=;IC+6Me|!7)yxMAWE4x z#FS?L^gGz4=tFMNTVged-VN4>JbSJ^&pykZFZxzm>%~o1Y&t?Rq;n48$X71urZeSR zhxcG5pOVkedJn+suv$c4W|M3V&LgC%v4S_E4{@fPcBa&ezFcdZ;=OD>#1%w|cUUay zVW3(y(2E6G2IM%CxLTZScI0w3pW#Y@YCg_UxoSVklUjQ&+FjmNHKJr_-OW{;d%#vX zeVI0=&q+n;Lq*Xa#U8d;@)Mxl7Agib9iwncHphS$%O9>GrK7Nv2JV3rvJFDN{cEhQ z)C6fizbwnXJH?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+++ b/include/Writer.h @@ -0,0 +1,71 @@ +#pragma once + +#include + +#include "FDTD.h" + +void write_x(Field& this_field, std::ofstream& fout) +{ + 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); + 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 i = 0; i < this_field.get_Ni(); ++i) + { + for (int j = 0; j < this_field.get_Nj(); ++j) + { + fout << this_field(i, j); + if (j == this_field.get_Nj() - 1) + { + fout << std::endl; + } + else + { + fout << ";"; + } + } + } + fout << std::endl << std::endl; +} + +void write_all(FDTD& test, char axis, char* file_path) +{ + std::ofstream test_fout; + test_fout.open(file_path); + + if (!test_fout.is_open()) + { + std::cout << "ERROR: Failed to open OutFile.csv" << std::endl; + exit(1); + } + for (int i = static_cast(Component::EX); i <= static_cast(Component::BZ); ++i) + { + if (axis == 'x') + { + write_x(test.get_field(static_cast(i)), test_fout); + } + else write_y(test.get_field(static_cast(i)), test_fout); + } + test_fout.close(); +} + +//#ifndef __DEBUG__ +//test_fout.open("OutFile.csv"); +//#else +//test_fout.open("../../PlotScript/OutFile.csv"); +//#endif diff --git a/include/test_FDTD.h b/include/test_FDTD.h index b323394..fb79f16 100644 --- a/include/test_FDTD.h +++ b/include/test_FDTD.h @@ -9,6 +9,32 @@ enum class Axis {X, Y}; +struct SelectedFields { + Component selected_1; + Component selected_2; + + Component calculated; +}; + +struct Parameters { + double ax; + double bx; + + double ay; + double by; + + double dx; + double dy; + + double time; + int iterations; +}; + +struct Functions { + std::function& init_function; + std::function& true_function; +}; + class Test_FDTD { private: @@ -17,7 +43,7 @@ class Test_FDTD bool shifted; double max_abs_error = 0.0; - void initial_filling(FDTD& _test, Component fields[2], double size_d, double size_wave[2], + void initial_filling(FDTD& _test, SelectedFields, double size_d, double size_wave[2], std::function& _init_function); void start_test(FDTD& _test, int _t); @@ -28,10 +54,7 @@ class Test_FDTD std::function& _true_function, double _t); public: - Test_FDTD(FDTD& test, Component fields[2], Component field_3, - double size_x[2], double size_y[2], double size_d[2], double time, int iters, - std::function& init_function, - std::function& true_function, bool _shifted); + Test_FDTD(FDTD& test, SelectedFields, Parameters, Functions, bool _shifted); double get_max_abs_error() { return max_abs_error; } }; diff --git a/samples/sample.cpp b/samples/sample.cpp index 2098321..c483c0d 100644 --- a/samples/sample.cpp +++ b/samples/sample.cpp @@ -6,71 +6,7 @@ #include #include "test_FDTD.h" - -void write_x(Field& this_field, std::ofstream& fout) -{ - 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); - 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 i = 0; i < this_field.get_Ni(); ++i) - { - for (int j = 0; j < this_field.get_Nj(); ++j) - { - fout << this_field(i, j); - if (j == this_field.get_Nj() - 1) - { - fout << std::endl; - } - else - { - fout << ";"; - } - } - } - fout << std::endl << std::endl; -} - -void write_all(FDTD& test, char axis) -{ - std::ofstream test_fout; - -#ifndef __DEBUG__ - test_fout.open("OutFile.csv"); -#else - test_fout.open("../../PlotScript/OutFile.csv"); -#endif - - if (!test_fout.is_open()) - { - std::cout << "ERROR: Failed to open OutFile.csv" << std::endl; - exit(1); - } - for (int i = static_cast(Component::EX); i <= static_cast(Component::BZ); ++i) - { - if (axis == 'x') - { - write_x(test.get_field(static_cast(i)), test_fout); - } - else write_y(test.get_field(static_cast(i)), test_fout); - } - test_fout.close(); -} +#include "Writer.h" #ifndef __DEBUG__ int main(int argc, char* argv[]) @@ -94,50 +30,55 @@ int main() } #ifdef __DEBUG__ - const char* argv[5] = {"1", "2", "3", "2", "1"}; + 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 flds_selected[2] = { selected_field_1, selected_field_2 }; + + // Saving data from the txt file double number; - while (source_fin >> number) + while (source_fin >> number) { numbers.push_back(number); } + int grid_sizes[2] = { numbers[0], numbers[1] }; // { Nx, Ny } + double sizes_x[2] = { numbers[2], numbers[3] }; // { ax, bx } + double sizes_y[2] = { numbers[4], numbers[5] }; // { ay, by } + double step_sizes[2] = { (sizes_x[1] - sizes_x[0]) / grid_sizes[0], + (sizes_y[1] - sizes_y[0]) / grid_sizes[1] }; // { dx, dy } + int iterations_num = static_cast(numbers[6]); + double time = numbers[7]; + double time_step = time / static_cast(iterations_num); // dt - int arr_N[2] = { numbers[0], numbers[1] }; - double arr_x[2] = { numbers[2], numbers[3] }; - double arr_y[2] = { numbers[4], numbers[5] }; - double arr_d[2] = { (arr_x[1] - arr_x[0]) / arr_N[0], (arr_y[1] - arr_y[0]) / arr_N[1] }; - - double t = numbers[7]; - int iter_nums = static_cast(numbers[6]); - double dt = t / static_cast(iter_nums); - - Component flds_selected[2] = { static_cast(std::atoi(argv[1])), static_cast(std::atoi(argv[2])) }; - Component fld_tested = static_cast(std::atoi(argv[3])); - bool shifted_flag = static_cast(std::atoi(argv[4])); - + source_fin.close(); + // Initialization of the initializing function and the true solution function std::function initial_func = - [](double x, double size[2]) { return sin(2.0 * M_PI * (x - size[0]) / (size[1] - size[0])); }; - + [](double x, double size[2]) { + return sin(2.0 * M_PI * (x - size[0]) / (size[1] - size[0])); + }; std::function true_func = - [](double x, double t, double size[2]) { return sin(2.0 * M_PI * (x - size[0] - FDTD_Const::C * t) / (size[1] - size[0])); }; - + [](double x, double t, double size[2]) { + 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'; - //dt = courant_condtition * arr_d[0] / FDTD_Const::C; - //t = courant_condtition * (arr_x[1] - arr_x[0]) / FDTD_Const::C; } 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'; - //dt = courant_condtition * arr_d[1] / FDTD_Const::C; - //t = courant_condtition * (arr_y[1] - arr_y[0]) / FDTD_Const::C; } else { @@ -145,18 +86,38 @@ int main() exit(1); } - - FDTD test_1(arr_N, arr_x, arr_y, dt); - - - - Test_FDTD test(test_1, flds_selected, fld_tested, arr_x, arr_y, arr_d, t, iter_nums, initial_func, true_func, shifted_flag); + // Meaningful calculations + FDTD test_1(grid_sizes, sizes_x, sizes_y, time_step); + SelectedFields current_fields{ + flds_selected[0], + flds_selected[1], + fld_tested + }; + Parameters params{ + sizes_x[0], + sizes_x[1], + sizes_y[0], + sizes_y[1], + step_sizes[0], + step_sizes[1], + time, + iterations_num + }; + 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; - - write_all(test_1, selected_axis); - - source_fin.close(); + // 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); return 0; } diff --git a/src/FDTD.cpp b/src/FDTD.cpp index 9ebdca4..7e67fd3 100644 --- a/src/FDTD.cpp +++ b/src/FDTD.cpp @@ -64,7 +64,7 @@ Field& FDTD::get_field(Component this_field) void FDTD::update_field(const int time) { - for (double t = 0; t <= time; ++t) + for (double t = 0; t < time; ++t) { #pragma omp parallel for collapse(2) for (int j = 0; j < Nj; ++j) diff --git a/src/test_FDTD.cpp b/src/test_FDTD.cpp index 807b052..86ff6d8 100644 --- a/src/test_FDTD.cpp +++ b/src/test_FDTD.cpp @@ -1,6 +1,6 @@ #include "test_FDTD.h" -void Test_FDTD::initial_filling(FDTD& _test, Component fields[2], double size_d, double size_wave[2], +void Test_FDTD::initial_filling(FDTD& _test, SelectedFields fields, double size_d, double size_wave[2], std::function& _init_function) { if (axis == Axis::X) @@ -15,8 +15,8 @@ void Test_FDTD::initial_filling(FDTD& _test, Component fields[2], double size_d, double x = static_cast(i) * size_d; for (int j = 0; j < _test.get_Nj(); j++) { - _test.get_field(fields[0])(i, j) = sign * _init_function(x, size_wave); - _test.get_field(fields[1])(i, j) = _init_function(x_b + x, size_wave); + _test.get_field(fields.selected_1)(i, j) = sign * _init_function(x, size_wave); + _test.get_field(fields.selected_2)(i, j) = _init_function(x_b + x, size_wave); } } } @@ -32,8 +32,8 @@ void Test_FDTD::initial_filling(FDTD& _test, Component fields[2], double size_d, double y = static_cast(j) * size_d; for (int i = 0; i < _test.get_Ni(); i++) { - _test.get_field(fields[0])(i, j) = sign * _init_function(y, size_wave); - _test.get_field(fields[1])(i, j) = _init_function(y_b + y, size_wave); + _test.get_field(fields.selected_1)(i, j) = sign * _init_function(y, size_wave); + _test.get_field(fields.selected_2)(i, j) = _init_function(y_b + y, size_wave); } } } @@ -91,13 +91,10 @@ void Test_FDTD::get_max_abs_error(Field& this_field, Component field, double _si } } -Test_FDTD::Test_FDTD(FDTD& test, Component fields[2], Component field_3, - double size_x[2], double size_y[2], double size_d[2], double time, int iters, - std::function& init_function, - std::function& true_function, bool _shifted) : shifted(_shifted) +Test_FDTD::Test_FDTD(FDTD& test, SelectedFields fields, Parameters parameters, Functions functions, bool _shifted) : shifted(_shifted) { - Component field_E = fields[0]; - Component field_B = fields[1]; + Component field_E = fields.selected_1; + Component field_B = fields.selected_2; if (field_E == Component::EY && field_B == Component::BZ || field_E == Component::EZ && field_B == Component::BX) { sign = 1.0; @@ -107,23 +104,26 @@ Test_FDTD::Test_FDTD(FDTD& test, Component fields[2], Component field_3, sign = -1.0; } + double size_steps[2] = { parameters.dx, parameters.dy }; if (field_E == Component::EY && field_B == Component::BZ || field_E == Component::EZ && field_B == Component::BY) { axis = Axis::X; - initial_filling(test, fields, size_d[0], size_x, init_function); + double size_x[2] = { parameters.ax, parameters.bx }; + initial_filling(test, fields, parameters.dx, size_x, functions.init_function); - start_test(test, iters); + start_test(test, parameters.iterations); - get_max_abs_error(test.get_field(field_3), field_3, size_d, size_x, true_function, time); + get_max_abs_error(test.get_field(fields.calculated), fields.calculated, size_steps, size_x, functions.true_function, parameters.time); } else if (field_E == Component::EX && field_B == Component::BZ || field_E == Component::EZ && field_B == Component::BX) { axis = Axis::Y; - initial_filling(test, fields, size_d[1], size_y, init_function); + double size_y[2] = { parameters.ay, parameters.by }; + initial_filling(test, fields, parameters.dy, size_y, functions.init_function); - start_test(test, iters); + start_test(test, parameters.iterations); - get_max_abs_error(test.get_field(field_3), field_3, size_d, size_y, true_function, time); + get_max_abs_error(test.get_field(fields.calculated), fields.calculated, size_steps, size_y, functions.true_function, parameters.time); } else {