Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch NaNs in static solver #94

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Loading