Skip to content

Commit

Permalink
remove Cell_number & add OpenMP
Browse files Browse the repository at this point in the history
  • Loading branch information
Amazingkivas committed Nov 21, 2023
1 parent 27b0fc3 commit 566aa5e
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 233 deletions.
25 changes: 20 additions & 5 deletions PlotScript/convergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,37 @@ def execute_cpp(field_1, field_2, field_to_plot):
numbers = [float(line.strip()) for line in file]

convergences = []
for n in range(0, 10):
nums = []
for n in range(0, 5):
with open("Source.txt", "w") as file:
tmp_n_0 = numbers[0] * (1.5 ** n)
mult_1 = 2
mult_2 = 4

tmp_n_0 = numbers[0] * (mult_1 ** n)
file.write(str(tmp_n_0) + "\n")

tmp_n_1 = numbers[1] * (1.5 ** n)
tmp_n_1 = numbers[1] * (mult_1 ** n)
file.write(str(tmp_n_1) + "\n")

file.write(str(numbers[2]) + "\n")
file.write(str(numbers[3]) + "\n")
file.write(str(numbers[4]) + "\n")
file.write(str(numbers[5]) + "\n")

tmp_n_6 = numbers[6] / (1.5 ** n)
tmp_n_6 = numbers[6] / (mult_2 ** n)
file.write(str(tmp_n_6) + "\n")

file.write(str(numbers[7]) + "\n")

convergences.append(execute_cpp("Ex", "Bz", "Ex"))
convergences.append(float(execute_cpp("Ex", "Bz", "Ex")))
convers = []
for n in range(0, 4):
convers.append(convergences[n] / convergences[n+1])
nums.append(n)
print(convers)
plt.plot(nums, convers)
plt.xlabel('n')
plt.ylabel('E/mult')
plt.title("Plot")
plt.grid(True)
plt.show()
Binary file modified PlotScript/src/Release/sample.exe
Binary file not shown.
16 changes: 1 addition & 15 deletions include/FDTD.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
#include <vector>
#include <cmath>
#include <iostream>
#include <omp.h>

namespace FDTD_Const
{
const double C = 3e10;
}

class Cell_number
{
private:
int border;
int current;
int start;
public:
explicit Cell_number(int max_num, int start_num = 0) : border(max_num), current(start_num), start(start_num) {}
int operator+ (int) const;
int operator- (int) const;
int operator* ();
Cell_number& operator++ ();
bool operator< (int);
};

class Field
{
private:
Expand Down
144 changes: 11 additions & 133 deletions include/test_FDTD.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
#include <iomanip>
#include <fstream>
#include <functional>

#include "FDTD.h"

//sin(2.0 * M_PI * (x - size_wave[0]) / (size_wave[1] - size_wave[0]))
//sin(2.0 * M_PI * (x - size_wave[0] - FDTD_Const::C * _t) / (size_wave[1] - size_wave[0])))

enum class Axis {X, Y};

class Test_FDTD
Expand All @@ -21,139 +17,21 @@ class Test_FDTD
bool shifted;
double max_abs_error = 0.0;

void initial_filling(FDTD& _test, Component field_1, Component field_2, double size_d, double size_wave[2],
std::function<double(double, double[2])>& _init_function)
{
if (axis == Axis::X)
{
double x = 0.0;
double x_b = 0.0;
for (int i = 0; i < _test.get_Ni(); x += size_d, ++i)
{
for (int j = 0; j < _test.get_Nj(); ++j)
{
_test.get_field(field_1)(i, j) = sign * _init_function(x, size_wave);
if (shifted)
{
x_b = x + size_d / 2.0;
}
else x_b = x;
_test.get_field(field_2)(i, j) = _init_function(x_b, size_wave);
}
}
}
else
{
double y = 0.0;
double y_b = 0.0;
for (int j = 0; j < _test.get_Nj(); y += size_d, ++j)
{
for (int i = 0; i < _test.get_Ni(); ++i)
{
_test.get_field(field_1)(i, j) = sign * _init_function(y, size_wave);
if (shifted)
{
y_b = y + size_d / 2.0;
}
else y_b = y;
_test.get_field(field_2)(i, j) = _init_function(y_b, size_wave);
}
}
}
}
void initial_filling(FDTD& _test, Component field_1, Component field_2, double size_d, double size_wave[2],
std::function<double(double, double[2])>& _init_function);

void start(FDTD& _test, double _t)
{
if (shifted)
{
_test.shifted_update_field(_t);
}
else _test.update_field(_t);
}
double get_shift(Component _field, double step)
{
if (static_cast<int>(_field) > static_cast<int>(Component::EZ))
{
sign = 1.0;
return step / 2;
}
}
void start(FDTD& _test, double _t);

void get_max_abs_error(Field& this_field, Component field, double _size_d[2], double size_wave[2],
std::function<double(double, double, double[2])>& _true_function, double _t)
{
double shift = 0.0;
if (axis == Axis::X)
{
shift = get_shift(field, _size_d[0]);
double extr_n = 0.0;
double x = (shifted) ? shift : 0.0;
int j = 0;
for (int i = 0; i < this_field.get_Ni(); ++i, x += _size_d[0])
{
double this_n = fabs(sign * this_field(i, j) - _true_function(x, _t, size_wave));
if (this_n > extr_n)
extr_n = this_n;
}
max_abs_error = extr_n;
}
else
{
shift = get_shift(field, _size_d[1]);
double extr_n = 0.0;
double y = (shifted) ? shift : 0.0;
int i = 0;
for (int j = 0; j < this_field.get_Nj(); ++j, y += _size_d[1])
{
double this_n = fabs(sign * this_field(i, j) - _true_function(y, _t, size_wave));
if (this_n > extr_n)
extr_n = this_n;
}
max_abs_error = extr_n;
}
}
double get_shift(Component _field, double step);

void get_max_abs_error(Field& this_field, Component field, double _size_d[2], double size_wave[2],
std::function<double(double, double, double[2])>& _true_function, double _t);

public:
Test_FDTD(FDTD& test, Component field_1, Component field_2, Component field_3,
double size_x[2], double size_y[2], double size_d[2], double t,
std::function<double(double, double[2])>& init_function,
std::function<double(double, double, double[2])>& true_function, bool _shifted) : shifted(_shifted)
{
if (field_1 == Component::EY && field_2 == Component::BZ || field_1 == Component::EZ && field_2 == Component::BX)
{
sign = 1.0;
}
else if (field_1 == Component::EX && field_2 == Component::BZ || field_1 == Component::EZ && field_2 == Component::BY)
{
sign = -1.0;
}

if (field_1 == Component::EY && field_2 == Component::BZ || field_1 == Component::EZ && field_2 == Component::BY)
{
axis = Axis::X;
initial_filling(test, field_1, field_2, size_d[0], size_x, init_function);

start(test, t);

get_max_abs_error(test.get_field(field_3), field_3, size_d, size_x, true_function, t);
}
else if (field_1 == Component::EX && field_2 == Component::BZ || field_1 == Component::EZ && field_2 == Component::BX)
{
axis = Axis::Y;
initial_filling(test, field_1, field_2, size_d[1], size_y, init_function);

start(test, t);

get_max_abs_error(test.get_field(field_3), field_3, size_d, size_y, true_function, t);
}
else
{
throw std::exception("ERROR: invalid selected fields");
}
}
double size_x[2], double size_y[2], double size_d[2], double t,
std::function<double(double, double[2])>& init_function,
std::function<double(double, double, double[2])>& true_function, bool _shifted);

