Changeset 20888
- Timestamp:
- 2014-05-29T03:11:02+02:00 (10 years ago)
- Location:
- trunk/SimulationRuntime/fmi/export
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SimulationRuntime/fmi/export/fmi1/fmu1_model_interface.c
r20635 r20888 530 530 eventIndicators[i] = comp->fmuData->simulationInfo.zeroCrossings[i]; 531 531 if (comp->loggingOn){ 532 533 532 comp->functions.logger(c, comp->instanceName, fmiOK, "log", 533 "fmiGetEventIndicators: z%d = %.16g", i, eventIndicators[i]); 534 534 } 535 535 } … … 632 632 { 633 633 if (comp->loggingOn) comp->functions.logger(c, comp->instanceName, fmiOK, "log", 634 "fmiEventUpdate: Need to iterate state values changed!");634 "fmiEventUpdate: Need to iterate state values changed!"); 635 635 /* if new set is calculated reinit the solver */ 636 636 eventInfo->stateValuesChanged = fmiTrue; … … 749 749 { 750 750 if (comp->loggingOn) comp->functions.logger(c, comp->instanceName, fmiOK, "log", 751 "fmiEventUpdate: Need to iterate state values changed!");751 "fmiEventUpdate: Need to iterate state values changed!"); 752 752 /* if new set is calculated reinit the solver */ 753 753 *callEventUpdate = fmiTrue; -
trunk/SimulationRuntime/fmi/export/fmi1/fmu1_model_interface.h
r19898 r20888 46 46 47 47 typedef enum { 48 49 50 51 48 modelInstantiated = 1<<0, 49 modelInitialized = 1<<1, 50 modelTerminated = 1<<2, 51 modelError = 1<<3 52 52 } ModelState; 53 53 54 54 typedef struct { 55 56 57 58 59 60 61 55 fmiString instanceName; 56 fmiString GUID; 57 fmiCallbackFunctions functions; 58 fmiBoolean loggingOn; 59 fmiEventInfo eventInfo; 60 ModelState state; 61 DATA* fmuData; 62 62 } ModelInstance; 63 63 64 64 fmiStatus setString(fmiComponent comp, fmiValueReference vr, fmiString value){ 65 65 return fmiSetString(comp, &vr, 1, &value); 66 66 } 67 67 -
trunk/SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.c
r20860 r20888 39 39 #include "simulation/simulation_input_xml.h" 40 40 41 // macro to be used to log messages. The macro check if current 42 // log category is valid and, if true, call the logger provided by simulator. 43 #define FILTERED_LOG(instance, status, categoryIndex, message, ...) if (isCategoryLogged(instance, categoryIndex)) \ 44 instance->functions->logger(instance->functions->componentEnvironment, instance->instanceName, status, \ 45 logCategoriesNames[categoryIndex], message, ##__VA_ARGS__); 46 47 static fmiString logCategoriesNames[] = {"logAll", "logError", "logFmiCall", "logEvent"}; 48 41 49 // array of value references of states 42 #if NUMBER_OF_ STATES>050 #if NUMBER_OF_REALS>0 43 51 fmiValueReference vrStates[NUMBER_OF_STATES] = STATES; 44 fmiValueReference vrStatesDerivatives[NUMBER_OF_STATES] = STATESDERIVATIVES;45 52 #endif 46 53 47 static fmiBoolean invalidNumber(ModelInstance* comp, const char* f, const char* arg, int n, int nExpected){ 54 // --------------------------------------------------------------------------- 55 // Private helpers used below to validate function arguments 56 // --------------------------------------------------------------------------- 57 static fmiBoolean invalidNumber(ModelInstance *comp, const char *f, const char *arg, int n, int nExpected) { 48 58 if (n != nExpected) { 49 59 comp->state = modelError; 50 comp->functions.logger(comp, comp->instanceName, fmiError, "error", 51 "%s: Invalid argument %s = %d. Expected %d.", f, arg, n, nExpected); 60 FILTERED_LOG(comp, fmiError, LOG_ERROR, "%s: Invalid argument %s = %d. Expected %d.", f, arg, n, nExpected) 52 61 return fmiTrue; 53 62 } … … 55 64 } 56 65 57 static fmiBoolean invalidState(ModelInstance * comp, const char* f, int statesExpected){66 static fmiBoolean invalidState(ModelInstance *comp, const char *f, int statesExpected) { 58 67 if (!comp) 59 68 return fmiTrue; 60 69 if (!(comp->state & statesExpected)) { 61 70 comp->state = modelError; 62 comp->functions.logger(comp, comp->instanceName, fmiError, "error", 63 "%s: Illegal call sequence. Expected State: %d.", f, statesExpected); 71 FILTERED_LOG(comp, fmiError, LOG_ERROR, "%s: Illegal call sequence.", f) 64 72 return fmiTrue; 65 73 } … … 67 75 } 68 76 69 static fmiBoolean nullPointer(ModelInstance* comp, const char * f, const char* arg, const void* p){77 static fmiBoolean nullPointer(ModelInstance* comp, const char *f, const char *arg, const void *p) { 70 78 if (!p) { 71 79 comp->state = modelError; 72 comp->functions.logger(comp, comp->instanceName, fmiError, "error", 73 "%s: Invalid argument %s = NULL.", f, arg); 80 FILTERED_LOG(comp, fmiError, LOG_ERROR, "%s: Invalid argument %s = NULL.", f, arg) 74 81 return fmiTrue; 75 82 } … … 77 84 } 78 85 79 static fmiBoolean vrOutOfRange(ModelInstance * comp, const char* f, fmiValueReference vr, unsignedint end) {86 static fmiBoolean vrOutOfRange(ModelInstance *comp, const char *f, fmiValueReference vr, int end) { 80 87 if (vr >= end) { 81 comp->functions.logger(comp, comp->instanceName, fmiError, "error",82 "%s: Illegal value reference %u.", f, vr);83 88 comp->state = modelError; 89 FILTERED_LOG(comp, fmiError, LOG_ERROR, "%s: Illegal value reference %u.", f, vr) 90 return fmiTrue; 91 } 92 return fmiFalse; 93 } 94 95 static fmiStatus unsupportedFunction(fmiComponent c, const char *fName, int statesExpected) { 96 ModelInstance *comp = (ModelInstance *)c; 97 fmiCallbackLogger log = comp->functions->logger; 98 if (invalidState(comp, fName, statesExpected)) 99 return fmiError; 100 if (comp->loggingOn) log(c, comp->instanceName, fmiOK, "log", fName); 101 FILTERED_LOG(comp, fmiError, LOG_ERROR, "%s: Function not implemented.", fName) 102 return fmiError; 103 } 104 105 fmiStatus setString(fmiComponent comp, fmiValueReference vr, fmiString value) { 106 return fmiSetString(comp, &vr, 1, &value); 107 } 108 109 // --------------------------------------------------------------------------- 110 // Private helpers logger 111 // --------------------------------------------------------------------------- 112 // return fmiTrue if logging category is on. Else return fmiFalse. 113 fmiBoolean isCategoryLogged(ModelInstance *comp, int categoryIndex) { 114 if (categoryIndex < NUMBER_OF_CATEGORIES && (comp->logCategories[categoryIndex] || comp->logCategories[LOG_ALL])) { 84 115 return fmiTrue; 85 116 } … … 90 121 Common Functions 91 122 ****************************************************/ 92 93 123 const char* fmiGetTypesPlatform() { 94 124 return fmiTypesPlatform; … … 106 136 fmiComponent fmiInstantiate(fmiString instanceName, fmiType fmuType, fmiString fmuGUID, fmiString fmuResourceLocation, const fmiCallbackFunctions* functions, 107 137 fmiBoolean visible, fmiBoolean loggingOn) { 108 // TODO Write code here 109 return NULL; 138 // ignoring arguments: fmuResourceLocation, visible 139 ModelInstance *comp; 140 if (!functions->logger) { 141 return NULL; 142 } 143 144 if (!functions->allocateMemory || !functions->freeMemory) { 145 functions->logger(functions->componentEnvironment, instanceName, fmiError, "error", "fmiInstantiate: Missing callback function."); 146 return NULL; 147 } 148 if (!instanceName || strlen(instanceName) == 0) { 149 functions->logger(functions->componentEnvironment, instanceName, fmiError, "error", "fmiInstantiate: Missing instance name."); 150 return NULL; 151 } 152 if (strcmp(fmuGUID, MODEL_GUID)) { 153 functions->logger(functions->componentEnvironment, instanceName, fmiError, "error", "fmiInstantiate: Wrong GUID %s. Expected %s.", fmuGUID, MODEL_GUID); 154 return NULL; 155 } 156 comp = (ModelInstance *)functions->allocateMemory(1, sizeof(ModelInstance)); 157 if (comp) { 158 comp->instanceName = functions->allocateMemory(1 + strlen(instanceName), sizeof(char)); 159 comp->GUID = functions->allocateMemory(1 + strlen(fmuGUID), sizeof(char)); 160 DATA* fmudata = (DATA *)functions->allocateMemory(1, sizeof(DATA)); 161 threadData_t *threadData = (threadData_t *)functions->allocateMemory(1, sizeof(threadData)); 162 fmudata->threadData = threadData; 163 comp->fmuData = fmudata; 164 if (!comp->fmuData) { 165 functions->logger(functions->componentEnvironment, instanceName, fmiError, "error", "fmiInstantiate: Could not initialize the global data structure file."); 166 return NULL; 167 } 168 // set all categories to on or off. fmiSetDebugLogging should be called to choose specific categories. 169 int i; 170 for (i = 0; i < NUMBER_OF_CATEGORIES; i++) { 171 comp->logCategories[i] = loggingOn; 172 } 173 } 174 if (!comp || !comp->instanceName || !comp->GUID || !comp->fmuData) { 175 functions->logger(functions->componentEnvironment, instanceName, fmiError, "error", "fmiInstantiate: Out of memory."); 176 return NULL; 177 } 178 strcpy(comp->instanceName, instanceName); 179 comp->type = fmuType; 180 strcpy(comp->GUID, fmuGUID); 181 comp->functions = functions; 182 comp->componentEnvironment = functions->componentEnvironment; 183 comp->loggingOn = loggingOn; 184 comp->state = modelInstantiated; 185 /* intialize modelData */ 186 fmu2_model_interface_setupDataStruc(comp->fmuData); 187 initializeDataStruc(comp->fmuData); 188 /* setup model data with default start data */ 189 setDefaultStartValues(comp); 190 setAllVarsToStart(comp->fmuData); 191 setAllParamsToStart(comp->fmuData); 192 read_input_xml(&(comp->fmuData->modelData), &(comp->fmuData->simulationInfo)); 193 modelInfoXmlInit(&(comp->fmuData->modelData.modelDataXml)); 194 FILTERED_LOG(comp, fmiOK, LOG_FMI_CALL, "fmiInstantiate: GUID=%s", fmuGUID) 195 return comp; 110 196 } 111 197 112 198 void fmiFreeInstance(fmiComponent c) { 113 199 ModelInstance *comp = (ModelInstance *)c; 200 if (!comp) return; 201 if (invalidState(comp, "fmiFreeInstance", modelTerminated)) 202 return; 203 FILTERED_LOG(comp, fmiOK, LOG_FMI_CALL, "fmiFreeInstance") 204 205 if (comp->instanceName) comp->functions->freeMemory(comp->instanceName); 206 if (comp->GUID) comp->functions->freeMemory(comp->GUID); 207 comp->functions->freeMemory(comp); 114 208 } 115 209 116 210 fmiStatus fmiSetupExperiment(fmiComponent c, fmiBoolean toleranceDefined, fmiReal tolerance, fmiReal startTime, fmiBoolean stopTimeDefined, fmiReal stopTime) { 117 // TODO Write code here 211 ModelInstance *comp = (ModelInstance *)c; 212 if (invalidState(comp, "fmiSetupExperiment", modelInstantiated)) 213 return fmiError; 214 FILTERED_LOG(comp, fmiOK, LOG_FMI_CALL, "fmiSetupExperiment: toleranceDefined=%d tolerance=%g startTime=%g stopTimeDefined=%d stopTime=%g", toleranceDefined, tolerance, 215 startTime, stopTimeDefined, stopTime) 216 217 comp->toleranceDefined = toleranceDefined; 218 comp->tolerance = tolerance; 219 comp->startTime = startTime; 220 comp->stopTimeDefined = stopTimeDefined; 221 comp->stopTime = stopTime; 118 222 return fmiOK; 119 223 } 120 224 121 225 fmiStatus fmiEnterInitializationMode(fmiComponent c) { 122 // TODO Write code here 226 ModelInstance *comp = (ModelInstance *)c; 227 if (invalidState(comp, "fmiEnterInitializationMode", modelInstantiated)) 228 return fmiError; 229 FILTERED_LOG(comp, fmiOK, LOG_FMI_CALL, "fmiEnterInitializationMode") 230 comp->state = modelInitializationMode; 231 /* set zero-crossing tolerance */ 232 setZCtol(comp->tolerance); 233 234 setStartValues(comp); 235 copyStartValuestoInitValues(comp->fmuData); 236 /* read input vars */ 237 //input_function(comp->fmuData); 238 /* initial sample and delay before initial the system */ 239 comp->fmuData->callback->callExternalObjectConstructors(comp->fmuData); 240 /* allocate memory for non-linear system solvers */ 241 allocateNonlinearSystem(comp->fmuData); 242 /* allocate memory for non-linear system solvers */ 243 allocatelinearSystem(comp->fmuData); 244 /* allocate memory for mixed system solvers */ 245 allocatemixedSystem(comp->fmuData); 246 /* allocate memory for state selection */ 247 initializeStateSetJacobians(comp->fmuData); 248 if (initialization(comp->fmuData, "", "", "", 0.0, 5)) { 249 comp->state = modelError; 250 FILTERED_LOG(comp, fmiOK, LOG_FMI_CALL, "fmiEnterInitializationMode: failed") 251 } 252 else 253 { 254 comp->state = modelInitializationMode; 255 FILTERED_LOG(comp, fmiOK, LOG_FMI_CALL, "fmiEnterInitializationMode: succeed") 256 } 257 /*TODO: Simulation stop time is need to calculate in before hand all sample events 258 We shouldn't generate them all in beforehand */ 259 initSample(comp->fmuData, comp->fmuData->localData[0]->timeValue, 100 /*should be stopTime*/); 260 initDelay(comp->fmuData, comp->fmuData->localData[0]->timeValue); 261 262 /* due to an event overwrite old values */ 263 overwriteOldSimulationData(comp->fmuData); 264 265 comp->eventInfo.terminateSimulation = fmiFalse; 266 comp->eventInfo.valuesOfContinuousStatesChanged = fmiTrue; 267 268 /* Get next event time (sample calls)*/ 269 double nextSampleEvent = 0; 270 nextSampleEvent = getNextSampleTimeFMU(comp->fmuData); 271 if (nextSampleEvent == -1){ 272 comp->eventInfo.nextEventTimeDefined = fmiFalse; 273 }else{ 274 comp->eventInfo.nextEventTimeDefined = fmiTrue; 275 comp->eventInfo.nextEventTime = nextSampleEvent; 276 //fmiEventUpdate(comp, fmiFalse, &(comp->eventInfo)); 277 } 123 278 return fmiOK; 124 279 } 125 280 126 281 fmiStatus fmiExitInitializationMode(fmiComponent c) { 127 // TODO Write code here 282 ModelInstance *comp = (ModelInstance *)c; 283 if (invalidState(comp, "fmiExitInitializationMode", modelInitializationMode)) 284 return fmiError; 285 FILTERED_LOG(comp, fmiOK, LOG_FMI_CALL, "fmiExitInitializationMode") 286 287 comp->state = modelInitialized; 128 288 return fmiOK; 129 289 } 130 290 131 291 fmiStatus fmiTerminate(fmiComponent c) { 132 // TODO Write code here 292 // ModelInstance* comp = (ModelInstance *)c; 293 // if (invalidState(comp, "fmiTerminate", modelInitialized)) 294 // return fmiError; 295 // if (comp->loggingOn) comp->functions->logger(c, comp->instanceName, fmiOK, "log", "fmiTerminate"); 296 // comp->state = modelTerminated; 133 297 return fmiOK; 134 298 } … … 330 494 if (nvr>0 && nullPointer(comp, "fmiSetExternalFunction", "value[]", value)) 331 495 return fmiError; 332 if (comp->loggingOn) comp->functions .logger(c, comp->instanceName, fmiOK, "log",496 if (comp->loggingOn) comp->functions->logger(c, comp->instanceName, fmiOK, "log", 333 497 "fmiSetExternalFunction"); 334 498 // no check wether setting the value is allowed in the current state -
trunk/SimulationRuntime/fmi/export/fmi2/fmu2_model_interface.h
r19899 r20888 43 43 #define copy(vr, value) setString(comp, vr, value) 44 44 45 #define not_modelError (modelInstantiated|modelInitialized|modelTerminated) 45 // categories of logging supported by model. 46 // Value is the index in logCategories of a ModelInstance. 47 #define LOG_ALL 0 48 #define LOG_ERROR 1 49 #define LOG_FMI_CALL 2 50 #define LOG_EVENT 3 51 52 #define NUMBER_OF_CATEGORIES 4 46 53 47 54 typedef enum { 48 modelInstantiated = 1<<0, 49 modelInitialized = 1<<1, 50 modelTerminated = 1<<2, 51 modelError = 1<<3 55 modelInstantiated = 1<<0, 56 modelInitializationMode = 1<<1, 57 modelInitialized = 1<<2, // state just after fmiExitInitializationMode 58 modelStepping = 1<<3, // state after initialization 59 modelTerminated = 1<<4, 60 modelError = 1<<5 52 61 } ModelState; 53 62 54 63 typedef struct { 55 fmiString instanceName; 56 fmiString GUID; 57 fmiCallbackFunctions functions; 58 fmiBoolean loggingOn; 59 fmiEventInfo eventInfo; 60 ModelState state; 61 DATA* fmuData; 64 fmiString instanceName; 65 fmiType type; 66 fmiString GUID; 67 const fmiCallbackFunctions *functions; 68 fmiBoolean loggingOn; 69 fmiBoolean logCategories[NUMBER_OF_CATEGORIES]; 70 fmiComponentEnvironment componentEnvironment; 71 ModelState state; 72 fmiEventInfo eventInfo; 73 74 DATA* fmuData; 75 fmiBoolean toleranceDefined; 76 fmiReal tolerance; 77 fmiReal startTime; 78 fmiBoolean stopTimeDefined; 79 fmiReal stopTime; 62 80 } ModelInstance; 63 64 fmiStatus setString(fmiComponent comp, fmiValueReference vr, fmiString value){65 return fmiSetString(comp, &vr, 1, &value);66 }67 81 68 82 #ifdef __cplusplus
Note: See TracChangeset
for help on using the changeset viewer.