Skip to content

Commit

Permalink
- Kinsol can now be used for FMUs, by passing a parameter to cmake
Browse files Browse the repository at this point in the history
- added some logger output to the kinsol and newton solver
  • Loading branch information
Marcus Walther authored and OpenModelica-Hudson committed Jul 27, 2015
1 parent 810476f commit d9b26e6
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 29 deletions.
21 changes: 14 additions & 7 deletions Compiler/Template/CodegenFMUCpp.tpl
Expand Up @@ -737,13 +737,16 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula

CALCHELPERMAINFILE=OMCpp<%fileNamePrefix%>CalcHelperMain.cpp

#OMCPP_LIBS= -lOMCppSystem_static -lOMCppDataExchange_static -lOMCppOMCFactory_static -OMCppSimulationSettings_static -lOMCppMath_static -lOMCppFMU_static -lOMCppExtensionUtilities_static -lOMCppModelicaUtilities_static
# CVode can be used for Co-Simulation FMUs, Kinsol is available to handle non linear equation systems
OMCPP_LIBS=-Wl,--start-group -lOMCppOMCFactory_FMU_static -lOMCppSystem_static -lOMCppSimController_static -Wl,--end-group -lOMCppDataExchange_static -lOMCppSimulationSettings_static -lOMCppNewton_static -lOMCppSolver_static -lOMCppMath_static -lOMCppModelicaUtilities_static -lOMCppExtensionUtilities_static -lOMCppFMU_static
OMCPP_SOLVER_LIBS=$(SUNDIALS_LIBRARIES)
OMCPP_SOLVER_LIBS=-lOMCppNewton_static
ifeq ($(USE_FMU_KINSOL),ON)
$(eval OMCPP_SOLVER_LIBS=$(OMCPP_SOLVER_LIBS) -lOMCppKinsol_static $(SUNDIALS_LIBRARIES))
endif

OMCPP_LIBS=-Wl,--start-group -lOMCppOMCFactory_FMU_static -lOMCppSystem_static -lOMCppSimController_static -Wl,--end-group -lOMCppDataExchange_static -lOMCppSimulationSettings_static $(OMCPP_SOLVER_LIBS) -lOMCppSolver_static -lOMCppMath_static -lOMCppModelicaUtilities_static -lOMCppExtensionUtilities_static -lOMCppFMU_static
MODELICA_EXTERNAL_LIBS=-lModelicaExternalC -lModelicaStandardTables -L$(LAPACK_LIBS) $(LAPACK_LIBRARIES)
BOOST_LIBRARIES = -l$(BOOST_SYSTEM_LIB) -l$(BOOST_FILESYSTEM_LIB) -l$(BOOST_PROGRAM_OPTIONS_LIB)
LIBS= $(OMCPP_LIBS) $(OMCPP_SOLVER_LIBS) $(MODELICA_EXTERNAL_LIBS) $(BASE_LIB) $(BOOST_LIBRARIES)
LIBS= $(OMCPP_LIBS) $(MODELICA_EXTERNAL_LIBS) $(BASE_LIB) $(BOOST_LIBRARIES)

CPPFILES=$(CALCHELPERMAINFILE)
OFILES=$(CPPFILES:.cpp=.o)
Expand All @@ -754,13 +757,17 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
<%\t%>$(CXX) -shared -o <%fileNamePrefix%>$(DLLEXT) $(OFILES) $(LDFLAGS) $(LIBS)
<%\t%>rm -rf binaries
<%\t%><%mkdir%> -p "binaries/$(PLATFORM)"
#<%\t%><%mkdir%> -p "documentation"
<%\t%>cp <%fileNamePrefix%>$(DLLEXT) "binaries/$(PLATFORM)/"
#<%\t%>cp $(SUNDIALS_LIBRARIES_KINSOL) "binaries/$(PLATFORM)/"
#<%\t%>cp $(OMHOME)/share/omc/runtime/cpp/licenses/sundials.license "documentation/"
<%\t%>rm -f <%modelName%>.fmu
<%\t%>zip -r "<%modelName%>.fmu" modelDescription.xml binaries
<%\t%>rm -rf binaries
ifeq ($(USE_FMU_KINSOL),ON)
<%\t%>rm -rf documentation
<%\t%><%mkdir%> -p "documentation"
<%\t%>cp $(SUNDIALS_LIBRARIES_KINSOL) "binaries/$(PLATFORM)/"
<%\t%>cp $(OMHOME)/share/omc/runtime/cpp/licenses/sundials.license "documentation/"
<%\t%>rm -rf documentation
endif

