Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into pullrequest
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenModelica-Hudson committed Oct 20, 2015
2 parents 19bd533 + 56438a0 commit 860b463
Show file tree
Hide file tree
Showing 18 changed files with 997 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Compiler/Template/CodegenCpp.tpl
Expand Up @@ -1854,7 +1854,7 @@ int _tmain(int argc, const _TCHAR* argv[])
{
throw std::runtime_error("error initialize");
}
fstream f;
std::fstream f;
f.open("output_rt.csv", ios::out);


Expand Down Expand Up @@ -2251,8 +2251,8 @@ extern "C" void <%modelname%>__FB_Init(<%modelname%>_FB_Init_struct* p)
ISimData* simData;

//double cycletime = p->instance->cycletime;
double cycletime;
getMotionCycle(cycletime);

int result = initSimulation(simController, simData, cycletime);
if (result < 0)
{
Expand Down Expand Up @@ -11977,7 +11977,7 @@ VAR_OUTPUT
<%outputVars%>
END_VAR
VAR
cycletime : REAL(0.05);
cycletime : REAL :=0.05;
bAlreadyInitialized : BOOL;
bErrorOccured : BOOL(FALSE);
controller : DWORD;
Expand Down
6 changes: 6 additions & 0 deletions SimulationRuntime/cpp/CMakeLists.txt
Expand Up @@ -72,6 +72,7 @@ SET(IdasName ${LIBPREFIX}Idas)
SET(KinsolName ${LIBPREFIX}Kinsol)
SET(ModelicaName ${LIBPREFIX}Modelica)
SET(NewtonName ${LIBPREFIX}Newton)
SET(BroydenName ${LIBPREFIX}Broyden)
SET(HybrjName ${LIBPREFIX}Hybrj)
SET(UmfPackName ${LIBPREFIX}UmfPack)
SET(DataExchangeName ${LIBPREFIX}DataExchange)
Expand Down Expand Up @@ -527,6 +528,7 @@ add_subdirectory (Solver/Euler)

add_subdirectory (Solver/RTEuler)
add_subdirectory (Solver/Newton)
add_subdirectory (Solver/Broyden)
add_subdirectory (Solver/Hybrj)
add_subdirectory (Solver/UmfPack)
add_subdirectory (Solver/Peer)
Expand Down Expand Up @@ -581,6 +583,9 @@ GET_FILENAME_COMPONENT(libRTRKName ${libRTRK} NAME)
GET_TARGET_PROPERTY(libNewton ${NewtonName} LOCATION)
GET_FILENAME_COMPONENT(libNewtonName ${libNewton} NAME)

GET_TARGET_PROPERTY(libBroyden ${BroydenName} LOCATION)
GET_FILENAME_COMPONENT(libBroydenName ${libBroyden} NAME)

GET_TARGET_PROPERTY(libUmfPack ${UmfPackName} LOCATION)
GET_FILENAME_COMPONENT(libUmfPackName ${libUmfPack} NAME)

Expand Down Expand Up @@ -633,6 +638,7 @@ set (RTEULER_LIB ${libRTEulerName})
set (SETTINGSFACTORY_LIB ${libSetFactoryName})
set (MODELICASYSTEM_LIB ${libModelicaName})
set (NEWTON_LIB ${libNewtonName})
set (BROYDEN_LIB ${libBroydenName})
set (UMFPACK_LIB ${libUmfPackName})
set (PEER_LIB ${libPeerName})
set (RTRK_LIB ${libRTRKName})
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/cpp/Include/Core/Modelica.h
Expand Up @@ -149,6 +149,7 @@ using std::runtime_error;

using boost::shared_ptr;
using boost::weak_ptr;
using boost::dynamic_pointer_cast;
#endif //USE_CPP_ELEVEN

#if defined(USE_THREAD)
Expand Down
Expand Up @@ -45,6 +45,18 @@ struct NonLinSolverOMCFactory : virtual public ObjectFactory<CreationPolicy>
}
nonlin_solver_key.assign("extension_export_newton");
}
else if(nonlin_solver.compare("broyden")==0)
{
PATH broyden_path = ObjectFactory<CreationPolicy>::_library_path;
PATH broyden_name(BROYDEN_LIB);
broyden_path/=broyden_name;
LOADERRESULT result = ObjectFactory<CreationPolicy>::_factory->LoadLibrary(broyden_path.string(),*_non_linsolver_type_map);
if (result != LOADER_SUCCESS)
{
throw ModelicaSimulationError(MODEL_FACTORY,"Failed loading Broyden solver library!");
}
nonlin_solver_key.assign("extension_export_broyden");
}
else if(nonlin_solver.compare("kinsol")==0)
{
PATH kinsol_path = ObjectFactory<CreationPolicy>::_library_path;
Expand Down
Expand Up @@ -26,6 +26,8 @@ struct NonLinSolverVxWorksFactory : virtual public ObjectFactory<CreationPolicy
string nonlin_solver;
if(solver_name.compare("newton")==0)
nonlin_solver_key.assign("createNewtonSettings");
else if(solver_name.compare("broyden")==0)
nonlin_solver_key.assign("createBroydenSettings");
else if(solver_name.compare("kinsol")==0)
nonlin_solver_key.assign("createKinsolSettings");
else if(solver_name.compare("Hybrj")==0)
Expand All @@ -44,6 +46,8 @@ struct NonLinSolverVxWorksFactory : virtual public ObjectFactory<CreationPolicy
string nonlin_solver_key;
if(solver_name.compare("newton")==0)
nonlin_solver_key.assign("createNewton");
if(solver_name.compare("broyden")==0)
nonlin_solver_key.assign("createBroyden");
else if(solver_name.compare("kinsol")==0)
nonlin_solver_key.assign("createKinsol");
else if(solver_name.compare("Hybrj")==0)
Expand Down
149 changes: 149 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Broyden/Broyden.h
@@ -0,0 +1,149 @@
/** @addtogroup solverBroyden
*
* @{
*/

#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/Broyden/BroydenSettings.h>

#include <Core/Utils/extension/logger.hpp>
#if defined(__vxworks)
//#include <klu.h>
#else
//#include <Solver/KLU/klu.h>
#endif

/*****************************************************************************/
/**
Damped Broyden-Raphson Method
The purpose of Broyden is to find a zero of a system F of n nonlinear functions in n
variables y of the form
F(t,y_1,...,y_n) = 0, (1)
or
f_1(t,y_1,...,y_n) = 0
... ...
f_n(t,y_1,...,y_n) = 0
by the use of an iterative Broyden method. The solution of the linear system is done
by Lapack/DGESV, which computes the solution to a real system of linear equations
A * y = B, (2)
where A is an n-by-n matrix and y and B are n-by-n(right hand side) matrices.
\date 2008, September, 16th
\author
*/
/*****************************************************************************
OSMS(c) 2008
*****************************************************************************/
class Broyden : public IAlgLoopSolver
{
public:

Broyden(IAlgLoop* algLoop,INonLinSolverSettings* settings);

virtual ~Broyden();

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

/// Solution of a (non-)linear system of equations
virtual void solve();

/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus();
virtual void stepCompleted(double time);
virtual void restoreOldValues();
virtual void restoreNewValues();
private:
/// Encapsulation of determination of residuals to given unknowns
void calcFunction(const double* y, double* residual);

/// Encapsulation of determination of Jacobian
void calcJacobian();

// Member variables
//---------------------------------------------------------------
INonLinSolverSettings
*_BroydenSettings; ///< Settings for the solver

IAlgLoop
*_algLoop; ///< Algebraic loop to be solved

ITERATIONSTATUS
_iterationStatus; ///< Output - Denotes the status of iteration

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

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

double
*_y, ///< Temp - Unknowns
*_yHelp,
*_fnew, ///< Temp - Residuals
*_fold, ///< Temp - Residuals
*_fHelp,
*_delta_s, ///< Temp - Auxillary variables
*_delta_b, ///< Temp - Auxillary variables
*_jac, ///< Temp - Jacobian
*_jacHelpMat1,
*_jacHelpMat2,
*_jacHelpVec1,
*_jacHelpVec2,
*_work;

int
_broydenMethod;

double _fNormTol,
_dONE,
_dZERO,
_dMINUSONE;


long int *_iHelp;

char
_N,
_T;

bool _sparse;


int _dim;
/*
klu_symbolic* _kluSymbolic ;
klu_numeric* _kluNumeric ;
klu_common* _kluCommon ;
int* _Ai;
int* _Ap;
double* _Ax;
int _nonzeros;
*/
long int* _ihelpArray;
double * _zeroVec;
double * _f ;




double* _identity;
};
/** @} */ // end of solverBroyden
38 changes: 38 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Broyden/BroydenSettings.h
@@ -0,0 +1,38 @@
#pragma once
/** @addtogroup solverBroyden
*
* @{
*/

#include <Core/Solver/INonLinSolverSettings.h>
class BroydenSettings :public INonLinSolverSettings
{
public:
BroydenSettings();

virtual ~BroydenSettings();
/*max. Anzahl an Broydenititerationen pro Schritt (default: 25)*/
virtual long int getNewtMax();
virtual void setNewtMax(long int);
/* Relative Toleranz für die Broydeniteration (default: 1e-6)*/
virtual double getRtol();
virtual void setRtol(double);
/*Absolute Toleranz für die Broydeniteration (default: 1e-6)*/
virtual double getAtol();
virtual void setAtol(double);
/*Dämpfungsfaktor (default: 0.9)*/
virtual double getDelta();
virtual void setDelta(double);
virtual void load(string);

virtual void setContinueOnError(bool);
virtual bool getContinueOnError();
private:
long int _iNewt_max; ///< max. Anzahl an Broydenititerationen pro Schritt (default: 25)

double _dRtol; ///< Relative Toleranz für die Broydeniteration (default: 1e-6)
double _dAtol; ///< Absolute Toleranz für die Broydeniteration (default: 1e-6)
double _dDelta; ///< Dämpfungsfaktor (default: 0.9)
bool _continueOnError;
};
/** @} */ // end of solverBroyden
19 changes: 19 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Broyden/FactoryExport.h
@@ -0,0 +1,19 @@
#pragma once
/** @defgroup solverNewton Solver.Broyden
* Nonlineaar solver class for Broyden methods
* @{
*/
#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
error "operating system not supported"
#endif
/** @} */ // end of solverBroyden


2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Include/Solver/Kinsol/Kinsol.h
Expand Up @@ -114,7 +114,7 @@ class Kinsol : public IAlgLoopSolver
int* _Ap;
double* _Ax;
int _nonzeros;
*/
*/
};
/** @} */ // end of solverKinsol
14 changes: 2 additions & 12 deletions SimulationRuntime/cpp/Include/Solver/Newton/Newton.h
Expand Up @@ -17,30 +17,20 @@

