17#include "libmesh/mesh_base.h"
19template <
typename OutputType>
24 _subproblem(_sys.subproblem()),
26 _dof_map(_sys.dofMap()),
28 _has_dof_values(false),
30 _solution_tag(_subproblem.getVectorTagID(
Moose::SOLUTION_TAG)),
31 _old_solution_tag(
Moose::INVALID_TAG_ID),
32 _older_solution_tag(
Moose::INVALID_TAG_ID),
33 _previous_nl_solution_tag(
Moose::INVALID_TAG_ID),
35 _need_u_dotdot(false),
36 _need_u_dot_old(false),
37 _need_u_dotdot_old(false),
38 _need_du_dot_du(false),
39 _need_du_dotdot_du(false),
40 _need_grad_dot(false),
41 _need_grad_dotdot(false),
42 _need_dof_values_dot(false),
43 _need_dof_values_dotdot(false),
44 _need_dof_values_dot_old(false),
45 _need_dof_values_dotdot_old(false),
46 _need_dof_du_dot_du(false),
47 _need_dof_du_dotdot_du(false),
55 const auto max_future_num_vector_tags = num_vector_tags + 2;
85template <
typename OutputType>
89 auto num_matrix_tags = _subproblem.numMatrixTags();
91 _matrix_tags_dof_u.resize(num_matrix_tags);
92 _need_matrix_tag_dof_u.resize(num_matrix_tags,
false);
94 _need_matrix_tag_u.resize(num_matrix_tags,
false);
95 _matrix_tag_u.resize(num_matrix_tags);
98template <
typename OutputType>
104 if (tag >= _need_vector_tag_dof_u.size())
107 _need_vector_tag_dof_u[tag] =
true;
109 if (_sys.hasVector(tag))
110 return _vector_tags_dof_u[tag];
113 "Tag ", tag,
" is not associated with any vector for nodal variable ", _var.name());
116 mooseError(
"Nodal values can be requested only on nodal variables, variable '",
121template <
typename OutputType>
127 if (tag >= _matrix_tags_dof_u.size())
129 _need_matrix_tag_dof_u.
resize(tag + 1,
false);
133 _need_matrix_tag_dof_u[tag] =
true;
135 if (_sys.hasMatrix(tag))
136 return _matrix_tags_dof_u[tag];
139 "Tag ", tag,
" is not associated with any matrix for nodal variable ", _var.name());
142 mooseError(
"Nodal values can be requested only on nodal variables, variable '",
147template <
typename OutputType>
151 mooseAssert(_need_vector_tag_dof_u.size() == _need_vector_tag_u.size() &&
152 _need_vector_tag_dof_u.size() == _need_vector_tag_grad.size() &&
153 _need_vector_tag_dof_u.size() == _vector_tags_dof_u.size() &&
154 _need_vector_tag_dof_u.size() == _vector_tag_u.size() &&
155 _need_vector_tag_dof_u.size() == _vector_tag_grad.size(),
156 "These sizes should be in sync.");
158 auto check_capacity = [tag](
const auto & vector_to_check)
160 if (tag + 1 > vector_to_check.capacity())
161 mooseError(
"New size greater than tag capacity. This will cause reallocation which will "
162 "invalidate any stored references.");
164 check_capacity(_need_vector_tag_dof_u);
165 check_capacity(_need_vector_tag_u);
166 check_capacity(_need_vector_tag_grad);
167 check_capacity(_vector_tags_dof_u);
168 check_capacity(_vector_tag_u);
169 check_capacity(_vector_tag_grad);
171 _need_vector_tag_dof_u.resize(tag + 1,
false);
172 _need_vector_tag_u.resize(tag + 1,
false);
173 _need_vector_tag_grad.resize(tag + 1,
false);
174 _vector_tags_dof_u.resize(tag + 1);
175 _vector_tag_u.resize(tag + 1);
176 _vector_tag_grad.resize(tag + 1);
179template <
typename OutputType>
183 if (tag >= _need_vector_tag_u.size())
186 _var.requireQpComputations();
188 _need_vector_tag_u[tag] =
true;
190 if (_sys.hasVector(tag))
191 return _vector_tag_u[tag];
193 mooseError(
"Tag ", tag,
" is not associated with any vector for variable ", _var.name());
196template <
typename OutputType>
200 if (tag >= _need_vector_tag_dof_u.size())
203 _var.requireQpComputations();
205 _need_vector_tag_dof_u[tag] =
true;
207 if (_sys.hasVector(tag))
208 return _vector_tags_dof_u[tag];
210 mooseError(
"Tag ", tag,
" is not associated with any vector for variable ", _var.name());
213template <
typename OutputType>
217 if (tag >= _need_vector_tag_grad.size())
220 _var.requireQpComputations();
222 _need_vector_tag_grad[tag] =
true;
224 if (_sys.hasVector(tag))
225 return _vector_tag_grad[tag];
227 mooseError(
"Tag ", tag,
" is not associated with any vector for variable ", _var.name());
230template <
typename OutputType>
234 if (tag >= _matrix_tag_u.size())
236 _need_matrix_tag_u.
resize(tag + 1,
false);
240 _var.requireQpComputations();
242 _need_matrix_tag_u[tag] =
true;
244 if (_sys.hasMatrix(tag))
245 return _matrix_tag_u[tag];
247 mooseError(
"Tag ", tag,
" is not associated with any matrix for variable ", _var.name());
250template <
typename OutputType>
257template <
typename OutputType>
261 if (state > _max_state)
263 _sys.needSolutionState(state);
268template <
typename OutputType>
269template <
typename ReturnType,
typename Functor>
278 needSolutionState(cast_int<unsigned int>(state));
289 insertSolutionTag(_old_solution_tag);
290 return functor(_old_solution_tag);
296 insertSolutionTag(_older_solution_tag);
297 return functor(_older_solution_tag);
303 insertSolutionTag(_previous_nl_solution_tag);
304 return functor(_previous_nl_solution_tag);
314template <
typename OutputType>
319 {
return vectorTagValue(tag_id); };
321 _var.requireQpComputations();
324 ->stateToTagHelper<FieldVariableValue>(state, functor);
327template <
typename OutputType>
332 {
return vectorTagGradient(tag_id); };
334 _var.requireQpComputations();
337 ->stateToTagHelper<FieldVariableGradient>(state, functor);
340template <
typename OutputType>
344 auto functor = [
this](
TagID tag_id) ->
const DofValues & {
return vectorTagDofValue(tag_id); };
346 _var.requireQpComputations();
352template <
typename OutputType>
359template <
typename OutputType>
366template <
typename OutputType>
373template <
typename OutputType>
380template <
typename OutputType>
391 mooseAssert(_dof_indices.size() == _count,
392 "setNodalValue is only valid for a variable with one shape function per node");
393 auto & dof_values = _vector_tags_dof_u[_solution_tag];
394 dof_values.resize(1);
395 dof_values[0] = value;
396 _has_dof_values =
true;
397 _nodal_value = value;
400 auto & u = _vector_tag_u[_solution_tag];
401 for (
unsigned int qp = 0; qp < u.size(); qp++)
413 mooseAssert(_count == 1,
"Vector-valued variables cannot be array variables");
415 _var.feType().family == LAGRANGE_VEC ||
416 (_var.feType().family == MONOMIAL_VEC && _var.feType().order == CONSTANT &&
417 (!_var.feType().p_refinement || !_subproblem.doingPRefinement())),
418 "setNodalValue only makes sense for LAGRANGE_VEC or non-p-refined CONSTANT MONOMIAL_VEC "
420 mooseAssert(_dof_indices.size() <= _var.sys().mesh().getMesh().mesh_dimension(),
421 "The number of dof indices for a vector variable should be at most the mesh manifold "
422 "dimension. We cannot use an exact equality because it's possible the vector "
423 "variable could live on a lower-dimensional manifold.");
425 auto & dof_values = _vector_tags_dof_u[_solution_tag];
426 dof_values.resize(_dof_indices.size());
427 for (
const auto i : index_range(dof_values))
428 dof_values[i] = value(i);
430 _has_dof_values =
true;
431 _nodal_value = value;
434template <
typename OutputType>
440 const auto & dof_values = _vector_tags_dof_u[_solution_tag];
441 mooseAssert(dof_values.size() == _dof_indices.size(),
442 "Degree of freedom values size and degree of freedom indices sizes must match.");
443 residual.insert(&dof_values[0], _dof_indices);
453 const auto & dof_values = _vector_tags_dof_u[_solution_tag];
454 mooseAssert(_dof_indices.size() % _count == 0,
455 "Dof indices should be cleanly divisible by the variable count");
456 const auto n_shapes = _dof_indices.size() / _count;
457 for (
const auto indx : index_range(_dof_indices))
459 const auto i = indx % n_shapes;
460 const auto j = indx / n_shapes;
461 residual.set(_dof_indices[indx], dof_values[i](j));
466template <
typename OutputType>
472 const auto & dof_values = _vector_tags_dof_u[_solution_tag];
473 residual.add_vector(&dof_values[0], _dof_indices);
483 const auto & dof_values = _vector_tags_dof_u[_solution_tag];
484 mooseAssert(_dof_indices.size() % _count == 0,
485 "Dof indices should be cleanly divisible by the variable count");
486 const auto n_shapes = _dof_indices.size() / _count;
487 for (
const auto indx : index_range(_dof_indices))
489 const auto i = indx % n_shapes;
490 const auto j = indx / n_shapes;
491 residual.add(_dof_indices[indx], dof_values[i](j));
496template <
typename OutputType>
503 vectorTagDofValue(state);
510 return _nodal_value_old;
513 return _nodal_value_older;
516 return _nodal_value_previous_nl;
525 mooseError(
"Nodal values can be requested only on nodal variables, variable '",
530template <
typename OutputType>
537 vectorTagDofValue(state);
541 return _nodal_value_array;
544 return _nodal_value_old_array;
547 return _nodal_value_older_array;
550 mooseError(
"No current support for PreviousNL for nodal value array");
554 mooseError(
"Nodal values can be requested only on nodal variables, variable '",
559template <
typename OutputType>
563 bool is_transient = _subproblem.isTransient();
565 auto n = _dof_indices.size();
570 if (_need_u_dot || _need_grad_dot || _need_dof_values_dot)
572 libmesh_assert(_sys.solutionUDot());
573 _dof_values_dot.resize(n);
574 _sys.solutionUDot()->get(_dof_indices, &_dof_values_dot[0]);
576 if (_need_u_dotdot || _need_grad_dotdot || _need_dof_values_dotdot)
578 libmesh_assert(_sys.solutionUDotDot());
579 _dof_values_dotdot.resize(n);
580 _sys.solutionUDotDot()->get(_dof_indices, &_dof_values_dotdot[0]);
582 if (_need_u_dot_old || _need_dof_values_dot_old)
584 libmesh_assert(_sys.solutionUDotOld());
585 _dof_values_dot_old.resize(n);
586 _sys.solutionUDotOld()->get(_dof_indices, &_dof_values_dot_old[0]);
588 if (_need_u_dotdot_old || _need_dof_values_dotdot_old)
590 libmesh_assert(_sys.solutionUDotDotOld());
591 _dof_values_dotdot_old.resize(n);
592 _sys.solutionUDotDotOld()->get(_dof_indices, &_dof_values_dotdot_old[0]);
594 if (_need_du_dot_du || _need_dof_du_dot_du)
596 _dof_du_dot_du.resize(n);
597 for (
decltype(n) i = 0; i < n; ++i)
598 _dof_du_dot_du[i] = _sys.duDotDu(_var.number());
600 if (_need_du_dotdot_du || _need_dof_du_dotdot_du)
602 _dof_du_dotdot_du.resize(n);
603 for (
decltype(n) i = 0; i < n; ++i)
604 _dof_du_dotdot_du[i] = _sys.duDotDotDu();
608 for (
auto tag : _required_vector_tags)
609 if (_need_vector_tag_u[tag] || _need_vector_tag_grad[tag] || _need_vector_tag_dof_u[tag])
611 _subproblem.safeAccessTaggedVectors()) ||
618 if (_sys.hasVector(tag) )
620 auto & vec = _sys.getVector(tag);
621 _vector_tags_dof_u[tag].resize(n);
622 vec.get(_dof_indices, &_vector_tags_dof_u[tag][0]);
626 if (_subproblem.safeAccessTaggedMatrices())
628 auto & active_coupleable_matrix_tags =
629 _subproblem.getActiveFEVariableCoupleableMatrixTags(_tid);
631 for (
auto tag : active_coupleable_matrix_tags)
633 if (_need_matrix_tag_dof_u[tag] || _need_matrix_tag_u[tag])
634 if (_sys.hasMatrix(tag) && _sys.matrixTagActive(tag))
636 mooseAssert(_sys.getMatrix(tag).closed(),
637 "Matrix with tag '" + std::to_string(tag) +
"' should be closed");
638 auto & mat = _sys.getMatrix(tag);
639 _matrix_tags_dof_u[tag].resize(n);
640 for (
unsigned i = 0; i < n; i++)
641 _matrix_tags_dof_u[tag][i] = mat(_dof_indices[i], _dof_indices[i]);
647template <
typename OutputType>
650 const unsigned int nshapes,
653 dof_values.
resize(nshapes);
654 for (
const auto i : make_range(nshapes))
656 dof_values[i].
resize(_count);
657 for (
const auto j : make_range(_count))
658 dof_values[i](j) = sol(_dof_indices[j * nshapes + i]);
666 bool is_transient = _subproblem.isTransient();
668 auto n = _dof_indices.size() / _count;
673 if (_need_u_dot || _need_grad_dot || _need_dof_values_dot)
675 libmesh_assert(_sys.solutionUDot());
676 getArrayDofValues(*_sys.solutionUDot(), n, _dof_values_dot);
678 if (_need_u_dotdot || _need_grad_dotdot || _need_dof_values_dotdot)
680 libmesh_assert(_sys.solutionUDotDot());
681 getArrayDofValues(*_sys.solutionUDot(), n, _dof_values_dotdot);
683 if (_need_u_dot_old || _need_dof_values_dot_old)
685 libmesh_assert(_sys.solutionUDotOld());
686 getArrayDofValues(*_sys.solutionUDotOld(), n, _dof_values_dot_old);
688 if (_need_u_dotdot_old || _need_dof_values_dotdot_old)
690 libmesh_assert(_sys.solutionUDotDotOld());
691 getArrayDofValues(*_sys.solutionUDotDotOld(), n, _dof_values_dotdot_old);
695 for (
auto tag : _required_vector_tags)
697 _subproblem.safeAccessTaggedVectors()) ||
700 if (_sys.hasVector(tag))
702 mooseAssert(_sys.getVector(tag).closed(),
703 "Vector with tag '" + std::to_string(tag) +
"' should be closed");
704 getArrayDofValues(_sys.getVector(tag), n, _vector_tags_dof_u[tag]);
707 if (_subproblem.safeAccessTaggedMatrices())
709 auto & active_coupleable_matrix_tags =
710 _subproblem.getActiveFEVariableCoupleableMatrixTags(_tid);
711 for (
auto tag : active_coupleable_matrix_tags)
713 _matrix_tags_dof_u[tag].resize(n);
714 if (_need_matrix_tag_dof_u[tag] || _need_matrix_tag_u[tag])
715 if (_sys.hasMatrix(tag) && _sys.matrixTagActive(tag))
717 mooseAssert(_sys.getMatrix(tag).closed(),
718 "Matrix with tag '" + std::to_string(tag) +
"' should be closed");
719 auto & mat = _sys.getMatrix(tag);
720 for (
unsigned i = 0; i < n; i++)
721 for (
unsigned j = 0; j < _count; j++)
722 _matrix_tags_dof_u[tag][i](j) = mat(_dof_indices[i] + j, _dof_indices[i] + j);
727 if (_need_du_dot_du || _need_dof_du_dot_du)
729 _dof_du_dot_du.resize(n);
730 for (
decltype(n) i = 0; i < n; ++i)
731 _dof_du_dot_du[i] = _sys.duDotDu(_var.number());
733 if (_need_du_dotdot_du || _need_dof_du_dotdot_du)
735 _dof_du_dotdot_du.resize(n);
736 for (
decltype(n) i = 0; i < n; ++i)
737 _dof_du_dotdot_du[i] = _sys.duDotDotDu();
741template <
typename OutputType>
745 if (_subproblem.isTransient())
747 _dof_values_dot.resize(0);
748 _dof_values_dotdot.resize(0);
749 _dof_values_dot_old.resize(0);
750 _dof_values_dotdot_old.resize(0);
751 _dof_du_dot_du.resize(0);
752 _dof_du_dotdot_du.resize(0);
755 for (
auto & dof_values : _vector_tags_dof_u)
756 dof_values.resize(0);
758 _has_dof_values =
false;
761template <
typename OutputType>
765 bool is_transient = _subproblem.isTransient();
766 libmesh_assert(_dof_indices.size());
768 auto & dof_values = _vector_tags_dof_u[_solution_tag];
769 _nodal_value = dof_values[0];
770 _nodal_value_array[0] = _nodal_value;
774 if (oldestSolutionStateRequested() >= 1)
776 _nodal_value_old = _vector_tags_dof_u[_old_solution_tag][0];
777 _nodal_value_old_array[0] = _nodal_value_old;
779 if (oldestSolutionStateRequested() >= 2)
781 _nodal_value_older = _vector_tags_dof_u[_older_solution_tag][0];
782 _nodal_value_older_array[0] = _nodal_value_older;
784 if (_need_dof_values_dot)
785 _nodal_value_dot = _dof_values_dot[0];
786 if (_need_dof_values_dotdot)
787 _nodal_value_dotdot = _dof_values_dotdot[0];
788 if (_need_dof_values_dot_old)
789 _nodal_value_dot_old = _dof_values_dot_old[0];
790 if (_need_dof_values_dotdot_old)
791 _nodal_value_dotdot_old = _dof_values_dotdot_old[0];
794 _nodal_value_previous_nl = _vector_tags_dof_u[_previous_nl_solution_tag][0];
801 bool is_transient = _subproblem.isTransient();
803 auto n = _dof_indices.size();
806 auto & dof_values = _vector_tags_dof_u[_solution_tag];
807 for (
decltype(n) i = 0; i < n; ++i)
808 _nodal_value(i) = dof_values[i];
809 _nodal_value_array[0] = _nodal_value;
813 if (oldestSolutionStateRequested() >= 1)
815 auto & dof_values_old = _vector_tags_dof_u[_old_solution_tag];
816 for (
decltype(n) i = 0; i < n; ++i)
817 _nodal_value_old(i) = dof_values_old[i];
818 _nodal_value_old_array[0] = _nodal_value_old;
820 if (oldestSolutionStateRequested() >= 2)
822 auto & dof_values_older = _vector_tags_dof_u[_older_solution_tag];
823 for (
decltype(n) i = 0; i < n; ++i)
824 _nodal_value_older(i) = dof_values_older[i];
825 _nodal_value_older_array[0] = _nodal_value_older;
827 if (_need_dof_values_dot)
828 for (
decltype(n) i = 0; i < n; ++i)
829 _nodal_value_dot(i) = _dof_values_dot[i];
830 if (_need_dof_values_dotdot)
831 for (
decltype(n) i = 0; i < n; ++i)
832 _nodal_value_dotdot(i) = _dof_values_dotdot[i];
833 if (_need_dof_values_dot_old)
834 for (
decltype(n) i = 0; i < n; ++i)
835 _nodal_value_dot_old(i) = _dof_values_dot_old[i];
836 if (_need_dof_values_dotdot_old)
837 for (
decltype(n) i = 0; i < n; ++i)
838 _nodal_value_dotdot_old(i) = _dof_values_dotdot_old[i];
842 auto & dof_values_previous_nl = _vector_tags_dof_u[_previous_nl_solution_tag];
843 for (
decltype(n) i = 0; i < n; ++i)
844 _nodal_value_previous_nl(i) = dof_values_previous_nl[i];
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
void resize(unsigned int size)
Change the number of elements the array can store.
MooseVariableDataBase(const MooseVariableField< OutputType > &var, SystemBase &sys, THREAD_ID tid)
void resizeVectorTagData(TagID tag)
resize the vector tag need flags and data containers to accomodate this tag index
std::vector< FieldVariableValue > _vector_tag_u
const FieldVariableValue & vectorTagValue(TagID tag) const
void insert(libMesh::NumericVector< libMesh::Number > &residual)
Set the current local DOF values to the input vector.
const DofValues & nodalMatrixTagValue(TagID tag) const
const DofValues & dofValuesOlder() const
void fetchDofValues()
Helper methods for assigning dof values from their corresponding solution values.
unsigned int oldestSolutionStateRequested() const
The oldest solution state that is requested for this variable (0 = current, 1 = old,...
const OutputType & nodalValue(Moose::SolutionState state) const
std::vector< DofValues > _vector_tags_dof_u
const MooseArray< OutputType > & nodalValueArray(Moose::SolutionState state) const
const FieldVariableValue & sln(Moose::SolutionState state) const
Local solution getter.
const DofValues & nodalVectorTagValue(TagID tag) const
const DofValues & dofValuesPreviousNL() const
const FieldVariableGradient & gradSln(Moose::SolutionState state) const
Local solution gradient getter.
std::vector< bool > _need_vector_tag_u
const FieldVariableGradient & vectorTagGradient(TagID tag) const
MooseArray< OutputType > _nodal_value_old_array
std::vector< FieldVariableGradient > _vector_tag_grad
const DofValues & vectorTagDofValue(TagID tag) const
const DofValues & dofValuesOld() const
void getArrayDofValues(const libMesh::NumericVector< libMesh::Number > &sol, unsigned int n, MooseArray< RealEigenVector > &dof_values) const
void insertSolutionTag(TagID tag_id)
insert a solution tag into our tag containers
MooseArray< OutputType > _nodal_value_array
Nodal values as MooseArrays for use with AuxKernels.
void needSolutionState(unsigned int state)
Request that we have at least state number of older solution states/vectors.
void setNodalValue(const OutputType &value)
Set nodal value.
const DofValues & dofValues() const
void sizeMatrixTagData()
size matrix tag data
std::vector< bool > _need_vector_tag_dof_u
const FieldVariableValue & matrixTagValue(TagID tag) const
const SubProblem & _subproblem
The subproblem which we can query for information related to tagged vectors and matrices.
MooseArray< OutputType > _nodal_value_older_array
void add(libMesh::NumericVector< libMesh::Number > &residual)
Add the current local DOF values to the input vector.
const ReturnType & stateToTagHelper(Moose::SolutionState state, Functor functor)
Helper method that converts a SolutionState argument into a corresponding tag ID, potentially request...
std::vector< bool > _need_vector_tag_grad
Class for stuff related to variables.
virtual TagID getVectorTagID(const TagName &tag_name) const
Get a TagID from a TagName.
virtual unsigned int numVectorTags(const Moose::VectorTagType type=Moose::VECTOR_TAG_ANY) const
The total number of tags, which can be limited to the tag type.
Base class for a system (of equations)
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const TagID INVALID_TAG_ID
const TagName OLDER_SOLUTION_TAG
const TagName PREVIOUS_NL_SOLUTION_TAG
const TagName OLD_SOLUTION_TAG
const TagName SOLUTION_TAG