Giter VIP home page Giter VIP logo

simulix's Introduction

Simulix

FMU builder for Simulink models using the C-API.

Content

License

GNU GPL V.3

Simulix generates an FMU from a simulink model source code.

Copyright (C) 2018 Scania CV AB and Simulix contributors

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Usage

Download and Installation

Windows

Dependencies

To use Simulix, you will need the following

Windows Installation

In order to use Simulix you can either

  1. Clone the GitHub repository via git:

    git clone https://github.com/Kvixen/Simulix.git
    cd Simulix
    pip install -r requirements.txt

Linux

Linux Dependencies

To use Simulix, you need the following

  • Python 3
    • Lxml - Python package
  • CMake

Optional for cross compiling to Windows

  • mingw-w64

Linux Installation

  1. Install the dependencies:

    sudo apt-get --assume-yes python
    sudo apt-get --assume-yes cmake
  2. (OPTIONAL) If you want to be able to cross compile:

    sudo apt-get --assume-yes mingw-w64
  3. Clone the github repository:

    git clone https://github.com/Kvixen/Simulix.git
    cd Simulix
    pip install -r requirements.txt

Exporting your model from Simulink

In order for Simulix to function correctly, the correct code generation options need to be selected in Simulink.

Options

In Simulink, the code generation options are located under Code, C/C++ Code, Code Generation Options.

CodeG_list CodeG

In Code Generation, under Target selection, make sure that System target file is set to grt.tlc and that Language is set to C.

Under Build process, select Package code and artifacts. You can also fill in the name of the zip file that will be generated. If the field is left empty, the zip file will have the name of the Simulink model.

Interface_list Interface

In the submenu Interface to Code Generation, make sure all boxes for C API generation are ticked.

To build your model, simply press Ctrl+B on your keyboard.

Running Simulix

Simulix comes with certain flags

Requires Arguments:

  • ZN: Positional argument for name of zipfile
  • -p : Path for generating files and build folder
  • -t : Path to templates and includes folders (if not Simulix installation folder)
  • -zp: Path to zipfile
  • -m : Makefile program
  • -f : Build folder name
  • -e : Path to extension
  • -tf: Name of temp folder

Just specify flag, not arguments:

  • --DEBUG_BUILD: Do not build release configuration, but build debug configuration
  • --ONLY_BUILD : Do not regenerate files, only build existing
  • --NO-TEMP : Do not delete the temp folder
  • --NO-CMAKE : Do not execute CMAKE
  • --NO-MAKE : Do not execute MAKE program

Here are some examples of executing Simulix

The model zip file is called example.zip and exists in the current directory

Simulix.py example

The model zip file exists in a subfolder called zip and I want to build in a subfolder called generated

mkdir generated
Simulix.py example -p generated -zp zip

Extensions

With Simulink, there's a lot of toolboxes. Instead of waiting around for support of a special toolbox you can temporary do your own.

With our program, you can supply a path to your extension with the model when building. This extension can provide additional information and replace our template files.

In order to use it, make a new folder containing a extension.py file. This is the only required step. If this file exists, even if it's empty, the extension will be recognized.

With an extension, you can use certain functions inside extension.py that will be called.

get_template_info(replace_dict):
    return replace_dict

If this function exist, the dict Simulix uses to generate template files will be passed so you either can log the variables, change them or remove them.
In order for this to take effect, you have to return the replace_dict after your changes.

With an extension, you can also modify Simulix template files. If a template file is found inside the extension in a templates folder, Simulix will default to that template file. An example of an extension can look something like this

extension.py

get_template_info(replace_dict):
    replace_dict['TEXT_STRING'] = "Hello World"
    return replace_dict

templates/CMakeLists_template.txt

message(STATUS {TEXT_STRING})

Because we are using pythons format function on the entire file, to use brackets without replacing anything and without raising a KeyError error, use double brackets. An example can look like this

templates/CMakeLists_template.txt

set(STRING_VARIABLE {TEXT_STRING})
message(STATUS ${{STRING_VARIABLE}})