/*****************************************************************************/
/**
Damped Newton-Raphson Method
The purpose of Newton is to find a zero of a system F of n nonlinear functions in n
variables y of the form
F(t,y_1,...,y_n) = 0, (1)
or
f_1(t,y_1,...,y_n) = 0
... ...
f_n(t,y_1,...,y_n) = 0
by the use of an iterative Newton method. The solution of the linear system is done
by Lapack/DGESV, which computes the solution to a real system of linear equations
A * y = B, (2)
where A is an n-by-n matrix and y and B are n-by-n(right hand side) matrices.
\date 2008, September, 16th
\author
*/
/*****************************************************************************
OSMS(c) 2008
Expand Down Expand Up @@ -99,5 +89,5 @@ class Newton : public IAlgLoopSolver
*_y_new,
* _zeroVec;
long int *_iHelp;
};
/** @} */ // end of solverNewton

};/** @} */ // end of solverNewton
3 changes: 2 additions & 1 deletion SimulationRuntime/cpp/LibrariesConfig.h.in
@@ -1,4 +1,5 @@
#define KLU_LIB "@KLU_LIB@"
//#define KLU_LIB "@KLU_LIB@"
#define BROYDEN_LIB "@BROYDEN_LIB@"

#define EULER_LIB "@EULER_LIB@"
#define RTEULER_LIB "@RTEULER_LIB@"
Expand Down

0 comments on commit 860b463

Please sign in to comment.