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
51 namespace oms
52{
53 class System;
54 class DirectedGraph;
55
56 typedef struct KINSOL_USER_DATA {
57 System* syst;
58 DirectedGraph* graph;
59 const int algLoopNumber;
60 unsigned int iteration;
61 }KINSOL_USER_DATA;
62
63 class KinsolSolver
64 {
65 public:
66 ~KinsolSolver();
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
101 class AlgLoop
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;
122 double relativeTolerance;
123 };
124}
125
126#endif
DirectedGraph.h
oms::AlgLoop
Definition AlgLoop.h:102
oms::AlgLoop::getAlgSolverName
std::string getAlgSolverName()
Return solver method.
Definition AlgLoop.cpp:640
oms::AlgLoop::algSolverMethod
oms_alg_solver_enu_t algSolverMethod
Definition AlgLoop.h:112
oms::AlgLoop::systNumber
const int systNumber
Definition AlgLoop.h:121
oms::AlgLoop::relativeTolerance
double relativeTolerance
Definition AlgLoop.h:122
oms::AlgLoop::SCC
const scc_t SCC
Strong connected components.
Definition AlgLoop.h:120
oms::AlgLoop::getSCC
scc_t getSCC()
Definition AlgLoop.h:106
oms::AlgLoop::solveAlgLoop
oms_status_enu_t solveAlgLoop(System &syst, DirectedGraph &graph)
Solve algebraic loop.
Definition AlgLoop.cpp:525
oms::AlgLoop::fixPointIteration
oms_status_enu_t fixPointIteration(System &syst, DirectedGraph &graph)
Fixed-point-iteration to solve algebraic loop.
Definition AlgLoop.cpp:549
oms::AlgLoop::dumpLoopVars
std::string dumpLoopVars(DirectedGraph &graph)
Dump variables of algebraic loop.
Definition AlgLoop.cpp:662
oms::AlgLoop::kinsolData
std::unique_ptr< KinsolSolver > kinsolData
Definition AlgLoop.h:117
oms::DirectedGraph
Definition DirectedGraph.h:71
oms::KinsolSolver
Definition AlgLoop.h:64
oms::KinsolSolver::linSol
SUNLinearSolver linSol
Definition AlgLoop.h:90
oms::KinsolSolver::sundialsErrorHandlerFunction
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
oms::KinsolSolver::nlsKinsolResiduals
static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data)
Residual function for KINSOL.
Definition AlgLoop.cpp:187
oms::KinsolSolver::kinsolMemory
void * kinsolMemory
Definition AlgLoop.h:85
oms::KinsolSolver::initialGuess
N_Vector initialGuess
Definition AlgLoop.h:78
oms::KinsolSolver::~KinsolSolver
~KinsolSolver()
Destroy the oms::KinsolSolver::KinsolSolver object.
Definition AlgLoop.cpp:267
oms::KinsolSolver::fnormtol
double fnormtol
Definition AlgLoop.h:75
oms::KinsolSolver::NewKinsolSolver
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
oms::KinsolSolver::y
N_Vector y
Definition AlgLoop.h:91
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:128
oms::KinsolSolver::sunctx
SUNContext sunctx
Definition AlgLoop.h:84
oms::KinsolSolver::fScale
N_Vector fScale
Definition AlgLoop.h:80
oms::KinsolSolver::J
SUNMatrix J
Definition AlgLoop.h:92
oms::KinsolSolver::kinsolSolve
oms_status_enu_t kinsolSolve(System &syst, DirectedGraph &graph)
Solve algebraic system with KINSOL.
Definition AlgLoop.cpp:422
oms::KinsolSolver::user_data
void * user_data
Definition AlgLoop.h:86
oms::KinsolSolver::size
int size
Definition AlgLoop.h:87
oms::KinsolSolver::uScale
N_Vector uScale
Definition AlgLoop.h:79
oms::KinsolSolver::fTmp
N_Vector fTmp
Definition AlgLoop.h:81
oms::System
Definition System.h:66
oms
Definition AlgLoop.h:52
oms::KINSOL_USER_DATA
Definition AlgLoop.h:56
oms::KINSOL_USER_DATA::syst
System * syst
Definition AlgLoop.h:57
oms::KINSOL_USER_DATA::graph
DirectedGraph * graph
Definition AlgLoop.h:58
oms::KINSOL_USER_DATA::iteration
unsigned int iteration
Definition AlgLoop.h:60
oms::KINSOL_USER_DATA::algLoopNumber
const int algLoopNumber
Definition AlgLoop.h:59
oms::scc_t
Strong connected components data type.
Definition DirectedGraph.h:59