From f5edff413633166b16b8e719c46de19721322e1b Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Fri, 1 Sep 2023 15:22:26 -0600 Subject: [PATCH 1/3] add more flexible parametrization --- .../impl/swe_2d_initial_condition.hpp | 15 +- .../impl/swe_2d_prob_class.hpp | 146 +++++++++++++----- include/pressiodemoapps/swe2d.hpp | 52 +++++-- tests_cpp/CMakeLists.txt | 1 + .../CMakeLists.txt | 13 ++ .../eigen_2d_swe_slip_wall_parameters/main.cc | 97 ++++++++++++ .../test.cmake | 16 ++ 7 files changed, 279 insertions(+), 61 deletions(-) create mode 100644 tests_cpp/eigen_2d_swe_slip_wall_parameters/CMakeLists.txt create mode 100644 tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc create mode 100644 tests_cpp/eigen_2d_swe_slip_wall_parameters/test.cmake diff --git a/include/pressiodemoapps/impl/swe_2d_initial_condition.hpp b/include/pressiodemoapps/impl/swe_2d_initial_condition.hpp index 906b6269..2d4c6940 100644 --- a/include/pressiodemoapps/impl/swe_2d_initial_condition.hpp +++ b/include/pressiodemoapps/impl/swe_2d_initial_condition.hpp @@ -54,23 +54,22 @@ namespace implswe2d{ template void GaussianPulse(state_type & state, - const mesh_t & meshObj, - const scalar_type pulseMag) + const mesh_t & meshObj, + const scalar_type pulseMag, + const scalar_type pulseX, + const scalar_type pulseY) { constexpr int numDofPerCell = 3; - constexpr auto one = static_cast(1); - constexpr auto zero = static_cast(0); - const auto &x= meshObj.viewX(); const auto &y= meshObj.viewY(); for (int i=0; i<::pressiodemoapps::extent(x,0); ++i) { const auto ind = i*numDofPerCell; - auto r = std::sqrt( (x(i) - one)*(x(i) - one) + (y(i) - one)*(y(i) - one) ); + auto r = std::sqrt( (x(i) - pulseX)*(x(i) - pulseX) + (y(i) - pulseY)*(y(i) - pulseY) ); state(ind) = static_cast(1) + pulseMag*std::exp( -(r*r) ); - state(ind+1) = zero; - state(ind+2) = zero; + state(ind+1) = static_cast(0); + state(ind+2) = static_cast(0); } } diff --git a/include/pressiodemoapps/impl/swe_2d_prob_class.hpp b/include/pressiodemoapps/impl/swe_2d_prob_class.hpp index 493571df..3c604493 100644 --- a/include/pressiodemoapps/impl/swe_2d_prob_class.hpp +++ b/include/pressiodemoapps/impl/swe_2d_prob_class.hpp @@ -71,6 +71,65 @@ namespace pressiodemoapps{ namespace implswe2d{ +constexpr int gravity_i = 0; +constexpr int coriolis_i = 1; +constexpr int pulseMagnitude_i = 2; +constexpr int pulseX_i = 3; +constexpr int pulseY_i = 4; + +template +auto create_vec_with_default_params(){ + const auto gravity = static_cast(9.8); + const auto coriolis = static_cast(-3); + const auto pulseMag = static_cast(1)/8; + const auto pulseX = static_cast(1); + const auto pulseY = pulseX; + return std::vector({gravity, coriolis, pulseMag, pulseX, pulseY}); +} + +template +const std::string param_index_to_string(IntT index){ + if (index == gravity_i) { return "gravity"; } + else if (index == coriolis_i) { return "coriolis"; } + else if (index == pulseMagnitude_i){ return "pulseMagnitude"; } + else if (index == pulseX_i) { return "pulseX"; } + else if (index == pulseY_i) { return "pulseY"; } + return "null"; +} + +template +int param_string_to_index(const std::string & s){ + if (s == "gravity") { return gravity_i; } + else if (s == "coriolis") { return coriolis_i; } + else if (s == "pulseMagnitude"){ return pulseMagnitude_i; } + else if (s == "pulseX") { return pulseX_i; } + else if (s == "pulseY") { return pulseY_i; } + else{ return std::numeric_limits::max(); } +} + +template +auto param_unord_map_to_vector(const std::unordered_map & map) +{ + // first create a vec with default params, and then loop over argument in map + // replacing whatever the map sets, while the rest remains default + auto result = create_vec_with_default_params(); + for (auto it = map.cbegin(); it != map.cend(); ++it){ + const int index = param_string_to_index<>(it->first); + result[index] = it->second; + } + return result; +} + +template +auto param_vector_to_unord_map(const std::vector & vec) +{ + std::unordered_map result; + for (std::size_t i=0; i && parameters) : m_probEn(::pressiodemoapps::Swe2d::SlipWall), m_recEn(recEn), m_fluxEn(fluxEnum), m_meshObj(meshObj), - m_gravity(gravity), - m_coriolis(coriolis), - m_initialPulseMagnitude(initialPulseMag) + m_parameters(std::move(parameters)) { + m_numDofStencilMesh = m_meshObj.get().stencilMeshSize() * numDofPerCell; m_numDofSampleMesh = m_meshObj.get().sampleMeshSize() * numDofPerCell; allocateGhosts(); @@ -133,26 +189,34 @@ class EigenApp ::pressiodemoapps::InviscidFluxReconstruction recEn, ::pressiodemoapps::InviscidFluxScheme fluxEnum, BCFunctorsHolderType && bcHolder, - scalar_type gravity, - scalar_type coriolis, - scalar_type initialPulseMag) + std::vector && parameters) : m_probEn(probEn), m_recEn(recEn), m_fluxEn(fluxEnum), m_meshObj(meshObj), - m_gravity(gravity), - m_coriolis(coriolis), - m_initialPulseMagnitude(initialPulseMag), + m_parameters(std::move(parameters)), m_bcFuncsHolder(std::move(bcHolder)) { + m_numDofStencilMesh = m_meshObj.get().stencilMeshSize() * numDofPerCell; m_numDofSampleMesh = m_meshObj.get().sampleMeshSize() * numDofPerCell; allocateGhosts(); } #endif - scalar_type gravity() const{ return m_gravity; } - scalar_type coriolis() const{ return m_coriolis; } + scalar_type gravity() const{ + return m_parameters[gravity_i]; + } + + scalar_type coriolis() const{ + return m_parameters[coriolis_i]; + } + + scalar_type queryParameter(const std::string & pname) const { + const int i = param_string_to_index(pname); + assert(i < (int) m_parameters.size()); + return m_parameters[i]; + } state_type initialCondition() const { @@ -161,7 +225,9 @@ class EigenApp case ::pressiodemoapps::Swe2d::SlipWall: case ::pressiodemoapps::Swe2d::CustomBCs: { - GaussianPulse(initialState, m_meshObj.get(), m_initialPulseMagnitude); + GaussianPulse(initialState, m_meshObj.get(), + m_parameters[pulseMagnitude_i], + m_parameters[pulseX_i], m_parameters[pulseY_i]); } }; @@ -481,7 +547,7 @@ class EigenApp /* end args for velo */ J, xAxis, m_meshObj.get(), /* end args for jac */ - m_fluxEn, normalX_, m_gravity, fluxL, fluxR, + m_fluxEn, normalX_, m_parameters[gravity_i], fluxL, fluxR, fluxJacLNeg, fluxJacLPos, fluxJacRNeg, fluxJacRPos, /* end args for flux */ xAxis, toReconstructionScheme(m_recEn), U, m_meshObj.get(), @@ -494,7 +560,7 @@ class EigenApp /* end args for velo */ J, yAxis, m_meshObj.get(), /* end args for jac */ - m_fluxEn, normalY_, m_gravity, fluxB, fluxF, + m_fluxEn, normalY_, m_parameters[gravity_i], fluxB, fluxF, fluxJacBNeg, fluxJacBPos, fluxJacFNeg, fluxJacFPos, /* end args for flux */ yAxis, toReconstructionScheme(m_recEn), U, m_meshObj.get(), @@ -574,7 +640,7 @@ class EigenApp /* end args for velo */ J, xAxis, m_meshObj.get(), /* end args for jac */ - m_fluxEn, normalX_, m_gravity, fluxL, fluxR, + m_fluxEn, normalX_, m_parameters[gravity_i], fluxL, fluxR, fluxJacLNeg, fluxJacLPos, fluxJacRNeg, fluxJacRPos, /* end args for flux */ toReconstructionScheme(m_recEn), stencilVals, @@ -586,7 +652,7 @@ class EigenApp /* end args for velo */ J, yAxis, m_meshObj.get(), /* end args for jac */ - m_fluxEn, normalY_, m_gravity, fluxB, fluxF, + m_fluxEn, normalY_, m_parameters[gravity_i], fluxB, fluxF, fluxJacBNeg, fluxJacBPos, fluxJacFNeg, fluxJacFPos, /* end args for flux */ toReconstructionScheme(m_recEn), stencilVals, @@ -685,7 +751,7 @@ class EigenApp velo_functor_type funcVeloX(V, m_meshObj.get().dxInv(), /* end args for velo */ - m_fluxEn, normalX_, m_gravity, fluxL, fluxR, + m_fluxEn, normalX_, m_parameters[gravity_i], fluxL, fluxR, /* end args for flux */ toReconstructionScheme(m_recEn), stencilValsForV, uMinusHalfNeg, uMinusHalfPos, uPlusHalfNeg, uPlusHalfPos @@ -694,7 +760,7 @@ class EigenApp velo_functor_type funcVeloY(V, m_meshObj.get().dyInv(), /* end args for velo */ - m_fluxEn, normalY_, m_gravity, fluxB, fluxF, + m_fluxEn, normalY_, m_parameters[gravity_i], fluxB, fluxF, /* end args for flux */ toReconstructionScheme(m_recEn), stencilValsForV, uMinusHalfNeg, uMinusHalfPos, uPlusHalfNeg, uPlusHalfPos @@ -725,7 +791,7 @@ class EigenApp jac_functor_type funcJacX(J, xAxis, m_meshObj.get(), /* end args for jac */ - m_fluxEn, normalX_, m_gravity, + m_fluxEn, normalX_, m_parameters[gravity_i], fluxJacLNeg, fluxJacLPos, fluxJacRNeg, fluxJacRPos, /* end args for flux */ toReconstructionScheme(firstOrderRec), stencilValsForJ, @@ -735,7 +801,7 @@ class EigenApp jac_functor_type funcJacY(J, yAxis, m_meshObj.get(), /* end args for jac */ - m_fluxEn, normalY_, m_gravity, + m_fluxEn, normalY_, m_parameters[gravity_i], fluxJacBNeg, fluxJacBPos, fluxJacFNeg, fluxJacFPos, /* end args for flux */ toReconstructionScheme(firstOrderRec), stencilValsForJ, @@ -842,7 +908,7 @@ class EigenApp functor_type Fx(V, m_meshObj.get().dxInv(), /* end args for velo */ - m_fluxEn, normalX_, m_gravity, fluxL, fluxR, + m_fluxEn, normalX_, m_parameters[gravity_i], fluxL, fluxR, /* end args for flux */ toReconstructionScheme(m_recEn),stencilVals, uMinusHalfNeg, uMinusHalfPos, uPlusHalfNeg, uPlusHalfPos @@ -851,7 +917,7 @@ class EigenApp functor_type Fy(V, m_meshObj.get().dyInv(), /* end args for velo */ - m_fluxEn, normalY_, m_gravity, fluxB, fluxF, + m_fluxEn, normalY_, m_parameters[gravity_i], fluxB, fluxF, /* end args for flux */ toReconstructionScheme(m_recEn),stencilVals, uMinusHalfNeg, uMinusHalfPos, uPlusHalfNeg, uPlusHalfPos @@ -900,7 +966,7 @@ class EigenApp functor_type Fx(V, m_meshObj.get().dxInv(), /* end args for velo */ - m_fluxEn, normalX_, m_gravity, fluxL, fluxR, + m_fluxEn, normalX_, m_parameters[gravity_i], fluxL, fluxR, /* end args for flux */ xAxis, toReconstructionScheme(m_recEn), U, m_meshObj.get(), uMinusHalfNeg, uMinusHalfPos, uPlusHalfNeg, uPlusHalfPos @@ -909,7 +975,7 @@ class EigenApp functor_type Fy(V, m_meshObj.get().dyInv(), /* end args for velo */ - m_fluxEn, normalY_, m_gravity, fluxB, fluxF, + m_fluxEn, normalY_, m_parameters[gravity_i], fluxB, fluxF, /* end args for flux */ yAxis, toReconstructionScheme(m_recEn), U, m_meshObj.get(), uMinusHalfNeg, uMinusHalfPos, uPlusHalfNeg, uPlusHalfPos @@ -933,10 +999,12 @@ class EigenApp V_t & V, index_t smPt) const { + + const auto coriolis = m_parameters[coriolis_i]; const auto vIndex = smPt*numDofPerCell; const auto uIndex = m_meshObj.get().graph()(smPt, 0)*numDofPerCell; - V(vIndex+1) -= m_coriolis*U(uIndex+2)/U(uIndex); - V(vIndex+2) += m_coriolis*U(uIndex+1)/U(uIndex); + V(vIndex+1) -= coriolis*U(uIndex+2)/U(uIndex); + V(vIndex+2) += coriolis*U(uIndex+1)/U(uIndex); } template @@ -945,14 +1013,16 @@ class EigenApp jacobian_type & J, index_t smPt) const { + + const auto coriolis = m_parameters[coriolis_i]; const auto vIndex = smPt*numDofPerCell; const index_t col_i = m_meshObj.get().graph()(smPt, 0)*numDofPerCell; - V(vIndex+1) -= m_coriolis*U(col_i+2)/U(col_i); - V(vIndex+2) += m_coriolis*U(col_i+1)/U(col_i); - J.coeffRef(vIndex+1, col_i) += m_coriolis*U(col_i+2)/(U(col_i)*U(col_i) ); - J.coeffRef(vIndex+1, col_i+2) += -m_coriolis/U(col_i); - J.coeffRef(vIndex+2, col_i+1) += m_coriolis/U(col_i); - J.coeffRef(vIndex+2, col_i) += -m_coriolis*U(col_i+1)/(U(col_i)*U(col_i) ); + V(vIndex+1) -= coriolis*U(col_i+2)/U(col_i); + V(vIndex+2) += coriolis*U(col_i+1)/U(col_i); + J.coeffRef(vIndex+1, col_i) += coriolis*U(col_i+2)/(U(col_i)*U(col_i) ); + J.coeffRef(vIndex+1, col_i+2) += -coriolis/U(col_i); + J.coeffRef(vIndex+2, col_i+1) += coriolis/U(col_i); + J.coeffRef(vIndex+2, col_i) += -coriolis*U(col_i+1)/(U(col_i)*U(col_i) ); } private: @@ -981,10 +1051,8 @@ class EigenApp mutable ghost_container_type m_ghostRight; mutable ghost_container_type m_ghostBack; - scalar_type m_gravity = {}; - scalar_type m_coriolis = {}; - scalar_type m_initialPulseMagnitude = {}; - + // gravity, coriolis, pulseMagnitude, pulseCenterX, pulseCenterY + std::vector m_parameters; std::array normalX_{1, 0}; std::array normalY_{0, 1}; diff --git a/include/pressiodemoapps/swe2d.hpp b/include/pressiodemoapps/swe2d.hpp index 1632d34f..46bfb179 100644 --- a/include/pressiodemoapps/swe2d.hpp +++ b/include/pressiodemoapps/swe2d.hpp @@ -94,16 +94,11 @@ RetType InviscidFluxReconstruction inviscRecEn) { - if (problemEnum == Swe2d::SlipWall) - { - return RetType(implswe2d::TagProblemSlipWall{}, - meshObj, inviscRecEn, + if (problemEnum == Swe2d::SlipWall){ + return RetType(implswe2d::TagProblemSlipWall{}, meshObj, inviscRecEn, ::pressiodemoapps::InviscidFluxScheme::Rusanov, - 9.8, // gravity - -3.0, // coriolis - 0.125); // pulse mag + implswe2d::create_vec_with_default_params()); } - else{ throw std::runtime_error("2D swe: invalid problem enum"); } @@ -129,7 +124,7 @@ create_problem_eigen(const mesh_t & meshObj, CustomBCsFunctorFront && customBCsFront, CustomBCsFunctorRight && customBCsRight, CustomBCsFunctorBack && customBCsBack, - [[maybe_unused]] int icFlag = 0 ) + const std::unordered_map & paramsMap) { if (problemEnum != Swe2d::CustomBCs){ @@ -144,7 +139,33 @@ create_problem_eigen(const mesh_t & meshObj, std::forward(customBCsRight), std::forward(customBCsBack)); return RetType(meshObj, problemEnum, recEnum, InviscidFluxScheme::Rusanov, - std::move(bcFuncs), 9.8 /*gravity*/, -3.0 /*coriolis*/, 0.125 /*pulse mag*/); + std::move(bcFuncs), implswe2d::param_unord_map_to_vector(paramsMap)); +} + +template< + class mesh_t, + class CustomBCsFunctorLeft, + class CustomBCsFunctorFront, + class CustomBCsFunctorRight, + class CustomBCsFunctorBack> +auto create_problem_eigen(const mesh_t & meshObj, + Swe2d problemEnum, + InviscidFluxReconstruction recEnum, + CustomBCsFunctorLeft && customBCsLeft, + CustomBCsFunctorFront && customBCsFront, + CustomBCsFunctorRight && customBCsRight, + CustomBCsFunctorBack && customBCsBack, + [[maybe_unused]] int icFlag = 0 ) +{ + + auto paramsVec = implswe2d::create_vec_with_default_params(); + auto paramsMap = implswe2d::param_vector_to_unord_map(paramsVec); + return create_problem_eigen(meshObj, problemEnum, recEnum, + std::forward(customBCsLeft), + std::forward(customBCsFront), + std::forward(customBCsRight), + std::forward(customBCsBack), + paramsMap); } #endif @@ -171,10 +192,13 @@ RetType typename mesh_t::scalar_t pulseMagnitude) { - return RetType(implswe2d::TagProblemSlipWall{}, - meshObj, inviscRecEn, - ::pressiodemoapps::InviscidFluxScheme::Rusanov, - gravity, coriolis, pulseMagnitude); + auto paramsVec = implswe2d::create_vec_with_default_params(); + paramsVec[implswe2d::param_string_to_index<>("gravity")] = gravity; + paramsVec[implswe2d::param_string_to_index<>("coriolis")] = coriolis; + paramsVec[implswe2d::param_string_to_index<>("pulseMagnitude")] = pulseMagnitude; + + return RetType(implswe2d::TagProblemSlipWall{}, meshObj, inviscRecEn, + ::pressiodemoapps::InviscidFluxScheme::Rusanov, std::move(paramsVec)); } }//end namespace pressiodemoapps diff --git a/tests_cpp/CMakeLists.txt b/tests_cpp/CMakeLists.txt index a1df825f..6354da06 100644 --- a/tests_cpp/CMakeLists.txt +++ b/tests_cpp/CMakeLists.txt @@ -85,6 +85,7 @@ add_subdirectory(eigen_2d_advdiffreac_probA_implicit) add_subdirectory(eigen_2d_euler_riemann_custom_bcs) add_subdirectory(eigen_2d_swe_custom_bcs) +add_subdirectory(eigen_2d_swe_slip_wall_parameters) # --------------------------------------------------------- # 3d problems diff --git a/tests_cpp/eigen_2d_swe_slip_wall_parameters/CMakeLists.txt b/tests_cpp/eigen_2d_swe_slip_wall_parameters/CMakeLists.txt new file mode 100644 index 00000000..5cb09aef --- /dev/null +++ b/tests_cpp/eigen_2d_swe_slip_wall_parameters/CMakeLists.txt @@ -0,0 +1,13 @@ + +set(testname eigen_2d_swe_slip_wall_parameters) +set(exename ${testname}_exe) +add_executable(${exename} ${CMAKE_CURRENT_SOURCE_DIR}/main.cc) + +add_test(NAME ${testname} +COMMAND ${CMAKE_COMMAND} +-DMESHDRIVER=${MESHSRC}/create_full_mesh.py +-DOUTDIR=${CMAKE_CURRENT_BINARY_DIR} +-DEXENAME=$ +-DSTENCILVAL=3 +-P ${CMAKE_CURRENT_SOURCE_DIR}/test.cmake +) diff --git a/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc b/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc new file mode 100644 index 00000000..b02552b8 --- /dev/null +++ b/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc @@ -0,0 +1,97 @@ + +#include "pressiodemoapps/swe2d.hpp" + +namespace +{ +template +void check(const AppT & app, + const std::unordered_map & gold) +{ + + for (auto it=gold.cbegin(); it!=gold.cend(); ++it){ + const auto value = app.queryParameter(it->first); + const auto diff = std::abs(value - it->second); + std::cout << it->first + << " : " + << " found = " << value + << " gold = " << it->second + << '\n'; + if ( diff > 1e-13 ){ + const std::string msg = "comparison for: " + it->first + ", failed"; + throw std::runtime_error(msg); + } + } +} + +} + +int main() +{ + namespace pda = pressiodemoapps; + const auto meshObj = pda::load_cellcentered_uniform_mesh_eigen("."); + // this test only cares about the physical parameters, + // it does not matter here which flux scheme we use + const auto flsc = pda::InviscidFluxReconstruction::FirstOrder; + + { + std::cout << "\ntesting overload 1\n"; + const auto probId = pda::Swe2d::SlipWall; + auto appObj = pda::create_problem_eigen(meshObj, probId, flsc); + std::unordered_map gold; + gold["gravity"] = 9.8; + gold["coriolis"] = static_cast(-3); + gold["pulseMagnitude"] = static_cast(1)/8; + gold["pulseX"] = static_cast(1); + gold["pulseY"] = static_cast(1); + check(appObj, gold); + } + + { + std::cout << "\ntesting overload 2\n"; + auto noop = pda::impl::NoOperation(); + auto appObj = pda:: create_slip_wall_swe_2d_problem_eigen(meshObj, flsc, + 12., 4.4, 0.5); + std::unordered_map gold; + gold["gravity"] = 12.; + gold["coriolis"] = static_cast(4.4); + gold["pulseMagnitude"] = static_cast(1)/2; + gold["pulseX"] = static_cast(1); + gold["pulseY"] = static_cast(1); + check(appObj, gold); + } + + { + std::cout << "\ntesting overload 3\n"; + const auto probId = pda::Swe2d::CustomBCs; + auto noop = pda::impl::NoOperation(); + auto appObj = pda::create_problem_eigen(meshObj, probId, flsc, + noop, noop, noop, noop); + std::unordered_map gold; + gold["gravity"] = 9.8; + gold["coriolis"] = static_cast(-3); + gold["pulseMagnitude"] = static_cast(1)/8; + gold["pulseX"] = static_cast(1); + gold["pulseY"] = static_cast(1); + check(appObj, gold); + } + + { + std::cout << "\ntesting overload 4\n"; + const auto probId = pda::Swe2d::CustomBCs; + auto noop = pda::impl::NoOperation(); + std::unordered_map myparams; + myparams["gravity"] = 10; + myparams["coriolis"] = static_cast(-5); + myparams["pulseMagnitude"] = static_cast(1.5)/8; + myparams["pulseX"] = static_cast(1.5); + myparams["pulseY"] = static_cast(1.5); + + auto appObj = pda::create_problem_eigen(meshObj, probId, flsc, + noop, noop, noop, noop, + myparams); + check(appObj, myparams); + } + + + return 0; +} diff --git a/tests_cpp/eigen_2d_swe_slip_wall_parameters/test.cmake b/tests_cpp/eigen_2d_swe_slip_wall_parameters/test.cmake new file mode 100644 index 00000000..12c298ea --- /dev/null +++ b/tests_cpp/eigen_2d_swe_slip_wall_parameters/test.cmake @@ -0,0 +1,16 @@ +include(FindUnixCommands) + +set(CMD "python3 ${MESHDRIVER} -n 8 8 --outDir ${OUTDIR} -s ${STENCILVAL} --bounds -5.0 5.0 -5.0 5.0") +execute_process(COMMAND ${BASH} -c ${CMD} RESULT_VARIABLE RES) +if(RES) + message(FATAL_ERROR "Mesh generation failed") +else() + message("Mesh generation succeeded!") +endif() + +execute_process(COMMAND ${EXENAME} RESULT_VARIABLE CMD_RESULT) +if(RES) + message(FATAL_ERROR "run failed") +else() + message("run succeeded!") +endif() From 29ec820f36a538a38e74206b26d602d2b8ab1b82 Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Fri, 1 Sep 2023 15:32:28 -0600 Subject: [PATCH 2/3] remove unused --- tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc b/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc index b02552b8..a984d402 100644 --- a/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc +++ b/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc @@ -48,7 +48,6 @@ int main() { std::cout << "\ntesting overload 2\n"; - auto noop = pda::impl::NoOperation(); auto appObj = pda:: create_slip_wall_swe_2d_problem_eigen(meshObj, flsc, 12., 4.4, 0.5); std::unordered_map gold; From ea761c5a8f076fa34971ee0a499206903d2f5e8e Mon Sep 17 00:00:00 2001 From: Francesco Rizzi Date: Fri, 1 Sep 2023 16:47:00 -0600 Subject: [PATCH 3/3] address comments --- .../impl/swe_2d_prob_class.hpp | 27 ++++++++----- include/pressiodemoapps/swe2d.hpp | 40 ++++++++++++++----- .../eigen_2d_swe_slip_wall_parameters/main.cc | 22 ++++++++-- 3 files changed, 64 insertions(+), 25 deletions(-) diff --git a/include/pressiodemoapps/impl/swe_2d_prob_class.hpp b/include/pressiodemoapps/impl/swe_2d_prob_class.hpp index 3c604493..b7bda2c1 100644 --- a/include/pressiodemoapps/impl/swe_2d_prob_class.hpp +++ b/include/pressiodemoapps/impl/swe_2d_prob_class.hpp @@ -77,16 +77,6 @@ constexpr int pulseMagnitude_i = 2; constexpr int pulseX_i = 3; constexpr int pulseY_i = 4; -template -auto create_vec_with_default_params(){ - const auto gravity = static_cast(9.8); - const auto coriolis = static_cast(-3); - const auto pulseMag = static_cast(1)/8; - const auto pulseX = static_cast(1); - const auto pulseY = pulseX; - return std::vector({gravity, coriolis, pulseMag, pulseX, pulseY}); -} - template const std::string param_index_to_string(IntT index){ if (index == gravity_i) { return "gravity"; } @@ -107,6 +97,16 @@ int param_string_to_index(const std::string & s){ else{ return std::numeric_limits::max(); } } +template +auto create_vec_with_default_params(){ + const auto gravity = static_cast(9.8); + const auto coriolis = static_cast(-3); + const auto pulseMag = static_cast(1)/8; + const auto pulseX = static_cast(1); + const auto pulseY = pulseX; + return std::vector({gravity, coriolis, pulseMag, pulseX, pulseY}); +} + template auto param_unord_map_to_vector(const std::unordered_map & map) { @@ -130,6 +130,13 @@ auto param_vector_to_unord_map(const std::vector & vec) return result; } +template +auto create_map_with_default_params(){ + const auto vec = create_vec_with_default_params(); + return param_vector_to_unord_map(vec); +} + + // tags are used inside he public create function: create_problem_...() // in the file ../advection_diffusion.hpp // to dispatch to the proper problem diff --git a/include/pressiodemoapps/swe2d.hpp b/include/pressiodemoapps/swe2d.hpp index 46bfb179..e0965679 100644 --- a/include/pressiodemoapps/swe2d.hpp +++ b/include/pressiodemoapps/swe2d.hpp @@ -78,22 +78,15 @@ namespace pressiodemoapps{ // create default problem // ---------------------------------------------------------- +#if defined PRESSIODEMOAPPS_ENABLE_BINDINGS template< class mesh_t, class RetType = PublicProblemEigenMixinCpp> > -RetType -// bindings need unique naming or we get error associated with overloads -#if defined PRESSIODEMOAPPS_ENABLE_BINDINGS - create_swe2d_problem_default_for_py -#else - create_problem_eigen -#endif -(const mesh_t & meshObj, - Swe2d problemEnum, - InviscidFluxReconstruction inviscRecEn) +RetType create_swe2d_problem_default_for_py(const mesh_t & meshObj, + Swe2d problemEnum, + InviscidFluxReconstruction inviscRecEn) { - if (problemEnum == Swe2d::SlipWall){ return RetType(implswe2d::TagProblemSlipWall{}, meshObj, inviscRecEn, ::pressiodemoapps::InviscidFluxScheme::Rusanov, @@ -104,6 +97,31 @@ RetType } } +#else + +template< + class mesh_t, + class RetType = PublicProblemEigenMixinCpp> + > +RetType +create_problem_eigen(const mesh_t & meshObj, + Swe2d problemEnum, + InviscidFluxReconstruction inviscRecEn, + const std::unordered_map & paramsMap + = implswe2d::create_map_with_default_params()) +{ + + if (problemEnum == Swe2d::SlipWall){ + auto paramsVec = implswe2d::param_unord_map_to_vector(paramsMap); + return RetType(implswe2d::TagProblemSlipWall{}, meshObj, inviscRecEn, + ::pressiodemoapps::InviscidFluxScheme::Rusanov, std::move(paramsVec)); + } + else{ + throw std::runtime_error("2D swe: invalid problem enum"); + } +} +#endif + #if !defined PRESSIODEMOAPPS_ENABLE_BINDINGS template< class mesh_t, diff --git a/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc b/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc index a984d402..88e36178 100644 --- a/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc +++ b/tests_cpp/eigen_2d_swe_slip_wall_parameters/main.cc @@ -34,7 +34,7 @@ int main() const auto flsc = pda::InviscidFluxReconstruction::FirstOrder; { - std::cout << "\ntesting overload 1\n"; + std::cout << "\ntesting overload \n"; const auto probId = pda::Swe2d::SlipWall; auto appObj = pda::create_problem_eigen(meshObj, probId, flsc); std::unordered_map gold; @@ -47,7 +47,21 @@ int main() } { - std::cout << "\ntesting overload 2\n"; + std::cout << "\ntesting overload \n"; + const auto probId = pda::Swe2d::SlipWall; + std::unordered_map myparams; + myparams["gravity"] = 10; + myparams["coriolis"] = static_cast(-5); + myparams["pulseMagnitude"] = static_cast(1.5)/8; + myparams["pulseX"] = static_cast(1.5); + myparams["pulseY"] = static_cast(1.5); + + auto appObj = pda::create_problem_eigen(meshObj, probId, flsc, myparams); + check(appObj, myparams); + } + + { + std::cout << "\ntesting overload \n"; auto appObj = pda:: create_slip_wall_swe_2d_problem_eigen(meshObj, flsc, 12., 4.4, 0.5); std::unordered_map gold; @@ -60,7 +74,7 @@ int main() } { - std::cout << "\ntesting overload 3\n"; + std::cout << "\ntesting overload \n"; const auto probId = pda::Swe2d::CustomBCs; auto noop = pda::impl::NoOperation(); auto appObj = pda::create_problem_eigen(meshObj, probId, flsc, @@ -75,7 +89,7 @@ int main() } { - std::cout << "\ntesting overload 4\n"; + std::cout << "\ntesting overload \n"; const auto probId = pda::Swe2d::CustomBCs; auto noop = pda::impl::NoOperation(); std::unordered_map myparams;