forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.c
171 lines (143 loc) · 4.33 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
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
#include <math.h> // for fabs()
#include <float.h> // for DBL_MIN
#include "config.h"
#include "model.h"
#define V_MIN (0.1)
#define EVENT_EPSILON (1e-10)
void setStartValues(ModelInstance *comp) {
M(h) = 1;
M(v) = 0;
M(g) = -9.81;
M(e) = 0.7;
}
Status calculateValues(ModelInstance *comp) {
UNUSED(comp);
// nothing to do
return OK;
}
Status getFloat64(ModelInstance* comp, ValueReference vr, double *value, size_t *index) {
switch (vr) {
case vr_time:
value[(*index)++] = comp->time;
return OK;
case vr_h:
value[(*index)++] = M(h);
return OK;
case vr_der_h:
case vr_v:
value[(*index)++] = M(v);
return OK;
case vr_der_v:
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:
logError(comp, "Get Float64 is not allowed for value reference %u.", vr);
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 %u) is constant and cannot be set.", vr_v_min);
return Error;
default:
logError(comp, "Unexpected value reference: %u.", vr);
return Error;
}
}
Status getOutputDerivative(ModelInstance *comp, ValueReference valueReference, int order, double *value) {
if (order != 1) {
logError(comp, "The output derivative order %d for value reference %u is not available.", order, valueReference);
return Error;
}
switch (valueReference) {
case vr_h:
*value = M(v);
return OK;
case vr_v:
*value = M(g);
return OK;
default:
logError(comp, "The output derivative for value reference %u is not available.", valueReference);
return Error;
}
}
void eventUpdate(ModelInstance *comp) {
if (M(h) <= 0 && M(v) < 0) {
M(h) = DBL_MIN; // slightly above 0 to avoid zero-crossing
M(v) = -M(v) * M(e);
if (M(v) < V_MIN) {
// stop bouncing
M(v) = 0;
M(g) = 0;
}
// reset previous event indicators
getEventIndicators(comp, comp->z, NZ);
comp->valuesOfContinuousStatesChanged = true;
} else {
comp->valuesOfContinuousStatesChanged = false;
}
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);
if (M(h) > -EVENT_EPSILON && M(h) <= 0 && M(v) > 0) {
// hysteresis for better stability
z[0] = -EVENT_EPSILON;
} else {
z[0] = M(h);
}
}