Line data Source code
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 : // Local includes
19 : #include "libmesh/dof_map.h"
20 : #include "libmesh/elem.h"
21 : #include "libmesh/exact_solution.h"
22 : #include "libmesh/equation_systems.h"
23 : #include "libmesh/fe_base.h"
24 : #include "libmesh/function_base.h"
25 : #include "libmesh/mesh_base.h"
26 : #include "libmesh/mesh_function.h"
27 : #include "libmesh/mesh_serializer.h"
28 : #include "libmesh/numeric_vector.h"
29 : #include "libmesh/parallel.h"
30 : #include "libmesh/quadrature.h"
31 : #include "libmesh/wrapped_function.h"
32 : #include "libmesh/wrapped_functor.h"
33 : #include "libmesh/fe_interface.h"
34 : #include "libmesh/raw_accessor.h"
35 : #include "libmesh/tensor_tools.h"
36 : #include "libmesh/enum_norm_type.h"
37 : #include "libmesh/utility.h"
38 :
39 : // C++ Includes
40 : #include <memory>
41 :
42 :
43 : namespace libMesh
44 : {
45 :
46 664 : ExactSolution::ExactSolution(const EquationSystems & es) :
47 296 : _equation_systems(es),
48 296 : _equation_systems_fine(nullptr),
49 1400 : _extra_order(1)
50 : {
51 : // Initialize the _errors data structure which holds all
52 : // the eventual values of the error.
53 1480 : for (auto sys : make_range(_equation_systems.n_systems()))
54 : {
55 : // Reference to the system
56 816 : const System & system = _equation_systems.get_system(sys);
57 :
58 : // The name of the system
59 220 : const std::string & sys_name = system.name();
60 :
61 : // The SystemErrorMap to be inserted
62 440 : ExactSolution::SystemErrorMap sem;
63 :
64 2806 : for (auto var : make_range(system.n_vars()))
65 : {
66 : // The name of this variable
67 1394 : const std::string & var_name = system.variable_name(var);
68 2396 : sem[var_name] = std::vector<Real>(5, 0.);
69 : }
70 :
71 816 : _errors[sys_name] = sem;
72 : }
73 664 : }
74 :
75 :
76 0 : ExactSolution::ExactSolution(ExactSolution &&) = default;
77 592 : ExactSolution::~ExactSolution() = default;
78 :
79 :
80 0 : void ExactSolution::
81 : set_excluded_subdomains(const std::set<subdomain_id_type> & excluded)
82 : {
83 0 : _excluded_subdomains = excluded;
84 0 : }
85 :
86 7 : void ExactSolution::attach_reference_solution (const EquationSystems * es_fine)
87 : {
88 2 : libmesh_assert(es_fine);
89 7 : _equation_systems_fine = es_fine;
90 :
91 : // If we're using a fine grid solution, we're not using exact values
92 5 : _exact_values.clear();
93 5 : _exact_derivs.clear();
94 5 : _exact_hessians.clear();
95 7 : }
96 :
97 :
98 241 : void ExactSolution::attach_exact_value (ValueFunctionPointer fptr)
99 : {
100 70 : libmesh_assert(fptr);
101 :
102 : // Clear out any previous _exact_values entries, then add a new
103 : // entry for each system.
104 241 : _exact_values.clear();
105 :
106 494 : for (auto sys : make_range(_equation_systems.n_systems()))
107 : {
108 253 : const System & system = _equation_systems.get_system(sys);
109 327 : _exact_values.emplace_back(std::make_unique<WrappedFunctor<Number>>(system, fptr, &_equation_systems.parameters));
110 : }
111 :
112 : // If we're using exact values, we're not using a fine grid solution
113 241 : _equation_systems_fine = nullptr;
114 241 : }
115 :
116 :
117 360 : void ExactSolution::attach_exact_values (const std::vector<FunctionBase<Number> *> & f)
118 : {
119 : // Automatically delete any previous _exact_values entries, then add a new
120 : // entry for each system.
121 360 : _exact_values.clear();
122 :
123 720 : for (auto ptr : f)
124 552 : _exact_values.emplace_back(ptr ? std::make_unique<WrappedFunctor<Number>>(*ptr) : nullptr);
125 360 : }
126 :
127 :
128 0 : void ExactSolution::attach_exact_values (const std::vector<FEMFunctionBase<Number> *> & f)
129 : {
130 : // Automatically delete any previous _exact_values entries, then add a new
131 : // entry for each system.
132 0 : _exact_values.clear();
133 :
134 0 : for (auto ptr : f)
135 0 : _exact_values.emplace_back(ptr ? ptr->clone() : nullptr);
136 0 : }
137 :
138 :
139 14 : void ExactSolution::attach_exact_value (unsigned int sys_num,
140 : FunctionBase<Number> * f)
141 : {
142 18 : if (_exact_values.size() <= sys_num)
143 14 : _exact_values.resize(sys_num+1);
144 :
145 14 : if (f)
146 18 : _exact_values[sys_num] = std::make_unique<WrappedFunctor<Number>>(*f);
147 14 : }
148 :
149 :
150 0 : void ExactSolution::attach_exact_value (unsigned int sys_num,
151 : FEMFunctionBase<Number> * f)
152 : {
153 0 : if (_exact_values.size() <= sys_num)
154 0 : _exact_values.resize(sys_num+1);
155 :
156 0 : if (f)
157 0 : _exact_values[sys_num] = f->clone();
158 0 : }
159 :
160 :
161 217 : void ExactSolution::attach_exact_deriv (GradientFunctionPointer gptr)
162 : {
163 62 : libmesh_assert(gptr);
164 :
165 : // Clear out any previous _exact_derivs entries, then add a new
166 : // entry for each system.
167 217 : _exact_derivs.clear();
168 :
169 434 : for (auto sys : make_range(_equation_systems.n_systems()))
170 : {
171 217 : const System & system = _equation_systems.get_system(sys);
172 279 : _exact_derivs.emplace_back(std::make_unique<WrappedFunctor<Gradient>>(system, gptr, &_equation_systems.parameters));
173 : }
174 :
175 : // If we're using exact values, we're not using a fine grid solution
176 217 : _equation_systems_fine = nullptr;
177 217 : }
178 :
179 :
180 360 : void ExactSolution::attach_exact_derivs (const std::vector<FunctionBase<Gradient> *> & g)
181 : {
182 : // Automatically delete any previous _exact_derivs entries, then add a new
183 : // entry for each system.
184 360 : _exact_derivs.clear();
185 :
186 720 : for (auto ptr : g)
187 552 : _exact_derivs.emplace_back(ptr ? std::make_unique<WrappedFunctor<Gradient>>(*ptr) : nullptr);
188 360 : }
189 :
190 :
191 0 : void ExactSolution::attach_exact_derivs (const std::vector<FEMFunctionBase<Gradient> *> & g)
192 : {
193 : // Automatically delete any previous _exact_derivs entries, then add a new
194 : // entry for each system.
195 0 : _exact_derivs.clear();
196 :
197 0 : for (auto ptr : g)
198 0 : _exact_derivs.emplace_back(ptr ? ptr->clone() : nullptr);
199 0 : }
200 :
201 :
202 0 : void ExactSolution::attach_exact_deriv (unsigned int sys_num,
203 : FunctionBase<Gradient> * g)
204 : {
205 0 : if (_exact_derivs.size() <= sys_num)
206 0 : _exact_derivs.resize(sys_num+1);
207 :
208 0 : if (g)
209 0 : _exact_derivs[sys_num] = std::make_unique<WrappedFunctor<Gradient>>(*g);
210 0 : }
211 :
212 :
213 0 : void ExactSolution::attach_exact_deriv (unsigned int sys_num,
214 : FEMFunctionBase<Gradient> * g)
215 : {
216 0 : if (_exact_derivs.size() <= sys_num)
217 0 : _exact_derivs.resize(sys_num+1);
218 :
219 0 : if (g)
220 0 : _exact_derivs[sys_num] = g->clone();
221 0 : }
222 :
223 :
224 42 : void ExactSolution::attach_exact_hessian (HessianFunctionPointer hptr)
225 : {
226 12 : libmesh_assert(hptr);
227 :
228 : // Clear out any previous _exact_hessians entries, then add a new
229 : // entry for each system.
230 42 : _exact_hessians.clear();
231 :
232 84 : for (auto sys : make_range(_equation_systems.n_systems()))
233 : {
234 42 : const System & system = _equation_systems.get_system(sys);
235 54 : _exact_hessians.emplace_back(std::make_unique<WrappedFunctor<Tensor>>(system, hptr, &_equation_systems.parameters));
236 : }
237 :
238 : // If we're using exact values, we're not using a fine grid solution
239 42 : _equation_systems_fine = nullptr;
240 42 : }
241 :
242 :
243 0 : void ExactSolution::attach_exact_hessians (std::vector<FunctionBase<Tensor> *> h)
244 : {
245 : // Automatically delete any previous _exact_hessians entries, then add a new
246 : // entry for each system.
247 0 : _exact_hessians.clear();
248 :
249 0 : for (auto ptr : h)
250 0 : _exact_hessians.emplace_back(ptr ? std::make_unique<WrappedFunctor<Tensor>>(*ptr) : nullptr);
251 0 : }
252 :
253 :
254 0 : void ExactSolution::attach_exact_hessians (std::vector<FEMFunctionBase<Tensor> *> h)
255 : {
256 : // Automatically delete any previous _exact_hessians entries, then add a new
257 : // entry for each system.
258 0 : _exact_hessians.clear();
259 :
260 0 : for (auto ptr : h)
261 0 : _exact_hessians.emplace_back(ptr ? ptr->clone() : nullptr);
262 0 : }
263 :
264 :
265 0 : void ExactSolution::attach_exact_hessian (unsigned int sys_num,
266 : FunctionBase<Tensor> * h)
267 : {
268 0 : if (_exact_hessians.size() <= sys_num)
269 0 : _exact_hessians.resize(sys_num+1);
270 :
271 0 : if (h)
272 0 : _exact_hessians[sys_num] = std::make_unique<WrappedFunctor<Tensor>>(*h);
273 0 : }
274 :
275 :
276 0 : void ExactSolution::attach_exact_hessian (unsigned int sys_num,
277 : FEMFunctionBase<Tensor> * h)
278 : {
279 0 : if (_exact_hessians.size() <= sys_num)
280 0 : _exact_hessians.resize(sys_num+1);
281 :
282 0 : if (h)
283 0 : _exact_hessians[sys_num] = h->clone();
284 0 : }
285 :
286 :
287 12267 : std::vector<Real> & ExactSolution::_check_inputs(std::string_view sys_name,
288 : std::string_view unknown_name)
289 : {
290 : // Return a reference to the proper error entry, or throw an error
291 : // if it doesn't exist.
292 12267 : auto & system_error_map = libmesh_map_find(_errors, sys_name);
293 12267 : return libmesh_map_find(system_error_map, unknown_name);
294 : }
295 :
296 :
297 :
298 2634 : void ExactSolution::compute_error(std::string_view sys_name,
299 : std::string_view unknown_name)
300 : {
301 2250 : LOG_SCOPE("compute_error", "ExactSolution");
302 :
303 : // Check the inputs for validity, and get a reference
304 : // to the proper location to store the error
305 1134 : std::vector<Real> & error_vals = this->_check_inputs(sys_name,
306 1500 : unknown_name);
307 :
308 750 : libmesh_assert( _equation_systems.has_system(sys_name) );
309 2634 : const System & sys = _equation_systems.get_system<System>( sys_name );
310 :
311 750 : libmesh_assert( sys.has_variable( unknown_name ) );
312 2634 : switch( FEInterface::field_type(sys.variable_type( unknown_name )) )
313 : {
314 2204 : case TYPE_SCALAR:
315 : {
316 2204 : this->_compute_error<Real>(sys_name,
317 : unknown_name,
318 : error_vals);
319 1570 : break;
320 : }
321 430 : case TYPE_VECTOR:
322 : {
323 430 : this->_compute_error<RealGradient>(sys_name,
324 : unknown_name,
325 : error_vals);
326 314 : break;
327 : }
328 0 : default:
329 0 : libmesh_error_msg("Invalid variable type!");
330 : }
331 2634 : }
332 :
333 :
334 :
335 :
336 :
337 566 : Real ExactSolution::error_norm(std::string_view sys_name,
338 : std::string_view unknown_name,
339 : const FEMNormType & norm)
340 : {
341 : // Check the inputs for validity, and get a reference
342 : // to the proper location to store the error
343 270 : std::vector<Real> & error_vals = this->_check_inputs(sys_name,
344 296 : unknown_name);
345 :
346 148 : libmesh_assert(_equation_systems.has_system(sys_name));
347 148 : libmesh_assert(_equation_systems.get_system(sys_name).has_variable( unknown_name ));
348 566 : const FEType & fe_type = _equation_systems.get_system(sys_name).variable_type(unknown_name);
349 :
350 566 : switch (norm)
351 : {
352 0 : case L2:
353 0 : return std::sqrt(error_vals[0]);
354 0 : case H1:
355 0 : return std::sqrt(error_vals[0] + error_vals[1]);
356 0 : case H2:
357 0 : return std::sqrt(error_vals[0] + error_vals[1] + error_vals[2]);
358 61 : case HCURL:
359 : {
360 61 : libmesh_error_msg_if(FEInterface::field_type(fe_type) == TYPE_SCALAR,
361 : "Cannot compute HCurl error norm of scalar-valued variables!");
362 :
363 61 : return std::sqrt(error_vals[0] + error_vals[5]);
364 : }
365 222 : case HDIV:
366 : {
367 222 : libmesh_error_msg_if(FEInterface::field_type(fe_type) == TYPE_SCALAR,
368 : "Cannot compute HDiv error norm of scalar-valued variables!");
369 :
370 222 : return std::sqrt(error_vals[0] + error_vals[6]);
371 : }
372 0 : case H1_SEMINORM:
373 0 : return std::sqrt(error_vals[1]);
374 0 : case H2_SEMINORM:
375 0 : return std::sqrt(error_vals[2]);
376 61 : case HCURL_SEMINORM:
377 : {
378 61 : libmesh_error_msg_if(FEInterface::field_type(fe_type) == TYPE_SCALAR,
379 : "Cannot compute HCurl error seminorm of scalar-valued variables!");
380 :
381 61 : return std::sqrt(error_vals[5]);
382 : }
383 222 : case HDIV_SEMINORM:
384 : {
385 222 : libmesh_error_msg_if(FEInterface::field_type(fe_type) == TYPE_SCALAR,
386 : "Cannot compute HDiv error seminorm of scalar-valued variables!");
387 :
388 222 : return std::sqrt(error_vals[6]);
389 : }
390 0 : case L1:
391 0 : return error_vals[3];
392 0 : case L_INF:
393 0 : return error_vals[4];
394 :
395 0 : default:
396 0 : libmesh_error_msg("Currently only Sobolev norms/seminorms are supported!");
397 : }
398 : }
399 :
400 :
401 :
402 :
403 :
404 :
405 :
406 4762 : Real ExactSolution::l2_error(std::string_view sys_name,
407 : std::string_view unknown_name)
408 : {
409 :
410 : // Check the inputs for validity, and get a reference
411 : // to the proper location to store the error
412 2046 : std::vector<Real> & error_vals = this->_check_inputs(sys_name,
413 2716 : unknown_name);
414 :
415 : // Return the square root of the first component of the
416 : // computed error.
417 4762 : return std::sqrt(error_vals[0]);
418 : }
419 :
420 :
421 :
422 :
423 :
424 :
425 :
426 0 : Real ExactSolution::l1_error(std::string_view sys_name,
427 : std::string_view unknown_name)
428 : {
429 :
430 : // Check the inputs for validity, and get a reference
431 : // to the proper location to store the error
432 0 : std::vector<Real> & error_vals = this->_check_inputs(sys_name,
433 0 : unknown_name);
434 :
435 : // Return the square root of the first component of the
436 : // computed error.
437 0 : return error_vals[3];
438 : }
439 :
440 :
441 :
442 :
443 :
444 :
445 :
446 0 : Real ExactSolution::l_inf_error(std::string_view sys_name,
447 : std::string_view unknown_name)
448 : {
449 :
450 : // Check the inputs for validity, and get a reference
451 : // to the proper location to store the error
452 0 : std::vector<Real> & error_vals = this->_check_inputs(sys_name,
453 0 : unknown_name);
454 :
455 : // Return the square root of the first component of the
456 : // computed error.
457 0 : return error_vals[4];
458 : }
459 :
460 :
461 :
462 :
463 :
464 :
465 :
466 2961 : Real ExactSolution::h1_error(std::string_view sys_name,
467 : std::string_view unknown_name)
468 : {
469 : // If the user has supplied no exact derivative function, we
470 : // just integrate the H1 norm of the solution; i.e. its
471 : // difference from an "exact solution" of zero.
472 :
473 : // Check the inputs for validity, and get a reference
474 : // to the proper location to store the error
475 1269 : std::vector<Real> & error_vals = this->_check_inputs(sys_name,
476 1692 : unknown_name);
477 :
478 : // Return the square root of the sum of the computed errors.
479 2961 : return std::sqrt(error_vals[0] + error_vals[1]);
480 : }
481 :
482 :
483 61 : Real ExactSolution::hcurl_error(std::string_view sys_name,
484 : std::string_view unknown_name)
485 : {
486 61 : return this->error_norm(sys_name,unknown_name,HCURL);
487 : }
488 :
489 :
490 222 : Real ExactSolution::hdiv_error(std::string_view sys_name,
491 : std::string_view unknown_name)
492 : {
493 222 : return this->error_norm(sys_name,unknown_name,HDIV);
494 : }
495 :
496 :
497 :
498 1344 : Real ExactSolution::h2_error(std::string_view sys_name,
499 : std::string_view unknown_name)
500 : {
501 : // If the user has supplied no exact derivative functions, we
502 : // just integrate the H2 norm of the solution; i.e. its
503 : // difference from an "exact solution" of zero.
504 :
505 : // Check the inputs for validity, and get a reference
506 : // to the proper location to store the error
507 576 : std::vector<Real> & error_vals = this->_check_inputs(sys_name,
508 768 : unknown_name);
509 :
510 : // Return the square root of the sum of the computed errors.
511 1344 : return std::sqrt(error_vals[0] + error_vals[1] + error_vals[2]);
512 : }
513 :
514 :
515 :
516 :
517 :
518 :
519 :
520 : template<typename OutputShape>
521 2634 : void ExactSolution::_compute_error(std::string_view sys_name,
522 : std::string_view unknown_name,
523 : std::vector<Real> & error_vals)
524 : {
525 : // Make sure we aren't "overconfigured"
526 750 : libmesh_assert (!(_exact_values.size() && _equation_systems_fine));
527 :
528 : // We need a communicator.
529 2634 : const Parallel::Communicator & communicator(_equation_systems.comm());
530 :
531 : // This function must be run on all processors at once
532 750 : libmesh_parallel_only(communicator);
533 :
534 : // Zero the error before summation
535 : // 0 - sum of square of function error (L2)
536 : // 1 - sum of square of gradient error (H1 semi)
537 : // 2 - sum of square of Hessian error (H2 semi)
538 : // 3 - sum of sqrt(square of function error) (L1)
539 : // 4 - max of sqrt(square of function error) (Linfty)
540 : // 5 - sum of square of curl error (HCurl semi)
541 : // 6 - sum of square of div error (HDiv semi)
542 2634 : error_vals.assign(7, 0);
543 :
544 : // Get a reference to the system whose error is being computed.
545 : // If we have a fine grid, however, we'll integrate on that instead
546 : // for more accuracy.
547 2634 : const System & computed_system = _equation_systems_fine ?
548 7 : _equation_systems_fine->get_system(sys_name) :
549 2627 : _equation_systems.get_system (sys_name);
550 :
551 1500 : const MeshBase & mesh = computed_system.get_mesh();
552 :
553 2634 : const unsigned int var = computed_system.variable_number(unknown_name);
554 750 : unsigned int var_component = 0;
555 3578 : for (const auto var_num : make_range(var))
556 : {
557 944 : const auto & var_fe_type = computed_system.variable_type(var_num);
558 944 : const auto var_vec_dim = FEInterface::n_vec_dim(mesh, var_fe_type);
559 944 : var_component += var_vec_dim;
560 : }
561 :
562 : // Prepare a global solution, a serialized mesh, and a MeshFunction
563 : // of the coarse system, if we need them for fine-system integration
564 2634 : std::unique_ptr<MeshFunction> coarse_values;
565 4134 : std::unique_ptr<NumericVector<Number>> comparison_soln =
566 2634 : NumericVector<Number>::build(_equation_systems.comm());
567 : MeshSerializer
568 3384 : serial(const_cast<MeshBase&>(_equation_systems.get_mesh()),
569 2634 : _equation_systems_fine);
570 2634 : if (_equation_systems_fine)
571 : {
572 : const System & comparison_system
573 7 : = _equation_systems.get_system(sys_name);
574 :
575 4 : std::vector<Number> global_soln;
576 7 : comparison_system.update_global_solution(global_soln);
577 9 : comparison_soln->init(comparison_system.solution->size(), true, SERIAL);
578 7 : (*comparison_soln) = global_soln;
579 :
580 12 : coarse_values = std::make_unique<MeshFunction>
581 : (_equation_systems,
582 2 : *comparison_soln,
583 : comparison_system.get_dof_map(),
584 10 : comparison_system.variable_number(unknown_name));
585 7 : coarse_values->init();
586 : }
587 :
588 : // Construct Quadrature rule based on default quadrature order
589 2634 : const FEType & fe_type = computed_system.variable_type(var);
590 2634 : const auto field_type = FEInterface::field_type(fe_type);
591 :
592 2634 : unsigned int n_vec_dim = FEInterface::n_vec_dim( mesh, fe_type );
593 :
594 : // FIXME: MeshFunction needs to be updated to support vector-valued
595 : // elements before we can use a reference solution.
596 2634 : if ((n_vec_dim > 1) && _equation_systems_fine)
597 : {
598 0 : libMesh::err << "Error calculation using reference solution not yet\n"
599 0 : << "supported for vector-valued elements."
600 0 : << std::endl;
601 0 : libmesh_not_implemented();
602 : }
603 :
604 : // Init functors before we enter threads, in case any of their
605 : // clones need point locators etc.
606 4961 : for (auto & ev : _exact_values)
607 2327 : if (ev)
608 2327 : ev->init();
609 :
610 4839 : for (auto & ed : _exact_derivs)
611 2205 : if (ed)
612 2205 : ed->init();
613 :
614 2970 : for (auto & eh : _exact_hessians)
615 336 : if (eh)
616 336 : eh->init();
617 :
618 2634 : const Real time = _equation_systems.get_system(sys_name).time;
619 :
620 : // Mutex for combining error values
621 750 : Threads::spin_mutex error_mtx;
622 :
623 : // Begin the loop over the elements
624 : Threads::parallel_for
625 7942 : (mesh.active_local_element_stored_range(),
626 112752353 : [this, &coarse_values, &computed_system, &error_mtx, &error_vals, &mesh, fe_type, field_type, n_vec_dim, time, var, var_component]
627 1516 : (const ConstElemRange & range)
628 : {
629 : // Grab which element dimensions are present in the mesh
630 758 : const std::set<unsigned char> & elem_dims = mesh.elem_dimensions();
631 :
632 1516 : const unsigned int sys_num = computed_system.number();
633 :
634 : // Get a reference to the dofmap and mesh for that system
635 758 : const DofMap & computed_dof_map = computed_system.get_dof_map();
636 :
637 4176 : FEMContext context(computed_system);
638 :
639 : // Clone and initialize any functors we're going to use
640 2274 : std::vector<std::unique_ptr<FEMFunctionBase<Number>>> my_exact_values;
641 2274 : std::vector<std::unique_ptr<FEMFunctionBase<Gradient>>> my_exact_derivs;
642 2274 : std::vector<std::unique_ptr<FEMFunctionBase<Tensor>>> my_exact_hessians;
643 :
644 5013 : for (auto & ev : _exact_values)
645 2353 : if (ev)
646 : {
647 4034 : my_exact_values.push_back(ev->clone());
648 672 : auto & my_ev = my_exact_values.back();
649 2353 : my_ev->init();
650 2353 : my_ev->init_context(context);
651 : }
652 : else
653 0 : my_exact_values.push_back(nullptr);
654 :
655 4891 : for (auto & ed : _exact_derivs)
656 2231 : if (ed)
657 : {
658 3830 : my_exact_derivs.push_back(ed->clone());
659 632 : auto & my_ed = my_exact_derivs.back();
660 2231 : my_ed->init();
661 2231 : my_ed->init_context(context);
662 : }
663 : else
664 0 : my_exact_derivs.push_back(nullptr);
665 :
666 2996 : for (auto & eh : _exact_hessians)
667 336 : if (eh)
668 : {
669 576 : my_exact_hessians.push_back(eh->clone());
670 96 : auto & my_eh = my_exact_hessians.back();
671 336 : my_eh->init();
672 336 : my_eh->init_context(context);
673 : }
674 : else
675 0 : my_exact_hessians.push_back(nullptr);
676 :
677 1902 : std::unique_ptr<MeshFunction> my_coarse_values;
678 2660 : if (coarse_values)
679 : {
680 9 : auto clone = coarse_values->clone();
681 : // clone() returns FunctionBase, but it's a MeshFunction
682 : my_coarse_values.reset
683 2 : (static_cast<MeshFunction *>(clone.release()));
684 7 : my_coarse_values->init();
685 3 : }
686 :
687 : // If we have *no* functors we intend to use (because we're using a
688 : // fine system, or because our exact solution is zero and we're just
689 : // computing norms in an outdated way) then let our FE objects know
690 : // we don't actually need anything from them, so they don't think
691 : // we've just invoked them in a deprecated "compute everything"
692 : // fashion.
693 2758 : if (_exact_values.empty() && _exact_derivs.empty() &&
694 98 : _exact_hessians.empty())
695 686 : for (auto dim : elem_dims)
696 686 : for (auto v : make_range(computed_system.n_vars()))
697 : {
698 : FEAbstract * fe;
699 98 : context.get_element_fe(v, fe, dim);
700 98 : fe->get_nothing();
701 : }
702 :
703 : // Zero the error before summation
704 : // 0 - sum of square of function error (L2)
705 : // 1 - sum of square of gradient error (H1 semi)
706 : // 2 - sum of square of Hessian error (H2 semi)
707 : // 3 - sum of sqrt(square of function error) (L1)
708 : // 4 - max of sqrt(square of function error) (Linfty)
709 : // 5 - sum of square of curl error (HCurl semi)
710 : // 6 - sum of square of div error (HDiv semi)
711 4176 : std::vector<Real> my_error_vals(error_vals.size(), 0);
712 :
713 : // Allow space for dims 0-3, even if we don't use them all
714 4176 : std::vector<std::unique_ptr<FEGenericBase<OutputShape>>> fe_ptrs(4);
715 4176 : std::vector<std::unique_ptr<QBase>> q_rules(4);
716 :
717 : // Prepare finite elements for each dimension present in the mesh
718 5327 : for (const auto dim : elem_dims)
719 : {
720 : // Build a quadrature rule.
721 2667 : q_rules[dim] = fe_type.default_quadrature_rule (dim, _extra_order);
722 :
723 : // Disallow rules with negative weights. That will use more
724 : // quadrature points, but we're going to be taking square roots
725 : // of element integral results here!
726 2667 : q_rules[dim]->allow_rules_with_negative_weights = false;
727 :
728 : // Construct finite element object
729 3814 : fe_ptrs[dim] = FEGenericBase<OutputShape>::build(dim, fe_type);
730 :
731 : // Attach quadrature rule to FE object
732 4187 : fe_ptrs[dim]->attach_quadrature_rule (q_rules[dim].get());
733 : }
734 :
735 : // The global degree of freedom indices associated
736 : // with the local degrees of freedom.
737 1516 : std::vector<dof_id_type> dof_indices;
738 :
739 520463 : for (const Elem * elem : range)
740 : {
741 : // Skip this element if it is in a subdomain excluded by the user.
742 517803 : const subdomain_id_type elem_subid = elem->subdomain_id();
743 126018 : if (_excluded_subdomains.count(elem_subid))
744 0 : continue;
745 :
746 : // The spatial dimension of the current Elem. FEs and other data
747 : // are indexed on dim.
748 517803 : const unsigned int dim = elem->dim();
749 :
750 : // If the variable is not active on this subdomain, don't bother
751 517803 : if (!computed_system.variable(var).active_on_subdomain(elem_subid))
752 0 : continue;
753 :
754 : /* If the variable is active, then we're going to restrict the
755 : MeshFunction evaluations to the current element subdomain.
756 : This is for cases such as mixed dimension meshes where we want
757 : to restrict the calculation to one particular domain. */
758 252036 : std::set<subdomain_id_type> subdomain_id;
759 391785 : subdomain_id.insert(elem_subid);
760 :
761 517803 : FEGenericBase<OutputShape> * fe = fe_ptrs[dim].get();
762 252036 : QBase * qrule = q_rules[dim].get();
763 126018 : libmesh_assert(fe);
764 126018 : libmesh_assert(qrule);
765 :
766 : // The Jacobian*weight at the quadrature points.
767 517803 : const std::vector<Real> & JxW = fe->get_JxW();
768 :
769 : // The value of the shape functions at the quadrature points
770 : // i.e. phi(i) = phi_values[i][qp]
771 126018 : const std::vector<std::vector<OutputShape>> & phi_values = fe->get_phi();
772 :
773 : // The value of the shape function gradients at the quadrature points
774 : const std::vector<std::vector<typename FEGenericBase<OutputShape>::OutputGradient>> &
775 126018 : dphi_values = fe->get_dphi();
776 :
777 : // The value of the shape function curls at the quadrature points
778 : // Only computed for vector-valued elements
779 126018 : const std::vector<std::vector<typename FEGenericBase<OutputShape>::OutputShape>> * curl_values = nullptr;
780 :
781 : // The value of the shape function divergences at the quadrature points
782 : // Only computed for vector-valued elements
783 126018 : const std::vector<std::vector<typename FEGenericBase<OutputShape>::OutputDivergence>> * div_values = nullptr;
784 :
785 517803 : if (field_type == TYPE_VECTOR)
786 : {
787 140577 : curl_values = &fe->get_curl_phi();
788 140577 : div_values = &fe->get_div_phi();
789 : }
790 :
791 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
792 : // The value of the shape function second derivatives at the quadrature points
793 : // Not computed for vector-valued elements
794 : const std::vector<std::vector<typename FEGenericBase<OutputShape>::OutputTensor>> *
795 126018 : d2phi_values = nullptr;
796 :
797 517803 : if (field_type != TYPE_VECTOR)
798 79159 : d2phi_values = &fe->get_d2phi();
799 : #endif
800 :
801 : // The XYZ locations (in physical space) of the quadrature points
802 377879 : const std::vector<Point> & q_point = fe->get_xyz();
803 :
804 : // reinitialize the element-specific data
805 : // for the current element
806 517803 : fe->reinit (elem);
807 :
808 517803 : context.pre_fe_reinit(computed_system, elem);
809 517803 : context.elem_fe_reinit();
810 :
811 : // Get the local to global degree of freedom maps
812 517803 : computed_dof_map.dof_indices (elem, dof_indices, var);
813 :
814 : // The number of quadrature points
815 126018 : const unsigned int n_qp = qrule->n_points();
816 :
817 : // The number of shape functions
818 : const unsigned int n_sf =
819 252036 : cast_int<unsigned int>(dof_indices.size());
820 :
821 : // Begin the loop over the Quadrature points.
822 8669482 : for (unsigned int qp=0; qp<n_qp; qp++)
823 : {
824 693190 : typename FEGenericBase<OutputShape>::OutputNumber u_h(0.);
825 :
826 1982881 : typename FEGenericBase<OutputShape>::OutputNumberGradient grad_u_h;
827 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
828 2676071 : typename FEGenericBase<OutputShape>::OutputNumberTensor grad2_u_h;
829 : #endif
830 693190 : typename FEGenericBase<OutputShape>::OutputNumber curl_u_h(0.0);
831 1982881 : typename FEGenericBase<OutputShape>::OutputNumberDivergence div_u_h = 0.0;
832 :
833 : // Compute solution values at the current
834 : // quadrature point. This requires a sum
835 : // over all the shape functions evaluated
836 : // at the quadrature point.
837 78925223 : for (unsigned int i=0; i<n_sf; i++)
838 : {
839 : // Values from current solution.
840 111853303 : u_h += phi_values[i][qp]*computed_system.current_solution (dof_indices[i]);
841 88157757 : grad_u_h += dphi_values[i][qp]*computed_system.current_solution (dof_indices[i]);
842 70773544 : if (field_type == TYPE_VECTOR)
843 : {
844 28766514 : curl_u_h += (*curl_values)[i][qp]*computed_system.current_solution (dof_indices[i]);
845 39839394 : div_u_h += (*div_values)[i][qp]*computed_system.current_solution (dof_indices[i]);
846 : }
847 : else
848 : {
849 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
850 95086940 : grad2_u_h += (*d2phi_values)[i][qp]*computed_system.current_solution (dof_indices[i]);
851 : #endif
852 : }
853 : }
854 :
855 : // Compute the value of the error at this quadrature point
856 4566735 : typename FEGenericBase<OutputShape>::OutputNumber exact_val(0);
857 1982881 : RawAccessor<typename FEGenericBase<OutputShape>::OutputNumber> exact_val_accessor( exact_val, n_vec_dim );
858 10134560 : if (_exact_values.size() > sys_num && _exact_values[sys_num])
859 : {
860 19648174 : for (unsigned int c = 0; c < n_vec_dim; c++)
861 12433555 : exact_val_accessor(c) =
862 : _exact_values[sys_num]->
863 18341647 : component(context, var_component+c, q_point[qp], time);
864 : }
865 937060 : else if (_equation_systems_fine)
866 : {
867 : // FIXME: Needs to be updated for vector-valued elements
868 281880 : DenseVector<Number> output(1);
869 469800 : (*my_coarse_values)(q_point[qp],time,output,&subdomain_id);
870 375840 : exact_val = output(0);
871 : }
872 3272568 : const typename FEGenericBase<OutputShape>::OutputNumber val_error = u_h - exact_val;
873 :
874 : // Add the squares of the error to each contribution
875 1982881 : Real error_sq = TensorTools::norm_sq(val_error);
876 8151679 : my_error_vals[0] += JxW[qp]*error_sq;
877 :
878 8151679 : Real norm = sqrt(error_sq);
879 8151679 : my_error_vals[3] += JxW[qp]*norm;
880 :
881 8151679 : if (my_error_vals[4]<norm) { my_error_vals[4] = norm; }
882 :
883 : // Compute the value of the error in the gradient at this
884 : // quadrature point
885 1982881 : typename FEGenericBase<OutputShape>::OutputNumberGradient exact_grad;
886 1982881 : RawAccessor<typename FEGenericBase<OutputShape>::OutputNumberGradient> exact_grad_accessor( exact_grad, LIBMESH_DIM );
887 10134560 : if (_exact_derivs.size() > sys_num && _exact_derivs[sys_num])
888 : {
889 19572406 : for (unsigned int c = 0; c < n_vec_dim; c++)
890 49582684 : for (unsigned int d = 0; d < LIBMESH_DIM; d++)
891 42878823 : exact_grad_accessor(d + c*LIBMESH_DIM) =
892 : _exact_derivs[sys_num]->
893 54853599 : component(context, var_component+c, q_point[qp], time)(d);
894 : }
895 974944 : else if (_equation_systems_fine)
896 : {
897 : // FIXME: Needs to be updated for vector-valued elements
898 375840 : std::vector<Gradient> output(1);
899 469800 : my_coarse_values->gradient(q_point[qp],time,output,&subdomain_id);
900 375840 : exact_grad = output[0];
901 : }
902 :
903 4278134 : const typename FEGenericBase<OutputShape>::OutputNumberGradient grad_error = grad_u_h - exact_grad;
904 :
905 8840393 : my_error_vals[1] += JxW[qp]*grad_error.norm_sq();
906 :
907 :
908 8151679 : if (field_type == TYPE_VECTOR)
909 : {
910 : // Compute the value of the error in the curl at this
911 : // quadrature point
912 693190 : typename FEGenericBase<OutputShape>::OutputNumber exact_curl(0.0);
913 3681633 : if (_exact_derivs.size() > sys_num && _exact_derivs[sys_num])
914 : {
915 2988443 : exact_curl = TensorTools::curl_from_grad( exact_grad );
916 : }
917 0 : else if (_equation_systems_fine)
918 : {
919 : // FIXME: Need to implement curl for MeshFunction and support reference
920 : // solution for vector-valued elements
921 : }
922 :
923 693190 : const typename FEGenericBase<OutputShape>::OutputNumber curl_error = curl_u_h - exact_curl;
924 :
925 3681633 : my_error_vals[5] += JxW[qp]*TensorTools::norm_sq(curl_error);
926 :
927 : // Compute the value of the error in the divergence at this
928 : // quadrature point
929 693190 : typename FEGenericBase<OutputShape>::OutputNumberDivergence exact_div = 0.0;
930 3681633 : if (_exact_derivs.size() > sys_num && _exact_derivs[sys_num])
931 : {
932 2988443 : exact_div = TensorTools::div_from_grad( exact_grad );
933 : }
934 0 : else if (_equation_systems_fine)
935 : {
936 : // FIXME: Need to implement div for MeshFunction and support reference
937 : // solution for vector-valued elements
938 : }
939 :
940 2295253 : const typename FEGenericBase<OutputShape>::OutputNumberDivergence div_error = div_u_h - exact_div;
941 :
942 4374823 : my_error_vals[6] += JxW[qp]*TensorTools::norm_sq(div_error);
943 : }
944 :
945 : #ifdef LIBMESH_ENABLE_SECOND_DERIVATIVES
946 : // Compute the value of the error in the hessian at this
947 : // quadrature point
948 2676071 : typename FEGenericBase<OutputShape>::OutputNumberTensor exact_hess;
949 1982881 : RawAccessor<typename FEGenericBase<OutputShape>::OutputNumberTensor> exact_hess_accessor( exact_hess, dim );
950 10134560 : if (_exact_hessians.size() > sys_num && _exact_hessians[sys_num])
951 : {
952 : //FIXME: This needs to be implemented to support rank 3 tensors
953 : // which can't happen until type_n_tensor is fully implemented
954 : // and a RawAccessor<TypeNTensor> is fully implemented
955 561220 : if (field_type == TYPE_VECTOR)
956 0 : libmesh_not_implemented();
957 :
958 1122440 : for (unsigned int c = 0; c < n_vec_dim; c++)
959 1682892 : for (unsigned int d = 0; d < dim; d++)
960 3364248 : for (unsigned int e =0; e < dim; e++)
961 2797820 : exact_hess_accessor(d + e*dim + c*dim*dim) =
962 : _exact_hessians[sys_num]->
963 3353064 : component(context, var_component+c, q_point[qp], time)(d,e);
964 :
965 : // FIXME: operator- is not currently implemented for TypeNTensor
966 561220 : const typename FEGenericBase<OutputShape>::OutputNumberTensor grad2_error = grad2_u_h - exact_hess;
967 1122440 : my_error_vals[2] += JxW[qp]*grad2_error.norm_sq();
968 : }
969 7590459 : else if (_equation_systems_fine)
970 : {
971 : // FIXME: Needs to be updated for vector-valued elements
972 375840 : std::vector<Tensor> output(1);
973 469800 : my_coarse_values->hessian(q_point[qp],time,output,&subdomain_id);
974 93960 : exact_hess = output[0];
975 :
976 : // FIXME: operator- is not currently implemented for TypeNTensor
977 375840 : const typename FEGenericBase<OutputShape>::OutputNumberTensor grad2_error = grad2_u_h - exact_hess;
978 751680 : my_error_vals[2] += JxW[qp]*grad2_error.norm_sq();
979 : }
980 : #endif // SECOND_DERIVATIVES
981 : } // end qp loop
982 : } // end elem loop
983 :
984 1516 : Threads::spin_mutex::scoped_lock lock(error_mtx);
985 :
986 21280 : for (auto i : index_range(my_error_vals))
987 : {
988 18620 : if (i == 4)
989 4143 : error_vals[4] = std::max(error_vals[4], my_error_vals[4]);
990 : else
991 25056 : error_vals[i] += my_error_vals[i];
992 : }
993 2288 : }); // end threaded element loop
994 :
995 : // Add up the error values on all processors, except for the L-infty
996 : // norm, for which the maximum is computed.
997 2634 : Real l_infty_norm = error_vals[4];
998 1884 : communicator.max(l_infty_norm);
999 2634 : communicator.sum(error_vals);
1000 2634 : error_vals[4] = l_infty_norm;
1001 2634 : }
1002 :
1003 : // Explicit instantiations of templated member functions
1004 : template LIBMESH_EXPORT void ExactSolution::_compute_error<Real>(std::string_view, std::string_view, std::vector<Real> &);
1005 : template LIBMESH_EXPORT void ExactSolution::_compute_error<RealGradient>(std::string_view, std::string_view, std::vector<Real> &);
1006 :
1007 : } // namespace libMesh
|