API 参考 - 核心模块 (Core)¶
核心模块 (Core)
核心模块 (Core) 包含了 TRICYS 的核心功能,如作业管理、Modelica 交互和事件拦截器。 请在下方的标签页中选择您感兴趣的特定模块。
generate_simulation_jobs(simulation_params)
¶
Generates a list of simulation jobs from parameters, handling sweeps and array expansion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simulation_params
|
Dict[str, Any]
|
Dictionary of simulation parameters. Can include: - "file": Path to CSV file for batch job loading - Parameter names with values (single values, lists, or special format strings) - Array-like parameters in format "{value1, value2, ...}" |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of job dictionaries. Each job contains one combination of parameter |
List[Dict[str, Any]]
|
values for simulation. Single-value parameters are included in all jobs. |
Note
If "file" parameter is present, jobs are loaded from CSV and other parameters are merged into each CSV job. For parameter sweeps, generates all combinations using Cartesian product. Array-like parameters are expanded using 1-based indexing before sweep generation. Returns empty dict list [{}] if no parameters provided.
Source code in tricys/core/jobs.py
parse_parameter_value(value)
¶
Parses a parameter value which can be a single value, a list, or a string with special formats.
Supported special string formats
- "start:stop:step" -> e.g., "1:10:2" for a linear range with step
- "linspace:start:stop:num" -> e.g., "linspace:0:10:5" for 5 evenly spaced points
- "log:start:stop:num" -> e.g., "log:1:1000:4" for 4 points on log scale
- "rand:min:max:count" -> e.g., "rand:0:1:10" for 10 random uniform values
- "file:path:column" -> e.g., "file:data.csv:voltage" to read a CSV column
- "file:path" -> e.g., "file:sampling.csv" to read all parameters from CSV
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
The parameter value to parse. Can be a single value, list, or special format string. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
A list of parsed values. Single values and strings without colons return |
List[Any]
|
a single-element list. Special format strings return expanded value lists. |
Note
Numbers are rounded to 8 decimal places to avoid floating point precision issues. If parsing fails, returns the original value as a single-element list and logs an error. For "file:" format, Windows paths with drive letters (C:) are handled.
Source code in tricys/core/jobs.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
Utilities for interacting with OpenModelica via OMPython.
This module provides a set of functions to manage an OpenModelica session, load models, retrieve parameter details, and format parameter values for simulation.
format_parameter_value(name, value)
¶
Formats a parameter value into a string recognized by OpenModelica.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the parameter. |
required |
value
|
Any
|
The value of the parameter (can be number, string, list, or bool). |
required |
Returns:
| Type | Description |
|---|---|
str
|
A formatted string for use in simulation overrides (e.g., "p=1.0", |
str
|
"name={1,2,3}", or 'path="value"'). |
Note
Lists are formatted as {v1,v2,...}. Strings are quoted with double quotes. Numbers and booleans use direct string conversion.
Source code in tricys/core/modelica.py
get_all_parameters_details(omc, model_name)
¶
Recursively retrieves detailed information for all parameters in a given model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omc
|
OMCSessionZMQ
|
The active OpenModelica session object. |
required |
model_name
|
str
|
The full name of the model. |
required |
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of dictionaries, where each dictionary contains the detailed |
List[Dict[str, Any]]
|
information of a single parameter including name, type, defaultValue, |
List[Dict[str, Any]]
|
comment, and dimensions. |
Note
Uses _recursive_get_parameters() to traverse the model hierarchy. Returns empty list if model is not found or on error. Each parameter dict includes 'name' (hierarchical), 'type', 'defaultValue', 'comment', and 'dimensions' fields.
Source code in tricys/core/modelica.py
get_model_default_parameters(omc, model_name)
¶
Retrieves the default values for all parameters in a given model.
This function leverages get_all_parameters_details to fetch detailed parameter information and then extracts and parses the name and default value into a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omc
|
OMCSessionZMQ
|
The active OpenModelica session object. |
required |
model_name
|
str
|
The full name of the model. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary mapping parameter names to their default values |
Dict[str, Any]
|
(e.g., float, list, bool, str). Returns an empty dictionary if |
Dict[str, Any]
|
the model is not found or has no parameters. |
Note
Values are parsed from OpenModelica string format to Python types using _parse_om_value(). Handles arrays, booleans, strings, and numeric values.
Source code in tricys/core/modelica.py
get_model_parameter_names(omc, model_name)
¶
Parses and returns all subcomponent parameter names for a given model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omc
|
OMCSessionZMQ
|
The active OpenModelica session object. |
required |
model_name
|
str
|
The full name of the model (e.g., 'example.Cycle'). |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
A list of all available parameter names in hierarchical format |
List[str]
|
(e.g., ['blanket.TBR', 'divertor.heatLoad']). |
Note
Only traverses components whose type starts with the package name. Returns empty list if model is not found or has no components. Uses getComponents() and getParameterNames() OMC API calls.
Source code in tricys/core/modelica.py
get_om_session()
¶
Initializes and returns a new OMCSessionZMQ session.
Returns:
| Type | Description |
|---|---|
OMCSessionZMQ
|
An active OpenModelica session object. |
Note
Creates a new ZMQ-based connection to OpenModelica Compiler. Each call creates an independent session that should be properly closed after use.
Source code in tricys/core/modelica.py
load_modelica_package(omc, package_path)
¶
Loads a Modelica package into the OpenModelica session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omc
|
OMCSessionZMQ
|
The active OpenModelica session object. |
required |
package_path
|
str
|
The file path to the Modelica package ( |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the package was loaded successfully, False otherwise. |
Note
Uses sendExpression('loadFile(...)') command. Logs error if loading fails. The package must be a valid Modelica package file.
Source code in tricys/core/modelica.py
integrate_interceptor_model(package_path, model_name, interception_configs)
¶
Integrates CSV data replacement into a system model.
This function supports two modes (all handlers must use the same mode): 1. "interceptor" (default): Creates interceptor models between submodels and system. 2. "replacement": Directly modifies submodels to use CSV data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_path
|
str
|
The file path to the Modelica package. For multi-file packages,
this should be the path to |
required |
model_name
|
str
|
The full name of the system model to be modified. |
required |
interception_configs
|
list[Dict[str, Any]]
|
A list of dictionaries, each defining an interception task. All configs must have the same 'mode' field. Each dict should contain 'submodel_name', 'csv_uri', 'instance_name', 'output_placeholder', and optionally 'mode' (defaults to 'interceptor'). |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing the paths to modified models. Structure varies by mode: - interceptor mode: interceptor_model_paths, system_model_path - replacement mode: replaced_models, system_model_path |
Raises:
| Type | Description |
|---|---|
ValueError
|
If interception_configs is empty or if mixed modes are detected. |
FileNotFoundError
|
If package_path is invalid or package.mo not found. |
Note
Mode is determined from the first config's 'mode' field. All configs must use the same mode or ValueError is raised. Automatically detects single-file vs multi-file package structure and routes to appropriate handler.
Source code in tricys/core/interceptor.py
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 | |
replace_submodels_with_csv(package_path, replacement_configs)
¶
Replaces multiple submodels with CSV data sources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_path
|
str
|
Path to the Modelica package directory or package.mo file. |
required |
replacement_configs
|
list[Dict[str, Any]]
|
A list of dictionaries, each defining a replacement task: - submodel_name: Full name of the submodel (e.g., 'MyPackage.MyModel') - output_ports: List of output port definitions - csv_file: Path to the CSV file |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing: - replaced_models: List of results from each replacement - package_path: Original package path |
Note
Automatically determines if package is directory-based or single-file. Continues processing remaining models if one fails, but re-raises the error after logging. Each submodel file is identified by splitting the full name and locating {ModelName}.mo in the package directory.
Source code in tricys/core/interceptor.py
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 | |