Skip to content

Commit ccbc4e5

Browse files
committed
Add Enumeration variables to Feedthrough model
1 parent 7dff567 commit ccbc4e5

File tree

8 files changed

+114
-3
lines changed

8 files changed

+114
-3
lines changed

Feedthrough/FMI1CS.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
numberOfContinuousStates="0"
1010
numberOfEventIndicators="0">
1111

12+
<TypeDefinitions>
13+
<Type name="Option">
14+
<EnumerationType>
15+
<Item name="Option 1" description="First option"/>
16+
<Item name="Option 2" description="Second option"/>
17+
</EnumerationType>
18+
</Type>
19+
</TypeDefinitions>
20+
1221
<DefaultExperiment startTime="0" stopTime="2"/>
1322

1423
<ModelVariables>
@@ -48,6 +57,12 @@
4857
<ScalarVariable name="String_parameter" valueReference="29" variability="parameter">
4958
<String start="Set me!"/>
5059
</ScalarVariable>
60+
<ScalarVariable name="Enumeration_input" valueReference="32" causality="input" variability="discrete">
61+
<Enumeration declaredType="Option" start="1"/>
62+
</ScalarVariable>
63+
<ScalarVariable name="Enumeration_output" valueReference="33" causality="output" variability="discrete">
64+
<Enumeration declaredType="Option"/>
65+
</ScalarVariable>
5166
</ModelVariables>
5267

5368
<Implementation>

Feedthrough/FMI1ME.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
numberOfContinuousStates="0"
1010
numberOfEventIndicators="0">
1111

12+
<TypeDefinitions>
13+
<Type name="Option">
14+
<EnumerationType>
15+
<Item name="Option 1" description="First option"/>
16+
<Item name="Option 2" description="Second option"/>
17+
</EnumerationType>
18+
</Type>
19+
</TypeDefinitions>
20+
1221
<DefaultExperiment startTime="0" stopTime="2"/>
1322

1423
<ModelVariables>
@@ -48,6 +57,12 @@
4857
<ScalarVariable name="String_parameter" valueReference="29" variability="parameter">
4958
<String start="Set me!"/>
5059
</ScalarVariable>
60+
<ScalarVariable name="Enumeration_input" valueReference="32" causality="input" variability="discrete">
61+
<Enumeration declaredType="Option" start="1"/>
62+
</ScalarVariable>
63+
<ScalarVariable name="Enumeration_output" valueReference="33" causality="output" variability="discrete">
64+
<Enumeration declaredType="Option"/>
65+
</ScalarVariable>
5166
</ModelVariables>
5267

5368
</fmiModelDescription>

Feedthrough/FMI2.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
</SourceFiles>
2929
</CoSimulation>
3030

31+
<TypeDefinitions>
32+
<SimpleType name="Option">
33+
<Enumeration>
34+
<Item name="Option 1" value="1" description="First option"/>
35+
<Item name="Option 2" value="2" description="Second option"/>
36+
</Enumeration>
37+
</SimpleType>
38+
</TypeDefinitions>
39+
3140
<LogCategories>
3241
<Category name="logEvents" description="Log events"/>
3342
<Category name="logStatusError" description="Log error messages"/>
@@ -72,6 +81,12 @@
7281
<ScalarVariable name="String_parameter" valueReference="29" causality="parameter" variability="fixed">
7382
<String start="Set me!"/>
7483
</ScalarVariable>
84+
<ScalarVariable name="Enumeration_input" valueReference="32" causality="input" variability="discrete">
85+
<Enumeration declaredType="Option" start="1"/>
86+
</ScalarVariable>
87+
<ScalarVariable name="Enumeration_output" valueReference="33" causality="output" variability="discrete" initial="calculated">
88+
<Enumeration declaredType="Option"/>
89+
</ScalarVariable>
7590
</ModelVariables>
7691

7792
<ModelStructure>
@@ -80,12 +95,14 @@
8095
<Unknown index="7" dependencies="6" dependenciesKind="constant"/>
8196
<Unknown index="9" dependencies="8" dependenciesKind="constant"/>
8297
<Unknown index="11" dependencies="10 12" dependenciesKind="constant constant"/>
98+
<Unknown index="14" dependencies="13" dependenciesKind="constant"/>
8399
</Outputs>
84100
<InitialUnknowns>
85101
<Unknown index="5" dependencies="2 3 4" dependenciesKind="constant constant constant"/>
86102
<Unknown index="7" dependencies="6" dependenciesKind="constant"/>
87103
<Unknown index="9" dependencies="8" dependenciesKind="constant"/>
88104
<Unknown index="11" dependencies="10 12" dependenciesKind="constant constant"/>
105+
<Unknown index="14" dependencies="13" dependenciesKind="constant"/>
89106
</InitialUnknowns>
90107
</ModelStructure>
91108

