OMSimulatorLib
The OMSimulator project is a FMI-based co-simulation environment.
Loading...
Searching...
No Matches
AlgLoop.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2026, 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 AGPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
14 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs:
19 * http://www.openmodelica.org or
20 * https://github.com/OpenModelica/ or
21 * http://www.ida.liu.se/projects/OpenModelica,
22 * and in the OpenModelica distribution.
23 *
24 * GNU AGPL version 3 is obtained from:
25 * https://www.gnu.org/licenses/licenses.html#GPL
26 *
27 * This program is distributed WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
30 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
31 *
32 * See the full OSMC Public License conditions for more details.
33 *
34 */
35
36#ifndef _OMS_ALGLOOP_H_
37#define _OMS_ALGLOOP_H_
38
39#include <memory>
40#include <string>
41#include <vector>
42#include "OMSimulator/Types.h"
43#include "DirectedGraph.h"
44
45#include <kinsol/kinsol.h>
46#include <nvector/nvector_serial.h>
47#include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */
48
49namespace oms
50{
51 class System;
52 class DirectedGraph;
53
54 typedef struct KINSOL_USER_DATA {
57 const int algLoopNumber;
58 unsigned int iteration;
59 }KINSOL_USER_DATA;
60
62 {
63 public:
65 static KinsolSolver* NewKinsolSolver(const int algLoopNum, const unsigned int size, double relativeTolerance, const bool useDirectionalDerivative);
66 oms_status_enu_t kinsolSolve(System& syst, DirectedGraph& graph);
67
68 private:
69 /* All members start out empty, so that the destructor can be run on an
70 * object NewKinsolSolver() gave up on half way through. */
71
72 /* tolerances */
73 double fnormtol = 0.0; /* function tolerance */
74
75 /* work arrays */
76 N_Vector initialGuess = nullptr;
77 N_Vector uScale = nullptr; /* Scaling vector for u */
78 N_Vector fScale = nullptr; /* Scaling vector for f(u) */
79 N_Vector fTmp = nullptr; /* Vector used for tmp computations */
80
81 /* kinsol internal data */
82 void* kinsolMemory = nullptr;
83 void* user_data = nullptr;
84 int size = 0;
85
86 /* linear solver data */
87 SUNLinearSolver linSol = nullptr; /* Linear solver object used by KINSOL */
88 N_Vector y = nullptr; /* Template for cloning vectors needed inside linear solver */
89 SUNMatrix J = nullptr; /* (Non-)Sparse matrix template for cloning matrices needed within linear solver */
90
91 /* member function */
92 static int nlsKinsolJac(N_Vector u, N_Vector fu, SUNMatrix J, void *user_data, N_Vector tmp1, N_Vector tmp2);
93 static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data);
94 static void sundialsErrorHandlerFunction(int error_code, const char *module, const char *function, char *msg, void *user_data);
95 static void sundialsInfoHandlerFunction(const char *module, const char *function, char *msg, void *user_data);
96 };
97
98 class AlgLoop
99 {
100 public:
101 AlgLoop(oms_alg_solver_enu_t method, double relativeTolerance, scc_t SCC, const int systNumber, const bool useDirectionalDerivative);
102
103 scc_t getSCC() {return SCC;}
104 oms_status_enu_t solveAlgLoop(System& syst, DirectedGraph& graph);
105 std::string getAlgSolverName();
106 std::string dumpLoopVars(DirectedGraph& graph);
107
108 private:
109 oms_alg_solver_enu_t algSolverMethod;
110 oms_status_enu_t fixPointIteration(System& syst, DirectedGraph& graph);
111
112 /* Owns the solver: an AlgLoop is created as a temporary and moved into
113 * System::algLoops, so the copy constructor has to stay deleted. */
114 std::unique_ptr<KinsolSolver> kinsolData;
115
116 /* Loop data */
117 const scc_t SCC;
118 const int systNumber;
120 };
121}
122
123#endif
Definition AlgLoop.h:99
std::string getAlgSolverName()
Return solver method.
Definition AlgLoop.cpp:627
oms_alg_solver_enu_t algSolverMethod
Definition AlgLoop.h:109
const int systNumber
Definition AlgLoop.h:118
double relativeTolerance
Definition AlgLoop.h:119
const scc_t SCC
Strong connected components.
Definition AlgLoop.h:117
scc_t getSCC()
Definition AlgLoop.h:103
oms_status_enu_t solveAlgLoop(System &syst, DirectedGraph &graph)
Solve algebraic loop.
Definition AlgLoop.cpp:512
oms_status_enu_t fixPointIteration(System &syst, DirectedGraph &graph)
Fixed-point-iteration to solve algebraic loop.
Definition AlgLoop.cpp:536
std::string dumpLoopVars(DirectedGraph &graph)
Dump variables of algebraic loop.
Definition AlgLoop.cpp:649
std::unique_ptr< KinsolSolver > kinsolData
Definition AlgLoop.h:114
Definition DirectedGraph.h:71
Definition AlgLoop.h:62
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:73
SUNLinearSolver linSol
Definition AlgLoop.h:87
static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data)
Residual function for KINSOL.
Definition AlgLoop.cpp:195
void * kinsolMemory
Definition AlgLoop.h:82
static void sundialsInfoHandlerFunction(const char *module, const char *function, char *msg, void *user_data)
Info handler function given to KINSOL.
Definition AlgLoop.cpp:102
N_Vector initialGuess
Definition AlgLoop.h:76
~KinsolSolver()
Destroy the oms::KinsolSolver::KinsolSolver object.
Definition AlgLoop.cpp:275
double fnormtol
Definition AlgLoop.h:73
static KinsolSolver * NewKinsolSolver(const int algLoopNum, const unsigned int size, double relativeTolerance, const bool useDirectionalDerivative)
Create new oms::KinsolSolver::KinsolSolver object.
Definition AlgLoop.cpp:299
N_Vector y
Definition AlgLoop.h:88
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:136
N_Vector fScale
Definition AlgLoop.h:78
SUNMatrix J
Definition AlgLoop.h:89
oms_status_enu_t kinsolSolve(System &syst, DirectedGraph &graph)
Solve algebraic system with KINSOL.
Definition AlgLoop.cpp:409
void * user_data
Definition AlgLoop.h:83
int size
Definition AlgLoop.h:84
N_Vector uScale
Definition AlgLoop.h:77
N_Vector fTmp
Definition AlgLoop.h:79
Definition System.h:66
Definition AlgLoop.h:50
Definition AlgLoop.h:54
System * syst
Definition AlgLoop.h:55
DirectedGraph * graph
Definition AlgLoop.h:56
unsigned int iteration
Definition AlgLoop.h:58
const int algLoopNumber
Definition AlgLoop.h:57
Strong connected components data type.
Definition DirectedGraph.h:59