Skip to content

Commit

Permalink
causality independent + non-float error check
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Meisrimel authored and Peter Meisrimel committed Jul 16, 2024
1 parent 3efaaeb commit 0c7d5e8
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
24 changes: 24 additions & 0 deletions Test/FMI3/fmi3_import_variable_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,3 +1002,27 @@ TEST_CASE("Invalid canHandleMultipleSetPerTimeInstant; set to non-default for ca
REQUIRE(fmi3_testutil_get_num_problems(tfmu) == 1);
fmi3_testutil_import_free(tfmu);
}

TEST_CASE("Causality independent on non float") {
const char* xmldir = FMI3_TEST_XML_DIR "/variables/invalid/nonfloat_independent";
fmi3_testutil_import_t* tfmu = fmi3_testutil_parse_xml_with_log(xmldir);
REQUIRE(tfmu != nullptr);
fmi3_import_t* fmu = tfmu->fmu;
REQUIRE(fmu != nullptr);

REQUIRE(fmi3_testutil_log_contains(tfmu, "Causality 'independent' is only allowed for float type variables.")); // 6 times
REQUIRE(fmi3_testutil_log_contains(tfmu, "Invalid combination of variability Unknown and causality independent for variable 'i32'. Setting variability to 'Unknown'"));
REQUIRE(fmi3_testutil_log_contains(tfmu, "Invalid combination of variability Unknown and causality independent for variable 'i64'. Setting variability to 'Unknown'"));
REQUIRE(fmi3_testutil_log_contains(tfmu, "Invalid combination of variability Unknown and causality independent for variable 'enum'. Setting variability to 'Unknown'"));
REQUIRE(fmi3_testutil_log_contains(tfmu, "Invalid combination of variability Unknown and causality independent for variable 'binary'. Setting variability to 'Unknown'"));
REQUIRE(fmi3_testutil_log_contains(tfmu, "Invalid combination of variability Unknown and causality independent for variable 'clock'. Setting variability to 'Unknown'"));
REQUIRE(fmi3_testutil_log_contains(tfmu, "Invalid combination of variability Unknown and causality independent for variable 'bool'. Setting variability to 'Unknown'"));
for (int i = 1; i <= 6; i++) {
fmi3_import_variable_t* var = fmi3_import_get_variable_by_vr(fmu, i);
REQUIRE(var != nullptr);
REQUIRE(fmi3_import_get_variable_causality(var) == fmi3_causality_enu_independent);
}

REQUIRE(fmi3_testutil_get_num_problems(tfmu) == 12);
fmi3_testutil_import_free(tfmu);
}
5 changes: 1 addition & 4 deletions Test/FMI3/fmi3_variability_causality_initial_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_CASE("Default valid variability") {
REQUIRE(fmi3_get_default_valid_variability(fmi3_causality_enu_input, 0) == fmi3_variability_enu_discrete);
REQUIRE(fmi3_get_default_valid_variability(fmi3_causality_enu_output, 0) == fmi3_variability_enu_discrete);
REQUIRE(fmi3_get_default_valid_variability(fmi3_causality_enu_local, 0) == fmi3_variability_enu_discrete);
REQUIRE(fmi3_get_default_valid_variability(fmi3_causality_enu_independent, 0) == fmi3_variability_enu_continuous);
REQUIRE(fmi3_get_default_valid_variability(fmi3_causality_enu_independent, 0) == fmi3_variability_enu_unknown);

REQUIRE(fmi3_get_default_valid_variability(fmi3_causality_enu_unknown, 0) == fmi3_variability_enu_unknown);

Expand Down Expand Up @@ -389,9 +389,6 @@ TEST_CASE("Test default variabilities") {

v = fmi3_import_get_variable_by_vr(tfmu->fmu, 16);
REQUIRE(fmi3_import_get_variable_variability(v) == fmi3_variability_enu_discrete);

v = fmi3_import_get_variable_by_vr(tfmu->fmu, 17);
REQUIRE(fmi3_import_get_variable_variability(v) == fmi3_variability_enu_continuous);
}

// Taking default does not have any warnings or similar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<Int64 name="i64_input" valueReference="14" start="0" causality="input"/>
<Int64 name="i64_output" valueReference="15" start="0" causality="output"/>
<Int64 name="i64_local" valueReference="16" start="0" causality="local"/>
<Int64 name="i64_independent" valueReference="17" causality="independent"/>
</ModelVariables>

<ModelStructure/>
Expand Down
6 changes: 5 additions & 1 deletion src/Util/src/FMI3/fmi3_enums.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ fmi3_variability_enu_t fmi3_get_default_valid_variability(fmi3_causality_enu_t c
return fmi3_variability_enu_unknown;
}
if (c == fmi3_causality_enu_independent) {
return fmi3_variability_enu_continuous; // only valid one
if (isFloat) {
return fmi3_variability_enu_continuous; // only valid one
} else {
return fmi3_variability_enu_unknown; // no valid combination
}
}

// Spec: "The default for variables of causality `parameter`, `structural parameter` or `calculated parameter` is `fixed`"
Expand Down
5 changes: 5 additions & 0 deletions src/XML/src/FMI2/fmi2_xml_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ int fmi2_xml_handle_ScalarVariable(fmi2_xml_parser_context_t *context, const cha
fmi2_xml_parse_error(context, "Only variables with causality 'input' can have canHandleMultipleSetPerTimeInstant=false");
return -1;
}

if ((variable->type != fmi2_base_type_real) && (variable->causality == fmi2_causality_enu_independent)) {
fmi2_xml_parse_error(context, "Causality 'independent' is only allowed for float type variables.");
return -1;
}
}
}
else {
Expand Down
8 changes: 7 additions & 1 deletion src/XML/src/FMI3/fmi3_xml_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,8 @@ static int fmi3_xml_variable_process_attr_causality_variability_initial(fmi3_xml
unsigned int variability, defaultVariability;
unsigned int initial, defaultInitial, validInitial;

bool isFloat = (elm_id == fmi3_xml_elmID_Float32) || (elm_id == fmi3_xml_elmID_Float64);

// Causality:
if (fmi3_xml_parse_attr_as_enum(context, elm_id, fmi_attr_id_causality, 0, &causality,
fmi3_causality_enu_local, causalityConventionMap))
Expand All @@ -1220,8 +1222,12 @@ static int fmi3_xml_variable_process_attr_causality_variability_initial(fmi3_xml
}
variable->causality = causality;

/* Specification: "The independent variable must be defined as a floating point type ..." */
if ((!isFloat) && (causality == fmi3_causality_enu_independent)) {
fmi3_xml_parse_warning(context, "Causality 'independent' is only allowed for float type variables.");
}

// Variability:
bool isFloat = (elm_id == fmi3_xml_elmID_Float32) || (elm_id == fmi3_xml_elmID_Float64);
defaultVariability = fmi3_get_default_valid_variability(causality, isFloat);
if (fmi3_xml_parse_attr_as_enum(context, elm_id, fmi_attr_id_variability, 0, &variability,
defaultVariability, variabilityConventionMap))
Expand Down

0 comments on commit 0c7d5e8

Please sign in to comment.