forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.c
More file actions
146 lines (121 loc) · 3.83 KB
/
Copy pathmodel.c
File metadata and controls
146 lines (121 loc) · 3.83 KB
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
#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 values[], size_t nValues, size_t* index) {
calculateValues(comp);
switch (vr) {
case vr_time:
ASSERT_NVALUES(1);
values[(*index)++] = comp->time;
return OK;
case vr_u:
ASSERT_NVALUES(M(n));
for (size_t i = 0; i < M(n); i++) {
values[(*index)++] = M(u)[i];
}
return OK;
case vr_A:
ASSERT_NVALUES(M(m) * M(n));
for (size_t i = 0; i < M(m); i++)
for (size_t j = 0; j < M(n); j++) {
values[(*index)++] = M(A)[i][j];
}
return OK;
case vr_y:
ASSERT_NVALUES(1);
for (size_t i = 0; i < M(m); i++) {
values[(*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 values[], size_t nValues, size_t* index) {
switch (vr) {
case vr_u:
ASSERT_NVALUES(M(n));
for (size_t i = 0; i < M(n); i++) {
M(u)[i] = values[(*index)++];
}
calculateValues(comp);
return OK;
case vr_A:
ASSERT_NVALUES(M(m) * M(n));
for (size_t i = 0; i < M(m); i++)
for (size_t j = 0; j < M(n); j++) {
M(A)[i][j] = values[(*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 values[], size_t nValues, size_t* index) {
ASSERT_NVALUES(1);
calculateValues(comp);
switch (vr) {
case vr_m:
values[(*index)++] = M(m);
return OK;
case vr_n:
values[(*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 values[], size_t nValues, size_t* index) {
ASSERT_NVALUES(1);
if (comp->state != ConfigurationMode && comp->state != ReconfigurationMode) {
logError(comp, "Structural variables can only be set in Configuration Mode or Reconfiguration Mode.");
return Error;
}
const uint64_t v = values[(*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;
}