The Cosimulation Master¶
Module for managing and executing co-simulations involving multiple FMUs.
This module provides a Master class that handles FMU initialization, input setting,
stepping, and result collection during simulation.
Master ¶
Master(
fmu_config_list,
connections,
sequence_order,
cosim_method="jacobi",
iterative=False,
fixed_point=False,
fixed_point_kwargs=None,
)
Manages and executes the co-simulation involving multiple FMUs.
| ATTRIBUTE | DESCRIPTION |
|---|---|
fmu_config_list |
A list of dictionaries containing information about the FMUs to be used in the simulation.
TYPE:
|
connections |
A dictionary of connections between FMUs. The keys are tuples (source_fmu, source_variable), and the values are dictionaries with information about the source and target FMUs and variables.
TYPE:
|
sequence_order |
The order in which FMUs should be executed.
TYPE:
|
cosim_method |
The method used to solve algebraic loops in the simulation.
TYPE:
|
current_time |
The current simulation time.
TYPE:
|
fixed_point |
Whether to use the fixed-point initialization method.
TYPE:
|
fixed_point_kwargs |
Keyword arguments for the fixed-point initialization method.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
sanity_check |
Checks FMU compatibility, I/Os, and headers with the corresponding algorithm. |
set_inputs |
Sets the input values for the current simulation step using the provided input dictionary. |
init_simulation |
Initializes the simulation environment and FMUs. |
get_outputs |
Returns the output dictionary for the current step. |
get_results |
Returns the results of the simulation. |
solve_loop |
Uses the defined algorithm to solve algebraic loops in the simulation. |
do_step |
Performs a single step of the simulation, updating inputs, executing FMUs, and propagating outputs. |
Initializes the Master class with FMU configurations, connection details, sequence order, and loop solver.
| PARAMETER | DESCRIPTION |
|---|---|
fmu_config_list
|
List of dictionaries with FMU configurations.
TYPE:
|
connections
|
Dictionary mapping connections between FMUs.
TYPE:
|
sequence_order
|
Execution order of FMUs.
TYPE:
|
cosim_method
|
Strategy for coordinating FMUs in co-simulation. Options are "jacobi" and "gauss-seidel". Defaults to "jacobi".
TYPE:
|
iterative
|
Whether to solve algebraic loops iteratively. Defaults to False.
TYPE:
|
fixed_point
|
whether to use the fixed-point initialization method.
TYPE:
|
fixed_point_kwargs
|
keyword arguments for the fixed point
TYPE:
|
Source code in cofmpy/master.py
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
apply_fmu_outputs_to_inputs ¶
apply_fmu_outputs_to_inputs(fmu_id, out_fmu)
Performs a copy of output values into input dict. The copy is based on connections between given fmu/outputs and inpout dict for each FMU.
| PARAMETER | DESCRIPTION |
|---|---|
out_fmu
|
A dictionary containing the output values for the current step on a given fmu, identified by fmu_id
TYPE:
|
fmu_id
|
A String identifying FMU into system. Used to find connections with outputs
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
No return, at the end of the method, self._input_dict is fill with updated values. |
Source code in cofmpy/master.py
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |
do_fixed_point_step ¶
do_fixed_point_step(step_size, input_dict=None)
This method updates the input dictionary with the values from the provided input dictionary, performs a single step of the simulation on each FMU, using the default jacobi method, propagates the output values to the corresponding variables for the next step, and updates the current simulation time accordingly. It also stores the output values in the results dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
step_size
|
The size of the simulation step.
TYPE:
|
input_dict
|
A dictionary containing input values for the simulation. Defaults to None.
TYPE:
|
record_outputs
|
Whether to store the output values in the results dictionary. Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing the output values for this step, structured as
|
Source code in cofmpy/master.py
520 521 522 523 524 525 526 527 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 | |
do_step ¶
do_step(step_size, input_dict=None, record_outputs=True)
This method updates the input dictionary with the values from the provided input dictionary, performs a single step of the simulation on each FMU, using the solve_loop method, propagates the output values to the corresponding variables for the next step, and updates the current simulation time accordingly. It also stores the output values in the results dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
step_size
|
The size of the simulation step.
TYPE:
|
input_dict
|
A dictionary containing input values for the simulation. Defaults to None.
TYPE:
|
record_outputs
|
Whether to store the output values in the results dictionary. Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing the output values for this step, structured as
TYPE:
|
Source code in cofmpy/master.py
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 | |
get_input_dict ¶
get_input_dict()
Returns the input dictionary for the current step.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing the input values for the current step,
structured as
TYPE:
|
Source code in cofmpy/master.py
350 351 352 353 354 355 356 357 358 | |
get_outputs ¶
get_outputs()
Returns the output dictionary for the current step.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing the output values of the current step,
structured as
TYPE:
|
Source code in cofmpy/master.py
408 409 410 411 412 413 414 415 416 | |
get_residual ¶
get_residual(input_dict, output_dict)
Performs check between outputs and connected inputs and return a list of residuals The check is based on connections between given fmu/outputs and inpout dict for each FMU.
| PARAMETER | DESCRIPTION |
|---|---|
output_dict
|
A dictionary containing the output values for the current step
TYPE:
|
input_dict
|
Input dict concerned by the check, transient dict with current calculated values
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
residuals
|
A list of residuals between inputs and outputs (1 for each connection) |
Source code in cofmpy/master.py
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 | |
get_results ¶
get_results()
Returns the results of the simulation, this includes the values of every output variables, for each step, up until the current time of simulation.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionnary containing output values of every step, structured as [(FMU_ID, Var)]
TYPE:
|
Source code in cofmpy/master.py
418 419 420 421 422 423 424 425 426 427 428 | |
init_simulation ¶
init_simulation(input_dict=None)
Initializes the simulation environment and FMUs.
This method sets up the necessary dictionaries for the simulation and initializes the FMUs with either a fixed point algorithm or values provided in the input dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
input_dict
|
A dictionary containing input values for the simulation. Defaults to None.
TYPE:
|
The method performs the following steps
- Sets the current simulation time to 0.
- If fixed_point is True, calls the _fixed_point_init() method.
- Otherwise, sets the inputs using the provided input_dict and initializes each FMU with these values.
Note: The FMUs are reset after setting the initial values.
Source code in cofmpy/master.py
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 | |
initialize_values_from_config ¶
initialize_values_from_config()
Initializes the FMU variables (inputs/outputs/parameters) with the values provided in the configuration dict.
If the variable is an input, it is also added to the input dictionary
Source code in cofmpy/master.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
sanity_check ¶
sanity_check()
Checks the compatibility of FMUs, including input/output validation and algorithm compliance.
Source code in cofmpy/master.py
212 213 214 215 216 217 | |
set_inputs ¶
set_inputs(input_dict=None)
Sets the input values for the current simulation step.
This method populates the internal input dictionary (self._input_dict) with
values for the current step. It updates these values with those provided in the
input_dict parameter, if given. The input_dict parameter is expected to be
a dictionary of dictionaries, where each key is an FMU identifier and each value
is another dictionary mapping variable names to their respective values (e.g.,
{"FMU1": {"var1": value}, "FMU2": {"var2": val, "var3": val}}).
| PARAMETER | DESCRIPTION |
|---|---|
input_dict
|
A dictionary of dictionaries containing input values to override the initialization values. Defaults to None.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the current simulation time ( |
Source code in cofmpy/master.py
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 | |
solve_loop ¶
solve_loop(
fmu_ids, step_size, algo="jacobi", iterative=False
)
Performs a single simulation step on the given FMUs, using the defined algorithm to solve algebraic loops in the simulation.
In the case there is no loop, the function will propagate the output values and return them.
| PARAMETER | DESCRIPTION |
|---|---|
fmu_ids
|
List of highly coupled FMUs. Contains only one FMU if there is no loop.
TYPE:
|
step_size
|
The step size for data exchange (in cosimulation mode, FMU integration step is fixed).
TYPE:
|
algo
|
The algorithm to use to solve the loop (default: "jacobi").
TYPE:
|
iterative
|
Whether iterative method requested to solve the loop.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing the output values for this step of the FMUs
given, structured as
TYPE:
|
Source code in cofmpy/master.py
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 518 | |
update_connected_inputs ¶
update_connected_inputs(fmu_id, output_name, value)
Performs a copy of output value into input dict. The copy is based on connections between given fmu/output name and inpout dict for each connected FMU.
| PARAMETER | DESCRIPTION |
|---|---|
fmu_id
|
A String identifying FMU into system. Used to find connections between inputs and output
TYPE:
|
output_name
|
A string that identifies name of the output. Used to find connections with inputs
TYPE:
|
value
|
the value to copy to inputs
|
| RETURNS | DESCRIPTION |
|---|---|
|
No return, at the end of the method, self._input_dict is fill with updated value. |
Source code in cofmpy/master.py
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | |