From e3d0fea7f8ea2c47d8f18eaadef4b377d24c0b62 Mon Sep 17 00:00:00 2001 From: Renato Coutinho Date: Fri, 26 Apr 2019 16:12:08 -0300 Subject: [PATCH 1/2] Updating sympy solver - it's actually mpmath. mpmath was separated from sympy a long time ago, it's no longer inside it. The solver actually uses only mpmath, not sympy. --- odespy/__init__.py | 2 +- odespy/problems.py | 4 ++-- odespy/solvers.py | 28 ++++++++++++++-------------- odespy/tests/test_basics.py | 4 ++-- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/odespy/__init__.py b/odespy/__init__.py index 9212728..ef04bf8 100644 --- a/odespy/__init__.py +++ b/odespy/__init__.py @@ -730,7 +730,7 @@ def f(self, u, t): ... Vode lsoda_scipy -odefun_sympy +odefun_mpmath odelab A similar function, ``list_available_solvers``, returns a list of the diff --git a/odespy/problems.py b/odespy/problems.py index 023e218..00e90f4 100644 --- a/odespy/problems.py +++ b/odespy/problems.py @@ -122,7 +122,7 @@ def __init__(self, a=1, b=0, c=2): self.not_suitable_solvers = [ 'AdaptiveResidual', 'Lsodi', 'Lsodis', 'Lsoibt'] if self.a < 0: - self.not_suitable_solvers += ['lsoda_scipy', 'odefun_sympy'] + self.not_suitable_solvers += ['lsoda_scipy', 'odefun_mpmath'] def f(self, u, t): return self.a + (u - self.a*t - self.b)**self.c @@ -194,7 +194,7 @@ def __init__(self, a=1, b=0, A=1): raise ValueError('a=%g is too small' % a) self.not_suitable_solvers = [ 'Lsodi', 'Lsodis', 'Lsoibt', 'MyRungeKutta', - 'MySolver', 'Lsodes', 'SymPy_odefun'] + 'MySolver', 'Lsodes', 'mpmath_odefun'] def f(self, u, t): return self.a*u + self.b diff --git a/odespy/solvers.py b/odespy/solvers.py index af595d4..d5f2fd8 100644 --- a/odespy/solvers.py +++ b/odespy/solvers.py @@ -45,7 +45,7 @@ class ``BackwardEuler``, for instance. developers need to install and import (in ``initialize``) the desired package as a Python module and just write a class in the ``Solver`` hierarchy that calls the proper functions in the package. See the -classes ``odefun_sympy`` and ``ode_scipy`` (and its subclasses). +classes ``odefun_mpmath`` and ``ode_scipy`` (and its subclasses). By an attempt to import these necessary modules (often set in method initialize()), we can check whether the necessary dependencies are @@ -106,7 +106,7 @@ class ``BackwardEuler``, for instance. software's ability to solve the whole ODE problem. Then it is more natural to write a new ``solve`` method (using ``Solver.solve`` as model) and call up the solve functionality in the external -software. Class ``odefun_sympy`` provides an example. On the other +software. Class ``odefun_mpmath`` provides an example. On the other hand, the wrappers for the ``scipy`` solvers (``vode`` for instance) applies ``solve`` from the present package and the ``scipy`` functions for doing one (adaptive) time step, called in the ``advance`` method. @@ -993,7 +993,7 @@ def set_initial_condition(self, U0): # Test first if U0 is sequence (len(U0) possible), # and use that as indicator for system of ODEs. # The below code should work for U0 having - # float,int,sympy.mpmath.mpi and other objects as elements. + # float,int,mpmath.mpi and other objects as elements. try: self.neq = len(U0) U0 = np.asarray(U0) # (assume U0 is sequence) @@ -2001,22 +2001,22 @@ def approx_Jacobian(f, u0, t0, h): return J.transpose() -class odefun_sympy(Solver): +class odefun_mpmath(Solver): """ - Wrapper for the sympy.mpmath.odefun method, which applies a high-order + Wrapper for the mpmath.odefun method, which applies a high-order Taylor series method to solve ODEs. """ - quick_description = "Very accurate high order Taylor method (from SymPy)" + quick_description = "Very accurate high order Taylor method (from mpmath)" def initialize(self): try: - import sympy - self.sympy = sympy + import mpmath + self.mpmath = mpmath except ImportError: - raise ImportError,'sympy is not installed - needed for sympy_odefun' + raise ImportError('mpmath is not installed - needed for mpmath_odefun') def initialize_for_solve(self): - # sympy.odefun requires f(t, u), not f(u, t, *args, **kwargs) + # mpmath.odefun requires f(t, u), not f(u, t, *args, **kwargs) # (self.f already handles f_args, f_kwargs) self.f4odefun = lambda t, u: self.f(u, t) Solver.initialize_for_solve(self) @@ -2024,7 +2024,7 @@ def initialize_for_solve(self): def solve(self, time_points, terminate=None): """ The complete solve method must be overridded in this class - since sympy.mpmath.odefun is such a solve method. + since mpmath.odefun is such a solve method. The class stores an attribute ufunc (return from odefun) which can be used to evaluate u at any time point (ufunc(t)). @@ -2034,8 +2034,8 @@ def solve(self, time_points, terminate=None): self.t = np.asarray(time_points) self.initialize_for_solve() - self.sympy.mpmath.mp.dps = 15 # accuracy - self.ufunc = self.sympy.mpmath.odefun( + self.mpmath.mp.dps = 15 # accuracy + self.ufunc = self.mpmath.odefun( self.f4odefun, time_points[0], self.U0) # u and t to be returned are now computed by sampling self.ufunc @@ -3062,7 +3062,7 @@ def list_not_suitable_complex_solvers(): 'Lsoda', 'Lsodar', 'Lsode', 'Lsodes', 'Lsodi', 'Lsodis', 'Lsoibt', 'RKC', 'RKF45', - 'odefun_sympy', 'lsoda_scipy', + 'odefun_mpmath', 'lsoda_scipy', 'Radau5', 'Radau5Explicit', 'Radau5Implicit', 'EulerCromer', ] diff --git a/odespy/tests/test_basics.py b/odespy/tests/test_basics.py index 7b16fd4..a8f3de3 100644 --- a/odespy/tests/test_basics.py +++ b/odespy/tests/test_basics.py @@ -31,7 +31,7 @@ f_kwargs=dict(a=-1., b=0.,), # These solvers are not suitable for this ODE problem exceptions=['Lsodi', 'Lsodis', 'Lsoibt', 'MyRungeKutta', - 'MySolver', 'Lsodes', 'SymPy_odefun', 'AdaptiveResidual', + 'MySolver', 'Lsodes', 'mpmath_odefun', 'AdaptiveResidual', 'Radau5', 'Radau5Explicit', 'Radau5Implicit', 'EulerCromer'], time_points=np.linspace(0., 1., 10), terminate=lambda u,t,step_number: u[step_number] <= 0.4, @@ -191,7 +191,7 @@ Trapezoidal=0.0375041929371673, Vode=0.0375037017349515, lsoda_scipy=0.0375036977160605, -odefun_sympy=0.0375036952176233, +odefun_mpmath=0.0375036952176233, ), ) From 2bae981bcbd4caa56dd7742f816c543704e3ac16 Mon Sep 17 00:00:00 2001 From: Renato Coutinho Date: Fri, 26 Apr 2019 16:18:48 -0300 Subject: [PATCH 2/2] completing previous commit: tests of mpmath method --- odespy/tests/test_basics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/odespy/tests/test_basics.py b/odespy/tests/test_basics.py index a8f3de3..63a1b21 100644 --- a/odespy/tests/test_basics.py +++ b/odespy/tests/test_basics.py @@ -78,7 +78,7 @@ Trapezoidal=0.0008237438335759, Vode=0.0000226336696082, lsoda_scipy=0.0000865549875511, -odefun_sympy=0.0000000000000000, +odefun_mpmath=0.0000000000000000, ), ) @@ -136,7 +136,7 @@ Trapezoidal=0.0012657016106976, Vode=0.0000019483075576, lsoda_scipy=0.0000003263486278, -odefun_sympy=0.0000000000000000, +odefun_mpmath=0.0000000000000000, ), )