def _run_test_problems(problem): method_no = 0 # Number of successful methods methods = [ method for method in odespy.list_available_solvers() if method not in problem['exceptions'] ] special_names = ('f', 'time_points', 'u0', 'exact', 'exact_final', 'help', 'terminate', 'exceptions', 'stop_value', 'f_with_args', 'f_args', 'f_with_kwargs', 'f_kwargs') kwargs = {key: problem[key] for key in problem if key not in special_names} print problem.get('help', '') for method in methods: solver = eval('odespy.%s' % method)(problem['f'], **kwargs) print 'Testing %s' % method solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) if 'f_with_args' in problem and 'f_args' in problem: print 'Testing %s with f_args' % method solver = eval('odespy.%s' % method)(problem['f_with_args'], f_args=problem['f_args'], **kwargs) solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) if 'f_with_kwargs' in problem and 'f_kwargs' in problem: print 'Testing %s with f_kwargs' % method solver = eval('odespy.%s' % method)(problem['f_with_kwargs'], f_kwargs=problem['f_kwargs'], **kwargs) solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) # Compare if exact values are specified if 'exact' in problem: exact = problem['exact'](np.asarray(t)) u = np.asarray(u) if len(u.shape) == 1: # make 2d-array for scalar ODE u = u.reshape(len(u), 1) diff = np.abs(u[:, 0] - exact).max() nt.assert_almost_equal(diff, 0, delta=0.2) elif 'exact_final' in problem: u_final, exact_final = u[-1], problem['exact_final'] u_final = np.asarray(u_final) exact_final = np.asarray(problem['exact_final']) diff = np.abs(u_final - exact_final).max() nt.assert_almost_equal(diff, 0, delta=0.2)
def _run_test_problems(problem): method_no = 0 # Number of successful methods methods = [method for method in odespy.list_available_solvers() if method not in problem['exceptions']] special_names = ('f', 'time_points', 'u0', 'exact', 'exact_final', 'help', 'terminate', 'exceptions', 'stop_value', 'f_with_args', 'f_args', 'f_with_kwargs', 'f_kwargs') kwargs = {key: problem[key] for key in problem if key not in special_names} print problem.get('help', '') for method in methods: solver = eval('odespy.%s' % method)(problem['f'], **kwargs) print 'Testing %s' % method solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) if 'f_with_args' in problem and 'f_args' in problem: print 'Testing %s with f_args' % method solver = eval('odespy.%s' % method)( problem['f_with_args'], f_args=problem['f_args'], **kwargs) solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) if 'f_with_kwargs' in problem and 'f_kwargs' in problem: print 'Testing %s with f_kwargs' % method solver = eval('odespy.%s' % method)( problem['f_with_kwargs'], f_kwargs=problem['f_kwargs'],**kwargs) solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) # Compare if exact values are specified if 'exact' in problem: exact = problem['exact'](np.asarray(t)) u = np.asarray(u) if len(u.shape) == 1: # make 2d-array for scalar ODE u = u.reshape(len(u), 1) diff = np.abs(u[:,0] - exact).max() nt.assert_almost_equal(diff, 0, delta=0.2) elif 'exact_final' in problem: u_final, exact_final = u[-1], problem['exact_final'] u_final = np.asarray(u_final) exact_final = np.asarray(problem['exact_final']) diff = np.abs(u_final - exact_final).max() nt.assert_almost_equal(diff, 0, delta=0.2)
def _run_test_problems(self, problem): self._load_problem(problem) method_no = 0 # Number of successful methods methods = [ method for method in odespy.list_available_solvers() if method not in self.exceptions ] print self.help for method in methods: print 'Testing %s' % method # Start up integration solver = eval('odespy.%s' % method)(self.f, **self.kwargs) solver.set_initial_condition(self.u0) u, t = solver.solve(self.time_points) # Compare if exact values are specified if hasattr(self, 'exact'): exact = self.exact(np.asarray(t)) u = np.asarray(u) if len(u.shape) == 1: # make 2d-array for scalar ODE u = u.reshape(len(u), 1) assert_array_almost_equal(\ u[:,0], exact, err_msg='Failed with method %s' % method, decimal=1, verbose=False) elif hasattr(self, 'exact_final'): u_final, exact_final = u[-1], self.exact_final if not np.iterable(u_final): u_final = np.asarray(u_final) exact_final = np.asarray(self.exact_final) try: assert_array_almost_equal(\ u_final, exact_final, err_msg='Failed with result of method %s' % method, decimal=1, verbose=True) except Exception, e: print e print 'Running method', method, 'for', problem raise e
def _run_test_problems(self, problem): self._load_problem(problem) method_no = 0 # Number of successful methods methods = [method for method in odespy.list_available_solvers() if method not in self.exceptions] print self.help for method in methods: print 'Testing %s' % method # Start up integration solver = eval('odespy.%s' % method)(self.f, **self.kwargs) solver.set_initial_condition(self.u0) u, t = solver.solve(self.time_points) # Compare if exact values are specified if hasattr(self, 'exact'): exact = self.exact(np.asarray(t)) u = np.asarray(u) if len(u.shape) == 1: # make 2d-array for scalar ODE u = u.reshape(len(u), 1) assert_array_almost_equal(\ u[:,0], exact, err_msg='Failed with method %s' % method, decimal=1, verbose=False) elif hasattr(self, 'exact_final'): u_final, exact_final = u[-1], self.exact_final if not np.iterable(u_final): u_final = np.asarray(u_final) exact_final = np.asarray(self.exact_final) try: assert_array_almost_equal(\ u_final, exact_final, err_msg='Failed with result of method %s' % method, decimal=1, verbose=True) except Exception, e: print e print 'Running method', method, 'for', problem raise e
def _run_test_problems(problem): """Main function for executing a unit test.""" # Test all available solvers for a problem, except those listed # as exceptions in the problem dict methods = [method for method in odespy.list_available_solvers() if method not in problem['exceptions']] # When constructing a method, supply all keys in problem except # the ones below as **kwargs arguments to the constructor special_names = ('f', 'time_points', 'u0', 'exact', 'exact_final', 'help', 'terminate', 'exceptions', 'stop_value', 'f_with_args', 'f_args', 'f_with_kwargs', 'f_kwargs') kwargs = {key: problem[key] for key in problem if key not in special_names} print(problem.get('help', '')) for method in methods: solver = eval('odespy.%s' % method)(problem['f'], **kwargs) print('Testing %s' % method, end=' ') solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) # Test additional versions of the right-hand side function if 'f_with_args' in problem and 'f_args' in problem: #print 'Testing %s with f_args' % method solver = eval('odespy.%s' % method)( problem['f_with_args'], f_args=problem['f_args'], **kwargs) solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) if 'f_with_kwargs' in problem and 'f_kwargs' in problem: #print 'Testing %s with f_kwargs' % method solver = eval('odespy.%s' % method)( problem['f_with_kwargs'], f_kwargs=problem['f_kwargs'],**kwargs) solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) # Compare with exact values if 'exact_diff' in problem: t = np.asarray(t) u = np.asarray(u) exact = problem['exact'](t) if len(u.shape) == 1: # make 2d-array for scalar ODE u = u.reshape(len(u), 1) diff = np.abs(u[:,0] - exact).max() if method in problem['exact_diff']: nt.assert_almost_equal(diff, problem['exact_diff'][method], delta=1E-14) print('...ok') else: pass print(' no exact diff available for comparison') if 'exact_final_diff' in problem: u_final = np.asarray(u[-1]) exact_final = np.asarray(problem['exact_final']) diff = np.abs(u_final - exact_final).max() if method in problem['exact_final_diff']: nt.assert_almost_equal(diff, problem['exact_final_diff'][method], delta=1E-14) print('...ok') else: print(' no exact final diff available for comparison')
from itertools import product # Check whether odespy is installed try: _ODESPY = True import odespy except ImportError: _ODESPY = False import numpy as np # Define availabe integrators for each of all ode' approaches ODE_INTEGRATORS = {} ODE_INTEGRATORS['ode'] = ['vode', 'zvode', 'lsoda', 'dopri5', 'dop853'] if _ODESPY: ODE_INTEGRATORS['odespy'] = odespy.list_available_solvers() # Standard setting for each ode approach STD_ODE_INTEGRATOR = {'ode': 'lsoda', 'odespy': 'lsoda_scipy', 'odeint': ''} class _Solver(object): r""" Base solver class for OdeSolver, AlgebraicSolver and HybridSolver """ def __init__(self, fun, independent): """ """ self.fun = fun
def _run_test_problems(problem): """Main function for executing a unit test.""" # Test all available solvers for a problem, except those listed # as exceptions in the problem dict methods = [ method for method in odespy.list_available_solvers() if method not in problem['exceptions'] ] # When constructing a method, supply all keys in problem except # the ones below as **kwargs arguments to the constructor special_names = ('f', 'time_points', 'u0', 'exact', 'exact_final', 'help', 'terminate', 'exceptions', 'stop_value', 'f_with_args', 'f_args', 'f_with_kwargs', 'f_kwargs') kwargs = {key: problem[key] for key in problem if key not in special_names} print problem.get('help', '') for method in methods: solver = eval('odespy.%s' % method)(problem['f'], **kwargs) print 'Testing %s' % method, solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) # Test additional versions of the right-hand side function if 'f_with_args' in problem and 'f_args' in problem: #print 'Testing %s with f_args' % method solver = eval('odespy.%s' % method)(problem['f_with_args'], f_args=problem['f_args'], **kwargs) solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) if 'f_with_kwargs' in problem and 'f_kwargs' in problem: #print 'Testing %s with f_kwargs' % method solver = eval('odespy.%s' % method)(problem['f_with_kwargs'], f_kwargs=problem['f_kwargs'], **kwargs) solver.set_initial_condition(problem['u0']) u, t = solver.solve(problem['time_points']) # Compare with exact values if 'exact_diff' in problem: t = np.asarray(t) u = np.asarray(u) exact = problem['exact'](t) if len(u.shape) == 1: # make 2d-array for scalar ODE u = u.reshape(len(u), 1) diff = np.abs(u[:, 0] - exact).max() if method in problem['exact_diff']: nt.assert_almost_equal(diff, problem['exact_diff'][method], delta=1E-14) print '...ok' else: pass print ' no exact diff available for comparison' if 'exact_final_diff' in problem: u_final = np.asarray(u[-1]) exact_final = np.asarray(problem['exact_final']) diff = np.abs(u_final - exact_final).max() if method in problem['exact_final_diff']: nt.assert_almost_equal(diff, problem['exact_final_diff'][method], delta=1E-14) print '...ok' else: print ' no exact final diff available for comparison'