Skip to content

Commit

Permalink
- Compiler/Template/CodegenCpp.mo:
Browse files Browse the repository at this point in the history
  - Add "giveConditions" function to C++ Target again.
  - Add a call to "saveAll()" within Event-Iteration this will make Modelica
    "pre()" Operator act more like discribed in Modelica Specification.
- SimulationRuntime/cpp:
  - moved AlgLoopSolverFactory.h to the more public Include directory.
  - Bug fixes and updates too the FMU C++ Target.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15978 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Matthias Arzt committed May 3, 2013
1 parent 0f970e7 commit ffb8b2c
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 57 deletions.
46 changes: 16 additions & 30 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -278,6 +278,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
<%checkForDiscreteEvents(discreteModelVars,simCode)%>
<%giveZeroFunc1(zeroCrossings,simCode)%>
<%setConditions(simCode)%>
<%giveConditions(simCode)%>
<%isODE(simCode)%>
<%DimZeroFunc(simCode)%>
Expand Down Expand Up @@ -2454,6 +2455,7 @@ case SIMCODE(modelInfo = MODELINFO(__)) then
//Provides current values of root/zero functions
virtual void giveZeroFunc(double* f);
virtual void setConditions(bool* c);
virtual void giveConditions(bool* c);
//Called to check conditions for event-handling
virtual bool checkConditions(const bool* events, bool all);
//Called to handle all events occured at same time
Expand Down Expand Up @@ -6353,25 +6355,22 @@ end checkConditions2;

template handleSystemEvents(list<ZeroCrossing> zeroCrossings,list<SimWhenClause> whenClauses,SimCode simCode)
::=
let &varDecls = buffer "" /*BUFD*/
let zeroCrossingsCode = handleSystemEvents1(zeroCrossings, &varDecls /*BUFD*/, simCode)