This will produce the CMakeLists file like so

ModelDir/CMakeLists.txt

set(STRING_VARIABLE "Hello World")
message(STATUS ${STRING_VARIABLE})

WIKI

Wiki pages are coming soon, so we can have less clutter in the read-me. This is not top-priority, but I promise, soon™.

Currently Work In Progress

Almost everything in this project is currently work in progress, even the project name. Feel free to contribute all you want.

Support

While we are in Alpha, we will list what we are currently supporting.

✓: We currently support this software/OS

✕: We currently do not support this software/OS

?: We are currently not able to test Simulix on this software and are unable to determine if we support it or not.

  • Windows
    • 32-bit: ✕
    • 64-bit: ✓
  • Linux: ✓
  • macOS: ?

simulix's People

Contributors

beutlich avatar h-rummukainen avatar kvixen avatar tkorvola avatar traversaro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simulix's Issues

The FMU file is nowhere to be found

I am trying to build an FMU with the following command:
Simulix.py Add_2_Inputs/ -zp Add_2_Inputs/ -p Add_2_Inputs/Generated/
Add_2_Inputs is the folder in which the simulink model and the .zip file are located.

When I execute the command the terminal window doesn't display any error or success message and it jumps straight to the following line of command. The FMU is nowhere to be found and the Add_2_Inputs/Generated folder is empty.

Any idea what am I doing wrong?

Support of release build

Is there any option or orkaround to build a non-debug configuration. Currently the binary is always (cross-)compiled as debug configuration.

FMU Import in Amesim

I am having trouble to import the generated FMU into Simcenter Amesim 2019.1

Error-Log from Amesim:

>Starting: "C:\Program Files\Simcenter\vXXXX\Amesim\win32\fmi_import_compiler.exe -outdir D:/AdvancedCAE/06_Models/Matlab R2019a/Antriebsmodell -name SIMULIX_FMU -ccopt -gcc -preferred me D:/Programme/Simulix/generated/Lastmodell_FMU.fmu"

It is an FMU for Co-simulation, version 2.0, running on win32

Model description not ok. There are some error(s).
Error(s):
Duplicate scalar variable name TSamp_WtEt 
Duplicate scalar variable name UnitDelay2_InitialCondition 
Duplicate scalar variable name UnitDelay1_InitialCondition 
Warning(s):
Missing InitialUnknown index 154
There are some errors.

1.): Why does the UI tell me, that this is a FMU for Co-Simulation. I assumed, Simulix would create ModelExchange-FMUs?!?
2.): The displayed errors cannot be solved manually in total. I was able to solve some duplicate-errors by myself - of elements that i had named. Yet, concerning the variable TSamp_WtEt, this originates from Simulink-Block Discrete Derivative of the standard library. It is protected and i cannot rename the inner elements.

This is the output of the FMU-Generation:

