Docs/AI Model Integration/Using AI Components in Models

Using AI Components in Models

After an ONNX model is uploaded, the AI component appears in the component tree on the left of the editor. You can drag it onto the canvas, connect it, and include it in simulations just like standard physics components. This article covers how to use AI components when modeling, and what to watch out for.

Locating AI Components in the Component Tree

Successfully parsed AI components are grouped by category under the top-level User Components node in the editor's left component tree:

User Components
├── Electrical
│   ├── LinearRegressor      ← AI component (third-level leaf node)
│   └── SpringPendulum1D_CS  ← FMU component
├── Mechanical
│   └── DamperFMU_CS
└── Uncategorized
    └── MyRegressor

AI components are structured like FMU components, both as third-level leaf nodes: the first level is User Components, the second level is the category, and the third level is the AI component itself. Each AI component shows its dedicated icon (with an AI badge in the center) and its IO counts (e.g. 1 in / 1 out).

AI component in the editor component tree

Dragging an AI Component onto the Canvas

  1. Find the target AI component in the component tree on the left.
  2. Press and hold the mouse button and drag it into the canvas area.
  3. Release the mouse; an AI node appears on the canvas with input ports on the left and output ports on the right.

The number and names of the ports on the node map one-to-one to the ONNX model's input/output tensors:

ONNX interface Canvas ports Position
Input tensor (scalar) 1 input port Left side of the node
Input tensor (vector [N]) N input ports Left side of the node
Output tensor (scalar) 1 output port Right side of the node
Output tensor (vector [M]) M output ports Right side of the node

Dragging an AI component onto the canvas

Connection Rules

AI component ports are causal signal ports (RealInput / RealOutput); connections follow these rules:

  • An input port can only connect to an output port;
  • An output port can only connect to an input port;
  • Input ports cannot connect to each other, and neither can output ports;
  • Connections involving AI ports are equality (equation assignment); no connect() is generated.

For example, connect a sine signal source to the AI component's input, then connect the AI component's output to a scope; the code preview shows these equations:

@named src = Sine(amplitude=2.0, frequency=1/(2π))
@named ai1 = LinearRegressor()

@named model = System([
    ai1.input ~ src.output,
], t, [], []; systems=[src, ai1])

Tip: An AI component is a black-box function; prefer using it in a feedforward arrangement—upstream dynamics drive the AI input, and the AI output drives downstream dynamics. Avoid feeding the AI output back algebraically into its own input, which easily creates an algebraic loop that is hard to solve.

Parameter Panel

Click an AI node on the canvas; the properties panel on the right shows different content depending on the component mode.

Static Mode

In static mode the AI component has no tunable parameters inside. The properties panel shows:

  • A notice that this is a black-box component with no tunable physics parameters;
  • The input/output port list;
  • A summary of the ONNX metadata (model name, input/output shapes, opset, etc.).

Lag Filter Mode

In lag filter mode the properties panel shows the parameter τ (the first-order lag time constant of the output), with a default of 1e-2 s. You can edit the value directly; the code preview updates in sync:

@named ai1 = LinearRegressor(τ=0.2)
Mode Tunable parameter Properties panel
Static mode None Black-box notice + ports/metadata
Lag filter mode τ Number input, default 1e-2

AI component properties panel

These properties correspond to the parameter and port definitions in the generated Julia wrapper file.

AI component properties

Solver Constraints

ONNX inference runs through a C library and is opaque to Julia's automatic differentiation. Models containing AI components therefore face these solver constraints:

Solver type Available Notes
Explicit solvers (e.g. Tsit5, Vern7) ✅ No Jacobian needed; the black box is called as a plain function
Implicit solver + AutoFiniteDiff() ✅ Jacobian by finite differences; no Dual numbers involved
Implicit solver + default ForwardDiff ❌ Dual numbers are truncated inside the black box, producing a wrong Jacobian

The CoModel simulation engine detects AI components in the model automatically and picks an appropriate solver strategy:

  • Without algebraic coupling, Tsit5() is used by default;
  • With algebraic coupling, Rodas5P(autodiff=AutoFiniteDiff()) is used by default;
  • If you select an implicit solver manually, the system automatically forces finite differences.

The simulation settings page shows a corresponding notice, e.g. "AI components present; automatically switched to finite differences".

Simulating Models with AI Components

After wiring and parameter setup, switch to the Simulation panel, configure the Duration, solver, and other settings, then click Run Simulation. The backend runs these steps:

  1. Detects every aiFileId referenced in the model;
  2. Injects using ONNXRunTime and the ONNX file path dictionary into the simulation sandbox;
  3. Loads each AI component's wrapper file in turn;
  4. Compiles and solves the hybrid model;
  5. Extracts the result variables and returns them to the frontend.

After a successful simulation, the results panel shows the AI component's inputs and outputs, plus the lag-filter state variable z in lag filter mode; you can plot and export them together with the physics model variables.

Code View Round-Trip

Models with AI components support two-way sync between the diagram view and the Code View. CodeGen adds the following markers to the generated code:

# @comodel:ai aiFileIds="42"
# @comodel:node id="inst_abc" ... componentName="LinearRegressor" source="ai" aiFileId=42
@named ai1 = LinearRegressor()

When you switch back to the diagram view, CodeParser reads these markers and fully restores the AI node's position, ports, connections, and parameters.

Common Questions

Q: Can AI components be mixed with FMU components?

Yes. CoModel supports MTK, FMU, and AI components in hybrid simulation within one model. When a model contains both FMU and AI components, the solver applies the black-box constraints uniformly.

Q: Why can't an AI component connect directly to another AI component's input?

Both ends are input ports—signal sinks with no source—so the system blocks the connection. Make sure every input port is connected to an output port.

Q: Simulation fails with "AI model failed to load"—what now?

Check that the corresponding ONNX file still exists and was not deleted by mistake. If the file is corrupted or missing, try re-uploading or reparsing.

Next Steps