ydot[3] = -y[1] * lambda_fkt(y[0], y[1]) - 1 return ydot # ydot = -y[0] # return np.array([ydot]) def lambda_fkt(y1, y2): k = 100 return k * (np.sqrt(y1**2 + y2**2) - 1) / np.sqrt(y1**2 + y2**2) #Define an Assimulo problem exp_mod = Explicit_Problem(f, 4) exp_mod.name = 'Simple BDF-2 Example' #Define another Assimulo problem def pend(t,y): #g=9.81 l=0.7134354980239037 gl=13.7503671 return np.array([y[1],-gl*np.sin(y[0])]) #pend_mod=Explicit_Problem(pend, y0=np.array([2.*np.pi,1.])) pend_mod=Explicit_Problem(f, y0=np.array([1., 0., 0., 0.])) pend_mod.name='Nonlinear Pendulum' #Define an explicit solver #exp_sim = BDF_2(pend_mod) #Create a BDF solver exp_sim = CVode(pend_mod) t, y = exp_sim.simulate(10) exp_sim.plot() mpl.show()
def run_example(with_plots=True): r""" Example to demonstrate the use of a preconditioner .. math:: \dot y_1 & = 2 t \sin y_1 + t \sin y_2 \\ \dot y_2 & = 3 t \sin y_1 + 2 t \sin y_2 on return: - :dfn:`exp_mod` problem instance - :dfn:`exp_sim` solver instance """ #Define the rhs def rhs(t, y): A = np.array([[2.0, 1.0], [3.0, 2.0]]) yd = np.dot(A * t, np.sin(y)) return yd #Define the preconditioner setup function def prec_setup(t, y, fy, jok, gamma, data): A = np.array([[2.0, 1.0], [3.0, 2.0]]) #If jok is false the jacobian data needs to be recomputed if jok == False: #Extract the diagonal of the jacobian to form a Jacobi preconditioner a0 = A[0, 0] * t * np.cos(y[0]) a1 = A[1, 1] * t * np.cos(y[1]) a = np.array([(1. - gamma * a0), (1. - gamma * a1)]) #Return true (jacobian data was recomputed) and the new data return [True, a] #If jok is true the existing jacobian data can be reused if jok == True: #Return false (jacobian data was reused) and the old data return [False, data] #Define the preconditioner solve function def prec_solve(t, y, fy, r, gamma, delta, data): #Solve the system Pz = r z0 = r[0] / data[0] z1 = r[1] / data[1] z = np.array([z0, z1]) return z #Initial conditions y0 = [1.0, 2.0] #Define an Assimulo problem exp_mod = Explicit_Problem( rhs, y0, name="Example of using a preconditioner in SUNDIALS") #Set the preconditioner setup and solve function for the problem exp_mod.prec_setup = prec_setup exp_mod.prec_solve = prec_solve #Create a CVode solver exp_sim = CVode(exp_mod) #Set the parameters for the solver exp_sim.iter = 'Newton' exp_sim.discr = 'BDF' exp_sim.atol = 1e-5 exp_sim.rtol = 1e-5 exp_sim.linear_solver = 'SPGMR' exp_sim.precond = "PREC_RIGHT" #Set the desired type of preconditioning #Simulate t, y = exp_sim.simulate(5) if with_plots: exp_sim.plot() #Basic verification nose.tools.assert_almost_equal(y[-1, 0], 3.11178295, 4) nose.tools.assert_almost_equal(y[-1, 1], 3.19318992, 4) return exp_mod, exp_sim
yd = np.dot(A, y) + b return yd #def rhs(t,y): # A =np.array([[0, 1],[-2, -1]]) # yd=np.dot(A, y) # # return yd def L(y, k): norm = ln.norm(y[0:2]) return k * (norm - 1) / norm y0 = np.array([1.0, 1.0, 1.0, 1.0]) t0 = 0.0 model = Explicit_Problem(rhs, y0, t0) # Create an Assimulo problem model.name = 'Linear Test ODE' # Specifies the name of problem sim = CVode(model) tfinal = 100.0 #Specify the final time t, y = sim.simulate( tfinal) #Use the .simulate method to simulate and provide the final time sim.plot()
def rhs(t, y): k = 10 A = np.array([[0, 0, 1, 0], [0, 0, 0, 1], [-L(y, k), 0, 0, 0], [0, -L(y, k), 0, 0]]) b = np.array([0, 0, 0, -1]) yd = np.dot(A, y) + b return yd def L(y, k): norm = ln.norm(y[0:2]) return k * (norm - 1) / norm pend_mod = Explicit_Problem(rhs, y0 = np.array([1.0, 1.0, 1.0, 1.0])) pend_mod.name = 'Nonlinear Pendulum' # Define an explicit solver exp_sim = BDF_3(pend_mod) # Create a BDF solver t, y = exp_sim.simulate(4) exp_sim.plot() mpl.show() toltmp=0.1 cvod= CVode(pend_mod) cvod.atol=toltmp cvod.rtol=toltmp cvod.maxord=19 cvod.simulate(5) cvod.plot()