Skip to content

Commit

Permalink
Add support or FMI local variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
timrulebosch committed Sep 19, 2024
1 parent 06bfcec commit e4410e0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
6 changes: 6 additions & 0 deletions dse/fmimcl/examples/fmi2fmu/fmi2fmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fmi2Component fmi2Instantiate(fmi2String instance_name, fmi2Type fmu_type,
hashmap_set_double(&fmu_inst->var, "9", 0);
hashmap_set_double(&fmu_inst->var, "10", 0);
hashmap_set_double(&fmu_inst->var, "11", 0);
/* Local variable. */
hashmap_set_double(&fmu_inst->var, "12", 0);

return (fmi2Component)fmu_inst;
}
Expand Down Expand Up @@ -265,6 +267,10 @@ fmi2Status fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint,
int* vr_4 = hashmap_get(&fmu_inst->var, "4");
*vr_4 += 1;

/* Local vars too. */
double* vr_12 = hashmap_get(&fmu_inst->var, "12");
*vr_12 = 12000 + *vr_4; // Offset step count.

return fmi2OK;
}

Expand Down
2 changes: 2 additions & 0 deletions dse/fmimcl/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static void _sort_by_marshal_group(HashList* signal_list)
*/
MarshalDir mDirOrder[] = {
MARSHAL_DIRECTION_NONE,
MARSHAL_DIRECTION_LOCAL,
MARSHAL_DIRECTION_RXONLY,
MARSHAL_DIRECTION_TXRX,
MARSHAL_DIRECTION_TXONLY,
Expand Down Expand Up @@ -80,6 +81,7 @@ static MarshalDir _decode_var_dir(const char* t)
if (strcmp(t, "output") == 0) return MARSHAL_DIRECTION_RXONLY;
if (strcmp(t, "inout") == 0) return MARSHAL_DIRECTION_TXRX;
if (strcmp(t, "parameter") == 0) return MARSHAL_DIRECTION_PARAMETER;
if (strcmp(t, "local") == 0) return MARSHAL_DIRECTION_LOCAL;

return MARSHAL_DIRECTION_NONE;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/cmocka/fmimcl/data/parser_sort.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,9 @@ spec:
fmi_variable_name: Boolean_1_rx
fmi_variable_type: Boolean
fmi_variable_causality: output
- signal: real_4_local
annotations:
fmi_variable_id: 8
fmi_variable_name: real_4_local
fmi_variable_type: Real
fmi_variable_causality: local
61 changes: 37 additions & 24 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, 8);
assert_int_equal(fmu_model->data.count, 9);

fmimcl_destroy(fmu_model);
}
Expand Down Expand Up @@ -118,12 +118,21 @@ void test_engine__create_marshal_tables(void** state)
.count = 1,
.ref = { 2 },
},
{
.name = "mg-1-5-10",
.kind = MARSHAL_KIND_PRIMITIVE,
.dir = MARSHAL_DIRECTION_LOCAL,
.type = MARSHAL_TYPE_DOUBLE,
.offset = 3,
.count = 1,
.ref = { 8 },
},
{
.name = "mg-1-2-10",
.kind = MARSHAL_KIND_PRIMITIVE,
.dir = MARSHAL_DIRECTION_RXONLY,
.type = MARSHAL_TYPE_DOUBLE,
.offset = 3,
.offset = 4,
.count = 1,
.ref = { 1 },
},
Expand All @@ -132,7 +141,7 @@ void test_engine__create_marshal_tables(void** state)
.kind = MARSHAL_KIND_PRIMITIVE,
.dir = MARSHAL_DIRECTION_TXONLY,
.type = MARSHAL_TYPE_DOUBLE,
.offset = 4,
.offset = 5,
.count = 2,
.ref = { 0, 5 },
},
Expand All @@ -141,7 +150,7 @@ void test_engine__create_marshal_tables(void** state)
.kind = MARSHAL_KIND_PRIMITIVE,
.dir = MARSHAL_DIRECTION_RXONLY,
.type = MARSHAL_TYPE_BOOL,
.offset = 6,
.offset = 7,
.count = 1,
.ref = { 7 },
},
Expand All @@ -150,7 +159,7 @@ void test_engine__create_marshal_tables(void** state)
.kind = MARSHAL_KIND_PRIMITIVE,
.dir = MARSHAL_DIRECTION_TXONLY,
.type = MARSHAL_TYPE_BOOL,
.offset = 7,
.offset = 8,
.count = 1,
.ref = { 6 },
},
Expand All @@ -168,15 +177,14 @@ 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, 6);
assert_int_equal(count, 7);
assert_int_equal(count, ARRAY_SIZE(tc));

