forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.c
132 lines (110 loc) · 3.34 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
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
#include <math.h> // for fabs()
#include "config.h"
#include "model.h"
// shorthand to access the variables
#define M(v) (comp->modelData->v)
#define V_MIN (0.1)
void setStartValues(ModelInstance *comp) {
M(h) = 1;
M(v) = 0;
M(g) = -9.81;
M(e) = 0.7;
}
void calculateValues(ModelInstance *comp) {
UNUSED(comp)
// do nothing
}
Status getFloat64(ModelInstance* comp, ValueReference vr, double *value, size_t *index) {
switch (vr) {
case vr_h:
value[(*index)++] = M(h);
return OK;
case vr_v:
value[(*index)++] = M(v);
return OK;
case vr_g:
value[(*index)++] = M(g);
return OK;
case vr_e:
value[(*index)++] = M(e);
return OK;
case vr_v_min:
value[(*index)++] = V_MIN;
return OK;
default:
return Error;
}
}
Status setFloat64(ModelInstance* comp, ValueReference vr, const double *value, size_t *index) {
switch (vr) {
case vr_h:
M(h) = value[(*index)++];
return OK;
case vr_v:
M(v) = value[(*index)++];
return OK;
case vr_g:
#if FMI_VERSION > 1
if (comp->type == ModelExchange &&
comp->state != Instantiated &&
comp->state != InitializationMode) {
logError(comp, "Variable g can only be set after instantiation or in initialization mode.");
return Error;
}
#endif
M(g) = value[(*index)++];
return OK;
case vr_e:
#if FMI_VERSION > 1
if (comp->type == ModelExchange &&
comp->state != Instantiated &&
comp->state != InitializationMode &&
comp->state != EventMode) {
logError(comp, "Variable e can only be set after instantiation, in initialization mode or event mode.");
return Error;
}
#endif
M(e) = value[(*index)++];
return OK;
case vr_v_min:
logError(comp, "Variable v_min (value reference %d) is constant and cannot be set.", vr_v_min);
return Error;
default:
logError(comp, "Unexpected value reference: %.", vr);
return Error;
}
}
void eventUpdate(ModelInstance *comp) {
if (M(h) <= 0 && M(v) < 0) {
M(h) = 0;
M(v) = -M(v) * M(e);
if (M(v) < V_MIN) {
// stop bouncing
M(v) = 0;
M(g) = 0;
}
comp->valuesOfContinuousStatesChanged = true;
}
comp->nominalsOfContinuousStatesChanged = false;
comp->terminateSimulation = false;
comp->nextEventTimeDefined = false;
}
void getContinuousStates(ModelInstance *comp, double x[], size_t nx) {
UNUSED(nx)
x[0] = M(h);
x[1] = M(v);
}
void setContinuousStates(ModelInstance *comp, const double x[], size_t nx) {
UNUSED(nx)
M(h) = x[0];
M(v) = x[1];
}
void getDerivatives(ModelInstance *comp, double dx[], size_t nx) {
UNUSED(nx)
dx[0] = M(v);
dx[1] = M(g);
}
void getEventIndicators(ModelInstance *comp, double z[], size_t nz) {
UNUSED(nz)
z[0] = (M(h) == 0 && M(v) == 0) ? 1 : M(h);
}