Beispiel #1
0
 def setUp(self):
     """
     This sets up the test case.
     """
     def f(t,y):
         eps = 1.e-6
         my = 1./eps
         yd_0 = y[1]
         yd_1 = my*((1.-y[0]**2)*y[1]-y[0])
         
         return N.array([yd_0,yd_1])
     
     def jac(t,y):
         eps = 1.e-6
         my = 1./eps
         J = N.zeros([2,2])
         
         J[0,0]=0.
         J[0,1]=1.
         J[1,0]=my*(-2.*y[0]*y[1]-1.)
         J[1,1]=my*(1.-y[0]**2)
         
         return J
     
     def jac_sparse(t,y):
         eps = 1.e-6
         my = 1./eps
         J = N.zeros([2,2])
         
         J[0,0]=0.
         J[0,1]=1.
         J[1,0]=my*(-2.*y[0]*y[1]-1.)
         J[1,1]=my*(1.-y[0]**2)
         
         return sp.csc_matrix(J)
     
     #Define an Assimulo problem
     y0 = [2.0,-0.6] #Initial conditions
     
     exp_mod = Explicit_Problem(f,y0)
     exp_mod_t0 = Explicit_Problem(f,y0,1.0)
     exp_mod_sp = Explicit_Problem(f,y0)
     
     exp_mod.jac = jac
     exp_mod_sp.jac = jac_sparse
     self.mod = exp_mod
         
     #Define an explicit solver
     self.sim = Radau5ODE(exp_mod) #Create a Radau5 solve
     self.sim_t0 = Radau5ODE(exp_mod_t0)
     self.sim_sp = Radau5ODE(exp_mod_sp)
     
     #Sets the parameters
     self.sim.atol = 1e-4 #Default 1e-6
     self.sim.rtol = 1e-4 #Default 1e-6
     self.sim.inith = 1.e-4 #Initial step-size
     self.sim.usejac = False
Beispiel #2
0
 def test_nbr_fcn_evals_due_to_jac(self):
     sim = Radau5ODE(self.mod)
     
     sim.usejac = False
     sim.simulate(1)
     
     assert sim.statistics["nfcnjacs"] > 0
     
     sim = Radau5ODE(self.mod)
     sim.simulate(1)
     
     assert sim.statistics["nfcnjacs"] == 0
Beispiel #3
0
 def test_time_event(self):
     f = lambda t,y: [1.0]
     global tnext
     global nevent
     tnext = 0.0
     nevent = 0
     def time_events(t,y,sw):
         global tnext,nevent
         events = [1.0, 2.0, 2.5, 3.0]
         for ev in events:
             if t < ev:
                 tnext = ev
                 break
             else:
                 tnext = None
         nevent += 1
         return tnext
         
     def handle_event(solver, event_info):
         solver.y+= 1.0
         global tnext
         nose.tools.assert_almost_equal(solver.t, tnext)
         assert event_info[0] == []
         assert event_info[1] == True
 
     exp_mod = Explicit_Problem(f,0.0)
     exp_mod.time_events = time_events
     exp_mod.handle_event = handle_event
     
     #CVode
     exp_sim = Radau5ODE(exp_mod)
     exp_sim(5.,100)
     
     assert nevent == 5
Beispiel #4
0
    def setUp(self):
        """
        This sets up the test case.
        """

        f = lambda t,y:[1.0,2.0]

        #Define an Assimulo problem
        y0 = [2.0,-0.6] #Initial conditions
        exp_mod = Explicit_Problem(f,y0)
        self.sim = Radau5ODE(exp_mod)
Beispiel #5
0
    def test_weighted_error(self):
        def handle_result(solver, t, y):
            err = solver.get_weighted_local_errors()
            assert len(err) == len(y)

        self.mod.handle_result = handle_result

        #Define an explicit solver
        sim = Radau5ODE(self.mod)  #Create a Radau5 solve

        sim.get_weighted_local_errors()

        sim.simulate(1)
Beispiel #6
0
    def test_event_localizer(self):
        exp_mod = Extended_Problem()  #Create the problem

        exp_sim = Radau5ODE(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)
Beispiel #7
0
    def test_switches(self):
        """
        This tests that the switches are actually turned when override.
        """
        f = lambda t,x,sw: N.array([1.0])
        state_events = lambda t,x,sw: N.array([x[0]-1.])
        def handle_event(solver, event_info):
            solver.sw = [False] #Override the switches to point to another instance
        
        mod = Explicit_Problem(f,[0.0])
        mod.sw0 = [True]

        mod.state_events = state_events
        mod.handle_event = handle_event
        
        sim = Radau5ODE(mod)
        assert sim.sw[0] == True
        sim.simulate(3)
        assert sim.sw[0] == False
Beispiel #8
0
 def test_init(self):
     
     #Test both y0 in problem and not.
     sim = Radau5ODE(self.mod)
     
     assert sim._leny == 2