Skip to content

Commit

Permalink
Catch NaNs in static solver (rice-solar-physics#94)
Browse files Browse the repository at this point in the history
* catch NaNs in static solver

* fix bug with integer

* add test to check for NaNs with adaptive solver

* merge tests into one function

---------

Co-authored-by: Jeffrey Reep <[email protected]>
  • Loading branch information
jwreep and Jeffrey Reep authored Aug 16, 2024
1 parent 824f76d commit 2aa6b36
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ 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);
if(obs->CheckNan(state))
{
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
14 changes: 13 additions & 1 deletion tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,16 @@ 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)

@pytest.mark.parametrize('use_adaptive_solver', [True, False])
def test_NaNs_in_solver(base_config, use_adaptive_solver):
config = base_config.copy()
config['use_adaptive_solver'] = use_adaptive_solver
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 2aa6b36

Please sign in to comment.