Skip to content

Commit

Permalink
seaparate declaration and implementation in fdtd2d
Browse files Browse the repository at this point in the history
  • Loading branch information
abc0990cba committed Jun 30, 2022
1 parent eda8f20 commit d3f77f4
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 305 deletions.
4 changes: 2 additions & 2 deletions dist/test-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function testMemoryUsage() {
var used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log("The script uses approximately ".concat(Math.round(used * 100) / 100, " MB"));
}
test1D();
// test2D();
// test1D();
test2D();
// testMemoryUsage();
// void printArray(std::array<double> &arr)
// {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "napi-addon-fdtd",
"version": "3.4.7",
"version": "3.4.9",
"description": "Build N-API native addon with CMake and node-addon-api C++ wrapper. FDTD physics simulation",
"main": "dist/index.js",
"types": "index.d.ts",
Expand Down
50 changes: 27 additions & 23 deletions src/fdtd/1d-pml/fdtd-pml-1d.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#include "fdtd-pml-1d.h"
#include <iostream> // header in standard library

void FdtdPml1D::SetParams(double new_tau,
double new_omega,
std::vector<double> &new_eps,
std::vector<double> &new_mu,
std::vector<double> &new_sigma,
size_t new_src_position) {
size_t new_src_position)
{

time_step = 0;

tau = new_tau;
omega = new_omega;
src_position = new_src_position;


for (int i = 0; i < grid_size; ++i) {
for (int i = 0; i < grid_size; ++i)
{
eps[i] = new_eps[i] * eps0;
mu[i] = new_mu[i] * mu0;
sigma[i] = new_sigma[i];
Expand All @@ -26,18 +26,20 @@ void FdtdPml1D::SetParams(double new_tau,
}

// Taflove, page 292, equation 7.60a.
for (int i = 0; i < pml_width; ++i) {
for (int i = 0; i < pml_width; ++i)
{

double sigma_in_pml = std::pow( (double)i / pml_width, m) * sigma_max;
double sigma_in_pml = std::pow((double)i / pml_width, m) * sigma_max;

// Lossy electric conductivity profile.
sigma[grid_size - pml_width + i] = sigma_in_pml;
sigma[pml_width - i - 1] = sigma_in_pml;
}

for (int i = 0; i < grid_size; ++i) {
for (int i = 0; i < grid_size; ++i)
{

// Taflove, page 275, equation 7.8.
// Taflove, page 275, equation 7.8.
// Magnetic conductivity loss.
sigma_star[i] = sigma[i] * mu0 / eps0;

Expand All @@ -50,44 +52,45 @@ void FdtdPml1D::SetParams(double new_tau,

D[i] = (dt / dx) / (eps[i] + 0.5 * dt * sigma[i]);
}

}


size_t FdtdPml1D::GetCurrentTimeStep() {
size_t FdtdPml1D::GetCurrentTimeStep()
{
return time_step;
}


double FdtdPml1D::GetTau() {
double FdtdPml1D::GetTau()
{
return tau;
}


size_t FdtdPml1D::GetSourcePosition() {
size_t FdtdPml1D::GetSourcePosition()
{
return src_position;
}


void FdtdPml1D::Calculation(std::vector<double> &vect_x,
std::vector<double> &vect_ex,
std::vector<double> &vect_hy) {
std::vector<double> &vect_hy)
{

// Insert source in certain space grid.
double t = (double)time_step * dt;
double source = std::exp(-(std::pow((t0 - t) / tau, 2))) * std::cos(omega * t);
ex[src_position] += source;

for (int i = 0; i < grid_size - 1; ++i) {
for (int i = 0; i < grid_size - 1; ++i)
{
hy[i] = A[i] * hy[i] - B[i] * (ex[i + 1] - ex[i]);
}

for (int i = 1; i < grid_size - 1; ++i) {
for (int i = 1; i < grid_size - 1; ++i)
{
ex[i] = C[i] * ex[i] - D[i] * (hy[i] - hy[i - 1]);
}

for (int i = 0; i < grid_size; ++i) {

for (int i = 0; i < grid_size; ++i)
{
vect_x.push_back(i);
vect_ex.push_back(ex[i]);
vect_hy.push_back(hy[i]);
Expand All @@ -101,7 +104,8 @@ FdtdPml1D::FdtdPml1D(double new_tau,
std::vector<double> &new_eps,
std::vector<double> &new_mu,
std::vector<double> &new_sigma,
size_t new_src_position) {
size_t new_src_position)
{

SetParams(new_tau, new_omega, new_eps,
new_mu, new_sigma, new_src_position);
Expand Down
13 changes: 7 additions & 6 deletions src/fdtd/1d-pml/fdtd-pml-1d.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include <vector>
#include <algorithm> // std::fill

class FdtdPml1D {
class FdtdPml1D
{

// Speed of light in nm/fs.
const double light_spd = 299.792458;
Expand Down Expand Up @@ -88,7 +89,6 @@ class FdtdPml1D {
size_t time_step = 0;

public:

void SetParams(double new_tau,
double new_omega,
std::vector<double> &new_eps,
Expand All @@ -100,10 +100,11 @@ class FdtdPml1D {
size_t GetCurrentTimeStep();
double GetTau();
size_t GetSourcePosition();
static size_t GetGridSize() {
// return grid_size - pml_width * 2;
return grid_size;
}
static size_t GetGridSize()
{
// return grid_size - pml_width * 2;
return grid_size;
}

void Calculation(std::vector<double> &vect_x,
std::vector<double> &vect_ex,
Expand Down
Loading

0 comments on commit d3f77f4

Please sign in to comment.