|
| 1 | +/* |
| 2 | + * This file is part of OpenModelica. |
| 3 | + * |
| 4 | + * Copyright (c) 1998-CurrentYear, Linköping University, |
| 5 | + * Department of Computer and Information Science, |
| 6 | + * SE-58183 Linköping, Sweden. |
| 7 | + * |
| 8 | + * All rights reserved. |
| 9 | + * |
| 10 | + * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 |
| 11 | + * AND THIS OSMC PUBLIC LICENSE (OSMC-PL). |
| 12 | + * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S |
| 13 | + * ACCEPTANCE OF THE OSMC PUBLIC LICENSE. |
| 14 | + * |
| 15 | + * The OpenModelica software and the Open Source Modelica |
| 16 | + * Consortium (OSMC) Public License (OSMC-PL) are obtained |
| 17 | + * from Linköping University, either from the above address, |
| 18 | + * from the URLs: http://www.ida.liu.se/projects/OpenModelica or |
| 19 | + * http://www.openmodelica.org, and in the OpenModelica distribution. |
| 20 | + * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html. |
| 21 | + * |
| 22 | + * This program is distributed WITHOUT ANY WARRANTY; without |
| 23 | + * even the implied warranty of MERCHANTABILITY or FITNESS |
| 24 | + * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH |
| 25 | + * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS |
| 26 | + * OF OSMC-PL. |
| 27 | + * |
| 28 | + * See the full OSMC Public License conditions for more details. |
| 29 | + * |
| 30 | + */ |
| 31 | + |
| 32 | +#include <FMU/IFMUInterface.h> |
| 33 | +#include <FMU/fmiModelFunctions.h> |
| 34 | +#include <FMU/FMUWrapper.h> |
| 35 | +//#include "FMU/log.hpp" |
| 36 | +#include <iostream> |
| 37 | + |
| 38 | +using namespace std; |
| 39 | + |
| 40 | +//#define LOG_FMI() EmptyLog() |
| 41 | +//#define DO_LOG_FMI false |
| 42 | + |
| 43 | +// --------------------------------------------------------------------------- |
| 44 | +// FMI functions: class methods not depending of a specific model instance |
| 45 | +// --------------------------------------------------------------------------- |
| 46 | + |
| 47 | +extern "C" const char* fmiGetModelTypesPlatform() { |
| 48 | + //LOG_FMI() << "Entry: fmiGetModelTypesPlatform" << endl; |
| 49 | + return fmiModelTypesPlatform; |
| 50 | +} |
| 51 | + |
| 52 | +extern "C" const char* fmiGetVersion() { |
| 53 | + //LOG_FMI() << "Entry: fmiGetVersion" << endl; |
| 54 | + return fmiVersion; |
| 55 | +} |
| 56 | + |
| 57 | +extern "C" fmiComponent fmiInstantiateModel(fmiString instanceName, fmiString GUID, |
| 58 | + fmiCallbackFunctions functions, fmiBoolean loggingOn) |
| 59 | +{ |
| 60 | + //LOG_FMI() << "Entry: fmiInstantiateModel" << endl; |
| 61 | + return reinterpret_cast<fmiComponent> |
| 62 | + (new FMUWrapper(instanceName, GUID, functions, loggingOn)); |
| 63 | +} |
| 64 | + |
| 65 | +extern "C" fmiStatus fmiSetDebugLogging(fmiComponent c, fmiBoolean loggingOn) { |
| 66 | + //LOG_FMI() << "Entry: fmiSetDebugLogging" << endl; |
| 67 | + return reinterpret_cast<IFMUInterface*>(c)->setDebugLogging(loggingOn); |
| 68 | +} |
| 69 | + |
| 70 | +extern "C" void fmiFreeModelInstance(fmiComponent c) { |
| 71 | + //LOG_FMI() << "Entry: fmiModelInstance" << endl; |
| 72 | + delete reinterpret_cast<IFMUInterface*>(c); |
| 73 | +} |
| 74 | + |
| 75 | +// --------------------------------------------------------------------------- |
| 76 | +// FMI functions: set variable values in the FMU |
| 77 | +// --------------------------------------------------------------------------- |
| 78 | + |
| 79 | +extern "C" fmiStatus fmiSetReal(fmiComponent c, const fmiValueReference vr[], size_t nvr, const fmiReal value[]){ |
| 80 | + //LOG_FMI() << "Entry: fmiSetReal nvr=" << nvr << endl; |
| 81 | + return reinterpret_cast<IFMUInterface*>(c)->setReal(vr, nvr, value); |
| 82 | +} |
| 83 | + |
| 84 | +extern "C" fmiStatus fmiSetInteger(fmiComponent c, const fmiValueReference vr[], size_t nvr, const fmiInteger value[]){ |
| 85 | + //LOG_FMI() << "Entry: fmiSetInteger nvr=" << nvr << endl; |
| 86 | + return reinterpret_cast<IFMUInterface*>(c)->setInteger(vr, nvr, value); |
| 87 | +} |
| 88 | + |
| 89 | +extern "C" fmiStatus fmiSetBoolean(fmiComponent c, const fmiValueReference vr[], size_t nvr, const fmiBoolean value[]){ |
| 90 | + //LOG_FMI() << "Entry: fmiBoolean nvr=" << nvr << endl; |
| 91 | + return reinterpret_cast<IFMUInterface*>(c)->setBoolean(vr, nvr, value); |
| 92 | +} |
| 93 | + |
| 94 | +extern "C" fmiStatus fmiSetString(fmiComponent c, const fmiValueReference vr[], size_t nvr, const fmiString value[]){ |
| 95 | + //LOG_FMI() << "Entry: fmiString nvr=" << nvr << endl; |
| 96 | + return reinterpret_cast<IFMUInterface*>(c)->setString(vr, nvr, value); |
| 97 | +} |
| 98 | + |
| 99 | +extern "C" fmiStatus fmiSetTime(fmiComponent c, fmiReal time) { |
| 100 | + //LOG_FMI() << "Entry: **fmiSetTime is called: fmiComponent=" << c << " time=" << time << endl; |
| 101 | + if(!c) return fmiFatal; // TODO OpenModelica produces this Error if loading an FMI Model. |
| 102 | + return reinterpret_cast<IFMUInterface*>(c)->setTime(time); |
| 103 | +} |
| 104 | + |
| 105 | +extern "C" fmiStatus fmiSetContinuousStates(fmiComponent c, const fmiReal x[], size_t nx){ |
| 106 | + //LOG_FMI() << "Entry: **fmiSetContinuousState nx=" << nx << endl; |
| 107 | + return reinterpret_cast<IFMUInterface*>(c)->setContinuousStates(x, nx); |
| 108 | +} |
| 109 | + |
| 110 | +// --------------------------------------------------------------------------- |
| 111 | +// FMI functions: get variable values from the FMU |
| 112 | +// --------------------------------------------------------------------------- |
| 113 | + |
| 114 | +extern "C" fmiStatus fmiGetReal(fmiComponent c, const fmiValueReference vr[], size_t nvr, fmiReal value[]) { |
| 115 | + //LOG_FMI() << "Entry: fmiGetReal nvr=" << nvr << "value references=" << printArray(vr, nvr, " ") << endl; |
| 116 | + return reinterpret_cast<IFMUInterface*>(c)->getReal(vr, nvr, value); |
| 117 | +} |
| 118 | + |
| 119 | +extern "C" fmiStatus fmiGetInteger(fmiComponent c, const fmiValueReference vr[], size_t nvr, fmiInteger value[]) { |
| 120 | + //LOG_FMI() << "Entry: fmiGetInteger nvr=" << nvr << "value references=" << printArray(vr, nvr, " ") << endl; |
| 121 | + return reinterpret_cast<IFMUInterface*>(c)->getInteger(vr, nvr, value); |
| 122 | +} |
| 123 | + |
| 124 | +extern "C" fmiStatus fmiGetBoolean(fmiComponent c, const fmiValueReference vr[], size_t nvr, fmiBoolean value[]) { |
| 125 | + //LOG_FMI() << "Entry: fmiGetBoolean nvr=" << nvr << "value references=" << printArray(vr, nvr, " ") << endl; |
| 126 | + return reinterpret_cast<IFMUInterface*>(c)->getBoolean(vr, nvr, value); |
| 127 | +} |
| 128 | + |
| 129 | +extern "C" fmiStatus fmiGetString(fmiComponent c, const fmiValueReference vr[], size_t nvr, fmiString value[]) { |
| 130 | + //LOG_FMI() << "Entry: fmiString nvr=" << nvr << "value references=" << printArray(vr, nvr, " ") << endl; |
| 131 | + return reinterpret_cast<IFMUInterface*>(c)->getString(vr, nvr, value); |
| 132 | +} |
| 133 | + |
| 134 | +extern "C" fmiStatus fmiGetStateValueReferences(fmiComponent c, fmiValueReference vrx[], size_t nx){ |
| 135 | + //LOG_FMI() << "Entry: fmiGetStateValueReferences nx=" << nx << endl; |
| 136 | + return reinterpret_cast<IFMUInterface*>(c)->getStateValueReferences(vrx, nx); |
| 137 | +} |
| 138 | + |
| 139 | +extern "C" fmiStatus fmiGetContinuousStates(fmiComponent c, fmiReal states[], size_t nx){ |
| 140 | + //LOG_FMI() << "Entry: fmiGetContinuousStates nx=" << nx << endl; |
| 141 | + return reinterpret_cast<IFMUInterface*>(c)->getContinuousStates(states, nx); |
| 142 | +} |
| 143 | + |
| 144 | +extern "C" fmiStatus fmiGetNominalContinuousStates(fmiComponent c, fmiReal x_nominal[], size_t nx){ |
| 145 | + //LOG_FMI() << "Entry: fmiGetNominalContinuousStates" << endl; |
| 146 | + return reinterpret_cast<IFMUInterface*>(c)->getNominalContinuousStates(x_nominal, nx); |
| 147 | +} |
| 148 | + |
| 149 | +extern "C" fmiStatus fmiGetDerivatives(fmiComponent c, fmiReal derivatives[], size_t nx) { |
| 150 | + //LOG_FMI() << "Entry: fmiGetDerivates nx = " << nx << endl; |
| 151 | + return reinterpret_cast<IFMUInterface*>(c)->getDerivatives(derivatives, nx); |
| 152 | +} |
| 153 | + |
| 154 | +extern "C" fmiStatus fmiGetEventIndicators(fmiComponent c, fmiReal eventIndicators[], size_t ni) { |
| 155 | + //if(DO_LOG_FMI) |
| 156 | + //{ |
| 157 | + // LOG_FMI() << "Entry: fmiGetEventIndicators ni=" << ni << endl; |
| 158 | + // fmiStatus result = reinterpret_cast<IFMUInterface*>(c)->getEventIndicators(eventIndicators, ni); |
| 159 | + // LOG_FMI() << "Return values eventIndicators =" << printArray(eventIndicators, ni, " ") << endl; |
| 160 | + // return result; |
| 161 | + //} |
| 162 | + //else |
| 163 | + //{ |
| 164 | + return reinterpret_cast<IFMUInterface*>(c)->getEventIndicators(eventIndicators, ni); |
| 165 | + //} |
| 166 | +} |
| 167 | + |
| 168 | +// --------------------------------------------------------------------------- |
| 169 | +// FMI functions: initialization, event handling, stepping and termination |
| 170 | +// --------------------------------------------------------------------------- |
| 171 | + |
| 172 | +extern "C" fmiStatus fmiInitialize(fmiComponent c, fmiBoolean toleranceControlled, |
| 173 | + fmiReal relativeTolerance, fmiEventInfo* eventInfo) |
| 174 | +{ |
| 175 | + //LOG_FMI() << "Entry: fmiInitialize" << endl; |
| 176 | + return reinterpret_cast<IFMUInterface*>(c)->initialize(toleranceControlled, |
| 177 | + relativeTolerance, *eventInfo); |
| 178 | +} |
| 179 | + |
| 180 | +extern "C" fmiStatus fmiEventUpdate(fmiComponent c, fmiBoolean intermediateResults, fmiEventInfo* eventInfo) { |
| 181 | + //LOG_FMI() << "Entry: ****fmiEventUpdate" << endl; |
| 182 | + return reinterpret_cast<IFMUInterface*>(c)->eventUpdate(intermediateResults, *eventInfo); |
| 183 | +} |
| 184 | + |
| 185 | +extern "C" fmiStatus fmiCompletedIntegratorStep(fmiComponent c, fmiBoolean* callEventUpdate){ |
| 186 | + //LOG_FMI() << "Entry: ***fmiCompletedIntegratorStep" << endl; |
| 187 | + return reinterpret_cast<IFMUInterface*>(c)->completedIntegratorStep(*callEventUpdate); |
| 188 | +} |
| 189 | + |
| 190 | +extern "C" fmiStatus fmiTerminate(fmiComponent c){ |
| 191 | + //LOG_FMI() << "Entry: fmiTerminate" << endl; |
| 192 | + return reinterpret_cast<IFMUInterface*>(c)->terminate(); |
| 193 | +} |
| 194 | + |
| 195 | +// --------------------------------------------------------------------------- |
| 196 | +// FMI functions: set external functions |
| 197 | +// --------------------------------------------------------------------------- |
| 198 | + |
| 199 | +extern "C" fmiStatus fmiSetExternalFunction(fmiComponent c, fmiValueReference vr[], size_t nvr, const void* value[]) { |
| 200 | + //LOG_FMI() << "Entry: fmiSetExternalFunction" << endl; |
| 201 | + return reinterpret_cast<IFMUInterface*>(c)->setExternalFunction(vr, nvr, value); |
| 202 | +} |
| 203 | + |
0 commit comments