Message Boards Message Boards

0
|
10990 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Creating S-Functions from SystemModeler generated source code?

I've been wondering if it is possible to use the generated source code from SystemModeler to create an S-Function. While I was trying to understand the generated code and looking for the header files included in it, I bumped into some C++ source files named sfunction__wrapper.cpp and hqp_sfunctionwrapper.cpp in ...\SystemModeler\include folder. These files are in the format of an S-Function file, but I'm not sure whether I can use these with the SystemModeler generated code to create an S-Function. If you have any recommendation on achieving this, I'd be glad to hear them.

I also couldn't find a way to compile and run the generated source code in other platforms. The code consists of bunch of functions, but not much explanations. The names of the functions give some idea, but that's not enough. I would be really glad if someone could explain how to make use of the generated source code.

POSTED BY: Kursad Kilinc
4 Replies

While I agree with Johan that it may be better to stay in SystemModeler, if you really need an S-function in Matlab, you can export the source code and create your S-function. The procedure would be to first Export the code with file->Export->Source Code. Once you have your code you can edit it into the format of an S-function. You must include all of the proper paths and libraries to build the code. The makefile has this information but from my experience I do the following to compile the code:

  1. Go to the makefile and grab the libraries -- it looks like this: (-lsim -lcruntime -lmmcom -llapack -lsuperlu4.3 -lblas -lf2c -lccommon -lexpat -lsundialscvodes -lsundialskinsol -lsundialsnvecserial -lfmi1importruntime -lfmi2importruntime -lfmilib -lmodelicautil -lpthread -lm)

  2. you must add these to the S-function libraries (either using a matlab script or in the s-function builder dialog --library tab). Remember to remove the "-l" flag and replace it with "lib" to make the full name of the libraries for example "-lsim" is "libsim.lib" on a pc.

  3. You must add the include search paths (on my Mac they start at: /Applications/SystemModeler_old.app/Contents/include/xxx). but you can go to the makefile and add each directory that has a -I in front of it, for example: -I/Applications/SystemModeler.app/Contents/include.

  4. If you have any external C code you will need to make sure to add that path to the header include path or you can edit the XXX_functions.cpp file and add the full path to the includes (ignore this if you do not use external C code in your model).

  5. Add the library search paths -- find the directories in the makefile with a "-L" for example: "-L/Applications/SystemModeler.app/Contents/lib/osx64 "

The C code assumes it is running as a standalone so you will need to change the main to be a function call. I have never actually built an S-function from systemModeler code -- I have built s-functions only from hand-written code -- but I have compiled Standalone SystemModeler-generated code using the procedure above in order to debug custom C code. I see no reason why it will not work in your situation. Good luck!

POSTED BY: Neil Singer

Thanks for the suggestions. There are some reasons why I want to create an S-Function.

I can import FMUs into Simulink with the toolbox that Mathworks provide (FMI Pilot Support Package). However, the toolbox works only with Matlab 2015 and newer versions. I'm trying to export the model into Matlab 2013 and unfortunately I can't change the version. There are other toolboxes (like Modelon FMI Toolbox) that I can use, but they are not free of charge. So we thought that maybe it is possible to create an S-Function using the generated source code.

I can't use WSMLinearize to linearize the model, because the model has a lot of different states and WSMLinearize linearizes the model around a single state.

POSTED BY: Kursad Kilinc
Posted 8 years ago
  • If you are not going to upgrade to 2015+ then why not take the full step and run everything in SystemModeler and Mathematica?
  • If that is not an option there's also the possibility to write an interface in Matlab that communicates with the standalone simulation executable. See the description and example here: WSMCOM for a discussion with codes on how that can be done from Mathematica. You'd then have to write the interface from Matlab/Simulink.
POSTED BY: Johan Rhodin
Posted 8 years ago

Is there a specific reason why you want an S-Function?

  1. While it might theoretically be possible to create an S-Function, that's not want you want to do. Instead export the part of your model that you want to export as an FMU (Functional Mock-up Unit) and use one of the toolboxes such as FMI Toolbox to import it.
  2. The generated source code is generated for the same architecture as the platform that you're on, so if you are on a Linux machine it will generate for Linux. The generated source code is to be used as a standalone simulation executable.
  3. For some systems you're able to export the model as a transfer function or state space model. This is done by using the WSMLinearize function from WSMLink in Mathematica. This transfer function you can than manually incorporate in your C-code, if that is your goal.
POSTED BY: Johan Rhodin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract