Skip to content

Commit

Permalink
Support Boolean Variables
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (VM/EMT3) <[email protected]>
  • Loading branch information
Smidt Jannik authored and GitHub Enterprise committed Aug 13, 2024
1 parent bd8c01b commit 3b58a00
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 72 deletions.
8 changes: 7 additions & 1 deletion dse/fmimcl/examples/fmi2fmu/fmi2fmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ fmi2Component fmi2Instantiate(fmi2String instance_name, fmi2Type fmu_type,
hashmap_set_long(&fmu_inst->var, "3", 0);
hashmap_set_long(&fmu_inst->var, "4", 0);
hashmap_set_double(&fmu_inst->var, "5", 0.0);
/* Boolean. */
hashmap_set_long(&fmu_inst->var, "6", 0);
hashmap_set_long(&fmu_inst->var, "7", 0);
/* Shared VRs. */
hashmap_set_double(&fmu_inst->var, "8", 0);
hashmap_set_double(&fmu_inst->var, "9", 0);
hashmap_set_double(&fmu_inst->var, "10", 0);
Expand Down Expand Up @@ -246,7 +248,11 @@ fmi2Status fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint,

int* vr_6 = hashmap_get(&fmu_inst->var, "6");
int* vr_7 = hashmap_get(&fmu_inst->var, "7");
*vr_7 = *vr_6;
if (*vr_6 == true) {
*vr_7 = true;
} else {
*vr_7 = false;
}

double* vr_8 = hashmap_get(&fmu_inst->var, "8");
double* vr_9 = hashmap_get(&fmu_inst->var, "9");
Expand Down
1 change: 1 addition & 0 deletions dse/fmimcl/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static MarshalType _decode_var_type(const char* t)

if (strcmp(t, "Real") == 0) return MARSHAL_TYPE_DOUBLE;
if (strcmp(t, "Integer") == 0) return MARSHAL_TYPE_INT32;
if (strcmp(t, "Boolean") == 0) return MARSHAL_TYPE_BOOL;

return MARSHAL_TYPE_NONE;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/cmocka/fmimcl/data/fmu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ spec:
fmi_variable_name: count
fmi_variable_type: Integer
fmi_variable_start: 0
- signal: active
annotations:
fmi_variable_id: 3
fmi_variable_name: active
fmi_variable_type: Boolean
fmi_variable_start: 0
36 changes: 24 additions & 12 deletions tests/cmocka/fmimcl/data/parser_sort.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,51 @@ metadata:
channel: signal_vector
spec:
signals:
- signal: real_3_rx
- signal: real_3_tx
annotations:
fmi_variable_id: 0
fmi_variable_name: real_3_rx
fmi_variable_name: real_3_tx
fmi_variable_type: Real
fmi_variable_causality: input
- signal: real_1_tx
- signal: real_1_rx
annotations:
fmi_variable_id: 1
fmi_variable_name: real_1_tx
fmi_variable_name: real_1_rx
fmi_variable_type: Real
fmi_variable_causality: output
- signal: integer_3_rx
- signal: integer_3_tx
annotations:
fmi_variable_id: 2
fmi_variable_name: integer_3_rx
fmi_variable_name: integer_3_tx
fmi_variable_type: Integer
fmi_variable_causality: input
- signal: integer_2_tx
- signal: integer_2_rx
annotations:
fmi_variable_id: 3
fmi_variable_name: integer_2_tx
fmi_variable_name: integer_2_rx
fmi_variable_type: Integer
fmi_variable_causality: output
- signal: integer_1_tx
- signal: integer_1_rx
annotations:
fmi_variable_id: 4
fmi_variable_name: integer_1_tx
fmi_variable_name: integer_1_rx
fmi_variable_type: Integer
fmi_variable_causality: output
- signal: real_2_rx
- signal: real_2_tx
annotations:
fmi_variable_id: 5
fmi_variable_name: real_2_rx
fmi_variable_name: real_2_tx
fmi_variable_type: Real
fmi_variable_causality: input
- signal: Boolean_2_tx
annotations:
fmi_variable_id: 6
fmi_variable_name: Boolean_2_tx
fmi_variable_type: Boolean
fmi_variable_causality: input
- signal: Boolean_1_rx
annotations:
fmi_variable_id: 7
fmi_variable_name: Boolean_1_rx
fmi_variable_type: Boolean
fmi_variable_causality: output
56 changes: 28 additions & 28 deletions tests/cmocka/fmimcl/mstep/fmu_mstep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,81 +59,81 @@ metadata:
channel: signal_vector
spec:
signals:
- signal: real_3_rx
- signal: real_3_tx
annotations:
fmi_variable_id: 0
fmi_variable_name: real_3_rx
fmi_variable_name: real_3_tx
fmi_variable_type: Real
fmi_variable_causality: input
- signal: real_1_tx
- signal: real_1_rx
annotations:
fmi_variable_id: 1
fmi_variable_name: real_1_tx
fmi_variable_name: real_1_rx
fmi_variable_type: Real
fmi_variable_causality: output
- signal: integer_3_rx
- signal: integer_3_tx
annotations:
fmi_variable_id: 2
fmi_variable_name: integer_3_rx
fmi_variable_name: integer_3_tx
fmi_variable_type: Integer
fmi_variable_causality: input
- signal: integer_2_tx
- signal: integer_2_rx
annotations:
fmi_variable_id: 3
fmi_variable_name: integer_2_tx
fmi_variable_name: integer_2_rx
fmi_variable_type: Integer
fmi_variable_causality: output
- signal: integer_1_tx
- signal: integer_1_rx
annotations:
fmi_variable_id: 4
fmi_variable_name: integer_1_tx
fmi_variable_name: integer_1_rx
fmi_variable_type: Integer
fmi_variable_causality: output
- signal: real_2_rx
- signal: real_2_tx
annotations:
fmi_variable_id: 5
fmi_variable_name: real_2_rx
fmi_variable_name: real_2_tx
fmi_variable_type: Real
fmi_variable_causality: input
- signal: bool_1_rx
- signal: bool_1_tx
annotations:
fmi_variable_id: 6
fmi_variable_name: bool_1_rx
fmi_variable_type: Integer
fmi_variable_name: bool_1_tx
fmi_variable_type: Boolean
fmi_variable_causality: input
- signal: bool_2_tx
- signal: bool_2_rx
annotations:
fmi_variable_id: 7
fmi_variable_name: bool_2_tx
fmi_variable_type: Integer
fmi_variable_name: bool_2_rx
fmi_variable_type: Boolean
fmi_variable_causality: output
- signal: real_A_rx
- signal: real_A_tx
annotations:
fmi_variable_id: 8
fmi_variable_name: real_A_rx
fmi_variable_name: real_A_tx
fmi_variable_type: Real
fmi_variable_causality: input
- signal: real_A_rx
- signal: real_A_tx
annotations:
fmi_variable_id: 9
fmi_variable_name: real_A2_rx
fmi_variable_name: real_A2_tx
fmi_variable_type: Real
fmi_variable_causality: input
- signal: real_B_tx
- signal: real_B_rx
annotations:
fmi_variable_id: 10
fmi_variable_name: real_A_rx
fmi_variable_name: real_A_tx
fmi_variable_type: Real
fmi_variable_causality: output
- signal: real_C_tx
- signal: real_C_rx
annotations:
fmi_variable_id: 11
fmi_variable_name: real_A2_rx
fmi_variable_name: real_A2_tx
fmi_variable_type: Real
fmi_variable_causality: output
- signal: real_D_tx
- signal: real_D_rx
annotations:
fmi_variable_id: 11
fmi_variable_name: real_A2_rx
fmi_variable_name: real_A2_tx
fmi_variable_type: Real
fmi_variable_causality: output
38 changes: 20 additions & 18 deletions tests/cmocka/fmimcl/mstep/test_mstep.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
#include <dse/mocks/simmock.h>
#include <dse/modelc/mcl.h>

#define real_3_rx 0
#define real_1_tx 1
#define integer_3_rx 2
#define integer_2_tx 3
#define integer_1_tx 4
#define real_A_rx 8
#define real_B_tx 10
#define real_C_tx 11
#define real_D_tx 12
#define real_3_tx 0
#define real_1_rx 1
#define integer_3_tx 2
#define integer_2_rx 3
#define integer_1_rx 4
#define bool_1_tx 6
#define bool_2_rx 7
#define real_A_tx 8
#define real_B_rx 10
#define real_C_rx 11
#define real_D_rx 12


int test_setup(void** state)
Expand Down Expand Up @@ -75,25 +77,25 @@ void test_mstep(void** state)

TC_MSTEP tc[] = {
{ // FMU runs 1 time
.init_value = { 0.0, 0.0, 1.0 },
.vr_input = { real_3_rx, integer_3_rx, real_A_rx },
.expected_value = { 1.0, 1.0, 11.0, 101.0, 101.0 },
.vr_output = { real_1_tx, integer_2_tx, real_B_tx, real_C_tx,
real_D_tx },
.init_value = { 0.0, 0.0, 1.0, 1},
.vr_input = { real_3_tx, integer_3_tx, real_A_tx, bool_1_tx },
.expected_value = { 1.0, 1.0, 11.0, 101.0, 101.0, 1 },
.vr_output = { real_1_rx, integer_2_rx, real_B_rx, real_C_rx,
real_D_rx, bool_2_rx },
.sim_stepsize = 0.0001,
.steps = 1 },
{ // FMU runs additional 1 time
.init_value = { 0.0, 0.0 },
.vr_input = { real_3_rx, integer_3_rx },
.vr_input = { real_3_tx, integer_3_tx },
.expected_value = { 2.0, 2.0 },
.vr_output = { real_1_tx, integer_2_tx },
.vr_output = { real_1_rx, integer_2_rx },
.sim_stepsize = 0.00001,
.steps = 10 },
{ // FMU runs additional 10 times
.init_value = { 0.0, 0.0 },
.vr_input = { real_3_rx, integer_3_rx },
.vr_input = { real_3_tx, integer_3_tx },
.expected_value = { 12.0, 12.0 },
.vr_output = { real_1_tx, integer_2_tx },
.vr_output = { real_1_rx, integer_2_rx },
.sim_stepsize = 0.001,
.steps = 1 },
};
Expand Down
40 changes: 33 additions & 7 deletions tests/cmocka/fmimcl/test_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void test_engine__allocate_scalar_source(void** state)
fmimcl_allocate_scalar_source(fmu_model);
assert_non_null(fmu_model->data.scalar);
assert_non_null(fmu_model->data.name);
assert_int_equal(fmu_model->data.count, 6);
assert_int_equal(fmu_model->data.count, 8);

fmimcl_destroy(fmu_model);
}
Expand Down Expand Up @@ -136,6 +136,24 @@ void test_engine__create_marshal_tables(void** state)
.count = 2,
.ref = { 0, 5 },
},
{
.name = "mg-1-2-15",
.kind = MARSHAL_KIND_PRIMITIVE,
.dir = MARSHAL_DIRECTION_RXONLY,
.type = MARSHAL_TYPE_BOOL,
.offset = 6,
.count = 1,
.ref = { 7 },
},
{
.name = "mg-1-3-15",
.kind = MARSHAL_KIND_PRIMITIVE,
.dir = MARSHAL_DIRECTION_TXONLY,
.type = MARSHAL_TYPE_BOOL,
.offset = 7,
.count = 1,
.ref = { 6 },
},
};

