コード例 #1
0
ファイル: test_sundials.py プロジェクト: apetcho/odespy
 def test_time_event(self):
     f = lambda t,y,yd: y-yd
     global tnext
     global nevent
     tnext = 0.0
     nevent = 0
     def time_events(t,y,yd,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 = Implicit_Problem(f,0.0,0.0)
     exp_mod.time_events = time_events
     exp_mod.handle_event = handle_event
     
     #CVode
     exp_sim = IDA(exp_mod)
     exp_sim(5.,100)
     
     assert nevent == 5
コード例 #2
0
    def test_time_event(self):
        f = lambda t, y, yd: y - yd
        global tnext
        global nevent
        tnext = 0.0
        nevent = 0

        def time_events(t, y, yd, 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 = Implicit_Problem(f, 0.0, 0.0)
        exp_mod.time_events = time_events
        exp_mod.handle_event = handle_event

        #CVode
        exp_sim = IDA(exp_mod)
        exp_sim(5., 100)

        assert nevent == 5
コード例 #3
0
    def simulate(self, Tend, nIntervals, gridWidth):

        problem = Implicit_Problem(self.rhs, self.y0, self.yd0)
        problem.name = 'IDA'
        # solver.rhs = self.right_hand_side
        problem.handle_result = self.handle_result
        problem.state_events = self.state_events
        problem.handle_event = self.handle_event
        problem.time_events = self.time_events
        problem.finalize = self.finalize
        # Create IDA object and set additional parameters
        simulation = IDA(problem)
        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.tout1 = self.tout1
        simulation.lsoff = self.lsoff

        # 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,  yd_new = simulation.simulate
コード例 #4
0
    def simulate(self, Tend, nIntervals, gridWidth):

        problem = Implicit_Problem(self.rhs, self.y0, self.yd0)
        problem.name = 'IDA'
        # solver.rhs = self.right_hand_side
        problem.handle_result = self.handle_result
        problem.state_events = self.state_events
        problem.handle_event = self.handle_event
        problem.time_events = self.time_events
        problem.finalize = self.finalize
        # Create IDA object and set additional parameters
        simulation = IDA(problem)
        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.tout1 = self.tout1
        simulation.lsoff = self.lsoff

        # 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,  yd_new = simulation.simulate
コード例 #5
0
def run_example():

	#initial values
	t0 = 0
	y0 = np.array([0., -0.10344, -0.65, 0., 0. , 0., -0.628993, 0.047088]) #|phi_s| <= 0.1034 rad, |phi_b| <= 0.12 rad
	yd0 = np.array([0., 0., 0., 0., 0., 0., 0., 0.])
	sw = [False, True, False]
	
	#problem
	model = Implicit_Problem(pecker, y0, yd0, t0, sw0=sw)
	model.state_events = state_events #from woodpecker.py
	model.handle_event = handle_event #from woodpecker.py
	model.name = 'Woodpeckermodel'
	sim = IDA(model) #create IDA solver
	tfinal = 2.0 #final time
	ncp = 500 #number control points	
	sim.suppress_alg = True
	sim.rtol=1.e-6
	sim.atol[6:8] = 1e6
	sim.algvar[6:8] = 0
	t, y, yd = sim.simulate(tfinal, ncp) #simulate
	
	#plot
	fig, ax = P.subplots()
	ax.plot(t, y[:, 0], label='z')
	legend = ax.legend(loc='upper center', shadow=True)
	P.grid()
	
	P.figure(1)
	fig2, ax2 = P.subplots()
	ax2.plot(t, y[:, 1], label='phi_s')
	ax2.plot(t, y[:, 2], label='phi_b')
	legend = ax2.legend(loc='upper center', shadow=True)
	P.grid()
	
	P.figure(2)
	fig3, ax3 = P.subplots()
	ax3.plot(t, y[:, 4], label='phi_sp')
	ax3.plot(t, y[:, 5], label='phi_bp')
	legend = ax3.legend(loc='upper center', shadow=True)
	P.grid()
	
	P.figure(3)
	fig4, ax4 = P.subplots()
	ax4.plot(t, y[:, 6], label='lambda_1')
	ax4.plot(t, y[:, 7], label='lambda_2')
	legend = ax4.legend(loc='upper right', shadow=True)
	P.grid()
	
	#event data
	sim.print_event_data()
	
	#show plots
	P.show()
	print("...")
	P.show()
コード例 #6
0
ファイル: test_sundials.py プロジェクト: apetcho/odespy
 def test_switches(self):
     """
     This tests that the switches are actually turned when override.
     """
     f = lambda t,x,xd,sw: N.array([xd[0]- 1.0])
     state_events = lambda t,x,xd,sw: N.array([x[0]-1.])
     def handle_event(solver, event_info):
         solver.sw = [False] #Override the switches to point to another instance
     
     mod = Implicit_Problem(f, [0.0],[1.0])
     mod.f = f
     mod.sw0 = [True]
     mod.state_events = state_events
     mod.handle_event = handle_event
     
     sim = IDA(mod)
     assert sim.sw[0] == True
     sim.simulate(3)
     assert sim.sw[0] == False
コード例 #7
0
    def test_switches(self):
        """
        This tests that the switches are actually turned when override.
        """
        res = lambda t,x,xd,sw: N.array([1.0 - xd])
        state_events = lambda t,x,xd,sw: N.array([x[0]-1.])
        def handle_event(solver, event_info):
            solver.sw = [False] #Override the switches to point to another instance
        
        mod = Implicit_Problem(res,[0.0], [1.0])
        mod.sw0 = [True]

        mod.state_events = state_events
        mod.handle_event = handle_event
        
        sim = Radau5DAE(mod)
        assert sim.sw[0] == True
        sim.simulate(3)
        assert sim.sw[0] == False
コード例 #8
0
    def test_time_event(self):
        """
        This tests the functionality of the time event function.
        """
        f = lambda t, x, xd, sw: xd - x

        def time_events(t, y, yd, sw):
            if sw[0]:
                return 1.0
            if sw[1]:
                return 3.0
            return None

        def handle_event(solver, event_info):

            if event_info[1]:
                solver.y = N.array([1.0])
                solver.yd = N.array([1.0])

                if not solver.sw[0]:
                    solver.sw[1] = False

                if solver.sw[0]:
                    solver.sw[0] = False

        mod = Implicit_Problem(f, [1.0], [1.0])
        mod.time_events = time_events
        mod.handle_event = handle_event
        mod.switches0 = [True, True]

        sim = IDA(mod)

        sim.simulate(5.0)

        nose.tools.assert_almost_equal(sim.y_sol[38], 1.0000000, 5)
        nose.tools.assert_almost_equal(sim.y_sol[87], 1.0000000, 5)

        sim = IDA(mod, [1.0], [1.0])
        sim.simulate(2.0)

        nose.tools.assert_almost_equal(sim.t_sol[-1], 2.0000000, 5)
コード例 #9
0
ファイル: test_sundials.py プロジェクト: apetcho/odespy
    def test_time_event(self):
        """
        This tests the functionality of the time event function.
        """
        f = lambda t,x,xd,sw: xd-x
        
        def time_events(t, y, yd, sw):
            if sw[0]:
                return 1.0
            if sw[1]:
                return 3.0
            return None
        
        def handle_event(solver, event_info):
            
            if event_info[1]:
                solver.y  = N.array([1.0])
                solver.yd = N.array([1.0])
                
                if not solver.sw[0]:
                    solver.sw[1] = False
                
                if solver.sw[0]:
                    solver.sw[0] = False
        
        mod = Implicit_Problem(f,[1.0],[1.0])
        mod.time_events = time_events
        mod.handle_event = handle_event
        mod.switches0 = [True, True]
        
        sim = IDA(mod)
        
        sim.simulate(5.0)

        nose.tools.assert_almost_equal(sim.y_sol[38], 1.0000000, 5)
        nose.tools.assert_almost_equal(sim.y_sol[87], 1.0000000, 5)
        
        sim = IDA(mod, [1.0],[1.0])
        sim.simulate(2.0)
        
        nose.tools.assert_almost_equal(sim.t_sol[-1], 2.0000000, 5)
コード例 #10
0
    def test_clear_event_log(self):
        """
        This tests the functionality of the time event function.
        """
        f = lambda t, x, xd, sw: xd - x

        def time_events(t, y, yd, sw):
            if sw[0]:
                return 1.0
            if sw[1]:
                return 3.0
            return None

        def handle_event(solver, event_info):

            if event_info[1]:
                solver.y = N.array([1.0])
                solver.yd = N.array([1.0])

                if not solver.sw[0]:
                    solver.sw[1] = False

                if solver.sw[0]:
                    solver.sw[0] = False

        mod = Implicit_Problem(f, [1.0], [1.0], sw0=[True, True])
        mod.time_events = time_events
        mod.handle_event = handle_event

        sim = IDA(mod)
        sim.verbosity = 10
        assert len(sim.event_data) == 0
        sim.simulate(5.0)
        assert len(sim.event_data) > 0

        sim.reset()
        assert len(sim.event_data) == 0
        sim.simulate(5.0)
        assert len(sim.event_data) > 0
コード例 #11
0
ファイル: sqz_cp.py プロジェクト: zicvic/FMNN05
    # State IV => State III
    if solver.sw[3]:
        solver.sw[3] = not solver.sw[3]
        solver.sw[2] = not solver.sw[2]          
        
        
        
        
y0,yp0 = init_woodpecker()
t0 = 0.0
sw0 = np.array([1,0,0,0])

model = Implicit_Problem(woodpecker,y0,yp0,t0,sw0)

model.state_events = state_events
model.handle_event = handle_event

solver = IDA(model)
solver.simulate(3)





        

        
	
	
	 
	
コード例 #12
0
from woodpecker_model import init_woodpecker
from woodpecker_model import res
from state_event_woodpecker import state_event
from handle_event_woodpecker import handle_event
from assimulo.problem import Implicit_Problem
from assimulo.solvers import IDA
import matplotlib.pyplot as P
import numpy as N

t0 = 0.0
tfinal = 2.0
y0, yd0, switches0 = init_woodpecker()

model = Implicit_Problem(res, y0, yd0, t0, sw0=switches0)
model.state_events = state_event
model.handle_event = handle_event

sim = IDA(model)
sim.algvar = [1, 1, 1, 0, 0, 0, 0, 0]
sim.suppress_alg = True
sim.atol = [1e-5, 1e-5, 1e-5, 1e15, 1e15, 1e15, 1e15, 1e15]
t, y, yd = sim.simulate(tfinal)

#sim.print_event_data()
"""
    Plotting

    y[:,0] - plots z (height)
    y[:,1] - plots sleeve angle
    y[:,2] - plots bird angle
"""
コード例 #13
0
	elif state_info[3] != 0:
		if solver.sw[2] == 1 and yp[2] > 0:
			yp[2] = -yp[2]
			print("DING")
	solver.yd = yp
	solver.y = y	
 
t0 = 0
tfinal = 1
ncp = 500
y0 = [0., -0.10344, -0.65, 0. ,0. , 0., -0.6911, -0.14161]
yp0 = [0., 0., 0., 0., 0., 1.40059e2, 0., 0.]
switches0 = [False, True, False]
mod = Implicit_Problem(woodpeckertoy, y0, yp0, t0, sw0 = switches0)
mod.state_events = state_events
mod.handle_event = handle_event

sim = IDA(mod)
sim.suppress_alg = True
sim.rtol=1.e-6
sim.atol[3:8] = 1e6
sim.algvar[3:8] = 0.
t, y, yp = sim.simulate(tfinal, ncp)

P.plot(t,y[:,0:3])
P.legend(["z","phiS", "phiB", "z vel", "phiS vel", "phiB vel"])
P.ylabel('y')
P.xlabel('x')
fontlabel_size = 20
tick_size = 14
params = {'lines.markersize' : 0, 'axes.labelsize': fontlabel_size, 'text.fontsize': fontlabel_size, 'legend.fontsize': fontlabel_size, 'xtick.labelsize': tick_size, 'ytick.labelsize': tick_size}
コード例 #14
0
ファイル: woodpecker2.py プロジェクト: lNsufficient/fmnn05
startsw = [1,0,0,0]
y0 = numpy.array([0.5, 0,0, -0, 0, 0.5,-1e-4,0])
yd0 =  numpy.array([-0, 0, 0.5,-g, 1e-12, 0, 0, 0])

w0 = -0.91
y0 = numpy.array([0.5, 0,0, -0, w0, w0,-1e-4,0])
yd0 =  numpy.array([-0, w0, w0,-g, 1e-12, 0, 0, 0])

#y0 = numpy.array([4.83617428e-01, -3.00000000e-02, -2.16050178e-01, 1.67315232e-16, -5.39725367e-14, -1.31300925e+01, -7.20313572e-02, -6.20545138e-02])
#yd0 = numpy.array([1.55140566e-17, -5.00453439e-15, -1.31302838e+01, 6.62087352e-13, -2.13577297e-10, 2.21484026e+02, -4.67637454e+00, -2.89824658e+00])
#startsw = [0,1, 0, 0]

problem = Implicit_Problem(res, y0, yd0, t0, sw0=startsw)

problem.state_events = state_events
problem.handle_event = handle_event
problem.name = 'Woodpecker'

phipIndex = [4, 5]
lambdaIndex = [6, 7]
sim = IDA(problem)
sim.rtol = 1e-6

sim.atol[phipIndex] = 1e8
sim.algvar[phipIndex] = 1
sim.atol[lambdaIndex] = 1e8
sim.algvar[lambdaIndex] = 1 

sim.suppress_alg = True
ncp = 500
コード例 #15
0
startsw = [1,0,0,0]
y0 = numpy.array([0.5, 0,0, -0, 0, 0.5,-1e-4,0])
yd0 =  numpy.array([-0, 0, 0.5,-g, 1e-12, 0, 0, 0])

w0 = -0.91
y0 = numpy.array([0.5, 0,0, -0, w0, w0,-1e-4,0])
yd0 =  numpy.array([-0, w0, w0,-g, 1e-12, 0, 0, 0])

#y0 = numpy.array([4.83617428e-01, -3.00000000e-02, -2.16050178e-01, 1.67315232e-16, -5.39725367e-14, -1.31300925e+01, -7.20313572e-02, -6.20545138e-02])
#yd0 = numpy.array([1.55140566e-17, -5.00453439e-15, -1.31302838e+01, 6.62087352e-13, -2.13577297e-10, 2.21484026e+02, -4.67637454e+00, -2.89824658e+00])
#startsw = [0,1, 0, 0]

problem = Implicit_Problem(res, y0, yd0, t0, sw0=startsw)

problem.state_events = state_events
problem.handle_event = handle_event
problem.name = 'Woodpecker'

phipIndex = [4, 5]
lambdaIndex = [6, 7]
sim = IDA(problem)
sim.rtol = 1e-6

sim.atol[phipIndex] = 1e8
sim.algvar[phipIndex] = 1
sim.atol[lambdaIndex] = 1e8
sim.algvar[lambdaIndex] = 1 

sim.suppress_alg = True
ncp = 500