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
48 namespace oms
49{
50 class System;
51 class DirectedGraph;
52
53 typedef struct KINSOL_USER_DATA {
54 System* syst;
55 DirectedGraph* graph;
56 const int algLoopNumber;
57 unsigned int iteration;
58 }KINSOL_USER_DATA;
59
60 class KinsolSolver
61 {
62 public:
63 ~KinsolSolver();
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 */
78 void* kinsolMemory;
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
108 KinsolSolver* kinsolData;
109
110 /* Loop data */
111 const scc_t SCC;
112 const int systNumber;
113 double relativeTolerance;
114 };
115}
116
117#endif
DirectedGraph.h
oms::AlgLoop
Definition AlgLoop.h:95
oms::AlgLoop::getAlgSolverName
std::string getAlgSolverName()
Return solver method.
Definition AlgLoop.cpp:625
oms::AlgLoop::algSolverMethod
oms_alg_solver_enu_t algSolverMethod
Definition AlgLoop.h:105
oms::AlgLoop::systNumber
const int systNumber
Definition AlgLoop.h:112
oms::AlgLoop::relativeTolerance
double relativeTolerance
Definition AlgLoop.h:113
oms::AlgLoop::SCC
const scc_t SCC
Strong connected components.
Definition AlgLoop.h:111
oms::AlgLoop::getSCC
scc_t getSCC()
Definition AlgLoop.h:99
oms::AlgLoop::solveAlgLoop
oms_status_enu_t solveAlgLoop(System &syst, DirectedGraph &graph)
Solve algebraic loop.
Definition AlgLoop.cpp:510
oms::AlgLoop::kinsolData
KinsolSolver * kinsolData
Definition AlgLoop.h:108
oms::AlgLoop::fixPointIteration
oms_status_enu_t fixPointIteration(System &syst, DirectedGraph &graph)
Fixed-point-iteration to solve algebraic loop.
Definition AlgLoop.cpp:534
oms::AlgLoop::dumpLoopVars
std::string dumpLoopVars(DirectedGraph &graph)
Dump variables of algebraic loop.
Definition AlgLoop.cpp:647
oms::DirectedGraph
Definition DirectedGraph.h:71
oms::KinsolSolver
Definition AlgLoop.h:61
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:73
oms::KinsolSolver::linSol
SUNLinearSolver linSol
Definition AlgLoop.h:83
oms::KinsolSolver::nlsKinsolResiduals
static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data)
Residual function for KINSOL.
Definition AlgLoop.cpp:195
oms::KinsolSolver::kinsolMemory
void * kinsolMemory
Definition AlgLoop.h:78
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:102
oms::KinsolSolver::initialGuess
N_Vector initialGuess
Definition AlgLoop.h:72
oms::KinsolSolver::~KinsolSolver
~KinsolSolver()
Destroy the oms::KinsolSolver::KinsolSolver object.
Definition AlgLoop.cpp:275
oms::KinsolSolver::fnormtol
double fnormtol
Definition AlgLoop.h:69
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:299
oms::KinsolSolver::y
N_Vector y
Definition AlgLoop.h:84
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:136
oms::KinsolSolver::fScale
N_Vector fScale
Definition AlgLoop.h:74
oms::KinsolSolver::J
SUNMatrix J
Definition AlgLoop.h:85
oms::KinsolSolver::kinsolSolve
oms_status_enu_t kinsolSolve(System &syst, DirectedGraph &graph)
Solve algebraic system with KINSOL.
Definition AlgLoop.cpp:407
oms::KinsolSolver::user_data
void * user_data
Definition AlgLoop.h:79
oms::KinsolSolver::size
int size
Definition AlgLoop.h:80
oms::KinsolSolver::uScale
N_Vector uScale
Definition AlgLoop.h:73
oms::KinsolSolver::fTmp
N_Vector fTmp
Definition AlgLoop.h:75
oms::System
Definition System.h:66
oms
Definition AlgLoop.h:49
oms::KINSOL_USER_DATA
Definition AlgLoop.h:53
oms::KINSOL_USER_DATA::syst
System * syst
Definition AlgLoop.h:54
oms::KINSOL_USER_DATA::graph
DirectedGraph * graph
Definition AlgLoop.h:55
oms::KINSOL_USER_DATA::iteration
unsigned int iteration
Definition AlgLoop.h:57
oms::KINSOL_USER_DATA::algLoopNumber
const int algLoopNumber
Definition AlgLoop.h:56
oms::scc_t
Strong connected components data type.
Definition DirectedGraph.h:59