Skip to content

Commit

Permalink
added min and max values
Browse files Browse the repository at this point in the history
  • Loading branch information
abc0990cba committed Apr 9, 2022
1 parent 1fb8f86 commit ee7c04b
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 175 deletions.
70 changes: 43 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
var addon = require("./build/Release/napi-addon-fdtd.node");


module.exports = addon;

const fs = require("fs");
const path = require("path");

let data = addon.getData2D([1, 10, 1], true, [1, 1.2], 2, 0.4, [0, 0.04]);
const test1D = () => {
const condition = [1, 10, 1];
const eps = [1, 1.2];
const sigma = [0, 0.04];
const srcPosition = [0.4, 0.8];

for (let j = 0; j < 150; ++j) {
data = addon.getData2D([1, 10, 1], false, [1, 1.2], 2, 0.4, [0, 0.04]);
let data = addon.getData2D(condition, true, eps, 2, srcPosition, sigma);

}
for (let j = 0; j < 150; ++j) {
data = addon.getData2D(condition, false, eps, 2, srcPosition, sigma);
}

const fs = require("fs");
const path = require("path");
fs.writeFileSync(path.resolve(__dirname, "tmp.txt"), JSON.stringify(data.dataHy), function (err) {
if (err) {
return console.log(err);
fs.writeFileSync(
path.resolve(__dirname, "tmp.txt"),
JSON.stringify(data.dataHy),
function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
}
); // Orfs.writeFileSync('/tmp/test-sync', 'Hey there!');
};

const test2D = () => {
let data = addon.getData3D([1, 10], true, [1, 2, 1, 1], 2, 0);
for (let j = 0; j < 150; ++j) {
data = addon.getData3D([1, 10], false, [1, 2, 1, 1], 2, 0);
}
console.log("The file was saved!");
}); // Orfs.writeFileSync('/tmp/test-sync', 'Hey there!');


// let data = addon.getData3D([1, 10], true, [1,2,1,1], 2, 0);
// for (let j = 0; j < 150; ++j) {
// data = addon.getData3D([1, 10], false, [1,2,1,1], 2, 0);
// }

// const fs = require("fs");
// const path = require("path");
// fs.writeFileSync(path.resolve(__dirname, "tmp.txt"), JSON.stringify(data.dataY), function (err) {
// if (err) {
// return console.log(err);
// }
// console.log("The file was saved!");
// }); // Orfs.writeFileSync('/tmp/test-sync', 'Hey there!');

fs.writeFileSync(
path.resolve(__dirname, "tmp.txt"),
JSON.stringify(data.dataY),
function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
}
); // Orfs.writeFileSync('/tmp/test-sync', 'Hey there!');
};


test1D();
test2D();
24 changes: 22 additions & 2 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "napi-addon-fdtd",
"version": "3.1.2",
"version": "3.1.15",
"description": "Build N-API native addon with CMake and node-addon-api C++ wrapper. FDTD physics simulation",
"main": "index.js",
"scripts": {
Expand All @@ -11,8 +11,8 @@
"author": "maxmaxkklosd99<[email protected]>",
"license": "ISC",
"dependencies": {
"node-addon-api": "^3.1.0",
"cmake-js": "^6.2.1"
"cmake-js": "^6.2.1",
"napi-addon-fdtd": "^3.1.5",
"node-addon-api": "^3.1.0"
}

}
48 changes: 24 additions & 24 deletions src/FDTD/2D/FDTD_2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,57 +45,57 @@ void FDTD_2D::setParams()

for (int i = 0; i < Nx; i++)
{
Ex1[i] = 1e-8;
Ex2[i] = 1e-8;
Hy1[i] = 1e-8;
Hy2[i] = 1e-8;
Ex_prev[i] = 1e-8;
Ex[i] = 1e-8;
Hy_prev[i] = 1e-8;
Hy[i] = 1e-8;
eps[i] = refractive_index;
}
}

// Moor`s boundary condition.
void FDTD_2D::BoundaryConditionsFirst()
{
Hy2[0] = Hy1[1] + (dt / eps[1] - dx) / (dt / eps[1] + dx) * (Hy2[1] - Hy1[0]);
Hy[0] = Hy_prev[1] + (dt / eps[1] - dx) / (dt / eps[1] + dx) * (Hy[1] - Hy_prev[0]);

Ex2[0] = Ex1[1] + (dt / eps[1] - dx) / (dt / eps[1] + dx) * (Ex2[1] - Ex1[0]);
Ex[0] = Ex_prev[1] + (dt / eps[1] - dx) / (dt / eps[1] + dx) * (Ex[1] - Ex_prev[0]);
}

