Skip to content

Commit

Permalink
Refactor warning code into member functions
Browse files Browse the repository at this point in the history
  • Loading branch information
n01r authored and ax3l committed Sep 20, 2022
1 parent 923ca59 commit 73f4bf8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 38 deletions.
10 changes: 10 additions & 0 deletions src/ImpactX.H
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,20 @@ namespace impactx
*/
void validate ();

/** Check inputs for unused parameters (e.g. typos) after step 1
*/
void early_param_check ();

/** Run the main simulation loop for a number of steps
*/
void evolve ();

/** Query input for warning logger variables and set up warning logger accordingly
*
* Input variables are: ``always_warn_immediately`` and ``abort_on_warning_threshold``.
*/
void warn_logger_control ();

private:
//! Tag cells for refinement. TagBoxArray tags is built on level lev grids.
void ErrorEst (int lev, amrex::TagBoxArray& tags, amrex::Real time,
Expand Down
91 changes: 53 additions & 38 deletions src/ImpactX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,41 @@ namespace impactx

// todo: if charge deposition and/or space charge are requested, require
// amr.n_cells from user inputs

// query input for warning logger variables and set up warning logger accordingly
warn_logger_control();

}

void ImpactX::warn_logger_control()
{
amrex::ParmParse pp_impactx("impactx");

//"Synthetic" warning messages may be injected in the Warning Manager via
// inputfile for debug&testing purposes.
ablastr::warn_manager::GetWMInstance().debug_read_warnings_from_input(pp_impactx);

// Set the flag to control if ImpactX has to emit a warning message as soon as a warning is recorded
bool always_warn_immediately = false;
pp_impactx.query("always_warn_immediately", always_warn_immediately);
ablastr::warn_manager::GetWMInstance().SetAlwaysWarnImmediately(always_warn_immediately);

// Set the WarnPriority threshold to decide if ImpactX has to abort when a warning is recorded
if(std::string str_abort_on_warning_threshold = "";
pp_impactx.query("abort_on_warning_threshold", str_abort_on_warning_threshold)){
std::optional<ablastr::warn_manager::WarnPriority> abort_on_warning_threshold = std::nullopt;
if (str_abort_on_warning_threshold == "high")
abort_on_warning_threshold = ablastr::warn_manager::WarnPriority::high;
else if (str_abort_on_warning_threshold == "medium" )
abort_on_warning_threshold = ablastr::warn_manager::WarnPriority::medium;
else if (str_abort_on_warning_threshold == "low")
abort_on_warning_threshold = ablastr::warn_manager::WarnPriority::low;
else {
amrex::Abort(ablastr::utils::TextMsg::Err(str_abort_on_warning_threshold
+"is not a valid option for impactx.abort_on_warning_threshold (use: low, medium or high)"));
}
ablastr::warn_manager::GetWMInstance().SetAbortThreshold(abort_on_warning_threshold);
}
}

void ImpactX::initGrids ()
Expand All @@ -57,6 +92,21 @@ namespace impactx
amrex::UtilCreateCleanDirectory("diags", true);
}

void ImpactX::early_param_check()
{
BL_PROFILE("ImpactX::early_param_check");

amrex::Print() << "\n"; // better: conditional \n based on return value
amrex::ParmParse().QueryUnusedInputs();

//Print the warning list right after the first step.
amrex::Print() << ablastr::warn_manager::GetWMInstance()
.PrintGlobalWarnings("FIRST STEP");

early_params_checked = true;

}

void ImpactX::evolve ()
{
BL_PROFILE("ImpactX::evolve");
Expand All @@ -67,7 +117,8 @@ namespace impactx
// before we start the evolve loop, we are in "step 0" (initial state)
int global_step = 0;

bool early_params_checked = false; // check typos in inputs after step 1
// check typos in inputs after step 1
bool early_params_checked = false;

// count particles - if no particles are found in our particle container, then a lot of
// AMReX routines over ParIter won't work and we have nothing to do here anyways
Expand Down Expand Up @@ -118,34 +169,6 @@ namespace impactx
pp_algo.queryAdd("space_charge", space_charge);
amrex::Print() << " Space Charge effects: " << space_charge << "\n";

amrex::ParmParse pp_impactx("impactx");

//"Synthetic" warning messages may be injected in the Warning Manager via
// inputfile for debug&testing purposes.
ablastr::warn_manager::GetWMInstance().debug_read_warnings_from_input(pp_impactx);

// Set the flag to control if WarpX has to emit a warning message as soon as a warning is recorded
bool always_warn_immediately = false;
pp_impactx.query("always_warn_immediately", always_warn_immediately);
ablastr::warn_manager::GetWMInstance().SetAlwaysWarnImmediately(always_warn_immediately);

// Set the WarnPriority threshold to decide if ImpactX has to abort when a warning is recorded
if(std::string str_abort_on_warning_threshold = "";
pp_impactx.query("abort_on_warning_threshold", str_abort_on_warning_threshold)){
std::optional<ablastr::warn_manager::WarnPriority> abort_on_warning_threshold = std::nullopt;
if (str_abort_on_warning_threshold == "high")
abort_on_warning_threshold = ablastr::warn_manager::WarnPriority::high;
else if (str_abort_on_warning_threshold == "medium" )
abort_on_warning_threshold = ablastr::warn_manager::WarnPriority::medium;
else if (str_abort_on_warning_threshold == "low")
abort_on_warning_threshold = ablastr::warn_manager::WarnPriority::low;
else {
amrex::Abort(ablastr::utils::TextMsg::Err(str_abort_on_warning_threshold
+"is not a valid option for impactx.abort_on_warning_threshold (use: low, medium or high)"));
}
ablastr::warn_manager::GetWMInstance().SetAbortThreshold(abort_on_warning_threshold);
}

// loop over all beamline elements
for (auto & element_variant : m_lattice)
{
Expand Down Expand Up @@ -247,15 +270,7 @@ namespace impactx
}

// inputs: unused parameters (e.g. typos) check after step 1 has finished
if (!early_params_checked) {
amrex::Print() << "\n"; // better: conditional \n based on return value
amrex::ParmParse().QueryUnusedInputs();

//Print the warning list right after the first step.
amrex::Print() << ablastr::warn_manager::GetWMInstance()
.PrintGlobalWarnings("FIRST STEP");
early_params_checked = true;
}
if (!early_params_checked) {early_param_check();}

} // end in-element space-charge slice-step loop
} // end beamline element loop
Expand Down

0 comments on commit 73f4bf8

Please sign in to comment.