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

Test for DiscontinuousTestODE #368

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 2 deletions pySDC/implementations/problem_classes/DiscontinuousTestODE.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class DiscontinuousTestODE(ptype):
dtype_u = mesh
dtype_f = mesh

def __init__(self, newton_maxiter=100, newton_tol=1e-8):
def __init__(self, newton_maxiter=100, newton_tol=1e-8, stop_at_nan=True):
"""Initialization routine"""
nvars = 1
super().__init__(init=(nvars, None, np.dtype('float64')))
self._makeAttributeAndRegister('nvars', localVars=locals(), readOnly=True)
self._makeAttributeAndRegister('newton_maxiter', 'newton_tol', localVars=locals())
self._makeAttributeAndRegister('newton_maxiter', 'newton_tol', 'stop_at_nan', localVars=locals())

if self.nvars != 1:
raise ParameterError('nvars has to be equal to 1!')
Expand Down
82 changes: 82 additions & 0 deletions pySDC/tests/test_problems/test_DiscontinuousTestODE.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import pytest


@pytest.mark.base
def test_event():
"""
Test if the event occurs at correct time.
"""
import numpy as np
from pySDC.implementations.problem_classes.DiscontinuousTestODE import DiscontinuousTestODE

problem_params = {
'newton_tol': 1e-13,
}

DODE = DiscontinuousTestODE(**problem_params)

u_event = DODE.u_exact(DODE.t_switch_exact)
h = u_event[0] - 5

assert h < 1e-15, 'Value of state function at exact event time is not zero!'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should test for the absolute value of h here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


t0 = 1.6
dt = 1e-2
u0 = DODE.u_exact(t0)

args = {
'rhs': u0,
'dt': dt,
'u0': u0,
't': t0,
}

sol = DODE.solve_system(**args)
assert np.isclose(
sol[0], DODE.u_exact(t0 + dt)[0], atol=2e-3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you use np.allclose and test all of the values instead of just the first one?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your are right, but in this case, the solution consists of only one value.

), 'Solution after one time step is too far away from exact value!'


@pytest.mark.base
def test_capture_errors_and_warnings(caplog):
r"""
Test that checks if the errors in the problem classes are raised.
"""
import numpy as np
from pySDC.core.Errors import ProblemError
from pySDC.implementations.problem_classes.DiscontinuousTestODE import DiscontinuousTestODE

problem_params = {
'newton_tol': 1e-13,
'stop_at_nan': True,
}

DODE = DiscontinuousTestODE(**problem_params)

t0 = 1.0
dt = 1e-3
u0 = DODE.u_exact(t0)

args = {
'rhs': u0,
'dt': np.nan,
'u0': u0,
't': t0,
}

# test of ProblemError is raised
with pytest.raises(ProblemError):
DODE.solve_system(**args)

# test if warnings are raised when nan values arises
DODE.stop_at_nan = False
DODE.solve_system(**args)
assert 'Newton got nan after 100 iterations...' in caplog.text
assert 'Newton did not converge after 100 iterations, error is nan' in caplog.text

# test if warning is raised when local error is tried to computed
u1 = DODE.u_exact(t0 + dt, u_init=u0, t_init=t0)
assert (
'DiscontinuousTestODE uses an analytic exact solution from t=0. If you try to compute the local error, you will get the global error instead!'
in caplog.text
)