-
Notifications
You must be signed in to change notification settings - Fork 49
/
AlgLoop.h
113 lines (96 loc) · 3.83 KB
/
AlgLoop.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#ifndef _OMS_ALGLOOP_H_
#define _OMS_ALGLOOP_H_
#include <string>
#include <vector>
#include "OMSimulator/Types.h"
#include "DirectedGraph.h"
#include <kinsol/kinsol.h>
#include <nvector/nvector_serial.h>
#include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */
namespace oms
{
class System;
class DirectedGraph;
typedef struct KINSOL_USER_DATA {
System* syst;
DirectedGraph* graph;
const int algLoopNumber;
unsigned int iteration;
}KINSOL_USER_DATA;
class KinsolSolver
{
public:
~KinsolSolver();
static KinsolSolver* NewKinsolSolver(const int algLoopNum, const unsigned int size, double absoluteTolerance, const bool useDirectionalDerivative);
oms_status_enu_t kinsolSolve(System& syst, DirectedGraph& graph);
private:
/* tolerances */
double fnormtol; /* function tolerance */
/* work arrays */
N_Vector initialGuess;
N_Vector uScale; /* Scaling vector for u */
N_Vector fScale; /* Scaling vector for f(u) */
N_Vector fTmp; /* Vector used for tmp computations */
/* kinsol internal data */
void* kinsolMemory;
void* user_data;
int size;
/* linear solver data */
SUNLinearSolver linSol; /* Linear solver object used by KINSOL */
N_Vector y; /* Template for cloning vectors needed inside linear solver */
SUNMatrix J; /* (Non-)Sparse matrix template for cloning matrices needed within linear solver */
/* member function */
static int nlsKinsolJac(N_Vector u, N_Vector fu, SUNMatrix J, void *user_data, N_Vector tmp1, N_Vector tmp2);
static int nlsKinsolResiduals(N_Vector u, N_Vector fval, void *user_data);
static void sundialsErrorHandlerFunction(int error_code, const char *module, const char *function, char *msg, void *user_data);
static void sundialsInfoHandlerFunction(const char *module, const char *function, char *msg, void *user_data);
};
class AlgLoop
{
public:
AlgLoop(oms_alg_solver_enu_t method, double absTol, scc_t SCC, const int systNumber, const bool useDirectionalDerivative);
scc_t getSCC() {return SCC;}
oms_status_enu_t solveAlgLoop(System& syst, DirectedGraph& graph);
std::string getAlgSolverName();
std::string dumpLoopVars(DirectedGraph& graph);
private:
oms_alg_solver_enu_t algSolverMethod;
oms_status_enu_t fixPointIteration(System& syst, DirectedGraph& graph);
KinsolSolver* kinsolData;
/* Loop data */
const scc_t SCC; ///< Strong connected components
const int systNumber;
double absoluteTolerance;
};
}
#endif