clean:
<%\t%>rm $(SRC) <%fileNamePrefix%>$(DLLEXT)
Expand Down
12 changes: 12 additions & 0 deletions SimulationRuntime/cpp/CMakeLists.txt
Expand Up @@ -12,6 +12,7 @@
# if the Intel TBB library was found -DUSE_INTEL_TBB
# if the Pugi XML library was found -DUSE_PUGI_XML
# if profiling for the simulation runtime should be enabled -DRUNTIME_PROFILING
# if the equation systems of a FMU should be solved with kinsol -DFMU_KINSOL
#
# Some of these options can be controlled by passing arguments to CMAKE
# if write output should be handled in parallel -DUSE_PARALLEL_OUTPUT=ON
Expand All @@ -36,6 +37,7 @@ OPTION(USE_PARALLEL_OUTPUT "USE_PARALLEL_OUTPUT" OFF)
OPTION(USE_SCOREP "USE_SCOREP" OFF)
OPTION(BOOST_STATIC_LINKING "BOOST_STATIC_LINKING" OFF)
OPTION(RUNTIME_PROFILING "RUNTIME_PROFILING" OFF)
OPTION(FMU_KINSOL "FMU_KINSOL" OFF)
OPTION(SUNDIALS_ROOT "SUNDIALS ROOT" "")
OPTION(BUILD_DOCUMENTATION "Use Doxygen to create the cpp runtime documentation" OFF)

Expand Down Expand Up @@ -140,6 +142,16 @@ ELSE(RUNTIME_PROFILING)
MESSAGE(STATUS "Runtime profiling disabled")
ENDIF(RUNTIME_PROFILING)

# Handle FMU kinsol support
IF(FMU_KINSOL)
ADD_DEFINITIONS(-DFMU_KINSOL)
SET(USE_FMU_KINSOL_ "ON")
MESSAGE(STATUS "FMU kinsol enabled")
ELSE(FMU_KINSOL)
SET(USE_FMU_KINSOL_ "OFF")
MESSAGE(STATUS "FMU kinsol disabled")
ENDIF(FMU_KINSOL)

# Handle build type
IF(OMC_BUILD)
ADD_DEFINITIONS(-DOMC_BUILD)
Expand Down
Expand Up @@ -35,4 +35,5 @@ ADDITIONAL_DEFINES=@ADDITIONAL_DEFINES@

SCOREP_INCLUDE=@SCOREP_INCLUDE_@
USE_SCOREP=@USE_SCOREP_@
USE_FMU_KINSOL=@USE_FMU_KINSOL_@
USE_PAPI=@USE_PAPI_@
4 changes: 2 additions & 2 deletions SimulationRuntime/cpp/Include/Core/Utils/extension/logger.hpp
Expand Up @@ -59,9 +59,9 @@ class BOOST_EXTENSION_LOGGER_DECL Logger
}

static std::pair<LogCategory,LogLevel> getLogMode(LogCategory cat, LogLevel lvl)
{
{
return std::pair<LogCategory, LogLevel>(cat, lvl);
}
}

bool isOutput(LogCategory cat, LogLevel lvl) const;

Expand Down
12 changes: 9 additions & 3 deletions SimulationRuntime/cpp/Include/FMU/FMUGlobalSettings.h
Expand Up @@ -2,6 +2,12 @@
#include <string.h>
using std::string;

#ifdef FMU_KINSOL
#define DEFAULT_NLS "kinsol"
#else
#define DEFAULT_NLS "newton"
#endif

#include <Core/SimulationSettings/IGlobalSettings.h>

class FMUGlobalSettings : public IGlobalSettings
Expand All @@ -27,16 +33,16 @@ class FMUGlobalSettings : public IGlobalSettings
virtual bool getInfoOutput() { return false; }
virtual void setInfoOutput(bool) {}
virtual string getOutputPath() { return "./"; }
virtual LogSettings getLogSettings() {return LogSettings();}
virtual LogSettings getLogSettings() {return LogSettings();}
virtual void setLogSettings(LogSettings) {}
virtual OutputPointType getOutputPointType() { return OPT_ALL; };
virtual void setOutputPointType(OutputPointType) {};
virtual void setOutputPath(string) {}
virtual string getSelectedSolver() { return "euler"; }
virtual void setSelectedSolver(string) {}
virtual string getSelectedLinSolver() { return "newton"; }
virtual string getSelectedLinSolver() { return DEFAULT_NLS; }
virtual void setSelectedLinSolver(string) {}
virtual string getSelectedNonLinSolver() { return "newton"; }
virtual string getSelectedNonLinSolver() { return DEFAULT_NLS; }
virtual void setSelectedNonLinSolver(string) {}
virtual void load(string xml_file) {};
virtual void setResultsFileName(string) {}
Expand Down
10 changes: 6 additions & 4 deletions SimulationRuntime/cpp/Include/FMU2/FMU2GlobalSettings.h
Expand Up @@ -37,8 +37,10 @@

#include <Core/SimulationSettings/IGlobalSettings.h>

#ifdef OFF
#undef OFF // avoid name clash with enum LogType
#ifdef FMU_KINSOL
#define DEFAULT_NLS "kinsol"
#else
#define DEFAULT_NLS "newton"
#endif

