def test_spgmr(self): f = lambda t, y: N.array([y[1], -9.82]) fsw = lambda t, y, sw: N.array([y[1], -9.82]) fp = lambda t, y, p: N.array([y[1], -9.82]) fswp = lambda t, y, sw, p: N.array([y[1], -9.82]) jacv = lambda t, y, fy, v: N.dot(N.array([[0, 1.], [0, 0]]), v) jacvsw = lambda t, y, fy, v, sw: N.dot(N.array([[0, 1.], [0, 0]]), v) jacvp = lambda t, y, fy, v, p: N.dot(N.array([[0, 1.], [0, 0]]), v) jacvswp = lambda t, y, fy, v, sw, p: N.dot(N.array([[0, 1.], [0, 0]]), v) y0 = [1.0, 0.0] #Initial conditions def run_sim(exp_mod): exp_sim = CVode(exp_mod) #Create a CVode solver exp_sim.linear_solver = 'SPGMR' #Change linear solver #Simulate t, y = exp_sim.simulate( 5, 1000) #Simulate 5 seconds with 1000 communication points #Basic tests nose.tools.assert_almost_equal(y[-1][0], -121.75000000, 4) nose.tools.assert_almost_equal(y[-1][1], -49.100000000) exp_mod = Explicit_Problem(f, y0) exp_mod.jacv = jacv #Sets the jacobian run_sim(exp_mod) #Need someway of suppressing error messages from deep down in the Cython wrapper #See http://stackoverflow.com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer try: from cStringIO import StringIO except ImportError: from io import StringIO import sys stderr = sys.stderr sys.stderr = StringIO() exp_mod = Explicit_Problem(f, y0) exp_mod.jacv = jacvsw #Sets the jacobian nose.tools.assert_raises(CVodeError, run_sim, exp_mod) exp_mod = Explicit_Problem(fswp, y0, sw0=[True], p0=1.0) exp_mod.jacv = jacvsw #Sets the jacobian nose.tools.assert_raises(CVodeError, run_sim, exp_mod) #Restore standard error sys.stderr = stderr exp_mod = Explicit_Problem(fp, y0, p0=1.0) exp_mod.jacv = jacvp #Sets the jacobian run_sim(exp_mod) exp_mod = Explicit_Problem(fsw, y0, sw0=[True]) exp_mod.jacv = jacvsw #Sets the jacobian run_sim(exp_mod) exp_mod = Explicit_Problem(fswp, y0, sw0=[True], p0=1.0) exp_mod.jacv = jacvswp #Sets the jacobian run_sim(exp_mod)
def run_example(with_plots=True): #Defines the rhs def f(t, y): yd_0 = y[1] yd_1 = -9.82 return N.array([yd_0, yd_1]) #Defines the jacobian*vector product def jacv(t, y, fy, v): j = N.array([[0, 1.], [0, 0]]) return N.dot(j, v) y0 = [1.0, 0.0] #Initial conditions #Defines an Assimulo explicit problem exp_mod = Explicit_Problem(f, y0) exp_mod.jacv = jacv #Sets the jacobian exp_mod.name = 'Example using the Jacobian Vector product' exp_sim = CVode(exp_mod) #Create a CVode solver #Set the parameters exp_sim.iter = 'Newton' #Default 'FixedPoint' exp_sim.discr = 'BDF' #Default 'Adams' exp_sim.atol = 1e-5 #Default 1e-6 exp_sim.rtol = 1e-5 #Default 1e-6 exp_sim.linear_solver = 'SPGMR' #Change linear solver #exp_sim.options["usejac"] = False #Simulate t, y = exp_sim.simulate( 5, 1000) #Simulate 5 seconds with 1000 communication points #Basic tests nose.tools.assert_almost_equal(y[-1][0], -121.75000000, 4) nose.tools.assert_almost_equal(y[-1][1], -49.100000000) #Plot if with_plots: P.plot(t, y) P.show()
def run_example(with_plots=True): #Defines the rhs def f(t,y): yd_0 = y[1] yd_1 = -9.82 return N.array([yd_0,yd_1]) #Defines the jacobian*vector product def jacv(t,y,fy,v): j = N.array([[0,1.],[0,0]]) return N.dot(j,v) y0 = [1.0,0.0] #Initial conditions #Defines an Assimulo explicit problem exp_mod = Explicit_Problem(f,y0) exp_mod.jacv = jacv #Sets the jacobian exp_mod.name = 'Example using the Jacobian Vector product' exp_sim = CVode(exp_mod) #Create a CVode solver #Set the parameters exp_sim.iter = 'Newton' #Default 'FixedPoint' exp_sim.discr = 'BDF' #Default 'Adams' exp_sim.atol = 1e-5 #Default 1e-6 exp_sim.rtol = 1e-5 #Default 1e-6 exp_sim.linear_solver = 'SPGMR' #Change linear solver #exp_sim.options["usejac"] = False #Simulate t, y = exp_sim.simulate(5, 1000) #Simulate 5 seconds with 1000 communication points #Basic tests nose.tools.assert_almost_equal(y[-1][0],-121.75000000,4) nose.tools.assert_almost_equal(y[-1][1],-49.100000000) #Plot if with_plots: P.plot(t,y) P.show()
def run_example(with_plots=True): r""" An example for CVode with scaled preconditioned GMRES method as a special linear solver. Note, how the operation Jacobian times vector is provided. ODE: .. math:: \dot y_1 &= y_2 \\ \dot y_2 &= -9.82 on return: - :dfn:`exp_mod` problem instance - :dfn:`exp_sim` solver instance """ #Defines the rhs def f(t, y): yd_0 = y[1] yd_1 = -9.82 return N.array([yd_0, yd_1]) #Defines the Jacobian*vector product def jacv(t, y, fy, v): j = N.array([[0, 1.], [0, 0]]) return N.dot(j, v) y0 = [1.0, 0.0] #Initial conditions #Defines an Assimulo explicit problem exp_mod = Explicit_Problem( f, y0, name='Example using the Jacobian Vector product') exp_mod.jacv = jacv #Sets the Jacobian exp_sim = CVode(exp_mod) #Create a CVode solver #Set the parameters exp_sim.iter = 'Newton' #Default 'FixedPoint' exp_sim.discr = 'BDF' #Default 'Adams' exp_sim.atol = 1e-5 #Default 1e-6 exp_sim.rtol = 1e-5 #Default 1e-6 exp_sim.linear_solver = 'SPGMR' #Change linear solver #exp_sim.options["usejac"] = False #Simulate t, y = exp_sim.simulate( 5, 1000) #Simulate 5 seconds with 1000 communication points #Basic tests nose.tools.assert_almost_equal(y[-1][0], -121.75000000, 4) nose.tools.assert_almost_equal(y[-1][1], -49.100000000) #Plot if with_plots: P.plot(t, y) P.xlabel('Time') P.ylabel('State') P.title(exp_mod.name) P.show() return exp_mod, exp_sim
def integration_assimulo(self, **kwargs): """ Perform time integration for ODEs with the assimulo package """ assert self.set_time_setting == 1, 'Time discretization must be specified first' if self.tclose > 0: close = True else: close = False # Control vector self.U = interpolate(self.boundary_cntrl_space, self.Vb).vector()[self.bndr_i_b] if self.discontinous_boundary_values == 1: self.U[self.Corner_indices] = self.U[self.Corner_indices]/2 # Definition of the sparse solver for the ODE rhs function to # be defined next #my_solver = factorized(csc_matrix(self.M)) my_solver = factorized(self.M) #my_jac_o = csr_matrix(my_solver(self.J @ self.Q)) #my_jac_c = csr_matrix(my_solver((self.J - self.R) @ self.Q)) # Definition of the rhs function required in assimulo def rhs(t,y): """ Definition of the rhs function required in the ODE part of assimulo """ if close: if t < self.tclose: z = self.my_mult(self.J, self.my_mult(self.Q,y)) + self.my_mult(self.Bext,self.U* self.boundary_cntrl_time(t,self.tclose)) else: z = self.my_mult((self.J - self.R), self.my_mult(self.Q,y)) else: z = self.my_mult(self.J, self.my_mult(self.Q,y)) + self.my_mult(self.Bext,self.U* self.boundary_cntrl_time(t,self.tclose)) return my_solver(z) def jacobian(t,y): """ Jacobian related to the ODE formulation """ if close: if t < self.tclose: my_jac = my_jac_o else: my_jac = my_jac_c else: my_jac = my_jac_o return my_jac def jacv(t,y,fy,v): """ Jacobian matrix-vector product related to the ODE formulation """ if close: if t < self.tclose: z = self.my_mult(self.J, self.my_mult(self.Q,v) ) else: z = self.my_mult((self.J - self.R), self.my_mult(self.Q,v)) else: z = self.my_mult(self.J, self.my_mult(self.Q,v)) return my_solver(z) print('ODE Integration using assimulo built-in functions:') # # https://jmodelica.org/assimulo/_modules/assimulo/examples/cvode_with_preconditioning.html#run_example # model = Explicit_Problem(rhs,self.A0,self.tinit) #model.jac = jacobian model.jacv = jacv sim = CVode(model,**kwargs) sim.atol = 1e-3 sim.rtol = 1e-3 sim.linear_solver = 'SPGMR' sim.maxord = 3 #sim.usejac = True #sim = RungeKutta34(model,**kwargs) time_span, ODE_solution = sim.simulate(self.tfinal) A_ode = ODE_solution.transpose() # Hamiltonian self.Nt = A_ode.shape[1] self.tspan = np.array(time_span) Ham_ode = np.zeros(self.Nt) for k in range(self.Nt): #Ham_ode[k] = 1/2 * A_ode[:,k] @ self.M @ self.Q @ A_ode[:,k] Ham_ode[k] = 1/2 * self.my_mult(A_ode[:,k].T, \ self.my_mult(self.M, self.my_mult(self.Q, A_ode[:,k]))) # Get q variables Aq_ode = A_ode[:self.Nq,:] # Get p variables Ap_ode = A_ode[self.Nq:,:] # Get Deformation Rho = np.zeros(self.Np) for i in range(self.Np): Rho[i] = self.rho(self.coord_p[i]) W_ode = np.zeros((self.Np,self.Nt)) theta = .5 for k in range(self.Nt-1): W_ode[:,k+1] = W_ode[:,k] + self.dt * 1/Rho[:] * ( theta * Ap_ode[:,k+1] + (1-theta) * Ap_ode[:,k] ) self.Ham_ode = Ham_ode return Aq_ode, Ap_ode, Ham_ode, W_ode, np.array(time_span)