Feedthrough/FMI3.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
fixedInternalStepSize="0.1"
1717
hasEventMode="true"/>
1818

19+
<TypeDefinitions>
20+
<EnumerationType name="Option">
21+
<Item name="Option 1" value="1" description="First option"/>
22+
<Item name="Option 2" value="2" description="Second option"/>
23+
</EnumerationType>
24+
</TypeDefinitions>
25+
1926
<LogCategories>
2027
<Category name="logEvents" description="Log events"/>
2128
<Category name="logStatusError" description="Log error messages"/>
@@ -75,6 +82,9 @@
7582
</Binary>
7683
<Binary name="Binary_output" valueReference="31" causality="output"/>
7784

85+
<Enumeration name="Enumeration_input" declaredType="Option" valueReference="32" causality="input" start="1"/>
86+
<Enumeration name="Enumeration_output" declaredType="Option" valueReference="33" causality="output"/>
87+
7888
</ModelVariables>
7989

8090
<ModelStructure>
@@ -92,6 +102,7 @@
92102
<Output valueReference="26" dependencies="25" dependenciesKind="constant"/>
93103
<Output valueReference="28" dependencies="27" dependenciesKind="constant"/>
94104
<Output valueReference="31" dependencies="30" dependenciesKind="constant"/>
105+
<Output valueReference="33" dependencies="32" dependenciesKind="constant"/>
95106
<InitialUnknown valueReference="2" dependencies="1" dependenciesKind="constant"/>
96107
<InitialUnknown valueReference="4" dependencies="3" dependenciesKind="constant"/>
97108
<InitialUnknown valueReference="8" dependencies="7" dependenciesKind="constant"/>
@@ -106,6 +117,7 @@
106117
<InitialUnknown valueReference="26" dependencies="25" dependenciesKind="constant"/>
107118
<InitialUnknown valueReference="28" dependencies="27" dependenciesKind="constant"/>
108119
<InitialUnknown valueReference="31" dependencies="30" dependenciesKind="constant"/>
120+
<InitialUnknown valueReference="33" dependencies="32" dependenciesKind="constant"/>
109121
</ModelStructure>
110122

111123
</fmiModelDescription>

Feedthrough/config.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,16 @@ typedef enum {
9999
vr_Binary_input,
100100
vr_Binary_output,
101101

102+
vr_Enumeration_input,
103+
vr_Enumeration_output
104+
102105
} ValueReference;
103106

