Ignis
Loading...
Searching...
No Matches
solver_kinsol.h
Go to the documentation of this file.
1#pragma once
2
3#include <kinsol/kinsol.h> // access to KINSOL func., consts.
4#include <nvector/nvector_serial.h> // access to serial N_Vector
5#include <sunmatrix/sunmatrix_dense.h> // access to dense SUNMatrix
6#include <sunlinsol/sunlinsol_dense.h> // access to dense SUNLinearSolver
7#include <sunmatrix/sunmatrix_band.h> // access to dense SUNMatrix
8#include <sunlinsol/sunlinsol_band.h> // access to dense SUNLinearSolver
9#include <sundials/sundials_types.h> // defs. of realtype, sunindextype
10
11#include <vector>
12#include <iostream>
13
20
22
24
25public:
26
27 void *user_data;
28 size_t nvar;
29
30 SUNContext sun;
31 void *kmem;
32 N_Vector vars;
33 N_Vector scales_v;
34 N_Vector scales_f;
35 N_Vector constraints;
36 SUNMatrix J;
37 SUNLinearSolver LS;
38 int rv;
41
43
58
60 void *_user_data,
61 const size_t _nvar,
62 const std::vector<double> &_scales_v,
63 const std::vector<double> &_scales_f,
64 const std::vector<double> &_constraints,
65 const int mu,
66 const int ml,
67 const double ftol = 1E-5,
68 const double stol = 1E-5) :
69 user_data(_user_data),
70 nvar(_nvar) {
71
72 solver_type = KIN_LINESEARCH;
74
75 rv = SUNContext_Create(NULL, &sun);
76
77 vars = N_VNew_Serial(nvar, sun);
78 scales_v = N_VNew_Serial(nvar, sun);
79 scales_f = N_VNew_Serial(nvar, sun);
80 constraints = N_VNew_Serial(nvar, sun);
81
82 // J = SUNDenseMatrix(nvar, nvar, sun); // linear solver matrix J
83 J = SUNBandMatrix(nvar, mu, ml, sun); // linear solver matrix J
84
85 for(size_t k=0; k<nvar; k++) {
86 NV_Ith_S(scales_v, k) = _scales_v[k];
87 NV_Ith_S(scales_f, k) = _scales_f[k];
88 NV_Ith_S(constraints, k) = _constraints[k];
89 }
90
91 kmem = KINCreate(sun);
92
93 rv = KINSetUserData( kmem, user_data);
94 rv = KINSetConstraints( kmem, constraints);
95 rv = KINSetScaledStepTol(kmem, stol);
96 rv = KINSetFuncNormTol( kmem, ftol);
97 rv = KINSetMaxSetupCalls(kmem, exact_or_modified_newton);
98 rv = KINSetNumMaxIters( kmem, 5000);
99}
100
108
109int solve(int (*Func)(N_Vector, N_Vector, void*),
110 std::vector<double> &y) {
111
112 //---------
113
114 for(size_t k=0; k<nvar; k++)
115 NV_Ith_S(vars, k) = y[k];
116
117 rv = KINInit(kmem, Func, vars);
118 // LS = SUNLinSol_Dense(vars, J, sun); // set linear solver
119 LS = SUNLinSol_Band(vars, J, sun); // set linear solver
120 rv = KINSetLinearSolver(kmem, LS, J); // associate matrix J with solver LS
121
122 //---------
123
124 rv = KINSol(kmem, vars, solver_type, scales_v, scales_f);
125
126 //---------
127
128 for(size_t k=0; k<nvar; k++)
129 y[k] = NV_Ith_S(vars,k);
130
131 return rv;
132}
133
139
141 N_VDestroy(vars);
142 N_VDestroy(scales_v);
143 N_VDestroy(scales_f);
144 N_VDestroy(constraints);
145 KINFree(&kmem);
146 SUNMatDestroy(J);
147 SUNLinSolFree(LS);
148 SUNContext_Free(&sun);
149}
150
151};
solver_kinsol(void *_user_data, const size_t _nvar, const std::vector< double > &_scales_v, const std::vector< double > &_scales_f, const std::vector< double > &_constraints, const int mu, const int ml, const double ftol=1E-5, const double stol=1E-5)
int solve(int(*Func)(N_Vector, N_Vector, void *), std::vector< double > &y)
N_Vector scales_v
scales for variables being solved: f(v)
size_t nvar
number of variables being solved
N_Vector vars
variables being solved
SUNContext sun
sundials object
void * user_data
pointer to user data/functions
SUNLinearSolver LS
linear solver
int rv
function return value
int exact_or_modified_newton
1 or 0, respectively
void * kmem
kinsol object
int solver_type
KIN_NONE, KIN_LINESEARCH, KIN_FP, KIN_PICARD.
N_Vector constraints
constraints on the scalars (e.g., >0 etc.)
SUNMatrix J
Jacobian matrix.
N_Vector scales_f
scales for functions being solved: f(v)