From ddb1e184c705fac4052669ee373a1cba16ebec9f Mon Sep 17 00:00:00 2001 From: deegan Date: Tue, 19 Dec 2023 14:00:30 +0100 Subject: [PATCH] wip updates --- .../ions/ion_population/ion_population.hpp | 36 +++-- src/core/data/ions/ions.hpp | 64 +++----- tests/core/data/electrons/test_electrons.cpp | 141 ++++++++---------- .../ion_population/test_ion_population.cpp | 7 + tests/core/data/vecfield/test_vecfield.hpp | 2 +- .../data/vecfield/test_vecfield_fixtures.hpp | 64 ++++++++ tests/initializer/init_functions.hpp | 27 ++-- 7 files changed, 188 insertions(+), 153 deletions(-) create mode 100644 tests/core/data/vecfield/test_vecfield_fixtures.hpp diff --git a/src/core/data/ions/ion_population/ion_population.hpp b/src/core/data/ions/ion_population/ion_population.hpp index bff0437a29..8a16ed77c6 100644 --- a/src/core/data/ions/ion_population/ion_population.hpp +++ b/src/core/data/ions/ion_population/ion_population.hpp @@ -36,6 +36,7 @@ namespace core : name_{initializer["name"].template to()} , mass_{initializer["mass"].template to()} , flux_{name_ + "_flux", HybridQuantity::Vector::V} + , rho_{name_ + "_rho", HybridQuantity::Scalar::rho} , particleInitializerInfo_{initializer["particle_initializer"]} { } @@ -52,13 +53,13 @@ namespace core NO_DISCARD bool isUsable() const { - return particles_ != nullptr && rho_ != nullptr && flux_.isUsable(); + return particles_ != nullptr && rho_.isUsable() && flux_.isUsable(); } NO_DISCARD bool isSettable() const { - return particles_ == nullptr && rho_ == nullptr && flux_.isSettable(); + return particles_ == nullptr && rho_.isSettable() && flux_.isSettable(); } @@ -181,7 +182,7 @@ namespace core { if (isUsable()) { - return *rho_; + return rho_; } else { @@ -252,21 +253,24 @@ namespace core - void setBuffer(std::string const& bufferName, field_type* field) - { - if (bufferName == name_ + "_rho") - { - rho_ = field; - } - else - { - throw std::runtime_error("Error - invalid density buffer name"); - } - } + // void setBuffer(std::string const& bufferName, field_type* field) + // { + // if (bufferName == name_ + "_rho") + // { + // rho_ = field; + // } + // else + // { + // throw std::runtime_error("Error - invalid density buffer name"); + // } + // } - NO_DISCARD auto getCompileTimeResourcesUserList() { return std::forward_as_tuple(flux_); } + NO_DISCARD auto getCompileTimeResourcesUserList() + { + return std::forward_as_tuple(flux_, rho_); + } //------------------------------------------------------------------------- @@ -288,7 +292,7 @@ namespace core std::string name_; double mass_; VecField flux_; - field_type* rho_{nullptr}; + field_type rho_; //{nullptr}; ParticlesPack* particles_{nullptr}; initializer::PHAREDict const& particleInitializerInfo_; }; diff --git a/src/core/data/ions/ions.hpp b/src/core/data/ions/ions.hpp index 09fea2013f..5e8241a00d 100644 --- a/src/core/data/ions/ions.hpp +++ b/src/core/data/ions/ions.hpp @@ -42,7 +42,9 @@ namespace core explicit Ions(PHARE::initializer::PHAREDict const& dict) - : bulkVelocity_{"bulkVel", HybridQuantity::Vector::V} + : rho_{densityName(), HybridQuantity::Scalar::rho} + , massDensity_{massDensityName(), HybridQuantity::Scalar::rho} + , bulkVelocity_{"bulkVel", HybridQuantity::Vector::V} , populations_{generate( [&dict](auto ipop) { // return IonPopulation{dict["pop" + std::to_string(ipop)]}; @@ -58,20 +60,16 @@ namespace core NO_DISCARD field_type const& density() const { - if (isUsable()) - return *rho_; - else + if (!isUsable()) throw std::runtime_error("Error - cannot access density data"); + return rho_; } - - NO_DISCARD field_type& density() { - if (isUsable()) - return *rho_; - else + if (!isUsable()) throw std::runtime_error("Error - cannot access density data"); + return rho_; } @@ -80,13 +78,13 @@ namespace core NO_DISCARD vecfield_type& velocity() { return bulkVelocity_; } - NO_DISCARD std::string densityName() const { return "rho"; } - NO_DISCARD std::string massDensityName() const { return "massDensity"; } + NO_DISCARD std::string static densityName() { return "rho"; } + NO_DISCARD std::string static massDensityName() { return "massDensity"; } void computeDensity() { - rho_->zero(); + rho_.zero(); for (auto const& pop : populations_) { @@ -95,8 +93,8 @@ namespace core // have to account for the field dimensionality. auto& popDensity = pop.density(); - std::transform(std::begin(*rho_), std::end(*rho_), std::begin(popDensity), - std::begin(*rho_), std::plus{}); + std::transform(std::begin(rho_), std::end(rho_), std::begin(popDensity), + std::begin(rho_), std::plus{}); } } void computeMassDensity() @@ -111,8 +109,8 @@ namespace core auto& popDensity = pop.density(); std::transform( - std::begin(*massDensity_), std::end(*massDensity_), std::begin(popDensity), - std::begin(*massDensity_), + std::begin(massDensity_), std::end(massDensity_), std::begin(popDensity), + std::begin(massDensity_), [&pop](auto const& n, auto const& pop_n) { return n + pop_n * pop.mass(); }); } } @@ -154,11 +152,11 @@ namespace core } - std::transform(std::begin(vx), std::end(vx), std::begin(*density), std::begin(vx), + std::transform(std::begin(vx), std::end(vx), std::begin(density), std::begin(vx), std::divides{}); - std::transform(std::begin(vy), std::end(vy), std::begin(*density), std::begin(vy), + std::transform(std::begin(vy), std::end(vy), std::begin(density), std::begin(vy), std::divides{}); - std::transform(std::begin(vz), std::end(vz), std::begin(*density), std::begin(vz), + std::transform(std::begin(vz), std::end(vz), std::begin(density), std::begin(vz), std::divides{}); } @@ -175,7 +173,7 @@ namespace core // because it is for internal use only so no object will ever need to access it. NO_DISCARD bool isUsable() const { - bool usable = rho_ != nullptr && bulkVelocity_.isUsable(); + bool usable = rho_.isUsable() && bulkVelocity_.isUsable(); for (auto const& pop : populations_) { usable = usable && pop.isUsable(); @@ -187,7 +185,7 @@ namespace core NO_DISCARD bool isSettable() const { - bool settable = rho_ == nullptr && bulkVelocity_.isSettable(); + bool settable = rho_.isSettable() && bulkVelocity_.isSettable(); for (auto const& pop : populations_) { settable = settable && pop.isSettable(); @@ -218,24 +216,6 @@ namespace core - void setBuffer(std::string const& bufferName, field_type* field) - { - if (bufferName == densityName()) - { - rho_ = field; - } - else if (bufferName == massDensityName()) - { - massDensity_ = field; - } - else - { - throw std::runtime_error("Error - invalid density buffer name : " + bufferName); - } - } - - - NO_DISCARD std::vector& getRunTimeResourcesUserList() { return populations_; @@ -243,7 +223,7 @@ namespace core NO_DISCARD auto getCompileTimeResourcesUserList() { - return std::forward_as_tuple(bulkVelocity_); + return std::forward_as_tuple(bulkVelocity_, rho_, massDensity_); } @@ -276,8 +256,8 @@ namespace core - field_type* rho_{nullptr}; - field_type* massDensity_{nullptr}; + field_type rho_; //{nullptr}; + field_type massDensity_; //{nullptr}; vecfield_type bulkVelocity_; std::vector populations_; diff --git a/tests/core/data/electrons/test_electrons.cpp b/tests/core/data/electrons/test_electrons.cpp index dc49a9ca3b..64897e45b8 100644 --- a/tests/core/data/electrons/test_electrons.cpp +++ b/tests/core/data/electrons/test_electrons.cpp @@ -15,6 +15,7 @@ #include "gtest/gtest.h" #include "tests/initializer/init_functions.hpp" +#include "tests/core/data/vecfield/test_vecfield_fixtures.hpp" #include @@ -131,9 +132,10 @@ struct ElectronsTest : public ::testing::Test using GridYee = GridLayout>; - using GridND = Grid, HybridQuantity::Scalar>; - using FieldND = Field; - using VecFieldND = VecField; + using GridND = Grid, HybridQuantity::Scalar>; + using FieldND = Field; + using VecFieldND = VecField; + using UsableVecFieldND = UsableVecField; using IonPopulationND = IonPopulation, VecFieldND, GridYee>; using IonsT = Ions; @@ -144,92 +146,66 @@ struct ElectronsTest : public ::testing::Test GridYee layout = NDlayout::create(); Electromag electromag; - GridND Jx, Jy, Jz; - VecFieldND J; + UsableVecFieldND J, F, Ve, Vi; - GridND Nibuffer; - GridND NiProtons; - GridND Fxi, Fyi, Fzi; - GridND Vex, Vey, Vez; - GridND Vix, Viy, Viz; + GridND Nibuffer, NiProtons, Pe; IonsT ions; Electrons electrons; - GridND Pe; PartPackND pack{}; template auto static _ions(Args&... args) { - auto const& [Fxi, Fyi, Fzi, Nibuffer, NiProtons, Vix, Viy, Viz, pack] - = std::forward_as_tuple(args...); + auto const& [Fi, Nibuffer, NiProtons, Vi, pack] = std::forward_as_tuple(args...); IonsT ions{createDict()["ions"]}; - ions.setBuffer(ions.densityName(), &Nibuffer); - ions.velocity().setBuffer(Vix.name(), &Vix); - ions.velocity().setBuffer(Viy.name(), &Viy); - ions.velocity().setBuffer(Viz.name(), &Viz); + { + auto const& [V, d, md] = ions.getCompileTimeResourcesUserList(); + d.setBuffer(&Nibuffer); + Vi.set_on(V); + } auto& pops = ions.getRunTimeResourcesUserList(); - pops[0].setBuffer(NiProtons.name(), &NiProtons); - pops[0].flux().setBuffer(Fxi.name(), &Fxi); - pops[0].flux().setBuffer(Fyi.name(), &Fyi); - pops[0].flux().setBuffer(Fzi.name(), &Fzi); + assert(pops.size() == 1); + + auto const& [F, d] = pops[0].getCompileTimeResourcesUserList(); + d.setBuffer(&NiProtons); + Fi.set_on(F); + pops[0].setBuffer("protons", &pack); return ions; } - template - auto static _J(Args&... args) - { - auto const& [Jx, Jy, Jz] = std::forward_as_tuple(args...); - VecFieldND J{"J", HybridQuantity::Vector::J}; - J.setBuffer(Jx.name(), &Jx); - J.setBuffer(Jy.name(), &Jy); - J.setBuffer(Jz.name(), &Jz); - return J; - } ElectronsTest() : electromag{createDict()["electromag"]} - , Jx{"J_x", HybridQuantity::Scalar::Jx, layout.allocSize(HybridQuantity::Scalar::Jx)} - , Jy{"J_y", HybridQuantity::Scalar::Jy, layout.allocSize(HybridQuantity::Scalar::Jy)} - , Jz{"J_z", HybridQuantity::Scalar::Jz, layout.allocSize(HybridQuantity::Scalar::Jz)} - , J{_J(Jx, Jy, Jz)} + , J{"J", layout, HybridQuantity::Vector::J} + , F{"protons_flux", layout, HybridQuantity::Vector::V} + , Ve{"StandardHybridElectronFluxComputer_Ve", layout, HybridQuantity::Vector::V} + , Vi{"bulkVel", layout, HybridQuantity::Vector::V} , Nibuffer{std::string{densityName}, HybridQuantity::Scalar::rho, layout.allocSize(HybridQuantity::Scalar::rho)} , NiProtons{"protons_rho", HybridQuantity::Scalar::rho, layout.allocSize(HybridQuantity::Scalar::rho)} - , Fxi{"protons_flux_x", HybridQuantity::Scalar::Vx, - layout.allocSize(HybridQuantity::Scalar::Vx)} - , Fyi{"protons_flux_y", HybridQuantity::Scalar::Vy, - layout.allocSize(HybridQuantity::Scalar::Vy)} - , Fzi{"protons_flux_z", HybridQuantity::Scalar::Vz, - layout.allocSize(HybridQuantity::Scalar::Vz)} - , Vex{"StandardHybridElectronFluxComputer_Ve_x", HybridQuantity::Scalar::Vx, - layout.allocSize(HybridQuantity::Scalar::Vx)} - , Vey{"StandardHybridElectronFluxComputer_Ve_y", HybridQuantity::Scalar::Vy, - layout.allocSize(HybridQuantity::Scalar::Vy)} - , Vez{"StandardHybridElectronFluxComputer_Ve_z", HybridQuantity::Scalar::Vz, - layout.allocSize(HybridQuantity::Scalar::Vz)} - , Vix{"bulkVel_x", HybridQuantity::Scalar::Vx, layout.allocSize(HybridQuantity::Scalar::Vx)} - , Viy{"bulkVel_y", HybridQuantity::Scalar::Vy, layout.allocSize(HybridQuantity::Scalar::Vy)} - , Viz{"bulkVel_z", HybridQuantity::Scalar::Vz, layout.allocSize(HybridQuantity::Scalar::Vz)} - , ions{_ions(Fxi, Fyi, Fzi, Nibuffer, NiProtons, Vix, Viy, Viz, pack)} - , electrons{createDict()["electrons"], ions, J} , Pe{"Pe", HybridQuantity::Scalar::P, layout.allocSize(HybridQuantity::Scalar::P)} + , ions{_ions(F, Nibuffer, NiProtons, Vi, pack)} + , electrons{createDict()["electrons"], ions, J} { auto&& emm = std::get<0>(electrons.getCompileTimeResourcesUserList()); auto&& fc = std::get<0>(emm.getCompileTimeResourcesUserList()); - auto&& Ve = std::get<0>(fc.getCompileTimeResourcesUserList()); + // auto&& Ve = std::get<0>(fc.getCompileTimeResourcesUserList()); + + Ve.set_on(std::get<0>(fc.getCompileTimeResourcesUserList())); - Ve.setBuffer(Vex.name(), &Vex); - Ve.setBuffer(Vey.name(), &Vey); - Ve.setBuffer(Vez.name(), &Vez); auto&& pc = std::get<1>(emm.getCompileTimeResourcesUserList()); pc.setBuffer(Pe.name(), &Pe); + auto const& [Jx, Jy, Jz] = J(); + auto const& [Vix, Viy, Viz] = Vi(); + // auto const& [Jx,Jy, Jz] = J(); + if constexpr (dim == 1) { auto fill = [this](auto& field, auto const& filler) { @@ -335,34 +311,34 @@ struct ElectronsTest : public ::testing::Test ~ElectronsTest() { - J.setBuffer(Jx.name(), nullptr); - J.setBuffer(Jy.name(), nullptr); - J.setBuffer(Jz.name(), nullptr); + // J.setBuffer(Jx.name(), nullptr); + // J.setBuffer(Jy.name(), nullptr); + // J.setBuffer(Jz.name(), nullptr); - ions.setBuffer(ions.densityName(), nullptr); - ions.velocity().setBuffer(Vix.name(), nullptr); - ions.velocity().setBuffer(Viy.name(), nullptr); - ions.velocity().setBuffer(Viz.name(), nullptr); + // ions.setBuffer(ions.densityName(), nullptr); + // ions.velocity().setBuffer(Vix.name(), nullptr); + // ions.velocity().setBuffer(Viy.name(), nullptr); + // ions.velocity().setBuffer(Viz.name(), nullptr); - auto& pops = ions.getRunTimeResourcesUserList(); + // auto& pops = ions.getRunTimeResourcesUserList(); - pops[0].setBuffer(NiProtons.name(), static_cast(nullptr)); - pops[0].flux().setBuffer(Fxi.name(), nullptr); - pops[0].flux().setBuffer(Fyi.name(), nullptr); - pops[0].flux().setBuffer(Fzi.name(), nullptr); - pops[0].setBuffer("protons", static_cast(nullptr)); + // pops[0].setBuffer(NiProtons.name(), static_cast(nullptr)); + // pops[0].flux().setBuffer(Fxi.name(), nullptr); + // pops[0].flux().setBuffer(Fyi.name(), nullptr); + // pops[0].flux().setBuffer(Fzi.name(), nullptr); + // pops[0].setBuffer("protons", static_cast(nullptr)); - auto&& emm = std::get<0>(electrons.getCompileTimeResourcesUserList()); - auto&& fc = std::get<0>(emm.getCompileTimeResourcesUserList()); - auto&& Ve = std::get<0>(fc.getCompileTimeResourcesUserList()); + // auto&& emm = std::get<0>(electrons.getCompileTimeResourcesUserList()); + // auto&& fc = std::get<0>(emm.getCompileTimeResourcesUserList()); + // auto&& Ve = std::get<0>(fc.getCompileTimeResourcesUserList()); - Ve.setBuffer(Vex.name(), nullptr); - Ve.setBuffer(Vey.name(), nullptr); - Ve.setBuffer(Vez.name(), nullptr); + // Ve.setBuffer(Vex.name(), nullptr); + // Ve.setBuffer(Vey.name(), nullptr); + // Ve.setBuffer(Vez.name(), nullptr); - auto&& pc = std::get<1>(emm.getCompileTimeResourcesUserList()); + // auto&& pc = std::get<1>(emm.getCompileTimeResourcesUserList()); - pc.setBuffer(Pe.name(), nullptr); + // pc.setBuffer(Pe.name(), nullptr); } }; @@ -374,7 +350,7 @@ using ElectronsTupleInfos std::pair, InterpConst<1>>, std::pair, InterpConst<2>>, std::pair, InterpConst<3>>>; -TYPED_TEST_SUITE(ElectronsTest, ElectronsTupleInfos); +TYPED_TEST_SUITE(ElectronsTest, ElectronsTupleInfos, ); @@ -527,9 +503,12 @@ TYPED_TEST(ElectronsTest, ThatElectronsVelocityEqualIonVelocityMinusJ) } }; - check(this->Vex, this->Vix, this->Jx, Ne, &GridYee::JxToMoments); - check(this->Vey, this->Viy, this->Jy, Ne, &GridYee::JyToMoments); - check(this->Vez, this->Viz, this->Jz, Ne, &GridYee::JzToMoments); + auto const& [Jx, Jy, Jz] = this->J(); + auto const& [Vix, Viy, Viz] = this->Vi(); + auto const& [Vex, Vey, Vez] = this->Ve(); + check(Vex, Vix, Jx, Ne, &GridYee::JxToMoments); + check(Vey, Viy, Jy, Ne, &GridYee::JyToMoments); + check(Vez, Viz, Jz, Ne, &GridYee::JzToMoments); } diff --git a/tests/core/data/ion_population/test_ion_population.cpp b/tests/core/data/ion_population/test_ion_population.cpp index 7aaf845788..02f52efc10 100644 --- a/tests/core/data/ion_population/test_ion_population.cpp +++ b/tests/core/data/ion_population/test_ion_population.cpp @@ -18,6 +18,13 @@ using namespace PHARE::initializer; struct DummyField { + template + DummyField(A const&, B const&) + { + } + + auto isUsable() const { return false; } + auto isSettable() const { return true; } }; diff --git a/tests/core/data/vecfield/test_vecfield.hpp b/tests/core/data/vecfield/test_vecfield.hpp index 7de393066d..fcfb025548 100644 --- a/tests/core/data/vecfield/test_vecfield.hpp +++ b/tests/core/data/vecfield/test_vecfield.hpp @@ -41,4 +41,4 @@ struct VecFieldMock -#endif /*PHARE_TEST_CORE_VECFIELD_TEST_H*/ +#endif /*PHARE_TEST_CORE_VECFIELD_TEST_HPP*/ diff --git a/tests/core/data/vecfield/test_vecfield_fixtures.hpp b/tests/core/data/vecfield/test_vecfield_fixtures.hpp new file mode 100644 index 0000000000..e2789be9da --- /dev/null +++ b/tests/core/data/vecfield/test_vecfield_fixtures.hpp @@ -0,0 +1,64 @@ +#ifndef PHARE_TEST_CORE_DATA_TEST_VECFIELD_FIXTURES_HPP +#define PHARE_TEST_CORE_DATA_TEST_VECFIELD_FIXTURES_HPP + +#include "core/data/field/field.hpp" +#include "core/data/grid/gridlayout.hpp" +#include "core/data/grid/gridlayout_impl.hpp" +#include "core/data/grid/gridlayoutdefs.hpp" +#include "core/data/vecfield/vecfield.hpp" + +namespace PHARE::core +{ +template +using Field_t = Field; + +template +class UsableVecField : public VecField, HybridQuantity> +{ + using Grid_t = Grid, HybridQuantity::Scalar>; + + template + auto static make_grids(std::string const& base, GridLayout const& layout, + HybridQuantity::Vector qty) + { + std::array constexpr componentParts{'x', 'y', 'z'}; + auto qts = HybridQuantity::componentsQuantities(qty); + return for_N<3, for_N_R_mode::make_array>([&](auto i) { + return Grid_t{base + componentParts[i], qts[i], layout.allocSize(qts[i])}; + }); + } + +public: + auto static constexpr dimension = dim; + using Super = VecField, HybridQuantity>; + + + template + UsableVecField(std::string const& name, GridLayout const& layout, HybridQuantity::Vector qty) + : Super{name, qty} + , name{name} + , xyz{make_grids(name, layout, qty)} + { + Super::setBuffer(name + "_x", &xyz[0]); + Super::setBuffer(name + "_y", &xyz[1]); + Super::setBuffer(name + "_z", &xyz[2]); + } + + template + void set_on(_VF_& vf) + { + vf.setBuffer(name + "_x", &xyz[0]); + vf.setBuffer(name + "_y", &xyz[1]); + vf.setBuffer(name + "_z", &xyz[2]); + } + +protected: + std::string name; + std::array xyz; +}; + + +} // namespace PHARE::core + + +#endif /*PHARE_TEST_CORE_DATA_TEST_VECFIELD_FIXTURES_HPP*/ diff --git a/tests/initializer/init_functions.hpp b/tests/initializer/init_functions.hpp index eda8a19424..9a7ad3c6b7 100644 --- a/tests/initializer/init_functions.hpp +++ b/tests/initializer/init_functions.hpp @@ -70,52 +70,52 @@ namespace PHARE::initializer::test_fn::func_2d using Param = std::vector const&; using Return = std::shared_ptr>; -Return density(Param x, Param y) +Return density(Param x, Param /*y*/) { return std::make_shared>(x); } -Return vx(Param x, Param y) +Return vx(Param x, Param /*y*/) { return std::make_shared>(x); } -Return vy(Param x, Param y) +Return vy(Param x, Param /*y*/) { return std::make_shared>(x); } -Return vz(Param x, Param y) +Return vz(Param x, Param /*y*/) { return std::make_shared>(x); } -Return vthx(Param x, Param y) +Return vthx(Param x, Param /*y*/) { return std::make_shared>(x); } -Return vthy(Param x, Param y) +Return vthy(Param x, Param /*y*/) { return std::make_shared>(x); } -Return vthz(Param x, Param y) +Return vthz(Param x, Param /*y*/) { return std::make_shared>(x); } -Return bx(Param x, Param y) +Return bx(Param x, Param /*y*/) { return std::make_shared>(x); } -Return by(Param x, Param y) +Return by(Param x, Param /*y*/) { return std::make_shared>(x); } -Return bz(Param x, Param y) +Return bz(Param x, Param /*y*/) { return std::make_shared>(x); } @@ -135,12 +135,13 @@ auto makeSharedPtr() } else if constexpr (dim == 2) { - return - [](Param x, Param y) { return std::make_shared>(x); }; + return [](Param x, Param /*y*/) { + return std::make_shared>(x); + }; } else if constexpr (dim == 3) { - return [](Param x, Param y, Param z) { + return [](Param x, Param /*y*/, Param /*z*/) { return std::make_shared>(x); }; }