libMesh
Loading...
Searching...
No Matches
weighted_patch_recovery_error_estimator.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
19// libmesh includes
20#include "libmesh/dense_matrix.h"
21#include "libmesh/dense_vector.h"
22#include "libmesh/dof_map.h"
23#include "libmesh/elem.h"
24#include "libmesh/error_vector.h"
25#include "libmesh/fe_base.h"
26#include "libmesh/fem_context.h"
27#include "libmesh/libmesh_logging.h"
28#include "libmesh/mesh_base.h"
29#include "libmesh/numeric_vector.h"
30#include "libmesh/quadrature_grid.h"
31#include "libmesh/system.h"
32#include "libmesh/tensor_value.h"
33#include "libmesh/threads.h"
34#include "libmesh/weighted_patch_recovery_error_estimator.h"
35#include "libmesh/enum_error_estimator_type.h"
36#include "libmesh/enum_order.h"
37#include "libmesh/enum_norm_type.h"
38#include "libmesh/enum_to_string.h"
39
40// C++ includes
41#include <algorithm> // for std::fill
42#include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
43#include <cmath> // for std::sqrt std::pow std::abs
44
45namespace libMesh
46{
47
52
53
54
56 ErrorVector & error_per_cell,
57 const NumericVector<Number> * solution_vector,
58 bool)
59{
60 LOG_SCOPE("estimate_error()", "WeightedPatchRecoveryErrorEstimator");
61
62 // The current mesh
63 const MeshBase & mesh = system.get_mesh();
64
65 // Resize the error_per_cell vector to be
66 // the number of elements, initialize it to 0.
67 error_per_cell.resize (mesh.max_elem_id());
68 std::fill (error_per_cell.begin(), error_per_cell.end(), 0.);
69
70 // Prepare current_local_solution to localize a non-standard
71 // solution vector if necessary
72 if (solution_vector && solution_vector != system.solution.get())
73 {
74 NumericVector<Number> * newsol =
75 const_cast<NumericVector<Number> *>(solution_vector);
76 System & sys = const_cast<System &>(system);
77 newsol->swap(*sys.solution);
78 sys.update();
79 }
80
81 //------------------------------------------------------------
82 // Iterate over all the active elements in the mesh
83 // that live on this processor.
84 Threads::parallel_for (mesh.active_local_element_stored_range(),
85 EstimateError(system,
86 *this,
87 error_per_cell)
88 );
89
90 // Each processor has now computed the error contributions
91 // for its local elements, and error_per_cell contains 0 for all the
92 // non-local elements. Summing the vector will provide the true
93 // value for each element, local or remote
94 this->reduce_error(error_per_cell, system.comm());
95
96 // If we used a non-standard solution before, now is the time to fix
97 // the current_local_solution
98 if (solution_vector && solution_vector != system.solution.get())
99 {
100 NumericVector<Number> * newsol =
101 const_cast<NumericVector<Number> *>(solution_vector);
102 System & sys = const_cast<System &>(system);
103 newsol->swap(*sys.solution);
104 sys.update();
105 }
106}
107
108
109
111{
112 // The current mesh
113 const MeshBase & mesh = system.get_mesh();
114
115 // The dimensionality of the mesh
116 const unsigned int dim = mesh.mesh_dimension();
117
118 // The number of variables in the system
119 const unsigned int n_vars = system.n_vars();
120
121 // The DofMap for this system
122 const DofMap & dof_map = system.get_dof_map();
123
124 //------------------------------------------------------------
125 // Iterate over all the elements in the range.
126 for (const auto & elem : range)
127 {
128 // We'll need an index into the error vector
129 const dof_id_type e_id=elem->id();
130
131 // We are going to build a patch containing the current element
132 // and its neighbors on the local processor
133 Patch patch(mesh.processor_id());
134
135 // If we are reusing patches and the current element
136 // already has an estimate associated with it, move on the
137 // next element
138 if (this->error_estimator.patch_reuse && error_per_cell[e_id] != 0)
139 continue;
140
141 // If we are not reusing patches or haven't built one containing this element, we build one
142
143 // Use user specified patch size and growth strategy
146
147 // Declare a new_error_per_cell vector to hold error estimates
148 // from each element in this patch, or one estimate if we are
149 // not reusing patches since we will only be computing error for
150 // one cell
151 std::vector<Real> new_error_per_cell(1, 0.);
153 new_error_per_cell.resize(patch.size(), 0.);
154
155 //------------------------------------------------------------
156 // Process each variable in the system using the current patch
157 for (unsigned int var=0; var<n_vars; var++)
158 {
159 const auto norm_type = error_estimator.error_norm.type(var);
160#ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
161#ifdef DEBUG
162 bool is_valid_norm_type =
163 norm_type == L2 ||
164 norm_type == H1_SEMINORM ||
165 norm_type == H2_SEMINORM ||
166 norm_type == H1_X_SEMINORM ||
167 norm_type == H1_Y_SEMINORM ||
168 norm_type == H1_Z_SEMINORM ||
169 norm_type == L_INF ||
170 norm_type == W1_INF_SEMINORM ||
171 norm_type == W2_INF_SEMINORM;
172 libmesh_assert (is_valid_norm_type);
173#endif // DEBUG
174#else
175 libmesh_assert (norm_type == L2 ||
176 norm_type == L_INF ||
177 norm_type == H1_SEMINORM ||
178 norm_type == H1_X_SEMINORM ||
179 norm_type == H1_Y_SEMINORM ||
180 norm_type == H1_Z_SEMINORM ||
181 norm_type == W1_INF_SEMINORM);
182#endif
183
184#ifdef DEBUG
185 if (var > 0)
186 {
187 // We can't mix L_inf and L_2 norms
188 bool is_valid_norm_combo =
189 ((norm_type == L2 ||
190 norm_type == H1_SEMINORM ||
191 norm_type == H1_X_SEMINORM ||
192 norm_type == H1_Y_SEMINORM ||
193 norm_type == H1_Z_SEMINORM ||
194 norm_type == H2_SEMINORM) &&
195 (error_estimator.error_norm.type(var-1) == L2 ||
201 ((norm_type == L_INF ||
202 norm_type == W1_INF_SEMINORM ||
203 norm_type == W2_INF_SEMINORM) &&
207 libmesh_assert (is_valid_norm_combo);
208 }
209#endif // DEBUG
210
211 // Possibly skip this variable
212 if (error_estimator.error_norm.weight(var) == 0.0) continue;
213
214 // The type of finite element to use for this variable
215 const FEType & fe_type = dof_map.variable_type (var);
216
217 const Order element_order = fe_type.order + elem->p_level();
218
219 // Finite element object for use in this patch
220 std::unique_ptr<FEBase> fe (FEBase::build (dim, fe_type));
221
222 // Build an appropriate Gaussian quadrature rule
223 std::unique_ptr<QBase> qrule =
225
226 // Tell the finite element about the quadrature rule.
227 fe->attach_quadrature_rule (qrule.get());
228
229 // Get Jacobian values, etc..
230 const std::vector<Real> & JxW = fe->get_JxW();
231 const std::vector<Point> & q_point = fe->get_xyz();
232
233 // Get whatever phi/dphi/d2phi values we need. Avoid
234 // getting them unless the requested norm is actually going
235 // to use them.
236
237 const std::vector<std::vector<Real>> * phi = nullptr;
238 // If we're using phi to assert the correct dof_indices
239 // vector size later, then we'll need to get_phi whether we
240 // plan to use it or not.
241#ifdef NDEBUG
242 if (norm_type == L2 ||
243 norm_type == L_INF)
244#endif
245 phi = &(fe->get_phi());
246
247 const std::vector<std::vector<RealGradient>> * dphi = nullptr;
248 if (norm_type == H1_SEMINORM ||
249 norm_type == H1_X_SEMINORM ||
250 norm_type == H1_Y_SEMINORM ||
251 norm_type == H1_Z_SEMINORM ||
252 norm_type == W1_INF_SEMINORM)
253 dphi = &(fe->get_dphi());
254
255#ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
256 const std::vector<std::vector<RealTensor>> * d2phi = nullptr;
257 if (norm_type == H2_SEMINORM ||
258 norm_type == W2_INF_SEMINORM)
259 d2phi = &(fe->get_d2phi());
260#endif
261
262 // global DOF indices
263 std::vector<dof_id_type> dof_indices;
264
265 // Compute the appropriate size for the patch projection matrices
266 // and vectors;
267 unsigned int matsize = element_order + 1;
268 if (dim > 1)
269 {
270 matsize *= (element_order + 2);
271 matsize /= 2;
272 }
273 if (dim > 2)
274 {
275 matsize *= (element_order + 3);
276 matsize /= 3;
277 }
278
279 DenseMatrix<Number> Kp(matsize,matsize);
280 DenseVector<Number> F, Fx, Fy, Fz, Fxy, Fxz, Fyz;
281 DenseVector<Number> Pu_h, Pu_x_h, Pu_y_h, Pu_z_h, Pu_xy_h, Pu_xz_h, Pu_yz_h;
282 if (norm_type == L2 ||
283 norm_type == L_INF)
284 {
285 F.resize(matsize); Pu_h.resize(matsize);
286 }
287 else if (norm_type == H1_SEMINORM ||
288 norm_type == W1_INF_SEMINORM ||
289 norm_type == H2_SEMINORM ||
290 norm_type == W2_INF_SEMINORM)
291 {
292 Fx.resize(matsize); Pu_x_h.resize(matsize); // stores xx in W2 cases
293#if LIBMESH_DIM > 1
294 Fy.resize(matsize); Pu_y_h.resize(matsize); // stores yy in W2 cases
295#endif
296#if LIBMESH_DIM > 2
297 Fz.resize(matsize); Pu_z_h.resize(matsize); // stores zz in W2 cases
298#endif
299 }
300 else if (norm_type == H1_X_SEMINORM)
301 {
302 Fx.resize(matsize); Pu_x_h.resize(matsize); // Only need to compute the x gradient for the x component seminorm
303 }
304 else if (norm_type == H1_Y_SEMINORM)
305 {
306 libmesh_assert(LIBMESH_DIM > 1);
307 Fy.resize(matsize); Pu_y_h.resize(matsize); // Only need to compute the y gradient for the y component seminorm
308 }
309 else if (norm_type == H1_Z_SEMINORM)
310 {
311 libmesh_assert(LIBMESH_DIM > 2);
312 Fz.resize(matsize); Pu_z_h.resize(matsize); // Only need to compute the z gradient for the z component seminorm
313 }
314
315#if LIBMESH_DIM > 1
316 if (norm_type == H2_SEMINORM ||
317 norm_type == W2_INF_SEMINORM)
318 {
319 Fxy.resize(matsize); Pu_xy_h.resize(matsize);
320#if LIBMESH_DIM > 2
321 Fxz.resize(matsize); Pu_xz_h.resize(matsize);
322 Fyz.resize(matsize); Pu_yz_h.resize(matsize);
323#endif
324 }
325#endif
326
327
328 //------------------------------------------------------
329 // Loop over each element in the patch and compute their
330 // contribution to the patch gradient projection.
331 for (const auto & e_p : patch)
332 {
333 // Reinitialize the finite element data for this element
334 fe->reinit (e_p);
335
336 // Get the global DOF indices for the current variable
337 // in the current element
338 dof_map.dof_indices (e_p, dof_indices, var);
339 libmesh_assert (dof_indices.size() == phi->size());
340
341 const unsigned int n_dofs =
342 cast_int<unsigned int>(dof_indices.size());
343 const unsigned int n_qp = qrule->n_points();
344
345 // Compute the weighted projection components from this cell.
346 // \int_{Omega_e} \psi_i \psi_j = \int_{Omega_e} w * du_h/dx_k \psi_i
347 for (unsigned int qp=0; qp<n_qp; qp++)
348 {
349 // Construct the shape function values for the patch projection
350 std::vector<Real> psi(specpoly(dim, element_order, q_point[qp], matsize));
351
352 const unsigned int psi_size = cast_int<unsigned int>(psi.size());
353
354 // Patch matrix contribution
355 const unsigned int m = Kp.m(), n = Kp.n();
356 for (unsigned int i=0; i<m; i++)
357 for (unsigned int j=0; j<n; j++)
358 Kp(i,j) += JxW[qp]*psi[i]*psi[j];
359
360 if (norm_type == L2 ||
361 norm_type == L_INF)
362 {
363 // Compute the solution on the current patch element
364 // the quadrature point
365 Number u_h = libMesh::zero;
366
367 for (unsigned int i=0; i<n_dofs; i++)
368 u_h += (*phi)[i][qp]*system.current_solution (dof_indices[i]);
369
370 // Patch RHS contributions
371 for (unsigned int i=0; i != psi_size; i++)
372 F(i) = JxW[qp]*u_h*psi[i];
373
374 }
375 else if (norm_type == H1_SEMINORM ||
376 norm_type == W1_INF_SEMINORM)
377 {
378 // Compute the gradient on the current patch element
379 // at the quadrature point
380 Gradient grad_u_h;
381
382 for (std::size_t i=0; i<n_dofs; i++)
383 grad_u_h.add_scaled ((*dphi)[i][qp],
384 system.current_solution(dof_indices[i]));
385
386
387
388 // Patch RHS contributions
389 for (unsigned int i=0; i != psi_size; i++)
390 {
391 Fx(i) += JxW[qp]*grad_u_h(0)*psi[i];
392#if LIBMESH_DIM > 1
393 Fy(i) += JxW[qp]*grad_u_h(1)*psi[i];
394#endif
395#if LIBMESH_DIM > 2
396 Fz(i) += JxW[qp]*grad_u_h(2)*psi[i];
397#endif
398 }
399 }
400 else if (norm_type == H1_X_SEMINORM)
401 {
402 // Compute the gradient on the current patch element
403 // at the quadrature point
404 Gradient grad_u_h;
405
406 for (unsigned int i=0; i<n_dofs; i++)
407 grad_u_h.add_scaled ((*dphi)[i][qp],
408 system.current_solution(dof_indices[i]));
409
410
411
412 // Patch RHS contributions
413 for (unsigned int i=0; i != psi_size; i++)
414 {
415 Fx(i) += JxW[qp]*grad_u_h(0)*psi[i];
416 }
417 }
418#if LIBMESH_DIM > 1
419 else if (norm_type == H1_Y_SEMINORM)
420 {
421 // Compute the gradient on the current patch element
422 // at the quadrature point
423 Gradient grad_u_h;
424
425 for (unsigned int i=0; i<n_dofs; i++)
426 grad_u_h.add_scaled ((*dphi)[i][qp],
427 system.current_solution(dof_indices[i]));
428
429
430
431 // Patch RHS contributions
432 for (unsigned int i=0; i != psi_size; i++)
433 {
434 Fy(i) += JxW[qp]*grad_u_h(1)*psi[i];
435 }
436 }
437#endif // LIBMESH_DIM > 1
438#if LIBMESH_DIM > 2
439 else if (norm_type == H1_Z_SEMINORM)
440 {
441 // Compute the gradient on the current patch element
442 // at the quadrature point
443 Gradient grad_u_h;
444
445 for (unsigned int i=0; i<n_dofs; i++)
446 grad_u_h.add_scaled ((*dphi)[i][qp],
447 system.current_solution(dof_indices[i]));
448
449
450
451 // Patch RHS contributions
452 for (unsigned int i=0; i != psi_size; i++)
453 {
454 Fz(i) += JxW[qp]*grad_u_h(2)*psi[i];
455 }
456 }
457#endif // LIBMESH_DIM > 2
458 else if (norm_type == H2_SEMINORM ||
459 norm_type == W2_INF_SEMINORM)
460 {
461#ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
462 // Compute the hessian on the current patch element
463 // at the quadrature point
464 Tensor hess_u_h;
465
466 for (unsigned int i=0; i<n_dofs; i++)
467 hess_u_h.add_scaled ((*d2phi)[i][qp],
468 system.current_solution(dof_indices[i]));
469
470
471
472 // Patch RHS contributions
473 for (unsigned int i=0; i != psi_size; i++)
474 {
475 Fx(i) += JxW[qp]*hess_u_h(0,0)*psi[i];
476#if LIBMESH_DIM > 1
477 Fy(i) += JxW[qp]*hess_u_h(1,1)*psi[i];
478 Fxy(i) += JxW[qp]*hess_u_h(0,1)*psi[i];
479#endif
480#if LIBMESH_DIM > 2
481 Fz(i) += JxW[qp]*hess_u_h(2,2)*psi[i];
482 Fxz(i) += JxW[qp]*hess_u_h(0,2)*psi[i];
483 Fyz(i) += JxW[qp]*hess_u_h(1,2)*psi[i];
484#endif
485 }
486#else
487 libmesh_error_msg("ERROR: --enable-second-derivatives is required \nfor _sobolev_order == 2!");
488#endif
489 }
490 else
491 libmesh_error_msg("Unsupported error norm type == " << Utility::enum_to_string(norm_type));
492 } // end quadrature loop
493 } // end patch loop
494
495
496
497 //--------------------------------------------------
498 // Now we have fully assembled the projection system
499 // for this patch. Project the gradient components.
500 // MAY NEED TO USE PARTIAL PIVOTING!
501 if (norm_type == L2 ||
502 norm_type == L_INF)
503 {
504 Kp.lu_solve(F, Pu_h);
505 }
506 else if (norm_type == H1_SEMINORM ||
507 norm_type == W1_INF_SEMINORM ||
508 norm_type == H2_SEMINORM ||
509 norm_type == W2_INF_SEMINORM)
510 {
511 Kp.lu_solve (Fx, Pu_x_h);
512#if LIBMESH_DIM > 1
513 Kp.lu_solve (Fy, Pu_y_h);
514#endif
515#if LIBMESH_DIM > 2
516 Kp.lu_solve (Fz, Pu_z_h);
517#endif
518 }
519 else if (norm_type == H1_X_SEMINORM)
520 {
521 Kp.lu_solve (Fx, Pu_x_h);
522 }
523 else if (norm_type == H1_Y_SEMINORM)
524 {
525 Kp.lu_solve (Fy, Pu_y_h);
526 }
527 else if (norm_type == H1_Z_SEMINORM)
528 {
529 Kp.lu_solve (Fz, Pu_z_h);
530 }
531
532#if LIBMESH_DIM > 1
533 if (norm_type == H2_SEMINORM ||
534 norm_type == W2_INF_SEMINORM)
535 {
536 Kp.lu_solve(Fxy, Pu_xy_h);
537#if LIBMESH_DIM > 2
538 Kp.lu_solve(Fxz, Pu_xz_h);
539 Kp.lu_solve(Fyz, Pu_yz_h);
540#endif
541 }
542#endif
543
544 // If we are reusing patches, reuse the current patch to loop
545 // over all elements in the current patch, otherwise build a new
546 // patch containing just the current element and loop over it
547 // Note that C++ will not allow patch_re_end to be a const here
548 Patch::const_iterator patch_re_it;
549 Patch::const_iterator patch_re_end;
550
551 // Declare a new patch
552 Patch patch_re(mesh.processor_id());
553
555 {
556 // Just get the iterators from the current patch
557 patch_re_it = patch.begin();
558 patch_re_end = patch.end();
559 }
560 else
561 {
562 // Use a target patch size of just 0, this will contain
563 // just the current element
564 patch_re.build_around_element (elem, 0,
566
567 // Get the iterators from this newly constructed patch
568 patch_re_it = patch_re.begin();
569 patch_re_end = patch_re.end();
570 }
571
572 // If we are reusing patches, loop over all the elements
573 // in the current patch and develop an estimate
574 // for all the elements by computing || w * (P u_h - u_h)|| or ||w *(P grad_u_h - grad_u_h)||
575 // or ||w * (P hess_u_h - hess_u_h)|| according to the requested
576 // seminorm, otherwise just compute it for the current element
577
578 // Get an FEMContext for this system, this will help us in
579 // obtaining the weights from the user code.
580 // We don't use full elem_jacobian or subjacobians here.
581 FEMContext femcontext(system, nullptr,
582 /* allocate_local_matrices = */ false);
583 error_estimator.weight_functions[var]->init_context(femcontext);
584
585 // Loop over every element in the patch
586 for (unsigned int e = 0 ; patch_re_it != patch_re_end; ++patch_re_it, ++e)
587 {
588 // Build the Finite Element for the current element
589
590 // The pth element in the patch
591 const Elem * e_p = *patch_re_it;
592
593 // We'll need an index into the error vector for this element
594 const dof_id_type e_p_id = e_p->id();
595
596 // Initialize the FEMContext
597 femcontext.pre_fe_reinit(system, e_p);
598
599 // We will update the new_error_per_cell vector with element_error if the
600 // error_per_cell[e_p_id] entry is non-zero, otherwise update it
601 // with 0. i.e. leave it unchanged
602
603 // No need to compute the estimate if we are reusing patches and already have one
604 if (this->error_estimator.patch_reuse && error_per_cell[e_p_id] != 0.)
605 continue;
606
607 // Reinitialize the finite element data for this element
608 fe->reinit (e_p);
609
610 // Get the global DOF indices for the current variable
611 // in the current element
612 dof_map.dof_indices (e_p, dof_indices, var);
613 libmesh_assert (dof_indices.size() == phi->size());
614
615 // The number of dofs for this variable on this element
616 const unsigned int n_dofs =
617 cast_int<unsigned int>(dof_indices.size());
618
619 // Variable to hold the error on the current element
620 Real element_error = 0;
621
622 const Order qorder = fe_type.order + e_p->p_level();
623
624 // A quadrature rule for this element
625 QGrid samprule (dim, qorder);
626
627 if (norm_type == W1_INF_SEMINORM ||
628 norm_type == W2_INF_SEMINORM)
629 fe->attach_quadrature_rule (&samprule);
630
631 // The number of points we will sample over
632 const unsigned int n_sp =
633 cast_int<unsigned int>(JxW.size());
634
635 // Loop over every sample point for the current element
636 for (unsigned int sp=0; sp<n_sp; sp++)
637 {
638 // Compute the solution at the current sample point
639
640 std::vector<Number> temperr(6,0.0); // x,y,z or xx,yy,zz,xy,xz,yz
641
642 if (norm_type == L2 ||
643 norm_type == L_INF)
644 {
645 // Compute the value at the current sample point
646 Number u_h = libMesh::zero;
647
648 for (unsigned int i=0; i<n_dofs; i++)
649 u_h += (*phi)[i][sp]*system.current_solution (dof_indices[i]);
650
651 // Compute the phi values at the current sample point
652 std::vector<Real> psi(specpoly(dim, element_order, q_point[sp], matsize));
653 for (unsigned int i=0; i<matsize; i++)
654 {
655 temperr[0] += psi[i]*Pu_h(i);
656 }
657
658 temperr[0] -= u_h;
659 }
660 else if (norm_type == H1_SEMINORM ||
661 norm_type == W1_INF_SEMINORM)
662 {
663 // Compute the gradient at the current sample point
664 Gradient grad_u_h;
665
666 for (unsigned int i=0; i<n_dofs; i++)
667 grad_u_h.add_scaled ((*dphi)[i][sp],
668 system.current_solution(dof_indices[i]));
669
670 // Compute the phi values at the current sample point
671 std::vector<Real> psi(specpoly(dim, element_order, q_point[sp], matsize));
672
673 for (unsigned int i=0; i<matsize; i++)
674 {
675 temperr[0] += psi[i]*Pu_x_h(i);
676#if LIBMESH_DIM > 1
677 temperr[1] += psi[i]*Pu_y_h(i);
678#endif
679#if LIBMESH_DIM > 2
680 temperr[2] += psi[i]*Pu_z_h(i);
681#endif
682 }
683 temperr[0] -= grad_u_h(0);
684#if LIBMESH_DIM > 1
685 temperr[1] -= grad_u_h(1);
686#endif
687#if LIBMESH_DIM > 2
688 temperr[2] -= grad_u_h(2);
689#endif
690 }
691 else if (norm_type == H1_X_SEMINORM)
692 {
693 // Compute the gradient at the current sample point
694 Gradient grad_u_h;
695
696 for (unsigned int i=0; i<n_dofs; i++)
697 grad_u_h.add_scaled ((*dphi)[i][sp],
698 system.current_solution(dof_indices[i]));
699
700 // Compute the phi values at the current sample point
701 std::vector<Real> psi(specpoly(dim, element_order, q_point[sp], matsize));
702 for (unsigned int i=0; i<matsize; i++)
703 {
704 temperr[0] += psi[i]*Pu_x_h(i);
705 }
706
707 temperr[0] -= grad_u_h(0);
708 }
709#if LIBMESH_DIM > 1
710 else if (norm_type == H1_Y_SEMINORM)
711 {
712 // Compute the gradient at the current sample point
713 Gradient grad_u_h;
714
715 for (unsigned int i=0; i<n_dofs; i++)
716 grad_u_h.add_scaled ((*dphi)[i][sp],
717 system.current_solution(dof_indices[i]));
718
719 // Compute the phi values at the current sample point
720 std::vector<Real> psi(specpoly(dim, element_order, q_point[sp], matsize));
721 for (unsigned int i=0; i<matsize; i++)
722 {
723 temperr[1] += psi[i]*Pu_y_h(i);
724 }
725
726 temperr[1] -= grad_u_h(1);
727 }
728#endif // LIBMESH_DIM > 1
729#if LIBMESH_DIM > 2
730 else if (norm_type == H1_Z_SEMINORM)
731 {
732 // Compute the gradient at the current sample point
733 Gradient grad_u_h;
734
735 for (unsigned int i=0; i<n_dofs; i++)
736 grad_u_h.add_scaled ((*dphi)[i][sp],
737 system.current_solution(dof_indices[i]));
738
739 // Compute the phi values at the current sample point
740 std::vector<Real> psi(specpoly(dim, element_order, q_point[sp], matsize));
741 for (unsigned int i=0; i<matsize; i++)
742 {
743 temperr[2] += psi[i]*Pu_z_h(i);
744 }
745
746 temperr[2] -= grad_u_h(2);
747 }
748#endif // LIBMESH_DIM > 2
749 else if (norm_type == H2_SEMINORM ||
750 norm_type == W2_INF_SEMINORM)
751 {
752#ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
753 // Compute the Hessian at the current sample point
754 Tensor hess_u_h;
755
756 for (unsigned int i=0; i<n_dofs; i++)
757 hess_u_h.add_scaled ((*d2phi)[i][sp],
758 system.current_solution(dof_indices[i]));
759
760 // Compute the phi values at the current sample point
761 std::vector<Real> psi(specpoly(dim, element_order, q_point[sp], matsize));
762 for (unsigned int i=0; i<matsize; i++)
763 {
764 temperr[0] += psi[i]*Pu_x_h(i);
765#if LIBMESH_DIM > 1
766 temperr[1] += psi[i]*Pu_y_h(i);
767 temperr[3] += psi[i]*Pu_xy_h(i);
768#endif
769#if LIBMESH_DIM > 2
770 temperr[2] += psi[i]*Pu_z_h(i);
771 temperr[4] += psi[i]*Pu_xz_h(i);
772 temperr[5] += psi[i]*Pu_yz_h(i);
773#endif
774 }
775
776 temperr[0] -= hess_u_h(0,0);
777#if LIBMESH_DIM > 1
778 temperr[1] -= hess_u_h(1,1);
779 temperr[3] -= hess_u_h(0,1);
780#endif
781#if LIBMESH_DIM > 2
782 temperr[2] -= hess_u_h(2,2);
783 temperr[4] -= hess_u_h(0,2);
784 temperr[5] -= hess_u_h(1,2);
785#endif
786#else
787 libmesh_error_msg("ERROR: --enable-second-derivatives is required \nfor _sobolev_order == 2!");
788#endif
789 }
790
791 // Get the weight from the user code
792 Number weight = (*error_estimator.weight_functions[var])(femcontext, q_point[sp], system.time);
793
794 // Add up relevant terms. We can easily optimize the
795 // LIBMESH_DIM < 3 cases a little bit with the exception
796 // of the W2 cases
797
798 if (norm_type == L_INF)
799 element_error = std::max(element_error, std::abs(weight*temperr[0]));
800 else if (norm_type == W1_INF_SEMINORM)
801 for (unsigned int i=0; i != LIBMESH_DIM; ++i)
802 element_error = std::max(element_error, std::abs(weight*temperr[i]));
803 else if (norm_type == W2_INF_SEMINORM)
804 for (unsigned int i=0; i != 6; ++i)
805 element_error = std::max(element_error, std::abs(weight*temperr[i]));
806 else if (norm_type == L2)
807 element_error += JxW[sp]*TensorTools::norm_sq(weight*temperr[0]);
808 else if (norm_type == H1_SEMINORM)
809 for (unsigned int i=0; i != LIBMESH_DIM; ++i)
810 element_error += JxW[sp]*TensorTools::norm_sq(weight*temperr[i]);
811 else if (norm_type == H1_X_SEMINORM)
812 element_error += JxW[sp]*TensorTools::norm_sq(weight*temperr[0]);
813 else if (norm_type == H1_Y_SEMINORM)
814 element_error += JxW[sp]*TensorTools::norm_sq(weight*temperr[1]);
815 else if (norm_type == H1_Z_SEMINORM)
816 element_error += JxW[sp]*TensorTools::norm_sq(weight*temperr[2]);
817 else if (norm_type == H2_SEMINORM)
818 {
819 for (unsigned int i=0; i != LIBMESH_DIM; ++i)
820 element_error += JxW[sp]*TensorTools::norm_sq(weight*temperr[i]);
821 // Off diagonal terms enter into the Hessian norm twice
822 for (unsigned int i=3; i != 6; ++i)
823 element_error += JxW[sp]*2*TensorTools::norm_sq(weight*temperr[i]);
824 }
825
826 } // End loop over sample points
827
828 if (norm_type == L_INF ||
829 norm_type == W1_INF_SEMINORM ||
830 norm_type == W2_INF_SEMINORM)
831 new_error_per_cell[e] += error_estimator.error_norm.weight(var) * element_error;
832 else if (norm_type == L2 ||
833 norm_type == H1_SEMINORM ||
834 norm_type == H1_X_SEMINORM ||
835 norm_type == H1_Y_SEMINORM ||
836 norm_type == H1_Z_SEMINORM ||
837 norm_type == H2_SEMINORM)
838 new_error_per_cell[e] += error_estimator.error_norm.weight_sq(var) * element_error;
839 else
840 libmesh_error_msg("Unsupported error norm type == " << Utility::enum_to_string(norm_type));
841 } // End (re) loop over patch elements
842
843 } // end variables loop
844
845 // Now that we have the contributions from each variable,
846 // we have take square roots of the entries we
847 // added to error_per_cell to get an error norm
848 // If we are reusing patches, once again reuse the current patch to loop
849 // over all elements in the current patch, otherwise build a new
850 // patch containing just the current element and loop over it
851 Patch::const_iterator patch_re_it;
852 Patch::const_iterator patch_re_end;
853
854 // Build a new patch if necessary
855 Patch current_elem_patch(mesh.processor_id());
856
858 {
859 // Just get the iterators from the current patch
860 patch_re_it = patch.begin();
861 patch_re_end = patch.end();
862 }
863 else
864 {
865 // Use a target patch size of just 0, this will contain
866 // just the current element.
867 current_elem_patch.build_around_element (elem, 0,
869
870 // Get the iterators from this newly constructed patch
871 patch_re_it = current_elem_patch.begin();
872 patch_re_end = current_elem_patch.end();
873 }
874
875 // Loop over every element in the patch we just constructed
876 for (unsigned int i = 0 ; patch_re_it != patch_re_end; ++patch_re_it, ++i)
877 {
878 // The pth element in the patch
879 const Elem * e_p = *patch_re_it;
880
881 // We'll need an index into the error vector
882 const dof_id_type e_p_id = e_p->id();
883
884 // Update the error_per_cell vector for this element
885 if (error_estimator.error_norm.type(0) == L2 ||
891 {
892 Threads::spin_mutex::scoped_lock acquire(Threads::spin_mtx);
893 if (!error_per_cell[e_p_id])
894 error_per_cell[e_p_id] = static_cast<ErrorVectorReal>
895 (std::sqrt(new_error_per_cell[i]));
896 }
897 else
898 {
902 Threads::spin_mutex::scoped_lock acquire(Threads::spin_mtx);
903 if (!error_per_cell[e_p_id])
904 error_per_cell[e_p_id] = static_cast<ErrorVectorReal>
905 (new_error_per_cell[i]);
906 }
907
908 } // End loop over every element in patch
909
910
911 } // end element loop
912
913} // End () operator definition
914
915} // namespace libMesh
unsigned int n_vars
unsigned int dim
Defines a dense matrix for use in Finite Element-type computations.
void lu_solve(const DenseVector< T > &b, DenseVector< T > &x)
Solve the system Ax=b given the input vector b.
Defines a dense vector for use in Finite Element-type computations.
void resize(const unsigned int n)
Resize the vector.
This class handles the numbering of degrees of freedom on a mesh.
Definition dof_map.h:181
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition dof_map.C:2231
const FEType & variable_type(const unsigned int i) const
Definition dof_map.h:2472
dof_id_type id() const
Definition dof_object.h:833
This is the base class from which all geometric element types are derived.
Definition elem.h:96
unsigned int p_level() const
Definition elem.h:3246
void reduce_error(std::vector< ErrorVectorReal > &error_per_cell, const Parallel::Communicator &comm) const
This method takes the local error contributions in error_per_cell from each processor and combines th...
SystemNorm error_norm
When estimating the error in a single system, the error_norm is used to control the scaling and norm ...
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
This class provides all data required for a physics package (e.g.
Definition fem_context.h:64
virtual void pre_fe_reinit(const System &, const Elem *e)
Reinitializes local data vectors/matrices on the current geometric element.
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition fe_type.h:197
std::unique_ptr< QBase > default_quadrature_rule(const unsigned int dim, const int extraorder=0) const
Definition fe_type.C:35
OrderWrapper order
The approximation order of the element (at 0 p-refinement level).
Definition fe_type.h:203
This is the MeshBase class.
Definition mesh_base.h:81
unsigned int mesh_dimension() const
Definition mesh_base.C:515
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
virtual void swap(NumericVector< T > &v)
Swaps the contents of this with v.
const Parallel::Communicator & comm() const
unsigned int target_patch_size
The PatchErrorEstimator will build patches of at least this many elements to perform estimates.
static std::vector< Real > specpoly(const unsigned int dim, const Order order, const Point p, const unsigned int matsize)
Patch::PMF patch_growth_strategy
The PatchErrorEstimator will use this pointer to a Patch member function when growing patches.
int _extra_order
Extra order to use for quadrature rule.
This class implements useful utility functions for a patch of elements.
Definition patch.h:48
void build_around_element(const Elem *elem, const unsigned int target_patch_size=10, PMF patchtype=&Patch::add_local_face_neighbors)
Erases any elements in the current patch, then builds a new patch containing element elem by repeated...
Definition patch.C:156
This class creates quadrature points on a uniform grid with order+1 intervals per edge.
The StoredRange class defines a contiguous, divisible set of objects.
FEMNormType type(unsigned int var) const
Real weight(unsigned int var) const
Real weight_sq(unsigned int var) const
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
Number current_solution(const dof_id_type global_dof_number) const
Definition system.C:164
Real time
For time-dependent problems, this is the time t at the beginning of the current timestep.
Definition system.h:1689
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition system.h:1667
unsigned int n_vars() const
Definition system.C:2729
const DofMap & get_dof_map() const
Definition system.h:2448
const MeshBase & get_mesh() const
Definition system.h:2432
This class defines a tensor in LIBMESH_DIM dimensional Real or Complex space.
LIBMESH_DEVICE_INLINE void add_scaled(const TypeTensor< T2 > &, const T &)
Add a scaled tensor to this tensor without creating a temporary.
LIBMESH_DEVICE_INLINE void add_scaled(const TypeVector< T2 > &, const T &)
Add a scaled value to this vector without creating a temporary.
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
Class to compute the error contribution for a range of elements.
const System & system
Function to set the boolean patch_reuse in case the user wants to change the default behaviour of pat...
virtual void estimate_error(const System &system, ErrorVector &error_per_cell, const NumericVector< Number > *solution_vector=nullptr, bool estimate_parent_error=false) override
This function uses the Patch Recovery error estimate to estimate the error on each cell.
std::vector< FEMFunctionBase< Number > * > weight_functions
Vector of fem function base pointers, the user will fill this in with pointers to the appropriate wei...
MeshBase & mesh
LIBMESH_DEVICE_INLINE auto norm_sq(const T &a)
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition threads.C:30
void parallel_for(const Range &range, const Body &body, unsigned int n_threads=libMesh::n_threads())
Execute the provided function object in parallel on the specified range.
std::string enum_to_string(const T e)
The libMesh namespace provides an interface to certain functionality in the library.
DIE A HORRIBLE DEATH HERE typedef float ErrorVectorReal
ErrorEstimatorType
Defines an enum for the different types of error estimators which are available.
libmesh_assert(ctx)
const Number zero
.
Definition libmesh.h:297
uint8_t dof_id_type
Definition id_types.h:67
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real