// Parse the config.
Expand All @@ -150,7 +168,7 @@ void test_engine__create_marshal_tables(void** state)
MarshalGroup* mg;
for (mg = fmu_model->data.mg_table; mg->name; mg++)
count++;
assert_int_equal(count, 4);
assert_int_equal(count, 6);
assert_int_equal(count, ARRAY_SIZE(tc));

// Check the test cases.
Expand Down Expand Up @@ -196,13 +214,15 @@ void test_engine__marshal_to_adapter(void** state)
MarshalGroup* mg;
for (mg = fmu_model->data.mg_table; mg->name; mg++)
count++;
assert_int_equal(count, 4);
assert_int_equal(count, 6);

// Set the source signals to known values.
assert_int_equal(fmu_model->data.count, 6);
for (size_t i = 0; i < fmu_model->data.count; i++) {
assert_int_equal(fmu_model->data.count, 8);
for (size_t i = 0; i < fmu_model->data.count - 2; i++) {
fmu_model->data.scalar[i] = i + 1;
}
fmu_model->data.scalar[6] = true;
fmu_model->data.scalar[7] = true;

// Marshal out.
marshal_group_out(fmu_model->data.mg_table);
Expand All @@ -215,6 +235,8 @@ void test_engine__marshal_to_adapter(void** state)
assert_double_equal(mg[2].target._double[0], 0.0, 0.0);
assert_double_equal(mg[3].target._double[0], 5.0, 0.0);
assert_double_equal(mg[3].target._double[1], 6.0, 0.0);
assert_false(mg[4].target._int32[0]);
assert_true(mg[5].target._int32[0]);

fmimcl_destroy(fmu_model);
}
Expand All @@ -237,17 +259,19 @@ void test_engine__marshal_from_adapter(void** state)
MarshalGroup* mg;
for (mg = fmu_model->data.mg_table; mg->name; mg++)
count++;
assert_int_equal(count, 4);
assert_int_equal(count, 6);

// Set the target signals/variables to known values.
assert_int_equal(fmu_model->data.count, 6);
assert_int_equal(fmu_model->data.count, 8);
mg = fmu_model->data.mg_table;
mg[0].target._int32[0] = 10;
mg[0].target._int32[1] = 20;
mg[1].target._int32[0] = 30;
mg[2].target._double[0] = 40.0;
mg[3].target._double[0] = 50.0;
mg[3].target._double[1] = 60.0;
mg[4].target._int32[0] = true;
mg[5].target._int32[0] = true;

// Marshal in.
marshal_group_in(fmu_model->data.mg_table);
Expand All @@ -259,6 +283,8 @@ void test_engine__marshal_from_adapter(void** state)
assert_double_equal(fmu_model->data.scalar[3], 40.0, 0.0);
assert_double_equal(fmu_model->data.scalar[4], 0.0, 0.0);
assert_double_equal(fmu_model->data.scalar[5], 0.0, 0.0);
assert_true(fmu_model->data.scalar[6]);
assert_false(fmu_model->data.scalar[7]);


fmimcl_destroy(fmu_model);
Expand Down
Loading

0 comments on commit 3b58a00

Please sign in to comment.