From 9a77bd5a918ba6112b6e206b511ed16aacda9fbf Mon Sep 17 00:00:00 2001 From: max99xam Date: Thu, 12 May 2022 14:39:03 +0300 Subject: [PATCH] update indexes in pml calcualting --- CMakeLists.txt | 4 +- dist/test-addon.js | 4 +- dist/tmp.txt | 2 +- index.d.ts | 11 ++- package-lock.json | 4 +- package.json | 2 +- src/{FDTD => fdtd}/1d-pml/fdtd-pml-1d.h | 11 +-- src/{FDTD => fdtd}/2d-pml/fdtd-pml-2d.h | 25 ++++--- src/globals.h | 5 -- src/index.cpp | 90 +++++++++++-------------- test-addon.ts | 4 +- tsconfig.json | 2 +- 12 files changed, 80 insertions(+), 84 deletions(-) rename src/{FDTD => fdtd}/1d-pml/fdtd-pml-1d.h (93%) rename src/{FDTD => fdtd}/2d-pml/fdtd-pml-2d.h (96%) delete mode 100644 src/globals.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 87fa328..807621b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,8 @@ include_directories(${CMAKE_JS_INC}) # Declare the location of the source files file(GLOB SOURCE_FILES "src/*.cpp" "src/*.h" - "src/FDTD/2d-pml/*.cpp" "src/FDTD/2d-pml/*.h" - "src/FDTD/1d-pml/*.cpp" "src/FDTD/1d-pml/*.h" + "src/fdtd/2d-pml/*.cpp" "src/fdtd/2d-pml/*.h" + "src/fdtd/1d-pml/*.cpp" "src/fdtd/1d-pml/*.h" ) # This line will tell CMake that we're building a shared library diff --git a/dist/test-addon.js b/dist/test-addon.js index bd3b4f8..b12951d 100644 --- a/dist/test-addon.js +++ b/dist/test-addon.js @@ -39,7 +39,7 @@ var test2D = function () { var reload = true; var data = index_1.default.getData2D(condition, reload, materialMatrix, matrixSize, eps, mu, sigma, returnDataNumber, srcPosition); reload = false; - for (var j = 0; j < 200; ++j) { + for (var j = 0; j < 50; ++j) { data = index_1.default.getData2D(condition, reload, materialMatrix, matrixSize, eps, mu, sigma, returnDataNumber, srcPosition); } console.log(data); @@ -63,5 +63,5 @@ function testMemoryUsage() { console.log("The script uses approximately ".concat(Math.round(used * 100) / 100, " MB")); } test1D(); -test2D(); +// test2D(); // testMemoryUsage(); diff --git a/dist/tmp.txt b/dist/tmp.txt index 5627424..3c1028d 100644 --- a/dist/tmp.txt +++ b/dist/tmp.txt @@ -1 +1 @@ -[-9.39396425566878e-74,3.1075656233251706e-57,4.498848239990849e-20,0.7421015235618883,1.4189518370070469,1.977043530626149,2.3593384653342016,2.5413770376992044,2.491565159736345,2.22429728711163,1.743080203180158,1.1001924609025715,0.3331211947405604,-0.48045963631556954,-1.281491695182893,-1.9884397470534967,-2.5415478317646474,-2.880324301186735,-2.9676997536129837,-2.7847111745070134,-2.3351390496496105,-1.6499160476906907,-0.7794766297807355,0.20386670568417442,1.2134024226122526,2.1601977577523526,2.9462951724156925,3.500186602615365,3.745150250985643,3.658631181081437,3.2140552850208555,2.454026202725322,1.4162892408044585,0.2007685361032705,-1.098106475911226,-2.355223509148887,-3.448665382092062,-4.27476805137157,-4.726529926917601,-4.765602627556546,-4.341171875797549,-3.5029474614161136,-2.2805893834837456,-0.8020975184173558,0.827004322410102,2.4409924324968113,3.8950656295147486,5.0417114756725665,5.756610613822861,5.9591025634755175,5.60754778541156,4.710903127851519,3.346713795777114,1.6055677549617273,-0.3295618791449513,-2.3163694450942973,-4.134707856162988,-5.631787535809645,-6.64303125935203,-7.057693382422351,-6.828153980042947,-5.949979867877014,-4.4869058773677395,-2.580820112983587,-0.3699114882457595,1.913421222957858,4.06854311185637,5.897645665244169,7.20991318446944,7.859070284233828,7.795672326575035,7.008126045110368,5.560890492089182,3.572098302491793,1.2277652292243761,-1.2622483147989967,-3.6598357843442897,-5.744397632081171,-7.312035543394781,-8.216560913072701,-8.363519582537188,-7.739387809753721,-6.3941700397610886,-4.455604659546936,-2.0989191400029306,2.0989191400029306,4.455604659546936,6.3941700397610886,7.739387809753721,8.363519582537188,8.216560913072701,7.312035543394781,5.744397632081171,3.6598357843442897,1.2622483147989967,-1.2277652292243761,-3.572098302491793,-5.560890492089182,-7.008126045110368,-7.794119538706236,-7.8495218640062365,-7.188827934318461,-5.878477729249168,-4.060240004434826,-1.9042059569098657,0.3741170949177821,2.5705883434069863,4.474359176395259,5.926851434020128,6.794834070087455,7.024800666970978,6.601717490277381,5.5953555589315025,4.103594095869217,2.291732054046535,0.32384638582820835,-1.6001244084977326,-3.3183264078776364,-4.6657370430243965,-5.546162777804934,-5.881526679911601,-5.676594058014988,-4.956810639557911,-3.8239315965726473,-2.384583367499495,-0.7985280947392004,0.79641837205699,2.2395669269039082,3.4188810588414222,4.225654004311862,4.615986295671221,4.5630333687957485,4.104424965064013,3.2933281934247436,2.2309436543520915,1.0206613578944126,-0.21266001656780875,-1.3594278194629672,-2.316413970234189,-3.008515662127692,-3.387890400586359,-3.436792477963944,-3.1740357819109826,-2.6379024221550607,-1.901066041445712,-1.0355561755238334,-0.13837005360103227,0.7173491462951687,1.4440432441503344,1.997244910823135,2.3243488015699056,2.4258809031020663,2.2935970026955883,1.9714028076793162,1.4877754982896167,0.9130370848732287,0.29310185706727154,-0.2998217334066292,-0.8244152553688672,-1.229205046578654,-1.4936336061684077,-1.6001599002343205,-1.554102544262964,-1.3724905523314423,-1.080279936667202,-0.7188506893290195,-0.31920789095358526,0.06882097245763397,0.42289363260830276,0.7001011476780283,0.8967635939589469,0.98684791300276,0.9872744934362394,0.8914052595020254,0.7306636809432207,0.5138751566015052,0.2757772055337032,0.033299461333931885,-0.18735777924829844,-0.36845461030966364,-0.5006642487638799,-0.5701450240853309,-0.5867272984911475,-0.5418188505109167,-0.4603623729371087,-0.33789413882647434,-0.20587280354035697,-0.06212748348556549,0.06549665208314027,0.1778462470884217,0.25913171630663884,0.3083927502606358,0.3261508207043803,0.30835777302169975,0.2707407804950766,0.20553962289316507,0.13761223249113266,0.056861421341490165,-0.011891480464539514,-0.07744511801681248,-0.1242798547967973,-0.15533718866731216,-0.17034308630755926,-0.1634965581218513,-0.14961633188532397,-0.11551787148568599,-0.08406640257469197,-0.04102245657983675,-0.005679146360368422,0.028476227670905022,0.05640906499731489,0.07113403294210274,0.08490083611465628,0.0800165558078738,0.07757588356391662,0.06138500760662793,0.0451660848271237,0.028387387899476667,0.005443346463993276,-0.005715493316742006,-0.024637391047762174,-0.03111376029377172,-0.03643197687814111,-0.04131376907465045,-0.0328927154513693,-0.03331990995549233,-0.023128433856885466,-0.012546700972476928,-0.01023176957660304,0.004205606672021854,0.009215266047994854,0.008303107421544198,0.019167935994999837,0.021085250150978074,0.00954842022758919,0.0015527878687986733,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] \ No newline at end of file +[0.26513420337100985,0.24756296069654327,0.21136554288739606,0.1610163350276805,0.09580362712589234,0.021036053019497625,-0.059242552477563264,-0.1417622574561465,-0.2176287882990065,-0.2831155064570815,-0.33212578810170446,-0.3535238578488235,-0.3545382086038569,-0.3138360967246292,-0.25341622298989686,-0.15320885604065942,-0.034382891789368625,0.0985594603637065,0.24718657261500743,0.3707868542778436,0.4936822828926011,0.5649472843678022,0.5992739994636243,0.5803767936619343,0.4964586420918576,0.35923934426166915,0.17905467182675405,-0.052467397328931714,-0.2968624556846812,-0.5234235768290945,-0.7322164688120193,-0.8914052595020254,-0.9872744934362394,-0.98684791300276,-0.8967635939589469,-0.7001011476780283,-0.42289363260830276,-0.06882097245763397,0.31920789095358526,0.7188506893290195,1.080279936667202,1.3724905523314423,1.554102544262964,1.6001599002343205,1.4936336061684077,1.229205046578654,0.8244152553688672,0.2998217334066292,-0.29310185706727154,-0.9130370848732287,-1.4877754982896167,-1.9714028076793162,-2.2935970026955883,-2.4258809031020663,-2.3243488015699056,-1.997244910823135,-1.4440432441503344,-0.7173491462951687,0.13837005360103227,1.0355561755238334,1.901066041445712,2.6379024221550607,3.1740357819109826,3.436792477963944,3.387890400586359,3.008515662127692,2.316413970234189,1.3594278194629672,0.21266001656780875,-1.0206613578944126,-2.2309436543520915,-3.2933281934247436,-4.104424965064013,-4.5630333687957485,-4.615986295671221,-4.225654004311862,-3.4188810588414222,-2.2395669269039082,-0.79641837205699,0.7985280947392004,2.384583367499495,3.8239315965726473,4.956810639557911,5.676594058014988,5.881526679911601,5.546162777804934,4.6657370430243965,3.3183264078776364,1.6001244084977326,-0.32384638582820835,-2.291732054046535,-4.103594095869217,-5.5953555589315025,-6.601717490277381,-7.024800666970978,-6.794834070087455,-5.926851434020128,-4.474359176395259,-2.5705883434069863,-0.3741170949177821,1.9042059569098657,4.060240004434826,5.878477729249168,7.188827934318461,7.8495218640062365,7.794119538706236,7.008126045110368,5.560890492089182,3.572098302491793,1.2277652292243761,-1.2622483147989967,-3.6598357843442897,-5.744397632081171,-7.312035543394781,-8.216560913072701,-8.363519582537188,-7.739387809753721,-6.3941700397610886,-4.455604659546936,-2.0989191400029306,2.0989191400029306,4.455604659546936,6.3941700397610886,7.739387809753721,8.363519582537188,8.216560913072701,7.312035543394781,5.744397632081171,3.6598357843442897,1.2622483147989967,-1.2277652292243761,-3.572098302491793,-5.560890492089182,-7.008126045110368,-7.794119538706236,-7.8495218640062365,-7.188827934318461,-5.878477729249168,-4.060240004434826,-1.9042059569098657,0.3741170949177821,2.5705883434069863,4.474359176395259,5.926851434020128,6.794834070087455,7.024800666970978,6.601717490277381,5.5953555589315025,4.103594095869217,2.291732054046535,0.32384638582820835,-1.6001244084977326,-3.3183264078776364,-4.6657370430243965,-5.546162777804934,-5.881526679911601,-5.676594058014988,-4.956810639557911,-3.8239315965726473,-2.384583367499495,-0.7985280947392004,0.79641837205699,2.2395669269039082,3.4188810588414222,4.225654004311862,4.615986295671221,4.5630333687957485,4.104424965064013,3.2933281934247436,2.2309436543520915,1.0206613578944126,-0.21266001656780875,-1.3594278194629672,-2.316413970234189,-3.008515662127692,-3.387890400586359,-3.436792477963944,-3.1740357819109826,-2.6379024221550607,-1.901066041445712,-1.0355561755238334,-0.13837005360103227,0.7173491462951687,1.4440432441503344,1.997244910823135,2.3243488015699056,2.4258809031020663,2.2935970026955883,1.9714028076793162,1.4877754982896167,0.9130370848732287,0.29310185706727154,-0.2998217334066292,-0.8244152553688672,-1.229205046578654,-1.4936336061684077,-1.6001599002343205,-1.554102544262964,-1.3724905523314423,-1.080279936667202,-0.7188506893290195,-0.31920789095358526,0.06882097245763397,0.42289363260830276,0.7001011476780283,0.8967635939589469,0.98684791300276,0.9872744934362394,0.8914052595020254,0.7306636809432207,0.5138751566015052,0.2757772055337032,0.033299461333931885,-0.18735777924829844,-0.36845461030966364,-0.5006642487638799,-0.5701450240853309,-0.5867272984911475,-0.5418188505109167,-0.4603623729371087,-0.33789413882647434,-0.20587280354035697,-0.06212748348556549,0.06549665208314027,0.1778462470884217,0.25913171630663884,0.3083927502606358,0.3261508207043803,0.30835777302169975,0.2707407804950766,0.20553962289316507,0.13761223249113266,0.056861421341490165,-0.011891480464539514,-0.07744511801681248,-0.1242798547967973,-0.15533718866731216,-0.17034308630755926,-0.1634965581218513,-0.14961633188532397,-0.11551787148568599,-0.08406640257469197,-0.04102245657983675,-0.005679146360368422,0.028476227670905022,0.05640906499731489,0.07113403294210274,0.08490083611465628,0.0800165558078738,0.07757588356391662,0.06138500760662793,0.0451660848271237,0.028387387899476667,0.005443346463993276,-0.005715493316742006,-0.024637391047762174,-0.03111376029377172,-0.03643197687814111,-0.04131376907465045,-0.0328927154513693,-0.03331990995549233,-0.023128433856885466,-0.012546700972476928,-0.01023176957660304,0.004205606672021854,0.009215266047994854,0.008303107421544198,0.019167935994999837,0.021085250150978074,0.00954842022758919,0.0015527878687986733,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index 1749ffe..ce6d81b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,7 +9,16 @@ type GetData2D = ( sigma: number[], dataToReturn: number, srcPositionRelativeSet: number[] -) => any +) => { + dataX: number[], + dataY: number[], + dataEz: number[], + rows: number, + cols: number, + timeStep: number, + max: number, + min: number, +} type GetData1D = ( condition: number[], diff --git a/package-lock.json b/package-lock.json index 21c0e79..3703f76 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "napi-addon-fdtd", - "version": "3.2.16", + "version": "3.3.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "napi-addon-fdtd", - "version": "3.2.16", + "version": "3.3.2", "hasInstallScript": true, "license": "ISC", "dependencies": { diff --git a/package.json b/package.json index 04306e6..633879a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "napi-addon-fdtd", - "version": "3.2.16", + "version": "3.3.2", "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", diff --git a/src/FDTD/1d-pml/fdtd-pml-1d.h b/src/fdtd/1d-pml/fdtd-pml-1d.h similarity index 93% rename from src/FDTD/1d-pml/fdtd-pml-1d.h rename to src/fdtd/1d-pml/fdtd-pml-1d.h index 6fbe51b..5aa2a23 100644 --- a/src/FDTD/1d-pml/fdtd-pml-1d.h +++ b/src/fdtd/1d-pml/fdtd-pml-1d.h @@ -41,7 +41,7 @@ class FdtdPml1D { const double tau = 8.0; // Position index of source. - size_t src_position = 85; + size_t src_position = 120; // 1D grid size. static const size_t grid_size = 500; @@ -114,12 +114,12 @@ class FdtdPml1D { // Taflove, pp 292, Eq 7.60a. - for(int i = 0; i <= pml_width; ++i) { - double sigma_in_pml = std::pow((i+1) / pml_width, m) * sigma_max; + for(int i = 0; i < pml_width; ++i) { + double sigma_in_pml = std::pow((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; + sigma[pml_width-i-1] = sigma_in_pml; } @@ -154,7 +154,7 @@ class FdtdPml1D { std::vector &vect_hy) { // Insert source in certain space grid. - double t = time_step * dt; + double t = (double)time_step * dt; double source = std::exp(-(std::pow((t0 - t) / tau, 2))) * std::cos(omega * t); ex[src_position] += source; @@ -166,6 +166,7 @@ class FdtdPml1D { for(int i = 1; i < grid_size-1; ++i) { ex[i] = C[i] * ex[i] - D[i] * (hy[i] - hy[i-1]); } + ex[grid_size-1] = ex[grid_size-2]; // ex[grid_size-1] = ex[grid_size-2]; for(int i = 0; i < grid_size; ++i) { diff --git a/src/FDTD/2d-pml/fdtd-pml-2d.h b/src/fdtd/2d-pml/fdtd-pml-2d.h similarity index 96% rename from src/FDTD/2d-pml/fdtd-pml-2d.h rename to src/fdtd/2d-pml/fdtd-pml-2d.h index 1bf45bb..89562d3 100644 --- a/src/FDTD/2d-pml/fdtd-pml-2d.h +++ b/src/fdtd/2d-pml/fdtd-pml-2d.h @@ -16,15 +16,10 @@ class FdtdPml2D { // Grid sizes. - static const size_t pml_width = 60; - - static const size_t rows = 200 + pml_width*2; - static const size_t cols = 200 + pml_width*2; - // static const size_t pml_width = 2; - - // static const size_t rows = 6 + pml_width*2; - // static const size_t cols = 6 + pml_width*2; + static const size_t pml_width = 40; + static const size_t rows = 220 + pml_width*2; + static const size_t cols = 220 + pml_width*2; // Set source position. size_t src_row = rows / 2; @@ -59,9 +54,6 @@ class FdtdPml2D double gaz[rows][cols]; - // Max simulation time. - const size_t max_time = 500; - // Space grid step. const double ddx = 1.0e-3; const double ddy = ddx; @@ -97,10 +89,21 @@ class FdtdPml2D public: + // Getters. + static size_t GetRows() { + return rows - pml_width * 2; + } + + static size_t GetCols() { + return cols - pml_width * 2; + } + + void SetParams(std::vector>& eps, std::vector>& mu, std::vector>& sigma) { + time_step = 0; // Init field arrays. std::fill_n(&dz[0][0], rows * cols, 0.0); diff --git a/src/globals.h b/src/globals.h deleted file mode 100644 index d655e58..0000000 --- a/src/globals.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include - -static const size_t Nx = 400; -static const size_t Ny = 400; \ No newline at end of file diff --git a/src/index.cpp b/src/index.cpp index 274785c..89988ad 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -11,52 +11,54 @@ #include #include - #include #include #include -#include "./FDTD/1d-pml/fdtd-pml-1d.h" -#include "./FDTD/2d-pml/fdtd-pml-2d.h" - - - -// https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574420#12574420 -// https://www.it-swarm.com.ru/ru/c%2B%2B/chto-takoe-neopredelennaya-ssylka-nerazreshennaya-vneshnyaya-oshibka-simvola-i-kak-ee-ispravit/1069256308/ +#include "./fdtd/1d-pml/fdtd-pml-1d.h" +#include "./fdtd/2d-pml/fdtd-pml-2d.h" -// FDTD method in 1D case. +// Fdtd method in 1D case. Napi::Value GetData1D(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - // Params: - // 0 - [lambda, tau, refractive_index] + + // Grid size. + size_t grid_size = 500; + + // Params(in 'info' array)): + // 0 - [lambda, tau] // 1 - reload // 2 - refractive index vector. // 3 - refractive index vector size. // 4 - relative source position 0..1 // 5 - conductivity vector (sigma). + const Napi::Array input_array_condition = info[0].As(); + double lambda = (float)input_array_condition[(uint32_t)0].As(); + double tau = (float)(input_array_condition[1].As()); - // Grid size. - size_t grid_size = 500; // Temporary matrix. std::vector tmp_vector = {}; 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(); + // Must be even. + int epsilon_vector_size = static_cast(info[3].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++) { @@ -99,11 +101,8 @@ Napi::Value GetData1D(const Napi::CallbackInfo &info) { } } - 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. std::vector vect_x = {}; @@ -216,11 +215,8 @@ Napi::Value GetData2D(const Napi::CallbackInfo &info) { } } - // const size_t rows = 200; - // const size_t cols = 200; - - const size_t rows = 200; - const size_t cols = 200; + const size_t rows = FdtdPml2D::GetRows(); + const size_t cols = FdtdPml2D::GetCols(); // Matrix size coefficient. size_t coeff = rows / material_matrix_size; @@ -261,18 +257,12 @@ Napi::Value GetData2D(const Napi::CallbackInfo &info) { } - - static FdtdPml2D fdtd = FdtdPml2D(eps_matrix, mu_matrix, sigma_matrix); - // 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) { - // fdtd_3D.setLambda(lambda); - // fdtd_3D.setBeamsize(beamsize); - // fdtd_3D.setParams(refr_index_matrix); - // } + // (fdtd_3D.getLambda() != lambda) || (fdtd_3D.getBeamsize() != beamsize) || + if (reload_check) { + fdtd.SetParams(eps_matrix, mu_matrix, sigma_matrix); + } std::vector vect_X = {}; std::vector vect_Y = {}; @@ -288,19 +278,21 @@ Napi::Value GetData2D(const Napi::CallbackInfo &info) { // Matrix sizes. - size_t Nx = rows / fdtd.GetStep(); - size_t Ny = cols / fdtd.GetStep(); + size_t client_rows = rows / fdtd.GetStep(); + size_t client_cols = cols / fdtd.GetStep(); + + const size_t js_arrays_size = client_rows * client_cols; // Creating JS arrays to store C++ arrays. - Napi::Array js_data_X = Napi::Array::New(env, Nx * Ny); - Napi::Array js_data_Y = Napi::Array::New(env, Nx * Ny); - Napi::Array js_data_Ez = Napi::Array::New(env, Nx * Ny); - Napi::Array js_data_Hy = Napi::Array::New(env, Nx * Ny); - Napi::Array js_data_Hx = Napi::Array::New(env, Nx * Ny); - Napi::Array js_data_Energy = Napi::Array::New(env, Nx * Ny); + Napi::Array js_data_X = Napi::Array::New(env, js_arrays_size); + Napi::Array js_data_Y = Napi::Array::New(env, js_arrays_size); + Napi::Array js_data_Ez = Napi::Array::New(env, js_arrays_size); + Napi::Array js_data_Hy = Napi::Array::New(env, js_arrays_size); + Napi::Array js_data_Hx = Napi::Array::New(env, js_arrays_size); + Napi::Array js_data_Energy = Napi::Array::New(env, js_arrays_size); // Filling JS arrays with C++ arrays data. - for (size_t i = 0; i < Nx * Ny; i++) { + for (size_t i = 0; i < js_arrays_size; i++) { js_data_X[i] = Napi::Number::New(env, vect_X[i]); js_data_Y[i] = Napi::Number::New(env, vect_Y[i]); js_data_Ez[i] = Napi::Number::New(env, vect_Ez[i]); @@ -313,9 +305,9 @@ Napi::Value GetData2D(const Napi::CallbackInfo &info) { Napi::Object data = Napi::Array::New(env); data.Set("dataX", js_data_X); data.Set("dataY", js_data_Y); - data.Set("row", Nx); - data.Set("col", Ny); - data.Set("currentTick", fdtd.GetCurrentTimeStep()); + data.Set("rows", client_rows); + data.Set("cols", client_cols); + data.Set("timeStep", fdtd.GetCurrentTimeStep()); @@ -359,10 +351,6 @@ Napi::Value GetData2D(const Napi::CallbackInfo &info) { return data; - - // Napi::Object data2 = Napi::Array::New(env); - // data2.Set("min", 3); - // return data2; } diff --git a/test-addon.ts b/test-addon.ts index 093368d..d9512f4 100644 --- a/test-addon.ts +++ b/test-addon.ts @@ -50,7 +50,7 @@ const test2D = () => { let data = addon.getData2D(condition, reload, materialMatrix, matrixSize, eps, mu, sigma, returnDataNumber, srcPosition); reload = false; - for (let j = 0; j < 200; ++j) { + for (let j = 0; j < 50; ++j) { data = addon.getData2D(condition, reload, materialMatrix, matrixSize, eps, mu, sigma, returnDataNumber, srcPosition); } @@ -79,5 +79,5 @@ function testMemoryUsage() { test1D(); -test2D(); +// test2D(); // testMemoryUsage(); diff --git a/tsconfig.json b/tsconfig.json index ece7461..adcf1f0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -42,7 +42,7 @@ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */ /* Emit */ - // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ + "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ // "declarationMap": true, /* Create sourcemaps for d.ts files. */ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */