Skip to content

Commit

Permalink
Added flag to use only one instance of algebraic loop solver in cpp
Browse files Browse the repository at this point in the history
runtime

Added flag to use only one instance of algebraich loop solver in cpp
runtime

[cppRuntime] Simplified handling algebraic loops in cpp runtime
Use only one solver for linear and one for nonlinear system. If needed a
own solver can used for a system

removed accidentally added files

added new AlglooperSolver class for linear and nonlinear systems

adapted linear and nonlinear solvers on new interfaces ILinearAlgloopSolver and INonLinearAlgloopSolver

added restart attribute to solve methode of algloopsolver, to restart without initialisation of algloop system

Adapted cpp template for new algloop solver interface

fix for intialize algloops at first solve call

fix for init algloop solvers used by analytic jacobians

fix for start algebraic loop solver without initializing

Combined initialize and evaluate function from algebraic loops

for parallel simulation use multiple instances of algloopsolver

fix typo

added flag to use only one instance of algloop solver in cpp runtime

removed initiequation call for some kind of linear equations

added missing code for jacobian  algloopsolvers

fix call for create kinsol solver in cpp runtime

Belonging to [master]:
  - OpenModelica/OMCompiler#2574
  • Loading branch information
niklwors authored and OpenModelica-Hudson committed Jul 11, 2018
1 parent 23656fa commit 65d1390
Show file tree
Hide file tree
Showing 53 changed files with 792 additions and 418 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Expand Up @@ -56,6 +56,8 @@ $RECYCLE.BIN/
.Spotlight-V100
.Trashes



# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Expand Down Expand Up @@ -153,5 +155,7 @@ Parser/ParModelica_Lexer_BaseModelica_Lexer.h

