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 <string>
40#include <vector>
41#include "OMSimulator/Types.h"
42#include "DirectedGraph.h"
43
44#include <kinsol/kinsol.h>
45#include <nvector/nvector_serial.h>
46#include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */
47
48namespace oms
49{
50 class System;
51 class DirectedGraph;
52
53 typedef struct KINSOL_USER_DATA {
56 const int algLoopNumber;
57 unsigned int iteration;
58 }KINSOL_USER_DATA;
59
61 {
62 public:
64 static KinsolSolver* NewKinsolSolver(const int algLoopNum, const unsigned int size, double relativeTolerance, const bool useDirectionalDerivative);
65 oms_status_enu_t kinsolSolve(System& syst, DirectedGraph& graph);
66
67 private:
68 /* tolerances */
69 double fnormtol; /* function tolerance */
70
71 /* work arrays */
72 N_Vector initialGuess;
73 N_Vector uScale; /* Scaling vector for u */
74 N_Vector fScale; /* Scaling vector for f(u) */
75 N_Vector fTmp; /* Vector used for tmp computations */
76
77 /* kinsol internal data */
79 void* user_data;
80 int size;
81
82 /* linear solver data */
83 SUNLinearSolver linSol; /* Linear solver object used by KINSOL */
84 N_Vector y; /* Template for cloning vectors needed inside linear solver */
85 SUNMatrix J; /* (Non-)Sparse matrix template for cloning matrices needed within linear solver */
86
87 /* member function */
88 static int nlsKinsolJac(N_Vector u, N_Vector fu, SUNMatrix J, void *user_data, N_Vector tmp1, N_Vector tmp2);
89 static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data);
90 static void sundialsErrorHandlerFunction(int error_code, const char *module, const char *function, char *msg, void *user_data);
91 static void sundialsInfoHandlerFunction(const char *module, const char *function, char *msg, void *user_data);
92 };
93
94 class AlgLoop
95 {
96 public:
97 AlgLoop(oms_alg_solver_enu_t method, double relativeTolerance, scc_t SCC, const int systNumber, const bool useDirectionalDerivative);
98
99 scc_t getSCC() {return SCC;}
100 oms_status_enu_t solveAlgLoop(System& syst, DirectedGraph& graph);
101 std::string getAlgSolverName();
102 std::string dumpLoopVars(DirectedGraph& graph);
103
104 private:
105 oms_alg_solver_enu_t algSolverMethod;
106 oms_status_enu_t fixPointIteration(System& syst, DirectedGraph& graph);
107
109
110 /* Loop data */
111 const scc_t SCC;
112 const int systNumber;
114 };
115}
116
117#endif
Definition AlgLoop.h:95
std::string getAlgSolverName()
Return solver method.
Definition AlgLoop.cpp:625
oms_alg_solver_enu_t algSolverMethod
Definition AlgLoop.h:105
const int systNumber
Definition AlgLoop.h:112
double relativeTolerance
Definition AlgLoop.h:113
const scc_t SCC
Strong connected components.
Definition AlgLoop.h:111
scc_t getSCC()
Definition AlgLoop.h:99
oms_status_enu_t solveAlgLoop(System &syst, DirectedGraph &graph)
Solve algebraic loop.
Definition AlgLoop.cpp:510
KinsolSolver * kinsolData
Definition AlgLoop.h:108
oms_status_enu_t fixPointIteration(System &syst, DirectedGraph &graph)
Fixed-point-iteration to solve algebraic loop.
Definition AlgLoop.cpp:534
std::string dumpLoopVars(DirectedGraph &graph)
Dump variables of algebraic loop.
Definition AlgLoop.cpp:647
Definition DirectedGraph.h:71
Definition AlgLoop.h:61
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:83
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:78
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:72
~KinsolSolver()
Destroy the oms::KinsolSolver::KinsolSolver object.
Definition AlgLoop.cpp:275
double fnormtol
Definition AlgLoop.h:69
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:84
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:74
SUNMatrix J
Definition AlgLoop.h:85
oms_status_enu_t kinsolSolve(System &syst, DirectedGraph &graph)
Solve algebraic system with KINSOL.
Definition AlgLoop.cpp:407
void * user_data
Definition AlgLoop.h:79
int size
Definition AlgLoop.h:80
N_Vector uScale
Definition AlgLoop.h:73
N_Vector fTmp
Definition AlgLoop.h:75
Definition System.h:66
Definition AlgLoop.h:49
Definition AlgLoop.h:53
System * syst
Definition AlgLoop.h:54
DirectedGraph * graph
Definition AlgLoop.h:55
unsigned int iteration
Definition AlgLoop.h:57
const int algLoopNumber
Definition AlgLoop.h:56
Strong connected components data type.
Definition DirectedGraph.h:59