libMesh
Loading...
Searching...
No Matches
system_projection.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
20// C++ includes
21#include <vector>
22#include <numeric> // std::iota
23
24// Local includes
25#include "libmesh/libmesh_config.h"
26
27#ifdef LIBMESH_HAVE_METAPHYSICL
28
29// With quad precision we need the shim function declarations to
30// precede the MetaPhysicL use of them
31#include "libmesh/libmesh_common.h"
32#include "libmesh/compare_types.h"
33
34// Template specialization declarations in here need to *precede* code
35// using them.
36#include "metaphysicl/dynamicsparsenumberarray_decl.h"
37
38using MetaPhysicL::DynamicSparseNumberArray;
39
40namespace libMesh
41{
42// From the perspective of libMesh gradient vectors, a DSNA is a
43// scalar component
44template <typename T, typename IndexType>
45struct ScalarTraits<MetaPhysicL::DynamicSparseNumberArray<T,IndexType> >
46{
47 static const bool value = true;
48};
49
50// And although MetaPhysicL knows how to combine DSNA with something
51// else, we need to teach libMesh too.
52template <typename T, typename IndexType, typename T2>
53struct CompareTypes<MetaPhysicL::DynamicSparseNumberArray<T,IndexType>, T2>
54{
55 typedef typename
56 MetaPhysicL::DynamicSparseNumberArray
58};
59
60template <typename T> struct TypeToSend;
61
62template <typename T, typename IndexType>
63struct TypeToSend<MetaPhysicL::DynamicSparseNumberArray<T,IndexType>> {
64 typedef std::vector<std::pair<IndexType,T>> type;
65};
66
67template <typename T, typename IndexType>
68const std::vector<std::pair<IndexType,T>>
69convert_to_send(MetaPhysicL::DynamicSparseNumberArray<T,IndexType> & in)
70{
71 const std::size_t in_size = in.size();
72 std::vector<std::pair<IndexType,T>> returnval(in_size);
73
74 for (std::size_t i=0; i != in_size; ++i)
75 {
76 returnval[i].first = in.raw_index(i);
77 returnval[i].second = in.raw_at(i);
78 }
79 return returnval;
80}
81
82template <typename SendT, typename T, typename IndexType>
83void convert_from_receive (SendT & received,
84 MetaPhysicL::DynamicSparseNumberArray<T,IndexType> & converted)
85{
86 const std::size_t received_size = received.size();
87 converted.resize(received_size);
88 for (std::size_t i=0; i != received_size; ++i)
89 {
90 converted.raw_index(i) = received[i].first;
91 converted.raw_at(i) = received[i].second;
92 }
93}
94
95}
96
97
98#endif
99
100#include "libmesh/boundary_info.h"
101#include "libmesh/dense_matrix.h"
102#include "libmesh/dense_vector.h"
103#include "libmesh/dof_map.h"
104#include "libmesh/elem.h"
105#include "libmesh/enum_to_string.h"
106#include "libmesh/fe_base.h"
107#include "libmesh/fe_interface.h"
108#include "libmesh/generic_projector.h"
109#include "libmesh/int_range.h"
110#include "libmesh/libmesh_logging.h"
111#include "libmesh/linear_solver.h"
112#include "libmesh/mesh_base.h"
113#include "libmesh/numeric_vector.h"
114#include "libmesh/quadrature.h"
115#include "libmesh/sparse_matrix.h"
116#include "libmesh/system.h"
117#include "libmesh/threads.h"
118#include "libmesh/wrapped_function.h"
119#include "libmesh/wrapped_functor.h"
120#include "libmesh/fe_interface.h"
121
122
123
124#ifdef LIBMESH_HAVE_METAPHYSICL
125// Include MetaPhysicL definitions finally
126#include "metaphysicl/dynamicsparsenumberarray.h"
127
128// And make sure we instantiate the methods we'll need to use on them.
129#include "libmesh/dense_matrix_impl.h"
130
131namespace libMesh {
132typedef DynamicSparseNumberArray<Real, dof_id_type> DSNAN;
133
134template LIBMESH_EXPORT void
137template LIBMESH_EXPORT void
139 DenseVector<DSNAN> &) const;
140}
141#endif
142
143
144
145namespace libMesh
146{
147
148// ------------------------------------------------------------
149// Helper class definitions
150
151#ifdef LIBMESH_ENABLE_AMR
152
164{
165private:
166 const System & system;
167
168public:
169 BuildProjectionList (const System & system_in) :
170 system(system_in),
171 send_list()
172 {}
173
175 system(other.system),
176 send_list()
177 {}
178
179 void unique();
180 void operator()(const ConstElemRange & range);
181 void join (const BuildProjectionList & other);
182 std::vector<dof_id_type> send_list;
183};
184
185#endif // LIBMESH_ENABLE_AMR
186
187
194{
195private:
196 const std::set<boundary_id_type> & b;
197 const std::vector<unsigned int> & variables;
198 const System & system;
199 std::unique_ptr<FunctionBase<Number>> f;
200 std::unique_ptr<FunctionBase<Gradient>> g;
203
204public:
205 BoundaryProjectSolution (const std::set<boundary_id_type> & b_in,
206 const std::vector<unsigned int> & variables_in,
207 const System & system_in,
210 const Parameters & parameters_in,
211 NumericVector<Number> & new_v_in) :
212 b(b_in),
213 variables(variables_in),
214 system(system_in),
215 f(f_in ? f_in->clone() : std::unique_ptr<FunctionBase<Number>>()),
216 g(g_in ? g_in->clone() : std::unique_ptr<FunctionBase<Gradient>>()),
217 parameters(parameters_in),
218 new_vector(new_v_in)
219 {
220 libmesh_assert(f.get());
221 f->init();
222 if (g.get())
223 g->init();
224 }
225
227 b(in.b),
228 variables(in.variables),
229 system(in.system),
230 f(in.f.get() ? in.f->clone() : std::unique_ptr<FunctionBase<Number>>()),
231 g(in.g.get() ? in.g->clone() : std::unique_ptr<FunctionBase<Gradient>>()),
232 parameters(in.parameters),
233 new_vector(in.new_vector)
234 {
235 libmesh_assert(f.get());
236 f->init();
237 if (g.get())
238 g->init();
239 }
240
241 void operator()(const ConstElemRange & range) const;
242};
243
244
245
246// ------------------------------------------------------------
247// System implementation
248void System::project_vector (NumericVector<Number> & vector,
249 int is_adjoint,
250 std::optional<ConstElemRange> active_local_range,
251 std::optional<std::vector<unsigned int>> variable_numbers) const
252{
253 // Create a copy of the vector, which currently
254 // contains the old data.
255 std::unique_ptr<NumericVector<Number>>
256 old_vector (vector.clone());
257
258 // Project the old vector to the new vector
259 this->project_vector (*old_vector, vector, is_adjoint, active_local_range, variable_numbers);
260}
261
262
268void System::project_vector (const NumericVector<Number> & old_v,
269 NumericVector<Number> & new_v,
270 int is_adjoint,
271 std::optional<ConstElemRange> active_local_range,
272 std::optional<std::vector<unsigned int>> variable_numbers) const
273{
274 LOG_SCOPE ("project_vector(old,new)", "System");
275
282 new_v.clear();
283
284#ifdef LIBMESH_ENABLE_AMR
285
286 // Resize the new vector and get a serial version.
287 NumericVector<Number> * new_vector_ptr = nullptr;
288 std::unique_ptr<NumericVector<Number>> new_vector_built;
289 NumericVector<Number> * local_old_vector;
290 std::unique_ptr<NumericVector<Number>> local_old_vector_built;
291 const NumericVector<Number> * old_vector_ptr = nullptr;
292
293 const ConstElemRange & range =
294 active_local_range ?
295 active_local_range.value() :
296 this->get_mesh().active_local_element_stored_range();
297
298 // If the old vector was uniprocessor, make the new
299 // vector uniprocessor
300 if (old_v.type() == SERIAL)
301 {
302 new_v.init (this->n_dofs(), false, SERIAL);
303 new_vector_ptr = &new_v;
304 old_vector_ptr = &old_v;
305 }
306
307 // Otherwise it is a parallel, distributed vector, which
308 // we need to localize.
309 else if (old_v.type() == PARALLEL)
310 {
311 // Build a send list for efficient localization
312 BuildProjectionList projection_list(*this);
313 Threads::parallel_reduce (range, projection_list);
314
315 // Create a sorted, unique send_list
316 projection_list.unique();
317
318 new_v.init (this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
319 new_vector_built = NumericVector<Number>::build(this->comm());
320 local_old_vector_built = NumericVector<Number>::build(this->comm());
321 new_vector_ptr = new_vector_built.get();
322 local_old_vector = local_old_vector_built.get();
323 new_vector_ptr->init(this->n_dofs(), this->n_local_dofs(),
324 this->get_dof_map().get_send_list(), false,
325 GHOSTED);
326 local_old_vector->init(old_v.size(), old_v.local_size(),
327 projection_list.send_list, false, GHOSTED);
328 old_v.localize(*local_old_vector, projection_list.send_list);
329 local_old_vector->close();
330 old_vector_ptr = local_old_vector;
331 }
332 else if (old_v.type() == GHOSTED)
333 {
334 // Build a send list for efficient localization
335 BuildProjectionList projection_list(*this);
336 Threads::parallel_reduce (range, projection_list);
337
338 // Create a sorted, unique send_list
339 projection_list.unique();
340
341 new_v.init (this->n_dofs(), this->n_local_dofs(),
342 this->get_dof_map().get_send_list(), false, GHOSTED);
343
344 local_old_vector_built = NumericVector<Number>::build(this->comm());
345 new_vector_ptr = &new_v;
346 local_old_vector = local_old_vector_built.get();
347 local_old_vector->init(old_v.size(), old_v.local_size(),
348 projection_list.send_list, false, GHOSTED);
349 old_v.localize(*local_old_vector, projection_list.send_list);
350 local_old_vector->close();
351 old_vector_ptr = local_old_vector;
352 }
353 else // unknown old_v.type()
354 libmesh_error_msg("ERROR: Unknown old_v.type() == " << old_v.type());
355
356 // Note that the above will have zeroed the new_vector.
357 // Just to be sure, assert that new_vector_ptr and old_vector_ptr
358 // were successfully set before trying to deref them.
359 libmesh_assert(new_vector_ptr);
360 libmesh_assert(old_vector_ptr);
361
362 NumericVector<Number> & new_vector = *new_vector_ptr;
363 const NumericVector<Number> & old_vector = *old_vector_ptr;
364
365 const unsigned int n_variables = this->n_vars();
366
367 if (n_variables)
368 {
369 std::vector<unsigned int> vars;
370 if (variable_numbers)
371 {
372 vars = *variable_numbers;
373 for (auto v : vars)
374 if (v >= n_variables)
375 libmesh_error_msg("ERROR: variable number " << v <<
376 " out of range for system with " <<
377 n_variables << " variables.");
378 }
379 else
380 {
381 vars.resize(n_variables);
382 std::iota(vars.begin(), vars.end(), 0);
383 }
384
385 std::vector<unsigned int> regular_vars, vector_vars, scalar_vars;
386 for (auto var : vars)
387 {
388 if (this->variable(var).type().family == SCALAR)
389 scalar_vars.push_back(var);
390 else if (FEInterface::field_type(this->variable_type(var)) == TYPE_SCALAR)
391 regular_vars.push_back(var);
392 else
393 vector_vars.push_back(var);
394 }
395
396 VectorSetAction<Number> setter(new_vector);
397
398 if (!regular_vars.empty())
399 {
400 // Use a typedef to make the calling sequence for parallel_for() a bit more readable
401 typedef
404 Number, VectorSetAction<Number>> FEMProjector;
405
407 f(*this, old_vector, &regular_vars);
409 g(*this, old_vector, &regular_vars);
410
411 FEMProjector projector(*this, f, &g, setter, regular_vars);
412 projector.project(range);
413 }
414
415 if (!vector_vars.empty())
416 {
417 typedef
420 Gradient, VectorSetAction<Number>> FEMVectorProjector;
421
422 OldSolutionValue<Gradient, &FEMContext::point_value> f_vector(*this, old_vector, &vector_vars);
423 OldSolutionValue<Tensor, &FEMContext::point_gradient> g_vector(*this, old_vector, &vector_vars);
424
425 FEMVectorProjector vector_projector(*this, f_vector, &g_vector, setter, vector_vars);
426 vector_projector.project(range);
427 }
428
429 // Copy the SCALAR dofs from old_vector to new_vector
430 // Note: We assume that all SCALAR dofs are on the
431 // processor with highest ID
432 if (this->processor_id() == (this->n_processors()-1))
433 {
434 const DofMap & dof_map = this->get_dof_map();
435 for (auto var : scalar_vars)
436 {
437 // We can just map SCALAR dofs directly across
438 std::vector<dof_id_type> new_SCALAR_indices, old_SCALAR_indices;
439 dof_map.SCALAR_dof_indices (new_SCALAR_indices, var, false);
440 dof_map.SCALAR_dof_indices (old_SCALAR_indices, var, true);
441 for (auto i : index_range(new_SCALAR_indices))
442 new_vector.set(new_SCALAR_indices[i], old_vector(old_SCALAR_indices[i]));
443 }
444 }
445 }
446
447 new_vector.close();
448
449 // If the old vector was serial, we probably need to send our values
450 // to other processors
451 //
452 // FIXME: I'm not sure how to make a NumericVector do that without
453 // creating a temporary parallel vector to use localize! - RHS
454 if (old_v.type() == SERIAL)
455 {
456 std::unique_ptr<NumericVector<Number>> dist_v = NumericVector<Number>::build(this->comm());
457 dist_v->init(this->n_dofs(), this->n_local_dofs(), false, PARALLEL);
458 dist_v->close();
459
460 for (auto i : make_range(dist_v->size()))
461 if (new_vector(i) != 0.0)
462 dist_v->set(i, new_vector(i));
463
464 dist_v->close();
465
466 dist_v->localize (new_v, this->get_dof_map().get_send_list());
467 new_v.close();
468 }
469 // If the old vector was parallel, we need to update it
470 // and free the localized copies
471 else if (old_v.type() == PARALLEL)
472 {
473 // We may have to set dof values that this processor doesn't
474 // own in certain special cases, like LAGRANGE FIRST or
475 // HERMITE THIRD elements on second-order meshes?
476 new_v = new_vector;
477 new_v.close();
478 }
479
480
481 // Apply constraints only if we we are asked to
482 if(this->project_with_constraints)
483 {
484 if (is_adjoint == -1)
485 {
486 this->get_dof_map().enforce_constraints_exactly(*this, &new_v);
487 }
488 else if (is_adjoint >= 0)
489 {
490 this->get_dof_map().enforce_adjoint_constraints_exactly(new_v,
491 is_adjoint);
492 }
493 }
494#else
495
496 // AMR is disabled: simply copy the vector
497 new_v = old_v;
498
499 libmesh_ignore(is_adjoint, active_local_range, variable_numbers);
500
501#endif // #ifdef LIBMESH_ENABLE_AMR
502}
503
504
505#ifdef LIBMESH_ENABLE_AMR
506#ifdef LIBMESH_HAVE_METAPHYSICL
507
508template <typename Output>
510{
511public:
512 typedef DynamicSparseNumberArray<Output, dof_id_type> type;
513};
514
515template <typename InnerOutput>
521
531template <typename Output,
532 void (FEMContext::*point_output) (unsigned int,
533 const Point &,
534 Output &,
535 const Real) const>
536class OldSolutionCoefs : public OldSolutionBase<Output, point_output>
537{
538public:
542
544 const std::vector<unsigned int> * vars) :
545 OldSolutionBase<Output, point_output>(sys_in, vars)
546 {
547 this->old_context.set_algebraic_type(FEMContext::OLD_DOFS_ONLY);
548 }
549
551 OldSolutionBase<Output, point_output>(in.sys, in.old_context.active_vars())
552 {
553 this->old_context.set_algebraic_type(FEMContext::OLD_DOFS_ONLY);
554 }
555
557 unsigned int i,
558 unsigned int elem_dim,
559 const Node & n,
560 bool extra_hanging_dofs,
561 Real /* time */ = 0.);
562
564 unsigned int i,
565 const Point & p,
566 Real time,
567 bool skip_context_check);
568
569 void eval_mixed_derivatives (const FEMContext & libmesh_dbg_var(c),
570 unsigned int i,
571 unsigned int dim,
572 const Node & n,
573 std::vector<DSNA> & derivs)
574 {
575 LOG_SCOPE ("eval_mixed_derivatives", "OldSolutionCoefs");
576
577 // This should only be called on vertices
578 libmesh_assert_less(c.get_elem().get_node_index(&n),
579 c.get_elem().n_vertices());
580
581 // Handle offset from non-scalar components in previous variables
582 libmesh_assert_less(i, this->component_to_var.size());
583 unsigned int var = this->component_to_var[i];
584
585 // We have 1 mixed derivative in 2D, 4 in 3D
586 const unsigned int n_mixed = (dim-1) * (dim-1);
587 derivs.resize(n_mixed);
588
589 // Be sure to handle cases where the variable wasn't defined on
590 // this node (e.g. due to changing subdomain support)
591 const DofObject * old_dof_object = n.get_old_dof_object();
592 if (old_dof_object &&
593 old_dof_object->n_vars(this->sys.number()) &&
594 old_dof_object->n_comp(this->sys.number(), var))
595 {
596 const dof_id_type first_old_id =
597 old_dof_object->dof_number(this->sys.number(), var, dim);
598 std::vector<dof_id_type> old_ids(n_mixed);
599 std::iota(old_ids.begin(), old_ids.end(), first_old_id);
600
601 for (auto d_i : index_range(derivs))
602 {
603 derivs[d_i].resize(1);
604 derivs[d_i].raw_at(0) = 1;
605 derivs[d_i].raw_index(0) = old_ids[d_i];
606 }
607 }
608 else
609 {
610 std::fill(derivs.begin(), derivs.end(), 0);
611 }
612 }
613
614
615 void eval_old_dofs (const Elem & elem,
616 unsigned int node_num,
617 unsigned int var_num,
618 std::vector<dof_id_type> & indices,
619 std::vector<DSNA> & values)
620 {
621 LOG_SCOPE ("eval_old_dofs(node)", "OldSolutionCoefs");
622
623 // We may be reusing a std::vector here, but the following
624 // dof_indices call appends without first clearing.
625 indices.clear();
626
627 this->sys.get_dof_map().dof_indices(elem, node_num, indices, var_num);
628
629 std::vector<dof_id_type> old_indices;
630
631 this->sys.get_dof_map().old_dof_indices(elem, node_num, old_indices, var_num);
632
633 libmesh_assert_equal_to (old_indices.size(), indices.size());
634
635 values.resize(old_indices.size());
636
637 for (auto i : index_range(values))
638 {
639 values[i].resize(1);
640 values[i].raw_at(0) = 1;
641 values[i].raw_index(0) = old_indices[i];
642 }
643 }
644
645
646 void eval_old_dofs (const Elem & elem,
647 const FEType & fe_type,
648 unsigned int sys_num,
649 unsigned int var_num,
650 std::vector<dof_id_type> & indices,
651 std::vector<DSNA> & values)
652 {
653 LOG_SCOPE ("eval_old_dofs(elem)", "OldSolutionCoefs");
654
655 // We're only to be asked for old dofs on elements that can copy
656 // them through DO_NOTHING or through refinement.
657 const Elem & old_elem =
658 (elem.refinement_flag() == Elem::JUST_REFINED) ?
659 *elem.parent() : elem;
660
661 // If there are any element-based DOF numbers, get them
662 const unsigned int nc =
663 FEInterface::n_dofs_per_elem(fe_type, &elem);
664
665 std::vector<dof_id_type> old_dof_indices(nc);
666 indices.resize(nc);
667
668 // We should never have fewer dofs than necessary on an
669 // element unless we're getting indices on a parent element,
670 // and we should never need those indices
671 if (nc != 0)
672 {
673 const DofObject & old_dof_object = old_elem.get_old_dof_object_ref();
674
675 const auto [vg, vig] =
676 elem.var_to_vg_and_offset(sys_num,var_num);
677
678 const unsigned int n_comp = elem.n_comp_group(sys_num,vg);
679 libmesh_assert_greater(elem.n_systems(), sys_num);
680 libmesh_assert_greater_equal(n_comp, nc);
681
682 for (unsigned int i=0; i<nc; i++)
683 {
684 const dof_id_type d_old =
685 old_dof_object.dof_number(sys_num, vg, vig, i, n_comp);
686 const dof_id_type d_new =
687 elem.dof_number(sys_num, vg, vig, i, n_comp);
688 libmesh_assert_not_equal_to (d_old, DofObject::invalid_id);
689 libmesh_assert_not_equal_to (d_new, DofObject::invalid_id);
690
691 old_dof_indices[i] = d_old;
692 indices[i] = d_new;
693 }
694 }
695
696 values.resize(old_dof_indices.size());
697
698 for (auto i : index_range(values))
699 {
700 values[i].resize(1);
701 values[i].raw_at(0) = 1;
702 values[i].raw_index(0) = old_dof_indices[i];
703 }
704 }
705};
706
707
708
709template<>
710inline
711DynamicSparseNumberArray<Real, dof_id_type>
713eval_at_point(const FEMContext & c,
714 unsigned int i,
715 const Point & p,
716 Real /* time */,
717 bool skip_context_check)
718{
719 LOG_SCOPE ("eval_at_point()", "OldSolutionCoefs");
720
721 if (!skip_context_check)
722 if (!this->check_old_context(c, p))
723 return 0;
724
725 // Get finite element object
726 FEGenericBase<Real> * fe = nullptr;
727 this->old_context.get_element_fe<Real>
728 (i, fe, this->old_context.get_elem_dim());
729
730 // Build a FE for calculating phi(p)
731 FEGenericBase<Real> * fe_new =
732 this->old_context.build_new_fe(fe, p);
733
734 // Get the values and global indices of the shape functions
735 const std::vector<std::vector<Real> > & phi = fe_new->get_phi();
736 const std::vector<dof_id_type> & dof_indices =
737 this->old_context.get_dof_indices(i);
738
739 const std::size_t n_dofs = phi.size();
740 libmesh_assert_equal_to(n_dofs, dof_indices.size());
741
742 DynamicSparseNumberArray<Real, dof_id_type> returnval;
743 returnval.resize(n_dofs);
744
745 for (auto j : index_range(phi))
746 {
747 returnval.raw_at(j) = phi[j][0];
748 returnval.raw_index(j) = dof_indices[j];
749 }
750
751 return returnval;
752}
753
754
755
756template<>
757inline
760eval_at_point(const FEMContext & c,
761 unsigned int i,
762 const Point & p,
763 Real /* time */,
764 bool skip_context_check)
765{
766 LOG_SCOPE ("eval_at_point()", "OldSolutionCoefs");
767
768 if (!skip_context_check)
769 if (!this->check_old_context(c, p))
770 return 0;
771
772 // Get finite element object
773 FEGenericBase<Real> * fe = nullptr;
774 this->old_context.get_element_fe<Real>
775 (i, fe, this->old_context.get_elem_dim());
776
777 // Build a FE for calculating phi(p)
778 FEGenericBase<Real> * fe_new =
779 this->old_context.build_new_fe(fe, p);
780
781 // Get the values and global indices of the shape functions
782 const std::vector<std::vector<RealGradient> > & dphi = fe_new->get_dphi();
783 const std::vector<dof_id_type> & dof_indices =
784 this->old_context.get_dof_indices(i);
785
786 const std::size_t n_dofs = dphi.size();
787 libmesh_assert_equal_to(n_dofs, dof_indices.size());
788
790
791 for (unsigned int d = 0; d != LIBMESH_DIM; ++d)
792 returnval(d).resize(n_dofs);
793
794 for (auto j : index_range(dphi))
795 for (int d = 0; d != LIBMESH_DIM; ++d)
796 {
797 returnval(d).raw_at(j) = dphi[j][0](d);
798 returnval(d).raw_index(j) = dof_indices[j];
799 }
800
801 return returnval;
802}
803
804
805template<>
806inline
807DynamicSparseNumberArray<Real, dof_id_type>
809eval_at_node(const FEMContext & c,
810 unsigned int i,
811 unsigned int /* elem_dim */,
812 const Node & n,
813 bool extra_hanging_dofs,
814 Real /* time */)
815{
816 LOG_SCOPE ("Real eval_at_node()", "OldSolutionCoefs");
817
818 // Optimize for the common case, where this node was part of the
819 // old solution.
820 //
821 // Be sure to handle cases where the variable wasn't defined on
822 // this node (due to changing subdomain support) or where the
823 // variable has no components on this node (due to Elem order
824 // exceeding FE order) or where the old_dof_object dofs might
825 // correspond to non-vertex dofs (due to extra_hanging_dofs and
826 // refinement)
827
829
830 const DofObject * old_dof_object = n.get_old_dof_object();
831 if (old_dof_object &&
832 (!extra_hanging_dofs ||
833 flag == Elem::JUST_COARSENED ||
834 flag == Elem::DO_NOTHING) &&
835 old_dof_object->n_vars(sys.number()) &&
836 old_dof_object->n_comp(sys.number(), i))
837 {
838 DynamicSparseNumberArray<Real, dof_id_type> returnval;
839 const dof_id_type old_id =
840 old_dof_object->dof_number(sys.number(), i, 0);
841 returnval.resize(1);
842 returnval.raw_at(0) = 1;
843 returnval.raw_index(0) = old_id;
844 return returnval;
845 }
846
847 return this->eval_at_point(c, i, n, 0, false);
848}
849
850
851
852template<>
853inline
856eval_at_node(const FEMContext & c,
857 unsigned int i,
858 unsigned int elem_dim,
859 const Node & n,
860 bool extra_hanging_dofs,
861 Real /* time */)
862{
863 LOG_SCOPE ("RealGradient eval_at_node()", "OldSolutionCoefs");
864
865 // Optimize for the common case, where this node was part of the
866 // old solution.
867 //
868 // Be sure to handle cases where the variable wasn't defined on
869 // this node (due to changing subdomain support) or where the
870 // variable has no components on this node (due to Elem order
871 // exceeding FE order) or where the old_dof_object dofs might
872 // correspond to non-vertex dofs (due to extra_hanging_dofs and
873 // refinement)
874
876
877 const DofObject * old_dof_object = n.get_old_dof_object();
878 if (old_dof_object &&
879 (!extra_hanging_dofs ||
880 flag == Elem::JUST_COARSENED ||
881 flag == Elem::DO_NOTHING) &&
882 old_dof_object->n_vars(sys.number()) &&
883 old_dof_object->n_comp(sys.number(), i))
884 {
886 for (unsigned int d = 0; d != elem_dim; ++d)
887 {
888 const dof_id_type old_id =
889 old_dof_object->dof_number(sys.number(), i, d+1);
890 g(d).resize(1);
891 g(d).raw_at(0) = 1;
892 g(d).raw_index(0) = old_id;
893 }
894 return g;
895 }
896
897 return this->eval_at_point(c, i, n, 0, false);
898}
899
900
901
910template <typename ValIn, typename ValOut>
912{
913public:
914 typedef DynamicSparseNumberArray<ValIn, dof_id_type> InsertInput;
915private:
917
918public:
920 target_matrix(target_mat) {}
921
923 const DynamicSparseNumberArray<ValIn, dof_id_type> & val)
924 {
925 // Lock the target matrix since it is shared among threads.
926 {
927 Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
928
929 const std::size_t dnsa_size = val.size();
930 for (unsigned int j = 0; j != dnsa_size; ++j)
931 {
932 const dof_id_type dof_j = val.raw_index(j);
933 const ValIn dof_val = val.raw_at(j);
934 target_matrix.set(id, dof_j, dof_val);
935 }
936 }
937 }
938
939
940 void insert(const std::vector<dof_id_type> & dof_indices,
941 const std::vector<DynamicSparseNumberArray<ValIn, dof_id_type> > & Ue)
942 {
944 begin_dof = target_matrix.row_start(),
945 end_dof = target_matrix.row_stop();
946
947 unsigned int size = Ue.size();
948
949 libmesh_assert_equal_to(size, dof_indices.size());
950
951 // Lock the target matrix since it is shared among threads.
952 {
953 Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
954
955 for (unsigned int i = 0; i != size; ++i)
956 {
957 const dof_id_type dof_i = dof_indices[i];
958 if ((dof_i >= begin_dof) && (dof_i < end_dof))
959 {
960 const DynamicSparseNumberArray<ValIn,dof_id_type> & dnsa = Ue[i];
961 const std::size_t dnsa_size = dnsa.size();
962 for (unsigned int j = 0; j != dnsa_size; ++j)
963 {
964 const dof_id_type dof_j = dnsa.raw_index(j);
965 const ValIn dof_val = dnsa.raw_at(j);
966 target_matrix.set(dof_i, dof_j, dof_val);
967 }
968 }
969 }
970 }
971 }
972};
973
974
975
980void System::projection_matrix (SparseMatrix<Number> & proj_mat) const
981{
982 LOG_SCOPE ("projection_matrix()", "System");
983
984 const unsigned int n_variables = this->n_vars();
985
986 if (n_variables)
987 {
988 std::vector<unsigned int> vars(n_variables);
989 std::iota(vars.begin(), vars.end(), 0);
990
991 // Use a typedef to make the calling sequence for parallel_for() a bit more readable
992 typedef OldSolutionCoefs<Real, &FEMContext::point_value> OldSolutionValueCoefs;
994
995 typedef
996 GenericProjector<OldSolutionValueCoefs,
997 OldSolutionGradientCoefs,
998 DynamicSparseNumberArray<Real,dof_id_type>,
999 MatrixFillAction<Real, Number> > ProjMatFiller;
1000
1001 OldSolutionValueCoefs f(*this, &vars);
1002 OldSolutionGradientCoefs g(*this, &vars);
1003 MatrixFillAction<Real, Number> setter(proj_mat);
1004
1005 ProjMatFiller mat_filler(*this, f, &g, setter, vars);
1006 mat_filler.project(this->get_mesh().active_local_element_stored_range());
1007
1008 // Set the SCALAR dof transfer entries too.
1009 // Note: We assume that all SCALAR dofs are on the
1010 // processor with highest ID
1011 if (this->processor_id() == (this->n_processors()-1))
1012 {
1013 const DofMap & dof_map = this->get_dof_map();
1014 for (auto var : make_range(this->n_vars()))
1015 if (this->variable(var).type().family == SCALAR)
1016 {
1017 // We can just map SCALAR dofs directly across
1018 std::vector<dof_id_type> new_SCALAR_indices, old_SCALAR_indices;
1019 dof_map.SCALAR_dof_indices (new_SCALAR_indices, var, false);
1020 dof_map.SCALAR_dof_indices (old_SCALAR_indices, var, true);
1021 const unsigned int new_n_dofs =
1022 cast_int<unsigned int>(new_SCALAR_indices.size());
1023
1024 for (unsigned int i=0; i<new_n_dofs; i++)
1025 {
1026 proj_mat.set( new_SCALAR_indices[i],
1027 old_SCALAR_indices[i], 1);
1028 }
1029 }
1030 }
1031 }
1032}
1033#endif // LIBMESH_HAVE_METAPHYSICL
1034#endif // LIBMESH_ENABLE_AMR
1035
1036
1037
1042void System::project_solution (ValueFunctionPointer fptr,
1043 GradientFunctionPointer gptr,
1044 const Parameters & function_parameters,
1045 std::optional<ConstElemRange> active_local_range,
1046 std::optional<std::vector<unsigned int>> variable_numbers) const
1047{
1048 WrappedFunction<Number> f(*this, fptr, &function_parameters);
1049 WrappedFunction<Gradient> g(*this, gptr, &function_parameters);
1050 this->project_solution(&f, &g, active_local_range, variable_numbers);
1051}
1052
1053
1058void System::project_solution (FunctionBase<Number> * f,
1060 std::optional<ConstElemRange> active_local_range,
1061 std::optional<std::vector<unsigned int>> variable_numbers) const
1062{
1063 this->project_vector(*solution, f, g, /*is_adjoint=*/-1, active_local_range, variable_numbers);
1064
1065 solution->localize(*current_local_solution, _dof_map->get_send_list());
1066}
1067
1068
1073void System::project_solution (FEMFunctionBase<Number> * f,
1075 std::optional<ConstElemRange> active_local_range,
1076 std::optional<std::vector<unsigned int>> variable_numbers) const
1077{
1078 this->project_vector(*solution, f, g, /*is_adjoint=*/-1, active_local_range, variable_numbers);
1079
1080 solution->localize(*current_local_solution, _dof_map->get_send_list());
1081}
1082
1083
1088void System::project_vector (ValueFunctionPointer fptr,
1089 GradientFunctionPointer gptr,
1090 const Parameters & function_parameters,
1091 NumericVector<Number> & new_vector,
1092 int is_adjoint,
1093 std::optional<ConstElemRange> active_local_range,
1094 std::optional<std::vector<unsigned int>> variable_numbers) const
1095{
1096 WrappedFunction<Number> f(*this, fptr, &function_parameters);
1097 WrappedFunction<Gradient> g(*this, gptr, &function_parameters);
1098 this->project_vector(new_vector, &f, &g, is_adjoint, active_local_range, variable_numbers);
1099}
1100
1105void System::project_vector (NumericVector<Number> & new_vector,
1108 int is_adjoint,
1109 std::optional<ConstElemRange> active_local_range,
1110 std::optional<std::vector<unsigned int>> variable_numbers) const
1111{
1112 LOG_SCOPE ("project_vector(FunctionBase)", "System");
1113
1114 libmesh_assert(f);
1115
1116 WrappedFunctor<Number> f_fem(*f);
1117
1118 if (g)
1119 {
1120 WrappedFunctor<Gradient> g_fem(*g);
1121
1122 this->project_vector(new_vector, &f_fem, &g_fem, is_adjoint, active_local_range, variable_numbers);
1123 }
1124 else
1125 this->project_vector(new_vector, &f_fem, nullptr, is_adjoint, active_local_range, variable_numbers);
1126}
1127
1128
1133void System::project_vector (NumericVector<Number> & new_vector,
1136 int is_adjoint,
1137 std::optional<ConstElemRange> active_local_range,
1138 std::optional<std::vector<unsigned int>> variable_numbers) const
1139{
1140 LOG_SCOPE ("project_fem_vector()", "System");
1141
1142 libmesh_assert (f);
1143
1144 const ConstElemRange & range =
1145 active_local_range ?
1146 active_local_range.value() :
1147 this->get_mesh().active_local_element_stored_range();
1148
1149 VectorSetAction<Number> setter(new_vector);
1150
1151 const unsigned int n_variables = this->n_vars();
1152
1153 std::vector<unsigned int> vars;
1154 if (variable_numbers)
1155 {
1156 vars = *variable_numbers;
1157 for (auto v : vars)
1158 if (v >= n_variables)
1159 libmesh_error_msg("ERROR: variable number " << v <<
1160 " out of range for system with " <<
1161 n_variables << " variables.");
1162 }
1163 else
1164 {
1165 vars.resize(n_variables);
1166 std::iota(vars.begin(), vars.end(), 0);
1167 }
1168
1169
1170 // Use a typedef to make the calling sequence for parallel_for() a bit more readable
1171 typedef
1173 Number, VectorSetAction<Number>> FEMProjector;
1174
1176
1177 if (g)
1178 {
1180
1181 FEMProjector projector(*this, fw, &gw, setter, vars);
1182 projector.project(range);
1183 }
1184 else
1185 {
1186 FEMProjector projector(*this, fw, nullptr, setter, vars);
1187 projector.project(range);
1188 }
1189
1190 // Also, load values into the SCALAR dofs
1191 // Note: We assume that all SCALAR dofs are on the
1192 // processor with highest ID
1193 if (this->processor_id() == (this->n_processors()-1))
1194 {
1195 // FIXME: Do we want to first check for SCALAR vars before building this? [PB]
1196 FEMContext context( *this );
1197
1198 const DofMap & dof_map = this->get_dof_map();
1199 for (auto var : vars)
1200 if (this->variable(var).type().family == SCALAR)
1201 {
1202 // FIXME: We reinit with an arbitrary element in case the user
1203 // doesn't override FEMFunctionBase::component. Is there
1204 // any use case we're missing? [PB]
1205 context.pre_fe_reinit(*this, *(this->get_mesh().active_local_elements_begin()));
1206
1207 std::vector<dof_id_type> SCALAR_indices;
1208 dof_map.SCALAR_dof_indices (SCALAR_indices, var);
1209 const unsigned int n_SCALAR_dofs =
1210 cast_int<unsigned int>(SCALAR_indices.size());
1211
1212 for (unsigned int i=0; i<n_SCALAR_dofs; i++)
1213 {
1214 const dof_id_type global_index = SCALAR_indices[i];
1215 const unsigned int component_index =
1216 this->variable_scalar_number(var,i);
1217
1218 new_vector.set(global_index, f->component(context, component_index, Point(), this->time));
1219 }
1220 }
1221 }
1222
1223 new_vector.close();
1224
1225 // Look for spline bases, in which case we need to backtrack
1226 // to calculate the spline DoF values.
1227 std::vector<const Variable *> rational_vars;
1228 for (auto varnum : vars)
1229 {
1230 const Variable & var = this->get_dof_map().variable(varnum);
1231 if (var.type().family == RATIONAL_BERNSTEIN)
1232 rational_vars.push_back(&var);
1233 }
1234
1235 // Okay, but are we really using any *spline* bases, or just
1236 // unconstrained rational bases?
1237 bool using_spline_bases = false;
1238 if (!rational_vars.empty())
1239 {
1240 // Look for a spline node: a NodeElem with a rational variable
1241 // on it.
1242 for (auto & elem : range)
1243 if (elem->type() == NODEELEM)
1244 for (auto rational_var : rational_vars)
1245 if (rational_var->active_on_subdomain(elem->subdomain_id()))
1246 {
1247 using_spline_bases = true;
1248 goto checked_on_splines;
1249 }
1250 }
1251
1252checked_on_splines:
1253
1254 // Not every processor may have a NodeElem, especially while
1255 // we're not partitioning them efficiently yet.
1256 this->comm().max(using_spline_bases);
1257
1258 if (using_spline_bases)
1259 this->solve_for_unconstrained_dofs(new_vector, is_adjoint);
1260
1261#ifdef LIBMESH_ENABLE_CONSTRAINTS
1262 if (is_adjoint == -1)
1263 this->get_dof_map().enforce_constraints_exactly(*this, &new_vector);
1264 else if (is_adjoint >= 0)
1265 this->get_dof_map().enforce_adjoint_constraints_exactly(new_vector,
1266 is_adjoint);
1267#else
1268 libmesh_ignore(is_adjoint);
1269#endif
1270}
1271
1272
1278void System::boundary_project_solution (const std::set<boundary_id_type> & b,
1279 const std::vector<unsigned int> & variables,
1280 ValueFunctionPointer fptr,
1281 GradientFunctionPointer gptr,
1282 const Parameters & function_parameters,
1283 std::optional<ConstElemRange> active_local_range)
1284
1285{
1286 WrappedFunction<Number> f(*this, fptr, &function_parameters);
1287 WrappedFunction<Gradient> g(*this, gptr, &function_parameters);
1288 this->boundary_project_solution(b, variables, &f, &g, active_local_range);
1289}
1290
1291
1297void System::boundary_project_solution (const std::set<boundary_id_type> & b,
1298 const std::vector<unsigned int> & variables,
1301 std::optional<ConstElemRange> active_local_range)
1302{
1303 this->boundary_project_vector(b, variables, *solution, f, g, -1 /*is_adjoint*/, active_local_range);
1304
1305 solution->localize(*current_local_solution);
1306}
1307
1308
1309
1310
1311
1316void System::boundary_project_vector (const std::set<boundary_id_type> & b,
1317 const std::vector<unsigned int> & variables,
1318 ValueFunctionPointer fptr,
1319 GradientFunctionPointer gptr,
1320 const Parameters & function_parameters,
1321 NumericVector<Number> & new_vector,
1322 int is_adjoint,
1323 std::optional<ConstElemRange> active_local_range) const
1324{
1325 WrappedFunction<Number> f(*this, fptr, &function_parameters);
1326 WrappedFunction<Gradient> g(*this, gptr, &function_parameters);
1327 this->boundary_project_vector(b, variables, new_vector, &f, &g,
1328 is_adjoint, active_local_range);
1329}
1330
1335void System::boundary_project_vector (const std::set<boundary_id_type> & b,
1336 const std::vector<unsigned int> & variables,
1337 NumericVector<Number> & new_vector,
1340 int is_adjoint,
1341 std::optional<ConstElemRange> active_local_range) const
1342{
1343 LOG_SCOPE ("boundary_project_vector()", "System");
1344
1345 const ConstElemRange & range =
1346 active_local_range ?
1347 active_local_range.value() :
1348 this->get_mesh().active_local_element_stored_range();
1349
1350 Threads::parallel_for
1351 (range,
1352 BoundaryProjectSolution(b, variables, *this, f, g,
1353 this->get_equation_systems().parameters,
1354 new_vector)
1355 );
1356
1357 // We don't do SCALAR dofs when just projecting the boundary, so
1358 // we're done here.
1359
1360 new_vector.close();
1361
1362#ifdef LIBMESH_ENABLE_CONSTRAINTS
1363 if (is_adjoint == -1)
1364 this->get_dof_map().enforce_constraints_exactly(*this, &new_vector);
1365 else if (is_adjoint >= 0)
1366 this->get_dof_map().enforce_adjoint_constraints_exactly(new_vector,
1367 is_adjoint);
1368#else
1369 libmesh_ignore(is_adjoint);
1370#endif
1371}
1372
1373
1374
1375#ifdef LIBMESH_ENABLE_AMR
1376void BuildProjectionList::unique()
1377{
1378 // Sort the send list. After this duplicated
1379 // elements will be adjacent in the vector
1380 std::sort(this->send_list.begin(),
1381 this->send_list.end());
1382
1383 // Now use std::unique to remove duplicate entries
1384 std::vector<dof_id_type>::iterator new_end =
1385 std::unique (this->send_list.begin(),
1386 this->send_list.end());
1387
1388 // Remove the end of the send_list. Use the "swap trick"
1389 // from Effective STL
1390 std::vector<dof_id_type>
1391 (this->send_list.begin(), new_end).swap (this->send_list);
1392}
1393
1394
1395
1396void BuildProjectionList::operator()(const ConstElemRange & range)
1397{
1398 // The DofMap for this system
1399 const DofMap & dof_map = system.get_dof_map();
1400
1401 const dof_id_type first_old_dof = dof_map.first_old_dof();
1402 const dof_id_type end_old_dof = dof_map.end_old_dof();
1403
1404 // We can handle all the variables at once.
1405 // The old global DOF indices
1406 std::vector<dof_id_type> di;
1407
1408 // Iterate over the elements in the range
1409 for (const auto & elem : range)
1410 {
1411 // If this element doesn't have an old_dof_object with dofs for the
1412 // current system, then it must be newly added, so the user
1413 // is responsible for setting the new dofs.
1414
1415 // ... but we need a better way to test for that; the code
1416 // below breaks on any FE type for which the elem stores no
1417 // dofs.
1418 // if (!elem->get_old_dof_object() || !elem->get_old_dof_object()->has_dofs(system.number()))
1419 // continue;
1420
1421 // Examining refinement flags instead should distinguish
1422 // between refinement-added and user-added elements lacking
1423 // old_dof_object
1424 const DofObject * old_dof_object = elem->get_old_dof_object();
1425 if (!old_dof_object &&
1426 elem->refinement_flag() != Elem::JUST_REFINED &&
1427 elem->refinement_flag() != Elem::JUST_COARSENED)
1428 continue;
1429
1430 const Elem * parent = elem->parent();
1431
1432 if (elem->refinement_flag() == Elem::JUST_REFINED)
1433 {
1434 libmesh_assert(parent);
1435
1436 // We used to hack_p_level here, but that wasn't thread-safe
1437 // so now we take p refinement flags into account in
1438 // old_dof_indices
1439
1440 dof_map.old_dof_indices (parent, di);
1441
1442 for (auto & node : elem->node_ref_range())
1443 {
1444 const DofObject * old_dofs = node.get_old_dof_object();
1445
1446 if (old_dofs)
1447 {
1448 const unsigned int sysnum = system.number();
1449 const unsigned int nvg = old_dofs->n_var_groups(sysnum);
1450
1451 for (unsigned int vg=0; vg != nvg; ++vg)
1452 {
1453 const unsigned int nvig =
1454 old_dofs->n_vars(sysnum, vg);
1455 for (unsigned int vig=0; vig != nvig; ++vig)
1456 {
1457 const unsigned int n_comp =
1458 old_dofs->n_comp_group(sysnum, vg);
1459 for (unsigned int c=0; c != n_comp; ++c)
1460 {
1461 const dof_id_type old_id =
1462 old_dofs->dof_number(sysnum, vg, vig,
1463 c, n_comp);
1464
1465 // We should either have no old id
1466 // (e.g. on a newly expanded subdomain)
1467 // or an id from the old system.
1468 libmesh_assert(old_id < dof_map.n_old_dofs() ||
1469 old_id == DofObject::invalid_id);
1470 di.push_back(old_id);
1471 }
1472 }
1473 }
1474 }
1475 }
1476
1477 std::sort(di.begin(), di.end());
1478 std::vector<dof_id_type>::iterator new_end =
1479 std::unique(di.begin(), di.end());
1480 std::vector<dof_id_type>(di.begin(), new_end).swap(di);
1481 }
1482 else if (elem->refinement_flag() == Elem::JUST_COARSENED)
1483 {
1484 std::vector<dof_id_type> di_child;
1485 di.clear();
1486 for (auto & child : elem->child_ref_range())
1487 {
1488 dof_map.old_dof_indices (&child, di_child);
1489 di.insert(di.end(), di_child.begin(), di_child.end());
1490 }
1491 }
1492 else
1493 dof_map.old_dof_indices (elem, di);
1494
1495 for (auto di_i : di)
1496 {
1497 // If we've just expanded a subdomain for a
1498 // subdomain-restricted variable, then we may have an
1499 // old_dof_object that doesn't have an old DoF for every
1500 // local index.
1501 if (di_i == DofObject::invalid_id)
1502 continue;
1503
1504 libmesh_assert_less(di_i, dof_map.n_old_dofs());
1505 if (di_i < first_old_dof || di_i >= end_old_dof)
1506 this->send_list.push_back(di_i);
1507 }
1508 } // end elem loop
1509}
1510
1511
1512
1513void BuildProjectionList::join(const BuildProjectionList & other)
1514{
1515 // Joining simply requires I add the dof indices from the other object
1516 this->send_list.insert(this->send_list.end(),
1517 other.send_list.begin(),
1518 other.send_list.end());
1519}
1520#endif // LIBMESH_ENABLE_AMR
1521
1522
1523
1524void BoundaryProjectSolution::operator()(const ConstElemRange & range) const
1525{
1526 // We need data to project
1527 libmesh_assert(f.get());
1528
1536 // The dimensionality of the current mesh
1537 const unsigned int dim = system.get_mesh().mesh_dimension();
1538
1539 // The DofMap for this system
1540 const DofMap & dof_map = system.get_dof_map();
1541
1542 // Boundary info for the current mesh
1543 const BoundaryInfo & boundary_info =
1544 system.get_mesh().get_boundary_info();
1545
1546 // The element matrix and RHS for projections.
1547 // Note that Ke is always real-valued, whereas
1548 // Fe may be complex valued if complex number
1549 // support is enabled
1552 // The new element coefficients
1554
1555
1556 // Loop over all the variables we've been requested to project
1557 for (auto v : make_range(variables.size()))
1558 {
1559 const unsigned int var = variables[v];
1560
1561 const Variable & variable = dof_map.variable(var);
1562
1563 const FEType & fe_type = variable.type();
1564
1565 if (fe_type.family == SCALAR)
1566 continue;
1567
1568 const unsigned int var_component =
1569 system.variable_scalar_number(var, 0);
1570
1571 // Get FE objects of the appropriate type
1572 std::unique_ptr<FEBase> fe (FEBase::build(dim, fe_type));
1573
1574 // Prepare variables for projection
1575 std::unique_ptr<QBase> qedgerule (fe_type.default_quadrature_rule(1));
1576 std::unique_ptr<QBase> qsiderule (fe_type.default_quadrature_rule(dim-1));
1577
1578 // The values of the shape functions at the quadrature
1579 // points
1580 const std::vector<std::vector<Real>> & phi = fe->get_phi();
1581
1582 // The gradients of the shape functions at the quadrature
1583 // points on the child element.
1584 const std::vector<std::vector<RealGradient>> * dphi = nullptr;
1585
1586 const FEContinuity cont = fe->get_continuity();
1587
1588 if (cont == C_ONE)
1589 {
1590 // We'll need gradient data for a C1 projection
1591 libmesh_assert(g.get());
1592
1593 const std::vector<std::vector<RealGradient>> &
1594 ref_dphi = fe->get_dphi();
1595 dphi = &ref_dphi;
1596 }
1597
1598 // The Jacobian * quadrature weight at the quadrature points
1599 const std::vector<Real> & JxW =
1600 fe->get_JxW();
1601
1602 // The XYZ locations of the quadrature points
1603 const std::vector<Point> & xyz_values =
1604 fe->get_xyz();
1605
1606 // The global DOF indices
1607 std::vector<dof_id_type> dof_indices;
1608 // Side/edge DOF indices
1609 std::vector<unsigned int> side_dofs;
1610
1611 // Container to catch IDs passed back from BoundaryInfo.
1612 std::vector<boundary_id_type> bc_ids;
1613
1614 // Iterate over all the elements in the range
1615 for (const auto & elem : range)
1616 {
1617 // Per-subdomain variables don't need to be projected on
1618 // elements where they're not active
1619 if (!variable.active_on_subdomain(elem->subdomain_id()))
1620 continue;
1621
1622 const unsigned short n_nodes = elem->n_nodes();
1623 const unsigned short n_edges = elem->n_edges();
1624 const unsigned short n_sides = elem->n_sides();
1625
1626 // Find out which nodes, edges and sides are on a requested
1627 // boundary:
1628 std::vector<bool> is_boundary_node(n_nodes, false),
1629 is_boundary_edge(n_edges, false),
1630 is_boundary_side(n_sides, false);
1631
1632 // We also maintain a separate list of nodeset-based boundary nodes
1633 std::vector<bool> is_boundary_nodeset(n_nodes, false);
1634
1635 for (unsigned char s=0; s != n_sides; ++s)
1636 {
1637 // First see if this side has been requested
1638 boundary_info.boundary_ids (elem, s, bc_ids);
1639 bool do_this_side = false;
1640 for (const auto & bc_id : bc_ids)
1641 if (b.count(bc_id))
1642 {
1643 do_this_side = true;
1644 break;
1645 }
1646 if (!do_this_side)
1647 continue;
1648
1649 is_boundary_side[s] = true;
1650
1651 // Then see what nodes and what edges are on it
1652 for (unsigned int n=0; n != n_nodes; ++n)
1653 if (elem->is_node_on_side(n,s))
1654 is_boundary_node[n] = true;
1655 for (unsigned int e=0; e != n_edges; ++e)
1656 if (elem->is_edge_on_side(e,s))
1657 is_boundary_edge[e] = true;
1658 }
1659
1660 // We can also project on nodes, so we should also independently
1661 // check whether the nodes have been requested
1662 for (unsigned int n=0; n != n_nodes; ++n)
1663 {
1664 boundary_info.boundary_ids (elem->node_ptr(n), bc_ids);
1665
1666 for (const auto & bc_id : bc_ids)
1667 if (b.count(bc_id))
1668 {
1669 is_boundary_node[n] = true;
1670 is_boundary_nodeset[n] = true;
1671 }
1672 }
1673
1674 // We can also project on edges, so we should also independently
1675 // check whether the edges have been requested
1676 for (unsigned short e=0; e != n_edges; ++e)
1677 {
1678 boundary_info.edge_boundary_ids (elem, e, bc_ids);
1679
1680 for (const auto & bc_id : bc_ids)
1681 if (b.count(bc_id))
1682 is_boundary_edge[e] = true;
1683 }
1684
1685 // Update the DOF indices for this element based on
1686 // the current mesh
1687 dof_map.dof_indices (elem, dof_indices, var);
1688
1689 // The number of DOFs on the element
1690 const unsigned int n_dofs =
1691 cast_int<unsigned int>(dof_indices.size());
1692
1693 // Fixed vs. free DoFs on edge/face projections
1694 std::vector<char> dof_is_fixed(n_dofs, false); // bools
1695 std::vector<int> free_dof(n_dofs, 0);
1696
1697 // Zero the interpolated values
1698 Ue.resize (n_dofs); Ue.zero();
1699
1700 // In general, we need a series of
1701 // projections to ensure a unique and continuous
1702 // solution. We start by interpolating boundary nodes, then
1703 // hold those fixed and project boundary edges, then hold
1704 // those fixed and project boundary faces,
1705
1706 // Interpolate node values first
1707 unsigned int current_dof = 0;
1708 for (unsigned short n = 0; n != n_nodes; ++n)
1709 {
1710 // FIXME: this should go through the DofMap,
1711 // not duplicate dof_indices code badly!
1712
1713 // This call takes into account elem->p_level() internally.
1714 const unsigned int nc =
1715 FEInterface::n_dofs_at_node (fe_type, elem, n);
1716
1717 if ((!elem->is_vertex(n) || !is_boundary_node[n]) &&
1718 !is_boundary_nodeset[n])
1719 {
1720 current_dof += nc;
1721 continue;
1722 }
1723 if (cont == DISCONTINUOUS)
1724 {
1725 libmesh_assert_equal_to (nc, 0);
1726 }
1727 // Assume that C_ZERO elements have a single nodal
1728 // value shape function
1729 else if (cont == C_ZERO)
1730 {
1731 libmesh_assert_equal_to (nc, 1);
1732 Ue(current_dof) = f->component(var_component,
1733 elem->point(n),
1734 system.time);
1735 dof_is_fixed[current_dof] = true;
1736 current_dof++;
1737 }
1738 // The hermite element vertex shape functions are weird
1739 else if (fe_type.family == HERMITE)
1740 {
1741 Ue(current_dof) = f->component(var_component,
1742 elem->point(n),
1743 system.time);
1744 dof_is_fixed[current_dof] = true;
1745 current_dof++;
1746 Gradient grad = g->component(var_component,
1747 elem->point(n),
1748 system.time);
1749 // x derivative
1750 Ue(current_dof) = grad(0);
1751 dof_is_fixed[current_dof] = true;
1752 current_dof++;
1753#if LIBMESH_DIM > 1
1754 if (dim > 1)
1755 {
1756 // We'll finite difference mixed derivatives
1757 Point nxminus = elem->point(n),
1758 nxplus = elem->point(n);
1759 nxminus(0) -= TOLERANCE;
1760 nxplus(0) += TOLERANCE;
1761 Gradient gxminus = g->component(var_component,
1762 nxminus,
1763 system.time);
1764 Gradient gxplus = g->component(var_component,
1765 nxplus,
1766 system.time);
1767 // y derivative
1768 Ue(current_dof) = grad(1);
1769 dof_is_fixed[current_dof] = true;
1770 current_dof++;
1771 // xy derivative
1772 Ue(current_dof) = (gxplus(1) - gxminus(1))
1773 / 2. / TOLERANCE;
1774 dof_is_fixed[current_dof] = true;
1775 current_dof++;
1776
1777#if LIBMESH_DIM > 2
1778 if (dim > 2)
1779 {
1780 // z derivative
1781 Ue(current_dof) = grad(2);
1782 dof_is_fixed[current_dof] = true;
1783 current_dof++;
1784 // xz derivative
1785 Ue(current_dof) = (gxplus(2) - gxminus(2))
1786 / 2. / TOLERANCE;
1787 dof_is_fixed[current_dof] = true;
1788 current_dof++;
1789 // We need new points for yz
1790 Point nyminus = elem->point(n),
1791 nyplus = elem->point(n);
1792 nyminus(1) -= TOLERANCE;
1793 nyplus(1) += TOLERANCE;
1794 Gradient gyminus = g->component(var_component,
1795 nyminus,
1796 system.time);
1797 Gradient gyplus = g->component(var_component,
1798 nyplus,
1799 system.time);
1800 // xz derivative
1801 Ue(current_dof) = (gyplus(2) - gyminus(2))
1802 / 2. / TOLERANCE;
1803 dof_is_fixed[current_dof] = true;
1804 current_dof++;
1805 // Getting a 2nd order xyz is more tedious
1806 Point nxmym = elem->point(n),
1807 nxmyp = elem->point(n),
1808 nxpym = elem->point(n),
1809 nxpyp = elem->point(n);
1810 nxmym(0) -= TOLERANCE;
1811 nxmym(1) -= TOLERANCE;
1812 nxmyp(0) -= TOLERANCE;
1813 nxmyp(1) += TOLERANCE;
1814 nxpym(0) += TOLERANCE;
1815 nxpym(1) -= TOLERANCE;
1816 nxpyp(0) += TOLERANCE;
1817 nxpyp(1) += TOLERANCE;
1818 Gradient gxmym = g->component(var_component,
1819 nxmym,
1820 system.time);
1821 Gradient gxmyp = g->component(var_component,
1822 nxmyp,
1823 system.time);
1824 Gradient gxpym = g->component(var_component,
1825 nxpym,
1826 system.time);
1827 Gradient gxpyp = g->component(var_component,
1828 nxpyp,
1829 system.time);
1830 Number gxzplus = (gxpyp(2) - gxmyp(2))
1831 / 2. / TOLERANCE;
1832 Number gxzminus = (gxpym(2) - gxmym(2))
1833 / 2. / TOLERANCE;
1834 // xyz derivative
1835 Ue(current_dof) = (gxzplus - gxzminus)
1836 / 2. / TOLERANCE;
1837 dof_is_fixed[current_dof] = true;
1838 current_dof++;
1839 }
1840#endif // LIBMESH_DIM > 2
1841 }
1842#endif // LIBMESH_DIM > 1
1843 }
1844 // Assume that other C_ONE elements have a single nodal
1845 // value shape function and nodal gradient component
1846 // shape functions
1847 else if (cont == C_ONE)
1848 {
1849 libmesh_assert_equal_to (nc, 1 + dim);
1850 Ue(current_dof) = f->component(var_component,
1851 elem->point(n),
1852 system.time);
1853 dof_is_fixed[current_dof] = true;
1854 current_dof++;
1855 Gradient grad = g->component(var_component,
1856 elem->point(n),
1857 system.time);
1858 for (unsigned int i=0; i!= dim; ++i)
1859 {
1860 Ue(current_dof) = grad(i);
1861 dof_is_fixed[current_dof] = true;
1862 current_dof++;
1863 }
1864 }
1865 else
1866 libmesh_error_msg("Unknown continuity " << cont);
1867 }
1868
1869 // In 3D, project any edge values next
1870 if (dim > 2 && cont != DISCONTINUOUS)
1871 for (unsigned short e = 0; e != n_edges; ++e)
1872 {
1873 if (!is_boundary_edge[e])
1874 continue;
1875
1876 FEInterface::dofs_on_edge(elem, dim, fe_type, e,
1877 side_dofs);
1878
1879 const unsigned int n_side_dofs =
1880 cast_int<unsigned int>(side_dofs.size());
1881
1882 // Some edge dofs are on nodes and already
1883 // fixed, others are free to calculate
1884 unsigned int free_dofs = 0;
1885 for (auto i : make_range(n_side_dofs))
1886 if (!dof_is_fixed[side_dofs[i]])
1887 free_dof[free_dofs++] = i;
1888
1889 // There may be nothing to project
1890 if (!free_dofs)
1891 continue;
1892
1893 Ke.resize (free_dofs, free_dofs); Ke.zero();
1894 Fe.resize (free_dofs); Fe.zero();
1895 // The new edge coefficients
1896 DenseVector<Number> Uedge(free_dofs);
1897
1898 // Initialize FE data on the edge
1899 fe->attach_quadrature_rule (qedgerule.get());
1900 fe->edge_reinit (elem, e);
1901 const unsigned int n_qp = qedgerule->n_points();
1902
1903 // Loop over the quadrature points
1904 for (unsigned int qp=0; qp<n_qp; qp++)
1905 {
1906 // solution at the quadrature point
1907 Number fineval = f->component(var_component,
1908 xyz_values[qp],
1909 system.time);
1910 // solution grad at the quadrature point
1911 Gradient finegrad;
1912 if (cont == C_ONE)
1913 finegrad = g->component(var_component,
1914 xyz_values[qp],
1915 system.time);
1916
1917 // Form edge projection matrix
1918 for (unsigned int sidei=0, freei=0;
1919 sidei != n_side_dofs; ++sidei)
1920 {
1921 unsigned int i = side_dofs[sidei];
1922 // fixed DoFs aren't test functions
1923 if (dof_is_fixed[i])
1924 continue;
1925 for (unsigned int sidej=0, freej=0;
1926 sidej != n_side_dofs; ++sidej)
1927 {
1928 unsigned int j = side_dofs[sidej];
1929 if (dof_is_fixed[j])
1930 Fe(freei) -= phi[i][qp] * phi[j][qp] *
1931 JxW[qp] * Ue(j);
1932 else
1933 Ke(freei,freej) += phi[i][qp] *
1934 phi[j][qp] * JxW[qp];
1935 if (cont == C_ONE)
1936 {
1937 if (dof_is_fixed[j])
1938 Fe(freei) -= ((*dphi)[i][qp] *
1939 (*dphi)[j][qp]) *
1940 JxW[qp] * Ue(j);
1941 else
1942 Ke(freei,freej) += ((*dphi)[i][qp] *
1943 (*dphi)[j][qp])
1944 * JxW[qp];
1945 }
1946 if (!dof_is_fixed[j])
1947 freej++;
1948 }
1949 Fe(freei) += phi[i][qp] * fineval * JxW[qp];
1950 if (cont == C_ONE)
1951 Fe(freei) += (finegrad * (*dphi)[i][qp]) *
1952 JxW[qp];
1953 freei++;
1954 }
1955 }
1956
1957 Ke.cholesky_solve(Fe, Uedge);
1958
1959 // Transfer new edge solutions to element
1960 for (unsigned int i=0; i != free_dofs; ++i)
1961 {
1962 Number & ui = Ue(side_dofs[free_dof[i]]);
1963 libmesh_assert(std::abs(ui) < TOLERANCE ||
1964 std::abs(ui - Uedge(i)) < TOLERANCE);
1965 ui = Uedge(i);
1966 dof_is_fixed[side_dofs[free_dof[i]]] = true;
1967 }
1968 }
1969
1970 // Project any side values (edges in 2D, faces in 3D)
1971 if (dim > 1 && cont != DISCONTINUOUS)
1972 for (unsigned short s = 0; s != n_sides; ++s)
1973 {
1974 if (!is_boundary_side[s])
1975 continue;
1976
1977 FEInterface::dofs_on_side(elem, dim, fe_type, s,
1978 side_dofs);
1979
1980 // Some side dofs are on nodes/edges and already
1981 // fixed, others are free to calculate
1982 unsigned int free_dofs = 0;
1983 for (auto i : index_range(side_dofs))
1984 if (!dof_is_fixed[side_dofs[i]])
1985 free_dof[free_dofs++] = i;
1986
1987 // There may be nothing to project
1988 if (!free_dofs)
1989 continue;
1990
1991 Ke.resize (free_dofs, free_dofs); Ke.zero();
1992 Fe.resize (free_dofs); Fe.zero();
1993 // The new side coefficients
1994 DenseVector<Number> Uside(free_dofs);
1995
1996 // Initialize FE data on the side
1997 fe->attach_quadrature_rule (qsiderule.get());
1998 fe->reinit (elem, s);
1999 const unsigned int n_qp = qsiderule->n_points();
2000
2001 const unsigned int n_side_dofs =
2002 cast_int<unsigned int>(side_dofs.size());
2003
2004 // Loop over the quadrature points
2005 for (unsigned int qp=0; qp<n_qp; qp++)
2006 {
2007 // solution at the quadrature point
2008 Number fineval = f->component(var_component,
2009 xyz_values[qp],
2010 system.time);
2011 // solution grad at the quadrature point
2012 Gradient finegrad;
2013 if (cont == C_ONE)
2014 finegrad = g->component(var_component,
2015 xyz_values[qp],
2016 system.time);
2017
2018 // Form side projection matrix
2019 for (unsigned int sidei=0, freei=0;
2020 sidei != n_side_dofs; ++sidei)
2021 {
2022 unsigned int i = side_dofs[sidei];
2023 // fixed DoFs aren't test functions
2024 if (dof_is_fixed[i])
2025 continue;
2026 for (unsigned int sidej=0, freej=0;
2027 sidej != n_side_dofs; ++sidej)
2028 {
2029 unsigned int j = side_dofs[sidej];
2030 if (dof_is_fixed[j])
2031 Fe(freei) -= phi[i][qp] * phi[j][qp] *
2032 JxW[qp] * Ue(j);
2033 else
2034 Ke(freei,freej) += phi[i][qp] *
2035 phi[j][qp] * JxW[qp];
2036 if (cont == C_ONE)
2037 {
2038 if (dof_is_fixed[j])
2039 Fe(freei) -= ((*dphi)[i][qp] *
2040 (*dphi)[j][qp]) *
2041 JxW[qp] * Ue(j);
2042 else
2043 Ke(freei,freej) += ((*dphi)[i][qp] *
2044 (*dphi)[j][qp])
2045 * JxW[qp];
2046 }
2047 if (!dof_is_fixed[j])
2048 freej++;
2049 }
2050 Fe(freei) += (fineval * phi[i][qp]) * JxW[qp];
2051 if (cont == C_ONE)
2052 Fe(freei) += (finegrad * (*dphi)[i][qp]) *
2053 JxW[qp];
2054 freei++;
2055 }
2056 }
2057
2058 Ke.cholesky_solve(Fe, Uside);
2059
2060 // Transfer new side solutions to element
2061 for (unsigned int i=0; i != free_dofs; ++i)
2062 {
2063 Number & ui = Ue(side_dofs[free_dof[i]]);
2064 libmesh_assert(std::abs(ui) < TOLERANCE ||
2065 std::abs(ui - Uside(i)) < TOLERANCE);
2066 ui = Uside(i);
2067 dof_is_fixed[side_dofs[free_dof[i]]] = true;
2068 }
2069 }
2070
2071 const dof_id_type
2072 first = new_vector.first_local_index(),
2073 last = new_vector.last_local_index();
2074
2075 // Lock the new_vector since it is shared among threads.
2076 {
2077 Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
2078
2079 for (unsigned int i = 0; i < n_dofs; i++)
2080 if (dof_is_fixed[i] &&
2081 (dof_indices[i] >= first) &&
2082 (dof_indices[i] < last))
2083 {
2084 new_vector.set(dof_indices[i], Ue(i));
2085 }
2086 }
2087 } // end elem loop
2088 } // end variables loop
2089}
2090
2091
2092void System::solve_for_unconstrained_dofs(NumericVector<Number> & vec,
2093 int is_adjoint) const
2094{
2095 // We generally don't need this function if we don't have
2096 // non-assembly elements, but let's make sure it's robust to
2097 // pre-INVALID_MAP definitions of those, and let's make sure
2098 // *that's* robust to meshes with non-spline NodeElem too.
2099 //
2100 // Once everything's INVALID_MAP compliant we can remove this.
2101 if (!this->get_mesh().n_constraint_rows())
2102 {
2103 libmesh_warning("Called solve_for_unconstrained_dofs on a mesh with no constraint_rows?");
2104 return;
2105 }
2106
2107 const DofMap & dof_map = this->get_dof_map();
2108
2109 std::unique_ptr<SparseMatrix<Number>> mat =
2110 SparseMatrix<Number>::build(this->comm());
2111
2112 std::unique_ptr<SparsityPattern::Build> sp;
2113
2114 if (dof_map.computed_sparsity_already())
2115 dof_map.update_sparsity_pattern(*mat);
2116 else
2117 {
2118 mat->attach_dof_map(dof_map);
2119 sp = dof_map.build_sparsity(this->get_mesh());
2120 mat->attach_sparsity_pattern(*sp);
2121 }
2122
2123 mat->init();
2124
2125 libmesh_assert_equal_to(vec.size(), dof_map.n_dofs());
2126 libmesh_assert_equal_to(vec.local_size(), dof_map.n_local_dofs());
2127
2128 std::unique_ptr<NumericVector<Number>> rhs =
2129 NumericVector<Number>::build(this->comm());
2130
2131 rhs->init(dof_map.n_dofs(), dof_map.n_local_dofs(), false,
2132 PARALLEL);
2133
2134 // Here we start with the unconstrained (and indeterminate) linear
2135 // system, K*u = f, where K is the identity matrix for
2136 // DoFs which have been evaluated and 0 elsewhere, and f is the
2137 // current solution values for evaluated DoFs and 0 elsewhere.
2138 // We then apply the usual heterogeneous constraint matrix C and
2139 // offset h, where u = C*x + h,
2140 // to get C^T*K*C*x = C^T*f - C^T*K*h
2141 // - a constrained and no-longer-singular system that finds the
2142 // closest approximation for the unconstrained degrees of freedom.
2143 //
2144 // Here, though "closest" is in an algebraic sense; we're
2145 // effectively using a pseudoinverse that optimizes in a
2146 // discretization-dependent norm. That only seems to give ~0.1%
2147 // excess error even in coarse unit test cases, but at some point it
2148 // might be reasonable to weight K and f properly.
2149 //
2150 // We used to do this by distinguishing "constrained" from
2151 // "unconstrained" DoFs rather than "evaluated" vs "unevaluated",
2152 // but that was failing for any IGA meshes where we didn't bother to
2153 // duplicate spline nodes that were exactly equal to assembly
2154 // element vertices.
2155
2156 // Keep track of which DoFs we've added. We have a contiguous DoF
2157 // range [a,b) so we'll store a vector with indices [0,b-a) for
2158 // efficiency
2159 std::size_t n_local_dofs = dof_map.n_local_dofs();
2160 std::vector<bool> have_added(n_local_dofs, false);
2161 std::set<dof_id_type> have_added_ghost;
2162 std::size_t first_dof = dof_map.first_dof();
2163
2164 std::vector<dof_id_type> di;
2165
2166 for (const auto & elem : this->get_mesh().active_local_element_ptr_range())
2167 {
2168 // INVALID_MAP for the upcoming spline node change, plus
2169 // NODEELEM for meshes pre-change. If we have non-spline-node
2170 // NODEELEM we should have returned early already.
2171 if (elem->mapping_type() == INVALID_MAP ||
2172 (elem->type() == NODEELEM))
2173 continue;
2174
2175 dof_map.dof_indices(elem, di);
2176
2177 for (auto d : di)
2178 {
2179 if (d < first_dof)
2180 continue;
2181 const std::size_t i = d - first_dof;
2182 if (i >= n_local_dofs ||
2183 have_added[i])
2184 continue;
2185
2186 DenseMatrix<Number> K(1,1);
2188 std::vector<dof_id_type> dof_indices(1, d);
2189 K(0,0) = 1;
2190 F(0) = (*this->solution)(d);
2192 (K, F, dof_indices, false, is_adjoint);
2193 mat->add_matrix(K, dof_indices);
2194 rhs->add_vector(F, dof_indices);
2195
2196 // Keep track of both constrained and constraining additions
2197 // to mat, so we can fix any "leftovers" later. Otherwise if
2198 // someone puts a non-constraining DoF on a spline node we
2199 // end up with a singular system.
2200 for (auto d2 : dof_indices)
2201 {
2202 if (d2 < first_dof)
2203 {
2204 have_added_ghost.insert(d2);
2205 continue;
2206 }
2207 const std::size_t i2 = d2 - first_dof;
2208 if (i2 >= n_local_dofs)
2209 {
2210 have_added_ghost.insert(d2);
2211 continue;
2212 }
2213
2214 have_added[i2] = true;
2215 }
2216 }
2217 }
2218
2219 std::unordered_map<processor_id_type, std::vector<dof_id_type>> adds_to_push;
2221 dof_id_type end_p_dof = dof_map.end_dof(0);
2222 for (auto d : have_added_ghost)
2223 {
2224 while (d >= end_p_dof)
2225 {
2226 ++p;
2227 end_p_dof = dof_map.end_dof(p);
2228 }
2229 adds_to_push[p].push_back(d);
2230 }
2231
2232 auto act_on_adds = [&have_added, first_dof]
2233 (processor_id_type, const std::vector<dof_id_type> & dofs)
2234 {
2235 for (auto d : dofs)
2236 have_added[d-first_dof] = true;
2237 };
2238
2239 Parallel::push_parallel_vector_data
2240 (this->comm(), adds_to_push, act_on_adds);
2241
2242 // Pin any unconstraining DoFs that lived on spline nodes
2243 for (auto i : index_range(have_added))
2244 {
2245 if (have_added[i])
2246 continue;
2247
2248 const std::size_t d = i + first_dof;
2249 mat->add(d, d, 1);
2250 rhs->add(d, (*this->solution)(d));
2251 }
2252
2253 std::unique_ptr<LinearSolver<Number>> linear_solver =
2254 LinearSolver<Number>::build(this->comm());
2255
2256 linear_solver->solve(*mat, vec, *rhs,
2257 double(this->get_equation_systems().parameters.get<Real>("linear solver tolerance")),
2258 this->get_equation_systems().parameters.get<unsigned int>("linear solver maximum iterations"));
2259
2260 LinearConvergenceReason conv_flag = linear_solver->get_converged_reason();
2261 libmesh_error_msg_if(conv_flag < 0, "Projection to unconstrained DoF space failed: " << Utility::enum_to_string(conv_flag) << std::endl);
2262}
2263
2264
2265} // namespace libMesh
unsigned int n_vars
unsigned int dim
void ErrorVector unsigned int
The BoundaryInfo class contains information relevant to boundary conditions including storing faces,...
void edge_boundary_ids(const Elem *const elem, const unsigned short int edge, std::vector< boundary_id_type > &vec_to_fill) const
void boundary_ids(const Node *node, std::vector< boundary_id_type > &vec_to_fill) const
Fills a user-provided std::vector with the boundary ids associated with Node node.
This class implements projecting an arbitrary boundary function to the current mesh.
const std::vector< unsigned int > & variables
BoundaryProjectSolution(const BoundaryProjectSolution &in)
std::unique_ptr< FunctionBase< Gradient > > g
std::unique_ptr< FunctionBase< Number > > f
NumericVector< Number > & new_vector
BoundaryProjectSolution(const std::set< boundary_id_type > &b_in, const std::vector< unsigned int > &variables_in, const System &system_in, FunctionBase< Number > *f_in, FunctionBase< Gradient > *g_in, const Parameters &parameters_in, NumericVector< Number > &new_v_in)
const std::set< boundary_id_type > & b
This class builds the send_list of old dof indices whose coefficients are needed to perform a project...
BuildProjectionList(const System &system_in)
BuildProjectionList(BuildProjectionList &other, Threads::split)
std::vector< dof_id_type > send_list
VectorValue< DynamicSparseNumberArray< InnerOutput, dof_id_type > > type
DynamicSparseNumberArray< Output, dof_id_type > type
Defines a dense matrix for use in Finite Element-type computations.
void cholesky_solve(const DenseVector< T2 > &b, DenseVector< T2 > &x)
For symmetric positive definite (SPD) matrices.
void _cholesky_back_substitute(const DenseVector< T2 > &b, DenseVector< T2 > &x) const
Solves the equation Ax=b for the unknown value x and rhs b based on the Cholesky factorization of A.
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
virtual void zero() override final
Sets all elements of the matrix to 0 and resets any decomposition flag which may have been previously...
Defines a dense vector for use in Finite Element-type computations.
void resize(const unsigned int n)
Resize the vector.
virtual void zero() override final
Set every element in the vector to 0.
dof_id_type end_old_dof(const processor_id_type proc) const
dof_id_type first_dof(const processor_id_type proc) const
dof_id_type n_old_dofs() const
dof_id_type first_old_dof(const processor_id_type proc) const
dof_id_type end_dof(const processor_id_type proc) const
This class handles the numbering of degrees of freedom on a mesh.
Definition dof_map.h:181
dof_id_type n_local_dofs(const unsigned int vn) const
Definition dof_map.h:801
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition dof_map.C:2231
void update_sparsity_pattern(SparseMatrix< Number > &matrix) const
Additional matrices may be be temporarily initialized by this DofMap.
Definition dof_map.C:279
bool computed_sparsity_already() const
Returns true iff a sparsity pattern has already been computed.
Definition dof_map.C:268
const Variable & variable(const unsigned int c) const override
Definition dof_map.h:2442
void SCALAR_dof_indices(std::vector< dof_id_type > &di, const unsigned int vn, const bool old_dofs=false) const
Fills the vector di with the global degree of freedom indices corresponding to the SCALAR variable vn...
Definition dof_map.C:2635
dof_id_type n_dofs(const unsigned int vn) const
Definition dof_map.h:783
void old_dof_indices(const Elem &elem, unsigned int n, std::vector< dof_id_type > &di, const unsigned int vn) const
Appends to the vector di the old global degree of freedom indices for elem.node_ref(n),...
Definition dof_map.C:2508
void heterogenously_constrain_element_matrix_and_vector(DenseMatrix< Number > &matrix, DenseVector< Number > &rhs, std::vector< dof_id_type > &elem_dofs, bool asymmetric_constraint_rows=true, int qoi_index=-1) const
Definition dof_map.h:1423
std::unique_ptr< SparsityPattern::Build > build_sparsity(const MeshBase &mesh, bool calculate_constrained=false, bool use_condensed_system=false) const
Builds a sparsity pattern for matrices using the current degree-of-freedom numbering and coupling.
Definition dof_map.C:64
The DofObject defines an abstract base class for objects that have degrees of freedom associated with...
Definition dof_object.h:55
unsigned int n_systems() const
Definition dof_object.h:927
unsigned int n_comp(const unsigned int s, const unsigned int var) const
Definition dof_object.h:992
unsigned int n_comp_group(const unsigned int s, const unsigned int vg) const
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
std::pair< unsigned int, unsigned int > var_to_vg_and_offset(const unsigned int s, const unsigned int var) const
unsigned int n_var_groups(const unsigned int s) const
Definition dof_object.h:947
DofObject * get_old_dof_object()
Pointer accessor for previously public old_dof_object.
Definition dof_object.h:96
DofObject & get_old_dof_object_ref()
As above, but do not use in situations where the old_dof_object may be nullptr, since this function a...
Definition dof_object.h:104
unsigned int n_vars(const unsigned int s, const unsigned int vg) const
Definition dof_object.h:957
This is the base class from which all geometric element types are derived.
Definition elem.h:96
RefinementState refinement_flag() const
Definition elem.h:3348
const Elem * parent() const
Definition elem.h:3168
RefinementState
Enumeration of possible element refinement states.
Definition elem.h:1567
This class forms the foundation from which generic finite elements may be derived.
Definition fe_base.h:86
const std::vector< std::vector< OutputShape > > & get_phi() const
Definition fe_base.h:207
const std::vector< std::vector< OutputGradient > > & get_dphi() const
Definition fe_base.h:252
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.
const Elem & get_elem() const
Accessor for current Elem object.
FEMFunctionBase is a base class from which users can derive in order to define "function-like" object...
virtual Output component(const FEMContext &, unsigned int i, const Point &p, Real time=0.)
The FEMFunctionWrapper input functor class can be used with a GenericProjector to read values from an...
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
FEFamily family
The type of finite element.
Definition fe_type.h:228
Base class for functors that can be evaluated at a point and (optionally) time.
The GenericProjector class implements the core of other projection operations, using two input functo...
This base class can be inherited from to provide interfaces to linear solvers from different packages...
The MatrixFillAction output functor class can be used with GenericProjector to write solution transfe...
DynamicSparseNumberArray< ValIn, dof_id_type > InsertInput
void insert(const std::vector< dof_id_type > &dof_indices, const std::vector< DynamicSparseNumberArray< ValIn, dof_id_type > > &Ue)
SparseMatrix< ValOut > & target_matrix
void insert(dof_id_type id, const DynamicSparseNumberArray< ValIn, dof_id_type > &val)
MatrixFillAction(SparseMatrix< ValOut > &target_mat)
A Node is like a Point, but with more information.
Definition node.h:55
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
virtual void set(const numeric_index_type i, const T value)=0
Sets v(i) = value.
virtual void get(const std::vector< numeric_index_type > &index, T *values) const
Access multiple components at once.
virtual void clear()
Restores the NumericVector<T> to a pristine state.
ParallelType type() const
virtual void close()=0
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
virtual void init(const numeric_index_type n, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC)=0
Change the dimension of the vector to n.
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.
virtual numeric_index_type size() const =0
virtual std::unique_ptr< NumericVector< T > > clone() const =0
virtual numeric_index_type local_size() const =0
The OldSolutionBase input functor abstract base class is the root of the OldSolutionValue and OldSolu...
The OldSolutionCoefs input functor class can be used with GenericProjector to read solution transfer ...
OldSolutionCoefs(const OldSolutionCoefs &in)
DSNAOutput< Output >::type DSNA
void eval_old_dofs(const Elem &elem, unsigned int node_num, unsigned int var_num, std::vector< dof_id_type > &indices, std::vector< DSNA > &values)
DSNA eval_at_point(const FEMContext &c, unsigned int i, const Point &p, Real time, bool skip_context_check)
void eval_mixed_derivatives(const FEMContext &libmesh_dbg_var(c), unsigned int i, unsigned int dim, const Node &n, std::vector< DSNA > &derivs)
OldSolutionCoefs(const libMesh::System &sys_in, const std::vector< unsigned int > *vars)
void eval_old_dofs(const Elem &elem, const FEType &fe_type, unsigned int sys_num, unsigned int var_num, std::vector< dof_id_type > &indices, std::vector< DSNA > &values)
DSNA eval_at_node(const FEMContext &c, unsigned int i, unsigned int elem_dim, const Node &n, bool extra_hanging_dofs, Real=0.)
The OldSolutionValue input functor class can be used with GenericProjector to read values from a solu...
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition parameters.h:75
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:41
Generic sparse matrix.
virtual numeric_index_type row_stop() const =0
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value)=0
Set the element (i,j) to value.
virtual numeric_index_type row_start() const =0
The StoredRange class defines a contiguous, divisible set of objects.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
This class defines the notion of a variable in the system.
Definition variable.h:51
bool active_on_subdomain(subdomain_id_type sid) const
Definition variable.h:167
const FEType & type() const
Definition variable.h:144
The VectorSetAction output functor class can be used with a GenericProjector to set projection values...
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
Wrap a libMesh-style function pointer into a FunctionBase object.
This class provides a wrapper with which to evaluate a (libMesh-style) function pointer in a Function...
static const Real b
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
Definition int_range.h:153
void libmesh_ignore(const Args &...)
const TypeToSend< T >::type convert_to_send(const T &in)
void convert_from_receive(SendT &received, T &converted)
libmesh_assert(ctx)
dof_id_type numeric_index_type
Definition id_types.h:99
uint8_t dof_id_type
Definition id_types.h:67
@ RATIONAL_BERNSTEIN
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t processor_id_type
Definition id_types.h:104
DynamicSparseNumberArray< Real, dof_id_type > DSNAN
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Definition int_range.h:176
LinearConvergenceReason
Linear solver convergence flags (taken from the PETSc flags).
Gradient gptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition projection.C:96
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
Definition projection.C:81
MetaPhysicL::DynamicSparseNumberArray< typename CompareTypes< T, T2 >::supertype, IndexType > supertype
static const bool value
For ease of communication, we allow users to translate their own value types to a more easily computa...
const dof_id_type n_nodes
Definition tecplot_io.C:67