def make_explicit_sim(self): explicit_sim = CVode(self.explicit_problem) explicit_sim.iter = 'Newton' explicit_sim.discr = 'BDF' explicit_sim.rtol = 1e-7 explicit_sim.atol = 1e-7 explicit_sim.sensmethod = 'SIMULTANEOUS' explicit_sim.suppress_sens = True explicit_sim.report_continuously = False explicit_sim.usesens = False explicit_sim.verbosity = 50 if self.use_jac and self.model_jac is not None: explicit_sim.usejac = True else: explicit_sim.usejac = False return explicit_sim
def run_example(with_plots=True): """ This is the same example from the Sundials package (cvsRoberts_FSA_dns.c) This simple example problem for CVode, due to Robertson, is from chemical kinetics, and consists of the following three equations: .. math:: \dot y_1 &= -p_1 y_1 + p_2 y_2 y_3 \\ \dot y_2 &= p_1 y_1 - p_2 y_2 y_3 - p_3 y_2^2 \\ \dot y_3 &= p_3 y_2^2 on return: - :dfn:`exp_mod` problem instance - :dfn:`exp_sim` solver instance """ def f(t, y, p): p3 = 3.0e7 yd_0 = -p[0] * y[0] + p[1] * y[1] * y[2] yd_1 = p[0] * y[0] - p[1] * y[1] * y[2] - p3 * y[1]**2 yd_2 = p3 * y[1]**2 return N.array([yd_0, yd_1, yd_2]) #The initial conditions y0 = [1.0, 0.0, 0.0] #Initial conditions for y #Create an Assimulo explicit problem exp_mod = Explicit_Problem(f, y0, name='Sundials test example: Chemical kinetics') #Sets the options to the problem exp_mod.p0 = [0.040, 1.0e4] #Initial conditions for parameters exp_mod.pbar = [0.040, 1.0e4] #Create an Assimulo explicit solver (CVode) exp_sim = CVode(exp_mod) #Sets the paramters exp_sim.iter = 'Newton' exp_sim.discr = 'BDF' exp_sim.rtol = 1.e-4 exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6]) exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitvity method used exp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test. exp_sim.report_continuously = True #Simulate t, y = exp_sim.simulate( 4, 400) #Simulate 4 seconds with 400 communication points #Plot if with_plots: import pylab as P P.plot(t, y) P.xlabel('Time') P.ylabel('State') P.title(exp_mod.name) P.show() #Basic test nose.tools.assert_almost_equal(y[-1][0], 9.05518032e-01, 4) nose.tools.assert_almost_equal(y[-1][1], 2.24046805e-05, 4) nose.tools.assert_almost_equal(y[-1][2], 9.44595637e-02, 4) nose.tools.assert_almost_equal( exp_sim.p_sol[0][-1][0], -1.8761, 2) #Values taken from the example in Sundials nose.tools.assert_almost_equal(exp_sim.p_sol[1][-1][0], 2.9614e-06, 8) return exp_mod, exp_sim
def run_example(with_plots=True): r""" This is the same example from the Sundials package (cvsRoberts_FSA_dns.c) Its purpose is to demonstrate the use of parameters in the differential equation. This simple example problem for CVode, due to Robertson see http://www.dm.uniba.it/~testset/problems/rober.php, is from chemical kinetics, and consists of the system: .. math:: \dot y_1 &= -p_1 y_1 + p_2 y_2 y_3 \\ \dot y_2 &= p_1 y_1 - p_2 y_2 y_3 - p_3 y_2^2 \\ \dot y_3 &= p_3 y_ 2^2 on return: - :dfn:`exp_mod` problem instance - :dfn:`exp_sim` solver instance """ def f(t, y, p): yd_0 = -p[0]*y[0]+p[1]*y[1]*y[2] yd_1 = p[0]*y[0]-p[1]*y[1]*y[2]-p[2]*y[1]**2 yd_2 = p[2]*y[1]**2 return N.array([yd_0,yd_1,yd_2]) def jac(t,y, p): J = N.array([[-p[0], p[1]*y[2], p[1]*y[1]], [p[0], -p[1]*y[2]-2*p[2]*y[1], -p[1]*y[1]], [0.0, 2*p[2]*y[1],0.0]]) return J def fsens(t, y, s, p): J = N.array([[-p[0], p[1]*y[2], p[1]*y[1]], [p[0], -p[1]*y[2]-2*p[2]*y[1], -p[1]*y[1]], [0.0, 2*p[2]*y[1],0.0]]) P = N.array([[-y[0],y[1]*y[2],0], [y[0], -y[1]*y[2], -y[1]**2], [0,0,y[1]**2]]) return J.dot(s)+P #The initial conditions y0 = [1.0,0.0,0.0] #Initial conditions for y #Create an Assimulo explicit problem exp_mod = Explicit_Problem(f,y0, name='Robertson Chemical Kinetics Example') exp_mod.rhs_sens = fsens exp_mod.jac = jac #Sets the options to the problem exp_mod.p0 = [0.040, 1.0e4, 3.0e7] #Initial conditions for parameters exp_mod.pbar = [0.040, 1.0e4, 3.0e7] #Create an Assimulo explicit solver (CVode) exp_sim = CVode(exp_mod) #Sets the solver paramters exp_sim.iter = 'Newton' exp_sim.discr = 'BDF' exp_sim.rtol = 1.e-4 exp_sim.atol = N.array([1.0e-8, 1.0e-14, 1.0e-6]) exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitvity method used exp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test. exp_sim.report_continuously = True #Simulate t, y = exp_sim.simulate(4,400) #Simulate 4 seconds with 400 communication points #Basic test nose.tools.assert_almost_equal(y[-1][0], 9.05518032e-01, 4) nose.tools.assert_almost_equal(y[-1][1], 2.24046805e-05, 4) nose.tools.assert_almost_equal(y[-1][2], 9.44595637e-02, 4) nose.tools.assert_almost_equal(exp_sim.p_sol[0][-1][0], -1.8761, 2) #Values taken from the example in Sundials nose.tools.assert_almost_equal(exp_sim.p_sol[1][-1][0], 2.9614e-06, 8) nose.tools.assert_almost_equal(exp_sim.p_sol[2][-1][0], -4.9334e-10, 12) #Plot if with_plots: P.plot(t, y) P.title(exp_mod.name) P.xlabel('Time') P.ylabel('State') P.show() return exp_mod, exp_sim
def run_example(with_plots=True): r""" This example shows how to use Assimulo and CVode for simulating sensitivities for initial conditions. .. math:: \dot y_1 &= -(k_{01}+k_{21}+k_{31}) y_1 + k_{12} y_2 + k_{13} y_3 + b_1\\ \dot y_2 &= k_{21} y_1 - (k_{02}+k_{12}) y_2 \\ \dot y_3 &= k_{31} y_1 - k_{13} y_3 with the parameter dependent inital conditions :math:`y_1(0) = 0, y_2(0) = 0, y_3(0) = 0` . The initial values are taken as parameters :math:`p_1,p_2,p_3` for the computation of the sensitivity matrix, see http://sundials.2283335.n4.nabble.com/Forward-sensitivities-for-initial-conditions-td3239724.html on return: - :dfn:`exp_mod` problem instance - :dfn:`exp_sim` solver instance """ def f(t, y, p): y1, y2, y3 = y k01 = 0.0211 k02 = 0.0162 k21 = 0.0111 k12 = 0.0124 k31 = 0.0039 k13 = 0.000035 b1 = 49.3 yd_0 = -(k01 + k21 + k31) * y1 + k12 * y2 + k13 * y3 + b1 yd_1 = k21 * y1 - (k02 + k12) * y2 yd_2 = k31 * y1 - k13 * y3 return N.array([yd_0, yd_1, yd_2]) #The initial conditions y0 = [0.0, 0.0, 0.0] #Initial conditions for y p0 = [0.0, 0.0, 0.0] #Initial conditions for parameters yS0 = N.array([[1, 0, 0], [0, 1, 0], [0, 0, 1.]]) #Create an Assimulo explicit problem exp_mod = Explicit_Problem(f, y0, p0=p0, name='Example: Computing Sensitivities') #Sets the options to the problem exp_mod.yS0 = yS0 #Create an Assimulo explicit solver (CVode) exp_sim = CVode(exp_mod) #Sets the paramters exp_sim.iter = 'Newton' exp_sim.discr = 'BDF' exp_sim.rtol = 1e-7 exp_sim.atol = 1e-6 exp_sim.pbar = [ 1, 1, 1 ] #pbar is used to estimate the tolerances for the parameters exp_sim.report_continuously = True #Need to be able to store the result using the interpolate methods exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitvity method used exp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test. #Simulate t, y = exp_sim.simulate(400) #Simulate 400 seconds #Basic test nose.tools.assert_almost_equal(y[-1][0], 1577.6552477, 5) nose.tools.assert_almost_equal(y[-1][1], 611.9574565, 5) nose.tools.assert_almost_equal(y[-1][2], 2215.88563217, 5) nose.tools.assert_almost_equal(exp_sim.p_sol[0][1][0], 1.0) #Plot if with_plots: title_text = r"Sensitivity w.r.t. ${}$" legend_text = r"$\mathrm{{d}}{}/\mathrm{{d}}{}$" P.figure(1) P.subplot(221) P.plot(t, N.array(exp_sim.p_sol[0])[:, 0], t, N.array(exp_sim.p_sol[0])[:, 1], t, N.array(exp_sim.p_sol[0])[:, 2]) P.title(title_text.format('p_1')) P.legend((legend_text.format('y_1', 'p_1'), legend_text.format('y_1', 'p_2'), legend_text.format('y_1', 'p_3'))) P.subplot(222) P.plot(t, N.array(exp_sim.p_sol[1])[:, 0], t, N.array(exp_sim.p_sol[1])[:, 1], t, N.array(exp_sim.p_sol[1])[:, 2]) P.title(title_text.format('p_2')) P.legend((legend_text.format('y_2', 'p_1'), legend_text.format('y_2', 'p_2'), legend_text.format('y_2', 'p_3'))) P.subplot(223) P.plot(t, N.array(exp_sim.p_sol[2])[:, 0], t, N.array(exp_sim.p_sol[2])[:, 1], t, N.array(exp_sim.p_sol[2])[:, 2]) P.title(title_text.format('p_3')) P.legend((legend_text.format('y_3', 'p_1'), legend_text.format('y_3', 'p_2'), legend_text.format('y_3', 'p_3'))) P.subplot(224) P.title('ODE Solution') P.plot(t, y) P.suptitle(exp_mod.name) P.show() return exp_mod, exp_sim
def func_set_system(): ############################################## #% initial conditions P_0 = depth*9.8*2500. #; % initial chamber pressure (Pa) T_0 = 1200 #; % initial chamber temperature (K) eps_g0 = 0.04 #; % initial gas volume fraction rho_m0 = 2600 #; % initial melt density (kg/m^3) rho_x0 = 3065 #; % initial crystal density (kg/m^3) a = 1000 #; % initial radius of the chamber (m) V_0 = (4.*pi/3.)*a**3. #; % initial volume of the chamber (m^3) ############################################## ############################################## IC = numpy.array([P_0,T_0,eps_g0,V_0,rho_m0,rho_x0]) # % store initial conditions ## Gas (eps_g = zero), eps_x is zero, too many crystals, 50 % crystallinity,eruption (yes/no) sw0 = [False,False,False,False,False] ############################################## #% error tolerances used in ode method dt = 30e7 N = int(round((end_time-begin_time)/dt)) ############################################## #Define an Assimulo problem exp_mod = Chamber_Problem(depth=depth,t0=begin_time,y0=IC,sw0=sw0) exp_mod.param['T_in'] = 1200. exp_mod.param['eps_g_in'] = 0.0 # Gas fraction of incoming melt - gas phase .. exp_mod.param['m_eq_in'] = 0.05 # Volatile fraction of incoming melt exp_mod.param['Mdot_in'] = mdot exp_mod.param['eta_x_max'] = 0.64 # Locking fraction exp_mod.param['delta_Pc'] = 20e6 exp_mod.tcurrent = begin_time exp_mod.radius = a exp_mod.permeability = perm_val exp_mod.R_steps = 1500 exp_mod.dt_init = dt ################# exp_mod.R_outside = numpy.linspace(a,3.*a,exp_mod.R_steps); exp_mod.T_out_all =numpy.array([exp_mod.R_outside*0.]) exp_mod.P_out_all =numpy.array([exp_mod.R_outside*0.]) exp_mod.sigma_rr_all = numpy.array([exp_mod.R_outside*0.]) exp_mod.sigma_theta_all = numpy.array([exp_mod.R_outside*0.]) exp_mod.sigma_eff_rr_all = numpy.array([exp_mod.R_outside*0.]) exp_mod.sigma_eff_theta_all = numpy.array([exp_mod.R_outside*0.]) exp_mod.max_count = 1 # counting for the append me arrays .. exp_mod.P_list = append_me() exp_mod.P_list.update(P_0-exp_mod.plith) exp_mod.T_list = append_me() exp_mod.T_list.update(T_0-exp_mod.param['T_S']) exp_mod.P_flux_list = append_me() exp_mod.P_flux_list.update(0) exp_mod.T_flux_list = append_me() exp_mod.T_flux_list.update(0) exp_mod.times_list = append_me() exp_mod.times_list.update(1e-7) exp_mod.T_out,exp_mod.P_out,exp_mod.sigma_rr,exp_mod.sigma_theta,exp_mod.T_der= Analytical_sol_cavity_T_Use(exp_mod.T_list.data[:exp_mod.max_count],exp_mod.P_list.data[:exp_mod.max_count],exp_mod.radius,exp_mod.times_list.data[:exp_mod.max_count],exp_mod.R_outside,exp_mod.permeability,exp_mod.param['material']) exp_mod.param['heat_cond'] = 1 # Turn on/off heat conduction exp_mod.param['visc_relax'] = 1 # Turn on/off viscous relaxation exp_mod.param['press_relax'] = 1 ## Turn on/off pressure diffusion exp_mod.param['frac_rad_Temp'] =0.75 exp_mod.param['vol_degass'] = 1. #exp_mod.state_events = stopChamber #Sets the state events to the problem #exp_mod.handle_event = handle_event #Sets the event handling to the problem #Sets the options to the problem #exp_mod.p0 = [beta_r, beta_m]#, beta_x, alpha_r, alpha_m, alpha_x, L_e, L_m, c_m, c_g, c_x, eruption, heat_cond, visc_relax] #Initial conditions for parameters #exp_mod.pbar = [beta_r, beta_m]#, beta_x, alpha_r, alpha_m, alpha_x, L_e, L_m, c_m, c_g, c_x, eruption, heat_cond, visc_relax] #Define an explicit solver exp_sim = CVode(exp_mod) #Create a CVode solver #Sets the parameters #exp_sim.iter = 'Newton' #exp_sim.discr = 'BDF' #exp_sim.inith = 1e-7 exp_sim.rtol = 1.e-7 exp_sim.maxh = 3e7 exp_sim.atol = 1e-7 exp_sim.sensmethod = 'SIMULTANEOUS' #Defines the sensitvity method used exp_sim.suppress_sens = False #Dont suppress the sensitivity variables in the error test. #exp_sim.usesens = True #exp_sim.report_continuously = True return exp_mod,exp_sim,N