/
perform_simulation.c
351 lines (303 loc) · 11.6 KB
/
perform_simulation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#include "solver_main.h"
#include "events.h"
#include "dassl.h"
#include "simulation/simulation_runtime.h"
#include "simulation/results/simulation_result.h"
#include "openmodelica_func.h"
#include "linearSystem.h"
#include "nonlinearSystem.h"
#include "mixedSystem.h"
#include "util/omc_error.h"
#include "simulation/options.h"
#include <math.h>
#include <string.h>
#include <errno.h>
#include <float.h>
/*! \fn updateContinuousSystem
*
* Function to update the whole system with EventIteration.
* Evaluate the functionDAE()
*
* \param [ref] [data]
*/
void updateContinuousSystem(DATA *data)
{
externalInputUpdate(data);
data->callback->input_function(data);
data->callback->functionODE(data);
data->callback->functionAlgebraics(data);
data->callback->output_function(data);
data->callback->function_storeDelayed(data);
storePreValues(data);
}
/*! \fn performSimulation(DATA* data, SOLVER_INFO* solverInfo)
*
* \param [ref] [data]
* \param [ref] [solverInfo]
*
* This function performs the simulation controlled by solverInfo.
*/
int prefixedName_performSimulation(DATA* data, SOLVER_INFO* solverInfo)
{
int retValIntegrator=0;
int retValue=0;
int i, ui, eventType, retry=0;
FILE *fmt = NULL;
unsigned int stepNo=0;
SIMULATION_INFO *simInfo = &(data->simulationInfo);
solverInfo->currentTime = simInfo->startTime;
unsigned int __currStepNo = 0;
if(measure_time_flag)
{
size_t len = strlen(data->modelData.modelFilePrefix);
char* filename = (char*) malloc((len+11) * sizeof(char));
strncpy(filename,data->modelData.modelFilePrefix,len);
strncpy(&filename[len],"_prof.data",11);
fmt = fopen(filename, "wb");
if(!fmt)
{
warningStreamPrint(LOG_SOLVER, 0, "Time measurements output file %s could not be opened: %s", filename, strerror(errno));
fclose(fmt);
fmt = NULL;
}
free(filename);
}
printAllVarsDebug(data, 0, LOG_DEBUG); /* ??? */
/***** Start main simulation loop *****/
while(solverInfo->currentTime < simInfo->stopTime)
{
int success = 0;
omc_alloc_interface.collect_a_little();
threadData->currentErrorStage = ERROR_SIMULATION;
/* try */
#if !defined(OMC_EMCC)
MMC_TRY_INTERNAL(simulationJumpBuffer)
#endif
{
if(measure_time_flag)
{
for(i=0; i<data->modelData.modelDataXml.nFunctions + data->modelData.modelDataXml.nProfileBlocks; i++)
{
rt_clear(i + SIM_TIMER_FIRST_FUNCTION);
}
rt_clear(SIM_TIMER_STEP);
rt_tick(SIM_TIMER_STEP);
}
rotateRingBuffer(data->simulationData, 1, (void**) data->localData);
/***** Calculation next step size *****/
if(solverInfo->didEventStep == 1)
{
infoStreamPrint(LOG_SOLVER, 0, "offset value for the next step: %.10f", (solverInfo->currentTime - solverInfo->laststep));
}
else
{
__currStepNo++;
}
solverInfo->currentStepSize = (double)(__currStepNo*(simInfo->stopTime-simInfo->startTime))/(simInfo->numSteps) + simInfo->startTime - solverInfo->currentTime;
// if retry reduce stepsize
if(retry)
{
solverInfo->currentStepSize /= 2;
}
/***** End calculation next step size *****/
/* check for next time event */
checkForSampleEvent(data, solverInfo);
infoStreamPrint(LOG_SOLVER, 1, "call solver from %g to %g (stepSize: %g)", solverInfo->currentTime, solverInfo->currentTime + solverInfo->currentStepSize, solverInfo->currentStepSize);
/*
* integration step determine all states by a integration method
* update continuous system
*/
communicateStatus("Running", (solverInfo->currentTime-simInfo->startTime)/(simInfo->stopTime-simInfo->startTime));
retValIntegrator = solver_main_step(data, solverInfo);
if (S_OPTIMIZATION == solverInfo->solverMethod) break;
updateContinuousSystem(data);
saveZeroCrossings(data);
if (ACTIVE_STREAM(LOG_SOLVER)) messageClose(LOG_SOLVER);
/***** Event handling *****/
if (measure_time_flag) rt_tick(SIM_TIMER_EVENT);
eventType = checkEvents(data, solverInfo->eventLst, &(solverInfo->currentTime), solverInfo);
if(eventType > 0) /* event */
{
threadData->currentErrorStage = ERROR_EVENTSEARCH;
infoStreamPrint(LOG_EVENTS, 1, "%s event at time %.12g", eventType == 1 ? "time" : "state", solverInfo->currentTime);
/* prevent emit if noEventEmit flag is used */
if (!(omc_flag[FLAG_NOEVENTEMIT])) /* output left limit */
sim_result.emit(&sim_result,data);
handleEvents(data, solverInfo->eventLst, &(solverInfo->currentTime), solverInfo);
if (ACTIVE_STREAM(LOG_EVENTS)) messageClose(LOG_EVENTS);
threadData->currentErrorStage = ERROR_SIMULATION;
solverInfo->didEventStep = 1;
overwriteOldSimulationData(data);
}
else /* no event */
{
solverInfo->laststep = solverInfo->currentTime;
solverInfo->didEventStep=0;
}
if (measure_time_flag) rt_accumulate(SIM_TIMER_EVENT);
/***** End event handling *****/
/***** check state selection *****/
if (stateSelection(data, 1, 1))
{
/* if new set is calculated reinit the solver */
solverInfo->didEventStep = 1;
overwriteOldSimulationData(data);
}
/* Check for warning of variables out of range assert(min<x || x>xmax, ...)*/
data->callback->checkForAsserts(data);
if(retry)
{
retry=0;
}
/***** Emit this time step *****/
storePreValues(data);
storeOldValues(data);
saveZeroCrossings(data);
if (fmt)
{
int flag = 1;
double tmpdbl;
unsigned int tmpint;
rt_tick(SIM_TIMER_OVERHEAD);
rt_accumulate(SIM_TIMER_STEP);
/* Disable time measurements if we have trouble writing to the file... */
flag = flag && 1 == fwrite(&stepNo, sizeof(unsigned int), 1, fmt);
stepNo++;
flag = flag && 1 == fwrite(&(data->localData[0]->timeValue), sizeof(double), 1, fmt);
tmpdbl = rt_accumulated(SIM_TIMER_STEP);
flag = flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, fmt);
for(i=0; i<data->modelData.modelDataXml.nFunctions + data->modelData.modelDataXml.nProfileBlocks; i++)
{
tmpint = rt_ncall(i + SIM_TIMER_FIRST_FUNCTION);
flag = flag && 1 == fwrite(&tmpint, sizeof(unsigned int), 1, fmt);
}
for(i=0; i<data->modelData.modelDataXml.nFunctions + data->modelData.modelDataXml.nProfileBlocks; i++)
{
tmpdbl = rt_accumulated(i + SIM_TIMER_FIRST_FUNCTION);
flag = flag && 1 == fwrite(&tmpdbl, sizeof(double), 1, fmt);
}
rt_accumulate(SIM_TIMER_OVERHEAD);
if (!flag)
{
warningStreamPrint(LOG_SOLVER, 0, "Disabled time measurements because the output file could not be generated: %s", strerror(errno));
fclose(fmt);
fmt = NULL;
}
}
/* prevent emit if noEventEmit flag is used, if it's an event */
if ((omc_flag[FLAG_NOEVENTEMIT] && solverInfo->didEventStep == 0) || !omc_flag[FLAG_NOEVENTEMIT])
{
sim_result.emit(&sim_result, data);
}
printAllVarsDebug(data, 0, LOG_DEBUG); /* ??? */
/***** end of Emit this time step *****/
/* save dassl stats before reset */
if (solverInfo->didEventStep == 1 && solverInfo->solverMethod == 3)
{
for(ui=0; ui<numStatistics; ui++)
{
((DASSL_DATA*)solverInfo->solverData)->dasslStatistics[ui] += ((DASSL_DATA*)solverInfo->solverData)->dasslStatisticsTmp[ui];
}
}
/* Check if terminate()=true */
if(terminationTerminate)
{
printInfo(stdout, TermInfo);
fputc('\n', stdout);
infoStreamPrint(LOG_STDOUT, 0, "Simulation call terminate() at time %f\nMessage : %s", data->localData[0]->timeValue, TermMsg);
simInfo->stopTime = solverInfo->currentTime;
}
/* terminate for some cases:
* - integrator fails
* - non-linear system failed to solve
* - assert was called
*/
if (retValIntegrator != 0
|| check_nonlinear_solutions(data, 0)
|| check_linear_solutions(data, 0)
|| check_mixed_solutions(data, 0))
{
if(retValIntegrator)
{
retValue = -1 + retValIntegrator;
infoStreamPrint(LOG_STDOUT, 0, "model terminate | Integrator failed. | Simulation terminated at time %g", solverInfo->currentTime);
}
else if(check_nonlinear_solutions(data, 0))
{
retValue = -2;
infoStreamPrint(LOG_STDOUT, 0, "model terminate | non-linear system solver failed. | Simulation terminated at time %g", solverInfo->currentTime);
}
else if(check_linear_solutions(data, 0))
{
retValue = -3;
infoStreamPrint(LOG_STDOUT, 0, "model terminate | linear system solver failed. | Simulation terminated at time %g", solverInfo->currentTime);
}
else if(check_mixed_solutions(data, 0))
{
retValue = -3;
infoStreamPrint(LOG_STDOUT, 0, "model terminate | mixed system solver failed. | Simulation terminated at time %g", solverInfo->currentTime);
}
else
{
retValue = -1;
infoStreamPrint(LOG_STDOUT, 0, "model terminate | probably a strong component solver failed. For more information use flags -lv LOG_NLS, LOG_LS. | Simulation terminated at time %g", solverInfo->currentTime);
}
break;
}
success = 1;
}
#if !defined(OMC_EMCC)
MMC_CATCH_INTERNAL(simulationJumpBuffer)
#endif
if (!success) { /* catch */
if(!retry)
{
/* reduce step size by a half and try again */
solverInfo->laststep = solverInfo->currentTime - solverInfo->laststep;
/* restore old values and try another step with smaller step-size by dassl*/
restoreOldValues(data);
solverInfo->currentTime = data->localData[0]->timeValue;
overwriteOldSimulationData(data);
warningStreamPrint(LOG_STDOUT, 0, "Integrator attempt to handle a problem with a called assert.");
retry = 1;
solverInfo->didEventStep = 1;
} else {
retValue = -1;
infoStreamPrint(LOG_STDOUT, 0, "model terminate | Simulation terminated by an assert at time: %g", data->localData[0]->timeValue);
break;
}
}
} /* end while solver */
if(fmt) fclose(fmt);
return retValue;
}