0

My research project group is trying to export matlab code as FMU. One of the way we came up with is generating c code from matlab script and export c code as FMU. I am curious if there is any tool for packaging c code as an FMU? If there is not, is there another way to achieve this?

Updating the question after the answer from @mklingn.
I have tried editing with bouncingball.c , but the problem I face is, in every example provided, variables are considered data type as either boolean or double. In my case one of the input is structure, for example my c file generated is

#include "mult.h"

/* Function Definitions */

/*
 * Arguments    : double u
 *                double v
 *                const struct0_T *m
 * Return Type  : double
 */
double mult(double u, double v, const struct0_T *m)
{
  double y;
  y = u * v * m->x[2];

  /*  fcn(u,v ,'parameter1',m,'parameter2',n) */
  return y;
}

and .h file generated is

#ifndef MULT_H
#define MULT_H
typedef struct {
  char y[6];
  double x[6];
} struct0_T;
extern double mult(double u, double v, const struct0_T *m);
#endif

so according to your point 2: such kind of exporting is not possible with FMI version 2.0. Did I understand your point correct?

1 Answers1

0

You can grab the FMU Software Development Kit for version 2.0. It has several examples implemented in C, both for Co-Simulation (which I guess is what you want) and Model-Exchange.

You can use one of the examples as a starting point (e.g. fmu20/src/models/bouncingBall from the zip file linked above). It is quite some manual work to adapt it, but you learn a lot about the interface.

Two things to keep in mind:

  1. Be aware that only small subset of Matlab features can be exported to C code. See MATLAB Language Features Supported for C/C++ Code Generation for a list.
  2. FMI does not yet handle array variables. This is planned for version 3.0. So you are limited to passing scalar variables only.

The structure type you show above cannot be passed - as is - to an FMU. No calling program adhering to the standard can be aware how your internal structure is defined. They can only pass the primitive scalar data types specified by the FMI standard.

The information, however, can be passed. You could define a double model variable called e.g. x2 and either modify the mult function to use it directly or rebuild the structure inside the FMU.

mklingn
  • 151
  • 3