Skip to content

Commit

Permalink
- fix in cpp runtime for division, initialization of CVode
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12909 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed Sep 13, 2012
1 parent 278f3c0 commit 46a41ff
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 36 deletions.
20 changes: 12 additions & 8 deletions Compiler/Template/CodegenCpp.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ FUNCTIONFILE=Functions.cpp

.PHONY: <%lastIdentOfPath(modelInfo.name)%>
<%lastIdentOfPath(modelInfo.name)%>: $(MAINFILE)
<%\t%>$(CXX) -shared -I. -o $(MODELICA_SYSTEM_LIB) $(MAINFILE) $(FUNCTIONFILE) <%algloopcppfilenames(allEquations,simCode)%> $(CFLAGS) $(LDFLAGS) -lOMCppSystem -lOMCppModelicaExternalC -Wl,-Bstatic -Wl,-Bdynamic
<%\t%>$(CXX) -shared -I. -o $(MODELICA_SYSTEM_LIB) $(MAINFILE) $(FUNCTIONFILE) <%algloopcppfilenames(allEquations,simCode)%> $(CFLAGS) $(LDFLAGS) -lOMCppSystem -lOMCppMath -lOMCppModelicaExternalC -Wl,-Bstatic -Wl,-Bdynamic

>>
end simulationMakefile;
Expand Down Expand Up @@ -6053,11 +6053,13 @@ template resetTimeEvents(list<SampleCondition> sampleConditions,list<SimWhenClau
::=
match simCode
case SIMCODE(sampleConditions=sam) then
let helpvars = match helpVarInfo case _::_ then 'double h[<%helpvarlength(simCode)%>];
<%helpvarvector(whenClauses,simCode)%>
_event_handling.setHelpVars(h);'

<<
<%resetTimeEvent(sampleConditions,simCode)%>
double h[<%helpvarlength(simCode)%>];
<%helpvarvector(whenClauses,simCode)%>
_event_handling.setHelpVars(h);
<%helpvars%>

>>
end resetTimeEvents;

Expand Down Expand Up @@ -6162,8 +6164,12 @@ template handleSystemEvents(list<ZeroCrossing> zeroCrossings,list<SimWhenClause>
::=
let &varDecls = buffer "" /*BUFD*/
let zeroCrossingsCode = handleSystemEvents1(zeroCrossings, &varDecls /*BUFD*/, simCode)

match simCode
case SIMCODE(modelInfo = MODELINFO(__)) then
let helpvars = match helpVarInfo case _::_ then 'double h[<%helpvarlength(simCode)%>];
<%helpvarvector(whenClauses,simCode)%>
_event_handling.setHelpVars(h);'
<<
void <%lastIdentOfPath(modelInfo.name)%>::handleSystemEvents(const bool* events)
{
Expand All @@ -6177,9 +6183,7 @@ template handleSystemEvents(list<ZeroCrossing> zeroCrossings,list<SimWhenClause>
while(restart && !(iter++ > 15))
{
double h[<%helpvarlength(simCode)%>];
<%helpvarvector(whenClauses,simCode)%>
_event_handling.setHelpVars(h);
<%helpvars%>
//iterate and handle all events inside the eventqueue
restart=_event_handling.IterateEventQueue(_conditions1);
saveAll();
Expand Down
10 changes: 7 additions & 3 deletions SimulationRuntime/cpp/Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,16 @@ GET_FILENAME_COMPONENT(libSystemName ${libSystem} NAME)
GET_TARGET_PROPERTY(libSolver ${SolverName} LOCATION)
GET_FILENAME_COMPONENT(libSolverName ${libSolver} NAME)

GET_TARGET_PROPERTY(libMath ${MathName} LOCATION)
GET_FILENAME_COMPONENT(libMathName ${libMath} NAME)

set (EULER_LIB ${libEulerName})
set (SETTINGSFACTORY_LIB ${libSetFactoryName})
set (MODELICASYSTEM_LIB ${libModelicaName})
set (NEWTON_LIB ${libNewtonName})
set (SYSTEM_LIB ${libSystemName})
set (SOLVER_LIB ${libSolverName})
set (MATH_LIB ${libMathName})
# configure a header file to pass some library names
# to the source code which are used to dynamic load and instantiate

Expand Down Expand Up @@ -223,9 +227,9 @@ install (FILES "DataExchange/Interfaces/IHistory.h"

#copy Utils/extension
SET(HS Utils/extension/adaptable_factory.hpp Utils/extension/common.hpp Utils/extension/convenience.hpp
Utils/extension/extension.hpp Utils/extension/factory.hpp Utils/extension/factory_map.hpp Utils/extension/filesystem.hpp
Utils/extension/parameter.hpp Utils/extension/parameter_map.hpp Utils/extension/registry.hpp Utils/extension/shared_library.hpp
Utils/extension/type_map.hpp
Utils/extension/extension.hpp Utils/extension/factory.hpp Utils/extension/factory_map.hpp Utils/extension/filesystem.hpp
Utils/extension/parameter.hpp Utils/extension/parameter_map.hpp Utils/extension/registry.hpp Utils/extension/shared_library.hpp
Utils/extension/type_map.hpp
Utils/extension/impl/adaptable_factory.hpp Utils/extension/impl/adaptable_factory_free_functions.hpp Utils/extension/impl/adaptable_factory_set.hpp
Utils/extension/impl/create.hpp Utils/extension/impl/create_func.hpp Utils/extension/impl/decl.hpp
Utils/extension/impl/factory.hpp Utils/extension/impl/factory_map.hpp Utils/extension/impl/function.hpp Utils/extension/impl/library_impl.hpp
Expand Down
3 changes: 2 additions & 1 deletion SimulationRuntime/cpp/Source/LibrariesConfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
#define MODELICASYSTEM_LIB "@MODELICASYSTEM_LIB@"
#define NEWTON_LIB "@NEWTON_LIB@"
#define SYSTEM_LIB "@SYSTEM_LIB@"
#define SOLVER_LIB "@SOLVER_LIB@"
#define SOLVER_LIB "@SOLVER_LIB@"
#define MATH_LIB "@MATH_LIB@"
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ cmake_minimum_required (VERSION 2.6)

project(${MathName})
# add the solver default implementation library
add_library(${MathName} SHARED ArrayOperations.cpp )
add_library(${MathName} SHARED ArrayOperations.cpp Functions.cpp )


install (TARGETS ${MathName} DESTINATION lib/omc)
install (FILES Functions.h
ArrayOperations.h
Utility.h
Utility.h
DESTINATION include/omc/cpp/Math/Implementation)
install (FILES ../Interfaces/ILapack.h
DESTINATION include/omc/cpp/Math/Interfaces)
../Interfaces/OMAPI.h
DESTINATION include/omc/cpp/Math/Interfaces)
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#include "stdafx.h"
#include "Functions.h"
#include <stdexcept>


double division (const double &a,const double &b,std::string text)
{
if(b != 0)
return a/b ;
else
throw std::invalid_argument(text);
}
10 changes: 5 additions & 5 deletions SimulationRuntime/cpp/Source/Math/Implementation/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <stdlib.h>
#include "Math/Interfaces/ILapack.h" ///< For the use of DGESV, etc.
#include <limits>
#include <exception>
#include <string>
#include "Math/Interfaces/OMAPI.h" ///< For the use of DGESV, etc.
/*****************************************************************************/
/**
Expand All @@ -26,11 +29,8 @@ inline static int sgn (const double &c)
}

/// Definition of Signum function
inline static double division (const double &a,const double &b,string text)
{
return (b != 0) ? a/b : throw std::invalid_argument(text);

}
OMC_API double division (const double &a,const double &b,std::string text);



/// Provides the maximum Norm
Expand Down
23 changes: 23 additions & 0 deletions SimulationRuntime/cpp/Source/Math/Interfaces/OMAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if !defined(OMC_INTERFACE)
#define OMC_INTERFACE

#if (defined _MSC_VER) && _MSC_VER<1300
#pragma warning (disable : 4786)
#endif

#if defined( _WIN32 ) && defined( _MSC_VER )
# if defined OMC_EXPORTS
# define OMC_API __declspec( dllexport )
# else
# define OMC_API __declspec( dllimport )
# pragma warning( disable : 4251 ) // 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
# endif
#endif

#if !defined(OMC_API)
#define OMC_API
#endif

#endif

#pragma warning( disable : 4297 ) // Issue warning 4297
17 changes: 16 additions & 1 deletion SimulationRuntime/cpp/Source/SimManager/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,17 @@ int main(int argc, const char* argv[])
fs::path default_system_path = libraries_path;
default_system_path/=default_system_name;



default_system_path.make_preferred();
modelica_system_path.make_preferred();

type_map types;
if(!load_single_library(types, default_system_path.string()))
throw std::invalid_argument("System default library could not be loaded");




if(!load_single_library(types, modelica_system_path.string()))
throw std::invalid_argument("ModelicaSystem library could not be loaded");
Expand Down Expand Up @@ -157,7 +163,16 @@ int main(int argc, const char* argv[])
solver->setEndTime(global_settings->getEndTime());
solver->setInitStepSize(config.getSolverSettings()->gethInit());
// Call the solver
try
{
solver->solve(command);
}
catch(std::exception& ex)
{
std::string error = ex.what();
cerr << "Simulation error: "<< error ;
//return 1;
}
// Get the status of the solver (is the interation done sucessfully?)
IDAESolver::SOLVERSTATUS status = solver->getSolverStatus();
//Todo: use flags for simulation outputs
Expand All @@ -172,5 +187,5 @@ int main(int argc, const char* argv[])
cerr << "Simulation stopped: "<< error ;
return 1;
}

}
28 changes: 15 additions & 13 deletions SimulationRuntime/cpp/Source/Solver/CVode/Implementation/CVode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ Cvode::~Cvode()
delete [] _zeroInit;
if(_Cond)
delete [] _Cond;

N_VDestroy_Serial(_CV_y0);
N_VDestroy_Serial(_CV_y);
N_VDestroy_Serial(_CV_yWrite);
Expand All @@ -86,10 +85,21 @@ void Cvode::init()
// 1) System assemblen und updaten, alles für Nullstellsuche anlegen
// 2) Spezielle Dimensionen bestimmen (muss wg. ODE/DAE im Solver stattfinden)
// 3) Zustandsvektor anlegen
_dimSys = continous_system->getDimVars(IContinous::ALL_STATES);
_dimZeroFunc = event_system->getDimZeroFunc();
_CV_y0 = N_VMake_Serial(_dimSys, _zInit);
_CV_y = N_VMake_Serial(_dimSys, _z);
_CV_yWrite = N_VMake_Serial(_dimSys, _zWrite);
// Allocate memory for the solver
_cvodeMem = CVodeCreate(CV_BDF, CV_NEWTON);
if(check_flag((void*)_cvodeMem, "CVodeCreate", 0))
{
_idid = -5;
throw std::invalid_argument(/*_idid,_tCurrent,*/"Cvode::init()");
}
SolverDefaultImplementation::init();

_dimSys = continous_system->getDimVars(IContinous::ALL_STATES);
_dimZeroFunc = event_system->getDimZeroFunc();


int dimAEq = continous_system->getDimVars(IContinous::DIFF_INDEX3);

Expand Down Expand Up @@ -146,13 +156,7 @@ void Cvode::init()

}

// Allocate memory for the solver
_cvodeMem = CVodeCreate(CV_BDF, CV_NEWTON);
if(check_flag((void*)_cvodeMem, "CVodeCreate", 0))
{
_idid = -5;
throw std::invalid_argument(/*_idid,_tCurrent,*/"Cvode::init()");
}


//
// Make Cvode ready for integration
Expand All @@ -166,9 +170,7 @@ void Cvode::init()
continous_system->giveVars(_zInit);
memcpy(_z,_zInit,_dimSys*sizeof(double));

_CV_y0 = N_VMake_Serial(_dimSys, _zInit);
_CV_y = N_VMake_Serial(_dimSys, _z);
_CV_yWrite = N_VMake_Serial(_dimSys, _zWrite);

if(check_flag((void*)_CV_y0, "N_VMake_Serial", 0))
{
_idid = -5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(${ModelicaName})

add_library(${ModelicaName} SHARED ModelicaSystem.cpp )

target_link_libraries (${ModelicaName} ${SystemName} ${ModelicaExternalCName})
target_link_libraries (${ModelicaName} ${SystemName} ${ModelicaExternalCName} ${MathName})


GET_TARGET_PROPERTY(libModelicaSystem ${ModelicaName} LOCATION)
Expand Down

0 comments on commit 46a41ff

Please sign in to comment.