--- name: simulate-modelica description: "Simulate Modelica models (.mo files) with WSMKernelX — compiles to C++, builds, and runs the simulation (no plotting; to plot the results too, use simulate-and-plot-modelica). Use this skill whenever the user asks to simulate a Modelica model, run a simulation, or get simulation results or time-domain behavior. Triggers on phrases like 'simulate this model', 'run the simulation', 'get simulation results'. Use it also when running a model is a step in a larger job you took on yourself — verifying a port, a datasheet claim or a hand-written implementation against a model built for the purpose — even if the request never mentioned Modelica. If the request also involves analysis — limits/requirements, violations, parameter sweeps, Monte Carlo, calibration — prefer wolfram-language-modelica when Wolfram Language is available." --- # Simulate Modelica Model This skill simulates Modelica models (.mo files) with WSMKernelX: it flattens the model, generates C++ code, compiles it, and runs the simulation. It produces a `.mat` results file. ## Before you run anything This skill drives WSMKernelX through the shared launcher `../scripts/wsm_run.py`. **Read [the shared-conventions appendix at the end of this file](#appendix-shared-conventions-for-the-modelica-skills) first** — launcher resolution, the Windows-vs-Unix shell/Python rules, the temp-dir and cleanup conventions, the JSON-array output gotcha, and the MSL 4.x dialect notes that every step below assumes. The launcher works in `_wsm_simulate_temp/` (next to the `.mo` file, or next to a directory-form library) and leaves `simulate.out.json` and the `.mat` there. Tell the user: "Working in temporary directory `_wsm_simulate_temp/`. This will be deleted after simulation." ## Workflow ### 1. Identify the model file and name Identify the `.mo` file and extract the **model name** — see [Appendix → Picking the model name](#picking-the-model-name). For a **directory-form (multi-file) library**, point `--model` at the library folder (not one class file) and pass the full dotted `--name` — see [Appendix → Directory-form (multi-file) libraries](#directory-form-multi-file-libraries). ### 2. Run the simulation ```bash python3 "/wsm_run.py" --mode simulate \ --model "" --name ModelName --timeout 180 ``` **Terser loop:** add `--quiet` to print only a one-line outcome (status + integration time + result file) instead of the full kernel log, and `--report "Vout,x.T"` to print a compact min/max/mean/pp/final table of those variables straight from the result `.mat` — so the common "simulate then inspect a few values" step is a single call. (`--report` runs `mat_summary.py`, which you can also call standalone on any `.mat`.) The launcher auto-detects MSL usage and loads the right version; force it with `--msl yes|no` or `--msl-version 4.1.0`. If the model uses an installed non-MSL library (e.g. `Hydraulic`), add `--load-library ` — see [Appendix → Using non-MSL libraries](#using-non-msl-libraries-hydraulic-and-other-installed-libraries). Timeout: allow up to 180 seconds — compilation and simulation of complex models can take time. Compiling needs a per-OS C++ toolchain (Visual Studio Build Tools / Xcode CLT / gcc) — the launcher finds it; if it can't, see [Appendix → When the install or compiler isn't found](#when-the-install-or-compiler-isnt-found). ### 3. Parse the output WSMKernelX writes structured results to `simulate.out.json` in the temp directory — a JSON *array*; take the first element. Check **`status.flatten`**, **`status.build`**, and **`status.result`** in that order; on success `simulation.resultFile` holds the path to the `.mat` (also printed as a `SimulationResult` record). Field reference: [Appendix → Reading the JSON output](#reading-the-json-output). ### 4. Report results Summarize clearly: - **Pass**: State the simulation completed successfully. Report key stats from the log (integration time, number of events, number of function evaluations). - **Build Fail**: The C++ compilation or linking failed. Show the build errors. - **Flatten Fail**: The model has structural errors. Show the flatten errors. - **`Fatal error: exception ...(_)` / no out.json**: a normal error (assertion, parameter/init, type, lookup) that escaped catching — not a crash. Read the launcher's `=== actual kernel diagnostic ===` block, which prints the recovered message; don't assume a compiler bug. **A clean "Pass" does not mean the result is correct.** A model can compile and simulate with zero errors while being physically wrong (mis-biased circuit with ~0 output, a state pinned at a saturation limit, a node that silently went NaN). After a successful run, sanity-check the trajectories: ```bash python3 "/check_sanity.py" "/.mat" ``` It flags NaN/Inf, variables that never move, and signals that swung then flatlined (possible saturation / mis-bias). Treat the flags as prompts to inspect operating points, not as failures. (It self-provisions DyMat/numpy.) When you simulate a system to settle, get the **operating point** — a domain-neutral steady-state report that works for thermal, hydraulic, mechanical, electrical, ... models. It checks each state's derivative to report whether the run reached equilibrium (and lists states still drifting), then prints the settled values: ```bash python3 "/op_report.py" "/.mat" --vars tank.T pump.dp ``` Domain add-ons fire automatically when applicable — e.g. for circuits it also classifies BJT regions (ACTIVE / SATURATED / CUTOFF from `Vbe`/`Vbc`), catching mis-bias that no error would report. (Self-provisions DyMat/numpy.) ### 4b. Parameter studies WITHOUT recompiling (`--override` / `--sweep`) To try different parameter values, do **not** rebuild or hand-write wrapper models. The launcher builds once and re-runs the compiled executable per value by editing the `.sim` init file's `value=` attribute: ```bash # one value set python3 "/wsm_run.py" --mode simulate --model M.mo --name Pkg.M \ --override "kfb=0.04,I0=45e-6" # sweep one parameter (one .mat per value), holding others via --override python3 "/wsm_run.py" --mode simulate --model M.mo --name Pkg.M \ --sweep "I0=12e-6,32.5e-6,90e-6" ``` Each run writes `run_