@@ -66,13 +66,13 @@ def set_annotations(model, annotations):
6666 with open (model_path , 'w' ) as file :
6767 file .write (modified_code )
6868
69- def set_constant (model , variable_name , new_value ):
69+ def set_value (model , variable_name , new_value , token , token_mod ):
7070 """
71- Change the value of a constant Integer variable in a Modelica model.
71+ Change the value of a constant or parameter in a Modelica model.
7272
7373 :param model: Path to the Modelica file to modify.
74- :param variable_name: The name of the constant Integer variable to change.
75- :param new_value: The new value to set for the constant Integer variable .
74+ :param variable_name: The name of the constant or parameter to change.
75+ :param new_value: The new value to set for the constant or parameter .
7676 """
7777
7878 model_path = fh .get_full_path (model )
@@ -81,11 +81,11 @@ def set_constant(model, variable_name, new_value):
8181 with open (model_path , 'r' ) as file :
8282 modelica_code = file .read ()
8383
84- # Regular expression to find the constant Integer variable
85- pattern = rf'constant \s+Integer \s+{ variable_name } \s*=\s*\d+;'
84+ # Regular expression to find the token
85+ pattern = rf'{ token } \s+{ token_mod } \s+{ variable_name } \s*=\s*\d+;'
8686
8787 # Create the new declaration with the new value
88- new_declaration = f'constant Integer { variable_name } = { new_value } ;'
88+ new_declaration = f'{ token } { token_mod } { variable_name } = { new_value } ;'
8989
9090 # Replace the old declaration with the new one
9191 modified_code = re .sub (pattern , new_declaration , modelica_code )
@@ -94,9 +94,20 @@ def set_constant(model, variable_name, new_value):
9494 with open (model_path , 'w' ) as file :
9595 file .write (modified_code )
9696
97+
98+ def set_constant (model , variable_name , new_value ):
99+ """
100+ Change the value of a constant Integer in a Modelica model.
101+
102+ :param model: Path to the Modelica file to modify.
103+ :param variable_name: The name of the constant Integer to change.
104+ :param new_value: The new value to set for the constant Integer.
105+ """
106+ set_value (model , variable_name , new_value , 'constant' , 'Integer' )
107+
97108def set_constants (model , constants ):
98109 """
99- Change the value of a constant Integer variable from the given dictionary.
110+ Change the value of a constant Integer from the given dictionary.
100111
101112 :param model: Path to the Modelica file to modify.
102113 :param constants: Dictionary containing the Modelica constants.
@@ -105,35 +116,79 @@ def set_constants(model, constants):
105116 set_constant (model , key , value )
106117
107118
108- def constants (model ):
119+ def values (model , token , token_mod ):
109120 """
110- Read all constant Integer variables from a Modelica model.
121+ Read all constant or parameters from a Modelica model.
111122
112123 :param model: Path to the Modelica file to read.
113- :return: A dictionary with variable names as keys and their values as integers .
124+ :return: A dictionary with token names as keys and their values.
114125 """
115126 model_path = fh .get_full_path (model )
116127
117- # Initialize an empty dictionary to store the constant Integer variables
118- constant_integers = {}
128+ # Initialize an empty dictionary to store the token values
129+ token_values = {}
119130
120131 # Read the existing content of the Modelica file
121132 with open (model_path , 'r' ) as file :
122133 modelica_code = file .read ()
123134
124- # Regular expression to find constant Integer declarations
125- pattern = r'constant \s+Integer \s+(\w+)\s*=\s*(\d+);'
135+ # Regular expression to find token declarations
136+ pattern = rf' { token } \s+{ token_mod } \s+(\w+)\s*=\s*(\d+);'
126137
127138 # Find all matches in the Modelica code
128139 matches = re .findall (pattern , modelica_code )
129140
130- # Populate the dictionary with variable names and their integer values
131- for variable_name , value in matches :
132- constant_integers [variable_name ] = int (value )
141+ # Populate the dictionary with token names and their values
142+ for token_name , value in matches :
143+ tr_value = value
144+ if token_mod == "Integer" :
145+ tr_value = int (value )
146+ elif token_mod == "Real" :
147+ tr_value = float (value )
148+ token_values [token_name ] = tr_value
149+
150+ return token_values
133151
134- return constant_integers
152+
153+ def constants (model ):
154+ """
155+ Read all constant Integer variables from a Modelica model.
156+
157+ :param model: Path to the Modelica file to read.
158+ :return: A dictionary with variable names as keys and their values as integers.
159+ """
160+ return values (model , 'constant' , 'Integer' )
135161
136162def parameters (model ):
163+ """
164+ Read all constant Integer variables from a Modelica model.
165+
166+ :param model: Path to the Modelica file to read.
167+ :return: A dictionary with variable names as keys and their values as integers.
168+ """
169+ return values (model , 'parameter' , 'Real' )
170+
171+ def set_parameter (model , variable_name , new_value ):
172+ """
173+ Change the value of a parameter in a Modelica model.
174+
175+ :param model: Path to the Modelica file to modify.
176+ :param variable_name: The name of the parameter to change.
177+ :param new_value: The new value to set for the parameter.
178+ """
179+ set_value (model , variable_name , new_value , 'parameter' , 'Real' )
180+
181+ def set_parameters (model , params ):
182+ """
183+ Change the value of a parameter from the given dictionary.
184+
185+ :param model: Path to the Modelica file to modify.
186+ :param constants: Dictionary containing the Modelica parameters.
187+ """
188+ for key , value in params .items ():
189+ set_parameter (model , key , value )
190+
191+ def json_parameters (model ):
137192 """
138193 Read a JSON file containing a list of parameter records with values that are either
139194 a double or a list of doubles.
@@ -170,7 +225,7 @@ def parameters(model):
170225
171226 return records
172227
173- def set_parameters (model , parameters ):
228+ def set_json_parameters (model , parameters ):
174229 """
175230 Generate a JSON parameters file with values that are either a double or a list of doubles.
176231
0 commit comments