match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
void <%lastIdentOfPath(modelInfo.name)%>::handleSystemEvents(bool* events)
{
<%varDecls%>
bool restart=true;
int iter=0;
<%zeroCrossingsCode%>
checkConditions(events,false);
while(restart && !(iter++ > 2*_dimZeroFunc))
{
//iterate and handle all events inside the eventqueue
saveAll();
restart=_event_handling.IterateEventQueue(_conditions);
}
Expand All @@ -6383,28 +6382,6 @@ template handleSystemEvents(list<ZeroCrossing> zeroCrossings,list<SimWhenClause>
>>
end handleSystemEvents;

template handleSystemEvents1(list<ZeroCrossing> zeroCrossings, Text &varDecls /*BUFP*/,SimCode simCode)
::=

(zeroCrossings |> ZERO_CROSSING(__) hasindex i0 =>
handleSystemEvents2(i0, relation_, &varDecls /*BUFD*/,simCode)
;separator="\n";empty)
end handleSystemEvents1;

template handleSystemEvents2(Integer index1, Exp relation, Text &varDecls /*BUFP*/,SimCode simCode)
::=
match relation
case RELATION(index=zerocrossingIndex) then
let &preExp = buffer "" /*BUFD*/
<<
<%preExp%>
if(events[<%zerocrossingIndex%>])
{
checkConditions(events,false);
}
>>
end handleSystemEvents2;

template zeroCrossingOpFunc(Operator op)
"Generates zero crossing function name for operator."
::=
Expand Down Expand Up @@ -6434,9 +6411,6 @@ template giveZeroFunc1(list<ZeroCrossing> zeroCrossings,SimCode simCode)
>>
end giveZeroFunc1;




template setConditions(SimCode simCode)
::=
match simCode
Expand All @@ -6449,6 +6423,18 @@ template setConditions(SimCode simCode)
>>
end setConditions;

template giveConditions(SimCode simCode)
::=
match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
<<
void <%lastIdentOfPath(modelInfo.name)%>::giveConditions(bool* c)
{
memcpy(c,_conditions,_dimZeroFunc*sizeof(bool));
}
>>
end giveConditions;

template saveConditions(SimCode simCode)
::=
match simCode
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/CMakeLists.txt
Expand Up @@ -265,6 +265,8 @@ install (FILES "Include/Core/DataExchange/IHistory.h"
Include/Core/Utils/extension/impl/library_impl.hpp
Include/Core/Utils/extension/impl/shared_library.hpp
Include/Core/Utils/extension/impl/typeinfo.hpp
Include/FMU/fmiModelFunctions.h
Include/FMU/fmiModelTypes.h
Include/FMU/FMUGlobalSettings.h
Include/FMU/IFMUInterface.h
Include/FMU/FMULibInterface.cpp
Expand Down
4 changes: 3 additions & 1 deletion SimulationRuntime/cpp/Core/Solver/CMakeLists.txt
Expand Up @@ -5,7 +5,9 @@ project(${SolverName})
add_library(${SolverName} SHARED SolverDefaultImplementation.cpp SolverSettings.cpp Initialization.cpp )

install (TARGETS ${SolverName} DESTINATION lib/omc/${LIBINSTALLEXT})
install (FILES ${CMAKE_SOURCE_DIR}/Include/Core/Solver/IAlgLoopSolver.h
install (FILES
${CMAKE_SOURCE_DIR}/Include/Core/Solver/IAlgLoopSolver.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/INonLinSolverSettings.h
DESTINATION include/omc/cpp/Core/Solver)


Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Core/System/AlgLoopSolverFactory.cpp
@@ -1,6 +1,6 @@

#include "stdafx.h"
#include "AlgLoopSolverFactory.h"
#include <System/AlgLoopSolverFactory.h>
#include <LibrariesConfig.h>

AlgLoopSolverFactory::AlgLoopSolverFactory(IGlobalSettings& global_settings)
Expand Down
3 changes: 2 additions & 1 deletion SimulationRuntime/cpp/Core/System/CMakeLists.txt
Expand Up @@ -23,7 +23,8 @@ install (FILES
install (FILES
${CMAKE_SOURCE_DIR}/Include/Core/System/IAlgLoop.h
${CMAKE_SOURCE_DIR}/Include/Core/System/IAlgLoopSolverFactory.h
${CMAKE_SOURCE_DIR}/Include/Core/System/IContinuous.h
${CMAKE_SOURCE_DIR}/Include/Core/System/AlgLoopSolverFactory.h
${CMAKE_SOURCE_DIR}/Include/Core/System/IContinuous.h
${CMAKE_SOURCE_DIR}/Include/Core/System/IMixedSystem.h
${CMAKE_SOURCE_DIR}/Include/Core/System/IEvent.h
${CMAKE_SOURCE_DIR}/Include/Core/System/ISystemProperties.h
Expand Down
8 changes: 4 additions & 4 deletions SimulationRuntime/cpp/Core/System/OMCppSystemTypeMap.cpp
Expand Up @@ -2,10 +2,10 @@
#define BOOST_EXTENSION_SYSTEM_DECL BOOST_EXTENSION_EXPORT_DECL
#define BOOST_EXTENSION_ALGLOOPDEFAULTIMPL_DECL BOOST_EXTENSION_EXPORT_DECL

#include <System/SystemDefaultImplementation.h>
#include "AlgLoopSolverFactory.h"
#include <System/EventHandling.h>
#include <System/AlgLoopDefaultImplementation.h>
//#include <System/SystemDefaultImplementation.h>
#include <System/AlgLoopSolverFactory.h>
//#include <System/EventHandling.h>
//#include <System/AlgLoopDefaultImplementation.h>
#include <LibrariesConfig.h>

using boost::extensions::factory;
Expand Down
Expand Up @@ -4,7 +4,7 @@
#define BOOST_EXTENSION_ALGLOOPDEFAULTIMPL_DECL BOOST_EXTENSION_EXPORT_DECL

#include <System/SystemDefaultImplementation.h>
#include "AlgLoopSolverFactory.h"
#include <System/AlgLoopSolverFactory.h>
#include <System/EventHandling.h>
#include <System/AlgLoopDefaultImplementation.h>
#include <LibrariesConfig.h>
Expand Down
Expand Up @@ -5,6 +5,7 @@
#include <Solver/IAlgLoopSolver.h> // Interface for algebraic loops
#include <Solver/INonLinSolverSettings.h>
#include <SimulationSettings/IGlobalSettings.h>
#include <boost/filesystem/path.hpp>
/*****************************************************************************/
/**
Factory used by the system to create a solver for the solution of a (possibly
Expand All @@ -24,6 +25,6 @@ class AlgLoopSolverFactory : public IAlgLoopSolverFactory
//std::vector<boost::shared_ptr<IKinsolSettings> > _algsolversettings;
std::vector<boost::shared_ptr<INonLinSolverSettings> > _algsolversettings;
std::vector<boost::shared_ptr<IAlgLoopSolver> > _algsolvers;
fs::path _libraries_path;
boost::filesystem::path _libraries_path;
IGlobalSettings& _global_settings;
};
52 changes: 45 additions & 7 deletions SimulationRuntime/cpp/Include/FMU/FMULibInterface.cpp
Expand Up @@ -31,34 +31,42 @@

#include "IFMUInterface.h"
#include "FMU/fmiModelFunctions.h"
//#include "FMU/log.hpp"
#include <iostream>

using namespace std;

//#define LOG_FMI() EmptyLog()
//#define DO_LOG_FMI false

// ---------------------------------------------------------------------------
// FMI functions: class methods not depending of a specific model instance
// ---------------------------------------------------------------------------

extern "C" const char* fmiGetModelTypesPlatform() {
//LOG_FMI() << "Entry: fmiGetModelTypesPlatform" << endl;
return fmiModelTypesPlatform;
}

extern "C" const char* fmiGetVersion() {
//LOG_FMI() << "Entry: fmiGetVersion" << endl;
return fmiVersion;
}

extern "C" fmiComponent fmiInstantiateModel(fmiString instanceName, fmiString GUID,
fmiCallbackFunctions functions, fmiBoolean loggingOn)
{
//cout << "fmiInstantiateModel called" << endl;
//LOG_FMI() << "Entry: fmiInstantiateModel" << endl;
return reinterpret_cast<fmiComponent> (OBJECTCONSTRUCTOR);
}

extern "C" fmiStatus fmiSetDebugLogging(fmiComponent c, fmiBoolean loggingOn) {
//LOG_FMI() << "Entry: fmiSetDebugLogging" << endl;
return reinterpret_cast<IFMUInterface*>(c)->setDebugLogging(loggingOn);
}

extern "C" void fmiFreeModelInstance(fmiComponent c) {
//LOG_FMI() << "Entry: fmiModelInstance" << endl;
delete reinterpret_cast<IFMUInterface*>(c);
}

Expand All @@ -67,28 +75,33 @@ extern "C" void fmiFreeModelInstance(fmiComponent c) {
// ---------------------------------------------------------------------------

extern "C" fmiStatus fmiSetReal(fmiComponent c, const fmiValueReference vr[], size_t nvr, const fmiReal value[]){
//LOG_FMI() << "Entry: fmiSetReal nvr=" << nvr << endl;
return reinterpret_cast<IFMUInterface*>(c)->setReal(vr, nvr, value);
}

extern "C" fmiStatus fmiSetInteger(fmiComponent c, const fmiValueReference vr[], size_t nvr, const fmiInteger value[]){
//LOG_FMI() << "Entry: fmiSetInteger nvr=" << nvr << endl;
return reinterpret_cast<IFMUInterface*>(c)->setInteger(vr, nvr, value);
}

extern "C" fmiStatus fmiSetBoolean(fmiComponent c, const fmiValueReference vr[], size_t nvr, const fmiBoolean value[]){
//LOG_FMI() << "Entry: fmiBoolean nvr=" << nvr << endl;
return reinterpret_cast<IFMUInterface*>(c)->setBoolean(vr, nvr, value);
}

extern "C" fmiStatus fmiSetString(fmiComponent c, const fmiValueReference vr[], size_t nvr, const fmiString value[]){
//LOG_FMI() << "Entry: fmiString nvr=" << nvr << endl;
return reinterpret_cast<IFMUInterface*>(c)->setString(vr, nvr, value);
}

extern "C" fmiStatus fmiSetTime(fmiComponent c, fmiReal time) {
//cout << "setTime is called: fmiComponent=" << c << " time=" << time << endl;
//LOG_FMI() << "Entry: **fmiSetTime is called: fmiComponent=" << c << " time=" << time << endl;
if(!c) return fmiFatal; // TODO OpenModelica produces this Error if loading an FMI Model.
return reinterpret_cast<IFMUInterface*>(c)->setTime(time);
}

extern "C" fmiStatus fmiSetContinuousStates(fmiComponent c, const fmiReal x[], size_t nx){
//LOG_FMI() << "Entry: **fmiSetContinuousState nx=" << nx << endl;
return reinterpret_cast<IFMUInterface*>(c)->setContinuousStates(x, nx);
}

Expand All @@ -97,59 +110,83 @@ extern "C" fmiStatus fmiSetContinuousStates(fmiComponent c, const fmiReal x[], s
// ---------------------------------------------------------------------------

extern "C" fmiStatus fmiGetReal(fmiComponent c, const fmiValueReference vr[], size_t nvr, fmiReal value[]) {
//LOG_FMI() << "Entry: fmiGetReal nvr=" << nvr << "value references=" << printArray(vr, nvr, " ") << endl;
return reinterpret_cast<IFMUInterface*>(c)->getReal(vr, nvr, value);
}

extern "C" fmiStatus fmiGetInteger(fmiComponent c, const fmiValueReference vr[], size_t nvr, fmiInteger value[]) {
//LOG_FMI() << "Entry: fmiGetInteger nvr=" << nvr << "value references=" << printArray(vr, nvr, " ") << endl;
return reinterpret_cast<IFMUInterface*>(c)->getInteger(vr, nvr, value);
}

extern "C" fmiStatus fmiGetBoolean(fmiComponent c, const fmiValueReference vr[], size_t nvr, fmiBoolean value[]) {
//LOG_FMI() << "Entry: fmiGetBoolean nvr=" << nvr << "value references=" << printArray(vr, nvr, " ") << endl;
return reinterpret_cast<IFMUInterface*>(c)->getBoolean(vr, nvr, value);
}

extern "C" fmiStatus fmiGetString(fmiComponent c, const fmiValueReference vr[], size_t nvr, fmiString value[]) {
//LOG_FMI() << "Entry: fmiString nvr=" << nvr << "value references=" << printArray(vr, nvr, " ") << endl;
return reinterpret_cast<IFMUInterface*>(c)->getString(vr, nvr, value);
}

extern "C" fmiStatus fmiGetStateValueReferences(fmiComponent c, fmiValueReference vrx[], size_t nx){
//LOG_FMI() << "Entry: fmiGetStateValueReferences nx=" << nx << endl;
return reinterpret_cast<IFMUInterface*>(c)->getStateValueReferences(vrx, nx);
}

extern "C" fmiStatus fmiGetContinuousStates(fmiComponent c, fmiReal states[], size_t nx){
//LOG_FMI() << "Entry: fmiGetContinuousStates nx=" << nx << endl;
return reinterpret_cast<IFMUInterface*>(c)->getContinuousStates(states, nx);
}

extern "C" fmiStatus fmiGetNominalContinuousStates(fmiComponent c, fmiReal x_nominal[], size_t nx){
//LOG_FMI() << "Entry: fmiGetNominalContinuousStates" << endl;
return reinterpret_cast<IFMUInterface*>(c)->getNominalContinuousStates(x_nominal, nx);
}

extern "C" fmiStatus fmiGetDerivatives(fmiComponent c, fmiReal derivatives[], size_t nx) {
//LOG_FMI() << "Entry: fmiGetDerivates nx = " << nx << endl;
return reinterpret_cast<IFMUInterface*>(c)->getDerivatives(derivatives, nx);
}

extern "C" fmiStatus fmiGetEventIndicators(fmiComponent c, fmiReal eventIndicators[], size_t ni) {
return reinterpret_cast<IFMUInterface*>(c)->getEventIndicators(eventIndicators, ni);
//if(DO_LOG_FMI)
//{
// LOG_FMI() << "Entry: fmiGetEventIndicators ni=" << ni << endl;
// fmiStatus result = reinterpret_cast<IFMUInterface*>(c)->getEventIndicators(eventIndicators, ni);
// LOG_FMI() << "Return values eventIndicators =" << printArray(eventIndicators, ni, " ") << endl;
// return result;
//}
//else
//{
return reinterpret_cast<IFMUInterface*>(c)->getEventIndicators(eventIndicators, ni);
//}
}

// ---------------------------------------------------------------------------
// FMI functions: initialization, event handling, stepping and termination
// ---------------------------------------------------------------------------

extern "C" fmiStatus fmiInitialize(fmiComponent c, fmiBoolean toleranceControlled, fmiReal relativeTolerance,
fmiEventInfo* eventInfo) {
return reinterpret_cast<IFMUInterface*>(c)->initialize(toleranceControlled, relativeTolerance, *eventInfo);
extern "C" fmiStatus fmiInitialize(fmiComponent c, fmiBoolean toleranceControlled,
fmiReal relativeTolerance, fmiEventInfo* eventInfo)
{
//LOG_FMI() << "Entry: fmiInitialize" << endl;
return reinterpret_cast<IFMUInterface*>(c)->initialize(toleranceControlled,
relativeTolerance, *eventInfo);
}

extern "C" fmiStatus fmiEventUpdate(fmiComponent c, fmiBoolean intermediateResults, fmiEventInfo* eventInfo) {
//LOG_FMI() << "Entry: ****fmiEventUpdate" << endl;
return reinterpret_cast<IFMUInterface*>(c)->eventUpdate(intermediateResults, *eventInfo);
}

extern "C" fmiStatus fmiCompletedIntegratorStep(fmiComponent c, fmiBoolean* callEventUpdate){
return reinterpret_cast<IFMUInterface*>(c)->completedIntegratorStep(callEventUpdate);
//LOG_FMI() << "Entry: ***fmiCompletedIntegratorStep" << endl;
return reinterpret_cast<IFMUInterface*>(c)->completedIntegratorStep(*callEventUpdate);
}

extern "C" fmiStatus fmiTerminate(fmiComponent c){
//LOG_FMI() << "Entry: fmiTerminate" << endl;
return reinterpret_cast<IFMUInterface*>(c)->terminate();
}

Expand All @@ -158,6 +195,7 @@ extern "C" fmiStatus fmiTerminate(fmiComponent c){
// ---------------------------------------------------------------------------

extern "C" fmiStatus fmiSetExternalFunction(fmiComponent c, fmiValueReference vr[], size_t nvr, const void* value[]) {
//LOG_FMI() << "Entry: fmiSetExternalFunction" << endl;
return reinterpret_cast<IFMUInterface*>(c)->setExternalFunction(vr, nvr, value);
}

0 comments on commit ffb8b2c

Please sign in to comment.