22import shutil
33import tempfile
44import warnings
5- from pathlib import Path
65from contextlib import contextmanager
6+ from pathlib import Path
77
88import fmpy
9- from fmpy import simulate_fmu
109import pandas as pd
10+ from fmpy import simulate_fmu
1111from sklearn .pipeline import Pipeline
1212
1313from corrai .base .model import Model
1414
15+ MODEL_VARIABLES_TYPES_MAP = {
16+ "Real" : float ,
17+ "Float32" : float ,
18+ "Float64" : float ,
19+ "Integer" : int ,
20+ "Int8" : int ,
21+ "UInt8" : int ,
22+ "Int16" : int ,
23+ "UInt16" : int ,
24+ "Int32" : int ,
25+ "UInt32" : int ,
26+ "Int64" : int ,
27+ "UInt64" : int ,
28+ "Boolean" : bool ,
29+ "String" : str ,
30+ # Not yet implemented
31+ # ('Enumeration'),
32+ # ('Binary'),
33+ # ('Clock'),
34+ }
35+
1536DEFAULT_SIMULATION_OPTIONS = {
1637 "startTime" : 0 ,
1738 "stopTime" : 24 * 3600 ,
@@ -297,6 +318,26 @@ def __init__(
297318 UserWarning ,
298319 stacklevel = 2 ,
299320 )
321+ self .property_dict = self ._get_init_property_dict ()
322+
323+ def _get_init_property_dict (self ):
324+ model_description = fmpy .read_model_description (self .fmu_path .as_posix ())
325+ fmu_param_dict = {
326+ var .name : var
327+ for var in model_description .modelVariables
328+ if var .causality == "parameter"
329+ }
330+ prop_dict = {}
331+ for name , var in fmu_param_dict .items ():
332+ try :
333+ prop_dict [name ] = MODEL_VARIABLES_TYPES_MAP [var .type ](var .start )
334+ except TypeError :
335+ prop_dict [name ] = var .start
336+
337+ except KeyError :
338+ pass
339+
340+ return prop_dict
300341
301342 def get_property_values (
302343 self , property_list : str | tuple [str , ...] | list [str ]
@@ -323,22 +364,13 @@ def get_property_values(
323364 >>> model.get_property_values(["x.k", "y.k"])
324365 ['2.0', '2.0']
325366 """
326- if isinstance (property_list , str ):
327- property_list = (property_list ,)
367+ property_list = (
368+ [property_list ] if isinstance (property_list , str ) else property_list
369+ )
370+ return [self .property_dict [prop ] for prop in property_list ]
328371
329- model_description = fmpy .read_model_description (self .fmu_path .as_posix ())
330- variable_map = {var .name : var for var in model_description .modelVariables }
331- values = []
332- for prop in property_list :
333- if prop in variable_map :
334- try :
335- val = float (variable_map [prop ].start )
336- except (ValueError , TypeError ):
337- val = variable_map [prop ].start
338- values .append (val )
339- else :
340- values .append (None )
341- return values
372+ def set_property_values (self , property_dict : dict ):
373+ self .property_dict .update (property_dict .items ())
342374
343375 def simulate (
344376 self ,
@@ -440,10 +472,12 @@ def simulate(
440472 2.0 3.0
441473
442474 """
443- property_dict = dict (property_dict or {})
475+
476+ simu_property = self .property_dict .copy ()
477+ simu_property .update (dict (property_dict or {}))
444478
445479 if property_dict and debug_param :
446- print (property_dict )
480+ print (simu_property )
447481
448482 simulation_options = {
449483 ** DEFAULT_SIMULATION_OPTIONS ,
@@ -463,8 +497,8 @@ def simulate(
463497 )
464498
465499 boundary_df = None
466- if property_dict :
467- boundary_df = property_dict .pop ("boundary" , boundary_df )
500+ if simu_property :
501+ boundary_df = simu_property .pop ("boundary" , boundary_df )
468502
469503 if simulation_options :
470504 sim_boundary = simulation_options .pop ("boundary" , boundary_df )
@@ -500,7 +534,7 @@ def simulate(
500534 local_boundary ,
501535 ):
502536 if local_boundary is not None and self .boundary_table_name :
503- property_dict [f"{ self .boundary_table_name } .fileName" ] = (
537+ simu_property [f"{ self .boundary_table_name } .fileName" ] = (
504538 local_boundary .as_posix ()
505539 )
506540
@@ -510,7 +544,7 @@ def simulate(
510544 stop_time = stop_sec ,
511545 step_size = step_sec ,
512546 relative_tolerance = simulation_options ["tolerance" ],
513- start_values = property_dict ,
547+ start_values = simu_property ,
514548 output = self .output_list ,
515549 solver = simulation_options ["solver" ],
516550 output_interval = output_int_sec ,
0 commit comments