forked from OPM/opm-polymer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGravityColumnSolverPolymer_impl.hpp
339 lines (302 loc) · 13.2 KB
/
GravityColumnSolverPolymer_impl.hpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
Copyright 2012 SINTEF ICT, Applied Mathematics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Copyright 2012 SINTEF ICT, Applied Mathematics.
This file is part of the Open Porous Media project (OPM).
OPM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OPM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opm/polymer/GravityColumnSolverPolymer.hpp>
#include <opm/core/linalg/blas_lapack.h>
#include <opm/core/utility/ErrorMacros.hpp>
#include <iterator>
namespace Opm
{
template <class FluxModel, class Model>
GravityColumnSolverPolymer<FluxModel, Model>::GravityColumnSolverPolymer(FluxModel& fmodel,
const Model& model,
const UnstructuredGrid& grid,
const double tol,
const int maxit)
: fmodel_(fmodel), model_(model), grid_(grid), tol_(tol), maxit_(maxit)
{
}
namespace {
struct ZeroVec
{
double operator[](int) const { return 0.0; }
};
struct StateWithZeroFlux
{
StateWithZeroFlux(std::vector<double>& s, std::vector<double>& c, std::vector<double>& cmax_arg) : sat(s), cpoly(c), cmax(cmax_arg) {}
const ZeroVec& faceflux() const { return zv; }
const std::vector<double>& saturation() const { return sat; }
std::vector<double>& saturation() { return sat; }
const std::vector<double>& concentration() const { return cpoly; }
std::vector<double>& concentration() { return cpoly; }
const std::vector<double>& maxconcentration() const { return cmax; }
std::vector<double>& maxconcentration() { return cmax; }
ZeroVec zv;
std::vector<double>& sat;
std::vector<double>& cpoly;
std::vector<double>& cmax;
};
struct Vecs
{
Vecs(int sz) : sol(sz, 0.0) {}
const std::vector<double>& solution() const { return sol; }
std::vector<double>& writableSolution() { return sol; }
std::vector<double> sol;
};
struct JacSys
{
JacSys(int sz) : v(sz) {}
const Vecs& vector() const { return v; }
Vecs& vector() { return v; }
Vecs v;
typedef std::vector<double> vector_type;
};
struct BandMatrixCoeff
{
BandMatrixCoeff(int N, int ku, int kl) : N_(N), ku_(ku), kl_(kl), nrow_(2*kl + ku + 1) {
}
// compute the position where to store the coefficient of a matrix A_{i,j} (i,j=0,...,N-1)
// in a array which is sent to the band matrix solver of LAPACK.
int operator ()(int i, int j) const {
return kl_ + ku_ + i - j + j*nrow_;
}
const int N_;
const int ku_;
const int kl_;
const int nrow_;
};
} // anon namespace
/// \param[in] columns for each column col, columns[col]
/// contains the cells on which to solve the segregation
/// problem. For each column, its cells must be in a single
/// vertical column, connected and ordered
/// (direction doesn't matter).
template <class FluxModel, class Model>
void GravityColumnSolverPolymer<FluxModel, Model>::solve(const std::vector<std::vector<int> >& columns,
const double dt,
std::vector<double>& s,
std::vector<double>& c,
std::vector<double>& cmax
)
{
// Initialize model. These things are done for the whole grid!
StateWithZeroFlux state(s, c, cmax); // This holds s, c and cmax by reference.
JacSys sys(2*grid_.number_of_cells);
std::vector<double> increment(2*grid_.number_of_cells, 0.0);
fmodel_.initStep(state, grid_, sys);
int iter = 0;
double max_delta = 1e100;
const double cmax_cell = 2.0*model_.cMax();
const double tol_c_cell = 1e-2*cmax_cell;
while (iter < maxit_) {
fmodel_.initIteration(state, grid_, sys);
int size = columns.size();
for(int i = 0; i < size; ++i) {
solveSingleColumn(columns[i], dt, s, c, cmax, increment);
}
for (int cell = 0; cell < grid_.number_of_cells; ++cell) {
double& s_cell = sys.vector().writableSolution()[2*cell + 0];
double& c_cell = sys.vector().writableSolution()[2*cell + 1];
s_cell += increment[2*cell + 0];
c_cell += increment[2*cell + 1];
if (s_cell < 0.) {
double& incr = increment[2*cell + 0];
s_cell -= incr;
if (std::fabs(incr) < 1e-2) {
incr = -s_cell;
s_cell = 0.;
} else {
incr = -s_cell/2.0;
s_cell = s_cell/2.0;
}
}
if (s_cell > 1.) {
double& incr = increment[2*cell + 0];
s_cell -= incr;
if (std::fabs(incr) < 1e-2) {
incr = 1. - s_cell;
s_cell = 1.;
} else {
incr = (1 - s_cell)/2.0;
s_cell = (1 + s_cell)/2.0;
}
}
if (c_cell < 0.) {
double& incr = increment[2*cell + 1];
c_cell -= incr;
if (std::fabs(incr) < tol_c_cell) {
incr = -c_cell;
c_cell = 0.;
} else {
incr = -c_cell/2.0;
c_cell = c_cell/2.0;
}
}
if (c_cell > cmax_cell) {
double& incr = increment[2*cell + 1];
c_cell -= incr;
if (std::fabs(incr) < tol_c_cell) {
incr = cmax_cell - c_cell;
c_cell = cmax_cell;
} else {
incr = (cmax_cell - c_cell)/2.0;
c_cell = (cmax_cell + c_cell)/2.0;
}
}
// if (s_cell < 0.) {
// increment[2*cell + 0] = increment[2*cell + 0] - s_cell;
// s_cell = 0.;
// } else if (s_cell > 1.) {
// increment[2*cell + 0] = increment[2*cell + 0] - s_cell + 1.;
// s_cell = 1.;
// }
// if (c_cell < 0) {
// increment[2*cell + 1] = increment[2*cell + 1] - c_cell;
// c_cell = 0.;
// } else if (c_cell > cmax_cell) {
// increment[2*cell + 1] = increment[2*cell + 1] - c_cell + cmax_cell;
// c_cell = cmax_cell;
// }
}
const double maxelem = *std::max_element(increment.begin(), increment.end());
const double minelem = *std::min_element(increment.begin(), increment.end());
max_delta = std::max(maxelem, -minelem);
std::cout << "Iteration " << iter << " max_delta = " << max_delta << std::endl;
if (max_delta < tol_) {
break;
}
++iter;
}
if (max_delta >= tol_) {
THROW("Failed to converge!");
}
// Finalize.
// fmodel_.finishIteration(); //
// finishStep() writes to state, which holds s by reference.
// This will update the entire grid's state... cmax is updated here.
fmodel_.finishStep(grid_, sys.vector().solution(), state);
}
/// \param[in] column_cells the cells on which to solve the segregation
/// problem. Must be in a single vertical column,
/// and ordered (direction doesn't matter).
template <class FluxModel, class Model>
void GravityColumnSolverPolymer<FluxModel, Model>::solveSingleColumn(const std::vector<int>& column_cells,
const double dt,
std::vector<double>& s,
std::vector<double>& c,
std::vector<double>& cmax,
std::vector<double>& sol_vec)
{
// This is written only to work with SinglePointUpwindTwoPhase,
// not with arbitrary problem models.
int col_size = column_cells.size();
// if (col_size == 1) {
// sol_vec[2*column_cells[0] + 0] = 0.0;
// sol_vec[2*column_cells[0] + 1] = 0.0;
// return;
// }
StateWithZeroFlux state(s, c, cmax); // This holds s by reference.
// Assemble.
const int kl = 3;
const int ku = 3;
const int nrow = 2*kl + ku + 1;
const int N = 2*col_size; // N unknowns: s and c for each cell.
std::vector<double> hm(nrow*N, 0.0); // band matrix with 3 upper and 3 lower diagonals.
std::vector<double> rhs(N, 0.0);
const BandMatrixCoeff bmc(N, ku, kl);
for (int ci = 0; ci < col_size; ++ci) {
std::vector<double> F(2, 0.);
std::vector<double> dFd1(4, 0.);
std::vector<double> dFd2(4, 0.);
std::vector<double> dF(4, 0.);
const int cell = column_cells[ci];
const int prev_cell = (ci == 0) ? -999 : column_cells[ci - 1];
const int next_cell = (ci == col_size - 1) ? -999 : column_cells[ci + 1];
// model_.initResidual(cell, F);
for (int j = grid_.cell_facepos[cell]; j < grid_.cell_facepos[cell+1]; ++j) {
const int face = grid_.cell_faces[j];
const int c1 = grid_.face_cells[2*face + 0];
const int c2 = grid_.face_cells[2*face + 1];
if (c1 == prev_cell || c2 == prev_cell || c1 == next_cell || c2 == next_cell) {
F.assign(2, 0.);
dFd1.assign(4, 0.);
dFd2.assign(4, 0.);
fmodel_.fluxConnection(state, grid_, dt, cell, face, &F[0], &dFd1[0], &dFd2[0]);
if (c1 == prev_cell || c2 == prev_cell) {
hm[bmc(2*ci + 0, 2*(ci - 1) + 0)] += dFd2[0];
hm[bmc(2*ci + 0, 2*(ci - 1) + 1)] += dFd2[1];
hm[bmc(2*ci + 1, 2*(ci - 1) + 0)] += dFd2[2];
hm[bmc(2*ci + 1, 2*(ci - 1) + 1)] += dFd2[3];
} else {
ASSERT(c1 == next_cell || c2 == next_cell);
hm[bmc(2*ci + 0, 2*(ci + 1) + 0)] += dFd2[0];
hm[bmc(2*ci + 0, 2*(ci + 1) + 1)] += dFd2[1];
hm[bmc(2*ci + 1, 2*(ci + 1) + 0)] += dFd2[2];
hm[bmc(2*ci + 1, 2*(ci + 1) + 1)] += dFd2[3];
}
hm[bmc(2*ci + 0, 2*ci + 0)] += dFd1[0];
hm[bmc(2*ci + 0, 2*ci + 1)] += dFd1[1];
hm[bmc(2*ci + 1, 2*ci + 0)] += dFd1[2];
hm[bmc(2*ci + 1, 2*ci + 1)] += dFd1[3];
rhs[2*ci + 0] += F[0];
rhs[2*ci + 1] += F[1];
}
}
F.assign(2, 0.);
dF.assign(4, 0.);
fmodel_.accumulation(grid_, cell, &F[0], &dF[0]);
hm[bmc(2*ci + 0, 2*ci + 0)] += dF[0];
hm[bmc(2*ci + 0, 2*ci + 1)] += dF[1];
hm[bmc(2*ci + 1, 2*ci + 0)] += dF[2];
if (std::abs(dF[3]) < 1e-12) {
hm[bmc(2*ci + 1, 2*ci + 1)] += 1e-12;
} else {
hm[bmc(2*ci + 1, 2*ci + 1)] += dF[3];
}
rhs[2*ci + 0] += F[0];
rhs[2*ci + 1] += F[1];
}
// model_.sourceTerms(); // Not needed
// Solve.
const int num_rhs = 1;
int info = 0;
std::vector<int> ipiv(N, 0);
// Solution will be written to rhs.
dgbsv_(&N, &kl, &ku, &num_rhs, &hm[0], &nrow, &ipiv[0], &rhs[0], &N, &info);
if (info != 0) {
std::cerr << "Failed column cells: ";
std::copy(column_cells.begin(), column_cells.end(), std::ostream_iterator<int>(std::cerr, " "));
std::cerr << "\n";
THROW("Lapack reported error in dgtsv: " << info);
}
for (int ci = 0; ci < col_size; ++ci) {
sol_vec[2*column_cells[ci] + 0] = -rhs[2*ci + 0];
sol_vec[2*column_cells[ci] + 1] = -rhs[2*ci + 1];
}
}
} // namespace Opm