-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from pnkraemer/refactor_odesolver
Refactor odesolver
- Loading branch information
Showing
5 changed files
with
201 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
from probnum.diffeq import logistic, ODESolver, ConstantSteps | ||
from probnum.prob import RandomVariable, Dirac | ||
|
||
|
||
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 ( | ||
RandomVariable(Dirac(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 = RandomVariable(distribution=Dirac(0.3)) | ||
ivp = logistic([0, 4], initrv=y0) | ||
self.solver = MockODESolver(ivp) | ||
self.step = 0.2 | ||
|
||
def test_solve(self): | ||
steprule = ConstantSteps(self.step) | ||
odesol = self.solver.solve( | ||
firststep=self.step, 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() |