-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslopeLimiter.cpp
373 lines (332 loc) · 16.7 KB
/
slopeLimiter.cpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "slopeLimiter.h"
#include "problem.h"
template <EquationsType equationsType, int dim>
void VertexBasedSlopeLimiter<equationsType, dim>::flush_cache()
{
this->postprocessData.clear();
}
template <EquationsType equationsType, int dim>
void VertexBasedSlopeLimiter<equationsType, dim>::postprocess(TrilinosWrappers::MPI::Vector& current_limited_solution, TrilinosWrappers::MPI::Vector& current_unlimited_solution)
{
int cell_count = 0;
// Loop through all cells.
for (typename DoFHandler<dim>::active_cell_iterator cell = this->dof_handler.begin_active(); cell != this->dof_handler.end(); ++cell)
{
if (!cell->is_locally_owned())
continue;
bool u_c_set[Equations<equationsType, dim>::n_components];
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
u_c_set[i] = false;
double u_c[Equations<equationsType, dim>::n_components];
cell->get_dof_indices(this->dof_indices);
typename SlopeLimiter<equationsType, dim>::PostprocessData* data = 0;
auto it = this->postprocessData.find(cell->active_cell_index());
if (it != this->postprocessData.end())
data = &(it->second);
else
{
data = &(((this->postprocessData.insert(std::make_pair(cell->active_cell_index(), typename SlopeLimiter<equationsType, dim>::PostprocessData()))).first)->second);
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
u_c_set[i] = false;
for (unsigned int i = 0; i < this->dofs_per_cell; ++i)
{
if (!this->is_primitive[i])
data->lambda_indices_to_multiply_all_B_components.push_back(this->dof_indices[i]);
else
{
// Here we rely on the fact, that the constant basis fn is the first one and all other basis fns come after.
if (!u_c_set[this->component_ii[i]])
u_c_set[this->component_ii[i]] = true;
else
data->lambda_indices_to_multiply[this->component_ii[i]].push_back(this->dof_indices[i]);
}
}
for (unsigned int vertex_i = 0; vertex_i < GeometryInfo<dim>::vertices_per_cell; ++vertex_i)
data->vertex_is_at_nonperiodic_boundary[vertex_i] = false;
for (unsigned int face = 0; face<GeometryInfo<dim>::faces_per_cell; ++face)
if (cell->at_boundary(face) && !(this->parameters.is_periodic_boundary(cell->face(face)->boundary_id())))
for (unsigned int v = 0; v < GeometryInfo<dim>::vertices_per_face; ++v)
{
int index_ = GeometryInfo<dim>::face_to_cell_vertices(face, v);
data->vertex_is_at_nonperiodic_boundary[index_] = true;
}
data->center = cell->center();
for (unsigned int vertex_i = 0; vertex_i < GeometryInfo<dim>::vertices_per_cell; ++vertex_i)
{
data->vertexIndex[vertex_i] = cell->vertex_index(vertex_i);
data->vertexPoint[vertex_i] = data->center + (1. - NEGLIGIBLE) * (cell->vertex(vertex_i) - data->center);
unsigned short neighbor_i = 0;
for (auto neighbor_element : GridTools::find_cells_adjacent_to_vertex(this->triangulation, data->vertexIndex[vertex_i]))
{
typename DoFHandler<dim>::active_cell_iterator neighbor(&this->triangulation, neighbor_element->level(), neighbor_element->index(), &this->dof_handler);
if (neighbor->active_cell_index() != cell->active_cell_index())
{
data->neighbor_dof_indices[vertex_i][neighbor_i].resize(this->dofs_per_cell);
neighbor->get_dof_indices(data->neighbor_dof_indices[vertex_i][neighbor_i++]);
}
}
data->neighbor_count = neighbor_i;
}
}
// Cell center value we must find in any case (new data or reused)
// - let us reuse this array for that.
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
u_c_set[i] = false;
for (unsigned int i = 0; i < this->dofs_per_cell; ++i)
{
if (this->is_primitive[i])
{
// Here we rely on the fact, that the constant basis fn is the first one and that all other basis fns have zero mean.
if (!u_c_set[this->component_ii[i]])
{
u_c[this->component_ii[i]] = current_unlimited_solution(this->dof_indices[i]);
u_c_set[this->component_ii[i]] = true;
}
}
}
if (this->parameters.debug & this->parameters.SlopeLimiting)
LOGL(2, "cell: " << ++cell_count << " - center: " << data->center << ", values: " << u_c[0] << ", " << u_c[1] << ", " << u_c[2] << ", " << u_c[3] << ", " << u_c[4]);
double alpha_e[Equations<equationsType, dim>::n_components];
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
alpha_e[i] = 1.;
for (unsigned int vertex_i = 0; vertex_i < GeometryInfo<dim>::vertices_per_cell; ++vertex_i)
{
if (!this->parameters.limit_edges_and_vertices && data->neighbor_count < 4 && data->vertex_is_at_nonperiodic_boundary[vertex_i])
continue;
// (!!!) Find out u_i
Vector<double> u_i(Equations<equationsType, dim>::n_components);
const Point<dim> p_cell = this->mapping.transform_real_to_unit_cell(cell, data->vertexPoint[vertex_i]);
const Quadrature<dim> one_point_quadrature(GeometryInfo<dim>::project_to_unit_cell(p_cell));
FEValues<dim> fe_values(this->mapping, this->fe, one_point_quadrature, update_values);
fe_values.reinit(cell);
std::vector<Vector<double> > u_value(1, Vector<double>(this->fe.n_components()));
fe_values.get_function_values(current_unlimited_solution, u_value);
u_i = u_value[0];
if (this->parameters.debug & this->parameters.SlopeLimiting)
{
LOGL(3, "\tv_i: " << cell->vertex(vertex_i) << ", values: ");
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
LOGL(4, u_i[i] << (i == Equations<equationsType, dim>::n_components - 1 ? "" : ", "));
}
// Init u_i_min, u_i_max
double u_i_min[Equations<equationsType, dim>::n_components];
double u_i_max[Equations<equationsType, dim>::n_components];
for (int k = 0; k < Equations<equationsType, dim>::n_components; k++)
{
u_i_min[k] = u_c[k];
u_i_max[k] = u_c[k];
}
// For all vertices -> v_i
for (auto dof_indices_neighbor : data->neighbor_dof_indices[vertex_i])
{
if (dof_indices_neighbor.size() == 0)
continue;
bool u_i_extrema_set[Equations<equationsType, dim>::n_components];
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
u_i_extrema_set[i] = false;
for (unsigned int i = 0; i < this->dofs_per_cell; ++i)
{
if (this->is_primitive[i])
{
// Here we rely on the fact, that the constant basis fn is the first one.
if (!u_i_extrema_set[this->component_ii[i]])
{
double val = current_unlimited_solution(dof_indices_neighbor[i]);
if (this->parameters.debug & this->parameters.SlopeLimiting)
{
if (val < u_i_min[this->component_ii[i]])
LOGL(3, "\tdecreasing u_i_min to: " << val);
if (val > u_i_max[this->component_ii[i]])
LOGL(3, "\tincreasing u_i_max to: " << val);
}
u_i_min[this->component_ii[i]] = std::min(u_i_min[this->component_ii[i]], val);
u_i_max[this->component_ii[i]] = std::max(u_i_max[this->component_ii[i]], val);
u_i_extrema_set[this->component_ii[i]] = true;
}
}
}
}
// Based on u_i_min, u_i_max, u_i, get alpha_e
for (int k = 0; k < Equations<equationsType, dim>::n_components; k++)
{
if (std::abs(u_c[k]) < SMALL)
continue;
if (std::abs((u_c[k] - u_i[k]) / u_c[k]) > NEGLIGIBLE)
{
alpha_e[k] = std::min(alpha_e[k], ((u_i[k] - u_c[k]) > 0.) ? std::min(1.0, (u_i_max[k] - u_c[k]) / (u_i[k] - u_c[k])) : std::min(1.0, (u_i_min[k] - u_c[k]) / (u_i[k] - u_c[k])));
if (this->parameters.debug & this->parameters.SlopeLimiting)
LOGL(1, "\talpha_e[" << k << "]: " << alpha_e[k]);
}
}
}
for (int k = 0; k < 5; k++)
for (int i = 0; i < data->lambda_indices_to_multiply[k].size(); i++)
current_limited_solution(data->lambda_indices_to_multiply[k][i]) *= alpha_e[k];
if (this->parameters.limitB)
{
double mag_alpha = std::min(std::min(alpha_e[5], alpha_e[6]), alpha_e[7]);
for (int k = 5; k < Equations<equationsType, dim>::n_components; k++)
for (int i = 0; i < data->lambda_indices_to_multiply[k].size(); i++)
current_limited_solution(data->lambda_indices_to_multiply[k][i]) *= mag_alpha;
for (int i = 0; i < data->lambda_indices_to_multiply_all_B_components.size(); i++)
current_limited_solution(data->lambda_indices_to_multiply_all_B_components[i]) *= mag_alpha;
}
}
}
template <EquationsType equationsType, int dim>
void BarthJespersenSlopeLimiter<equationsType, dim>::flush_cache()
{
this->postprocessData.clear();
}
template <EquationsType equationsType, int dim>
void BarthJespersenSlopeLimiter<equationsType, dim>::postprocess(TrilinosWrappers::MPI::Vector& current_limited_solution, TrilinosWrappers::MPI::Vector& current_unlimited_solution)
{
int cell_count = 0;
// Loop through all cells.
for (typename DoFHandler<dim>::active_cell_iterator cell = this->dof_handler.begin_active(); cell != this->dof_handler.end(); ++cell)
{
if (!cell->is_locally_owned())
continue;
bool u_c_set[Equations<equationsType, dim>::n_components];
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
u_c_set[i] = false;
double u_c[Equations<equationsType, dim>::n_components];
cell->get_dof_indices(this->dof_indices);
typename SlopeLimiter<equationsType, dim>::PostprocessData* data = 0;
auto it = this->postprocessData.find(cell->active_cell_index());
if (it != this->postprocessData.end())
data = &(it->second);
else
{
data = &(((this->postprocessData.insert(std::make_pair(cell->active_cell_index(), typename SlopeLimiter<equationsType, dim>::PostprocessData()))).first)->second);
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
u_c_set[i] = false;
for (unsigned int i = 0; i < this->dofs_per_cell; ++i)
{
if (!this->is_primitive[i])
data->lambda_indices_to_multiply_all_B_components.push_back(this->dof_indices[i]);
else
{
// Here we rely on the fact, that the constant basis fn is the first one and all other basis fns come after.
if (!u_c_set[this->component_ii[i]])
u_c_set[this->component_ii[i]] = true;
else
data->lambda_indices_to_multiply[this->component_ii[i]].push_back(this->dof_indices[i]);
}
}
data->center = cell->center();
for (unsigned int vertex_i = 0; vertex_i < GeometryInfo<dim>::vertices_per_cell; ++vertex_i)
{
data->vertexIndex[vertex_i] = cell->vertex_index(vertex_i);
data->vertexPoint[vertex_i] = data->center + (1. - NEGLIGIBLE) * (cell->vertex(vertex_i) - data->center);
unsigned short neighbor_i = 0;
for (auto neighbor_element : GridTools::find_cells_adjacent_to_vertex(this->triangulation, data->vertexIndex[vertex_i]))
{
typename DoFHandler<dim>::active_cell_iterator neighbor(&this->triangulation, neighbor_element->level(), neighbor_element->index(), &this->dof_handler);
if (neighbor->active_cell_index() != cell->active_cell_index())
{
data->neighbor_dof_indices[vertex_i][neighbor_i].resize(this->dofs_per_cell);
neighbor->get_dof_indices(data->neighbor_dof_indices[vertex_i][neighbor_i++]);
}
}
}
}
// Cell center value we must find in any case (new data or reused)
// - let us reuse this array for that.
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
u_c_set[i] = false;
for (unsigned int i = 0; i < this->dofs_per_cell; ++i)
{
if (this->is_primitive[i])
{
// Here we rely on the fact, that the constant basis fn is the first one and that all other basis fns have zero mean.
if (!u_c_set[this->component_ii[i]])
{
u_c[this->component_ii[i]] = current_unlimited_solution(this->dof_indices[i]);
u_c_set[this->component_ii[i]] = true;
}
}
}
if (this->parameters.debug & this->parameters.SlopeLimiting)
LOGL(2, "cell: " << ++cell_count << " - center: " << data->center << ", values: " << u_c[0] << ", " << u_c[1] << ", " << u_c[2] << ", " << u_c[3] << ", " << u_c[4]);
double alpha_e[Equations<equationsType, dim>::n_components];
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
alpha_e[i] = 1.;
// Init u_i_min, u_i_max
double u_i_min[Equations<equationsType, dim>::n_components];
double u_i_max[Equations<equationsType, dim>::n_components];
for (int k = 0; k < Equations<equationsType, dim>::n_components; k++)
{
u_i_min[k] = u_c[k];
u_i_max[k] = u_c[k];
}
for (unsigned int vertex_i = 0; vertex_i < GeometryInfo<dim>::vertices_per_cell; ++vertex_i)
{
// For all vertices -> v_i
for (auto dof_indices_neighbor : data->neighbor_dof_indices[vertex_i])
{
if (dof_indices_neighbor.size() == 0)
continue;
bool u_i_extrema_set[Equations<equationsType, dim>::n_components];
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
u_i_extrema_set[i] = false;
for (unsigned int i = 0; i < this->dofs_per_cell; ++i)
{
if (this->is_primitive[i])
{
// Here we rely on the fact, that the constant basis fn is the first one.
if (!u_i_extrema_set[this->component_ii[i]])
{
double val = current_unlimited_solution(dof_indices_neighbor[i]);
if (this->parameters.debug & this->parameters.SlopeLimiting)
{
if (val < u_i_min[this->component_ii[i]])
LOGL(3, "\tdecreasing u_i_min to: " << val);
if (val > u_i_max[this->component_ii[i]])
LOGL(3, "\tincreasing u_i_max to: " << val);
}
u_i_min[this->component_ii[i]] = std::min(u_i_min[this->component_ii[i]], val);
u_i_max[this->component_ii[i]] = std::max(u_i_max[this->component_ii[i]], val);
u_i_extrema_set[this->component_ii[i]] = true;
}
}
}
}
}
// Based on u_i_min, u_i_max, u_i, get alpha_e
for (unsigned int vertex_i = 0; vertex_i < GeometryInfo<dim>::vertices_per_cell; ++vertex_i)
{
// (!!!) Find out u_i
Vector<double> u_i(Equations<equationsType, dim>::n_components);
const Point<dim> p_cell = this->mapping.transform_real_to_unit_cell(cell, data->vertexPoint[vertex_i]);
const Quadrature<dim> one_point_quadrature(GeometryInfo<dim>::project_to_unit_cell(p_cell));
FEValues<dim> fe_values(this->mapping, this->fe, one_point_quadrature, update_values);
fe_values.reinit(cell);
std::vector<Vector<double> > u_value(1, Vector<double>(this->fe.n_components()));
fe_values.get_function_values(current_unlimited_solution, u_value);
u_i = u_value[0];
if (this->parameters.debug & this->parameters.SlopeLimiting)
{
LOGL(3, "\tv_i: " << cell->vertex(vertex_i) << ", values: ");
for (int i = 0; i < Equations<equationsType, dim>::n_components; i++)
LOGL(4, u_i[i] << (i == Equations<equationsType, dim>::n_components - 1 ? "" : ", "));
}
for (int k = 0; k < Equations<equationsType, dim>::n_components; k++)
if (std::abs((u_c[k] - u_i[k]) / u_c[k]) > NEGLIGIBLE)
{
alpha_e[k] = std::min(alpha_e[k], ((u_i[k] - u_c[k]) > 0.) ? std::min(1.0, (u_i_max[k] - u_c[k]) / (u_i[k] - u_c[k])) : std::min(1.0, (u_i_min[k] - u_c[k]) / (u_i[k] - u_c[k])));
if (this->parameters.debug & this->parameters.SlopeLimiting)
LOGL(5, "\talpha_e[" << k << "]: " << alpha_e[k]);
}
}
for (int k = 0; k < Equations<equationsType, dim>::n_components; k++)
for (int i = 0; i < data->lambda_indices_to_multiply[k].size(); i++)
current_limited_solution(data->lambda_indices_to_multiply[k][i]) *= alpha_e[k];
double alpha_e_B = std::min(std::min(alpha_e[5], alpha_e[6]), alpha_e[7]);
for (int i = 0; i < data->lambda_indices_to_multiply_all_B_components.size(); i++)
current_limited_solution(data->lambda_indices_to_multiply_all_B_components[i]) *= alpha_e_B;
}
}
template class SlopeLimiter<EquationsTypeMhd, 3>;
template class VertexBasedSlopeLimiter<EquationsTypeMhd, 3>;
template class BarthJespersenSlopeLimiter<EquationsTypeMhd, 3>;