// Check the test cases.
for (size_t i = 0; i < ARRAY_SIZE(tc); i++) {
mg = &fmu_model->data.mg_table[i];
MCT_TC* t = &tc[i];
if (t->name == NULL) continue;

assert_string_equal(mg->name, t->name);
assert_int_equal(mg->kind, t->kind);
assert_int_equal(mg->dir, t->dir);
Expand Down Expand Up @@ -214,15 +222,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, 6);
assert_int_equal(count, 7);

// Set the source signals to known values.
assert_int_equal(fmu_model->data.count, 8);
assert_int_equal(fmu_model->data.count, 9);
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;
fmu_model->data.scalar[8] = true;

// Marshal out.
marshal_group_out(fmu_model->data.mg_table);
Expand All @@ -233,10 +241,11 @@ void test_engine__marshal_to_adapter(void** state)
assert_int_equal(mg[0].target._int32[1], 0);
assert_int_equal(mg[1].target._int32[0], 3);
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]);
assert_double_equal(mg[3].target._double[0], 0.0, 0.0);
assert_double_equal(mg[4].target._double[0], 6.0, 0.0);
assert_double_equal(mg[4].target._double[1], 7.0, 0.0);
assert_false(mg[5].target._int32[0]);
assert_true(mg[6].target._int32[0]);

fmimcl_destroy(fmu_model);
}
Expand All @@ -259,19 +268,22 @@ 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, 6);
assert_int_equal(count, 7);

// Set the target signals/variables to known values.
assert_int_equal(fmu_model->data.count, 8);
assert_int_equal(fmu_model->data.count, 9);
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[2].target._double[0] = 24.0;

mg[3].target._double[0] = 40.0;
mg[4].target._double[0] = 50.0;
mg[4].target._double[1] = 60.0;
mg[5].target._int32[0] = true;
mg[6].target._int32[0] = true;

// Marshal in.
marshal_group_in(fmu_model->data.mg_table);
Expand All @@ -280,11 +292,12 @@ void test_engine__marshal_from_adapter(void** state)
assert_double_equal(fmu_model->data.scalar[0], 10.0, 0.0);
assert_double_equal(fmu_model->data.scalar[1], 20.0, 0.0);
assert_double_equal(fmu_model->data.scalar[2], 0.0, 0.0);
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[3], 24.0, 0.0);
assert_double_equal(fmu_model->data.scalar[4], 40.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]);
assert_double_equal(fmu_model->data.scalar[6], 0.0, 0.0);
assert_true(fmu_model->data.scalar[7]);
assert_false(fmu_model->data.scalar[8]);


fmimcl_destroy(fmu_model);
Expand Down
6 changes: 6 additions & 0 deletions tests/cmocka/fmimcl/test_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ void test_parser__fmu_signal_sorting(void** state)
.variable_dir = MARSHAL_DIRECTION_TXONLY,
.variable_type = MARSHAL_TYPE_INT32,
},
{
.name = "real_4_local",
.variable_kind = MARSHAL_KIND_PRIMITIVE,
.variable_dir = MARSHAL_DIRECTION_LOCAL,
.variable_type = MARSHAL_TYPE_DOUBLE,
},
{
.name = "real_1_rx",
.variable_kind = MARSHAL_KIND_PRIMITIVE,
Expand Down
3 changes: 2 additions & 1 deletion tests/cmocka/fmimodelc/test_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int test_index_setup(void** state)
*m = (RuntimeModelDesc){
.runtime = {
.runtime_model = "network_fmu",
.sim_path = (const char*)EXAMPLE_MODEL_PATH,
.sim_path = strdup(EXAMPLE_MODEL_PATH),
.simulation_yaml = "data/simulation.yaml",
.end_time = 3600,
.log_level = 5,
Expand All @@ -40,6 +40,7 @@ int test_index_teardown(void** state)
RuntimeModelDesc* m = *state;
if (m) {
free(m->model.sim);
free(m->runtime.sim_path);
free(m);
}
return 0;
Expand Down

0 comments on commit e4410e0

Please sign in to comment.