def get_solver(self, func): """ Returns the solver method from odespy package. Args: func: function function with ODE system. Returns: an instance of odeSolver """ if self.solverMethod is solverMethod.LSODA: solver = odespy.Lsoda(func) elif self.solverMethod is solverMethod.LSODAR: solver = odespy.Lsodar(func) elif self.solverMethod is solverMethod.LSODE: solver = odespy.Lsode(func) elif self.solverMethod is solverMethod.HEUN: solver = odespy.Heun(func) elif self.solverMethod is solverMethod.EULER: solver = odespy.Euler(func) elif self.solverMethod is solverMethod.RK4: solver = odespy.RK4(func) elif self.solverMethod is solverMethod.DORMAN_PRINCE: solver = odespy.DormandPrince(func) elif self.solverMethod is solverMethod.RKFehlberg: solver = odespy.RKFehlberg(func) elif self.solverMethod is solverMethod.Dopri5: solver = odespy.Dopri5(func) elif self.solverMethod is solverMethod.Dop853: solver = odespy.Dop853(func) elif self.solverMethod is solverMethod.Vode: solver = odespy.Vode(func) elif self.solverMethod is solverMethod.AdamsBashforth2: solver = odespy.AdamsBashforth2(func, method='bdf') elif self.solverMethod is solverMethod.Radau5: solver = odespy.Radau5(func) elif self.solverMethod is solverMethod.AdamsBashMoulton2: solver = odespy.AdamsBashMoulton2(func) # update default parameters solver.nsteps = SolverConfigurations.N_STEPS solver.atol = SolverConfigurations.ABSOLUTE_TOL solver.rtol = SolverConfigurations.RELATIVE_TOL return solver
adams_or_bdf = 'bdf' import odespy solvers = [ odespy.AdamsBashMoulton2(problem), odespy.AdamsBashMoulton3(problem), odespy.AdamsBashforth2(problem), odespy.AdamsBashforth3(problem), odespy.AdamsBashforth4(problem), odespy.AdaptiveResidual(problem, solver='Euler'), odespy.Backward2Step(problem), odespy.BackwardEuler(problem), odespy.Dop853(problem, rtol=rtol, atol=atol), odespy.Dopri5(problem, rtol=rtol, atol=atol), odespy.Euler(problem), odespy.Heun(problem), odespy.Leapfrog(problem), odespy.LeapfrogFiltered(problem), odespy.MidpointImplicit(problem), odespy.MidpointIter(problem, max_iter=10, eps_iter=1E-7), odespy.RK2(problem), odespy.RK3(problem), odespy.RK4(problem), odespy.RKFehlberg(problem, rtol=rtol, atol=atol), odespy.SymPy_odefun(problem), odespy.ThetaRule(problem), odespy.Trapezoidal(problem), odespy.Vode(problem, rtol=rtol, atol=atol, adams_or_bdf=adams_or_bdf), ] theta_exact = lambda t: problem.Theta * numpy.cos(sqrt(problem.c) * t)
"""Oscillating pendulum.""" import odespy, numpy from math import sin, pi, sqrt c = 1 Theta = pi / 4 def f(u, t): theta, omega = u return [omega, -c * sin(theta)] solver = odespy.Heun(f) solver.set_initial_condition([Theta, 0]) freq = sqrt(c) # frequency of oscillations when Theta is small period = 2 * pi / freq # the period of the oscillations T = 10 * period # final time N_per_period = 20 # resolution of one period N = N_per_period * period time_points = numpy.linspace(0, T, N + 1) u, t = solver.solve(time_points) theta = u[:, 0] omega = u[:, 1] from matplotlib.pyplot import * plot(t, theta, 'r-') savefig('tmppng')
numpy.random.seed(12) t = self.solver.t dW = numpy.random.normal(loc=0, scale=1, size=len(t)-1) dt = t[1:] - t[:-1] self.N = self.sigma*dW/numpy.sqrt(dt) x, v = u N = self.N[self.solver.n] return [v, N -self.b*v -self.c*x] from numpy import pi, linspace from matplotlib.pyplot import * import odespy problem = WhiteNoiseOscillator(b=0.1, c=pi**2, sigma=1) solvers = [odespy.Heun(problem.f), odespy.RK4(problem.f), odespy.ForwardEuler(problem.f)] for solver in solvers: f.connect_solver(solver) solver.set_initial_condition([0,0]) # start from rest T = 60 # with c=pi**2, the period is 1 u, t = solver.solve(linspace(0, T, 10001)) x = u[:,0] plot(t, x) hold(True) legend([str(s) for s in solvers]) savefig('tmppng'); savefig('tmp.pdf') show()