Skip to content

Commit

Permalink
Whitespaces Newton solver
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke committed Apr 6, 2016
1 parent cf3ceac commit 325d842
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 343 deletions.
4 changes: 1 addition & 3 deletions SimulationRuntime/cpp/Include/Solver/Newton/FactoryExport.h
Expand Up @@ -12,8 +12,6 @@
#define BOOST_EXTENSION_SOLVER_DECL BOOST_EXTENSION_IMPORT_DECL
#define BOOST_EXTENSION_SOLVERSETTINGS_DECL BOOST_EXTENSION_IMPORT_DECL
#else
error "operating system not supported"
error "operating system not supported"
#endif
/** @} */ // end of solverNewton


111 changes: 55 additions & 56 deletions SimulationRuntime/cpp/Include/Solver/Newton/Newton.h
Expand Up @@ -13,77 +13,76 @@

/*****************************************************************************/
/**
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
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
*****************************************************************************/
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
*****************************************************************************/
class Newton : public IAlgLoopSolver
{
public:
public:
Newton(IAlgLoop* algLoop,INonLinSolverSettings* settings);

Newton(IAlgLoop* algLoop,INonLinSolverSettings* settings);
virtual ~Newton();

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

/// (Re-) initialize the solver
virtual void initialize();
/// Solution of a (non-)linear system of equations
virtual void solve();

/// 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);

/// 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();

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

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

// Member variables
//---------------------------------------------------------------
INonLinSolverSettings
*_newtonSettings; ///< Settings for the solver
IAlgLoop
*_algLoop; ///< Algebraic loop to be solved

IAlgLoop
*_algLoop; ///< Algebraic loop to be solved
ITERATIONSTATUS
_iterationStatus; ///< Output - Denotes the status of iteration

ITERATIONSTATUS
_iterationStatus; ///< Output - Denotes the status of iteration
long int
_dimSys; ///< Temp - Number of unknowns (=dimension of system of equations)

long int
_dimSys; ///< Temp - Number of unknowns (=dimension of system of equations)
bool
_firstCall; ///< Temp - Denotes the first call to the solver, init() is called

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

double
*_y, ///< Temp - Unknowns
*_f, ///< Temp - Residuals
*_yHelp, ///< Temp - Auxillary variables
*_fHelp, ///< Temp - Auxillary variables
*_jac, ///< Temp - Jacobian
*_y_old,
*_y_new,
* _zeroVec;
double
*_y, ///< Temp - Unknowns
*_f, ///< Temp - Residuals
*_yHelp, ///< Temp - Auxillary variables
*_fHelp, ///< Temp - Auxillary variables
*_jac, ///< Temp - Jacobian
*_y_old,
*_y_new,
* _zeroVec;
long int *_iHelp;

};/** @} */ // end of solverNewton
};/** @} */ // end of solverNewton
50 changes: 26 additions & 24 deletions SimulationRuntime/cpp/Include/Solver/Newton/NewtonSettings.h
Expand Up @@ -5,34 +5,36 @@
*/

#include <Core/Solver/INonLinSolverSettings.h>

class NewtonSettings :public INonLinSolverSettings
{
public:
NewtonSettings();
public:
NewtonSettings();

virtual ~NewtonSettings();
/*max. Anzahl an Newtonititerationen pro Schritt (default: 25)*/
virtual long int getNewtMax();
virtual void setNewtMax(long int);
/* Relative Toleranz für die Newtoniteration (default: 1e-6)*/
virtual double getRtol();
virtual void setRtol(double);
/*Absolute Toleranz für die Newtoniteration (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 ~NewtonSettings();
/*max. Anzahl an Newtonititerationen pro Schritt (default: 50)*/
virtual long int getNewtMax();
virtual void setNewtMax(long int);
/* Relative Toleranz für die Newtoniteration (default: 1e-6)*/
virtual double getRtol();
virtual void setRtol(double);
/*Absolute Toleranz für die Newtoniteration (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 Newtonititerationen pro Schritt (default: 25)
virtual void setContinueOnError(bool);
virtual bool getContinueOnError();
private:
long int _iNewt_max; ///< max. Anzahl an Newtonititerationen pro Schritt (default: 25)

double _dRtol; ///< Relative Toleranz für die Newtoniteration (default: 1e-6)
double _dAtol; ///< Absolute Toleranz für die Newtoniteration (default: 1e-6)
double _dDelta; ///< Dämpfungsfaktor (default: 0.9)
bool _continueOnError;
double _dRtol; ///< Relative Toleranz für die Newtoniteration (default: 1e-6)
double _dAtol; ///< Absolute Toleranz für die Newtoniteration (default: 1e-6)
double _dDelta; ///< Dämpfungsfaktor (default: 0.9)
bool _continueOnError;
};

/** @} */ // end of solverNewton
41 changes: 21 additions & 20 deletions SimulationRuntime/cpp/Solver/Newton/FactoryExport.cpp
Expand Up @@ -2,6 +2,7 @@
*
* @{
*/

#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>
#if defined(__vxworks)
Expand All @@ -10,46 +11,46 @@

extern "C" IAlgLoopSolver* createNewton(IAlgLoop* algLoop, INonLinSolverSettings* settings)
{
return new Newton(algLoop, settings);
return new Newton(algLoop, settings);
}

extern "C" INonLinSolverSettings* createNewtonSettings()
{
return new NewtonSettings();
return new NewtonSettings();
}


#elif defined(OMC_BUILD) && !defined(RUNTIME_STATIC_LINKING)

#include <Solver/Newton/Newton.h>
#include <Solver/Newton/NewtonSettings.h>

/* OMC factory */
using boost::extensions::factory;
/* OMC factory */
using boost::extensions::factory;

BOOST_EXTENSION_TYPE_MAP_FUNCTION {
types.get<std::map<std::string, factory<IAlgLoopSolver,IAlgLoop*, INonLinSolverSettings*> > >()
["newton"].set<Newton>();
types.get<std::map<std::string, factory<INonLinSolverSettings> > >()
["newtonSettings"].set<NewtonSettings>();
}
}

#elif defined(OMC_BUILD) && defined(RUNTIME_STATIC_LINKING)
#include <Solver/Newton/Newton.h>
#include <Solver/Newton/NewtonSettings.h>
shared_ptr<INonLinSolverSettings> createNewtonSettings()
{
shared_ptr<INonLinSolverSettings> settings = shared_ptr<INonLinSolverSettings>(new NewtonSettings());
return settings;
}
shared_ptr<IAlgLoopSolver> createNewtonSolver(IAlgLoop* algLoop, shared_ptr<INonLinSolverSettings> solver_settings)
{
shared_ptr<IAlgLoopSolver> solver = shared_ptr<IAlgLoopSolver>(new Newton(algLoop,solver_settings.get()));
return solver;
}
#else
error "operating system not supported"
#endif
/** @} */ // end of solverNewton

shared_ptr<INonLinSolverSettings> createNewtonSettings()
{
shared_ptr<INonLinSolverSettings> settings = shared_ptr<INonLinSolverSettings>(new NewtonSettings());
return settings;
}

shared_ptr<IAlgLoopSolver> createNewtonSolver(IAlgLoop* algLoop, shared_ptr<INonLinSolverSettings> solver_settings)
{
shared_ptr<IAlgLoopSolver> solver = shared_ptr<IAlgLoopSolver>(new Newton(algLoop,solver_settings.get()));
return solver;
}

#else
error "operating system not supported"
#endif
/** @} */ // end of solverNewton

0 comments on commit 325d842

Please sign in to comment.