forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.c
100 lines (79 loc) · 2.66 KB
/
model.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
#include <float.h> // for DBL_EPSILON
#include <math.h> // for fabs()
#include "config.h"
#include "model.h"
void setStartValues(ModelInstance *comp) {
M(counter) = 1;
// TODO: move this to initialize()?
comp->nextEventTime = 1;
comp->nextEventTimeDefined = true;
}
Status calculateValues(ModelInstance *comp) {
UNUSED(comp);
// nothing to do
return OK;
}
Status getFloat64(ModelInstance* comp, ValueReference vr, double values[], size_t nValues, size_t* index) {
ASSERT_NVALUES(1);
switch (vr) {
case vr_time:
values[(*index)++] = comp->time;
return OK;
default:
logError(comp, "Get Float64 is not allowed for value reference %u.", vr);
return Error;
}
}
Status getInt32(ModelInstance* comp, ValueReference vr, int32_t values[], size_t nValues, size_t* index) {
ASSERT_NVALUES(1);
switch (vr) {
case vr_counter:
values[(*index)++] = M(counter);
return OK;
default:
logError(comp, "Get Int32 is not allowed for value reference %u.", vr);
return Error;
}
}
Status setInt32(ModelInstance* comp, ValueReference vr, const int32_t values[], size_t nValues, size_t* index) {
ASSERT_NVALUES(1);
switch (vr) {
case vr_counter:
#if FMI_VERSION == 1
if (comp->state != Instantiated) {
logError(comp, "Variable \"counter\" can only be set after instantiation.");
return Error;
}
#else
if (comp->state != Instantiated && comp->state != InitializationMode) {
logError(comp, "Variable \"counter\" can only be set in Instantiated and Intialization Mode.");
return Error;
}
#endif
if (values[*index] >= 10) {
logError(comp, "The maximum value for variable \"counter\" is 10.");
return Error;
}
M(counter) = values[(*index)++];
return OK;
default:
logError(comp, "Set Int32 is not allowed for value reference %u.", vr);
return Error;
}
}
Status eventUpdate(ModelInstance *comp) {
if (M(counter) >= 10) {
logError(comp, "Variable \"counter\" cannot be incremented for values >= 10.");
return Error;
}
const double epsilon = (1.0 + fabs(comp->time)) * DBL_EPSILON;
if (comp->nextEventTimeDefined && comp->time + epsilon >= comp->nextEventTime) {
M(counter)++;
comp->nextEventTime += 1;
}
comp->valuesOfContinuousStatesChanged = false;
comp->nominalsOfContinuousStatesChanged = false;
comp->terminateSimulation = M(counter) >= 10;
comp->nextEventTimeDefined = true;
return OK;
}