-
Notifications
You must be signed in to change notification settings - Fork 56
/
test_odesolver.py
50 lines (36 loc) · 1.34 KB
/
test_odesolver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import unittest
import numpy as np
from probnum.diffeq import ConstantSteps, ODESolver, logistic
from probnum.random_variables import Constant
class MockODESolver(ODESolver):
"""Euler method as an ODE solver."""
def initialise(self):
return self.ivp.t0, self.ivp.initrv
def step(self, start, stop, current):
h = stop - start
x = current.mean
xnew = x + h * self.ivp(start, x)
return (
Constant(xnew),
np.nan,
) # return nan as error estimate to ensure that it is not used
class ODESolverTestCase(unittest.TestCase):
"""An ODE Solver has to work with just step() and initialise() provided.
We implement Euler in MockODESolver to assure this.
"""
def setUp(self):
y0 = Constant(0.3)
ivp = logistic([0, 4], initrv=y0)
euler_order = 1
self.solver = MockODESolver(ivp, order=euler_order)
self.step = 0.2
def test_solve(self):
steprule = ConstantSteps(self.step)
odesol = self.solver.solve(
steprule=steprule,
) # this is the actual part of the test
# quick check that the result is sensible
self.assertAlmostEqual(odesol.t[-1], self.solver.ivp.tmax)
self.assertAlmostEqual(odesol.y[-1].mean, 1.0, places=2)
if __name__ == "__main__":
unittest.main()