Operations
FMU ¶
Unpack and repack facilities for FMU archives.
Extracts an FMU (.fmu zip archive) into a temporary directory so that operations can be applied to its modelDescription.xml descriptor. After manipulation, the FMU can be repacked into a new archive.
The temporary directory is removed by close(), at the end of a with block, or at the latest when the object is garbage-collected or the interpreter exits:
with FMU("module.fmu") as fmu:
fmu.apply_operation(OperationStripTopLevel())
fmu.repack("module-stripped.fmu")
Attributes:
| Name | Type | Description |
|---|---|---|
FMI2_TYPES | tuple[str, ...] | FMI-2 scalar variable type names. |
FMI3_TYPES | tuple[str, ...] | FMI-3 variable type names. |
fmu_filename | str | Path to the original |
tmp_directory | str | Path to the temporary extraction directory. |
fmi_version | int | None | Detected FMI version ( |
descriptor_filename | str | Path to the extracted |
Raises:
| Type | Description |
|---|---|
FMUError | If the file does not exist or is not a valid FMU. |
Source code in fmu_manipulation_toolbox/operations.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
model_description property ¶
The descriptor tree, parsed once and shared by the successive operations.
It is parsed again if modelDescription.xml was changed on disk by other means (different modification time or size).
Raises:
| Type | Description |
|---|---|
FMUError | If the descriptor cannot be read. |
apply_operation(operation, apply_on=None, *, types=None, variabilities=None) ¶
Apply an operation to the FMU's modelDescription.xml.
Invokes the operation's callbacks on the descriptor tree (parsed once per FMU), and writes back the descriptor unless the operation is read_only. The filters combine: a port is processed only if every filter set accepts it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation | OperationAbstract | The operation to apply. | required |
apply_on | list[str] | None | If set, only apply the operation to ports with a causality in this list. | None |
types | list[str] | None | If set, only apply the operation to ports of these types: the exact type ( | None |
variabilities | list[str] | None | If set, only apply the operation to ports with a variability in this list. A port without | None |
Example
Source code in fmu_manipulation_toolbox/operations.py
close() ¶
repack(filename) ¶
Repack the (possibly modified) FMU into a new .fmu archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | str | Output path for the repacked FMU. | required |
Source code in fmu_manipulation_toolbox/operations.py
save_descriptor(filename) ¶
Save a copy of the current modelDescription.xml to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | str | Destination path for the descriptor copy. | required |
Source code in fmu_manipulation_toolbox/operations.py
FMUError ¶
Bases: Exception
Exception raised for FMU-related errors.
Attributes:
| Name | Type | Description |
|---|---|---|
reason | str | Human-readable description of the error. |
Source code in fmu_manipulation_toolbox/operations.py
Manipulation ¶
Applies an operation to modelDescription.xml, on its ElementTree.
Loads the descriptor as a ModelDescription, invokes the operation's callbacks in document order, modifies the tree in place and writes it back. Everything the operation does not touch is kept as is (annotations, aliases, comments, namespaces...).
When ports are removed, the references to them are updated: FMI-2 <ModelStructure> indexes and derivative attributes are renumbered, entries and dependencies of <ModelStructure> that refer to removed ports are dropped.
The operation is refused (OperationError, descriptor left unchanged) when it would produce an FMU that breaks the FMI standard:
- every variable removed (empty
<ModelVariables>, forbidden by the XSD); - a removed variable still referenced by a kept one (
derivative,previous,clocks,<Dimension valueReference>); - several variables (or FMI-3 aliases) with the same name.
Attributes:
| Name | Type | Description |
|---|---|---|
operation | OperationAbstract | The operation being applied. |
fmu | FMU | The FMU being manipulated. |
model_description | ModelDescription | None | The descriptor tree, once loaded. |
port_translation | list[int | None] | Maps original port indices (0-based) to new 1-based indices, or |
port_names_list | list[str | None] | Original names of all encountered ports. |
port_removed_vr | set[str | None] | Value references of removed ports. |
Source code in fmu_manipulation_toolbox/operations.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | |
all_names(md) staticmethod ¶
Names of variables and FMI-3 aliases, which must all be unique.
Source code in fmu_manipulation_toolbox/operations.py
broken_references(md, variables, removed) staticmethod ¶
References from kept variables to removed ones, as human-readable strings.
Source code in fmu_manipulation_toolbox/operations.py
check_result(md, variables, removed, names_before) ¶
Refuse a result that breaks the FMI standard (only for what the operation changed).
Source code in fmu_manipulation_toolbox/operations.py
dependency_pairs(attrs) staticmethod ¶
(dependency, dependencyKind) pairs; the kind is None without dependenciesKind.
Source code in fmu_manipulation_toolbox/operations.py
handle_ports(md) ¶
Call port_attrs on every port. Returns the elements to remove.
Source code in fmu_manipulation_toolbox/operations.py
handle_structure(section, attrs) ¶
FMI-3: filter a <ModelStructure> entry. Raises ManipulationSkipTag if its port is removed.
Source code in fmu_manipulation_toolbox/operations.py
in_scope(port) ¶
Whether the operation applies to this port: every filter set must accept it.
Source code in fmu_manipulation_toolbox/operations.py
manipulate(descriptor_filename, apply_on=None, *, types=None, variabilities=None) ¶
Apply the operation and rewrite the descriptor file (unless the operation is read_only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
descriptor_filename | str | Path to the | required |
apply_on | list[str] | None | If set, only process ports with a causality in this list. | None |
types | list[str] | None | If set, only process ports of these types (see ModelVariable.has_type). | None |
variabilities | list[str] | None | If set, only process ports with a variability in this list (the default of the standard when not declared). | None |
Raises:
| Type | Description |
|---|---|
FMUError | If the descriptor cannot be loaded. |
OperationError | If the result would break the FMI standard. The descriptor file is then left unchanged. |
Source code in fmu_manipulation_toolbox/operations.py
remove_children(parent, children) staticmethod ¶
Remove several children at once (Element.remove is linear: one call per child is quadratic).
Source code in fmu_manipulation_toolbox/operations.py
renumber_derivatives(md) ¶
FMI-2: derivative is the 1-based index of the state variable.
Source code in fmu_manipulation_toolbox/operations.py
set_dependencies(attrs, kept) staticmethod ¶
Write back filtered dependencies (and dependenciesKind), or drop them if empty.
Source code in fmu_manipulation_toolbox/operations.py
unknown_attrs(attrs) ¶
FMI-2: renumber an <Unknown> entry. Raises ManipulationSkipTag if its port is removed.
Source code in fmu_manipulation_toolbox/operations.py
ManipulationSkipTag ¶
Bases: Exception
Internal exception used to skip XML content until a matching closing tag.
Raised during SAX parsing to signal that the current element and all its children should be omitted from the output.
Source code in fmu_manipulation_toolbox/operations.py
OperationAbstract ¶
Base class for all FMU manipulation operations.
Subclass this to implement custom operations on FMU descriptors. The methods act as callbacks invoked, in document order, while modelDescription.xml is walked: fmi_attrs, then cosimulation_attrs, modelexchange_attrs and experiment_attrs, then port_attrs for each variable, then model_structure_attrs, and finally closure.
The attrs dictionaries given to the callbacks are those of the descriptor tree: changing them changes the descriptor.
Attributes:
| Name | Type | Description |
|---|---|---|
fmu | FMU | None | The FMU being processed, set via |
model_description | ModelDescription | None | The whole descriptor tree, set before the first callback. Use it for changes that the callbacks cannot express (e.g. removing an element). |
read_only | bool | Set to |
Source code in fmu_manipulation_toolbox/operations.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 | |
closure() ¶
Called after the descriptor has been fully parsed.
Override this for post-processing or cleanup.
cosimulation_attrs(attrs) ¶
Called when the <CoSimulation> element is encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs | dict[str, str] | XML attributes of the co-simulation element. | required |
experiment_attrs(attrs) ¶
Called when the <DefaultExperiment> element is encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs | dict[str, str] | XML attributes of the default experiment. | required |
fmi_attrs(attrs) ¶
Called when the <fmiModelDescription> element is encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs | dict[str, str] | XML attributes of the root element. | required |
model_structure_attrs(section, attrs) ¶
Called for each entry of the <ModelStructure> section.
For FMI-2, section is the name of the enclosing sub-section ("Outputs", "Derivatives", "InitialUnknowns") and attrs holds the attributes of an <Unknown> element (1-based index of the variable).
For FMI-3, section is the name of the element itself ("Output", "ContinuousStateDerivative", "InitialUnknown", "EventIndicator", "ClockedState") and attrs holds its attributes (including valueReference).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
section | str | Name of the model-structure section or element. | required |
attrs | dict[str, str] | XML attributes of the entry. | required |
Source code in fmu_manipulation_toolbox/operations.py
modelexchange_attrs(attrs) ¶
Called when the <ModelExchange> element is encountered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attrs | dict[str, str] | XML attributes of the model-exchange element. | required |
port_attrs(fmu_port) ¶
Called for each port (variable) in the descriptor.
Override this to inspect or modify port attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmu_port | ModelVariable | The port being processed: a view on the variable of the descriptor tree (see ModelVariable). Attributes can be modified in place. | required |
Returns:
| Name | Type | Description |
|---|---|---|
int | int |
|
Source code in fmu_manipulation_toolbox/operations.py
OperationError ¶
Bases: Exception
Exception raised for operation-related errors.
Attributes:
| Name | Type | Description |
|---|---|---|
reason | str | Human-readable description of the error. |
Source code in fmu_manipulation_toolbox/operations.py
OperationKeepOnlyRegexp ¶
Bases: OperationAbstract
Keep only ports whose names match a regular expression; remove all others.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex_string | str | Regular expression pattern. Only ports with names matching this pattern (from the start) are kept. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
regex_string | str | The original regex pattern string. |
regex | Pattern | Compiled regular expression. |
Source code in fmu_manipulation_toolbox/operations.py
OperationMergeTopLevel ¶
Bases: OperationAbstract
Merge the top-level bus prefix into port names by replacing "." with "_".
Only the first dot is replaced. For example, "Bus1.signal_name" becomes "Bus1_signal_name".
Source code in fmu_manipulation_toolbox/operations.py
OperationRemoveRegexp ¶
Bases: OperationAbstract
Remove all ports whose names match a regular expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regex_string | str | Regular expression pattern. Ports with names matching this pattern (from the start) are removed. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
regex_string | str | The original regex pattern string. |
regex | Pattern | Compiled regular expression. |
Source code in fmu_manipulation_toolbox/operations.py
OperationRemoveSources ¶
Bases: OperationAbstract
Remove the sources/ directory from the FMU.
Strips the embedded C/C++ source files that some FMUs include for recompilation on the target platform, and the <SourceFiles> elements (FMI-2) that list them. For FMI-3, the list of sources is sources/buildDescription.xml, removed with the directory.
Source code in fmu_manipulation_toolbox/operations.py
OperationRename ¶
Bases: OperationAbstract
Rename or remove ports according to a mapping of names.
If the new name is empty, the port is removed. Ports not in the mapping are kept unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
translations | dict[str, str] | Mapping from original to new names. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
translations | dict[str, str] | Mapping from original to new names. |
found | set[str] | Original names of the mapping met in the FMU (among the ports in scope), once the operation is applied. |
Example
Source code in fmu_manipulation_toolbox/operations.py
OperationRenameFromCSV ¶
Bases: OperationRename
Rename or remove ports according to a CSV mapping file.
Reads a semicolon-delimited CSV where column 0 is the original name and column 1 is the new name. If the new name is empty, the port is removed. Ports not listed in the CSV are kept unchanged.
Attributes:
| Name | Type | Description |
|---|---|---|
csv_filename | str | Path to the CSV mapping file. |
translations | dict[str, str] | Mapping from original to new names. |
Raises:
| Type | Description |
|---|---|
OperationError | If the CSV file is not found or malformed. |
Source code in fmu_manipulation_toolbox/operations.py
OperationSaveNamesToCSV ¶
Bases: OperationAbstract
Export all port names and metadata to a CSV file.
Generates a semicolon-delimited CSV with columns: name, newName, valueReference, causality, variability, scalarType, startValue.
The resulting file can be edited and used with OperationRenameFromCSV to rename or remove ports.
Attributes:
| Name | Type | Description |
|---|---|---|
output_filename | str | Path to the output CSV file. |
Source code in fmu_manipulation_toolbox/operations.py
OperationStripTopLevel ¶
Bases: OperationAbstract
Remove the top-level bus prefix from all port names.
Strips everything before and including the first "." separator. For example, "Bus1.signal_name" becomes "signal_name".
Source code in fmu_manipulation_toolbox/operations.py
OperationSummary ¶
Bases: OperationAbstract
Log a detailed summary of the FMU contents.
Reports FMI properties, co-simulation capabilities, default experiment values, supported platforms, embedded resources, and port counts grouped by causality.
Attributes:
| Name | Type | Description |
|---|---|---|
nb_port_per_causality | dict[str, int] | Count of ports per causality. |
number_of_continuous_states | int | Model-Exchange number of continuous states ( |
number_of_event_indicators | int | Model-Exchange number of event indicators ( |
Source code in fmu_manipulation_toolbox/operations.py
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 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 | |
OperationTrimUntil ¶
Bases: OperationAbstract
Trim port names up to and including a separator string.
For example, with separator "__", the name "prefix__signal" becomes "signal".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
separator | str | The separator string to search for in port names. | required |
Attributes:
| Name | Type | Description |
|---|---|---|
separator | str | The separator string. |