forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.c
132 lines (112 loc) · 3.38 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 "config.h"
#include "model.h"
void setStartValues(ModelInstance *comp) {
M(m) = 2;
M(n) = 2;
// identity matrix
for (int i = 0; i < M_MAX; i++) {
for (int j = 0; j < N_MAX; j++) {
M(A)[i][j] = i == j ? 1 : 0;
}}
for (int i = 0; i < M_MAX; i++) {
M(u)[i] = i + 1;
}
for (int i = 0; i < N_MAX; i++) {
M(y)[i] = 0;
}
}
Status calculateValues(ModelInstance *comp) {
// y = A * u
for (size_t i = 0; i < M(m); i++) {
M(y)[i] = 0;
for (size_t j = 0; j < M(n); j++) {
M(y)[i] += M(A)[i][j] * M(u)[j];
}
}
return OK;
}
Status getFloat64(ModelInstance* comp, ValueReference vr, double *value, size_t *index) {
calculateValues(comp);
switch (vr) {
case vr_time:
value[(*index)++] = comp->time;
return OK;
case vr_u:
for (size_t i = 0; i < M(n); i++) {
value[(*index)++] = M(u)[i];
}
return OK;
case vr_A:
for (size_t i = 0; i < M(m); i++)
for (size_t j = 0; j < M(n); j++) {
value[(*index)++] = M(A)[i][j];
}
return OK;
case vr_y:
for (size_t i = 0; i < M(m); i++) {
value[(*index)++] = M(y)[i];
}
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_u:
for (size_t i = 0; i < M(n); i++) {
M(u)[i] = value[(*index)++];
}
calculateValues(comp);
return OK;
case vr_A:
for (size_t i = 0; i < M(m); i++)
for (size_t j = 0; j < M(n); j++) {
M(A)[i][j] = value[(*index)++];
}
return OK;
default:
logError(comp, "Set Float64 is not allowed for value reference %u.", vr);
return Error;
}
}
Status getUInt64(ModelInstance* comp, ValueReference vr, uint64_t *value, size_t *index) {
calculateValues(comp);
switch (vr) {
case vr_m:
value[(*index)++] = M(m);
return OK;
case vr_n:
value[(*index)++] = M(n);
return OK;
default:
logError(comp, "Get UInt64 is not allowed for value reference %u.", vr);
return Error;
}
}
Status setUInt64(ModelInstance* comp, ValueReference vr, const uint64_t *value, size_t *index) {
if (comp->state != ConfigurationMode && comp->state != ReconfigurationMode) {
return Error;
}
const uint64_t v = value[(*index)++];
switch (vr) {
case vr_m:
if (v < 1 || v > M_MAX) return Error;
M(m) = v;
return OK;
case vr_n:
if (v < 1 || v > N_MAX) return Error;
M(n) = v;
return OK;
default:
logError(comp, "Set UInt64 is not allowed for value reference %u.", vr);
return Error;
}
}
void eventUpdate(ModelInstance *comp) {
comp->valuesOfContinuousStatesChanged = false;
comp->nominalsOfContinuousStatesChanged = false;
comp->terminateSimulation = false;
comp->nextEventTimeDefined = false;
}