A C# implementation of the Dymola interface based on the JavaScript interface provided in Dymola 2025x Refresh 1.
This library provides a .NET API for communicating with Dymola via its HTTP JSON-RPC interface. It allows you to:
- Start and manage Dymola processes
- Execute Modelica commands
- Simulate models
- Check and translate models
- Generate plots
- Open and manage libraries
- And more...
- Dymola 2025x Refresh 1 or compatible version
- .NET 10.0 or later
- Dymola must be started with the
-serverportcommand-line option (default: 8082)
The factory pattern manages a singleton DymolaInterface instance configured from application settings:
using DymolaInterface;
// Inject the factory (registered as IDymolaInterfaceFactory in DI)
var dymola = await dymolaFactory.GetOrCreateAsync();
// Check connection status
bool connected = dymolaFactory.IsConnected;
// Reset when settings change (forces recreation on next GetOrCreateAsync)
await dymolaFactory.ResetAsync();See FACTORY_USAGE.md for detailed factory documentation.
using DymolaInterface;
// Option 1: Connect to an already running Dymola instance
var dymola = new DymolaInterface();
// Option 2: Start Dymola programmatically
var dymola = new DymolaInterface(
dymolaPath: @"C:\Program Files\Dymola 2025x Refresh 1\bin64\Dymola.exe",
portNumber: 8082,
hostname: "127.0.0.1"
);
await dymola.StartDymolaProcessAsync();
try
{
// Simulate a model
var result = await dymola.SimulateModelAsync(
"Modelica.Mechanics.Rotational.Examples.CoupledClutches"
);
if (result)
{
Console.WriteLine("Simulation successful!");
// Plot results
result = await dymola.PlotAsync(new[] { "J1.w", "J2.w", "J3.w", "J4.w" });
if (result)
{
// Export plot as image
result = await dymola.ExportPlotAsImageAsync(@"C:\temp\plot.png");
}
}
else
{
// Get error details
var errorLog = await dymola.GetLastErrorLogAsync();
Console.Error.WriteLine($"Simulation failed: {errorLog}");
}
}
finally
{
// Clean up
dymola.Dispose();
}var dymola = new DymolaInterface();
// Check model without simulating
var success = await dymola.CheckModelAsync("MyPackage.MyModel");
if (!success)
{
var error = await dymola.GetLastErrorAsync();
Console.WriteLine($"Check failed: {error}");
}var dymola = new DymolaInterface();
// Open a Modelica library
await dymola.OpenModelAsync(@"C:\MyLibrary\package.mo");
// Add library path
await dymola.AddModelicaPathAsync(@"C:\Libraries");var dymola = new DymolaInterface();
// Execute arbitrary Dymola commands
await dymola.ExecuteCommandAsync("Advanced.Define.DAEsolver = true");
await dymola.SetVariableAsync("myVariable", 42.0);StartDymolaProcessAsync()- Start Dymola processStopDymolaProcess()- Stop Dymola processIsOfflineMode()- Check if in offline modeSetOfflineMode(bool)- Enable/disable offline mode
CheckModelAsync(problem, simulate, constraint)- Check a modelSimulateModelAsync(problem, startTime, stopTime, ...)- Simulate a modelTranslateModelAsync(problem)- Translate (compile) a modelOpenModelAsync(path, mustRead, changeDirectory)- Open a library/package
PlotAsync(y, legends, plotInAll, colors, patterns, markers, thicknesses, axes)- Create plotsExportPlotAsImageAsync(fileName, id, includeInLog, onlyActiveSubplot)- Export plot as image
GetLastErrorAsync()- Get last error messageGetLastErrorLogAsync()- Get detailed error log
ExecuteCommandAsync(cmd)- Execute arbitrary Dymola commandSetVariableAsync(name, value)- Set a Dymola variableAddModelicaPathAsync(path, erase)- Add to Modelica library pathCdAsync(dir)- Change working directoryClearAsync(fast)- Clear Dymola workspaceSaveLogAsync(logfile)- Save log to fileGetLastResultFileNameAsync()- Get result file nameDymolaVersion()- Get Dymola version stringDymolaVersionNumber()- Get Dymola version number
- Default, None, Solid, Dash, Dot, DashDot, DashDotDot
- Default, None, Cross, Circle, Square, FilledCircle, FilledSquare, TriangleDown, TriangleUp, Diamond, Dot, SmallSquare, Point, BarChart, AreaFill
- Bold, Italic, UnderLine
- Left, Center, Right
- Min, Max, ArithmeticMean, RectifiedMean, RMS, ACCoupledRMS, SlewRate, THD, FirstHarmonic
The interface uses HTTP JSON-RPC to communicate with Dymola's built-in server. Each command is sent as a JSON request:
{
"method": "simulateModel",
"params": ["MyModel", 0.0, 1.0, 0, 0.0, "Dassl", 0.0001, 0.0, "dsres"],
"id": 1
}And receives a JSON response:
{
"result": true,
"error": null,
"id": 1
}- Based on the JavaScript interface in
Dymola 2025x Refresh 1\Modelica\Library\javascript_interface\dymola_interface.js - Uses
HttpClientfor JSON-RPC communication - All methods are async for non-blocking operations
- Implements
IDisposablefor proper resource cleanup - Automatically checks Dymola version compatibility on connection
- Supports both connecting to existing Dymola instances and starting new ones
- Boolean result handling: Dymola returns different result types for different commands:
- Some commands return
trueorfalse - Others return an empty object
{}to indicate success (e.g.,AddModelicaPath,cd) - The interface automatically handles both cases and treats empty objects as success
- Some commands return
The interface provides multiple ways to handle errors:
- Return values: Most methods return
boolindicating success/failure - Exception handling: Connection and communication errors throw exceptions
- Error messages: Use
GetLastErrorAsync()andGetLastErrorLogAsync()to retrieve detailed error information
The HttpClient is thread-safe, but the Dymola instance itself may not support concurrent operations. It's recommended to use the interface from a single thread or implement proper synchronization.
- Microsoft.Extensions.DependencyInjection (v10.0.2) - DI container for factory pattern
MIT License — see LICENSE for details.
This C# implementation is based on the JavaScript interface distributed with Dymola 2025x Refresh 1
(Modelica\Library\javascript_interface\dymola_interface.js), copyright (c) 2013-2025 Dassault Systèmes.
Using this library requires a valid Dymola license.