//Moor`s boundary condition
void FDTD_2D::BoundaryConditionsSecond()
{
Hy2[Nx - 1] =
Hy1[Nx - 2] + (dt / eps[Nx - 2] - dx) * (Hy2[Nx - 2] - Hy1[Nx - 1]) /
Hy[Nx - 1] =
Hy_prev[Nx - 2] + (dt / eps[Nx - 2] - dx) * (Hy[Nx - 2] - Hy_prev[Nx - 1]) /
(dt / eps[Nx - 2] + dx);

Ex2[Nx - 1] =
Ex1[Nx - 2] + (dt / eps[Nx - 2] - dx) * (Ex2[Nx - 2] - Ex1[Nx - 1]) /
Ex[Nx - 1] =
Ex_prev[Nx - 2] + (dt / eps[Nx - 2] - dx) * (Ex[Nx - 2] - Ex_prev[Nx - 1]) /
(dt / eps[Nx - 2] + dx);
}

void FDTD_2D::Calculation()
{
for (int i = 1; i <= Ny - 2; i++)
{
Hy2[i] =
Hy1[i] * dt / dx - (Ex1[i] - Ex1[i - 1]);
Hy[i] =
Hy_prev[i] * dt / dx - (Ex_prev[i] - Ex_prev[i - 1]);

Ex2[i - 1] =
Ex1[i - 1] - (Hy2[i] - Hy2[i - 1]) * dt / (eps[i - 1] * dx);
Ex[i - 1] =
Ex_prev[i - 1] - (Hy[i] - Hy[i - 1]) * dt / (eps[i - 1] * dx);
}

Ex1[Ny - 1] =
Ex_prev[Ny - 1] =
std::exp(aa1 * (tMax - dt * ticks) * (dt * ticks - tMax)) * std::sin(2 * PI * dt * ticks);

Hy1[Ny - 1] = eps[Ny - 1] * Ex1[Ny - 1];
Hy_prev[Ny - 1] = eps[Ny - 1] * Ex_prev[Ny - 1];

for (int i = Ny; i < Nx; i++)
{
Hy2[i] =
Hy1[i] - (Ex1[i] - Ex1[i - 1]) * dt / dx;
Hy[i] =
Hy_prev[i] - (Ex_prev[i] - Ex_prev[i - 1]) * dt / dx;

Ex2[i - 1] =
Ex1[i - 1] - (Hy2[i] - Hy2[i - 1]) * dt / (eps[i - 1] * dx);
Ex[i - 1] =
Ex_prev[i - 1] - (Hy[i] - Hy[i - 1]) * dt / (eps[i - 1] * dx);
}
}

Expand All @@ -114,11 +114,11 @@ void FDTD_2D::CalcNextLayer( std::vector<double> &vectX,

for (int i = 0; i < Nx; i++)
{
Hy1[i] = Hy2[i];
Ex1[i] = Ex2[i];
Hy_prev[i] = Hy[i];
Ex_prev[i] = Ex[i];
vectX.push_back(dx * lambda * (i - 1));
vectEx.push_back(Ex1[i]);
vectHy.push_back(Hy1[i]);
vectEx.push_back(Ex_prev[i]);
vectHy.push_back(Hy_prev[i]);
}

ticks++;
Expand Down
10 changes: 5 additions & 5 deletions src/FDTD/2D/FDTD_2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class FDTD_2D
double tMax; //??

// Grid size
static const size_t Nx = 2001;
static const size_t Nx = 800;
static const size_t Ny = 501;

// Grid steps.
Expand All @@ -25,13 +25,13 @@ class FDTD_2D
double eps[Nx];

//magnetic field strength
double Hy1[Nx];
double Hy2[Nx];
double Hy[Nx];
double Hy_prev[Nx];


//electric field strength
double Ex1[Nx];
double Ex2[Nx];
double Ex[Nx];
double Ex_prev[Nx];

// lambda - wave length
double lambda;
Expand Down
Loading

0 comments on commit ee7c04b

Please sign in to comment.