Skip to content
This repository has been archived by the owner on May 18, 2019. It is now read-only.

Commit

Permalink
Removed code generation for algebraic loop variables and added it to …
Browse files Browse the repository at this point in the history
…algebraic loop default solver implementation

Belonging to [master]:
  - #2577
  • Loading branch information
niklwors authored and OpenModelica-Hudson committed Jul 19, 2018
1 parent 04d541f commit c5bc6e5
Show file tree
Hide file tree
Showing 36 changed files with 646 additions and 450 deletions.
419 changes: 137 additions & 282 deletions Compiler/Template/CodegenCpp.tpl

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Compiler/Template/CodegenFMUCpp.tpl
Expand Up @@ -707,7 +707,7 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
# /MD - link with MSVCRT.LIB
# /link - [linker options and libraries]
# /LIBPATH: - Directories where libs can be found
OMCPP_SOLVER_LIBS=OMCppNewton_static.lib OMCppDgesv_static.lib OMCppDgesvSolver_static.lib
OMCPP_SOLVER_LIBS=OMCppNewton_static.lib OMCppDgesv_static.lib OMCppDgesvSolver_static.lib -lOMCppSolver_static
MODELICA_UTILITIES_LIB=OMCppModelicaUtilities_static.lib
EXTRA_LIBS=<%dirExtra%> <%libsExtra%>
LDFLAGS=/link /DLL /NOENTRY /LIBPATH:"<%makefileParams.omhome%>/lib/omc/cpp/msvc" /LIBPATH:"<%makefileParams.omhome%>/bin" OMCppSystem_static.lib OMCppMath_static.lib OMCppExtensionUtilities_static.lib OMCppFMU_static.lib $(OMCPP_SOLVER_LIBS) $(EXTRA_LIBS) $(MODELICA_UTILITIES_LIB)
Expand Down Expand Up @@ -795,7 +795,7 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
CALCHELPERMAINFILE=OMCpp<%fileNamePrefix%>CalcHelperMain.cpp

# CVode can be used for Co-Simulation FMUs, Kinsol is available to handle non linear equation systems
OMCPP_SOLVER_LIBS=-lOMCppNewton_static -lOMCppDgesvSolver_static
OMCPP_SOLVER_LIBS=-lOMCppNewton_static -lOMCppDgesvSolver_static -lOMCppSolver_static
ifeq ($(USE_FMU_SUNDIALS),ON)
$(eval OMCPP_SOLVER_LIBS=$(OMCPP_SOLVER_LIBS) -lOMCppKinsol_static $(SUNDIALS_LIBRARIES))
$(eval CFLAGS=-DENABLE_SUNDIALS_STATIC $(CFLAGS))
Expand Down
@@ -0,0 +1,71 @@
/** @addtogroup coreSolver
*
* @{
*/
#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>
#include <Core/Solver/FactoryExport.h>
#include <Core/Solver/AlgLoopSolverDefaultImplementation.h>


AlgLoopSolverDefaultImplementation::AlgLoopSolverDefaultImplementation()
:_dimZeroFunc(-1)
, _dimSys(-1)
,_algloopVars(NULL)
,_conditions0(NULL)
,_conditions1(NULL)
{

}

AlgLoopSolverDefaultImplementation::~AlgLoopSolverDefaultImplementation()
{
if(_algloopVars)
delete [] _algloopVars;
if(_conditions0)
delete [] _conditions0;
if(_conditions1)
delete [] _conditions1;
}
bool* AlgLoopSolverDefaultImplementation::getConditionsWorkArray()
{
if(_conditions0)
return _conditions0;
else
ModelicaSimulationError(ALGLOOP_SOLVER, "algloop working arrays are not initialized");

}
bool* AlgLoopSolverDefaultImplementation::getConditions2WorkArray()
{
if(_conditions1)
return _conditions1;
else
ModelicaSimulationError(ALGLOOP_SOLVER, "algloop working arrays are not initialized");
}


double* AlgLoopSolverDefaultImplementation::getVariableWorkArray()
{
if(_algloopVars)
return _algloopVars;
else
ModelicaSimulationError(ALGLOOP_SOLVER, "algloop working arrays are not initialized");

}

