From a976eb66d220b7b436330b098a3a895a95f3be3c Mon Sep 17 00:00:00 2001 From: Tobit Flatscher <53856473+2b-t@users.noreply.github.com> Date: Sun, 16 Jun 2024 01:28:46 +0100 Subject: [PATCH] feat: Make interface to continua use velocity and pressure units --- include/lbt/continuums/continuum_base.hpp | 47 +++++++++++++++---- include/lbt/continuums/simple_continuum.hpp | 51 +++++++++++---------- include/lbt/continuums/vtk_continuum.hpp | 49 ++++++++++---------- test/continuums/continuum_base_test.hpp | 17 ++++--- 4 files changed, 99 insertions(+), 65 deletions(-) diff --git a/include/lbt/continuums/continuum_base.hpp b/include/lbt/continuums/continuum_base.hpp index 85fa8ca..4ea3d3b 100755 --- a/include/lbt/continuums/continuum_base.hpp +++ b/include/lbt/continuums/continuum_base.hpp @@ -12,6 +12,8 @@ #include #include +#include "lbt/units/units.hpp" + namespace lbt { @@ -21,7 +23,7 @@ namespace lbt { * \tparam T Floating data type used for simulation * \tparam Dummy argument used for SFINAE */ - template >* = nullptr> + template class ContinuumBase { public: /**\fn setP @@ -32,7 +34,7 @@ namespace lbt { * \param[in] z The z-coordinate of cell * \param[in] value The value the pressure should be set to */ - virtual void setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept = 0; + virtual void setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Pressure const value) noexcept = 0; /**\fn setU * \brief Set the x-velocity to a given value \param value at the coordinates \param x, \param y and \param z @@ -42,7 +44,7 @@ namespace lbt { * \param[in] z The z-coordinate of cell * \param[in] value The value the pressure should be set to */ - virtual void setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept = 0; + virtual void setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept = 0; /**\fn setV * \brief Set the y-velocity to a given value \param value at the coordinates \param x, \param y and \param z @@ -52,7 +54,7 @@ namespace lbt { * \param[in] z The z-coordinate of cell * \param[in] value The value the pressure should be set to */ - virtual void setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept = 0; + virtual void setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept = 0; /**\fn setW * \brief Set the z-velocity to a given value \param value at the coordinates \param x, \param y and \param z @@ -62,7 +64,7 @@ namespace lbt { * \param[in] z The z-coordinate of cell * \param[in] value The value the pressure should be set to */ - virtual void setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept = 0; + virtual void setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept = 0; /**\fn getP * \brief Get the pressure at the coordinates \param x, \param y and \param z @@ -72,7 +74,7 @@ namespace lbt { * \param[in] z The z-coordinate of cell * \return The pressure value at the coordinates \param x, \param y and \param z */ - virtual T getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept = 0; + virtual lbt::unit::Pressure getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept = 0; /**\fn getU * \brief Get the x-velocity at the coordinates \param x, \param y and \param z @@ -82,7 +84,7 @@ namespace lbt { * \param[in] z The z-coordinate of cell * \return The x-velocity value at the coordinates \param x, \param y and \param z */ - virtual T getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept = 0; + virtual lbt::unit::Velocity getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept = 0; /**\fn getV * \brief Get the y-velocity at the coordinates \param x, \param y and \param z @@ -92,7 +94,7 @@ namespace lbt { * \param[in] z The z-coordinate of cell * \return The y-velocity value at the coordinates \param x, \param y and \param z */ - virtual T getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept = 0; + virtual lbt::unit::Velocity getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept = 0; /**\fn getW * \brief Get the z-velocity at the coordinates \param x, \param y and \param z @@ -102,7 +104,7 @@ namespace lbt { * \param[in] z The z-coordinate of cell * \return The z-velocity value at the coordinates \param x, \param y and \param z */ - virtual T getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept = 0; + virtual lbt::unit::Velocity getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept = 0; /**\fn save * \brief Function for exporting the domain to a file @@ -111,6 +113,16 @@ namespace lbt { */ virtual void save(double const timestamp) const noexcept = 0; + /**\fn initializeUniform + * \brief Initialize the continuum with uniform values + * + * \param[in] p The pressure to be set for the entire flow field + * \param[in] u The x-velocity to be set to the entire flow field + * \param[in] v The y-velocity to be set to the entire flow field + * \param[in] w The z-velocity to be set to the entire flow field + */ + void initializeUniform(lbt::unit::Pressure const p, lbt::unit::Velocity const u, lbt::unit::Velocity const v, lbt::unit::Velocity const w = 0); + protected: /**\fn ContinuumBase * \brief Class constructor @@ -122,6 +134,7 @@ namespace lbt { */ ContinuumBase(std::int32_t const NX, std::int32_t const NY, std::int32_t const NZ, std::filesystem::path const& output_path) noexcept : NX{NX}, NY{NY}, NZ{NZ}, output_path{output_path} { + static_assert(std::is_floating_point_v); return; } @@ -137,6 +150,22 @@ namespace lbt { std::filesystem::path output_path; }; + template + void ContinuumBase::initializeUniform(lbt::unit::Pressure const p, lbt::unit::Velocity const u, + lbt::unit::Velocity const v, lbt::unit::Velocity const w) { + for(std::int32_t z = 0; z < NZ; ++z) { + for(std::int32_t y = 0; y < NY; ++y) { + for(std::int32_t x = 0; x < NX; ++x) { + setP(x, y, z, p); + setU(x, y, z, u); + setV(x, y, z, v); + setW(x, y, z, w); + } + } + } + return; + } + } #endif // LBT__CONTINUUMS__CONTINUUM_BASE diff --git a/include/lbt/continuums/simple_continuum.hpp b/include/lbt/continuums/simple_continuum.hpp index 944f5e8..59fb52d 100755 --- a/include/lbt/continuums/simple_continuum.hpp +++ b/include/lbt/continuums/simple_continuum.hpp @@ -22,6 +22,7 @@ #include "lbt/common/output_utilities.hpp" #include "lbt/common/type_definitions.hpp" #include "lbt/continuums/continuum_base.hpp" +#include "lbt/units/units.hpp" namespace lbt { @@ -54,14 +55,14 @@ namespace lbt { SimpleContinuum(SimpleContinuum&&) = default; SimpleContinuum& operator = (SimpleContinuum&&) = default; - void setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept override; - void setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept override; - void setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept override; - void setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept override; - T getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; - T getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; - T getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; - T getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; + void setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Pressure const value) noexcept override; + void setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept override; + void setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept override; + void setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept override; + lbt::unit::Pressure getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; + lbt::unit::Velocity getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; + lbt::unit::Velocity getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; + lbt::unit::Velocity getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; void save(double const timestamp) const noexcept override; /**\fn operator() @@ -152,47 +153,47 @@ namespace lbt { }; template - void SimpleContinuum::setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept { - this->operator()(x,y,z,0) = value; + void SimpleContinuum::setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Pressure const value) noexcept { + this->operator()(x,y,z,0) = value.get(); return; } template - void SimpleContinuum::setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept { - this->operator()(x,y,z,1) = value; + void SimpleContinuum::setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept { + this->operator()(x,y,z,1) = value.get(); return; } template - void SimpleContinuum::setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept { - this->operator()(x,y,z,2) = value; + void SimpleContinuum::setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept { + this->operator()(x,y,z,2) = value.get(); return; } template - void SimpleContinuum::setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept { - this->operator()(x,y,z,3) = value; + void SimpleContinuum::setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept { + this->operator()(x,y,z,3) = value.get(); return; } template - T SimpleContinuum::getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { - return this->operator()(x,y,z,0); + lbt::unit::Pressure SimpleContinuum::getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { + return lbt::unit::Pressure{this->operator()(x,y,z,0)}; } template - T SimpleContinuum::getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { - return this->operator()(x,y,z,1); + lbt::unit::Velocity SimpleContinuum::getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { + return lbt::unit::Velocity{this->operator()(x,y,z,1)}; } template - T SimpleContinuum::getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { - return this->operator()(x,y,z,2); + lbt::unit::Velocity SimpleContinuum::getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { + return lbt::unit::Velocity{this->operator()(x,y,z,2)}; } template - T SimpleContinuum::getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { - return this->operator()(x,y,z,3); + lbt::unit::Velocity SimpleContinuum::getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { + return lbt::unit::Velocity{this->operator()(x,y,z,3)}; } template @@ -260,7 +261,7 @@ namespace lbt { template inline std::int64_t SimpleContinuum::spatialToLinear_(std::int32_t const x, std::int32_t const y, std::int32_t const z, - std::int32_t const m) const noexcept { + std::int32_t const m) const noexcept { return ((static_cast(z)*ContinuumBase::NY + y)*ContinuumBase::NX + x)*number_of_values + m; } diff --git a/include/lbt/continuums/vtk_continuum.hpp b/include/lbt/continuums/vtk_continuum.hpp index a047b94..f7b576c 100755 --- a/include/lbt/continuums/vtk_continuum.hpp +++ b/include/lbt/continuums/vtk_continuum.hpp @@ -25,6 +25,7 @@ #include "lbt/common/output_utilities.hpp" #include "lbt/common/vtk_utilities.hpp" #include "lbt/continuums/continuum_base.hpp" + #include "lbt/units/units.hpp" namespace lbt { @@ -112,14 +113,14 @@ return *this; } - void setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept override; - void setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept override; - void setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept override; - void setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept override; - T getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; - T getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; - T getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; - T getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; + void setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Pressure const value) noexcept override; + void setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept override; + void setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept override; + void setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept override; + lbt::unit::Pressure getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; + lbt::unit::Velocity getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; + lbt::unit::Velocity getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; + lbt::unit::Velocity getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept override; void save(double const timestamp) const noexcept override; /**\fn saveToVtk @@ -180,47 +181,47 @@ }; template - void VtkContinuum::setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept { - setImageDataComponent_(p, x, y, z, value); + void VtkContinuum::setP(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Pressure const value) noexcept { + setImageDataComponent_(p, x, y, z, value.get()); return; } template - void VtkContinuum::setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept { - setImageDataComponent_(u, x, y, z, value); + void VtkContinuum::setU(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept { + setImageDataComponent_(u, x, y, z, value.get()); return; } template - void VtkContinuum::setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept { - setImageDataComponent_(v, x, y, z, value); + void VtkContinuum::setV(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept { + setImageDataComponent_(v, x, y, z, value.get()); return; } template - void VtkContinuum::setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, T const value) noexcept { - setImageDataComponent_(w, x, y, z, value); + void VtkContinuum::setW(std::int32_t const x, std::int32_t const y, std::int32_t const z, lbt::unit::Velocity const value) noexcept { + setImageDataComponent_(w, x, y, z, value.get()); return; } template - T VtkContinuum::getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { - return getImageDataComponent_(p, x, y, z); + lbt::unit::Pressure VtkContinuum::getP(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { + return lbt::unit::Pressure{getImageDataComponent_(p, x, y, z)}; } template - T VtkContinuum::getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { - return getImageDataComponent_(u, x, y, z); + lbt::unit::Velocity VtkContinuum::getU(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { + return lbt::unit::Velocity{getImageDataComponent_(u, x, y, z)}; } template - T VtkContinuum::getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { - return getImageDataComponent_(v, x, y, z); + lbt::unit::Velocity VtkContinuum::getV(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { + return lbt::unit::Velocity{getImageDataComponent_(v, x, y, z)}; } template - T VtkContinuum::getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { - return getImageDataComponent_(w, x, y, z); + lbt::unit::Velocity VtkContinuum::getW(std::int32_t const x, std::int32_t const y, std::int32_t const z) const noexcept { + return lbt::unit::Velocity{getImageDataComponent_(w, x, y, z)}; } template diff --git a/test/continuums/continuum_base_test.hpp b/test/continuums/continuum_base_test.hpp index 19ae73d..421bb25 100755 --- a/test/continuums/continuum_base_test.hpp +++ b/test/continuums/continuum_base_test.hpp @@ -30,7 +30,7 @@ namespace lbt { */ template class ContinuumBaseTest { - using T = TemplateDataType_t; + using U = TemplateDataType_t; public: /**\fn ContinuumBaseTest * \brief (Default) constructor @@ -57,34 +57,36 @@ namespace lbt { using namespace std::placeholders; auto setter {std::bind(&C::setP, std::ref(c), _1, _2, _3, _4)}; auto getter {std::bind(&C::getP, std::ref(c), _1, _2, _3)}; - return testSetAndGetHelper(setter, getter); + return testSetAndGetHelper(setter, getter); } bool testSetAndGetVelocityX() noexcept { using namespace std::placeholders; auto setter {std::bind(&C::setU, std::ref(c), _1, _2, _3, _4)}; auto getter {std::bind(&C::getU, std::ref(c), _1, _2, _3)}; - return testSetAndGetHelper(setter, getter); + return testSetAndGetHelper(setter, getter); } bool testSetAndGetVelocityY() noexcept { using namespace std::placeholders; auto setter {std::bind(&C::setV, std::ref(c), _1, _2, _3, _4)}; auto getter {std::bind(&C::getV, std::ref(c), _1, _2, _3)}; - return testSetAndGetHelper(setter, getter); + return testSetAndGetHelper(setter, getter); } bool testSetAndGetVelocityZ() noexcept { using namespace std::placeholders; auto setter {std::bind(&C::setW, std::ref(c), _1, _2, _3, _4)}; auto getter {std::bind(&C::getW, std::ref(c), _1, _2, _3)}; - return testSetAndGetHelper(setter, getter); + return testSetAndGetHelper(setter, getter); } protected: /**\fn testSetAndGetHelper * \brief Helper method for testing setting and getting values for the different entities * + * \tparam Type of the value to be set * \param[in] setter The function to be called for setting a value * \param[in] getter The function to be called for getting a value * \return Boolean argument signaling whether the test succeeded (true) or failed (false) */ + template bool testSetAndGetHelper(std::function setter, std::function getter) noexcept { bool is_success {true}; @@ -92,7 +94,7 @@ namespace lbt { for (std::int32_t x = 0; x < NX; ++x) { for (std::int32_t y = 0; y < NY; ++y) { for (std::int32_t z = 0; z < NZ; ++z) { - setter(x,y,z,static_cast(i)); + setter(x, y, z, T{static_cast(i)}); ++i; } } @@ -101,7 +103,8 @@ namespace lbt { for (std::int32_t x = 0; x < NX; ++x) { for (std::int32_t y = 0; y < NY; ++y) { for (std::int32_t z = 0; z < NZ; ++z) { - is_success &= lbt::math::isAlmostEqualEpsAbs(getter(x,y,z), static_cast(i)); + auto const value {getter(x,y,z)}; + is_success &= lbt::math::isAlmostEqualEpsAbs(static_cast(value.get()), static_cast(i)); ++i; } }