From b3a9da056f62d38729ca29bd10ea62a24d2a37c3 Mon Sep 17 00:00:00 2001 From: "Rule Timothy (VM/EMT3)" Date: Thu, 19 Sep 2024 12:15:48 +0200 Subject: [PATCH] Measurement file handling. Signed-off-by: Rule Timothy (VM/EMT3) --- dse/fmimcl/fmimcl.h | 6 +++ dse/fmimcl/model.c | 54 +++++++++++++++++-- tests/testscript/e2e/fmimcl_fmu2.txtar | 8 ++- tests/testscript/e2e/fmimcl_measurement.txtar | 31 +++++++++++ 4 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 tests/testscript/e2e/fmimcl_measurement.txtar diff --git a/dse/fmimcl/fmimcl.h b/dse/fmimcl/fmimcl.h index 0231c56..d595540 100644 --- a/dse/fmimcl/fmimcl.h +++ b/dse/fmimcl/fmimcl.h @@ -251,6 +251,12 @@ typedef struct FmuModel { void* adapter; /* Data marshalling support. */ FmuData data; + /* Measurement file. */ + struct { + char* file_name; + void* file; + // TODO measurement descriptor + } measurement; } FmuModel; diff --git a/dse/fmimcl/model.c b/dse/fmimcl/model.c index af4ccf3..4faa6bd 100644 --- a/dse/fmimcl/model.c +++ b/dse/fmimcl/model.c @@ -2,13 +2,35 @@ // // SPDX-License-Identifier: Apache-2.0 +#include +#include +#include #include #include +#include #include +#include -#define UNUSED(x) ((void)x) -#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +#define UNUSED(x) ((void)x) +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) +#define VARNAME_MAXLEN 250 + + +char* _get_measurement_file_name(ModelDesc* model) +{ + char var[VARNAME_MAXLEN] = {}; + snprintf(var, VARNAME_MAXLEN, "${%s_MEASUREMENT_FILE:-}", model->mi->name); + for (char* p = var; *p; p++) + *p = toupper(*p); + char* value = dse_expand_vars(var); + if (strlen(value)) { + return value; + } else { + free(value); + return NULL; + } +} ModelDesc* model_create(ModelDesc* model) @@ -22,7 +44,21 @@ ModelDesc* model_create(ModelDesc* model) rc = mcl_init(m); if (rc != 0) log_fatal("Could not initiate MCL (%d)", rc); - /* Return the extended object. */ + /* Initialise measurement. */ + FmuModel* fmu = (FmuModel*)m; + fmu->measurement.file_name = _get_measurement_file_name(model); + log_notice("Measurement File: %s", fmu->measurement.file_name); + if (fmu->measurement.file_name) { + errno = 0; + fmu->measurement.file = fopen(fmu->measurement.file_name, "wb"); + if (fmu->measurement.file == NULL) + log_fatal("Failed to open measurement file: %s", + fmu->measurement.file_name); + // TODO configure the measurement interface + // (fmu_model->data.count/name/scalar) + } + + /* Return the extended object (FmuModel). */ return (ModelDesc*)m; } @@ -32,6 +68,8 @@ int model_step(ModelDesc* model, double* model_time, double stop_time) int32_t rc; MclDesc* m = (MclDesc*)model; + // TODO call measurement interface + rc = mcl_marshal_out(m); if (rc != 0) return rc; @@ -53,6 +91,16 @@ void model_destroy(ModelDesc* model) int32_t rc; MclDesc* m = (MclDesc*)model; + /* Finalise measurement. */ + FmuModel* fmu = (FmuModel*)m; + if (fmu->measurement.file) { + // TODO finalise the measurement interface + fclose(fmu->measurement.file); + fmu->measurement.file = NULL; + } + free(fmu->measurement.file_name); + + /* Unload the MCL. */ rc = mcl_unload((void*)m); if (rc != 0) log_fatal("Could not unload MCL (%d)", rc); diff --git a/tests/testscript/e2e/fmimcl_fmu2.txtar b/tests/testscript/e2e/fmimcl_fmu2.txtar index 573e11a..3af3a70 100644 --- a/tests/testscript/e2e/fmimcl_fmu2.txtar +++ b/tests/testscript/e2e/fmimcl_fmu2.txtar @@ -1,4 +1,4 @@ -env NAME=fmimcl_fmu2 +env NAME=fmu_inst env SIM=dse/build/_out/fmimcl @@ -28,4 +28,8 @@ stdout 'uid=3471121503, val=127.000000, final_val=127.000000, name=real_D_tx' -- test.sh -- SIMER="${SIMER:-ghcr.io/boschglobal/dse-simer:latest}" docker run --name simer -i --rm -v $ENTRYDIR/$SIM:/sim \ - $SIMER -valgrind $NAME -env $NAME:SIMBUS_LOGLEVEL=2 -stepsize 0.0005 -endtime 0.005 + $SIMER -valgrind $NAME \ + -env simbus:SIMBUS_LOGLEVEL=5 \ + -env extended_inst:SIMBUS_LOGLEVEL=5 \ + -env $NAME:SIMBUS_LOGLEVEL=2 \ + -stepsize 0.0005 -endtime 0.005 diff --git a/tests/testscript/e2e/fmimcl_measurement.txtar b/tests/testscript/e2e/fmimcl_measurement.txtar new file mode 100644 index 0000000..d8d973c --- /dev/null +++ b/tests/testscript/e2e/fmimcl_measurement.txtar @@ -0,0 +1,31 @@ +env NAME=fmu_inst +env SIM=dse/build/_out/fmimcl +env MEASUREMENT_FILE=examples/data/measurement.mf4 + +# TEST: FMI MCL with Measurement +rm /repo/$SIM/data/measurement.mf4 +exec sh -e $WORK/test.sh + +exists /repo/$SIM/$MEASUREMENT_FILE + + +stdout 'Load YAML File: examples/data/simulation.yaml' +stdout 'Loading symbol: model_create ... ok' +stdout 'Loading symbol: model_step ... ok' +stdout 'Loading symbol: model_destroy ... ok' +stdout 'Run the Simulation ...' +stdout 'Controller exit ...' +stdout 'Measurement File: /sim/examples/data/measurement.mf4' + + +# TODO check measurement file content + +-- test.sh -- +SIMER="${SIMER:-ghcr.io/boschglobal/dse-simer:latest}" +docker run --name simer -i --rm -v $ENTRYDIR/$SIM:/sim \ + $SIMER -valgrind $NAME \ + -env simbus:SIMBUS_LOGLEVEL=5 \ + -env extended_inst:SIMBUS_LOGLEVEL=5 \ + -env $NAME:SIMBUS_LOGLEVEL=4 \ + -env $NAME:FMU_INST_MEASUREMENT_FILE=/sim/$MEASUREMENT_FILE \ + -stepsize 0.0005 -endtime 0.005