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 <sundials/sundials_context.h> /* SUNContext */
46#include <sundials/sundials_logger.h> /* SUNLogger */
47#include <kinsol/kinsol.h>
48#include <nvector/nvector_serial.h>
49#include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */
50
51namespace oms
52{
53 class System;
54 class DirectedGraph;
55
56 typedef struct KINSOL_USER_DATA {
59 const int algLoopNumber;
60 unsigned int iteration;
61 }KINSOL_USER_DATA;
62
64 {
65 public:
67 static KinsolSolver* NewKinsolSolver(const int algLoopNum, const unsigned int size, double relativeTolerance, const bool useDirectionalDerivative);
68 oms_status_enu_t kinsolSolve(System& syst, DirectedGraph& graph);
69
70 private:
71 /* All members start out empty, so that the destructor can be run on an
72 * object NewKinsolSolver() gave up on half way through. */
73
74 /* tolerances */
75 double fnormtol = 0.0; /* function tolerance */
76
77 /* work arrays */
78 N_Vector initialGuess = nullptr;
79 N_Vector uScale = nullptr; /* Scaling vector for u */
80 N_Vector fScale = nullptr; /* Scaling vector for f(u) */
81 N_Vector fTmp = nullptr; /* Vector used for tmp computations */
82
83 /* kinsol internal data */
84 SUNContext sunctx = nullptr; /* SUNDIALS simulation context */
85 void* kinsolMemory = nullptr;
86 void* user_data = nullptr;
87 int size = 0;
88
89 /* linear solver data */
90 SUNLinearSolver linSol = nullptr; /* Linear solver object used by KINSOL */
91 N_Vector y = nullptr; /* Template for cloning vectors needed inside linear solver */
92 SUNMatrix J = nullptr; /* (Non-)Sparse matrix template for cloning matrices needed within linear solver */
93
94 /* member function */
95 static int nlsKinsolJac(N_Vector u, N_Vector fu, SUNMatrix J, void *user_data, N_Vector tmp1, N_Vector tmp2);
96 static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data);
97 static void sundialsErrorHandlerFunction(int line, const char *func, const char *file, const char *msg,
98 SUNErrCode err_code, void *err_user_data, SUNContext sunctx);
99 };
100
102 {
103 public:
104 AlgLoop(oms_alg_solver_enu_t method, double relativeTolerance, scc_t SCC, const int systNumber, const bool useDirectionalDerivative);
105
106 scc_t getSCC() {return SCC;}
107 oms_status_enu_t solveAlgLoop(System& syst, DirectedGraph& graph);
108 std::string getAlgSolverName();
109 std::string dumpLoopVars(DirectedGraph& graph);
110
111 private:
112 oms_alg_solver_enu_t algSolverMethod;
113 oms_status_enu_t fixPointIteration(System& syst, DirectedGraph& graph);
114
115 /* Owns the solver: an AlgLoop is created as a temporary and moved into
116 * System::algLoops, so the copy constructor has to stay deleted. */
117 std::unique_ptr<KinsolSolver> kinsolData;
118
119 /* Loop data */
120 const scc_t SCC;
121 const int systNumber;
123 };
124}
125
126#endif
Definition AlgLoop.h:102
std::string getAlgSolverName()
Return solver method.
Definition AlgLoop.cpp:640
oms_alg_solver_enu_t algSolverMethod
Definition AlgLoop.h:112
const int systNumber
Definition AlgLoop.h:121
double relativeTolerance
Definition AlgLoop.h:122
const scc_t SCC
Strong connected components.
Definition AlgLoop.h:120
scc_t getSCC()
Definition AlgLoop.h:106
oms_status_enu_t solveAlgLoop(System &syst, DirectedGraph &graph)
Solve algebraic loop.
Definition AlgLoop.cpp:525
oms_status_enu_t fixPointIteration(System &syst, DirectedGraph &graph)
Fixed-point-iteration to solve algebraic loop.
Definition AlgLoop.cpp:549
std::string dumpLoopVars(DirectedGraph &graph)
Dump variables of algebraic loop.
Definition AlgLoop.cpp:662
std::unique_ptr< KinsolSolver > kinsolData
Definition AlgLoop.h:117
Definition DirectedGraph.h:71
Definition AlgLoop.h:64
SUNLinearSolver linSol
Definition AlgLoop.h:90
static void sundialsErrorHandlerFunction(int line, const char *func, const char *file, const char *msg, SUNErrCode err_code, void *err_user_data, SUNContext sunctx)
Error handler function of type SUNErrHandlerFn.
Definition AlgLoop.cpp:86
static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data)
Residual function for KINSOL.
Definition AlgLoop.cpp:187
void * kinsolMemory
Definition AlgLoop.h:85
N_Vector initialGuess
Definition AlgLoop.h:78
~KinsolSolver()
Destroy the oms::KinsolSolver::KinsolSolver object.
Definition AlgLoop.cpp:267
double fnormtol
Definition AlgLoop.h:75
static KinsolSolver * NewKinsolSolver(const int algLoopNum, const unsigned int size, double relativeTolerance, const bool useDirectionalDerivative)
Create new oms::KinsolSolver::KinsolSolver object.
Definition AlgLoop.cpp:293
N_Vector y
Definition AlgLoop.h:91
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:128
SUNContext sunctx
Definition AlgLoop.h:84
N_Vector fScale
Definition AlgLoop.h:80
SUNMatrix J
Definition AlgLoop.h:92
oms_status_enu_t kinsolSolve(System &syst, DirectedGraph &graph)
Solve algebraic system with KINSOL.
Definition AlgLoop.cpp:422
void * user_data
Definition AlgLoop.h:86
int size
Definition AlgLoop.h:87
N_Vector uScale
Definition AlgLoop.h:79
N_Vector fTmp
Definition AlgLoop.h:81
Definition System.h:66
Definition AlgLoop.h:52
Definition AlgLoop.h:56
System * syst
Definition AlgLoop.h:57
DirectedGraph * graph
Definition AlgLoop.h:58
unsigned int iteration
Definition AlgLoop.h:60
const int algLoopNumber
Definition AlgLoop.h:59
Strong connected components data type.
Definition DirectedGraph.h:59