Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes #4

Merged
merged 6 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
- name: Run tests
shell: bash
run: |
${PWD}/sln/build/FDTD_test/Release/FDTD_test.exe --gtest_repeat=2 --gtest_break_on_failure
${PWD}/sln/build/test_FDTD_method/Release/test_FDTD_method.exe --gtest_repeat=2 --gtest_break_on_failure
3,840 changes: 3,072 additions & 768 deletions PlotScript/OutFile.csv

Large diffs are not rendered by default.

Binary file modified PlotScript/Plots/plt_Bx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified PlotScript/Plots/plt_By.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified PlotScript/Plots/plt_Bz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified PlotScript/Plots/plt_Ex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified PlotScript/Plots/plt_Ey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified PlotScript/Plots/plt_Ez.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions PlotScript/Reserve.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
16.0
16.0
4.0
4.0
4.0
0.0
1.0
0.0
1.0
16.0
0.0
1.0
4.0
5e-13
5 changes: 4 additions & 1 deletion PlotScript/Source.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
8.0
32.0
16.0
16.0
0.0
1.0
0.0
1.0
0.0
Expand Down
30 changes: 12 additions & 18 deletions PlotScript/convergence.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import matplotlib.pyplot as plt
import csv
import subprocess

components = {1: "Ex", 2: "Ey", 3: "Ez", 4: "Bx", 5: "By", 6: "Bz"}
nums_com = {"Ex": 0, "Ey": 1, "Ez": 2, "Bx": 3, "By": 4, "Bz": 5}
input_list = ["Ni", "Nj", "ax", "bx", "ay", "by", "dt", "t"]
input_list = ["Ni", "Nj", "Nk" "ax", "bx", "ay", "by", "az", "bz", "dt", "t"]


def execute_cpp(field_1, field_2, field_to_plot, shift_flag):
Expand Down Expand Up @@ -35,7 +34,7 @@ def update_sources():
if not (select_update == 0 or select_update == 1):
print("Invalid input")
exit(1)
elif (select_update == 1):
elif select_update == 1:
with open("Source.txt", "w") as file:
for component in input_list:
file.write(input(component + ": ") + "\n")
Expand Down Expand Up @@ -64,23 +63,18 @@ def analyze_convergence(numbers, mult_2, iterations, shifts):
for n in range(0, iterations):
with open("Source.txt", "w") as file:
mult_1 = 2
for num in range(0, 3):
tmp = numbers[num] * (mult_1 ** n)
file.write(str(tmp) + "\n")

tmp_n_0 = numbers[0] * (mult_1 ** n)
file.write(str(tmp_n_0) + "\n")
for num in range(3, 9):
file.write(str(numbers[num]) + "\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] * (mult_2 ** n)
tmp_n_6 = numbers[9] * (mult_2 ** n)
file.write(str(tmp_n_6) + "\n")
file.write(str(numbers[7]) + "\n")
file.write(str(numbers[10]) + "\n")

convergences.append(float(execute_cpp("Ex", "Bz", "Ex", flag)))
convergences.append(float(execute_cpp("Ex", "By", "Ex", flag)))

convers = []
for n in range(0, iterations - 1):
Expand All @@ -92,13 +86,13 @@ 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, 5, False)
nums, convergence = analyze_convergence(loaded_numbers, 2, 5, True)
reload_source()

print(convergence)
plt.plot(nums, convergence)
plt.xlabel('n')
plt.ylabel('E/mult')
plt.ylabel('error reduction factor')
plt.title("Plot")
plt.grid(True)
plt.show()
118 changes: 64 additions & 54 deletions PlotScript/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,98 @@
import subprocess


components = {1: "Ex", 2: "Ey", 3: "Ez", 4: "Bx", 5: "By", 6: "Bz"}
nums_com = {"Ex": 0, "Ey": 1, "Ez": 2, "Bx": 3, "By": 4, "Bz": 5}
components = {1: 'Ex', 2: 'Ey', 3: 'Ez', 4: 'Bx', 5: 'By', 6: 'Bz'}
nums_com = {'Ex': 0, 'Ey': 1, 'Ez': 2, 'Bx': 3, 'By': 4, 'Bz': 5}
shift_flag = "1"

def get_plot(num, data, size_n, size_x, time):
x = 0.
X = []

def get_plot(field, data_path, size_axis, data_block_height, axis_boundaries, time):
field_num = nums_com[field] + 1
a = 0.
A = []
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=';'))
dA = (float(axis_boundaries[1]) - float(axis_boundaries[0])) / float(size_axis)

V.extend([float(value) for value in plotting[(num - 1) * (size_n[1] + 2)]])
for n in range(size_axis):
A.append(a)
a += dA
with open(data_path, 'r') as datafile:
plotting = list(csv.reader(datafile, delimiter=';'))
V.extend([float(value) for value in plotting[(field_num - 1) * (data_block_height + 2)]])

plt.plot(X, V)
plt.xlabel('X')
plt.ylabel(components[num])
plt.title(f"Plot {components[num]} (Time: {str(time)})")
plt.plot(A, V)
plt.xlabel('Coordinate')
plt.ylabel(components[field_num])
plt.title(f'Plot {components[field_num]} (Time: {str(time)})')
plt.grid(True)
plt.savefig(f"Plots/plt_{components[num]}")
plt.savefig(f'Plots/plt_{components[field_num]}')
plt.show()


def execute_cpp(field_1, field_2, field_to_plot, source_nums):
num_field_1 = nums_com[field_1]
num_field_2 = nums_com[field_2]
def select_parameters(field_E, field_B, source_nums):
num_field_E = nums_com[field_E]
num_field_B = nums_com[field_B]

