1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2016-2024 EDF R&D
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
20 # Author: Luis Corona Mesa-Molles, luis.corona-mesa-molles@edf.fr, EDF R&D
23 This module leads to easy calibration of a dynamical model from DYMOLA or
24 Modelica, with respect to experimental measurements, with the help of
25 optimization algorithms available in ADAO tool.
30 The most simple use of the module, using default arguments and files living in
31 the current directory, is the following:
33 from modelica_calibration.case import Calibration
35 newcase = Calibration()
36 resultats = newcase.calibrate(
37 DataName = "measures.txt",
38 ModelName = os.path.join("Models","dymosim.exe"),
39 MethodName = "parameters.py",
40 VariablesToCalibrate = ["COP_val","hVE_val","PEE_val","hEE_val"],
41 OutputVariables = ["proeeF.T","Ev.P","prospC.T","Sp.Q","proseF.T"],
44 for k,v in resultats.items():
45 print("%18s = %s"%(k,v))
48 ARGUMENTS TO BE GIVEN AS INPUT FOR "CALIBRATE" TASK
49 ---------------------------------------------------
51 The "calibrate" method is used to set and realize a calibration (optimal
52 estimation task) of model parameters using measures data. It can be directly
53 called with all the input arguments to set the full calibration problem.
55 The input arguments are the following named ones. They are not all required,
56 and can be divided in category.
58 Category: dynamic model description
60 ModelName............: required file (or directory name that contains the
61 files) for the dynamic model.
64 ModelFormat..........: this indicates the format of the dynamic model. It
65 can be given as a Dymola executable pair "dymosim.exe/dsin.txt".
66 (default "GUESS", allowed "DYMOSIM", "PYSIM", "GUESS")
68 Category: measures description
70 DataName.............: required file name for measures
73 DataFormat...........: this indicates the format of the measures, given as
74 columns of chronological series, with the same column names that are
75 used for variables in the output of the model simulation.
76 (default "GUESS", allowed "TXT", "CSV", "TSV", "GUESS")
78 Category: initial/background values description
80 BackgroundName.......: file name for initial/background. If no file name is
81 given, it is assumed that the initial value will come from model.
84 BackgroundFormat.....: this indicates the source of the initial values used
85 as background ones. The background values can be read in ADAO, DSIN or
86 USER format. The priority is "USER", then "ADAO", then "DSIN".
87 (default "GUESS", allowed "ADAO", "DSIN", "USER", "GUESS")
89 Category: calibration method description
91 MethodName...........: file name for ADAO parameters
94 MethodFormat.........: this indicates the format of the assimilation
95 parameters, excluding background.
96 (default "ADAO", allowed "ADAO")
98 VariablesToCalibrate.: model names to calibrate, that have to exist as
99 input variables in the model description.
102 OutputVariables......: model names of result to compare to measures, that
103 have to exist as output variables in the model description.
106 Category: results description
108 ResultName...........: required file name for writing results.
109 (default "results.txt")
111 ResultFormat.........: this indicates the format of the result output.
112 (default "GUESS", allowed "TXT", "PY", "GUESS")
114 ResultLevel..........: quantity of results that will be retrieved. By
115 default, it will always write at least the calibration results.
116 (default None, allowed None="Final", "Final", "IntermediaryFinal")
118 Verbose..............: True or False
121 ARGUMENTS OUTPUT THAT CAN BE OBTAINDED FOR "CALIBRATE" TASK
122 -----------------------------------------------------------
124 The output results are returned at the end of the calibration process, and
125 evantually during it. The output will be at least written in a file, and in the
126 same time returned in a python dictionary containing result information. The
127 dictionnary entries are the following ones:
129 NamesOfParameters....: names of the individual parameter variables to be
132 InitialParameters....: initial or background parameter set, as known before
133 calibration. If it is given by the user, it can differ from the initial
134 model values embedded in the model.
136 OptimalParameters....: optimal parameter set, as known after calibration.
139 NamesOfMeasures......: names of the measures subsets used for the
142 Measures.............: measures subsets used for calibration
144 OptimalSimulation....: simulation results obtained with optimal parameter
145 set, using the model. It can be directly compared to measures.
147 HOW TO DESCRIBE A DYNAMIC MODEL?
148 --------------------------------
150 Model can be given either by the name of the model parameter file (e.g.
151 "dsin.txt") with absolute or relative path, or by the directory where live the
152 DYMOLA compiled executable "dymosim.exe" and the parameters file "dsin.txt".
154 HOW TO DESCRIBE THE MEASURES?
155 -----------------------------
157 Measures can be given by colums files, with named colums, in TXT, CVS or TSV
158 file types (with respective delimiters "space", ";" and "tab" and respective
159 file extension ".txt", ".csv" and ".tsv"). Beginning lines with "#" indicates a
160 comment line, with the first uncommented line having to present the names of the
161 columns variables if available.
163 Example of a TXT file:
166 Date Heure Variable1 Variable2
167 22/04/2018 8:0:0 295 0.01
168 23/04/2018 8:0:0 296 0.01
169 24/04/2018 8:0:0 294 0.01
170 25/04/2018 8:0:0 293 0.001
171 26/04/2018 8:0:0 295 0.01
172 27/04/2018 8:0:0 297 0.5
174 Example of a CSV file:
177 Date,Heure,Variable1,Variable2
178 22/04/2018,8:0:0,295,0.01
179 23/04/2018,8:0:0,296,0.01
180 24/04/2018,8:0:0,294,0.01
181 25/04/2018,8:0:0,293,0.001
182 26/04/2018,8:0:0,295,0.01
183 27/04/2018,8:0:0,297,0.5
185 HOW TO SET CALIBRATION PARAMETERS?
186 ----------------------------------
188 ADAO parameters can be given by Python file, with named standard variables (see
189 ADAO documentation for details). All parameters have wise defaults). Error
190 covariance matrices can be set also in this parameter file.
192 The variables, that can (but has not to) be set (see ADAO documentation
194 Algorithm........: calibration algorithm (default "3DVAR")
195 Parameters.......: dictionary of optimization parameters and of
196 optimization bounds (default Model ones)
197 BackgroundError..: a priori errors covariance matrix (default 1.)
198 ObservationError.: a priori model-observation errors covariance
200 Background.......: see below.
202 Initial values and bounds of the variables to calibrate can be read from user
203 file, ADAO parameters file or model parameters file, allowing to superseed the
204 model initial default values. The priority is "USER", then "ADAO", then "DSIN",
205 and the default one correspond to "DSIN". So the background can be set using
206 three alternative ways:
208 - by reading named parameters in a user file, with 2 (or 4) colums
209 describing pairs (or quadruplets) for each variable with the Name, the
210 Values (and Min/Max bounds if required). The file can be in CSV or TXT
214 BackgroundFormat = "USER"
217 Example of CSV file content:
218 Name,Value,Minimum,Maximum
220 hVE_val,2321820,0,None
221 PEE_val,8.3226E06,0,None
222 hEE_val,759483,None,None
224 - by reading values and bounds as series in the calibration parameters file
225 with the ADAO standard syntax.
228 BackgroundFormat = "ADAO"
230 Example of subset of Python parameters calibration file:
232 "Bounds":[[0.2,1],[0,None],[0,None],[None,None]],
234 Background=[1.4,2321820,8.3226E06,759483]
236 - by reading initial values in a model description file as the "dsin.txt"
237 for Dymola. In this case, the only information required is the names of
238 the calibrated variables as known in Dymola "dsin.txt" file.
241 BackgroundFormat = "DSIN"
245 The license for this tool and its documentation is the GNU Lesser General
246 Public License (Lesser GPL).
250 For more information, see:
251 * DYMOLA User documentation
252 * ADAO, a module for Data Assimilation and Optimization,
253 http://www.salome-platform.org/
256 __author__ = "Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D"