def run_example(with_plots=True): #Create an instance of the problem exp_mod = Extended_Problem() #Create the problem exp_sim = RungeKutta34(exp_mod) #Create the solver exp_sim.verbosity = 0 exp_sim.report_continuously = True #Simulate t, y = exp_sim.simulate( 10.0, 1000) #Simulate 10 seconds with 1000 communications points #Basic test nose.tools.assert_almost_equal(y[-1][0], 8.0) nose.tools.assert_almost_equal(y[-1][1], 3.0) nose.tools.assert_almost_equal(y[-1][2], 2.0) #Plot if with_plots: import pylab as P P.plot(t, y) P.title(exp_mod.name) P.ylabel('States') P.xlabel('Time') P.show() return exp_mod, exp_sim
def simulate(self, Tend, nIntervals, gridWidth): problem = Explicit_Problem(self.rhs, self.y0) problem.name = 'RK34' problem.handle_result = self.handle_result problem.handle_event = self.handle_event problem.time_events = self.time_events problem.finalize = self.finalize if hasattr(self, 'state_events'): print 'Warning: state_event function in RK34 is not supported and will be ignored!' simulation = RungeKutta34(problem) # Sets additional parameters simulation.atol = self.atol simulation.rtol = self.rtol simulation.verbosity = self.verbosity if hasattr(simulation, 'continuous_output'): simulation.continuous_output = False # default 0, if one step approach should be used elif hasattr(simulation, 'report_continuously'): simulation.report_continuously = False # default 0, if one step approach should be used simulation.inith = self.inith # Calculate nOutputIntervals: if gridWidth <> None: nOutputIntervals = int((Tend - self.t0) / gridWidth) else: nOutputIntervals = nIntervals # Check for feasible input parameters if nOutputIntervals == 0: print 'Error: gridWidth too high or nIntervals set to 0! Continue with nIntervals=1' nOutputIntervals = 1 # Perform simulation simulation.simulate(Tend, nOutputIntervals) # to get the values: t_new, y_new = simulation.simulate
def run_example(with_plots=True): r""" Demonstration of the use of the use of Runge-Kutta 34 by solving the linear test equation :math:`\dot y = - y` on return: - :dfn:`exp_mod` problem instance - :dfn:`exp_sim` solver instance """ #Defines the rhs def f(t, y): ydot = -y[0] return N.array([ydot]) #Define an Assimulo problem exp_mod = Explicit_Problem(f, 4.0, name='RK34 Example: $\dot y = - y$') exp_sim = RungeKutta34(exp_mod) #Create a RungeKutta34 solver exp_sim.inith = 0.1 #Sets the initial step, default = 0.01 #Simulate t, y = exp_sim.simulate(5) #Simulate 5 seconds #Basic test nose.tools.assert_almost_equal(float(y[-1]), 0.02695199, 5) #Plot if with_plots: import pylab as P P.plot(t, y) P.title(exp_mod.name) P.ylabel('y') P.xlabel('Time') P.show() return exp_mod, exp_sim
def _default_solver_instance(self): from assimulo.solvers import RungeKutta34 return RungeKutta34(self._assimulo_problem)