Skip to content

Commit

Permalink
Measurement file handling.
Browse files Browse the repository at this point in the history
Signed-off-by: Rule Timothy (VM/EMT3) <Timothy.Rule@de.bosch.com>
  • Loading branch information
timrulebosch committed Sep 19, 2024
1 parent e4410e0 commit b3a9da0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 5 deletions.
6 changes: 6 additions & 0 deletions dse/fmimcl/fmimcl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
54 changes: 51 additions & 3 deletions dse/fmimcl/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,35 @@
//
// SPDX-License-Identifier: Apache-2.0

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <dse/testing.h>
#include <dse/logger.h>
#include <dse/clib/util/strings.h>
#include <dse/fmimcl/fmimcl.h>
#include <dse/modelc/runtime.h>


#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)
Expand All @@ -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;
}

Expand All @@ -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;

Expand All @@ -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);

Expand Down
8 changes: 6 additions & 2 deletions tests/testscript/e2e/fmimcl_fmu2.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
env NAME=fmimcl_fmu2
env NAME=fmu_inst
env SIM=dse/build/_out/fmimcl


Expand Down Expand Up @@ -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
31 changes: 31 additions & 0 deletions tests/testscript/e2e/fmimcl_measurement.txtar
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b3a9da0

Please sign in to comment.