107+
typedef enum {
108+
Option1 = 1,
109+
Option2
110+
} Option;
111+
104112
typedef struct {
105113

106114
float Float32_continuous_input;
@@ -149,6 +157,9 @@ typedef struct {
149157
size_t Binary_output_size;
150158
char Binary_output[BINARY_MAX_LEN];
151159

160+
Option Enumeration_input;
161+
Option Enumeration_output;
162+
152163
} ModelData;
153164

154165
extern const char* STRING_START;

Feedthrough/model.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ void setStartValues(ModelInstance *comp) {
5757
strncpy(M(Binary_input), BINARY_START, BINARY_MAX_LEN);
5858
M(Binary_output_size) = strlen(BINARY_START);
5959
strncpy(M(Binary_output), BINARY_START, BINARY_MAX_LEN);
60+
61+
M(Enumeration_input) = Option1;
62+
M(Enumeration_output) = Option1;
6063
}
6164

6265
Status calculateValues(ModelInstance *comp) {
@@ -88,6 +91,8 @@ Status calculateValues(ModelInstance *comp) {
8891
M(Binary_output_size) = M(Binary_input_size);
8992
memcpy(M(Binary_output), M(Binary_input), M(Binary_input_size));
9093

94+
M(Enumeration_output) = M(Enumeration_input);
95+
9196
return OK;
9297
}
9398

@@ -237,6 +242,14 @@ Status getInt32(ModelInstance* comp, ValueReference vr, int32_t values[], size_t
237242
case vr_Int32_output:
238243
values[(*index)++] = M(Int32_output);
239244
break;
245+
#if FMI_VERSION == 1 || FMI_VERSION == 2
246+
case vr_Enumeration_input:
247+
values[(*index)++] = M(Enumeration_input);
248+
break;
249+
case vr_Enumeration_output:
250+
values[(*index)++] = M(Enumeration_output);
251+
break;
252+
#endif
240253
default:
241254
logError(comp, "Get Int32 is not allowed for value reference %u.", vr);
242255
return Error;
@@ -275,6 +288,14 @@ Status getInt64(ModelInstance* comp, ValueReference vr, int64_t values[], size_t
275288
case vr_Int64_output:
276289
values[(*index)++] = M(Int64_output);
277290
break;
291+
#if FMI_VERSION == 3
292+
case vr_Enumeration_input:
293+
values[(*index)++] = M(Enumeration_input);
294+
break;
295+
case vr_Enumeration_output:
296+
values[(*index)++] = M(Enumeration_output);
297+
break;
298+
#endif
278299
default:
279300
logError(comp, "Get Int64 is not allowed for value reference %u.", vr);
280301
return Error;
@@ -517,6 +538,15 @@ Status setInt32(ModelInstance* comp, ValueReference vr, const int32_t values[],
517538
case vr_Int32_input:
518539
M(Int32_input) = values[(*index)++];
519540
break;
541+
#if FMI_VERSION == 1 || FMI_VERSION == 2
542+
case vr_Enumeration_input:
543+
if (values[*index] != Option1 && values[*index] != Option2) {
544+
logError(comp, "%d is not a legal value for Enumeration_input.", values[*index]);
545+
return Error;
546+
}
547+
M(Enumeration_input) = values[(*index)++];
548+
break;
549+
#endif
520550
default:
521551
logError(comp, "Set Int32 is not allowed for value reference %u.", vr);
522552
return Error;
@@ -553,6 +583,15 @@ Status setInt64(ModelInstance* comp, ValueReference vr, const int64_t values[],
553583
case vr_Int64_input:
554584
M(Int64_input) = values[(*index)++];
555585
break;
586+
#if FMI_VERSION == 3
587+
case vr_Enumeration_input:
588+
if (values[*index] != Option1 && values[*index] != Option2) {
589+
logError(comp, "%llu is not a legal value for Enumeration_input.", values[*index]);
590+
return Error;
591+
}
592+
M(Enumeration_input) = values[(*index)++];
593+
break;
594+
#endif
556595
default:
557596
logError(comp, "Set Int64 is not allowed for value reference %u.", vr);
558597
return Error;

fmusim/FMIModelDescription.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ static FMIModelDescription* readModelDescriptionFMI3(xmlNodePtr root) {
301301
" or self::UInt32"
302302
" or self::Int64"
303303
" or self::UInt64"
304-
" or self::UInt64"
304+
" or self::Enumeration"
305305
" or self::Boolean"
306306
" or self::String"
307307
" or self::Binary"
@@ -340,7 +340,7 @@ static FMIModelDescription* readModelDescriptionFMI3(xmlNodePtr root) {
340340
type = FMIInt32Type;
341341
} else if (!strcmp(name, "UInt32")) {
342342
type = FMIUInt32Type;
343-
} else if (!strcmp(name, "Int64")) {
343+
} else if (!strcmp(name, "Int64") || !strcmp(name, "Enumeration")) {
344344
type = FMIInt64Type;
345345
} else if (!strcmp(name, "UInt64")) {
346346
type = FMIUInt64Type;

tests/test_fmusim.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ def test_start_value_types(fmi_version, interface_type):
6464
'--start-value', 'Float64_continuous_input', '-5e-1',
6565
'--start-value', 'Int32_input', '2',
6666
'--start-value', 'Boolean_input', '1',
67-
'--start-value', 'String_parameter', 'FMI is awesome!'
67+
'--start-value', 'String_parameter', 'FMI is awesome!',
68+
'--start-value', 'Enumeration_input', '2',
6869
],
6970
model='Feedthrough.fmu')
7071

7172
assert result['Float64_continuous_output'][0] == -0.5
7273
assert result['Int32_output'][0] == 2
7374
assert result['Boolean_output'][0] == 1
75+
assert result['Enumeration_output'][0] == 2
7476

7577

7678
@pytest.mark.parametrize('interface_type', ['cs', 'me'])

0 commit comments

Comments
 (0)