if num_field_E == 0 and num_field_B == 5 or num_field_E == 2 and num_field_B == 3:
boundaries = (float(source_nums[3]), float(source_nums[4]))
data_size = source_nums[1] * source_nums[2]
axis_size = source_nums[0]
elif num_field_E == 1 and num_field_B == 5 or num_field_E == 2 and num_field_B == 4:
boundaries = (float(source_nums[5]), float(source_nums[6]))
data_size = source_nums[0] * source_nums[2]
axis_size = source_nums[1]
elif num_field_E == 0 and num_field_B == 4 or num_field_E == 1 and num_field_B == 3:
boundaries = (float(source_nums[7]), float(source_nums[8]))
data_size = source_nums[0] * source_nums[1]
axis_size = source_nums[2]
else:
print('Invalid selected fields')
exit(1)

return int(axis_size), boundaries, int(data_size)


def execute_cpp(field_E, field_B, field_to_plot):
num_field_E = nums_com[field_E]
num_field_B = nums_com[field_B]
num_field_to_plot = nums_com[field_to_plot]

print("\n" + field_to_plot + ":\n")

cpp_executable = "src/Release/sample.exe"
args = [cpp_executable, str(num_field_1), str(num_field_2), str(num_field_to_plot), shift_flag]
cpp_executable = 'src/Release/sample.exe'
args = [cpp_executable, str(num_field_E), str(num_field_B), str(num_field_to_plot), shift_flag]
try:
subprocess.run(args, check=True)
except subprocess.CalledProcessError:
print("Error when starting a C++ project")
print('Error when starting a C++ project')
except FileNotFoundError:
print("sample.exe not found")

if (num_field_1 == 0 and num_field_2 == 5 or num_field_1 == 2 and num_field_2 == 3):
arr_n = (int(source_nums[1]), int(source_nums[0]))
arr_x = (float(source_nums[4]), float(source_nums[5]))

elif (num_field_1 == 1 and num_field_2 == 5 or num_field_1 == 2 and num_field_2 == 4):
arr_n = (int(source_nums[0]), int(source_nums[1]))
arr_x = (float(source_nums[2]), float(source_nums[3]))
else:
print("Invalid selected fields")
exit(1)

file = 'OutFile.csv'
get_plot(num_field_to_plot + 1, file, arr_n, arr_x, source_nums[7])
print('sample.exe not found')


if __name__ == '__main__':
input_list = ["Ni", "Nj", "ax", "bx", "ay", "by", "Nt", "t"]
input_list = ['Ni', 'Nj', 'Nk' 'ax', 'bx', 'ay', 'by', 'az', 'bz', 'Nt', 't']

print("Update parameters? \n \
print('Update parameters? \n \
1 - Yes \n \
2 - No")
select_update = int(input("Number: ")) * (-1) + 2
2 - No')
select_update = int(input('Number: ')) * (-1) + 2
if not (select_update == 0 or select_update == 1):
print("Invalid input")
print('Invalid input')
exit(1)

if (select_update):
with open("Source.txt", "w") as file:
with open('Source.txt', 'w') as file:
for component in input_list:
file.write(input(component + ": ") + "\n")
file.write(input(component + ': ') + '\n')

with open('Source.txt', 'r') as file:
numbers = [float(line.strip()) for line in file]

execute_cpp("Ex", "Bz", "Ex", numbers)
execute_cpp("Ey", "Bz", "Ey", numbers)
execute_cpp("Ez", "By", "Ez", numbers)
execute_cpp("Ez", "Bx", "Bx", numbers)
execute_cpp("Ez", "By", "By", numbers)
execute_cpp("Ex", "Bz", "Bz", numbers)
field_E = 'Ex'
field_B = 'Bz'
field_to_plot = 'Ex'

execute_cpp(field_E, field_B, field_to_plot)
size_axis, axis_boundaries, block_size = select_parameters(field_E, field_B, numbers)
file = 'OutFile.csv'
get_plot(field_to_plot, file, size_axis, block_size, axis_boundaries, numbers[7])
Binary file modified PlotScript/src/Release/sample.exe
Binary file not shown.
31 changes: 15 additions & 16 deletions include/FDTD.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,44 @@
#include <iostream>
#include <omp.h>

namespace FDTD_Const
#include "Structures.h"

using namespace FDTDstruct;

namespace FDTDconst
{
const double C = 3e10;
const double C = 3e10; // light speed
}

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

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

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

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

enum class Component { EX, EY, EZ, BX, BY, BZ };

class FDTD
{
private:
Field Ex, Ey, Ez, Bx, By, Bz;
int Ni, Nj;
double ax, bx, ay, by, dx, dy, dt;
Parameters parameters;
double dt;

public:
FDTD(int size_grid[2], double size_x[2], double size_y[2], double _dt);
FDTD(Parameters _parameters, double _dt);

Field& get_field(Component);

void update_field(const int);
void shifted_update_field(const int);

int get_Ni() { return Ni; }
int get_Nj() { return Nj; }
//void update_field(const int);
void shifted_update_field(const int time);
};
32 changes: 32 additions & 0 deletions include/Structures.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

namespace FDTDstruct
{
enum class Component { EX, EY, EZ, BX, BY, BZ };

enum class Axis { X, Y, Z };

struct SelectedFields {
Component selected_E;
Component selected_B;
};

struct Parameters {
int Ni;
int Nj;
int Nk;

double ax;
double bx;

double ay;
double by;

double az;
double bz;

double dx = (bx - ax) / static_cast<double>(Ni);
double dy = (by - ay) / static_cast<double>(Nj);
double dz = (bz - az) / static_cast<double>(Nk);
};
}
Loading