-
Notifications
You must be signed in to change notification settings - Fork 708
/
error_estimator_1d.cc
555 lines (488 loc) · 19.7 KB
/
error_estimator_1d.cc
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// ---------------------------------------------------------------------
//
// Copyright (C) 1998 - 2020 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE.md at
// the top level directory of deal.II.
//
// ---------------------------------------------------------------------
#include <deal.II/base/geometry_info.h>
#include <deal.II/base/quadrature.h>
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/work_stream.h>
#include <deal.II/distributed/tria_base.h>
#include <deal.II/dofs/dof_accessor.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/fe/fe.h>
#include <deal.II/fe/fe_update_flags.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/fe/mapping_q1.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/hp/fe_values.h>
#include <deal.II/hp/mapping_collection.h>
#include <deal.II/hp/q_collection.h>
#include <deal.II/lac/block_vector.h>
#include <deal.II/lac/la_parallel_block_vector.h>
#include <deal.II/lac/la_parallel_vector.h>
#include <deal.II/lac/la_vector.h>
#include <deal.II/lac/petsc_block_vector.h>
#include <deal.II/lac/petsc_vector.h>
#include <deal.II/lac/trilinos_parallel_block_vector.h>
#include <deal.II/lac/trilinos_vector.h>
#include <deal.II/lac/vector.h>
#include <deal.II/numerics/error_estimator.h>
#include <algorithm>
#include <cmath>
#include <functional>
#include <numeric>
#include <vector>
DEAL_II_NAMESPACE_OPEN
template <int spacedim>
template <typename InputVector>
void
KellyErrorEstimator<1, spacedim>::estimate(
const Mapping<1, spacedim> & mapping,
const DoFHandler<1, spacedim> &dof_handler,
const Quadrature<0> & quadrature,
const std::map<types::boundary_id,
const Function<spacedim, typename InputVector::value_type> *>
& neumann_bc,
const InputVector & solution,
Vector<float> & error,
const ComponentMask & component_mask,
const Function<spacedim> *coefficients,
const unsigned int n_threads,
const types::subdomain_id subdomain_id,
const types::material_id material_id,
const Strategy strategy)
{
// just pass on to the other function
const std::vector<const InputVector *> solutions(1, &solution);
std::vector<Vector<float> *> errors(1, &error);
estimate(mapping,
dof_handler,
quadrature,
neumann_bc,
solutions,
errors,
component_mask,
coefficients,
n_threads,
subdomain_id,
material_id,
strategy);
}
template <int spacedim>
template <typename InputVector>
void
KellyErrorEstimator<1, spacedim>::estimate(
const DoFHandler<1, spacedim> &dof_handler,
const Quadrature<0> & quadrature,
const std::map<types::boundary_id,
const Function<spacedim, typename InputVector::value_type> *>
& neumann_bc,
const InputVector & solution,
Vector<float> & error,
const ComponentMask & component_mask,
const Function<spacedim> *coefficients,
const unsigned int n_threads,
const types::subdomain_id subdomain_id,
const types::material_id material_id,
const Strategy strategy)
{
estimate(StaticMappingQ1<1, spacedim>::mapping,
dof_handler,
quadrature,
neumann_bc,
solution,
error,
component_mask,
coefficients,
n_threads,
subdomain_id,
material_id,
strategy);
}
template <int spacedim>
template <typename InputVector>
void
KellyErrorEstimator<1, spacedim>::estimate(
const DoFHandler<1, spacedim> &dof_handler,
const Quadrature<0> & quadrature,
const std::map<types::boundary_id,
const Function<spacedim, typename InputVector::value_type> *>
& neumann_bc,
const std::vector<const InputVector *> &solutions,
std::vector<Vector<float> *> & errors,
const ComponentMask & component_mask,
const Function<spacedim> * coefficients,
const unsigned int n_threads,
const types::subdomain_id subdomain_id,
const types::material_id material_id,
const Strategy strategy)
{
estimate(StaticMappingQ1<1, spacedim>::mapping,
dof_handler,
quadrature,
neumann_bc,
solutions,
errors,
component_mask,
coefficients,
n_threads,
subdomain_id,
material_id,
strategy);
}
template <int spacedim>
template <typename InputVector>
void
KellyErrorEstimator<1, spacedim>::estimate(
const Mapping<1, spacedim> & mapping,
const DoFHandler<1, spacedim> &dof_handler,
const hp::QCollection<0> & quadrature,
const std::map<types::boundary_id,
const Function<spacedim, typename InputVector::value_type> *>
& neumann_bc,
const InputVector & solution,
Vector<float> & error,
const ComponentMask & component_mask,
const Function<spacedim> *coefficients,
const unsigned int n_threads,
const types::subdomain_id subdomain_id,
const types::material_id material_id,
const Strategy strategy)
{
// just pass on to the other function
const std::vector<const InputVector *> solutions(1, &solution);
std::vector<Vector<float> *> errors(1, &error);
estimate(mapping,
dof_handler,
quadrature,
neumann_bc,
solutions,
errors,
component_mask,
coefficients,
n_threads,
subdomain_id,
material_id,
strategy);
}
template <int spacedim>
template <typename InputVector>
void
KellyErrorEstimator<1, spacedim>::estimate(
const DoFHandler<1, spacedim> &dof_handler,
const hp::QCollection<0> & quadrature,
const std::map<types::boundary_id,
const Function<spacedim, typename InputVector::value_type> *>
& neumann_bc,
const InputVector & solution,
Vector<float> & error,
const ComponentMask & component_mask,
const Function<spacedim> *coefficients,
const unsigned int n_threads,
const types::subdomain_id subdomain_id,
const types::material_id material_id,
const Strategy strategy)
{
estimate(StaticMappingQ1<1, spacedim>::mapping,
dof_handler,
quadrature,
neumann_bc,
solution,
error,
component_mask,
coefficients,
n_threads,
subdomain_id,
material_id,
strategy);
}
template <int spacedim>
template <typename InputVector>
void
KellyErrorEstimator<1, spacedim>::estimate(
const DoFHandler<1, spacedim> &dof_handler,
const hp::QCollection<0> & quadrature,
const std::map<types::boundary_id,
const Function<spacedim, typename InputVector::value_type> *>
& neumann_bc,
const std::vector<const InputVector *> &solutions,
std::vector<Vector<float> *> & errors,
const ComponentMask & component_mask,
const Function<spacedim> * coefficients,
const unsigned int n_threads,
const types::subdomain_id subdomain_id,
const types::material_id material_id,
const Strategy strategy)
{
estimate(StaticMappingQ1<1, spacedim>::mapping,
dof_handler,
quadrature,
neumann_bc,
solutions,
errors,
component_mask,
coefficients,
n_threads,
subdomain_id,
material_id,
strategy);
}
template <int spacedim>
template <typename InputVector>
void
KellyErrorEstimator<1, spacedim>::estimate(
const Mapping<1, spacedim> & /*mapping*/,
const DoFHandler<1, spacedim> & /*dof_handler*/,
const hp::QCollection<0> &,
const std::map<types::boundary_id,
const Function<spacedim, typename InputVector::value_type> *>
& /*neumann_bc*/,
const std::vector<const InputVector *> & /*solutions*/,
std::vector<Vector<float> *> & /*errors*/,
const ComponentMask & /*component_mask_*/,
const Function<spacedim> * /*coefficient*/,
const unsigned int,
const types::subdomain_id /*subdomain_id*/,
const types::material_id /*material_id*/,
const Strategy /*strategy*/)
{
Assert(false, ExcNotImplemented());
}
template <int spacedim>
template <typename InputVector>
void
KellyErrorEstimator<1, spacedim>::estimate(
const Mapping<1, spacedim> & mapping,
const DoFHandler<1, spacedim> &dof_handler,
const Quadrature<0> &,
const std::map<types::boundary_id,
const Function<spacedim, typename InputVector::value_type> *>
& neumann_bc,
const std::vector<const InputVector *> &solutions,
std::vector<Vector<float> *> & errors,
const ComponentMask & component_mask,
const Function<spacedim> * coefficient,
const unsigned int,
const types::subdomain_id subdomain_id_,
const types::material_id material_id,
const Strategy strategy)
{
AssertThrow(strategy == cell_diameter_over_24, ExcNotImplemented());
using number = typename InputVector::value_type;
types::subdomain_id subdomain_id = numbers::invalid_subdomain_id;
if (const auto *triangulation = dynamic_cast<
const parallel::DistributedTriangulationBase<1, spacedim> *>(
&dof_handler.get_triangulation()))
{
Assert((subdomain_id_ == numbers::invalid_subdomain_id) ||
(subdomain_id_ == triangulation->locally_owned_subdomain()),
ExcMessage(
"For distributed Triangulation objects and associated "
"DoFHandler objects, asking for any subdomain other than the "
"locally owned one does not make sense."));
subdomain_id = triangulation->locally_owned_subdomain();
}
else
{
subdomain_id = subdomain_id_;
}
const unsigned int n_components = dof_handler.get_fe(0).n_components();
const unsigned int n_solution_vectors = solutions.size();
// sanity checks
Assert(neumann_bc.find(numbers::internal_face_boundary_id) ==
neumann_bc.end(),
ExcMessage("You are not allowed to list the special boundary "
"indicator for internal boundaries in your boundary "
"value map."));
for (const auto &boundary_function : neumann_bc)
{
(void)boundary_function;
Assert(boundary_function.second->n_components == n_components,
ExcInvalidBoundaryFunction(boundary_function.first,
boundary_function.second->n_components,
n_components));
}
Assert(component_mask.represents_n_components(n_components),
ExcInvalidComponentMask());
Assert(component_mask.n_selected_components(n_components) > 0,
ExcInvalidComponentMask());
Assert((coefficient == nullptr) ||
(coefficient->n_components == n_components) ||
(coefficient->n_components == 1),
ExcInvalidCoefficient());
Assert(solutions.size() > 0, ExcNoSolutions());
Assert(solutions.size() == errors.size(),
ExcIncompatibleNumberOfElements(solutions.size(), errors.size()));
for (unsigned int n = 0; n < solutions.size(); ++n)
Assert(solutions[n]->size() == dof_handler.n_dofs(),
ExcDimensionMismatch(solutions[n]->size(), dof_handler.n_dofs()));
Assert((coefficient == nullptr) ||
(coefficient->n_components == n_components) ||
(coefficient->n_components == 1),
ExcInvalidCoefficient());
for (const auto &boundary_function : neumann_bc)
{
(void)boundary_function;
Assert(boundary_function.second->n_components == n_components,
ExcInvalidBoundaryFunction(boundary_function.first,
boundary_function.second->n_components,
n_components));
}
// reserve one slot for each cell and set it to zero
for (unsigned int n = 0; n < n_solution_vectors; ++n)
(*errors[n]).reinit(dof_handler.get_triangulation().n_active_cells());
// fields to get the gradients on the present and the neighbor cell.
//
// for the neighbor gradient, we need several auxiliary fields, depending on
// the way we get it (see below)
std::vector<std::vector<std::vector<Tensor<1, spacedim, number>>>>
gradients_here(n_solution_vectors,
std::vector<std::vector<Tensor<1, spacedim, number>>>(
2,
std::vector<Tensor<1, spacedim, number>>(n_components)));
std::vector<std::vector<std::vector<Tensor<1, spacedim, number>>>>
gradients_neighbor(gradients_here);
std::vector<Vector<typename ProductType<number, double>::type>>
grad_dot_n_neighbor(n_solution_vectors,
Vector<typename ProductType<number, double>::type>(
n_components));
// reserve some space for coefficient values at one point. if there is no
// coefficient, then we fill it by unity once and for all and don't set it
// any more
Vector<double> coefficient_values(n_components);
if (coefficient == nullptr)
for (unsigned int c = 0; c < n_components; ++c)
coefficient_values(c) = 1;
const QTrapezoid<1> quadrature;
const hp::QCollection<1> q_collection(quadrature);
const QGauss<0> face_quadrature(1);
const hp::QCollection<0> q_face_collection(face_quadrature);
const hp::FECollection<1, spacedim> &fe = dof_handler.get_fe_collection();
hp::MappingCollection<1, spacedim> mapping_collection;
mapping_collection.push_back(mapping);
hp::FEValues<1, spacedim> fe_values(mapping_collection,
fe,
q_collection,
update_gradients);
hp::FEFaceValues<1, spacedim> fe_face_values(
/*mapping_collection,*/ fe, q_face_collection, update_normal_vectors);
// loop over all cells and do something on the cells which we're told to
// work on. note that the error indicator is only a sum over the two
// contributions from the two vertices of each cell.
for (const auto &cell : dof_handler.active_cell_iterators())
if (((subdomain_id == numbers::invalid_subdomain_id) ||
(cell->subdomain_id() == subdomain_id)) &&
((material_id == numbers::invalid_material_id) ||
(cell->material_id() == material_id)))
{
for (unsigned int n = 0; n < n_solution_vectors; ++n)
(*errors[n])(cell->active_cell_index()) = 0;
fe_values.reinit(cell);
for (unsigned int s = 0; s < n_solution_vectors; ++s)
fe_values.get_present_fe_values().get_function_gradients(
*solutions[s], gradients_here[s]);
// loop over the two points bounding this line. n==0 is left point,
// n==1 is right point
for (unsigned int n = 0; n < 2; ++n)
{
// find left or right active neighbor
auto neighbor = cell->neighbor(n);
if (neighbor.state() == IteratorState::valid)
while (neighbor->has_children())
neighbor = neighbor->child(n == 0 ? 1 : 0);
fe_face_values.reinit(cell, n);
Tensor<1, spacedim> normal =
fe_face_values.get_present_fe_values().get_normal_vectors()[0];
if (neighbor.state() == IteratorState::valid)
{
fe_values.reinit(neighbor);
for (unsigned int s = 0; s < n_solution_vectors; ++s)
fe_values.get_present_fe_values().get_function_gradients(
*solutions[s], gradients_neighbor[s]);
fe_face_values.reinit(neighbor, n == 0 ? 1 : 0);
Tensor<1, spacedim> neighbor_normal =
fe_face_values.get_present_fe_values()
.get_normal_vectors()[0];
// extract the gradient in normal direction of all the
// components.
for (unsigned int s = 0; s < n_solution_vectors; ++s)
for (unsigned int c = 0; c < n_components; ++c)
grad_dot_n_neighbor[s](c) =
-(gradients_neighbor[s][n == 0 ? 1 : 0][c] *
neighbor_normal);
}
else if (neumann_bc.find(n) != neumann_bc.end())
// if Neumann b.c., then fill the gradients field which will be
// used later on.
{
if (n_components == 1)
{
const typename InputVector::value_type v =
neumann_bc.find(n)->second->value(cell->vertex(n));
for (unsigned int s = 0; s < n_solution_vectors; ++s)
grad_dot_n_neighbor[s](0) = v;
}
else
{
Vector<typename InputVector::value_type> v(n_components);
neumann_bc.find(n)->second->vector_value(cell->vertex(n),
v);
for (unsigned int s = 0; s < n_solution_vectors; ++s)
grad_dot_n_neighbor[s] = v;
}
}
else
// fill with zeroes.
for (unsigned int s = 0; s < n_solution_vectors; ++s)
grad_dot_n_neighbor[s] = 0;
// if there is a coefficient, then evaluate it at the present
// position. if there is none, reuse the preset values.
if (coefficient != nullptr)
{
if (coefficient->n_components == 1)
{
const double c_value = coefficient->value(cell->vertex(n));
for (unsigned int c = 0; c < n_components; ++c)
coefficient_values(c) = c_value;
}
else
coefficient->vector_value(cell->vertex(n),
coefficient_values);
}
for (unsigned int s = 0; s < n_solution_vectors; ++s)
for (unsigned int component = 0; component < n_components;
++component)
if (component_mask[component] == true)
{
// get gradient here
const typename ProductType<number, double>::type
grad_dot_n_here =
gradients_here[s][n][component] * normal;
const typename ProductType<number, double>::type jump =
((grad_dot_n_here - grad_dot_n_neighbor[s](component)) *
coefficient_values(component));
(*errors[s])(cell->active_cell_index()) +=
numbers::NumberTraits<
typename ProductType<number,
double>::type>::abs_square(jump) *
cell->diameter();
}
}
for (unsigned int s = 0; s < n_solution_vectors; ++s)
(*errors[s])(cell->active_cell_index()) =
std::sqrt((*errors[s])(cell->active_cell_index()));
}
}
// explicit instantiations
#include "error_estimator_1d.inst"
DEAL_II_NAMESPACE_CLOSE