Skip to content
FMI 2.0 implementation written in modern C++.
Branch: master
Clone or download
markaren Merge pull request #67 from NTNU-IHB/fix/42-canBeInstantiatedOnlyOnce…
…PerProcess-handling

Fix/42 can be instantiated_only_once_per_process handling
Latest commit 5ba6137 Jun 13, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
cmake link openssl on unix if available May 25, 2019
examples
include/fmi4cpp use boost/optional in public headers Apr 16, 2019
resources/fmus/2.0 add me Test-FMUs Mar 15, 2019
src Merge pull request #67 from NTNU-IHB/fix/42-canBeInstantiatedOnlyOnce… Jun 13, 2019
tests
tool snake_case and rename function Mar 27, 2019
.gitignore update gitignore Jan 9, 2019
BUILDING.md mostly done refactoring Mar 21, 2019
CMakeLists.txt update logging Mar 23, 2019
INSTALL.md mostly done refactoring Mar 21, 2019
LICENSE update license Sep 19, 2018
README.md Fix another error in the README code [skip ci] May 29, 2019
azure-pipelines.yml re-enable curl on azure vcpkg May 25, 2019
conanfile.py
run_conan_install.bat add conan install script for windows Mar 13, 2019
run_conan_install.sh Update conan stuff Feb 13, 2019

README.md

FMI4cpp (work in progress)

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

CircleCI Build status

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 it provides a clean, easy to use API and is easy to install (through vcpkg). It perform just as good as FMI Library, provides more features and is significantly easier to both build and use.

Install instructions

Refer to INSTALL.md

Build instructions

Refer to BUILDING.md

API

#include <iostream> 
#include <fmi4cpp/fmi4cpp.hpp>

using namespace std;
using namespace fmi4cpp;

const double stop = ...;
const double stepSize = ...;

int main() {

    fmi2::fmu fmu("path/to/fmu.fmu");
    // or (with -DFMI4CPP_WITH_CURL=ON)
    // auto fmu = fmi2::fmu::from_url("http://somewebsite.org/somefmu.fmu")
    
    auto cs_fmu = fmu.as_cs_fmu();
    auto me_fmu = fmu.as_me_fmu();
    
    auto cs_md = cs_fmu->get_model_description(); //smart pointer to a cs_model_description instance
    cout << "model_identifier=" << cs_md->model_identifier << endl;
    
    auto me_md = me_fmu->get_model_description(); //smart pointer to a me_model_description instance
    cout << "model_identifier=" << me_md->model_identifier << endl;
    
    auto var = cs_md->get_variable_by_name("my_var").asReal();
    cout << "Name=" << var.name() <<  ", start=" << var.start().value_or(0) << endl;
              
    auto slave = cs_fmu->new_instance();
    
    // or (with -DFMI4CPP_WITH_ODEINT=ON)
    // auto solver = make_solver<rk4_classic_solver>(1E-3);
    // auto slave = me_fmu->new_instance(solver);
         
    slave->setup_experiment();
    slave->enter_initialization_mode();
    slave->exit_initialization_mode();
    
    double t;
    double value;
    while ( (t = slave->get_simulation_time()) <= stop) {

        if (!slave->step(stepSize)) {
            cerr << "Error! step() returned with status: " << to_string(slave->last_status()) << endl;
            break;
        }
        
        if (!var.read(*slave, value)) {
            cerr << "Error! step() returned with status: " << to_string(slave->last_status()) << endl;
            break;
        }
        cout << "t=" << t << ", " << var.name() << "=" << value << endl;
     
    }
    
    slave->terminate();
    
    return 0;
    
}

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.
You can’t perform that action at this time.