You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用新版JModelica结合CasADi在Python中获取线性化模型?

Hey there, let's work through this problem since the 2014 paper's JModelica/CasADi workflow is definitely outdated—those old classes like CasadiModel and functions like compile_fmux are long gone, and JModelica's docs on this are pretty sparse. Here are a few solid, up-to-date approaches to get your Modelica nonlinear model into Python for symbolic linearization and controller design:

1. Stick with Latest JModelica (Using PyFMI)

The modern JModelica stack uses PyFMI as its primary Python interface, and it supports extracting symbolic equations directly from compiled FMUs. Here's how to do it:

  • First, install the latest JModelica release and ensure PyFMI is set up correctly.
  • Compile your Modelica model into an FMU with symbolic linearization enabled, then load it to extract equations:
    from pymodelica import compile_fmu
    from pyfmi import load_fmu
    
    # Compile Modelica model to FMU with symbolic support
    fmu_path = compile_fmu(
        "YourModelPackage.YourModel",
        "path/to/your/modelica/files",
        compiler_options={"generate_symbolic_linearization": True}
    )
    
    # Load the compiled FMU
    model = load_fmu(fmu_path)
    
    # Extract symbolic state equations (dx/dt = f(x,u,p))
    symbolic_state_eqs = model.get_symbolic_states_equations()
    # Extract symbolic output equations (y = g(x,u,p))
    symbolic_output_eqs = model.get_symbolic_output_equations()
    
  • Once you have these symbolic expressions, you can use libraries like SymPy or CasADi to compute Jacobian matrices (for linearization) and proceed with controller design.

2. Switch to OpenModelica (More Active Python Support)

OpenModelica has a far more maintained Python interface and better documentation for symbolic model extraction. It's a great alternative if you don't strictly need JModelica:

  • Install OpenModelica and the OMPython package via pip.
  • Load your model and pull symbolic equations directly:
    from OMPython import OMCSessionZMQ
    import sympy
    
    # Initialize OpenModelica session
    omc = OMCSessionZMQ()
    omc.loadModel("Modelica")  # Load base Modelica library
    omc.cd("path/to/your/model/directory")
    omc.loadModel("YourModelPackage")  # Load your custom model package
    
    # Extract symbolic states and equations
    symbolic_states = omc.sendExpression("getSymbolicStates(YourModelPackage.YourModel)")
    state_eqs = omc.sendExpression("getSymbolicStateEquations(YourModelPackage.YourModel)")
    output_eqs = omc.sendExpression("getSymbolicOutputEquations(YourModelPackage.YourModel)")
    
    # Convert equations to SymPy objects for easy manipulation
    sym_state_eqs = sympy.sympify(state_eqs)
    sym_output_eqs = sympy.sympify(output_eqs)
    
  • From here, use SymPy's jacobian function to calculate the linearized A/B/C/D matrices at your desired operating point.

3. Directly Import Modelica to CasADi

CasADi (a popular symbolic computation tool for control) now supports importing Modelica models directly (using OpenModelica or JModelica as a backend). This is perfect if you plan to do controller design directly in CasADi:

  • Install CasADi and ensure OpenModelica is available on your system (CasADi uses it under the hood for Modelica parsing).
  • Import your model and extract symbolic equations + compute linearization:
    import casadi as cs
    
    # Import your Modelica model (point to your .mo file or package)
    model = cs.import_modelica("YourModel.mo")
    
    # Get symbolic variables for states, inputs, outputs, parameters
    x = model.x
    u = model.u
    y = model.y
    p = model.p
    
    # Extract symbolic state and output equations
    f = model.f  # dx/dt = f(x,u,p)
    g = model.g  # y = g(x,u,p)
    
    # Compute Jacobian matrices for linearization
    A = cs.jacobian(f, x)
    B = cs.jacobian(f, u)
    C = cs.jacobian(g, x)
    D = cs.jacobian(g, u)
    
    # You can evaluate these matrices at a specific operating point, or keep them symbolic
    # Example: Evaluate at x0, u0, p0
    A_val = A(x=x0, u=u0, p=p0)
    
  • CasADi also has built-in control design tools, so you can go straight from linearization to designing controllers like LQR, MPC, etc., all within the same environment.

内容的提问来源于stack exchange,提问作者Yannick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:37:34