Exemple #1
0
def run_demo(with_plots=True):
    """
    An example on how to simulate a model using the DAE simulator. The result 
    can be compared with that of sim_rlc.py which has solved the same problem 
    using dymola. Also writes information to a file.
    """

    curr_dir = os.path.dirname(os.path.abspath(__file__));

    model_name = 'RLC_Circuit'
    mofile = curr_dir+'/files/RLC_Circuit.mo'
    
    jmu_name = compile_jmu(model_name, mofile)
    model = JMUModel(jmu_name)
    init_res = model.initialize()

    (E_dae,A_dae,B_dae,F_dae,g_dae,state_names,input_names,algebraic_names, \
     dx0,x0,u0,w0,t0) = linearize_dae(init_res.model)
    
    (A_ode,B_ode,g_ode,H_ode,M_ode,q_ode) = linear_dae_to_ode(
        E_dae,A_dae,B_dae,F_dae,g_dae)

    res1 = model.simulate()
    
    jmu_name = compile_jmu("RLC_Circuit_Linearized",mofile)
    lin_model = JMUModel(jmu_name)
    res2 = lin_model.simulate()
    
    c_v_1 = res1['capacitor.v']
    i_p_i_1 = res1['inductor.p.i']
    i_p1_i_1 = res1['inductor1.p.i']
    t_1 = res1['time']
    
    c_v_2 = res2['x[1]']
    i_p_i_2 = res2['x[2]']
    i_p1_i_2 = res2['x[3]']
    t_2 = res2['time']

    assert N.abs(res1.final('capacitor.v') - res2.final('x[1]')) < 1e-3
    
    if with_plots:
        p.figure(1)
        p.hold(True)
        p.subplot(311)
        p.plot(t_1,c_v_1)
        p.plot(t_2,c_v_2,'g')
        p.ylabel('c.v')
        p.legend(('original model','linearized ODE'))
        p.grid()
        p.subplot(312)
        p.plot(t_1,i_p_i_1)
        p.plot(t_2,i_p_i_2,'g')
        p.ylabel('i.p.i')
        p.grid()
        p.subplot(313)
        p.plot(t_1,i_p1_i_1)
        p.plot(t_2,i_p1_i_2,'g')
        p.ylabel('i.p1.i')
        p.grid()
        p.show()
def run_demo(with_plots=True):
    """
    An example on how to simulate a model using the DAE simulator. The result 
    can be compared with that of sim_rlc.py which has solved the same problem 
    using dymola. Also writes information to a file.
    """

    curr_dir = os.path.dirname(os.path.abspath(__file__))

    model_name = "RLC_Circuit"
    mofile = curr_dir + "/files/RLC_Circuit.mo"

    jmu_name = compile_jmu(model_name, mofile)
    model = JMUModel(jmu_name)
    init_res = model.initialize()

    (E_dae, A_dae, B_dae, F_dae, g_dae, state_names, input_names, algebraic_names, dx0, x0, u0, w0, t0) = linearize_dae(
        init_res.model
    )

    (A_ode, B_ode, g_ode, H_ode, M_ode, q_ode) = linear_dae_to_ode(E_dae, A_dae, B_dae, F_dae, g_dae)

    res1 = model.simulate()

    jmu_name = compile_jmu("RLC_Circuit_Linearized", mofile)
    lin_model = JMUModel(jmu_name)
    res2 = lin_model.simulate()

    c_v_1 = res1["capacitor.v"]
    i_p_i_1 = res1["inductor.p.i"]
    i_p1_i_1 = res1["inductor1.p.i"]
    t_1 = res1["time"]

    c_v_2 = res2["x[1]"]
    i_p_i_2 = res2["x[2]"]
    i_p1_i_2 = res2["x[3]"]
    t_2 = res2["time"]

    assert N.abs(res1.final("capacitor.v") - res2.final("x[1]")) < 1e-3

    if with_plots:
        p.figure(1)
        p.hold(True)
        p.subplot(311)
        p.plot(t_1, c_v_1)
        p.plot(t_2, c_v_2, "g")
        p.ylabel("c.v")
        p.legend(("original model", "linearized ODE"))
        p.grid()
        p.subplot(312)
        p.plot(t_1, i_p_i_1)
        p.plot(t_2, i_p_i_2, "g")
        p.ylabel("i.p.i")
        p.grid()
        p.subplot(313)
        p.plot(t_1, i_p1_i_1)
        p.plot(t_2, i_p1_i_2, "g")
        p.ylabel("i.p1.i")
        p.grid()
        p.show()
 def getLinearDaeFromJmu(self):
     """
     Takes a ModelicaObject, and returns a dictionary containing the output
     """
     if(self.linJmu):
         return self.linJmu
     else:
         init_res=self.jmu.initialize()
         (E_dae,A_dae,B_dae,F_dae,g_dae,state_names,input_names,algebraic_names, dx0,x0,u0,w0,t0) = linearize_dae(init_res.model);
         toReturn={"E":E_dae,"A":A_dae,"B":B_dae,"F":F_dae,"g":g_dae,"State_names":state_names,"input_names":input_names,"algebraic_names":algebraic_names,"dx0":dx0,"x0":x0,"u0":u0,"w0":w0,"t0":t0}
         self.linJmu=toReturn;
     return self.linJmu