Thanks to visit codestin.com
Credit goes to github.com

Skip to content

NTNU-IHB/FMI4cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FMI4cpp (work in progress)

License: MIT contributions welcome Join the chat at https://gitter.im/NTNU-IHB/FMI4cpp

CircleCI

FMI4cpp is a cross-platform FMI 2.0 implementation written in modern C++.

Influenced by it's spiritual brother FMI4j, it aims to be an easy to install, easy to use, object oriented and fast FMI implementation for C++.

FMI4cpp supports both Co-simulation and Model Exchange.
For Model Exchange, solvers from odeint can be used.

Why should I use this over other C/C++ FMI Libraries

Because this library provides a clean, easy to use API and is easy to install (through vcpkg).

How do I use it in my own project?

Recommended way is to install vcpkg and run:

./vcpkg install fmi4cpp

The alternative is to fetch the dependencies manually, put them in the path somewhere and run the regular CMake install procedures.

Development dependencies

Install vcpkg and run:

./vcpkg install boost-property-tree boost-ublas boost-odeint boost-program-options libzip

Then tell CMake about your vcpkg installation by passing
-DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake to it.

Note that boost-ublas and boost-odeint are optional and can be omitted by passing -DFMI4CPP_WITH_ODEINT=OFF to CMake. Similarly, boost-program-options can be omitted by passing -DFMI4CPP_BUILD_TOOL=OFF.

On *NIX systems the dependencies can of course be installed using the native package handler.

sudo apt-get install libzip-dev libboost-dev libboost-test-dev

API

#include <fmi4cpp/fmi2/fmi2.hpp>
#include <fmi4cpp/common/logger.hpp>

using namespace fmi4cpp::fmi2;

namespace logger = fmi4cpp::logger;

const double stop = 10.0;
const double stepSize = 1.0/100;

int main() {

    Fmu fmu("path/to/fmu.fmu");
    
    auto cs_fmu = fmu.asCoSimulationFmu();
    auto me_fmu = fmu.asModelExchangeFmu();
    
    auto cs_md = fmu->getModelDescription(); //smart pointer to a CoSimulationModelDescription instance
    logger::info("modelIdentifier={}", cs_fmu->getModelDescription()->modelIdentifier());
    
    auto me_md = fmu->getModelDescription(); //smart pointer to a ModelExchangeModelDescription instance
    logger::info("modelIdentifier={}", me_fmu->getModelDescription()->modelIdentifier());
    
    auto var = cs_md->getVariableByName("my_var").asRealVariable();
    logger::info("Name={}, start={}", var.name(), var.start().value_or(0));
              
    auto slave = cs_fmu->newInstance();
    
    // or 
    // auto solver = make_solver<RK4ClassicSolver>(1E-3);
    // auto slave = me_fmu->newInstance(solver);
         
    slave->setupExperiment();
    slave->enterInitializationMode();
    slave->exitInitializationMode();
    
    double t;
    double value;
    while ( (t = slave->getSimulationTime()) <= stop) {

        if (!slave->doStep(stepSize)) {
            logger::error("Error! doStep() returned with status: {}", to_string(slave->getLastStatus()));
            break;
        }
        
        if (!var.read(*slave, value)) {
            logger::error("Error! readReal() returned with status: {}", to_string(slave->getLastStatus()));
            break;
        }
        logger::info("t={}, {}={}", t, var.name(), value);
     
    }
    
    slave->terminate();
    
}

fmu_driver

FMI4cpp comes with a simple CLI for testing FMUs, called fmu_driver.

Options:
  -h [ --help ]          Print this help message and quits.
  -f [ --fmu ] arg       Path to FMU.
  -o [ --output ] arg    Where to store the generated CSV results.
  --start arg            Start time.
  --stop arg             Stop time.
  --stepsize arg         StepSize.
  --me                   Treat FMU as an Model Exchange FMU.
  -v [ --variables ] arg Variables to print.

Running examples/tests

In order to run the example/test code, a system variable named TEST_FMUs must be present on your system. This variable should point to the location of the content found here.

The examples are built by default. To change this pass -DFMI4CPP_BUILD_EXAMPLES=OFF to CMake. Test are on the other hand not built by default. To change this pass -DFMI4CPP_BUILD_TESTS=ON to CMake.

To run the tests you will need an additional dependency:

./vcpkg install boost-test

About

FMI 2.0 implementation written in modern C++.

Topics

Resources

License

Stars

Watchers

Forks

Contributors 7