void AlgLoopSolverDefaultImplementation::initialize(int dimZeroFunc,int dimSys)
{
_dimZeroFunc = dimZeroFunc;
if(_conditions0)
delete [] _conditions0;
if(_conditions1)
delete [] _conditions1;
_conditions0 = new bool[_dimZeroFunc];
_conditions1 = new bool[_dimZeroFunc];
_dimSys=dimSys;
if(_algloopVars)
delete [] _algloopVars;
_algloopVars = new double[_dimSys];
memset(_algloopVars, 0, _dimSys*sizeof(double));
}
/** @} */ // end of coreSolver
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Core/Solver/CMakeLists.txt
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8.9)

project(${SolverName})

add_library(${SolverName} SolverDefaultImplementation.cpp SolverSettings.cpp SystemStateSelection.cpp FactoryExport.cpp SimulationMonitor.cpp)
add_library(${SolverName} SolverDefaultImplementation.cpp AlgLoopSolverDefaultImplementation.cpp SolverSettings.cpp SystemStateSelection.cpp FactoryExport.cpp SimulationMonitor.cpp)

if(NOT BUILD_SHARED_LIBS)
set_target_properties(${SolverName} PROPERTIES COMPILE_DEFINITIONS "RUNTIME_STATIC_LINKING;ENABLE_SUNDIALS_STATIC")
Expand Down
Expand Up @@ -32,6 +32,10 @@ int LinearAlgLoopDefaultImplementation::getDimReal() const
return _dimAEq;
}

int LinearAlgLoopDefaultImplementation::getDimZeroFunc() const
{
return _dimZeroFunc;
}
/// (Re-) initialize the system of equations
void LinearAlgLoopDefaultImplementation::initialize()
{
Expand Down
Expand Up @@ -36,6 +36,11 @@ int NonLinearAlgLoopDefaultImplementation::getDimReal() const
return _dimAEq;
}

int NonLinearAlgLoopDefaultImplementation::getDimZeroFunc() const
{
return _dimZeroFunc;
}