D:\Programme\Simulix>Simulix.py Code_Export -p generated -zp test
Generating files
Building
Executing cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release ..
-- The C compiler identification is GNU 4.2.1
-- The CXX compiler identification is GNU 4.2.1
-- Check for working C compiler: C:/Program Files/Simcenter/vXXXX/Amesim/sys/mingw32/bin/gcc.exe
-- Check for working C compiler: C:/Program Files/Simcenter/vXXXX/Amesim/sys/mingw32/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files/Simcenter/vXXXX/Amesim/sys/mingw32/bin/g++.exe
-- Check for working CXX compiler: C:/Program Files/Simcenter/vXXXX/Amesim/sys/mingw32/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: C:/Python/Python37-32/python.exe (found suitable version "3.7.4", minimum required is "3")
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Programme/Simulix/generated/build
Executing mingw32-make
Scanning dependencies of target main
[  4%] Building C object CMakeFiles/main.dir/temp_folder/Simulix_exemain.c.obj
[  8%] Building C object CMakeFiles/main.dir/temp_folder/includes/cJSON.c.obj
[ 12%] Building C object CMakeFiles/main.dir/temp_folder/includes/Simulix_capi_utils.c.obj
[ 16%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/rtw_modelmap_utils.c.obj
[ 20%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/rt_logging.c.obj
[ 24%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/rt_logging_mmi.c.obj
[ 28%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/Lastmodell_FMU.c.obj
[ 32%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/Lastmodell_FMU_capi.c.obj
[ 36%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/Lastmodell_FMU_data.c.obj
[ 40%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/rtGetInf.c.obj
[ 44%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/rtGetNaN.c.obj
[ 48%] Building C object CMakeFiles/main.dir/Lastmodell_FMU/sources/rt_nonfinite.c.obj
[ 52%] Linking C executable main.exe
Creating library file: libmain.dll.a
[ 52%] Built target main
[ 56%] Generating ../Lastmodell_FMU/sources/Simulix_dllmain.c
"Executing main executable"
"JSON to XML with Python"
Scanning dependencies of target Lastmodell_FMU
[ 60%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/rtw_modelmap_utils.c.obj
[ 64%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/rt_logging.c.obj
[ 68%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/rt_logging_mmi.c.obj
[ 72%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/Lastmodell_FMU.c.obj
[ 76%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/Lastmodell_FMU_capi.c.obj
[ 80%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/Lastmodell_FMU_data.c.obj
[ 84%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/rtGetInf.c.obj
[ 88%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/rtGetNaN.c.obj
[ 92%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/rt_nonfinite.c.obj
[ 96%] Building C object CMakeFiles/Lastmodell_FMU.dir/Lastmodell_FMU/sources/Simulix_dllmain.c.obj
[100%] Linking C shared library ..\Lastmodell_FMU\binaries\win32\Lastmodell_FMU.dll
Creating library file: libLastmodell_FMU.dll.a
[100%] Built target Lastmodell_FMU

Used Software:
Matlab/Simulink R2018a
Amesim2019.1

Lookup 2D Table Column and Row Parameters exchanged in FMU Interface

Hello together,

we just noticed that the column and row are exchanged from a Lookup 2D Table in Simulink in the FMU Parameter interface (XML).
This issue is just concerning the parameter "name" in the XML. The parameter "valueReference " is totally fine.

For example, if the table in Simulink Lookup 2D Table looks like this:
A=[
0 1 2
0 1 2
0 1 2
0 1 2]

So 4 rows and 3 columns.
In the XML the tunable Parameter "name" A should be listed with its default parameters like this if I'm correct.

A[1,1]=0
A[1,2]=1
A[1,3]=2

A[2,1]=0
A[2,2]=1
A[2,3]=2

A[3,1]=0
A[3,2]=1
A[3,3]=2

A[4,1]=0
A[4,2]=1
A[4,3]=2

But what happens with the parameter "name" is this in the XML:

A[1,1]=0
A[1,2]=0
A[1,3]=0

A[2,1]=0
A[2,2]=1
A[2,3]=1

A[3,1]=1
A[3,2]=1
A[3,3]=2

A[4,1]=2
A[4,2]=2
A[4,3]=2

For the row entries it reads out the column and if the last index of the row is reached A[1,3]=0 it just writes the next index of the column in the next row A[ 2,1] = 0. Then it has read one full column and writes the next first column entry where it stopped in the row A[2,2]=1

To reproduce this just create a Lookup 2D Table in Simulink and make the table parameter tunable.
Generate the FMU and check the XML with its Parameters.

The simulation results between Simulink model and FMU are the same. Therefore, i guess the generated Code is correct. Just the connection between tunable parameter index and actual table in the code is off.

Is there a way to correct this?

Thanks alot in advance.

Best Regards
Stefan

Expected behaviour and actual Simulink Lookup 2D Table parametrization from a more complex table:
image
Read out from XML or loaded FMU in Simulink Parameter interface:
image

Simulink 2017b, 2018b, 2020b
Simulix Version 1.0.0

Empty /fmiModelDescription/ModelStructure/Derivatives and InitialUnknowns

Simulix produces empty Derivatives and InitialUnknowns elements in the model description. The FMI 2.0 schema allows these elements to be absent but not empty. Removing their output from Simulix_exemain_template.c appears to suffice as a fix.

Software (please complete the following information):

  • Simulix version 1.0
  • Matlab version R2018b

Bug with regard to integer and boolean output

The generated code has errors when you have integer and boolean output from the FMUs. The obvious errors are in the setInteger, getInteger, setBoolean, getBoolean functions. I fixed it myself, but still not working properly. Please check the bugs.

error in command for debug build

In the readme file, the command for the debug build: --DEBUG_BUILD has a typo. I tried the one suggested by you, but it threw an error. After trying --DEBUG-BUILD ("-" instead of "_"), it worked.

documentation/model.png

What is the purpose of adding the model.png to the /documentation of the FMU? The image is just a 6KB PNG header and not a valid file. If it would work, I also wonder if it is necessary to add a such default file.

Rename exception if FMU already exists

There is a FileExistsError during rename if the FMU file already exists

Traceback (most recent call last):
  File "Simulix.py", line 80, in <module>
    main()
  File "Simulix.py", line 56, in main
    pack_fmu(args.p, path.join(args.p, environ['SIMX_MODEL_NAME']), environ['SIMX_MODEL_NAME'])
  File "D:\Projects\Simulix\pack.py", line 9, in main
    rename(path.join(dst, name + '.zip'), name + ".fmu")
FileExistsError: [WinError 183] Eine Datei kann nicht erstellt werden, wenn sie bereits vorhanden ist: 'Example.fmu'

Runtime errors

If the Simulink model detects a runtime error, is there any way for it to report the error that would work in the compiled FMU? assert and error appear essentially unsupported by Matlab Coder.

PermissionError:

I am trying to run simulix.py on a zipped model from Matlab Simulink with following command:
python Simulix.py crane.zip (crane.zip is located in the same folder as simulix)

The result is as follows:
Traceback (most recent call last):
File "Simulix.py", line 85, in
main()
File "Simulix.py", line 49, in main
rmtree(args.p)
File "C:\Program Files (x86)\Python37-32\lib\shutil.py", line 516, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Program Files (x86)\Python37-32\lib\shutil.py", line 395, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Program Files (x86)\Python37-32\lib\shutil.py", line 395, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Program Files (x86)\Python37-32\lib\shutil.py", line 395, in _rmtree_unsafe
_rmtree_unsafe(fullname, onerror)
File "C:\Program Files (x86)\Python37-32\lib\shutil.py", line 400, in _rmtree_unsafe
onerror(os.unlink, fullname, sys.exc_info())
File "C:\Program Files (x86)\Python37-32\lib\shutil.py", line 398, in _rmtree_unsafe
os.unlink(fullname)
PermissionError: [WinError 5] Access is denied: 'C:\Simulix\.git\objects\pack\pack-0bc701e4e3fa698c4ae0d453a19465eec7da6f91.idx'

Any idea what can be wrong?

Destroys current working directory

The argument of the -p option is destroyed with shutil.rmtree. It defaults to the current working directory, which is likely to cause extremely nasty surprises.

This appears to have got botched in 923a7ef.

Clarify license of FMUs generated with Simulix

Currently the Simulix project is distributed under the GPL.
The GPL license might inhibit the industrial usage of Source Code FMU and for the creation of binary FMU with external code, because of the copyleft effect: to my understanding one would have to put the whole model code from which the FMU is created unter GPL license.
So it would be very beneficial to exclude the files that are included in an FMU generated by Simulix and the generated FMUs from the GPL license.
It is also recommended to include the license for the FMU in the documentation folder of the FMU.
Thanks in advance for considering this.

canHandleVariableCommunicationStepSize

I believe the canHandleVariableCommunicationStepSize capabillity flag needs to be set to false, since the Simulink model can only be exported with a fixed-step-size solver by Simulink Coder.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.