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 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
22 changes: 10 additions & 12 deletions pySDC/implementations/problem_classes/DiscontinuousTestODE.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

from pySDC.core.Errors import ParameterError, ProblemError
from pySDC.core.Problem import ptype
from pySDC.core.Problem import ptype, WorkCounter
from pySDC.implementations.datatype_classes.mesh import mesh


Expand Down Expand Up @@ -31,30 +31,30 @@ class DiscontinuousTestODE(ptype):
Time point of the discrete event found by switch estimation.
nswitches: int
Number of switches found by switch estimation.
newton_itercount: int
Counts the number of Newton iterations.
newton_ncalls: int
Counts the number of how often Newton is called in the simulation of the problem.

Attributes
----------
work_counters : WorkCounter
Counts different things, here: Number of Newton iterations is counted.
"""

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!')

self.t_switch_exact = np.log(5)
self.t_switch = None
self.nswitches = 0
self.newton_itercount = 0
self.newton_ncalls = 0
self.work_counters['newton'] = WorkCounter()

def eval_f(self, u, t):
"""
Expand Down Expand Up @@ -133,6 +133,7 @@ def solve_system(self, rhs, dt, u0, t):
u -= 1.0 / dg * g

n += 1
self.work_counters['newton']()

if np.isnan(res) and self.stop_at_nan:
raise ProblemError('Newton got nan after %i iterations, aborting...' % n)
Expand All @@ -142,9 +143,6 @@ def solve_system(self, rhs, dt, u0, t):
if n == self.newton_maxiter:
self.logger.warning('Newton did not converge after %i iterations, error is %s' % (n, res))

self.newton_ncalls += 1
self.newton_itercount += n

me = self.dtype_u(self.init)
me[:] = u[:]

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 abs(h) < 1e-15, 'Value of state function at exact event time is not zero!'

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
)