-
Notifications
You must be signed in to change notification settings - Fork 298
/
fmu_read_flags.c.inc
206 lines (191 loc) · 6.05 KB
/
fmu_read_flags.c.inc
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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, 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 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
* OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, 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 "fmu_read_flags.h"
#include "../simulation/options.h"
#include "../util/simulation_options.h"
#include "../util/omc_mmap.h"
/**
* @ Skips to a given character or end of file
*
* @param str
* @param c
* @return
*/
static inline const char* skipTo(const char *str, char c)
{
while(*str != c && *str != '\0')
str++;
return str;
}
/**
* @ brief puts double quotes around a string
*
* @param str
* @return
*/
static inline char* quote(const char *str)
{
char* tmp;
const char* quot = "\"";
tmp = (char*) malloc(sizeof(char) * 200);
strcpy(tmp, quot);
strcat(tmp, str);
strcat(tmp, quot);
return tmp;
}
/**
* @brief parseses and sets the solver method string
*
* @param solverInfo
* @param str string that starts at solver method string
* @return input string skipped to endline
*/
static inline const char* setSolverMethod(SOLVER_INFO *solverInfo, const char *str)
{
/* Variables */
int i;
int len;
char* value;
for(i=1; i<S_MAX; i++)
{
value = quote(SOLVER_METHOD_NAME[i]);
if (strncmp(str, value, strlen(SOLVER_METHOD_NAME[i]) + 2) == 0)
{
solverInfo->solverMethod = i;
break;
}
}
return skipTo(str, '\n');
}
/**
* @brief parses flags from resources/modelName_flags.json
*
* @param solverInfo
* @param str string read from file
*/
void parseFlags(SOLVER_INFO *solverInfo, const char *str)
{
/* Variables */
int i;
char* value;
str = skipTo(str, '\"');
while(*str != '\0')
{
for(i=1; i<FMU_FLAG_MAX; i++)
{
// map the fmu flags to regular flags
value = quote(FLAG_NAME[FMU_FLAG_MAP[i]]);
if (strncmp(str, value, strlen(FLAG_NAME[FMU_FLAG_MAP[i]]) + 2) == 0)
{
str = skipTo(str, ':');
str = skipTo(str, '\"');
switch(i) {
case FMU_FLAG_SOLVER: str = setSolverMethod(solverInfo, str); break;
default: str = skipTo(str, '\n'); break;
}
}
}
str = skipTo(str, '\"');
}
}
void readFlags(DATA *fmudata, const char *str)
{
/* Variables */
const fmi2CallbackFunctions* functions;
DATA* data;
threadData_t* threadData;
SOLVER_INFO* solverInfo;
CVODE_SOLVER* cvodeData;
int retValue;
functions = comp->functions;
data = comp->fmuData;
threadData = comp->threadData;
/* Allocate memory */
solverInfo = (SOLVER_INFO*) functions->allocateMemory(1, sizeof(SOLVER_INFO));
/* Initialize solverInfo */
solverInfo->currentTime = 0;
solverInfo->currentStepSize = 0;
solverInfo->laststep = 0;
solverInfo->solverRootFinding = 0;
solverInfo->solverNoEquidistantGrid = 0;
solverInfo->lastdesiredStep = solverInfo->currentTime + solverInfo->currentStepSize;
solverInfo->eventLst = NULL;
solverInfo->didEventStep = 0;
solverInfo->stateEvents = 0;
solverInfo->sampleEvents = 0;
solverInfo->solverStats = NULL;
solverInfo->solverStatsTmp = NULL;
/* read fmu flags from flags.json */
char* flags_filename = functions->allocateMemory(strlen(comp->fmuData->modelData->resourcesDir) + strlen(comp->fmuData->modelData->modelFilePrefix) + 20, sizeof(char));
flags_filename[0] = 0;
strcat(flags_filename, comp->fmuData->modelData->resourcesDir);
strcat(flags_filename, "/");
strcat(flags_filename, comp->fmuData->modelData->modelFilePrefix);
strcat(flags_filename, "_flags.json");
FILTERED_LOG(comp, fmi2OK, LOG_ALL, "fmi2Instantiate: Trying to find simulation settings %s.", flags_filename)
if( access( flags_filename, 0 ) != -1 )
{
FILTERED_LOG(comp, fmi2OK, LOG_ALL, "fmi2Instantiate: Found simulation settings %s.", flags_filename)
omc_mmap_read mmap_reader = {0};
mmap_reader = omc_mmap_open_read(flags_filename);
parseFlags(solverInfo, mmap_reader.data);
omc_mmap_close_read(mmap_reader);
}
else
{
FILTERED_LOG(comp, fmi2OK, LOG_ALL, "fmi2Instantiate: Using default simulation settings.")
solverInfo->solverMethod = S_EULER;
}
switch (solverInfo->solverMethod)
{
case S_EULER:
/* Needs no initialization */
retValue = 0;
break;
case S_CVODE:
useStream[LOG_SOLVER] = 1;
cvodeData = NULL;
FILTERED_LOG(comp, fmi2OK, LOG_ALL, "Initializing CVODE ODE Solver")
cvodeData = (CVODE_SOLVER*) functions->allocateMemory(1, sizeof(CVODE_SOLVER));
if (!cvodeData) {
FILTERED_LOG(comp, fmi2Error, LOG_STATUSERROR, "fmi2Instantiate: Out of memory.")
}
retValue = cvode_solver_initial(data, threadData, solverInfo, cvodeData); /* TODO: cvode_solver_initial needs to use malloc and free from fmi2CallbackFunctions */
solverInfo->solverData = cvodeData;
useStream[LOG_SOLVER] = 0;
break;
default:
FILTERED_LOG(comp, fmi2Error, LOG_STATUSERROR, "fmi2Instantiate: Unknown solver method.")
retValue = -1;
}
functions->freeMemory(flags_filename);
comp->solverInfo = solverInfo;
return retValue;
}