/// (Re-) initialize the system of equations
void NonLinearAlgLoopDefaultImplementation::initialize()
{
Expand Down
@@ -0,0 +1,35 @@
#pragma once
/** @addtogroup coreSolver
*
* @{
*/


/*****************************************************************************
Copyright (c) 2008, OSMC
*****************************************************************************/

class BOOST_EXTENSION_SOLVER_DECL AlgLoopSolverDefaultImplementation
{
public:
AlgLoopSolverDefaultImplementation();
~AlgLoopSolverDefaultImplementation();
/// (Re-) initialize the solver
virtual void initialize(int dimZeroFunc,int dimSys);
virtual bool* getConditionsWorkArray();
virtual bool* getConditions2WorkArray();
virtual double* getVariableWorkArray();
protected:
long int _dimZeroFunc;
long int _dimSys; ///< Number of unknowns (=dimension of system of equations)
long int _max_dimSys;
long int _max_dimZeroFunc;
bool _single_instance;
private:
double* _algloopVars;
bool* _conditions0;
bool* _conditions1;


};
/** @} */ // end of coreSolver
Expand Up @@ -40,7 +40,9 @@ class ILinearAlgLoopSolver
virtual void solve() = 0;
//solve for a single instance call
virtual void solve(shared_ptr<ILinearAlgLoop> algLoop,bool first_solve = false) = 0;

virtual bool* getConditionsWorkArray()=0;
virtual bool* getConditions2WorkArray()=0;
virtual double* getVariableWorkArray()=0;
/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus() = 0;
virtual void stepCompleted(double time) = 0;
Expand Down
Expand Up @@ -40,7 +40,9 @@ class INonLinearAlgLoopSolver
virtual void solve() = 0;
//solve for a single instance call
virtual void solve(shared_ptr<INonLinearAlgLoop> algLoop,bool first_solve = false) = 0;

virtual bool* getConditionsWorkArray()=0;
virtual bool* getConditions2WorkArray()=0;
virtual double* getVariableWorkArray()=0;

/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus() = 0;
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Include/Core/System/ILinearAlgLoop.h
Expand Up @@ -29,7 +29,7 @@ class ILinearAlgLoop

/// Provide number (dimension) of variables according to the data type
virtual int getDimReal() const = 0;

virtual int getDimZeroFunc() const =0;
/// (Re-) initialize the system of equations
virtual void initialize() = 0;

Expand Down
Expand Up @@ -35,7 +35,7 @@ class INonLinearAlgLoop

/// Provide index of equation
virtual int getEquationIndex() const = 0;

virtual int getDimZeroFunc() const = 0;;
/// Provide number (dimension) of variables according to the data type
virtual int getDimReal() const = 0;

Expand Down
Expand Up @@ -27,7 +27,7 @@ class BOOST_EXTENSION_ALGLOOPDEFAULTIMPL_DECL LinearAlgLoopDefaultImplementation

/// Provide number (dimension) of variables according to data type
int getDimReal() const;

virtual int getDimZeroFunc() const;
/// (Re-) initialize the system of equations
void initialize();

Expand All @@ -38,12 +38,14 @@ class BOOST_EXTENSION_ALGLOOPDEFAULTIMPL_DECL LinearAlgLoopDefaultImplementation

void setUseSparseFormat(bool value);
virtual void getRealStartValues(double* vars) const;

//void getSparseAdata(double* data, int nonzeros);

// Member variables
//---------------------------------------------------------------
protected:
int _dimAEq; ///< Number (dimension) of unknown/equations (the index denotes the data type; 0: double, 1: int, 2: bool)
int _dimZeroFunc;
double* _b;
double* _x0;

Expand Down
Expand Up @@ -59,7 +59,7 @@ class BOOST_EXTENSION_ALGLOOPDEFAULTIMPL_DECL NonLinearAlgLoopDefaultImplementat

/// Provide number (dimension) of variables according to data type
int getDimReal() const;

virtual int getDimZeroFunc() const;
/// (Re-) initialize the system of equations
void initialize();

Expand All @@ -77,6 +77,7 @@ class BOOST_EXTENSION_ALGLOOPDEFAULTIMPL_DECL NonLinearAlgLoopDefaultImplementat
//---------------------------------------------------------------
protected:
int _dimAEq; ///< Number (dimension) of unknown/equations (the index denotes the data type; 0: double, 1: int, 2: bool)
int _dimZeroFunc;
double* _res;
double* _x0;

Expand Down
11 changes: 7 additions & 4 deletions SimulationRuntime/cpp/Include/Solver/Broyden/Broyden.h
Expand Up @@ -6,10 +6,9 @@
#pragma once

#include "FactoryExport.h"

#include <Core/Solver/AlgLoopSolverDefaultImplementation.h>

#include <Solver/Broyden/BroydenSettings.h>

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

/*****************************************************************************/
Expand Down Expand Up @@ -42,7 +41,7 @@ where A is an n-by-n matrix and y and B are n-by-n(right hand side) matrices.
/*****************************************************************************
OSMS(c) 2008
*****************************************************************************/
class Broyden : public INonLinearAlgLoopSolver
class Broyden : public INonLinearAlgLoopSolver, public AlgLoopSolverDefaultImplementation
{
public:

Expand All @@ -65,6 +64,10 @@ class Broyden : public INonLinearAlgLoopSolver
virtual void stepCompleted(double time);
virtual void restoreOldValues();
virtual void restoreNewValues();

virtual bool* getConditionsWorkArray();
virtual bool* getConditions2WorkArray();
virtual double* getVariableWorkArray();
private:
/// Encapsulation of determination of residuals to given unknowns
void calcFunction(const double* y, double* residual);
Expand All @@ -83,7 +86,7 @@ class Broyden : public INonLinearAlgLoopSolver
_iterationStatus; ///< Output - Denotes the status of iteration

long int
_dimSys, ///< Temp - Number of unknowns (=dimension of system of equations)

_lwork,
_iONE;

Expand Down
11 changes: 9 additions & 2 deletions SimulationRuntime/cpp/Include/Solver/Dgesv/DgesvSolver.h
Expand Up @@ -4,7 +4,11 @@
* @{
*/

class DgesvSolver : public ILinearAlgLoopSolver
#include "FactoryExport.h"
#include <Core/Solver/AlgLoopSolverDefaultImplementation.h>


class DgesvSolver : public ILinearAlgLoopSolver, public AlgLoopSolverDefaultImplementation
{
public:
DgesvSolver(ILinSolverSettings* settings,shared_ptr<ILinearAlgLoop> algLoop=shared_ptr<ILinearAlgLoop>());
Expand All @@ -24,6 +28,9 @@ class DgesvSolver : public ILinearAlgLoopSolver
virtual void restoreOldValues();
virtual void restoreNewValues();

virtual bool* getConditionsWorkArray();
virtual bool* getConditions2WorkArray();
virtual double* getVariableWorkArray();
private:
// Member variables
//---------------------------------------------------------------
Expand All @@ -34,7 +41,7 @@ class DgesvSolver : public ILinearAlgLoopSolver
_iterationStatus; ///< Output - Denotes the status of iteration

long int
_dimSys, ///< Number of unknowns (=dimension of system of equations)

*_iHelp, ///< Pivot indices for LAPACK routines
*_jHelp; ///< Pivot indices for LAPACK routines

Expand Down
11 changes: 8 additions & 3 deletions SimulationRuntime/cpp/Include/Solver/Hybrj/Hybrj.h
Expand Up @@ -4,6 +4,8 @@
* @{
*/
#include "FactoryExport.h"
#include <Core/Solver/AlgLoopSolverDefaultImplementation.h>

#include "HybrjSettings.h"

#if defined(__MINGW32__) || defined(_MSC_VER) /* we have static libcminpack.a on MinGW and MSVC */
Expand All @@ -16,7 +18,7 @@
see documentation: http://www.math.utah.edu/software/minpack/minpack/hybrj.html
*/

class Hybrj : public INonLinearAlgLoopSolver
class Hybrj : public INonLinearAlgLoopSolver, public AlgLoopSolverDefaultImplementation
{
public:

Expand All @@ -40,6 +42,10 @@ class Hybrj : public INonLinearAlgLoopSolver
virtual void restoreOldValues();
virtual void restoreNewValues();

virtual bool* getConditionsWorkArray();
virtual bool* getConditions2WorkArray();
virtual double* getVariableWorkArray();

private:
/// Encapsulation of determination of residuals to given unknowns
void calcFunction(const double* y, double* residual);
Expand All @@ -59,8 +65,7 @@ class Hybrj : public INonLinearAlgLoopSolver
ITERATIONSTATUS
_iterationStatus; ///< Output - Denotes the status of iteration

int
_dimSys; ///< Temp - Number of unknowns (=dimension of system of equations)


bool
_firstCall; ///< Temp - Denotes the first call to the solver, initialize() is called
Expand Down
14 changes: 11 additions & 3 deletions SimulationRuntime/cpp/Include/Solver/Kinsol/Kinsol.h
Expand Up @@ -4,7 +4,11 @@
* @{
*/

class Kinsol : public INonLinearAlgLoopSolver
#include "FactoryExport.h"
#include <Core/Solver/AlgLoopSolverDefaultImplementation.h>


class Kinsol : public INonLinearAlgLoopSolver, public AlgLoopSolverDefaultImplementation
{
public:
Kinsol(INonLinSolverSettings* settings,shared_ptr<INonLinearAlgLoop> algLoop=shared_ptr<INonLinearAlgLoop>());
Expand All @@ -22,6 +26,11 @@ class Kinsol : public INonLinearAlgLoopSolver
virtual void stepCompleted(double time);
virtual void restoreOldValues();
virtual void restoreNewValues();

virtual bool* getConditionsWorkArray();
virtual bool* getConditions2WorkArray();
virtual double* getVariableWorkArray();

int kin_f(N_Vector y, N_Vector fval, void *user_data);

/*will be used with new sundials version
Expand All @@ -48,8 +57,7 @@ class Kinsol : public INonLinearAlgLoopSolver
ITERATIONSTATUS
_iterationStatus; ///< Output - Denotes the status of iteration

long int
_dimSys; ///< Temp - Number of unknowns (=dimension of system of equations)

int _dim;
bool
_firstCall; ///< Temp - Denotes the first call to the solver, init() is called
Expand Down

0 comments on commit c5bc6e5

Please sign in to comment.