diff --git a/index.js b/index.js index 9f1abe5..812ae63 100644 --- a/index.js +++ b/index.js @@ -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!'); \ No newline at end of file + + 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(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 0d89165..c8a47f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { "name": "napi-addon-fdtd", - "version": "3.1.1", + "version": "3.1.15", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "napi-addon-fdtd", - "version": "3.1.1", + "version": "3.1.15", "hasInstallScript": true, "license": "ISC", "dependencies": { "cmake-js": "^6.2.1", + "napi-addon-fdtd": "^3.1.5", "node-addon-api": "^3.1.0" } }, @@ -524,6 +525,16 @@ "mkdirp": "bin/cmd.js" } }, + "node_modules/napi-addon-fdtd": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/napi-addon-fdtd/-/napi-addon-fdtd-3.1.5.tgz", + "integrity": "sha512-l0PU1otM+Q7dvsFPGEa+DCaNJxgDAkexTfCdsDjrhpEjoVOk3hL5m4DcyKBdTMz6RP/KaYIlocI+qfA29QpKRw==", + "hasInstallScript": true, + "dependencies": { + "cmake-js": "^6.2.1", + "node-addon-api": "^3.1.0" + } + }, "node_modules/node-addon-api": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.2.1.tgz", @@ -1276,6 +1287,15 @@ "minimist": "^1.2.5" } }, + "napi-addon-fdtd": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/napi-addon-fdtd/-/napi-addon-fdtd-3.1.5.tgz", + "integrity": "sha512-l0PU1otM+Q7dvsFPGEa+DCaNJxgDAkexTfCdsDjrhpEjoVOk3hL5m4DcyKBdTMz6RP/KaYIlocI+qfA29QpKRw==", + "requires": { + "cmake-js": "^6.2.1", + "node-addon-api": "^3.1.0" + } + }, "node-addon-api": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.2.1.tgz", diff --git a/package.json b/package.json index 150377c..5c0a3fb 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -11,8 +11,8 @@ "author": "maxmaxkklosd99", "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" } - } diff --git a/src/FDTD/2D/FDTD_2D.cpp b/src/FDTD/2D/FDTD_2D.cpp index 2f3586c..17c93f2 100644 --- a/src/FDTD/2D/FDTD_2D.cpp +++ b/src/FDTD/2D/FDTD_2D.cpp @@ -45,10 +45,10 @@ 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; } } @@ -56,20 +56,20 @@ void FDTD_2D::setParams() // 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); } @@ -77,25 +77,25 @@ 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); } } @@ -114,11 +114,11 @@ void FDTD_2D::CalcNextLayer( std::vector &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++; diff --git a/src/FDTD/2D/FDTD_2D.h b/src/FDTD/2D/FDTD_2D.h index bd74c24..2c6dcbe 100644 --- a/src/FDTD/2D/FDTD_2D.h +++ b/src/FDTD/2D/FDTD_2D.h @@ -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. @@ -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; diff --git a/src/FDTD/2D_UPDATED/FDTD_2D_UPDATED.cpp b/src/FDTD/2D_UPDATED/FDTD_2D_UPDATED.cpp index 67ad995..a8c6aac 100644 --- a/src/FDTD/2D_UPDATED/FDTD_2D_UPDATED.cpp +++ b/src/FDTD/2D_UPDATED/FDTD_2D_UPDATED.cpp @@ -4,10 +4,10 @@ #include -FDTD_2D_UPDATED::FDTD_2D_UPDATED(double lambda, double tau, std::vector& Epsilon, std::vector& Omega, int source_position) - : lambda(lambda), tau(tau), source_position(source_position) +FDTD_2D_UPDATED::FDTD_2D_UPDATED(double lambda, double tau, std::vector& Epsilon, std::vector& Sigma, std::vector src) + : lambda(lambda), tau(tau) { - setParams(Epsilon, Omega); + setParams(Epsilon, Sigma, src); } //Getters @@ -32,23 +32,29 @@ int FDTD_2D_UPDATED::GetSourcePosition() } //double lambda = 1, double tau = 10, double refractive_index = 1 -void FDTD_2D_UPDATED::setParams(std::vector& Epsilon, std::vector& Omega) +void FDTD_2D_UPDATED::setParams(std::vector& Epsilon, std::vector& Sigma, std::vector src) { time_step = 0; float eaf; + // for (int i = 0; i < src.size(); i++){ + // source_position_vector.push_back(src[i]); + // } + source_position_vector = src; + for (int i = 0; i < jmax; i++) { Ex[i] = 0; Hy[i] = 0; eps[i] = Epsilon[i]; - omega[i] = Omega[i]; + sigma[i] = Sigma[i]; - eaf = dt * omega[i] / (2 * epsz * eps[i]); - ca[i] = (1 - eaf) / (1 + eaf); - cb[i] = cfl_factor / (eps[i] * (1 + eaf)); + // eaf = dt * sigma[i] / (2 * epsz * eps[i]); + // ca[i] = (1 - eaf) / (1 + eaf); + // cb[i] = cfl_factor / (eps[i] * (1 + eaf)); + cb[i] = cfl_factor / eps[i]; } @@ -60,17 +66,17 @@ size_t FDTD_2D_UPDATED::GetCurrentTick() return time_step; } -double FDTD_2D_UPDATED::SourceFunction(double time_step) -{ - double lambda_0 = 800e-6; +// double FDTD_2D_UPDATED::SourceFunction(double time_step) +// { +// double lambda_0 = 800e-6; - // Frequency - double w0 = c0 / lambda_0; - double tau = 50; - double t0 = tau * 1; +// // Frequency +// double w0 = c0 / lambda_0; +// double tau = 50; +// double t0 = tau * 1; - return exp(-pow((time_step - t0),2) / pow(tau,2)) * sin(w0 * time_step * dt); -} +// return exp(-pow((time_step - t0),2) / pow(tau,2)) * sin(w0 * time_step * dt); +// } void FDTD_2D_UPDATED::CalcNextLayer( std::vector &vectX, std::vector &vectEx, @@ -80,14 +86,18 @@ void FDTD_2D_UPDATED::CalcNextLayer( std::vector &vectX, // Calculate the Ex field. for (int k = 1; k < jmax; ++k) { - Ex[k] = ca[k] * Ex[k] + cb[k] * (Hy[k - 1] - Hy[k]); + // Ex[k] = ca[k] * Ex[k] + cb[k] * (Hy[k - 1] - Hy[k]); + Ex[k] = Ex[k] + cb[k] * (Hy[k - 1] - Hy[k]); } // Electromagnetic "hard" source. // Put a Gaussian pulse in the middle. double pulse = exp(-cfl_factor * pow((t0 - time_step) / spread, 2));//* sin(freq_in * time_step * dt); - Ex[source_position] += pulse; + for (int i = 0; i < source_position_vector.size(); i++){ + Ex[source_position_vector[i]] += pulse; + } + // Absorbing Boundary Conditions. @@ -162,10 +172,10 @@ void FDTD_2D_UPDATED::CalcNextLayer( std::vector &vectX, // #include -// FDTD_2D_UPDATED::FDTD_2D_UPDATED(double lambda, double tau, std::vector& Epsilon, std::vector& Omega, int source_position) +// FDTD_2D_UPDATED::FDTD_2D_UPDATED(double lambda, double tau, std::vector& Epsilon, std::vector& sigma, int source_position) // : lambda(lambda), tau(tau), source_position(source_position) // { -// setParams(Epsilon, Omega); +// setParams(Epsilon, sigma); // } // //Getters @@ -188,7 +198,7 @@ void FDTD_2D_UPDATED::CalcNextLayer( std::vector &vectX, // } // //double lambda = 1, double tau = 10, double refractive_index = 1 -// void FDTD_2D_UPDATED::setParams(std::vector& Epsilon, std::vector& Omega) +// void FDTD_2D_UPDATED::setParams(std::vector& Epsilon, std::vector& sigma) // { // time_step = 0; @@ -214,9 +224,9 @@ void FDTD_2D_UPDATED::CalcNextLayer( std::vector &vectX, // // eps[i] = refractive_index; // eps[i] = Epsilon[i]; -// omega[i] = Omega[i]; +// sigma[i] = sigma[i]; -// eaf = dt * omega[i] / (2 * eps[i]); +// eaf = dt * sigma[i] / (2 * eps[i]); // ca[i] = (1.0 - eaf) / (1.0 + eaf); // cb[i] = cfl_factor / (eps[i] * (1 + eaf)); diff --git a/src/FDTD/2D_UPDATED/FDTD_2D_UPDATED.h b/src/FDTD/2D_UPDATED/FDTD_2D_UPDATED.h index 6fa521a..cc67c21 100644 --- a/src/FDTD/2D_UPDATED/FDTD_2D_UPDATED.h +++ b/src/FDTD/2D_UPDATED/FDTD_2D_UPDATED.h @@ -1,28 +1,25 @@ -#include -#include #include +#include + #include using namespace std; class FDTD_2D_UPDATED { size_t time_step; - const double PI = 3.141592653; + const double PI = 3.141592653; - // Grid size static const size_t jmax = 400; - double ca[jmax]; double cb[jmax]; - // epsilon - electric permitivitty double eps[jmax]; - // omega - conductivity - double omega[jmax]; + // sigma - conductivity + double sigma[jmax]; // Magnetic field strength. double Hy[jmax]; @@ -39,19 +36,15 @@ class FDTD_2D_UPDATED // Light speed. double c0 = 3e8; - int source_position; + std::vector source_position_vector; int nmax = 400; // Coefficient corresponds Courant Condition. float cfl_factor = 0.5; - - vector boundary_low = {0, 0}; - vector boundary_high = {0, 0}; - - - + vector boundary_low = {0, 0}; + vector boundary_high = {0, 0}; // Frequency in MHz. double freq_in = 700e6; @@ -62,13 +55,11 @@ class FDTD_2D_UPDATED // Grid steps. double dx = lambda0 / 25; - double dt = dx / (2*c0); + double dt = dx / (2 * c0); double t0 = 50; int spread = 10; - - // Moor`s boundary condition. // void BoundaryConditionsFirst(); @@ -78,33 +69,32 @@ class FDTD_2D_UPDATED // Updating values for new time layer. // void Calculation(); - - - public: - FDTD_2D_UPDATED(double lambda, double tau, std::vector& Epsilon, std::vector& Omega, int source_position); + // FDTD_2D_UPDATED(double lambda, double tau, std::vector& Epsilon, + // std::vector& Sigma, int source_position); + FDTD_2D_UPDATED(double lambda, double tau, std::vector &Epsilon, + std::vector &Sigma, std::vector src); - void setParams(std::vector& Epsilon, std::vector& Omega); + void setParams(std::vector &Epsilon, std::vector &Sigma, + std::vector src); // Getters. size_t GetNx(); double GetLambda(); double GetTau(); int GetSourcePosition(); - // double GetRefractiveIndex(); + size_t GetCurrentTick(); // Setters. - double SourceFunction(double time_step); + // double SourceFunction(double time_step); void setLambda(double l) { lambda = l; } void setTau(double t) { tau = t; } - void setSourcePosition(int new_source_pos) { source_position = new_source_pos; } - // void setRefractiveIndex(double n) { refractive_index = n; } - - size_t GetCurrentTick(); - - //start calculation - void CalcNextLayer( std::vector &vectX, - std::vector &vectEx, - std::vector &vectHy); + // void setSourcePosition(int new_source_pos) + // { + // source_position = new_source_pos; + // } + + // Start Calculation. + void CalcNextLayer(std::vector &vectX, std::vector &vectEx, + std::vector &vectHy); }; - diff --git a/src/index.cpp b/src/index.cpp index d33b40e..9b30283 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -32,7 +32,6 @@ // Difraction FDTD-3D. Napi::Value GetData3D(const Napi::CallbackInfo &info) { - Napi::Env env = info.Env(); // 0 - conditions - (lambda, beamsize) @@ -41,7 +40,7 @@ Napi::Value GetData3D(const Napi::CallbackInfo &info) { // 3 - refractive index matrix(flatten) size (for 2x2 is 2). // 4 - data return type('Ez' = 0 | 'Hy' = 1 |'Hx' = 2 |'Energy' = 3) const Napi::Array input_array_condition = info[0].As(); - + // Reload params checker. bool reload_check = static_cast(info[1].As()); @@ -97,11 +96,10 @@ Napi::Value GetData3D(const Napi::CallbackInfo &info) { } } - static FDTD_3D_DIFRACTION fdtd_3D = FDTD_3D_DIFRACTION(lambda, beamsize, refr_index_matrix); - if ((fdtd_3D.getLambda() != lambda) || (fdtd_3D.getBeamsize() != beamsize) - || reload_check) { + if ((fdtd_3D.getLambda() != lambda) || (fdtd_3D.getBeamsize() != beamsize) || + reload_check) { fdtd_3D.setLambda(lambda); fdtd_3D.setBeamsize(beamsize); fdtd_3D.setParams(refr_index_matrix); @@ -146,24 +144,45 @@ Napi::Value GetData3D(const Napi::CallbackInfo &info) { data.Set("col", Ny); data.Set("currentTick", fdtd_3D.getCurrentTick()); + double max; + double min; + switch (data_return_type) { case 0: data.Set("dataEz", js_data_Ez); + + max = *std::max_element(std::begin(vect_Ez), std::end(vect_Ez)); + min = *std::min_element(std::begin(vect_Ez), std::end(vect_Ez)); break; case 1: data.Set("dataHy", js_data_Hy); + + max = *std::max_element(std::begin(vect_Hy), std::end(vect_Hy)); + min = *std::min_element(std::begin(vect_Hy), std::end(vect_Hy)); break; case 2: - data.Set("dataHx", js_data_Hy); + data.Set("dataHx", js_data_Hx); + + max = *std::max_element(std::begin(vect_Hx), std::end(vect_Hx)); + min = *std::min_element(std::begin(vect_Hx), std::end(vect_Hx)); break; case 3: data.Set("dataEnergy", js_data_Energy); + + max = *std::max_element(std::begin(vect_Energy), std::end(vect_Energy)); + min = *std::min_element(std::begin(vect_Energy), std::end(vect_Energy)); break; default: + max = *std::max_element(std::begin(vect_Ez), std::end(vect_Ez)); + min = *std::min_element(std::begin(vect_Ez), std::end(vect_Ez)); break; - } + + } + // Fill max and min values. + data.Set("max", max); + data.Set("min", min); return data; } @@ -268,99 +287,103 @@ Napi::Value GetData2D(const Napi::CallbackInfo &info) { // 2 - refractive index vector. // 3 - refractive index vector size. // 4 - relative source position 0..1 - // 5 - conductivity vector. + // 5 - conductivity vector (sigma). const Napi::Array input_array_condition = info[0].As(); - + // Grid size. size_t Nx = 400; // Temporary matrix. std::vector tmp_vector = {}; - std::vector tmp_vector_omega = {}; - + std::vector tmp_vector_sigma = {}; // Reload params checker. bool reload_check = static_cast(info[1].As()); - // Refraction index matrix transformation JS -> C++. const Napi::Array epsilon_vector_js = info[2].As(); - // Omega matrix transformation JS -> C++. - const Napi::Array omega_vector_js = info[5].As(); + // sigma matrix transformation JS -> C++. + const Napi::Array sigma_vector_js = info[5].As(); // Must be even. int epsilon_vector_size = static_cast(info[3].As()); // Transform input JS data to C++. for (int i = 0; i < epsilon_vector_size; i++) { - tmp_vector.push_back( - (float)epsilon_vector_js[i].As()); + tmp_vector.push_back((float)epsilon_vector_js[i].As()); } - // Comductivity(omega). + // Comductivity(sigma). for (int i = 0; i < epsilon_vector_size; i++) { - tmp_vector_omega.push_back( - (float)omega_vector_js[i].As()); + tmp_vector_sigma.push_back((float)sigma_vector_js[i].As()); } + // Transform source position array(JS -> C++). + const Napi::Array relative_source_position_array = info[4].As(); - float relative_source_position = static_cast(info[4].As()); - - // Transform relative source position to absolute. - int source_position = (int)(relative_source_position * Nx); + std::vector source_position_vector = {}; + for (int i = 0; i < relative_source_position_array.Length(); ++i) { + float source_position_relative = static_cast( + relative_source_position_array[i].As()); - // Must be Repaired!!!!!! - if (source_position == 0) source_position = 1; + // Transform relative source position to absolute. + source_position_vector.push_back(int(source_position_relative * Nx)); + } // Matrix size coefficient. size_t coeff = Nx / epsilon_vector_size; // Filling epsilon matrix. std::vector epsilon_vector = {}; - for (int i = 0; i < epsilon_vector_size; i++) { - for (int k = 0; k < coeff; k++) { - epsilon_vector.push_back(tmp_vector[i]); - } + for (int i = 0; i < epsilon_vector_size; i++) { + for (int k = 0; k < coeff; k++) { + epsilon_vector.push_back(tmp_vector[i]); + } } - // Filling conductivity(omega) matrix. - std::vector omega_vector = {}; - for (int i = 0; i < epsilon_vector_size; i++) { - for (int k = 0; k < coeff; k++) { - omega_vector.push_back(tmp_vector_omega[i]); - } + // Filling conductivity(sigma) matrix. + std::vector sigma_vector = {}; + for (int i = 0; i < epsilon_vector_size; i++) { + for (int k = 0; k < coeff; k++) { + sigma_vector.push_back(tmp_vector_sigma[i]); + } } - - int nil = 0; //!!!! MUST BE REFACTORED !!!!!!! - float lambda = (float)input_array_condition[nil].As(); - float tau = (float)(input_array_condition[1].As()); - float refractive_index = (float)(input_array_condition[2].As()); + int nil = 0; // ! MUST BE REFACTORED + // float lambda = (float)input_array_condition[nil].As(); + // float tau = (float)(input_array_condition[1].As()); + // float refractive_index = + // (float)(input_array_condition[2].As()); // Containers to storage coordinates. vector vect_X = {}; vector vect_Ex = {}; vector vect_Hy = {}; + double lambda = 1.0; + double tau = 10.0; + double refractive_index = 1.0; // Using static to save save data for different function call. - // static FDTD_2D fdtd = FDTD_2D(lambda, tau, refractive_index); - static FDTD_2D_UPDATED fdtd = FDTD_2D_UPDATED(lambda, tau, epsilon_vector, omega_vector, source_position); - - if ((fdtd.GetLambda() != lambda) || (fdtd.GetTau() != tau) - || (fdtd.GetSourcePosition() != source_position) - || - reload_check) { - fdtd.setLambda(lambda); + static FDTD_2D fdtd = FDTD_2D(lambda, tau, refractive_index); + // static FDTD_2D_UPDATED fdtd = FDTD_2D_UPDATED(lambda, tau, epsilon_vector, + // sigma_vector, source_position_vector); + + if ((fdtd.GetLambda() != lambda) || + (fdtd.GetTau() != tau) + // || (fdtd.GetSourcePosition() != src) + || reload_check) { + fdtd.setLambda(lambda); fdtd.setTau(tau); // fdtd.setRefractiveIndex(refractive_index) - fdtd.setSourcePosition(source_position); - fdtd.setParams(epsilon_vector, omega_vector); + // fdtd.setSourcePosition(src); + // fdtd.setParams(epsilon_vector, sigma_vector, source_position_vector); + fdtd.setParams(); } - - fdtd.CalcNextLayer(vect_X, vect_Ex, vect_Hy); - // size_t Nx = vect_X.size(); + fdtd.CalcNextLayer(vect_X, vect_Ex, vect_Hy); + + Nx = vect_X.size(); // Creating JS data for response. Napi::Array js_data_X = Napi::Array::New(env, Nx); @@ -373,17 +396,28 @@ Napi::Value GetData2D(const Napi::CallbackInfo &info) { js_data_Hy[i] = Napi::Number::New(env, vect_Hy[i]); } + double maxEx = *std::max_element(std::begin(vect_Ex), std::end(vect_Ex)); + double minEx = *std::min_element(std::begin(vect_Ex), std::end(vect_Ex)); + + double maxHy = *std::max_element(std::begin(vect_Hy), std::end(vect_Hy)); + double minHy = *std::min_element(std::begin(vect_Hy), std::end(vect_Hy)); + + Napi::Object data = Napi::Array::New(env); data.Set("dataX", js_data_X); data.Set("dataEx", js_data_Ex); data.Set("dataHy", js_data_Hy); data.Set("col", Nx); data.Set("currentTick", fdtd.GetCurrentTick()); + + data.Set("maxEx", maxEx); + data.Set("minEx", minEx); + data.Set("maxHy", maxHy); + data.Set("minHy", minHy); return data; } - // Callback method when module is registered with Node.js. Napi::Object Init(Napi::Env env, Napi::Object exports) { exports.Set(Napi::String::New(env, "getData2D"),