Skip to content

Commit

Permalink
catch NaNs in static solver
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Reep authored and Jeffrey Reep committed Jul 18, 2024
1 parent 824f76d commit 66462fd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
7 changes: 6 additions & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ int main(int argc, char *argv[])
{
//Declarations
int num_steps;
int fail = 1;
state_type state;
char config[256];
LOOP loop;
Expand Down Expand Up @@ -74,7 +75,6 @@ int main(int argc, char *argv[])
// Start integration loop
while(t<loop->parameters.total_time)
{
int fail = 1;
int num_failures = 0;
while(fail>0)
{
Expand Down Expand Up @@ -106,6 +106,11 @@ int main(int argc, char *argv[])
{
// Constant timestep integration
num_steps = boost::numeric::odeint::integrate_const( controlled_stepper, loop->CalculateDerivs, state, loop->parameters.tau, loop->parameters.total_time, loop->parameters.tau, obs->Observe);
fail = obs->CheckNan(state);
if(fail)
{
throw std::runtime_error("NaNs were detected in the output. Check the input configuration.");
}
}

//Print results to file
Expand Down
15 changes: 15 additions & 0 deletions source/observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ int Observer::CheckNan(state_type &state, double &time, double &tau, double old_
// Pass otherwise
return 0;
}

int Observer::CheckNan(state_type &state)
{
// Check for NaNs in the state
for(int j=0; j<state.size(); j++)
{
if(std::isnan(state[j]))
{
return 1;
}
}

// Pass otherwise
return 0;
}
3 changes: 3 additions & 0 deletions source/observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ class Observer {
// Boost integrator does not check for NaNs so this is done manually. If a
// NaN is found anywhere in the state vector, the state and time are set
// back to the previous step and the timestep is reduced.
// The overloaded function, for use with the static time step solver, only checks for NaNs
// and does not reset the state or time.
int CheckNan(state_type &state, double &time, double &tau, double old_time, double old_tau);
int CheckNan(state_type &state);
};
// Pointer to the <Observer> class
typedef Observer* OBSERVER;
Expand Down
12 changes: 11 additions & 1 deletion tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,14 @@ def test_insufficient_heating(base_config, value):
config['use_adaptive_solver'] = False
config['heating']['background'] = value
with pytest.raises(EbtelPlusPlusError):
run_ebtelplusplus(config)
run_ebtelplusplus(config)

def test_NaNs_in_static_solver(base_config):
config = base_config.copy()
config['use_adaptive_solver'] = False
config['heating']['events'] = [
{'event': {'rise_start': 0.0, 'rise_end': 100.0, 'decay_start': 100.0,
'decay_end': 200.0, 'magnitude': -10.0}}
]
with pytest.raises(EbtelPlusPlusError):
run_ebtelplusplus(config)

0 comments on commit 66462fd

Please sign in to comment.