class FMU2GlobalSettings : public IGlobalSettings
Expand Down Expand Up @@ -70,9 +72,9 @@ class FMU2GlobalSettings : public IGlobalSettings
virtual void setOutputPath(string) {}
virtual string getSelectedSolver() { return "euler"; }
virtual void setSelectedSolver(string) {}
virtual string getSelectedLinSolver() { return "kinsol"; }
virtual string getSelectedLinSolver() { return DEFAULT_NLS; }
virtual void setSelectedLinSolver(string) {}
virtual string getSelectedNonLinSolver() { return "kinsol"; }
virtual string getSelectedNonLinSolver() { return DEFAULT_NLS; }
virtual void setSelectedNonLinSolver(string) {}
virtual void load(std::string xml_file) {};
virtual void setResultsFileName(string) {}
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Kinsol/FactoryExport.h
Expand Up @@ -4,9 +4,11 @@
* @{
*/
#if defined(__vxworks) || defined(__TRICORE__) || defined(RUNTIME_STATIC_LINKING)
#define BOOST_EXTENSION_LOGGER_DECL
#define BOOST_EXTENSION_SOLVER_DECL
#define BOOST_EXTENSION_SOLVERSETTINGS_DECL
#elif defined(OMC_BUILD) || defined(SIMSTER_BUILD)
#define BOOST_EXTENSION_LOGGER_DECL BOOST_EXTENSION_IMPORT_DECL
#define BOOST_EXTENSION_SOLVER_DECL BOOST_EXTENSION_IMPORT_DECL
#define BOOST_EXTENSION_SOLVERSETTINGS_DECL BOOST_EXTENSION_IMPORT_DECL
#else
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Kinsol/Kinsol.h
Expand Up @@ -18,6 +18,8 @@
#include <boost/numeric/ublas/lu.hpp>
#include <boost/numeric/ublas/symmetric.hpp>

#include <Core/Utils/extension/logger.hpp>

//#include<kinsol_lapack.h>
int kin_fCallback(N_Vector y, N_Vector fval, void *user_data);
class Kinsol : public IAlgLoopSolver
Expand Down
4 changes: 3 additions & 1 deletion SimulationRuntime/cpp/Include/Solver/Newton/FactoryExport.h
Expand Up @@ -3,10 +3,12 @@
* Nonlineaar solver class for Newton methods
* @{
*/
#if defined(__vxworks) || define(RUNTIME_STATIC_LINKING)
#if defined(__vxworks) || defined(RUNTIME_STATIC_LINKING)
#define BOOST_EXTENSION_LOGGER_DECL
#define BOOST_EXTENSION_SOLVER_DECL
#define BOOST_EXTENSION_SOLVERSETTINGS_DECL
#elif defined(OMC_BUILD) || defined(SIMSTER_BUILD)
#define BOOST_EXTENSION_LOGGER_DECL BOOST_EXTENSION_IMPORT_DECL
#define BOOST_EXTENSION_SOLVER_DECL BOOST_EXTENSION_IMPORT_DECL
#define BOOST_EXTENSION_SOLVERSETTINGS_DECL BOOST_EXTENSION_IMPORT_DECL
#else
Expand Down
4 changes: 3 additions & 1 deletion SimulationRuntime/cpp/Include/Solver/Newton/Newton.h
Expand Up @@ -5,12 +5,14 @@

#pragma once

#include "FactoryExport.h"

#include <Core/System/IAlgLoop.h> // Interface to AlgLoo
#include <Core/Solver/IAlgLoopSolver.h> // Export function from dll
#include <Core/Solver/INonLinSolverSettings.h>
#include <Solver/Newton/NewtonSettings.h>


#include <Core/Utils/extension/logger.hpp>


/*****************************************************************************/
Expand Down
6 changes: 5 additions & 1 deletion SimulationRuntime/cpp/Makefile.in
Expand Up @@ -17,6 +17,7 @@ BOOST_REALPATHS_COMMAND="-DUSE_BOOST_REALPATHS=ON"
RUNTIME_PROFILING_COMMAND="-DRUNTIME_PROFILING=ON"
SCOREP_COMMAND="-DUSE_SCOREP=ON"
SCOREP_HOME=""
FMU_KINSOL_COMMAND="-DUSE_KINSOL=ON"
BUILD_DOC_COMMAND="-DBUILD_DOCUMENTATION=ON"

#Experimental
Expand All @@ -43,14 +44,17 @@ ifneq ("$(SCOREP_SUPPORT)","true")
$(eval CMAKE_COMMANDS=CC="@CC@" CXX="@CXX@" CFLAGS="@CFLAGS@" CXXFLAGS="@CXXFLAGS@")
$(eval SCOREP_COMMAND=)
endif
ifneq ("$(FMU_KINSOL_SUPPORT)","true")
$(eval FMU_KINSOL_COMMAND=)
endif
ifneq ("$(SUNDIALS_LAPACK)","true")
$(eval SUNDIALS_LAPACK_COMMAND=)
endif
ifneq ("$(BUILD_DOCUMENTATION)","true")
$(eval BUILD_DOC_COMMAND=)
endif
mkdir -p Build
cd ./Build && echo "change to Build" && $(CMAKE_COMMANDS) cmake $(BOOST_STATIC_LINKING_COMMAND) $(BOOST_REALPATHS_COMMAND) $(PARALLEL_OUTPUT_COMMAND) $(RUNTIME_PROFILING_COMMAND) $(SCOREP_COMMAND) $(SUNDIALS_LAPACK_COMMAND) -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX:PATH="$(OMBUILDDIR)" -DLIBINSTALLEXT=lib/@host_short@/omc/cpp -DSCOREP_HOME:STRING="$(SCOREP_HOME)" $(BUILD_DOC_COMMAND) $(CMAKE_ARGS) ../
cd ./Build && echo "change to Build" && $(CMAKE_COMMANDS) cmake $(BOOST_STATIC_LINKING_COMMAND) $(BOOST_REALPATHS_COMMAND) $(PARALLEL_OUTPUT_COMMAND) $(RUNTIME_PROFILING_COMMAND) $(SCOREP_COMMAND) $(FMU_KINSOL_COMMAND) $(SUNDIALS_LAPACK_COMMAND) -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX:PATH="$(OMBUILDDIR)" -DLIBINSTALLEXT=lib/@host_short@/omc/cpp -DSCOREP_HOME:STRING="$(SCOREP_HOME)" $(BUILD_DOC_COMMAND) $(CMAKE_ARGS) ../
$(MAKE) -C Build VERBOSE=1

install: runtimeCpp
Expand Down
14 changes: 10 additions & 4 deletions SimulationRuntime/cpp/SimCoreFactory/OMCFactory/CMakeLists.txt
Expand Up @@ -6,17 +6,23 @@ add_library(${OMCFactoryName} SHARED OMCFactory.cpp)
add_library(${OMCFactoryName}_static STATIC OMCFactory.cpp StaticOMCFactory.cpp StaticNonLinSolverOMCFactory.cpp StaticLinSolverOMCFactory.cpp StaticSolverOMCFactory.cpp)
add_library(${OMCFactoryName}_FMU_static STATIC OMCFactory.cpp StaticOMCFactory.cpp StaticNonLinSolverOMCFactory.cpp StaticLinSolverOMCFactory.cpp StaticSolverOMCFactory.cpp)
set_target_properties(${OMCFactoryName}_static PROPERTIES COMPILE_DEFINITIONS "RUNTIME_STATIC_LINKING;ENABLE_CVODE_STATIC;ENABLE_KINSOL_STATIC")
set_target_properties(${OMCFactoryName}_FMU_static PROPERTIES COMPILE_DEFINITIONS "RUNTIME_STATIC_LINKING")

if(FMU_KINSOL)
set_target_properties(${OMCFactoryName}_FMU_static PROPERTIES COMPILE_DEFINITIONS "RUNTIME_STATIC_LINKING;ENABLE_KINSOL_STATIC")
else(FMU_KINSOL)
set_target_properties(${OMCFactoryName}_FMU_static PROPERTIES COMPILE_DEFINITIONS "RUNTIME_STATIC_LINKING")
endif(FMU_KINSOL)

include_directories(${SUNDIALS_INCLUDE_DIR}/cvodes ${SUNDIALS_INCLUDE_DIR}/nvector ${SUNDIALS_INCLUDE_DIR}/sundials ${SUNDIALS_INCLUDE_DIR}/kinsol ${SUNDIALS_INCLUDE_DIR})

install (TARGETS ${OMCFactoryName}_static DESTINATION ${LIBINSTALLEXT})
install (TARGETS ${OMCFactoryName}_FMU_static DESTINATION ${LIBINSTALLEXT})
install (TARGETS ${OMCFactoryName} DESTINATION ${LIBINSTALLEXT})

if (UNIX)
set_target_properties(${OMCFactoryName}_static PROPERTIES COMPILE_FLAGS -fPIC)
set_target_properties(${OMCFactoryName}_FMU_static PROPERTIES COMPILE_FLAGS -fPIC)
set_target_properties(${OMCFactoryName} PROPERTIES COMPILE_FLAGS -fPIC)
set_target_properties(${OMCFactoryName}_static PROPERTIES COMPILE_FLAGS -fPIC)
set_target_properties(${OMCFactoryName}_FMU_static PROPERTIES COMPILE_FLAGS -fPIC)
set_target_properties(${OMCFactoryName} PROPERTIES COMPILE_FLAGS -fPIC)
endif(UNIX)

target_link_libraries(${OMCFactoryName} ${Boost_LIBRARIES} ${CMAKE_DL_LIBS})
Expand Down
1 change: 0 additions & 1 deletion SimulationRuntime/cpp/Solver/CVode/CVode.cpp
Expand Up @@ -9,7 +9,6 @@
#include <Core/Utils/numeric/bindings/ublas/matrix_sparse.hpp>



Cvode::Cvode(IMixedSystem* system, ISolverSettings* settings)
: SolverDefaultImplementation(system, settings),
_cvodesettings(dynamic_cast<ISolverSettings*>(_settings)),
Expand Down
4 changes: 1 addition & 3 deletions SimulationRuntime/cpp/Solver/Kinsol/Kinsol.cpp
Expand Up @@ -8,14 +8,11 @@
#include <Solver/Kinsol/Kinsol.h>
#include <Solver/Kinsol/KinsolSettings.h>



//#include <Core/Utils/numeric/bindings/lapack/driver/gesv.hpp>
#include <Core/Utils/numeric/bindings/ublas/matrix.hpp>
#include <Core/Utils/numeric/bindings/std/vector.hpp>
#include <Core/Utils/numeric/utils.h>


extern "C" void dgesv_(long int *n, long int *nrhs, double *J, long int *ldj, long int *pivot,double *b, long int *ldb, long int *idid);

#if defined(__TRICORE__)
Expand Down Expand Up @@ -203,6 +200,7 @@ void Kinsol::initialize()
_iterationStatus = SOLVERERROR;
}
}
Logger::write("Kinsol: initialized",LC_NLS,LL_DEBUG);
}

void Kinsol::solve()
Expand Down
3 changes: 1 addition & 2 deletions SimulationRuntime/cpp/Solver/Newton/Newton.cpp
Expand Up @@ -88,8 +88,7 @@ void Newton::initialize()
_iterationStatus = SOLVERERROR;
}
}


Logger::write("Newton: initialized",LC_NLS,LL_DEBUG);
}

void Newton::solve()
Expand Down

0 comments on commit d9b26e6

Please sign in to comment.