SimulationRuntime/**/test_files/*.jar
SimulationRuntime/cpp/build_msvc/

# Visual Studio 2015/2017 cache/options directory
SimulationRuntime/cpp/.vs/
SimulationRuntime/cpp/CMakeSettings.json
tags
433 changes: 328 additions & 105 deletions Compiler/Template/CodegenCpp.tpl

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Compiler/Template/SimCodeTV.mo
Expand Up @@ -3551,7 +3551,7 @@ package Flags
constant ConfigFlag LABELED_REDUCTION;
constant ConfigFlag LOAD_MSL_MODEL;
constant ConfigFlag Load_PACKAGE_FILE;

constant ConfigFlag SINGLE_INSTANCE_AGLSOLVER;
function set
input DebugFlag inFlag;
input Boolean inValue;
Expand Down
6 changes: 5 additions & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -1459,6 +1459,9 @@ constant ConfigFlag IGNORE_REPLACEABLE = CONFIG_FLAG(117, "ignoreReplaceable",
"evalRecursionLimit", NONE(), EXTERNAL(), INT_FLAG(256), NONE(),
Util.gettext("The recursion limit used when evaluating constant function calls."));

constant ConfigFlag SINGLE_INSTANCE_AGLSOLVER = CONFIG_FLAG(127, "singleInstanceAglSolver",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Sets to instantiate only one algebraic loop solver all algebraic loops"));

protected
// This is a list of all configuration flags. A flag can not be used unless it's
Expand Down Expand Up @@ -1590,7 +1593,8 @@ constant list<ConfigFlag> allConfigFlags = {
BUILDING_MODEL,
POST_OPT_MODULES_DAE,
EVAL_LOOP_LIMIT,
EVAL_RECURSION_LIMIT
EVAL_RECURSION_LIMIT,
SINGLE_INSTANCE_AGLSOLVER
};

public function new
Expand Down
9 changes: 6 additions & 3 deletions SimulationRuntime/cpp/CMakeLists.txt
Expand Up @@ -69,7 +69,7 @@ else()
message(STATUS "Libs will be installed in ${LIBINSTALLEXT}")
endif()

PROJECT(CppSolverInterface)
PROJECT(CppSimulationRuntime)
SET(CMAKE_VERBOSE_MAKEFILE ON)
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
SET(CMAKE_POSITION_INDEPENDENT_CODE ON)
Expand All @@ -93,11 +93,14 @@ OPTION(USE_CPP_03 "USE_CPP_03" OFF)
#Set Variables
IF(NOT FMU_TARGET)
IF(MSVC)
SET(LIBINSTALLEXT "lib/omc/cpp/msvc" CACHE STRING "library directory")
SET(LIBINSTALLEXT "lib/omc/cpp/msvc" CACHE STRINGz "library directory")
ELSE(MSVC)
SET(LIBINSTALLEXT "lib/omc/cpp" CACHE STRING "library directory")
ENDIF()
ENDIF(NOT FMU_TARGET)

MESSAGE(STATUS "Using library folder extension" ${LIBINSTALLEXT})

SET(MODELICA_MODEL "ModelicaSystem")
SET(LIBPREFIX "OMCpp")
IF(BUILD_SHARED_LIBS)
Expand Down Expand Up @@ -350,7 +353,7 @@ IF(WIN32)
SET(MICO_LIB_HOME $ENV{OMDEV}/lib/mico-msys-mingw/)
SET(MICO_INCLUDE_HOME $ENV{OMDEV}/include/mico-msys-mingw/)

SET(INSTALL_OMDEV_LIBS ON)
SET(INSTALL_OMDEV_LIBS OFF)
ENDIF(WIN32)

# Find OpenMP
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Core/Modelica/CMakeLists.txt
Expand Up @@ -268,7 +268,7 @@ install (FILES
DESTINATION include/omc/cpp)
ENDIF("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")

install(FILES $<TARGET_PDB_FILE:${ModelicaName}> DESTINATION ${LIBINSTALLEXT} OPTIONAL)


install(TARGETS ${ModelicaName} DESTINATION ${LIBINSTALLEXT})
install (FILES ${CMAKE_SOURCE_DIR}/Include/Core/Modelica.h ${CMAKE_SOURCE_DIR}/Include/Core/ModelicaDefine.h DESTINATION include/omc/cpp/Core)
Expand Down
2 changes: 2 additions & 0 deletions SimulationRuntime/cpp/Core/SimController/Initialization.cpp
Expand Up @@ -53,6 +53,8 @@ void Initialization::initializeSystem()
if( _solver->stateSelection())
{
_system->initEquations();
continous_system->stepCompleted(0.0);


/* report a warning about strange start values */
if(_solver->stateSelection())
Expand Down
3 changes: 2 additions & 1 deletion SimulationRuntime/cpp/Core/Solver/CMakeLists.txt
Expand Up @@ -15,7 +15,8 @@ install(FILES $<TARGET_PDB_FILE:${SolverName}> DESTINATION ${LIBINSTALLEXT} OPTI

install(TARGETS ${SolverName} DESTINATION ${LIBINSTALLEXT})
install(FILES
${CMAKE_SOURCE_DIR}/Include/Core/Solver/IAlgLoopSolver.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/ILinearAlgLoopSolver.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/INonLinearAlgLoopSolver.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/ILinSolverSettings.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/INonLinSolverSettings.h
${CMAKE_SOURCE_DIR}/Include/Core/Solver/ISolver.h
Expand Down
41 changes: 20 additions & 21 deletions SimulationRuntime/cpp/Core/System/AlgLoopSolverFactory.cpp
Expand Up @@ -20,15 +20,15 @@ AlgLoopSolverFactory::~AlgLoopSolverFactory()
{
}

shared_ptr<IAlgLoopSolver> AlgLoopSolverFactory::createLinearAlgLoopSolver(ILinearAlgLoop* algLoop)
shared_ptr<ILinearAlgLoopSolver> AlgLoopSolverFactory::createLinearAlgLoopSolver(shared_ptr<ILinearAlgLoop> algLoop)
{
try
{
string linsolver_name = _global_settings->getSelectedLinSolver();
shared_ptr<ILinSolverSettings> algsolversetting= createLinSolverSettings(linsolver_name);
_linalgsolversettings.push_back(algsolversetting);
shared_ptr<IAlgLoopSolver> algsolver= createLinSolver(algLoop,linsolver_name,algsolversetting);
_algsolvers.push_back(algsolver);
shared_ptr<ILinearAlgLoopSolver> algsolver= createLinSolver(linsolver_name,algsolversetting,algLoop);
_linear_algsolvers.push_back(algsolver);
return algsolver;
}
catch(std::exception &arg)
Expand All @@ -39,24 +39,23 @@ shared_ptr<IAlgLoopSolver> AlgLoopSolverFactory::createLinearAlgLoopSolver(ILine
}

/// Creates a nonlinear solver according to given system of equations of type algebraic loop
shared_ptr<IAlgLoopSolver> AlgLoopSolverFactory::createNonLinearAlgLoopSolver(INonLinearAlgLoop* algLoop)
shared_ptr<INonLinearAlgLoopSolver> AlgLoopSolverFactory::createNonLinearAlgLoopSolver(shared_ptr<INonLinearAlgLoop> algLoop)
{
if(algLoop->getDimReal() > 0)
{

string nonlinsolver_name = _global_settings->getSelectedNonLinSolver();
shared_ptr<INonLinSolverSettings> algsolversetting= createNonLinSolverSettings(nonlinsolver_name);
algsolversetting->setContinueOnError(_global_settings->getNonLinearSolverContinueOnError());
_algsolversettings.push_back(algsolversetting);

shared_ptr<IAlgLoopSolver> algsolver= createNonLinSolver(algLoop,nonlinsolver_name,algsolversetting);
_algsolvers.push_back(algsolver);
return algsolver;
}
else
{
// TODO: Throw an error message here.
throw ModelicaSimulationError(MODEL_FACTORY,"AlgLoop solver is not available");
}
try
{
string nonlinsolver_name = _global_settings->getSelectedNonLinSolver();
shared_ptr<INonLinSolverSettings> algsolversetting= createNonLinSolverSettings(nonlinsolver_name);
algsolversetting->setContinueOnError(_global_settings->getNonLinearSolverContinueOnError());
_algsolversettings.push_back(algsolversetting);

shared_ptr<INonLinearAlgLoopSolver> algsolver= createNonLinSolver(nonlinsolver_name,algsolversetting,algLoop);
_non_linear_algsolvers.push_back(algsolver);
return algsolver;
}
catch(std::exception &arg)
{
throw ModelicaSimulationError(MODEL_FACTORY,"Linear AlgLoop solver is not available");
}

}
/** @} */ // end of coreSystem
Expand Up @@ -12,13 +12,18 @@ LinearAlgLoopDefaultImplementation::LinearAlgLoopDefaultImplementation()
,_b(NULL)
,_AData(NULL)
,_Ax(NULL)
,_x0(NULL)
, _firstcall(true)
{

}

LinearAlgLoopDefaultImplementation::~LinearAlgLoopDefaultImplementation()
{
if(_b)
delete [] _b;
if (_x0)
delete [] _x0;
}

/// Provide number (dimension) of variables according to data type
Expand All @@ -37,6 +42,9 @@ void LinearAlgLoopDefaultImplementation::initialize()
delete [] _b;
_b = new double[_dimAEq];
memset(_b,0,_dimAEq*sizeof(double));
if(_x0)
delete [] _x0;
_x0 = new double[_dimAEq];
};

void LinearAlgLoopDefaultImplementation::getb(double* res) const
Expand All @@ -52,7 +60,10 @@ bool LinearAlgLoopDefaultImplementation::getUseSparseFormat(){
void LinearAlgLoopDefaultImplementation::setUseSparseFormat(bool value){
_useSparseFormat = value;
}

void LinearAlgLoopDefaultImplementation::getRealStartValues(double* vars) const
{
memcpy(vars, _x0, sizeof(double) * _dimAEq);
}


//void LinearAlgLoopDefaultImplementation::getSparseAdata(double* data, int nonzeros)
Expand Down
Expand Up @@ -17,13 +17,17 @@ NonLinearAlgLoopDefaultImplementation::NonLinearAlgLoopDefaultImplementation()
,_res(NULL)
,_AData(NULL)
,_Ax(NULL)
,_x0(NULL)
, _firstcall(true)
{
}

NonLinearAlgLoopDefaultImplementation::~NonLinearAlgLoopDefaultImplementation()
{
if(_res)
delete [] _res;
if (_x0)
delete _x0;
}

/// Provide number (dimension) of variables according to data type
Expand All @@ -42,6 +46,9 @@ void NonLinearAlgLoopDefaultImplementation::initialize()
delete [] _res;
_res = new double[_dimAEq];
memset(_res,0,_dimAEq*sizeof(double));
if(_x0)
delete [] _x0;
_x0 = new double[_dimAEq];
};

//in algloop default verschieben
Expand All @@ -58,6 +65,13 @@ void NonLinearAlgLoopDefaultImplementation::setUseSparseFormat(bool value){
_useSparseFormat = value;
}

void NonLinearAlgLoopDefaultImplementation::getRealStartValues(double* vars) const
{

memcpy(vars, _x0, sizeof(double) * _dimAEq);
}


//void NonLinearAlgLoopDefaultImplementation::getSparseAdata(double* data, int nonzeros)
//{
// memcpy(data, _AData, sizeof(double) * nonzeros);
Expand Down
3 changes: 2 additions & 1 deletion SimulationRuntime/cpp/Include/Core/Modelica.h
Expand Up @@ -219,7 +219,8 @@ typedef ublas::matrix<double, ublas::column_major> matrix_t;
#include <Core/System/INonLinearAlgLoop.h>
#include <Core/System/ISystemTypes.h>
#include <Core/Solver/ISolver.h>
#include <Core/Solver/IAlgLoopSolver.h>
#include <Core/Solver/ILinearAlgLoopSolver.h>
#include <Core/Solver/INonLinearAlgLoopSolver.h>
#include <Core/System/IAlgLoopSolverFactory.h>
#include <Core/System/ISimVars.h>
#include <Core/DataExchange/ISimVar.h>
Expand Down
Expand Up @@ -3,8 +3,8 @@
*
* @{
*/
class IAlgLoop;
class IContinuous;
class ILinearAlgLoop;


/*****************************************************************************/
/**
Expand All @@ -20,7 +20,7 @@ solution of algebraic loops in open modelica.
Copyright (c) 2008, OSMC
*****************************************************************************/

class IAlgLoopSolver
class ILinearAlgLoopSolver
{
public:
/// Enumeration to denote the status of iteration
Expand All @@ -31,13 +31,15 @@ class IAlgLoopSolver
DONE,
};

virtual ~IAlgLoopSolver() {};
virtual ~ILinearAlgLoopSolver() {};

/// (Re-) initialize the solver
virtual void initialize() = 0;

/// Solution of a (non-)linear system of equations
virtual void solve() = 0;
//solve for a single instance call
virtual void solve(shared_ptr<ILinearAlgLoop> algLoop,bool first_solve = false) = 0;

/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus() = 0;
Expand Down
@@ -0,0 +1,51 @@
#pragma once
/** @addtogroup coreSolver
*
* @{
*/
class INonLinearAlgLoop;


/*****************************************************************************/
/**
Abstract interface class for numerical methods for the (possibly iterative)
solution of algebraic loops in open modelica.
\date October, 1st, 2008
\author
*/
/*****************************************************************************
Copyright (c) 2008, OSMC
*****************************************************************************/

class INonLinearAlgLoopSolver
{
public:
/// Enumeration to denote the status of iteration
enum ITERATIONSTATUS
{
CONTINUE,
SOLVERERROR,
DONE,
};

virtual ~INonLinearAlgLoopSolver() {};

/// (Re-) initialize the solver
virtual void initialize() = 0;

/// Solution of a (non-)linear system of equations
virtual void solve() = 0;
//solve for a single instance call
virtual void solve(shared_ptr<INonLinearAlgLoop> algLoop,bool first_solve = false) = 0;


/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus() = 0;
virtual void stepCompleted(double time) = 0;
virtual void restoreOldValues() = 0;
virtual void restoreNewValues() = 0;
};
/** @} */ // end of coreSolver
Expand Up @@ -17,13 +17,14 @@ class AlgLoopSolverFactory : public IAlgLoopSolverFactory, public NonLinSolverPo
virtual ~AlgLoopSolverFactory();

/// Creates a solver according to given system of equations of type algebraic loop
virtual shared_ptr<IAlgLoopSolver> createLinearAlgLoopSolver(ILinearAlgLoop* algLoop);
virtual shared_ptr<IAlgLoopSolver> createNonLinearAlgLoopSolver(INonLinearAlgLoop* algLoop);
virtual shared_ptr<ILinearAlgLoopSolver> createLinearAlgLoopSolver(shared_ptr<ILinearAlgLoop> algLoop = shared_ptr<ILinearAlgLoop>());
virtual shared_ptr<INonLinearAlgLoopSolver> createNonLinearAlgLoopSolver(shared_ptr<INonLinearAlgLoop> algLoop = shared_ptr<INonLinearAlgLoop>());
private:
//std::vector<shared_ptr<IKinsolSettings> > _algsolversettings;
std::vector<shared_ptr<INonLinSolverSettings> > _algsolversettings;
std::vector<shared_ptr<ILinSolverSettings> > _linalgsolversettings;
std::vector<shared_ptr<IAlgLoopSolver> > _algsolvers;
std::vector<shared_ptr<ILinearAlgLoopSolver> > _linear_algsolvers;
std::vector<shared_ptr<INonLinearAlgLoopSolver> > _non_linear_algsolvers;
IGlobalSettings* _global_settings;
};
/** @} */ // end of coreSystem
Expand Up @@ -23,7 +23,7 @@ class IAlgLoopSolverFactory
public:
IAlgLoopSolverFactory() {};
virtual ~IAlgLoopSolverFactory() {};
virtual shared_ptr<IAlgLoopSolver> createLinearAlgLoopSolver(ILinearAlgLoop* algLoop) = 0;
virtual shared_ptr<IAlgLoopSolver> createNonLinearAlgLoopSolver(INonLinearAlgLoop* algLoop) = 0;
virtual shared_ptr<ILinearAlgLoopSolver> createLinearAlgLoopSolver(shared_ptr<ILinearAlgLoop> algLoop = shared_ptr<ILinearAlgLoop>()) = 0;
virtual shared_ptr<INonLinearAlgLoopSolver> createNonLinearAlgLoopSolver(shared_ptr<INonLinearAlgLoop> algLoop = shared_ptr<INonLinearAlgLoop>()) = 0;
};
/** @} */ // end of coreSystem
3 changes: 2 additions & 1 deletion SimulationRuntime/cpp/Include/Core/System/ILinearAlgLoop.h
Expand Up @@ -48,7 +48,8 @@ class ILinearAlgLoop
virtual void getReal(double* lambda) const = 0;
/// Set variables with given data type
virtual void setReal(const double* lambda) = 0;

virtual void setRealStartValues() = 0;
virtual void getRealStartValues(double* vars) const= 0;
/// Evaluate equations for given variables
virtual void evaluate() = 0;

Expand Down

0 comments on commit 65d1390

Please sign in to comment.