25#include "libmesh/libmesh_config.h"
27#ifdef LIBMESH_HAVE_METAPHYSICL
31#include "libmesh/libmesh_common.h"
32#include "libmesh/compare_types.h"
36#include "metaphysicl/dynamicsparsenumberarray_decl.h"
38using MetaPhysicL::DynamicSparseNumberArray;
44template <
typename T,
typename IndexType>
52template <
typename T,
typename IndexType,
typename T2>
56 MetaPhysicL::DynamicSparseNumberArray
62template <
typename T,
typename IndexType>
64 typedef std::vector<std::pair<IndexType,T>>
type;
67template <
typename T,
typename IndexType>
68const std::vector<std::pair<IndexType,T>>
71 const std::size_t in_size = in.size();
72 std::vector<std::pair<IndexType,T>> returnval(in_size);
74 for (std::size_t i=0; i != in_size; ++i)
76 returnval[i].first = in.raw_index(i);
77 returnval[i].second = in.raw_at(i);
82template <
typename SendT,
typename T,
typename IndexType>
84 MetaPhysicL::DynamicSparseNumberArray<T,IndexType> & converted)
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)
90 converted.raw_index(i) = received[i].first;
91 converted.raw_at(i) = received[i].second;
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"
124#ifdef LIBMESH_HAVE_METAPHYSICL
126#include "metaphysicl/dynamicsparsenumberarray.h"
129#include "libmesh/dense_matrix_impl.h"
132typedef DynamicSparseNumberArray<Real, dof_id_type>
DSNAN;
134template LIBMESH_EXPORT
void
137template LIBMESH_EXPORT
void
151#ifdef LIBMESH_ENABLE_AMR
175 system(other.system),
196 const std::set<boundary_id_type> &
b;
199 std::unique_ptr<FunctionBase<Number>>
f;
200 std::unique_ptr<FunctionBase<Gradient>>
g;
206 const std::vector<unsigned int> & variables_in,
213 variables(variables_in),
217 parameters(parameters_in),
228 variables(in.variables),
232 parameters(in.parameters),
233 new_vector(in.new_vector)
250 std::optional<ConstElemRange> active_local_range,
251 std::optional<std::vector<unsigned int>> variable_numbers)
const
255 std::unique_ptr<NumericVector<Number>>
256 old_vector (vector.
clone());
259 this->project_vector (*old_vector, vector, is_adjoint, active_local_range, variable_numbers);
271 std::optional<ConstElemRange> active_local_range,
272 std::optional<std::vector<unsigned int>> variable_numbers)
const
274 LOG_SCOPE (
"project_vector(old,new)",
"System");
284#ifdef LIBMESH_ENABLE_AMR
288 std::unique_ptr<NumericVector<Number>> new_vector_built;
290 std::unique_ptr<NumericVector<Number>> local_old_vector_built;
295 active_local_range.value() :
296 this->get_mesh().active_local_element_stored_range();
303 new_vector_ptr = &new_v;
304 old_vector_ptr = &old_v;
313 Threads::parallel_reduce (range, projection_list);
318 new_v.
init (this->n_dofs(), this->n_local_dofs(),
false,
PARALLEL);
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,
329 local_old_vector->
close();
330 old_vector_ptr = local_old_vector;
336 Threads::parallel_reduce (range, projection_list);
341 new_v.
init (this->n_dofs(), this->n_local_dofs(),
342 this->get_dof_map().get_send_list(),
false,
GHOSTED);
345 new_vector_ptr = &new_v;
346 local_old_vector = local_old_vector_built.
get();
350 local_old_vector->
close();
351 old_vector_ptr = local_old_vector;
354 libmesh_error_msg(
"ERROR: Unknown old_v.type() == " << old_v.
type());
365 const unsigned int n_variables = this->
n_vars();
369 std::vector<unsigned int> vars;
370 if (variable_numbers)
372 vars = *variable_numbers;
374 if (v >= n_variables)
375 libmesh_error_msg(
"ERROR: variable number " << v <<
376 " out of range for system with " <<
377 n_variables <<
" variables.");
381 vars.resize(n_variables);
382 std::iota(vars.begin(), vars.end(), 0);
385 std::vector<unsigned int> regular_vars, vector_vars, scalar_vars;
386 for (
auto var : vars)
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);
393 vector_vars.push_back(var);
398 if (!regular_vars.empty())
407 f(*
this, old_vector, ®ular_vars);
409 g(*
this, old_vector, ®ular_vars);
411 FEMProjector projector(*
this, f, &g, setter, regular_vars);
412 projector.project(range);
415 if (!vector_vars.empty())
425 FEMVectorProjector vector_projector(*
this, f_vector, &g_vector, setter, vector_vars);
426 vector_projector.project(range);
432 if (this->processor_id() == (this->n_processors()-1))
434 const DofMap & dof_map = this->get_dof_map();
435 for (
auto var : scalar_vars)
438 std::vector<dof_id_type> new_SCALAR_indices, old_SCALAR_indices;
442 new_vector.
set(new_SCALAR_indices[i], old_vector(old_SCALAR_indices[i]));
457 dist_v->init(this->n_dofs(), this->n_local_dofs(),
false,
PARALLEL);
461 if (new_vector(i) != 0.0)
462 dist_v->set(i, new_vector(i));
466 dist_v->localize (new_v, this->get_dof_map().get_send_list());
482 if(this->project_with_constraints)
484 if (is_adjoint == -1)
486 this->get_dof_map().enforce_constraints_exactly(*
this, &new_v);
488 else if (is_adjoint >= 0)
490 this->get_dof_map().enforce_adjoint_constraints_exactly(new_v,
505#ifdef LIBMESH_ENABLE_AMR
506#ifdef LIBMESH_HAVE_METAPHYSICL
508template <
typename Output>
512 typedef DynamicSparseNumberArray<Output, dof_id_type>
type;
515template <
typename InnerOutput>
531template <
typename Output,
544 const std::vector<unsigned int> * vars) :
547 this->old_context.set_algebraic_type(FEMContext::OLD_DOFS_ONLY);
551 OldSolutionBase<Output, point_output>(in.sys, in.old_context.active_vars())
553 this->old_context.set_algebraic_type(FEMContext::OLD_DOFS_ONLY);
558 unsigned int elem_dim,
560 bool extra_hanging_dofs,
567 bool skip_context_check);
573 std::vector<DSNA> & derivs)
575 LOG_SCOPE (
"eval_mixed_derivatives",
"OldSolutionCoefs");
578 libmesh_assert_less(c.get_elem().get_node_index(&n),
579 c.get_elem().n_vertices());
582 libmesh_assert_less(i, this->component_to_var.size());
583 unsigned int var = this->component_to_var[i];
586 const unsigned int n_mixed = (
dim-1) * (
dim-1);
587 derivs.resize(n_mixed);
592 if (old_dof_object &&
593 old_dof_object->
n_vars(this->sys.number()) &&
594 old_dof_object->
n_comp(this->sys.number(), var))
598 std::vector<dof_id_type> old_ids(n_mixed);
599 std::iota(old_ids.begin(), old_ids.end(), first_old_id);
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];
610 std::fill(derivs.begin(), derivs.end(), 0);
616 unsigned int node_num,
617 unsigned int var_num,
618 std::vector<dof_id_type> & indices,
619 std::vector<DSNA> & values)
621 LOG_SCOPE (
"eval_old_dofs(node)",
"OldSolutionCoefs");
627 this->sys.get_dof_map().dof_indices(elem, node_num, indices, var_num);
629 std::vector<dof_id_type> old_indices;
631 this->sys.get_dof_map().old_dof_indices(elem, node_num, old_indices, var_num);
633 libmesh_assert_equal_to (old_indices.size(), indices.size());
635 values.resize(old_indices.size());
640 values[i].raw_at(0) = 1;
641 values[i].raw_index(0) = old_indices[i];
648 unsigned int sys_num,
649 unsigned int var_num,
650 std::vector<dof_id_type> & indices,
651 std::vector<DSNA> & values)
653 LOG_SCOPE (
"eval_old_dofs(elem)",
"OldSolutionCoefs");
657 const Elem & old_elem =
662 const unsigned int nc =
663 FEInterface::n_dofs_per_elem(fe_type, &elem);
665 std::vector<dof_id_type> old_dof_indices(nc);
675 const auto [vg, vig] =
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);
682 for (
unsigned int i=0; i<nc; i++)
685 old_dof_object.
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);
691 old_dof_indices[i] = d_old;
696 values.resize(old_dof_indices.size());
701 values[i].raw_at(0) = 1;
702 values[i].raw_index(0) = old_dof_indices[i];
711DynamicSparseNumberArray<Real, dof_id_type>
717 bool skip_context_check)
719 LOG_SCOPE (
"eval_at_point()",
"OldSolutionCoefs");
721 if (!skip_context_check)
722 if (!this->check_old_context(c, p))
727 this->old_context.get_element_fe<
Real>
728 (i, fe, this->old_context.get_elem_dim());
732 this->old_context.build_new_fe(fe, p);
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);
739 const std::size_t n_dofs = phi.size();
740 libmesh_assert_equal_to(n_dofs, dof_indices.size());
742 DynamicSparseNumberArray<Real, dof_id_type> returnval;
743 returnval.resize(n_dofs);
747 returnval.raw_at(j) = phi[j][0];
748 returnval.raw_index(j) = dof_indices[j];
764 bool skip_context_check)
766 LOG_SCOPE (
"eval_at_point()",
"OldSolutionCoefs");
768 if (!skip_context_check)
769 if (!this->check_old_context(c, p))
774 this->old_context.get_element_fe<
Real>
775 (i, fe, this->old_context.get_elem_dim());
779 this->old_context.build_new_fe(fe, p);
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);
786 const std::size_t n_dofs = dphi.size();
787 libmesh_assert_equal_to(n_dofs, dof_indices.size());
791 for (
unsigned int d = 0; d != LIBMESH_DIM; ++d)
792 returnval(d).resize(n_dofs);
795 for (
int d = 0; d != LIBMESH_DIM; ++d)
797 returnval(d).raw_at(j) = dphi[j][0](d);
798 returnval(d).raw_index(j) = dof_indices[j];
807DynamicSparseNumberArray<Real, dof_id_type>
813 bool extra_hanging_dofs,
816 LOG_SCOPE (
"Real eval_at_node()",
"OldSolutionCoefs");
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))
838 DynamicSparseNumberArray<Real, dof_id_type> returnval;
840 old_dof_object->
dof_number(sys.number(), i, 0);
842 returnval.raw_at(0) = 1;
843 returnval.raw_index(0) = old_id;
847 return this->eval_at_point(c, i, n, 0,
false);
858 unsigned int elem_dim,
860 bool extra_hanging_dofs,
863 LOG_SCOPE (
"RealGradient eval_at_node()",
"OldSolutionCoefs");
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))
886 for (
unsigned int d = 0; d != elem_dim; ++d)
889 old_dof_object->
dof_number(sys.number(), i, d+1);
892 g(d).raw_index(0) = old_id;
897 return this->eval_at_point(c, i, n, 0,
false);
910template <
typename ValIn,
typename ValOut>
920 target_matrix(target_mat) {}
923 const DynamicSparseNumberArray<ValIn, dof_id_type> & val)
927 Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
929 const std::size_t dnsa_size = val.size();
930 for (
unsigned int j = 0; j != dnsa_size; ++j)
933 const ValIn dof_val = val.raw_at(j);
934 target_matrix.
set(
id, dof_j, dof_val);
940 void insert(
const std::vector<dof_id_type> & dof_indices,
941 const std::vector<DynamicSparseNumberArray<ValIn, dof_id_type> > & Ue)
947 unsigned int size = Ue.size();
949 libmesh_assert_equal_to(size, dof_indices.size());
953 Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
955 for (
unsigned int i = 0; i != size; ++i)
958 if ((dof_i >= begin_dof) && (dof_i < end_dof))
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)
965 const ValIn dof_val = dnsa.raw_at(j);
966 target_matrix.
set(dof_i, dof_j, dof_val);
982 LOG_SCOPE (
"projection_matrix()",
"System");
984 const unsigned int n_variables = this->
n_vars();
988 std::vector<unsigned int> vars(n_variables);
989 std::iota(vars.begin(), vars.end(), 0);
997 OldSolutionGradientCoefs,
998 DynamicSparseNumberArray<Real,dof_id_type>,
1001 OldSolutionValueCoefs f(*
this, &vars);
1002 OldSolutionGradientCoefs g(*
this, &vars);
1005 ProjMatFiller mat_filler(*
this, f, &g, setter, vars);
1006 mat_filler.project(this->get_mesh().active_local_element_stored_range());
1011 if (this->processor_id() == (this->n_processors()-1))
1013 const DofMap & dof_map = this->get_dof_map();
1015 if (this->variable(var).type().family ==
SCALAR)
1018 std::vector<dof_id_type> new_SCALAR_indices, old_SCALAR_indices;
1021 const unsigned int new_n_dofs =
1022 cast_int<unsigned int>(new_SCALAR_indices.size());
1024 for (
unsigned int i=0; i<new_n_dofs; i++)
1026 proj_mat.
set( new_SCALAR_indices[i],
1027 old_SCALAR_indices[i], 1);
1042void System::project_solution (ValueFunctionPointer
fptr,
1043 GradientFunctionPointer
gptr,
1045 std::optional<ConstElemRange> active_local_range,
1046 std::optional<std::vector<unsigned int>> variable_numbers)
const
1050 this->project_solution(&f, &g, active_local_range, variable_numbers);
1060 std::optional<ConstElemRange> active_local_range,
1061 std::optional<std::vector<unsigned int>> variable_numbers)
const
1063 this->project_vector(*solution, f, g, -1, active_local_range, variable_numbers);
1065 solution->localize(*current_local_solution, _dof_map->get_send_list());
1075 std::optional<ConstElemRange> active_local_range,
1076 std::optional<std::vector<unsigned int>> variable_numbers)
const
1078 this->project_vector(*solution, f, g, -1, active_local_range, variable_numbers);
1080 solution->localize(*current_local_solution, _dof_map->get_send_list());
1088void System::project_vector (ValueFunctionPointer
fptr,
1089 GradientFunctionPointer
gptr,
1093 std::optional<ConstElemRange> active_local_range,
1094 std::optional<std::vector<unsigned int>> variable_numbers)
const
1098 this->project_vector(new_vector, &f, &g, is_adjoint, active_local_range, variable_numbers);
1109 std::optional<ConstElemRange> active_local_range,
1110 std::optional<std::vector<unsigned int>> variable_numbers)
const
1112 LOG_SCOPE (
"project_vector(FunctionBase)",
"System");
1122 this->project_vector(new_vector, &f_fem, &g_fem, is_adjoint, active_local_range, variable_numbers);
1125 this->project_vector(new_vector, &f_fem,
nullptr, is_adjoint, active_local_range, variable_numbers);
1137 std::optional<ConstElemRange> active_local_range,
1138 std::optional<std::vector<unsigned int>> variable_numbers)
const
1140 LOG_SCOPE (
"project_fem_vector()",
"System");
1145 active_local_range ?
1146 active_local_range.value() :
1147 this->get_mesh().active_local_element_stored_range();
1151 const unsigned int n_variables = this->
n_vars();
1153 std::vector<unsigned int> vars;
1154 if (variable_numbers)
1156 vars = *variable_numbers;
1158 if (v >= n_variables)
1159 libmesh_error_msg(
"ERROR: variable number " << v <<
1160 " out of range for system with " <<
1161 n_variables <<
" variables.");
1165 vars.resize(n_variables);
1166 std::iota(vars.begin(), vars.end(), 0);
1181 FEMProjector projector(*
this, fw, &gw, setter, vars);
1182 projector.project(range);
1186 FEMProjector projector(*
this, fw,
nullptr, setter, vars);
1187 projector.project(range);
1193 if (this->processor_id() == (this->n_processors()-1))
1198 const DofMap & dof_map = this->get_dof_map();
1199 for (
auto var : vars)
1200 if (this->variable(var).type().family ==
SCALAR)
1205 context.
pre_fe_reinit(*
this, *(this->get_mesh().active_local_elements_begin()));
1207 std::vector<dof_id_type> SCALAR_indices;
1209 const unsigned int n_SCALAR_dofs =
1210 cast_int<unsigned int>(SCALAR_indices.size());
1212 for (
unsigned int i=0; i<n_SCALAR_dofs; i++)
1214 const dof_id_type global_index = SCALAR_indices[i];
1215 const unsigned int component_index =
1216 this->variable_scalar_number(var,i);
1218 new_vector.
set(global_index, f->
component(context, component_index,
Point(), this->time));
1227 std::vector<const Variable *> rational_vars;
1228 for (
auto varnum : vars)
1230 const Variable & var = this->get_dof_map().variable(varnum);
1232 rational_vars.push_back(&var);
1237 bool using_spline_bases =
false;
1238 if (!rational_vars.empty())
1242 for (
auto & elem : range)
1244 for (
auto rational_var : rational_vars)
1245 if (rational_var->active_on_subdomain(elem->subdomain_id()))
1247 using_spline_bases =
true;
1248 goto checked_on_splines;
1256 this->comm().max(using_spline_bases);
1258 if (using_spline_bases)
1259 this->solve_for_unconstrained_dofs(new_vector, is_adjoint);
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,
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,
1283 std::optional<ConstElemRange> active_local_range)
1288 this->boundary_project_solution(
b, variables, &f, &g, active_local_range);
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)
1303 this->boundary_project_vector(
b, variables, *solution, f, g, -1 , active_local_range);
1305 solution->localize(*current_local_solution);
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,
1323 std::optional<ConstElemRange> active_local_range)
const
1327 this->boundary_project_vector(
b, variables, new_vector, &f, &g,
1328 is_adjoint, active_local_range);
1335void System::boundary_project_vector (
const std::set<boundary_id_type> &
b,
1336 const std::vector<unsigned int> & variables,
1341 std::optional<ConstElemRange> active_local_range)
const
1343 LOG_SCOPE (
"boundary_project_vector()",
"System");
1346 active_local_range ?
1347 active_local_range.value() :
1348 this->get_mesh().active_local_element_stored_range();
1350 Threads::parallel_for
1353 this->get_equation_systems().parameters,
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,
1375#ifdef LIBMESH_ENABLE_AMR
1376void BuildProjectionList::unique()
1380 std::sort(this->send_list.begin(),
1381 this->send_list.end());
1384 std::vector<dof_id_type>::iterator new_end =
1385 std::unique (this->send_list.begin(),
1386 this->send_list.end());
1390 std::vector<dof_id_type>
1391 (this->send_list.begin(), new_end).swap (this->send_list);
1399 const DofMap & dof_map = system.get_dof_map();
1406 std::vector<dof_id_type> di;
1409 for (
const auto & elem : range)
1425 if (!old_dof_object &&
1426 elem->refinement_flag() != Elem::JUST_REFINED &&
1427 elem->refinement_flag() != Elem::JUST_COARSENED)
1432 if (elem->refinement_flag() == Elem::JUST_REFINED)
1442 for (
auto & node : elem->node_ref_range())
1448 const unsigned int sysnum = system.number();
1449 const unsigned int nvg = old_dofs->
n_var_groups(sysnum);
1451 for (
unsigned int vg=0; vg != nvg; ++vg)
1453 const unsigned int nvig =
1454 old_dofs->
n_vars(sysnum, vg);
1455 for (
unsigned int vig=0; vig != nvig; ++vig)
1457 const unsigned int n_comp =
1459 for (
unsigned int c=0; c != n_comp; ++c)
1469 old_id == DofObject::invalid_id);
1470 di.push_back(old_id);
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);
1482 else if (elem->refinement_flag() == Elem::JUST_COARSENED)
1484 std::vector<dof_id_type> di_child;
1486 for (
auto & child : elem->child_ref_range())
1489 di.insert(di.end(), di_child.begin(), di_child.end());
1495 for (
auto di_i : di)
1501 if (di_i == DofObject::invalid_id)
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);
1516 this->send_list.insert(this->send_list.end(),
1537 const unsigned int dim = system.get_mesh().mesh_dimension();
1540 const DofMap & dof_map = system.get_dof_map();
1544 system.get_mesh().get_boundary_info();
1559 const unsigned int var = variables[v];
1568 const unsigned int var_component =
1569 system.variable_scalar_number(var, 0);
1572 std::unique_ptr<FEBase> fe (FEBase::build(
dim, fe_type));
1580 const std::vector<std::vector<Real>> & phi = fe->get_phi();
1584 const std::vector<std::vector<RealGradient>> * dphi =
nullptr;
1593 const std::vector<std::vector<RealGradient>> &
1594 ref_dphi = fe->get_dphi();
1599 const std::vector<Real> & JxW =
1603 const std::vector<Point> & xyz_values =
1607 std::vector<dof_id_type> dof_indices;
1609 std::vector<unsigned int> side_dofs;
1612 std::vector<boundary_id_type> bc_ids;
1615 for (
const auto & elem : range)
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();
1628 std::vector<bool> is_boundary_node(
n_nodes,
false),
1629 is_boundary_edge(n_edges,
false),
1630 is_boundary_side(n_sides,
false);
1633 std::vector<bool> is_boundary_nodeset(
n_nodes,
false);
1635 for (
unsigned char s=0; s != n_sides; ++s)
1639 bool do_this_side =
false;
1640 for (
const auto & bc_id : bc_ids)
1643 do_this_side =
true;
1649 is_boundary_side[s] =
true;
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;
1662 for (
unsigned int n=0; n !=
n_nodes; ++n)
1664 boundary_info.
boundary_ids (elem->node_ptr(n), bc_ids);
1666 for (
const auto & bc_id : bc_ids)
1669 is_boundary_node[n] =
true;
1670 is_boundary_nodeset[n] =
true;
1676 for (
unsigned short e=0; e != n_edges; ++e)
1680 for (
const auto & bc_id : bc_ids)
1682 is_boundary_edge[e] =
true;
1690 const unsigned int n_dofs =
1691 cast_int<unsigned int>(dof_indices.size());
1694 std::vector<char> dof_is_fixed(n_dofs,
false);
1695 std::vector<int> free_dof(n_dofs, 0);
1707 unsigned int current_dof = 0;
1708 for (
unsigned short n = 0; n !=
n_nodes; ++n)
1714 const unsigned int nc =
1715 FEInterface::n_dofs_at_node (fe_type, elem, n);
1717 if ((!elem->is_vertex(n) || !is_boundary_node[n]) &&
1718 !is_boundary_nodeset[n])
1725 libmesh_assert_equal_to (nc, 0);
1731 libmesh_assert_equal_to (nc, 1);
1732 Ue(current_dof) = f->component(var_component,
1735 dof_is_fixed[current_dof] =
true;
1741 Ue(current_dof) = f->component(var_component,
1744 dof_is_fixed[current_dof] =
true;
1746 Gradient grad = g->component(var_component,
1750 Ue(current_dof) = grad(0);
1751 dof_is_fixed[current_dof] =
true;
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,
1764 Gradient gxplus = g->component(var_component,
1768 Ue(current_dof) = grad(1);
1769 dof_is_fixed[current_dof] =
true;
1772 Ue(current_dof) = (gxplus(1) - gxminus(1))
1774 dof_is_fixed[current_dof] =
true;
1781 Ue(current_dof) = grad(2);
1782 dof_is_fixed[current_dof] =
true;
1785 Ue(current_dof) = (gxplus(2) - gxminus(2))
1787 dof_is_fixed[current_dof] =
true;
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,
1797 Gradient gyplus = g->component(var_component,
1801 Ue(current_dof) = (gyplus(2) - gyminus(2))
1803 dof_is_fixed[current_dof] =
true;
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,
1821 Gradient gxmyp = g->component(var_component,
1824 Gradient gxpym = g->component(var_component,
1827 Gradient gxpyp = g->component(var_component,
1830 Number gxzplus = (gxpyp(2) - gxmyp(2))
1832 Number gxzminus = (gxpym(2) - gxmym(2))
1835 Ue(current_dof) = (gxzplus - gxzminus)
1837 dof_is_fixed[current_dof] =
true;
1847 else if (cont ==
C_ONE)
1849 libmesh_assert_equal_to (nc, 1 +
dim);
1850 Ue(current_dof) = f->component(var_component,
1853 dof_is_fixed[current_dof] =
true;
1855 Gradient grad = g->component(var_component,
1858 for (
unsigned int i=0; i!=
dim; ++i)
1860 Ue(current_dof) = grad(i);
1861 dof_is_fixed[current_dof] =
true;
1866 libmesh_error_msg(
"Unknown continuity " << cont);
1871 for (
unsigned short e = 0; e != n_edges; ++e)
1873 if (!is_boundary_edge[e])
1876 FEInterface::dofs_on_edge(elem,
dim, fe_type, e,
1879 const unsigned int n_side_dofs =
1880 cast_int<unsigned int>(side_dofs.size());
1884 unsigned int free_dofs = 0;
1886 if (!dof_is_fixed[side_dofs[i]])
1887 free_dof[free_dofs++] = i;
1899 fe->attach_quadrature_rule (qedgerule.get());
1900 fe->edge_reinit (elem, e);
1901 const unsigned int n_qp = qedgerule->n_points();
1904 for (
unsigned int qp=0; qp<n_qp; qp++)
1907 Number fineval = f->component(var_component,
1913 finegrad = g->component(var_component,
1918 for (
unsigned int sidei=0, freei=0;
1919 sidei != n_side_dofs; ++sidei)
1921 unsigned int i = side_dofs[sidei];
1923 if (dof_is_fixed[i])
1925 for (
unsigned int sidej=0, freej=0;
1926 sidej != n_side_dofs; ++sidej)
1928 unsigned int j = side_dofs[sidej];
1929 if (dof_is_fixed[j])
1930 Fe(freei) -= phi[i][qp] * phi[j][qp] *
1933 Ke(freei,freej) += phi[i][qp] *
1934 phi[j][qp] * JxW[qp];
1937 if (dof_is_fixed[j])
1938 Fe(freei) -= ((*dphi)[i][qp] *
1942 Ke(freei,freej) += ((*dphi)[i][qp] *
1946 if (!dof_is_fixed[j])
1949 Fe(freei) += phi[i][qp] * fineval * JxW[qp];
1951 Fe(freei) += (finegrad * (*dphi)[i][qp]) *
1960 for (
unsigned int i=0; i != free_dofs; ++i)
1962 Number & ui = Ue(side_dofs[free_dof[i]]);
1964 std::abs(ui - Uedge(i)) < TOLERANCE);
1966 dof_is_fixed[side_dofs[free_dof[i]]] =
true;
1972 for (
unsigned short s = 0; s != n_sides; ++s)
1974 if (!is_boundary_side[s])
1977 FEInterface::dofs_on_side(elem,
dim, fe_type, s,
1982 unsigned int free_dofs = 0;
1984 if (!dof_is_fixed[side_dofs[i]])
1985 free_dof[free_dofs++] = i;
1997 fe->attach_quadrature_rule (qsiderule.get());
1998 fe->reinit (elem, s);
1999 const unsigned int n_qp = qsiderule->n_points();
2001 const unsigned int n_side_dofs =
2002 cast_int<unsigned int>(side_dofs.size());
2005 for (
unsigned int qp=0; qp<n_qp; qp++)
2008 Number fineval = f->component(var_component,
2014 finegrad = g->component(var_component,
2019 for (
unsigned int sidei=0, freei=0;
2020 sidei != n_side_dofs; ++sidei)
2022 unsigned int i = side_dofs[sidei];
2024 if (dof_is_fixed[i])
2026 for (
unsigned int sidej=0, freej=0;
2027 sidej != n_side_dofs; ++sidej)
2029 unsigned int j = side_dofs[sidej];
2030 if (dof_is_fixed[j])
2031 Fe(freei) -= phi[i][qp] * phi[j][qp] *
2034 Ke(freei,freej) += phi[i][qp] *
2035 phi[j][qp] * JxW[qp];
2038 if (dof_is_fixed[j])
2039 Fe(freei) -= ((*dphi)[i][qp] *
2043 Ke(freei,freej) += ((*dphi)[i][qp] *
2047 if (!dof_is_fixed[j])
2050 Fe(freei) += (fineval * phi[i][qp]) * JxW[qp];
2052 Fe(freei) += (finegrad * (*dphi)[i][qp]) *
2061 for (
unsigned int i=0; i != free_dofs; ++i)
2063 Number & ui = Ue(side_dofs[free_dof[i]]);
2065 std::abs(ui - Uside(i)) < TOLERANCE);
2067 dof_is_fixed[side_dofs[free_dof[i]]] =
true;
2072 first = new_vector.first_local_index(),
2073 last = new_vector.last_local_index();
2077 Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
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))
2084 new_vector.set(dof_indices[i], Ue(i));
2093 int is_adjoint)
const
2101 if (!this->get_mesh().n_constraint_rows())
2103 libmesh_warning(
"Called solve_for_unconstrained_dofs on a mesh with no constraint_rows?");
2107 const DofMap & dof_map = this->get_dof_map();
2109 std::unique_ptr<SparseMatrix<Number>> mat =
2112 std::unique_ptr<SparsityPattern::Build> sp;
2118 mat->attach_dof_map(dof_map);
2120 mat->attach_sparsity_pattern(*sp);
2125 libmesh_assert_equal_to(vec.
size(), dof_map.
n_dofs());
2128 std::unique_ptr<NumericVector<Number>> rhs =
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();
2164 std::vector<dof_id_type> di;
2166 for (
const auto & elem : this->get_mesh().active_local_element_ptr_range())
2181 const std::size_t i = d - first_dof;
2182 if (i >= n_local_dofs ||
2188 std::vector<dof_id_type> dof_indices(1, d);
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);
2200 for (
auto d2 : dof_indices)
2204 have_added_ghost.insert(d2);
2207 const std::size_t i2 = d2 - first_dof;
2208 if (i2 >= n_local_dofs)
2210 have_added_ghost.insert(d2);
2214 have_added[i2] =
true;
2219 std::unordered_map<processor_id_type, std::vector<dof_id_type>> adds_to_push;
2222 for (
auto d : have_added_ghost)
2224 while (d >= end_p_dof)
2227 end_p_dof = dof_map.
end_dof(p);
2229 adds_to_push[p].push_back(d);
2232 auto act_on_adds = [&have_added, first_dof]
2236 have_added[d-first_dof] =
true;
2239 Parallel::push_parallel_vector_data
2240 (this->comm(), adds_to_push, act_on_adds);
2248 const std::size_t d = i + first_dof;
2250 rhs->add(d, (*this->solution)(d));
2253 std::unique_ptr<LinearSolver<Number>> linear_solver =
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"));
2261 libmesh_error_msg_if(conv_flag < 0,
"Projection to unconstrained DoF space failed: " << Utility::enum_to_string(conv_flag) << std::endl);
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 ¶meters_in, NumericVector< Number > &new_v_in)
const std::set< boundary_id_type > & b
const Parameters & parameters
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.
dof_id_type n_local_dofs(const unsigned int vn) const
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
void update_sparsity_pattern(SparseMatrix< Number > &matrix) const
Additional matrices may be be temporarily initialized by this DofMap.
bool computed_sparsity_already() const
Returns true iff a sparsity pattern has already been computed.
const Variable & variable(const unsigned int c) const override
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...
dof_id_type n_dofs(const unsigned int vn) const
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),...
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
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.
The DofObject defines an abstract base class for objects that have degrees of freedom associated with...
unsigned int n_systems() const
unsigned int n_comp(const unsigned int s, const unsigned int var) const
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
DofObject * get_old_dof_object()
Pointer accessor for previously public old_dof_object.
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...
unsigned int n_vars(const unsigned int s, const unsigned int vg) const
This is the base class from which all geometric element types are derived.
RefinementState refinement_flag() const
const Elem * parent() const
RefinementState
Enumeration of possible element refinement states.
This class forms the foundation from which generic finite elements may be derived.
const std::vector< std::vector< OutputShape > > & get_phi() const
const std::vector< std::vector< OutputGradient > > & get_dphi() const
This class provides all data required for a physics package (e.g.
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...
std::unique_ptr< QBase > default_quadrature_rule(const unsigned int dim, const int extraorder=0) const
FEFamily family
The type of finite element.
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.
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...
A Point defines a location in LIBMESH_DIM dimensional Real space.
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.
Dummy "splitting object" used to distinguish splitting constructors from copy constructors.
This class defines the notion of a variable in the system.
bool active_on_subdomain(subdomain_id_type sid) const
const FEType & type() const
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...
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...
void libmesh_ignore(const Args &...)
const TypeToSend< T >::type convert_to_send(const T &in)
void convert_from_receive(SendT &received, T &converted)
dof_id_type numeric_index_type
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
uint8_t processor_id_type
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...
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)
Number fptr(const Point &p, const Parameters &, const std::string &libmesh_dbg_var(sys_name), const std::string &unknown_name)
For ease of communication, we allow users to translate their own value types to a more easily computa...
const dof_id_type n_nodes