From d0bf05e9b6b6eb69153dc47689521100d253ba98 Mon Sep 17 00:00:00 2001 From: Andy Nonaka Date: Tue, 24 Nov 2026 15:05:13 -0800 Subject: [PATCH] lumped element cleanup --- .../MacroscopicEvolveE.cpp | 41 +++++----- .../MacroscopicProperties.cpp | 77 +++++++++++++------ Source/WarpX.cpp | 27 ++++++- 3 files changed, 99 insertions(+), 46 deletions(-) diff --git a/Source/FieldSolver/FiniteDifferenceSolver/MacroscopicEvolveE.cpp b/Source/FieldSolver/FiniteDifferenceSolver/MacroscopicEvolveE.cpp index c4ae9a8b6..cd926ec3c 100644 --- a/Source/FieldSolver/FiniteDifferenceSolver/MacroscopicEvolveE.cpp +++ b/Source/FieldSolver/FiniteDifferenceSolver/MacroscopicEvolveE.cpp @@ -141,6 +141,20 @@ void FiniteDifferenceSolver::MacroscopicEvolveECartesian ( int use_lumped_resistor = warpx.use_lumped_resistor; int use_lumped_capacitor = warpx.use_lumped_capacitor; + // Geometric factor (edge length)/(cross-sectional area) for a lumped element on an + // x-, y-, or z-edge: sigma_eff = geom/R, eps_eff = eps + C*geom. + // Lumped elements are only supported in 3D (enforced in WarpX::ReadParameters). +#if defined(WARPX_DIM_3D) + amrex::Real const lumped_geom_x = dx[0] / (dx[1]*dx[2]); + amrex::Real const lumped_geom_y = dx[1] / (dx[0]*dx[2]); + amrex::Real const lumped_geom_z = dx[2] / (dx[0]*dx[1]); +#else + amrex::ignore_unused(dx); + amrex::Real const lumped_geom_x = 0._rt; + amrex::Real const lumped_geom_y = 0._rt; + amrex::Real const lumped_geom_z = 0._rt; +#endif + amrex::MultiFab& sigma_mf = macroscopic_properties->getsigma_mf(); amrex::MultiFab& epsilon_mf = macroscopic_properties->getepsilon_mf(); @@ -173,18 +187,6 @@ void FiniteDifferenceSolver::MacroscopicEvolveECartesian ( amrex::GpuArray const& epsilon_stag = macroscopic_properties->epsilon_IndexType; amrex::GpuArray const& macro_cr = macroscopic_properties->macro_cr_ratio; - if (use_lumped_resistor){ - amrex::GpuArray const& lumped_resistor_x_stag = macroscopic_properties->lumped_resistor_x_IndexType; - amrex::GpuArray const& lumped_resistor_y_stag = macroscopic_properties->lumped_resistor_y_IndexType; - amrex::GpuArray const& lumped_resistor_z_stag = macroscopic_properties->lumped_resistor_z_IndexType; - } - - if (use_lumped_capacitor){ - amrex::GpuArray const& lumped_capacitor_x_stag = macroscopic_properties->lumped_capacitor_x_IndexType; - amrex::GpuArray const& lumped_capacitor_y_stag = macroscopic_properties->lumped_capacitor_y_IndexType; - amrex::GpuArray const& lumped_capacitor_z_stag = macroscopic_properties->lumped_capacitor_z_IndexType; - } - amrex::GpuArray const& Ex_stag = macroscopic_properties->Ex_IndexType; amrex::GpuArray const& Ey_stag = macroscopic_properties->Ey_IndexType; amrex::GpuArray const& Ez_stag = macroscopic_properties->Ez_IndexType; @@ -283,12 +285,12 @@ void FiniteDifferenceSolver::MacroscopicEvolveECartesian ( // Extra conductance term from a lumped resistor on Ex edges amrex::Real const fac1 = (dt/epsilon_interp) * ( sigma_interp + ((use_lumped_resistor && resistor_x_arr(i,j,k)!=0._rt) - ? dx[0] / (dx[1]*dx[2]*resistor_x_arr(i,j,k)) + ? lumped_geom_x / resistor_x_arr(i,j,k) : 0._rt) ); // Extra term from a lumped capacitor on Ex edges amrex::Real const fac2 = (use_lumped_capacitor && capacitor_x_arr(i,j,k)!=0._rt) - ? capacitor_x_arr(i,j,k) * dx[0] / (dx[1]*dx[2]*epsilon_interp) + ? capacitor_x_arr(i,j,k) * lumped_geom_x / epsilon_interp : 0._rt; amrex::Real alpha_compact = T_MacroAlgo::alpha_compact(fac1, fac2); @@ -320,15 +322,12 @@ void FiniteDifferenceSolver::MacroscopicEvolveECartesian ( // Extra conductance term from a lumped resistor on Ey edges amrex::Real const fac1 = (dt/epsilon_interp) * ( sigma_interp + ((use_lumped_resistor && resistor_y_arr(i,j,k)!=0._rt) - ? dx[1] / (dx[0]*dx[2]*resistor_y_arr(i,j,k)) + ? lumped_geom_y / resistor_y_arr(i,j,k) : 0._rt) ); - // if (use_lumped_resistor && resistor_y_arr(i,j,k)!=0._rt){ - // printf("fac1=%f\n", fac1); - // } // Extra term from a lumped capacitor on Ey edges amrex::Real const fac2 = (use_lumped_capacitor && capacitor_y_arr(i,j,k)!=0._rt) - ? capacitor_y_arr(i,j,k) * dx[1] / (dx[0]*dx[2]*epsilon_interp) + ? capacitor_y_arr(i,j,k) * lumped_geom_y / epsilon_interp : 0._rt; amrex::Real alpha_compact = T_MacroAlgo::alpha_compact(fac1, fac2); @@ -355,12 +354,12 @@ void FiniteDifferenceSolver::MacroscopicEvolveECartesian ( // Extra conductance term from a lumped resistor on Ez edges amrex::Real const fac1 = (dt/epsilon_interp) * ( sigma_interp + ((use_lumped_resistor && resistor_z_arr(i,j,k)!=0._rt) - ? dx[2] / (dx[0]*dx[1]*resistor_z_arr(i,j,k)) + ? lumped_geom_z / resistor_z_arr(i,j,k) : 0._rt) ); // Extra term from a lumped capacitor on Ez edges amrex::Real const fac2 = (use_lumped_capacitor && capacitor_z_arr(i,j,k)!=0._rt) - ? capacitor_z_arr(i,j,k) * dx[2] / (dx[0]*dx[1]*epsilon_interp) + ? capacitor_z_arr(i,j,k) * lumped_geom_z / epsilon_interp : 0._rt; amrex::Real alpha_compact = T_MacroAlgo::alpha_compact(fac1, fac2); diff --git a/Source/FieldSolver/FiniteDifferenceSolver/MacroscopicProperties/MacroscopicProperties.cpp b/Source/FieldSolver/FiniteDifferenceSolver/MacroscopicProperties/MacroscopicProperties.cpp index 2fcde2fc5..853b04f71 100644 --- a/Source/FieldSolver/FiniteDifferenceSolver/MacroscopicProperties/MacroscopicProperties.cpp +++ b/Source/FieldSolver/FiniteDifferenceSolver/MacroscopicProperties/MacroscopicProperties.cpp @@ -166,32 +166,42 @@ MacroscopicProperties::ReadParameters () } - if (warpx.use_lumped_resistor == 1){ - utils::parser::Store_parserString(pp_macroscopic, "lumped_resistor_x_function(x,y,z)", m_str_lumped_resistor_x_function); - m_lumped_resistor_x_parser = std::make_unique( - utils::parser::makeParser(m_str_lumped_resistor_x_function,{"x","y","z"})); - - utils::parser::Store_parserString(pp_macroscopic, "lumped_resistor_y_function(x,y,z)", m_str_lumped_resistor_y_function); - m_lumped_resistor_y_parser = std::make_unique( - utils::parser::makeParser(m_str_lumped_resistor_y_function,{"x","y","z"})); + // Lumped resistor / capacitor values per edge direction. Any direction that is not + // specified defaults to "0", which means "no element" on those edges. + auto read_lumped_parser = [&pp_macroscopic] (std::string const& name, std::string& str, + std::unique_ptr& parser) -> bool { + bool const specified = pp_macroscopic.contains(name.c_str()); + if (specified) { + utils::parser::Store_parserString(pp_macroscopic, name, str); + } else { + str = "0"; + } + parser = std::make_unique(utils::parser::makeParser(str, {"x","y","z"})); + return specified; + }; - utils::parser::Store_parserString(pp_macroscopic, "lumped_resistor_z_function(x,y,z)", m_str_lumped_resistor_z_function); - m_lumped_resistor_z_parser = std::make_unique( - utils::parser::makeParser(m_str_lumped_resistor_z_function,{"x","y","z"})); + if (warpx.use_lumped_resistor == 1){ + bool any = false; + any |= read_lumped_parser("lumped_resistor_x_function(x,y,z)", m_str_lumped_resistor_x_function, m_lumped_resistor_x_parser); + any |= read_lumped_parser("lumped_resistor_y_function(x,y,z)", m_str_lumped_resistor_y_function, m_lumped_resistor_y_parser); + any |= read_lumped_parser("lumped_resistor_z_function(x,y,z)", m_str_lumped_resistor_z_function, m_lumped_resistor_z_parser); + if (!any) { + ablastr::warn_manager::WMRecordWarning("Lumped elements", + "algo.use_lumped_resistor = 1 but no macroscopic.lumped_resistor_{x,y,z}_function(x,y,z) " + "was given; no resistors will be applied."); + } } if (warpx.use_lumped_capacitor == 1){ - utils::parser::Store_parserString(pp_macroscopic, "lumped_capacitor_x_function(x,y,z)", m_str_lumped_capacitor_x_function); - m_lumped_capacitor_x_parser = std::make_unique( - utils::parser::makeParser(m_str_lumped_capacitor_x_function,{"x","y","z"})); - - utils::parser::Store_parserString(pp_macroscopic, "lumped_capacitor_y_function(x,y,z)", m_str_lumped_capacitor_y_function); - m_lumped_capacitor_y_parser = std::make_unique( - utils::parser::makeParser(m_str_lumped_capacitor_y_function,{"x","y","z"})); - - utils::parser::Store_parserString(pp_macroscopic, "lumped_capacitor_z_function(x,y,z)", m_str_lumped_capacitor_z_function); - m_lumped_capacitor_z_parser = std::make_unique( - utils::parser::makeParser(m_str_lumped_capacitor_z_function,{"x","y","z"})); + bool any = false; + any |= read_lumped_parser("lumped_capacitor_x_function(x,y,z)", m_str_lumped_capacitor_x_function, m_lumped_capacitor_x_parser); + any |= read_lumped_parser("lumped_capacitor_y_function(x,y,z)", m_str_lumped_capacitor_y_function, m_lumped_capacitor_y_parser); + any |= read_lumped_parser("lumped_capacitor_z_function(x,y,z)", m_str_lumped_capacitor_z_function, m_lumped_capacitor_z_parser); + if (!any) { + ablastr::warn_manager::WMRecordWarning("Lumped elements", + "algo.use_lumped_capacitor = 1 but no macroscopic.lumped_capacitor_{x,y,z}_function(x,y,z) " + "was given; no capacitors will be applied."); + } } #ifdef WARPX_MAG_LLG @@ -293,6 +303,18 @@ MacroscopicProperties::InitData () jy_IndexType[idim] = jy_stag[idim]; jz_IndexType[idim] = jz_stag[idim]; } + // lumped elements live at the same (edge) locations as J and E + for (int idim = 0; idim < 3; ++idim) { + int const sx = (idim < AMREX_SPACEDIM) ? jx_stag[idim] : 0; + int const sy = (idim < AMREX_SPACEDIM) ? jy_stag[idim] : 0; + int const sz = (idim < AMREX_SPACEDIM) ? jz_stag[idim] : 0; + lumped_resistor_x_IndexType[idim] = sx; + lumped_resistor_y_IndexType[idim] = sy; + lumped_resistor_z_IndexType[idim] = sz; + lumped_capacitor_x_IndexType[idim] = sx; + lumped_capacitor_y_IndexType[idim] = sy; + lumped_capacitor_z_IndexType[idim] = sz; + } // Define material property multifabs using ba and dmap from WarpX instance // sigma is cell-centered MultiFab @@ -411,6 +433,12 @@ MacroscopicProperties::InitData () InitializeMacroMultiFabUsingParser(m_lumped_resistor_x_mf.get(), m_lumped_resistor_x_parser->compile<3>(), lev); InitializeMacroMultiFabUsingParser(m_lumped_resistor_y_mf.get(), m_lumped_resistor_y_parser->compile<3>(), lev); InitializeMacroMultiFabUsingParser(m_lumped_resistor_z_mf.get(), m_lumped_resistor_z_parser->compile<3>(), lev); + + // R < 0 is a negative conductance and makes the E update unstable; R = 0 means "no resistor" + for (auto const* mf : {m_lumped_resistor_x_mf.get(), m_lumped_resistor_y_mf.get(), m_lumped_resistor_z_mf.get()}) { + WARPX_ALWAYS_ASSERT_WITH_MESSAGE(mf->min(0, mf->nGrow()) >= 0._rt, + "macroscopic.lumped_resistor_*_function must be non-negative (0 means no resistor)"); + } } if (warpx.use_lumped_capacitor == 1){ @@ -421,6 +449,11 @@ MacroscopicProperties::InitData () InitializeMacroMultiFabUsingParser(m_lumped_capacitor_x_mf.get(), m_lumped_capacitor_x_parser->compile<3>(), lev); InitializeMacroMultiFabUsingParser(m_lumped_capacitor_y_mf.get(), m_lumped_capacitor_y_parser->compile<3>(), lev); InitializeMacroMultiFabUsingParser(m_lumped_capacitor_z_mf.get(), m_lumped_capacitor_z_parser->compile<3>(), lev); + + for (auto const* mf : {m_lumped_capacitor_x_mf.get(), m_lumped_capacitor_y_mf.get(), m_lumped_capacitor_z_mf.get()}) { + WARPX_ALWAYS_ASSERT_WITH_MESSAGE(mf->min(0, mf->nGrow()) >= 0._rt, + "macroscopic.lumped_capacitor_*_function must be non-negative (0 means no capacitor)"); + } } #ifdef WARPX_MAG_LLG diff --git a/Source/WarpX.cpp b/Source/WarpX.cpp index d3abb397b..3b9deeab2 100644 --- a/Source/WarpX.cpp +++ b/Source/WarpX.cpp @@ -1073,9 +1073,6 @@ WarpX::ReadParameters () ); } - pp_warpx.query("use_lumped_resistor", use_lumped_resistor); - pp_warpx.query("use_lumped_capacitor", use_lumped_capacitor); - #ifdef WARPX_MAG_LLG // Read the value of the time advancement scheme of M field pp_warpx.query("mag_time_scheme_order", mag_time_scheme_order); @@ -1313,6 +1310,30 @@ WarpX::ReadParameters () pp_algo.query("use_lumped_inductor",use_lumped_inductor); pp_algo.query("use_josephson_junction",use_josephson_junction); + // Lumped resistor / capacitor (values set via macroscopic.lumped_{resistor,capacitor}_*_function) + pp_algo.query("use_lumped_resistor", use_lumped_resistor); + pp_algo.query("use_lumped_capacitor", use_lumped_capacitor); + { + // backward compatibility: these used to be read as warpx.use_lumped_* + const amrex::ParmParse pp_warpx_old("warpx"); + for (auto const& [name, flag] : {std::pair{"use_lumped_resistor", &use_lumped_resistor}, + std::pair{"use_lumped_capacitor", &use_lumped_capacitor}}) { + if (pp_warpx_old.contains(name)) { + ablastr::warn_manager::WMRecordWarning("Lumped elements", + std::string("warpx.") + name + " is deprecated; use algo." + name + " instead."); + if (!pp_algo.contains(name)) { pp_warpx_old.query(name, *flag); } + } + } + } + if (use_lumped_resistor || use_lumped_capacitor) { + WARPX_ALWAYS_ASSERT_WITH_MESSAGE(em_solver_medium == MediumForEM::Macroscopic, + "algo.use_lumped_resistor / algo.use_lumped_capacitor require algo.em_solver_medium = macroscopic"); +#if !defined(WARPX_DIM_3D) + WARPX_ABORT_WITH_MESSAGE( + "algo.use_lumped_resistor / algo.use_lumped_capacitor are only implemented in 3D"); +#endif + } + // Load balancing parameters std::vector load_balance_intervals_string_vec = {"0"}; pp_algo.queryarr("load_balance_intervals", load_balance_intervals_string_vec);