1 /* Simulation code for FMUCallBeforeInitializationTestModel generated by the OpenModelica Compiler 1.8.1+ (r11690). */
\r
3 #include "openmodelica.h"
\r
4 #include "openmodelica_func.h"
\r
5 #include "simulation_data.h"
\r
6 #include "simulation_runtime.h"
\r
7 #include "omc_error.h"
\r
12 #include "FMUCallBeforeInitializationTestModel_functions.h"
\r
14 #include "_FMUCallBeforeInitializationTestModel.h"
\r
15 #include "FMUCallBeforeInitializationTestModel_functions.c"
\r
16 /* dummy VARINFO and FILEINFO */
\r
17 const FILE_INFO dummyFILE_INFO = {"",-1,-1,-1,-1,1};
\r
18 const VAR_INFO dummyVAR_INFO = {-1,"","",(FILE_INFO){"",-1,-1,-1,-1,1}};
\r
22 #ifdef _OMC_MEASURE_TIME
\r
23 int measure_time_flag = 1;
\r
25 int measure_time_flag = 0;
\r
28 void setupDataStruc(DATA *data)
\r
30 ASSERT(data,"Error while initialize Data");
\r
31 data->modelData.modelName = "FMUCallBeforeInitializationTestModel";
\r
32 data->modelData.modelFilePrefix = "FMUCallBeforeInitializationTestModel";
\r
33 data->modelData.modelDir = "";
\r
34 data->modelData.modelGUID = "{f4c1919a-ebce-4a1a-baca-8e57e23985cf}";
\r
36 data->modelData.nStates = 3;
\r
37 data->modelData.nVariablesReal = 2*3+3;
\r
38 data->modelData.nVariablesInteger = 0;
\r
39 data->modelData.nVariablesBoolean = 0;
\r
40 data->modelData.nVariablesString = 0;
\r
41 data->modelData.nParametersReal = 2;
\r
42 data->modelData.nParametersInteger = 1;
\r
43 data->modelData.nParametersBoolean = 1;
\r
44 data->modelData.nParametersString = 0;
\r
45 data->modelData.nInputVars = 0;
\r
46 data->modelData.nOutputVars = 0;
\r
47 data->modelData.nJacobians = 4;
\r
48 data->modelData.nHelpVars = 0;
\r
50 data->modelData.nAliasReal = 0;
\r
51 data->modelData.nAliasInteger = 0;
\r
52 data->modelData.nAliasBoolean = 0;
\r
53 data->modelData.nAliasString = 0;
\r
55 data->modelData.nZeroCrossings = 0;
\r
56 data->modelData.nSamples = 0;
\r
57 data->modelData.nInitEquations = 0;
\r
58 data->modelData.nResiduals = 3;
\r
59 data->modelData.nExtObjs = 0;
\r
60 data->modelData.nFunctions = 0;
\r
61 data->modelData.nEquations = 12;
\r
63 data->modelData.nDelayExpressions = 0;
\r
67 void setupDataStruc2(DATA *data)
\r
69 const struct FUNCTION_INFO funcInfo[1] = {{-1,"",omc_dummyFileInfo}};
\r
70 memcpy(data->modelData.functionNames, &funcInfo, data->modelData.nFunctions*sizeof(FUNCTION_INFO));
\r
72 const VAR_INFO** equationInfo_cref1 = (const VAR_INFO**)calloc(1,sizeof(VAR_INFO*));
\r
73 equationInfo_cref1[0] = &$PvalveReal__varInfo;
\r
74 const VAR_INFO** equationInfo_cref3 = (const VAR_INFO**)calloc(1,sizeof(VAR_INFO*));
\r
75 equationInfo_cref3[0] = &$P$DER$PstockReal__varInfo;
\r
76 const VAR_INFO** equationInfo_cref5 = (const VAR_INFO**)calloc(1,sizeof(VAR_INFO*));
\r
77 equationInfo_cref5[0] = &$PvalveInteger__varInfo;
\r
78 const VAR_INFO** equationInfo_cref7 = (const VAR_INFO**)calloc(1,sizeof(VAR_INFO*));
\r
79 equationInfo_cref7[0] = &$P$DER$PstockInteger__varInfo;
\r
80 const VAR_INFO** equationInfo_cref9 = (const VAR_INFO**)calloc(1,sizeof(VAR_INFO*));
\r
81 equationInfo_cref9[0] = &$PvalveBoolean__varInfo;
\r
82 const VAR_INFO** equationInfo_cref11 = (const VAR_INFO**)calloc(1,sizeof(VAR_INFO*));
\r
83 equationInfo_cref11[0] = &$P$DER$PstockBoolean__varInfo;
\r
84 const struct EQUATION_INFO equationInfo[12] = {
\r
85 {1009,"SES_ALGORITHM 0", 0, NULL},
\r
86 {1010,"SES_SIMPLE_ASSIGN 1",1,equationInfo_cref1},
\r
87 {1011,"SES_ALGORITHM 2", 0, NULL},
\r
88 {1012,"SES_SIMPLE_ASSIGN 3",1,equationInfo_cref3},
\r
89 {1013,"SES_ALGORITHM 4", 0, NULL},
\r
90 {1014,"SES_SIMPLE_ASSIGN 5",1,equationInfo_cref5},
\r
91 {1015,"SES_ALGORITHM 6", 0, NULL},
\r
92 {1016,"SES_SIMPLE_ASSIGN 7",1,equationInfo_cref7},
\r
93 {1017,"SES_ALGORITHM 8", 0, NULL},
\r
94 {1018,"SES_SIMPLE_ASSIGN 9",1,equationInfo_cref9},
\r
95 {1019,"SES_ALGORITHM 10", 0, NULL},
\r
96 {1020,"SES_SIMPLE_ASSIGN 11",1,equationInfo_cref11}
\r
98 const int n_omc_equationInfo_reverse_prof_index = 0;
\r
99 const int omc_equationInfo_reverse_prof_index[] = {
\r
102 memcpy(data->modelData.equationInfo, &equationInfo, data->modelData.nEquations*sizeof(EQUATION_INFO));
\r
104 data->modelData.nProfileBlocks = n_omc_equationInfo_reverse_prof_index;
\r
105 data->modelData.equationInfo_reverse_prof_index = (int*) malloc(data->modelData.nProfileBlocks*sizeof(int));
\r
106 memcpy(data->modelData.equationInfo_reverse_prof_index, omc_equationInfo_reverse_prof_index, data->modelData.nProfileBlocks*sizeof(int));
\r
109 /* Has to be performed after _init.xml file has been read */
\r
110 void callExternalObjectConstructors(DATA *data)
\r
113 mem_state = get_memory_state();
\r
114 /* data->simulationInfo.extObjs = NULL; */
\r
117 void callExternalObjectDestructors(DATA *data)
\r
119 if (data->simulationInfo.extObjs) {
\r
120 free(data->simulationInfo.extObjs);
\r
121 data->simulationInfo.extObjs = 0;
\r
126 int input_function(DATA *data)
\r
131 int output_function(DATA *data)
\r
136 /* Initializes the raw time events of the simulation using the now
\r
137 calcualted parameters. */
\r
138 void function_sampleInit(DATA *data)
\r
142 int function_updateSample(DATA *data)
\r
146 mem_state = get_memory_state();
\r
147 restore_memory_state(mem_state);
\r
152 int function_storeDelayed(DATA *data)
\r
156 mem_state = get_memory_state();
\r
157 restore_memory_state(mem_state);
\r
162 int updateBoundStartValues(DATA *data)
\r
166 DEBUG_INFO(LOG_INIT, "updating start-values:");
\r
171 int initial_residual(DATA *data, double $P$_lambda, double* initialResiduals)
\r
176 mem_state = get_memory_state();
\r
177 DEBUG_INFO(LOG_RES_INIT, "updating initial_residuals:");
\r
178 initialResiduals[i++] = ((1.0 - $P$_lambda) * ($PstockReal - $P$START$PstockReal));
\r
179 DEBUG_INFO_AL2(LOG_RES_INIT, " residual[%d] : (1.0 - $_lambda) * (stockReal - $_start(stockReal)) = %f", i, initialResiduals[i-1]);
\r
180 initialResiduals[i++] = ((1.0 - $P$_lambda) * ($PstockInteger - $P$START$PstockInteger));
\r
181 DEBUG_INFO_AL2(LOG_RES_INIT, " residual[%d] : (1.0 - $_lambda) * (stockInteger - $_start(stockInteger)) = %f", i, initialResiduals[i-1]);
\r
182 initialResiduals[i++] = ((1.0 - $P$_lambda) * ($PstockBoolean - $P$START$PstockBoolean));
\r
183 DEBUG_INFO_AL2(LOG_RES_INIT, " residual[%d] : (1.0 - $_lambda) * (stockBoolean - $_start(stockBoolean)) = %f", i, initialResiduals[i-1]);
\r
184 restore_memory_state(mem_state);
\r
189 int updateBoundParameters(DATA *data)
\r
193 mem_state = get_memory_state();
\r
194 restore_memory_state(mem_state);
\r
200 void eqFunction_0(DATA *data) {
\r
201 modelica_boolean tmp0;
\r
202 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:13:2-13:93]*/
\r
204 $PvalveBoolean = (((tmp0 || $Pb) || (!$Pb))?($Pb?1.0:-1.0):$P$PRE$PvalveBoolean);
\r
205 /*#endModelicaLine*/
\r
209 void eqFunction_1(DATA *data) {
\r
210 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:16:2-16:34]*/
\r
211 $P$DER$PstockBoolean = $PvalveBoolean;
\r
212 /*#endModelicaLine*/
\r
216 void eqFunction_2(DATA *data) {
\r
217 modelica_boolean tmp1;
\r
218 modelica_boolean tmp2;
\r
219 modelica_boolean tmp3;
\r
220 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:14:2-14:80]*/
\r
222 RELATIONTOZC(tmp2, (modelica_integer)$Pi, (modelica_integer) 0, -1,Less,<);
\r
223 RELATIONTOZC(tmp3, (modelica_integer)$Pi, (modelica_integer) 0, -1,GreaterEq,>=);
\r
224 $PvalveInteger = (((tmp1 || tmp2) || tmp3)?((modelica_real)(modelica_integer)$Pi):$P$PRE$PvalveInteger);
\r
225 /*#endModelicaLine*/
\r
229 void eqFunction_3(DATA *data) {
\r
230 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:17:2-17:34]*/
\r
231 $P$DER$PstockInteger = $PvalveInteger;
\r
232 /*#endModelicaLine*/
\r
236 void eqFunction_4(DATA *data) {
\r
237 modelica_boolean tmp4;
\r
238 modelica_boolean tmp5;
\r
239 modelica_boolean tmp6;
\r
240 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:15:2-15:74]*/
\r
242 RELATIONTOZC(tmp5, $Pr, 0.0, -1,Less,<);
\r
243 RELATIONTOZC(tmp6, $Pr, 0.0, -1,GreaterEq,>=);
\r
244 $PvalveReal = (((tmp4 || tmp5) || tmp6)?$Pr:$P$PRE$PvalveReal);
\r
245 /*#endModelicaLine*/
\r
249 void eqFunction_5(DATA *data) {
\r
250 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:18:2-18:28]*/
\r
251 $P$DER$PstockReal = $PvalveReal;
\r
252 /*#endModelicaLine*/
\r
255 static void functionODE_system0(DATA *data,int omc_thread_number)
\r
257 eqFunction_0(data);
\r
258 eqFunction_1(data);
\r
259 eqFunction_2(data);
\r
260 eqFunction_3(data);
\r
261 eqFunction_4(data);
\r
262 eqFunction_5(data);
\r
264 static void (*functionODE_systems[1])(DATA *, int) = {
\r
265 functionODE_system0
\r
268 void function_initMemoryState()
\r
270 push_memory_states(1);
\r
273 int functionODE(DATA *data)
\r
276 state mem_state; /* We need to have separate memory pools for separate systems... */
\r
277 mem_state = get_memory_state();
\r
278 for (id=0; id<1; id++) {
\r
279 th_id = omp_get_thread_num();
\r
280 functionODE_systems[id](data,th_id);
\r
282 restore_memory_state(mem_state);
\r
286 #include <simulation_inline_solver.h>
\r
287 const char *_omc_force_solver=_OMC_FORCE_SOLVER;
\r
288 const int inline_work_states_ndims=_OMC_SOLVER_WORK_STATES_NDIMS;
\r
289 int functionODE_inline(DATA* data, double stepSize)
\r
294 /* for continuous time variables */
\r
295 int functionAlgebraics(DATA *data)
\r
299 mem_state = get_memory_state();
\r
300 restore_memory_state(mem_state);
\r
306 void eqFunction_6(DATA *data) {
\r
307 modelica_boolean tmp10;
\r
308 modelica_boolean tmp11;
\r
309 modelica_boolean tmp12;
\r
310 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:15:2-15:74]*/
\r
312 SAVEZEROCROSS(tmp11, $Pr, 0.0, -1,Less,<);
\r
313 SAVEZEROCROSS(tmp12, $Pr, 0.0, -1,GreaterEq,>=);
\r
314 $PvalveReal = (((tmp10 || tmp11) || tmp12)?$Pr:$P$PRE$PvalveReal);
\r
315 /*#endModelicaLine*/
\r
319 void eqFunction_7(DATA *data) {
\r
320 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:18:2-18:28]*/
\r
321 $P$DER$PstockReal = $PvalveReal;
\r
322 /*#endModelicaLine*/
\r
326 void eqFunction_8(DATA *data) {
\r
327 modelica_boolean tmp13;
\r
328 modelica_boolean tmp14;
\r
329 modelica_boolean tmp15;
\r
330 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:14:2-14:80]*/
\r
332 SAVEZEROCROSS(tmp14, (modelica_integer)$Pi, (modelica_integer) 0, -1,Less,<);
\r
333 SAVEZEROCROSS(tmp15, (modelica_integer)$Pi, (modelica_integer) 0, -1,GreaterEq,>=);
\r
334 $PvalveInteger = (((tmp13 || tmp14) || tmp15)?((modelica_real)(modelica_integer)$Pi):$P$PRE$PvalveInteger);
\r
335 /*#endModelicaLine*/
\r
339 void eqFunction_9(DATA *data) {
\r
340 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:17:2-17:34]*/
\r
341 $P$DER$PstockInteger = $PvalveInteger;
\r
342 /*#endModelicaLine*/
\r
346 void eqFunction_10(DATA *data) {
\r
347 modelica_boolean tmp16;
\r
348 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:13:2-13:93]*/
\r
350 $PvalveBoolean = (((tmp16 || $Pb) || (!$Pb))?($Pb?1.0:-1.0):$P$PRE$PvalveBoolean);
\r
351 /*#endModelicaLine*/
\r
355 void eqFunction_11(DATA *data) {
\r
356 /*#modelicaLine [FMUCallBeforeInitializationTest.mo:16:2-16:34]*/
\r
357 $P$DER$PstockBoolean = $PvalveBoolean;
\r
358 /*#endModelicaLine*/
\r
361 int functionDAE(DATA *data, int *needToIterate)
\r
364 *needToIterate = 0;
\r
366 mem_state = get_memory_state();
\r
367 eqFunction_6(data);
\r
368 eqFunction_7(data);
\r
369 eqFunction_8(data);
\r
370 eqFunction_9(data);
\r
371 eqFunction_10(data);
\r
372 eqFunction_11(data);
\r
373 restore_memory_state(mem_state);
\r
378 int function_onlyZeroCrossings(DATA *data, double *gout,double *t)
\r
382 mem_state = get_memory_state();
\r
383 restore_memory_state(mem_state);
\r
388 int checkForDiscreteChanges(DATA *data)
\r
390 int needToIterate = 0;
\r
393 return needToIterate;
\r
396 /* function to check assert after a step is done */
\r
397 int checkForAsserts(DATA *data)
\r
404 int initialAnalyticJacobianA(DATA* data){
\r
408 int initialAnalyticJacobianB(DATA* data){
\r
412 int initialAnalyticJacobianC(DATA* data){
\r
416 int initialAnalyticJacobianD(DATA* data){
\r
419 int functionJacA(DATA* data, double* jac){
\r
423 int functionJacB(DATA* data, double* jac){
\r
427 int functionJacC(DATA* data, double* jac){
\r
431 int functionJacD(DATA* data, double* jac){
\r
435 const char *linear_model_frame =
\r
436 "model linear_FMUCallBeforeInitializationTestModel\n parameter Integer n = 3; // states \n parameter Integer k = 0; // top-level inputs \n parameter Integer l = 0; // top-level outputs \n"
\r
437 " parameter Real x0[3] = {%s};\n"
\r
438 " parameter Real u0[0] = {%s};\n"
\r
439 " parameter Real A[3,3] = [%s];\n"
\r
440 " parameter Real B[3,0] = zeros(3,0);%s\n"
\r
441 " parameter Real C[0,3] = zeros(0,3);%s\n"
\r
442 " parameter Real D[0,0] = zeros(0,0);%s\n"
\r
443 " Real x[3](start=x0);\n"
\r
444 " input Real u[0];\n"
\r
445 " output Real y[0];\n"
\r
446 "\n Real x_PstockBoolean = x[1];\n Real x_PstockInteger = x[2];\n Real x_PstockReal = x[3];\n \n"
\r
447 "equation\n der(x) = A * x + B * u;\n y = C * x + D * u;\nend linear_FMUCallBeforeInitializationTestModel;\n"
\r
454 /* forward the main in the simulation runtime */
\r
455 extern int _main_SimulationRuntime(int argc, char**argv, DATA *data);
\r
457 /* call the simulation runtime main from our main! */
\r
458 int main(int argc, char**argv)
\r
461 setupDataStruc(&data);
\r
462 return _main_SimulationRuntime(argc, argv, &data);
\r