double get_max_abs_error()
{
return max_abs_error;
}
double get_max_abs_error() { return max_abs_error; }
};
2 changes: 1 addition & 1 deletion samples/sample.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define _USE_MATH_DEFINES
//#define __DEBUG__
#define __DEBUG__

#include <iostream>
#include <iomanip>
Expand Down
4 changes: 4 additions & 0 deletions sln/FDTD/FDTD.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;CMAKE_INTDIR="Debug"</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_DEBUG;_WINDOWS;CMAKE_INTDIR=\"Debug\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -125,6 +126,7 @@
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -158,6 +160,7 @@
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"MinSizeRel\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -190,6 +193,7 @@
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="RelWithDebInfo"</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"RelWithDebInfo\"</PreprocessorDefinitions>
Expand Down
4 changes: 4 additions & 0 deletions sln/FDTD_test/FDTD_test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;CMAKE_INTDIR="Debug"</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_DEBUG;_WINDOWS;CMAKE_INTDIR=\"Debug\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -143,6 +144,7 @@
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -186,6 +188,7 @@
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"MinSizeRel\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -228,6 +231,7 @@
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="RelWithDebInfo"</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"RelWithDebInfo\"</PreprocessorDefinitions>
Expand Down
4 changes: 4 additions & 0 deletions sln/gtest/gtest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;CMAKE_INTDIR="Debug"</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_DEBUG;_WINDOWS;CMAKE_INTDIR=\"Debug\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -125,6 +126,7 @@
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -158,6 +160,7 @@
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"MinSizeRel\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -190,6 +193,7 @@
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="RelWithDebInfo"</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"RelWithDebInfo\"</PreprocessorDefinitions>
Expand Down
4 changes: 4 additions & 0 deletions sln/sample/sample.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;CMAKE_INTDIR="Debug"</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_DEBUG;_WINDOWS;CMAKE_INTDIR=\"Debug\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -143,6 +144,7 @@
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"Release\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -186,6 +188,7 @@
<ObjectFileName>$(IntDir)</ObjectFileName>
<DebugInformationFormat>
</DebugInformationFormat>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"MinSizeRel\"</PreprocessorDefinitions>
Expand Down Expand Up @@ -228,6 +231,7 @@
<WarningLevel>Level3</WarningLevel>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR="RelWithDebInfo"</PreprocessorDefinitions>
<ObjectFileName>$(IntDir)</ObjectFileName>
<OpenMPSupport>true</OpenMPSupport>
</ClCompile>
<ResourceCompile>
<PreprocessorDefinitions>%(PreprocessorDefinitions);WIN32;_WINDOWS;NDEBUG;CMAKE_INTDIR=\"RelWithDebInfo\"</PreprocessorDefinitions>
Expand Down
Loading

0 comments on commit 566aa5e

Please sign in to comment.