OMSimulatorLib
The OMSimulator project is a FMI-based co-simulation environment that supports ordinary (i.e., non-delayed) and TLM connections.
AlgLoop.h
Go to the documentation of this file.
1 /*
2  * This file is part of OpenModelica.
3  *
4  * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5  * c/o Linköpings universitet, Department of Computer and Information Science,
6  * SE-58183 Linköping, Sweden.
7  *
8  * All rights reserved.
9  *
10  * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11  * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12  * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13  * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14  * ACCORDING TO RECIPIENTS CHOICE.
15  *
16  * The OpenModelica software and the Open Source Modelica
17  * Consortium (OSMC) Public License (OSMC-PL) are obtained
18  * from OSMC, either from the above address,
19  * from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20  * http://www.openmodelica.org, and in the OpenModelica distribution.
21  * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22  *
23  * This program is distributed WITHOUT ANY WARRANTY; without
24  * even the implied warranty of MERCHANTABILITY or FITNESS
25  * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26  * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27  *
28  * See the full OSMC Public License conditions for more details.
29  *
30  */
31 
32 #ifndef _OMS_ALGLOOP_H_
33 #define _OMS_ALGLOOP_H_
34 
35 #include <string>
36 #include <vector>
37 #include "OMSimulator/Types.h"
38 #include "DirectedGraph.h"
39 
40 #include <kinsol/kinsol.h>
41 #include <nvector/nvector_serial.h>
42 #include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */
43 
44 namespace oms
45 {
46  class System;
47  class DirectedGraph;
48 
49  typedef struct KINSOL_USER_DATA {
52  const int algLoopNumber;
53  unsigned int iteration;
55 
57  {
58  public:
59  ~KinsolSolver();
60  static KinsolSolver* NewKinsolSolver(const int algLoopNum, const unsigned int size, double absoluteTolerance, const bool useDirectionalDerivative);
61  oms_status_enu_t kinsolSolve(System& syst, DirectedGraph& graph);
62 
63  private:
64  /* tolerances */
65  double fnormtol; /* function tolerance */
66 
67  /* work arrays */
68  N_Vector initialGuess;
69  N_Vector uScale; /* Scaling vector for u */
70  N_Vector fScale; /* Scaling vector for f(u) */
71  N_Vector fTmp; /* Vector used for tmp computations */
72 
73  /* kinsol internal data */
74  void* kinsolMemory;
75  void* user_data;
76  int size;
77 
78  /* linear solver data */
79  SUNLinearSolver linSol; /* Linear solver object used by KINSOL */
80  N_Vector y; /* Template for cloning vectors needed inside linear solver */
81  SUNMatrix J; /* (Non-)Sparse matrix template for cloning matrices needed within linear solver */
82 
83  /* member function */
84  static int nlsKinsolJac(N_Vector u, N_Vector fu, SUNMatrix J, void *user_data, N_Vector tmp1, N_Vector tmp2);
85  static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data);
86  static void sundialsErrorHandlerFunction(int error_code, const char *module, const char *function, char *msg, void *user_data);
87  static void sundialsInfoHandlerFunction(const char *module, const char *function, char *msg, void *user_data);
88  };
89 
90  class AlgLoop
91  {
92  public:
93  AlgLoop(oms_alg_solver_enu_t method, double absTol, scc_t SCC, const int systNumber, const bool useDirectionalDerivative);
94 
95  scc_t getSCC() {return SCC;}
96  oms_status_enu_t solveAlgLoop(System& syst, DirectedGraph& graph);
97  std::string getAlgSolverName();
98  std::string dumpLoopVars(DirectedGraph& graph);
99 
100  private:
101  oms_alg_solver_enu_t algSolverMethod;
102  oms_status_enu_t fixPointIteration(System& syst, DirectedGraph& graph);
103 
105 
106  /* Loop data */
107  const scc_t SCC;
108  const int systNumber;
110  };
111 }
112 
113 #endif
oms
Definition: AlgLoop.h:44
System.h
oms::KINSOL_USER_DATA::graph
DirectedGraph * graph
Definition: AlgLoop.h:51
oms::KinsolSolver::sundialsInfoHandlerFunction
static void sundialsInfoHandlerFunction(const char *module, const char *function, char *msg, void *user_data)
Info handler function given to KINSOL.
Definition: AlgLoop.cpp:98
oms::KINSOL_USER_DATA
struct oms::KINSOL_USER_DATA KINSOL_USER_DATA
oms::ComRef::front
ComRef front() const
returns the first part of the ComRef (including suffix if its the only part)
Definition: ComRef.cpp:166
oms::KinsolSolver::sundialsErrorHandlerFunction
static void sundialsErrorHandlerFunction(int error_code, const char *module, const char *function, char *msg, void *user_data)
Error handler function given to KINSOL.
Definition: AlgLoop.cpp:69
oms::scc_t
Strong connected components data type.
Definition: DirectedGraph.h:53
oms::AlgLoop::AlgLoop
AlgLoop(oms_alg_solver_enu_t method, double absTol, scc_t SCC, const int systNumber, const bool useDirectionalDerivative)
Construct a new oms::AlgLoop::AlgLoop object.
Definition: AlgLoop.cpp:470
oms::ComRef
ComRef - component reference.
Definition: ComRef.h:46
oms::KINSOL_USER_DATA::syst
System * syst
Definition: AlgLoop.h:50
oms::AlgLoop::algSolverMethod
oms_alg_solver_enu_t algSolverMethod
Definition: AlgLoop.h:101
oms::Flags::DumpAlgLoops
static bool DumpAlgLoops()
Definition: Flags.h:66
oms::KinsolSolver::nlsKinsolJac
static int nlsKinsolJac(N_Vector u, N_Vector fu, SUNMatrix J, void *user_data, N_Vector tmp1, N_Vector tmp2)
Jacobian function for KINSOL.
Definition: AlgLoop.cpp:132
oms::KinsolSolver::initialGuess
N_Vector initialGuess
Definition: AlgLoop.h:68
oms::System::getDirectionalDerivative
oms_status_enu_t getDirectionalDerivative(const ComRef &unknownCref, const ComRef &knownCref, double &value)
Definition: System.cpp:2369
oms::AlgLoop::fixPointIteration
oms_status_enu_t fixPointIteration(System &syst, DirectedGraph &graph)
Fixed-point-iteration to solve algebraic loop.
Definition: AlgLoop.cpp:528
oms::System::setReal
oms_status_enu_t setReal(const ComRef &cref, double value)
Definition: System.cpp:2554
oms::System::getAlgLoop
AlgLoop * getAlgLoop(const int systemNumber)
Definition: System.cpp:3038
oms::Flags::MaxLoopIteration
static unsigned int MaxLoopIteration()
Definition: Flags.h:91
logInfo
#define logInfo(msg)
Definition: Logging.h:100
DirectedGraph.h
oms::KinsolSolver::~KinsolSolver
~KinsolSolver()
Destroy the oms::KinsolSolver::KinsolSolver object.
Definition: AlgLoop.cpp:271
oms::KINSOL_USER_DATA::iteration
unsigned int iteration
Definition: AlgLoop.h:53
oms::DirectedGraph
Definition: DirectedGraph.h:64
oms::KinsolSolver::uScale
N_Vector uScale
Definition: AlgLoop.h:69
oms::KinsolSolver::kinsolSolve
oms_status_enu_t kinsolSolve(System &syst, DirectedGraph &graph)
Solve algebraic system with KINSOL.
Definition: AlgLoop.cpp:403
oms::KinsolSolver::NewKinsolSolver
static KinsolSolver * NewKinsolSolver(const int algLoopNum, const unsigned int size, double absoluteTolerance, const bool useDirectionalDerivative)
Create new oms::KinsolSolver::KinsolSolver object.
Definition: AlgLoop.cpp:295
logDebug
#define logDebug(msg)
Definition: Logging.h:109
oms::System::getReal
oms_status_enu_t getReal(const ComRef &cref, double &value)
Definition: System.cpp:2241
oms::AlgLoop::getAlgSolverName
std::string getAlgSolverName()
Return solver method.
Definition: AlgLoop.cpp:619
oms::KinsolSolver::fnormtol
double fnormtol
Definition: AlgLoop.h:65
AlgLoop.h
oms::KinsolSolver::linSol
SUNLinearSolver linSol
Definition: AlgLoop.h:79
oms::Flags::DirectionalDerivatives
static bool DirectionalDerivatives()
Definition: Flags.h:65
logWarning
#define logWarning(msg)
Definition: Logging.h:101
oms::KinsolSolver::y
N_Vector y
Definition: AlgLoop.h:80
oms::KinsolSolver::kinsolMemory
void * kinsolMemory
Definition: AlgLoop.h:74
oms::KinsolSolver::user_data
void * user_data
Definition: AlgLoop.h:75
checkFlag
bool checkFlag(int flag, std::string functionName)
Check flag returned by KINSOL function and log error.
Definition: AlgLoop.cpp:49
oms::KINSOL_USER_DATA
Definition: AlgLoop.h:49
Component.h
oms::AlgLoop::kinsolData
KinsolSolver * kinsolData
Definition: AlgLoop.h:104
Log::DebugEnabled
static bool DebugEnabled()
Definition: Logging.cpp:187
oms::AlgLoop::solveAlgLoop
oms_status_enu_t solveAlgLoop(System &syst, DirectedGraph &graph)
Solve algebraic loop.
Definition: AlgLoop.cpp:504
oms::System::getTime
double getTime() const
Definition: System.h:147
oms::KinsolSolver::fTmp
N_Vector fTmp
Definition: AlgLoop.h:71
oms::AlgLoop
Definition: AlgLoop.h:90
oms::System
Definition: System.h:65
oms::KinsolSolver::fScale
N_Vector fScale
Definition: AlgLoop.h:70
oms::KINSOL_USER_DATA::algLoopNumber
const int algLoopNumber
Definition: AlgLoop.h:52
oms::AlgLoop::getSCC
scc_t getSCC()
Definition: AlgLoop.h:95
oms::AlgLoop::absoluteTolerance
double absoluteTolerance
Definition: AlgLoop.h:109
logError
#define logError(msg)
Definition: Logging.h:102
oms::KinsolSolver::nlsKinsolResiduals
static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data)
Residual function for KINSOL.
Definition: AlgLoop.cpp:191
oms::AlgLoop::dumpLoopVars
std::string dumpLoopVars(DirectedGraph &graph)
Dump variables of algebraic loop.
Definition: AlgLoop.cpp:641
oms::KinsolSolver
Definition: AlgLoop.h:56
oms::ComRef::pop_front
ComRef pop_front()
returns the first part of the ComRef and removed it from the current object
Definition: ComRef.cpp:184
oms::scc_t::connections
std::vector< std::pair< int, int > > connections
Definition: DirectedGraph.h:55
oms::DirectedGraph::getNodes
const std::vector< Connector > & getNodes() const
Definition: DirectedGraph.h:81
Flags.h
oms::KinsolSolver::J
SUNMatrix J
Definition: AlgLoop.h:81
oms::AlgLoop::SCC
const scc_t SCC
Strong connected components.
Definition: AlgLoop.h:107
oms::KinsolSolver::size
int size
Definition: AlgLoop.h:76
oms::AlgLoop::systNumber
const int systNumber
Definition: AlgLoop.h:108