www.mooseframework.org
SystemBase.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "MooseApp.h"
11 #include "SystemBase.h"
12 #include "Factory.h"
13 #include "SubProblem.h"
14 #include "MooseVariableFE.h"
15 #include "MooseVariableScalar.h"
17 #include "Conversion.h"
18 #include "Parser.h"
20 #include "MooseTypes.h"
21 #include "InitialCondition.h"
22 #include "ScalarInitialCondition.h"
23 #include "Assembly.h"
24 #include "MooseMesh.h"
25 #include "MooseUtils.h"
26 
27 #include "libmesh/dof_map.h"
28 
30 void
31 extraSendList(std::vector<dof_id_type> & send_list, void * context)
32 {
33  SystemBase * sys = static_cast<SystemBase *>(context);
34  sys->augmentSendList(send_list);
35 }
36 
38 void
39 extraSparsity(SparsityPattern::Graph & sparsity,
40  std::vector<dof_id_type> & n_nz,
41  std::vector<dof_id_type> & n_oz,
42  void * context)
43 {
44  SystemBase * sys = static_cast<SystemBase *>(context);
45  sys->augmentSparsity(sparsity, n_nz, n_oz);
46 }
47 
48 template <>
49 void
50 dataStore(std::ostream & stream, SystemBase & system_base, void * context)
51 {
52  System & libmesh_system = system_base.system();
53 
54  NumericVector<Real> & solution = *(libmesh_system.solution.get());
55 
56  dataStore(stream, solution, context);
57 
58  for (System::vectors_iterator it = libmesh_system.vectors_begin();
59  it != libmesh_system.vectors_end();
60  it++)
61  dataStore(stream, *(it->second), context);
62 }
63 
64 template <>
65 void
66 dataLoad(std::istream & stream, SystemBase & system_base, void * context)
67 {
68  System & libmesh_system = system_base.system();
69 
70  NumericVector<Real> & solution = *(libmesh_system.solution.get());
71 
72  dataLoad(stream, solution, context);
73 
74  for (System::vectors_iterator it = libmesh_system.vectors_begin();
75  it != libmesh_system.vectors_end();
76  it++)
77  dataLoad(stream, *(it->second), context);
78 
79  system_base.update();
80 }
81 
83  const std::string & name,
84  Moose::VarKindType var_kind)
85  : libMesh::ParallelObject(subproblem),
86  _subproblem(subproblem),
87  _app(subproblem.getMooseApp()),
88  _factory(_app.getFactory()),
89  _mesh(subproblem.mesh()),
90  _name(name),
91  _vars(libMesh::n_threads()),
92  _var_map(),
93  _saved_old(NULL),
94  _saved_older(NULL),
95  _saved_dot_old(NULL),
96  _saved_dotdot_old(NULL),
97  _var_kind(var_kind),
98  _max_var_n_dofs_per_elem(0),
99  _max_var_n_dofs_per_node(0),
100  _time_integrator(nullptr)
101 {
102 }
103 
105 SystemBase::getVariable(THREAD_ID tid, const std::string & var_name)
106 {
107  MooseVariableFEBase * var = dynamic_cast<MooseVariableFEBase *>(_vars[tid].getVariable(var_name));
108  if (!var)
109  {
110  std::stringstream errMsg;
111  errMsg << "Variable '" << var_name << "' does not exist in this system" << std::endl;
112  throw std::runtime_error(errMsg.str().c_str());
113  // mooseError("Variable '" + var_name + "' does not exist in this system");
114  }
115  return *var;
116 }
117 
119 SystemBase::getVariable(THREAD_ID tid, unsigned int var_number)
120 {
121  MooseVariableFEBase * var =
122  dynamic_cast<MooseVariableFEBase *>(_vars[tid].getVariable(var_number));
123  if (!var)
124  {
125  std::stringstream errMsg;
126  errMsg << "Variable '" << Moose::stringify(var_number) << "' does not exist in this system"
127  << std::endl;
128  throw std::runtime_error(errMsg.str().c_str());
129  // mooseError("variable #" + Moose::stringify(var_number) + " does not exist in this system");
130  }
131  return *var;
132 }
133 
134 template <typename T>
136 SystemBase::getFieldVariable(THREAD_ID tid, const std::string & var_name)
137 {
138  return *_vars[tid].getFieldVariable<T>(var_name);
139 }
140 
141 template <typename T>
143 SystemBase::getFieldVariable(THREAD_ID tid, unsigned int var_number)
144 {
145  return *_vars[tid].getFieldVariable<T>(var_number);
146 }
147 
149 SystemBase::getScalarVariable(THREAD_ID tid, const std::string & var_name)
150 {
151  MooseVariableScalar * var = dynamic_cast<MooseVariableScalar *>(_vars[tid].getVariable(var_name));
152  if (!var)
153  mooseError("Scalar variable '" + var_name + "' does not exist in this system");
154  return *var;
155 }
156 
158 SystemBase::getScalarVariable(THREAD_ID tid, unsigned int var_number)
159 {
160  MooseVariableScalar * var =
161  dynamic_cast<MooseVariableScalar *>(_vars[tid].getVariable(var_number));
162  if (!var)
163  mooseError("variable #" + Moose::stringify(var_number) + " does not exist in this system");
164  return *var;
165 }
166 
167 const std::set<SubdomainID> *
168 SystemBase::getVariableBlocks(unsigned int var_number)
169 {
170  mooseAssert(_var_map.find(var_number) != _var_map.end(), "Variable does not exist.");
171  if (_var_map[var_number].empty())
172  return nullptr;
173  else
174  return &_var_map[var_number];
175 }
176 
177 void
179 {
180  _vars_to_be_zeroed_on_residual.push_back(var_name);
181 }
182 
183 void
185 {
186  _vars_to_be_zeroed_on_jacobian.push_back(var_name);
187 }
188 
189 void
190 SystemBase::zeroVariables(std::vector<std::string> & vars_to_be_zeroed)
191 {
192  if (vars_to_be_zeroed.size() > 0)
193  {
194  NumericVector<Number> & solution = this->solution();
195 
196  AllLocalDofIndicesThread aldit(system(), vars_to_be_zeroed);
197  ConstElemRange & elem_range = *_mesh.getActiveLocalElementRange();
198  Threads::parallel_reduce(elem_range, aldit);
199 
200  const std::set<dof_id_type> & dof_indices_to_zero = aldit._all_dof_indices;
201 
202  solution.close();
203 
204  for (const auto & dof : dof_indices_to_zero)
205  solution.set(dof, 0);
206 
207  solution.close();
208 
209  // Call update to update the current_local_solution for this system
210  system().update();
211  }
212 }
213 
214 void
216 {
218 }
219 
220 void
222 {
224 }
225 
226 Order
228 {
229  Order order = CONSTANT;
230  const std::vector<MooseVariableFEBase *> & vars = _vars[0].fieldVariables();
231  for (const auto & var : vars)
232  {
233  FEType fe_type = var->feType();
234  if (fe_type.default_quadrature_order() > order)
235  order = fe_type.default_quadrature_order();
236  }
237 
238  return order;
239 }
240 
241 void
243 {
245  {
246  const std::set<MooseVariableFEBase *> & active_elemental_moose_variables =
248  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
249  for (const auto & var : vars)
250  var->clearDofIndices();
251 
252  for (const auto & var : active_elemental_moose_variables)
253  if (&(var->sys()) == this)
254  var->prepare();
255  }
256  else
257  {
258  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
259  for (const auto & var : vars)
260  var->prepare();
261  }
262 }
263 
264 void
265 SystemBase::prepareFace(THREAD_ID tid, bool resize_data)
266 {
267  // We only need to do something if the element prepare was restricted
269  {
270  const std::set<MooseVariableFEBase *> & active_elemental_moose_variables =
272 
273  std::vector<MooseVariableFEBase *> newly_prepared_vars;
274 
275  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
276  for (const auto & var : vars)
277  {
278  // If it wasn't in the active list, we need to prepare it
279  if (&(var->sys()) == this && !active_elemental_moose_variables.count(var))
280  {
281  var->prepare();
282  newly_prepared_vars.push_back(var);
283  }
284  }
285 
286  // Make sure to resize the residual and jacobian datastructures for all the new variables
287  if (resize_data)
288  for (const auto var_ptr : newly_prepared_vars)
289  {
290  _subproblem.assembly(tid).prepareVariable(var_ptr);
293  }
294  }
295 }
296 
297 void
299 {
300  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
301  for (const auto & var : vars)
302  var->prepareNeighbor();
303 }
304 
305 void
307 {
308  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
309  for (const auto & var : vars)
310  var->prepareLowerD();
311 }
312 
313 void
314 SystemBase::reinitElem(const Elem * /*elem*/, THREAD_ID tid)
315 {
316 
318  {
319  const std::set<MooseVariableFEBase *> & active_elemental_moose_variables =
321  for (const auto & var : active_elemental_moose_variables)
322  if (&(var->sys()) == this)
323  var->computeElemValues();
324  }
325  else
326  {
327  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
328  for (const auto & var : vars)
329  var->computeElemValues();
330  }
331 }
332 
333 void
334 SystemBase::reinitElemFace(const Elem * /*elem*/,
335  unsigned int /*side*/,
336  BoundaryID /*bnd_id*/,
337  THREAD_ID tid)
338 {
339  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
340  for (const auto & var : vars)
341  var->computeElemValuesFace();
342 }
343 
344 void
345 SystemBase::reinitNeighborFace(const Elem * /*elem*/,
346  unsigned int /*side*/,
347  BoundaryID /*bnd_id*/,
348  THREAD_ID tid)
349 {
350  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
351  for (const auto & var : vars)
352  var->computeNeighborValuesFace();
353 }
354 
355 void
356 SystemBase::reinitNeighbor(const Elem * /*elem*/, THREAD_ID tid)
357 {
358  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
359  for (const auto & var : vars)
360  var->computeNeighborValues();
361 }
362 
363 void
365 {
366  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
367  for (const auto & var : vars)
368  var->computeLowerDValues();
369 }
370 
371 void
372 SystemBase::reinitNode(const Node * /*node*/, THREAD_ID tid)
373 {
374  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
375  for (const auto & var : vars)
376  {
377  if (var->isNodal())
378  {
379  var->reinitNode();
380  var->computeNodalValues();
381  }
382  }
383 }
384 
385 void
386 SystemBase::reinitNodeFace(const Node * /*node*/, BoundaryID /*bnd_id*/, THREAD_ID tid)
387 {
388  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
389  for (const auto & var : vars)
390  {
391  if (var->isNodal())
392  {
393  var->reinitNode();
394  var->computeNodalValues();
395  }
396  }
397 }
398 
399 void
400 SystemBase::reinitNodes(const std::vector<dof_id_type> & nodes, THREAD_ID tid)
401 {
402  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
403  for (const auto & var : vars)
404  {
405  var->reinitNodes(nodes);
406  var->computeNodalValues();
407  }
408 }
409 
410 void
411 SystemBase::reinitNodesNeighbor(const std::vector<dof_id_type> & nodes, THREAD_ID tid)
412 {
413  const std::vector<MooseVariableFEBase *> & vars = _vars[tid].fieldVariables();
414  for (const auto & var : vars)
415  {
416  var->reinitNodesNeighbor(nodes);
417  var->computeNodalNeighborValues();
418  }
419 }
420 
421 void
423 {
424  const std::vector<MooseVariableScalar *> & vars = _vars[tid].scalars();
425  for (const auto & var : vars)
426  var->reinit();
427 }
428 
429 void
430 SystemBase::augmentSendList(std::vector<dof_id_type> & send_list)
431 {
432  std::set<dof_id_type> & ghosted_elems = _subproblem.ghostedElems();
433 
434  DofMap & dof_map = dofMap();
435 
436  std::vector<dof_id_type> dof_indices;
437 
438  System & sys = system();
439 
440  unsigned int sys_num = sys.number();
441 
442  unsigned int n_vars = sys.n_vars();
443 
444  for (const auto & elem_id : ghosted_elems)
445  {
446  Elem * elem = _mesh.elemPtr(elem_id);
447 
448  if (elem->active())
449  {
450  dof_map.dof_indices(elem, dof_indices);
451 
452  // Only need to ghost it if it's actually not on this processor
453  for (const auto & dof : dof_indices)
454  if (dof < dof_map.first_dof() || dof >= dof_map.end_dof())
455  send_list.push_back(dof);
456 
457  // Now add the DoFs from all of the nodes. This is necessary because of block
458  // restricted variables. A variable might not live _on_ this element but it
459  // might live on nodes connected to this element.
460  for (unsigned int n = 0; n < elem->n_nodes(); n++)
461  {
462  Node * node = elem->node_ptr(n);
463 
464  // Have to get each variable's dofs
465  for (unsigned int v = 0; v < n_vars; v++)
466  {
467  const Variable & var = sys.variable(v);
468  unsigned int var_num = var.number();
469  unsigned int n_comp = var.n_components();
470 
471  // See if this variable has any dofs at this node
472  if (node->n_dofs(sys_num, var_num) > 0)
473  {
474  // Loop over components of the variable
475  for (unsigned int c = 0; c < n_comp; c++)
476  send_list.push_back(node->dof_number(sys_num, var_num, c));
477  }
478  }
479  }
480  }
481  }
482 }
483 
487 void
489 {
490  if (!_saved_old)
491  _saved_old = &addVector("save_solution_old", false, PARALLEL);
492  if (!_saved_older)
493  _saved_older = &addVector("save_solution_older", false, PARALLEL);
495  _saved_dot_old = &addVector("save_solution_dot_old", false, PARALLEL);
497  _saved_dotdot_old = &addVector("save_solution_dotdot_old", false, PARALLEL);
498 
499  *_saved_old = solutionOld();
501 
502  if (solutionUDotOld())
504 
505  if (solutionUDotDotOld())
507 }
508 
512 void
514 {
515  if (_saved_old)
516  {
517  solutionOld() = *_saved_old;
518  removeVector("save_solution_old");
519  _saved_old = nullptr;
520  }
521  if (_saved_older)
522  {
524  removeVector("save_solution_older");
525  _saved_older = nullptr;
526  }
528  {
530  removeVector("save_solution_dot_old");
531  _saved_dot_old = NULL;
532  }
534  {
536  removeVector("save_solution_dotdot_old");
537  _saved_dotdot_old = NULL;
538  }
539 }
540 
541 NumericVector<Number> &
542 SystemBase::addVector(const std::string & vector_name, const bool project, const ParallelType type)
543 {
544  if (hasVector(vector_name))
545  return getVector(vector_name);
546 
547  NumericVector<Number> & vec = system().add_vector(vector_name, project, type);
548  return vec;
549 }
550 
551 NumericVector<Number> &
552 SystemBase::addVector(TagID tag, const bool project, const ParallelType type)
553 {
554  if (!_subproblem.vectorTagExists(tag))
555  mooseError("Cannot add a tagged vector with vector_tag, ",
556  tag,
557  ", that tag does not exist in System ",
558  name());
559 
560  if (hasVector(tag))
561  return getVector(tag);
562 
563  auto vector_name = _subproblem.vectorTagName(tag);
564 
565  NumericVector<Number> & vec = system().add_vector(vector_name, project, type);
566 
567  if (_tagged_vectors.size() < tag + 1)
568  _tagged_vectors.resize(tag + 1);
569 
570  _tagged_vectors[tag] = &vec;
571 
572  return vec;
573 }
574 
575 void
576 SystemBase::closeTaggedVectors(const std::set<TagID> & tags)
577 {
578  for (auto & tag : tags)
579  {
580  mooseAssert(_subproblem.vectorTagExists(tag), "Tag: " << tag << " does not exsit");
581  getVector(tag).close();
582  }
583 }
584 
585 void
586 SystemBase::zeroTaggedVectors(const std::set<TagID> & tags)
587 {
588  for (auto & tag : tags)
589  {
590  mooseAssert(_subproblem.vectorTagExists(tag), "Tag: " << tag << " does not exsit");
591  getVector(tag).zero();
592  }
593 }
594 
595 void
597 {
598  if (!_subproblem.vectorTagExists(tag_id))
599  mooseError("Cannot remove an unexisting tag or its associated vector, ",
600  tag_id,
601  ", that tag does not exist in System ",
602  name());
603 
604  if (hasVector(tag_id))
605  {
606  auto vector_name = _subproblem.vectorTagName(tag_id);
607  system().remove_vector(vector_name);
608  _tagged_vectors[tag_id] = nullptr;
609  }
610 }
611 
612 void
613 SystemBase::addVariable(const std::string & var_name,
614  const FEType & type,
615  Real scale_factor,
616  const std::set<SubdomainID> * const active_subdomains)
617 {
618  unsigned int var_num = system().add_variable(var_name, type, active_subdomains);
619  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
620  {
621  // FIXME: we cannot refer fetype in libMesh at this point, so we will just make a copy in
622  // MooseVariableBase.
623  MooseVariableBase * var;
624  if (type == FEType(0, MONOMIAL))
625  var = new MooseVariableConstMonomial(
626  var_num, type, *this, _subproblem.assembly(tid), _var_kind, tid);
627  else if (type == FEType(FIRST, NEDELEC_ONE) || type.family == LAGRANGE_VEC)
628  var =
629  new VectorMooseVariable(var_num, type, *this, _subproblem.assembly(tid), _var_kind, tid);
630  else
631  var = new MooseVariable(var_num, type, *this, _subproblem.assembly(tid), _var_kind, tid);
632 
633  var->scalingFactor(scale_factor);
634  _vars[tid].add(var_name, var);
635  }
636  if (active_subdomains == nullptr)
637  _var_map[var_num] = std::set<SubdomainID>();
638  else
639  for (const auto subdomain_id : *active_subdomains)
640  _var_map[var_num].insert(subdomain_id);
641 }
642 
643 void
644 SystemBase::addScalarVariable(const std::string & var_name,
645  Order order,
646  Real scale_factor,
647  const std::set<SubdomainID> * const active_subdomains)
648 {
649  FEType type(order, SCALAR);
650  unsigned int var_num = system().add_variable(var_name, type, active_subdomains);
651  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
652  {
653  // FIXME: we cannot refer fetype in libMesh at this point, so we will just make a copy in
654  // MooseVariableBase.
655  MooseVariableScalar * var =
656  new MooseVariableScalar(var_num, type, *this, _subproblem.assembly(tid), _var_kind, tid);
657  var->scalingFactor(scale_factor);
658  _vars[tid].add(var_name, var);
659  }
660  if (active_subdomains == nullptr)
661  _var_map[var_num] = std::set<SubdomainID>();
662  else
663  for (const auto subdomain_id : *active_subdomains)
664  _var_map[var_num].insert(subdomain_id);
665 }
666 
667 bool
668 SystemBase::hasVariable(const std::string & var_name) const
669 {
670  if (system().has_variable(var_name))
671  return system().variable_type(var_name).family != SCALAR;
672  else
673  return false;
674 }
675 
676 bool
677 SystemBase::hasScalarVariable(const std::string & var_name) const
678 {
679  if (system().has_variable(var_name))
680  return system().variable_type(var_name).family == SCALAR;
681  else
682  return false;
683 }
684 
685 bool
686 SystemBase::isScalarVariable(unsigned int var_num) const
687 {
688  return (system().variable(var_num).type().family == SCALAR);
689 }
690 
691 unsigned int
693 {
694  return _vars[0].names().size();
695 }
696 
700 bool
701 SystemBase::hasVector(const std::string & name) const
702 {
703  return system().have_vector(name);
704 }
705 
706 TagID
708 {
709  mooseError("Not implemented yet");
710  return 0;
711 }
712 
713 TagID
715 {
716  mooseError("Not implemented yet");
717  return 0;
718 }
719 
720 TagID
722 {
723  mooseError("Not implemented yet");
724  return 0;
725 }
726 
727 TagID
729 {
730  mooseError("Not implemented yet");
731  return 0;
732 }
733 
734 TagID
736 {
737  mooseError("Not implemented yet");
738  return 0;
739 }
740 
741 bool
743 {
744  return tag < _tagged_vectors.size() && _tagged_vectors[tag];
745 }
746 
750 NumericVector<Number> &
751 SystemBase::getVector(const std::string & name)
752 {
753  return system().get_vector(name);
754 }
755 
756 NumericVector<Number> &
758 {
759  mooseAssert(hasVector(tag), "Cannot retrieve vector with residual_tag: " << tag);
760 
761  return *_tagged_vectors[tag];
762 }
763 
764 const NumericVector<Number> &
766 {
767  mooseAssert(hasVector(tag), "Cannot retrieve vector with residual_tag: " << tag);
768 
769  return *_tagged_vectors[tag];
770 }
771 
772 void
773 SystemBase::associateVectorToTag(NumericVector<Number> & vec, TagID tag)
774 {
775  mooseAssert(_subproblem.vectorTagExists(tag),
776  "You can't associate a tag that does not exist " << tag);
777  if (_tagged_vectors.size() < tag + 1)
778  _tagged_vectors.resize(tag + 1);
779 
780  _tagged_vectors[tag] = &vec;
781 }
782 
783 void
784 SystemBase::disassociateVectorFromTag(NumericVector<Number> & vec, TagID tag)
785 {
786  mooseAssert(_subproblem.vectorTagExists(tag),
787  "You can't associate a tag that does not exist " << tag);
788  if (_tagged_vectors.size() < tag + 1)
789  _tagged_vectors.resize(tag + 1);
790 
791  if (_tagged_vectors[tag] != &vec)
792  mooseError("You can not disassociate a vector from a tag which it was not associated to");
793 
794  _tagged_vectors[tag] = nullptr;
795 }
796 
797 void
799 {
800  for (auto & tagged_vector : _tagged_vectors)
801  tagged_vector = nullptr;
802 }
803 
804 bool
806 {
807  return tag < _tagged_matrices.size() && _tagged_matrices[tag];
808 }
809 
810 SparseMatrix<Number> &
812 {
813  mooseAssert(hasMatrix(tag), "Cannot retrieve matrix with matrix_tag: " << tag);
814 
815  return *_tagged_matrices[tag];
816 }
817 
818 const SparseMatrix<Number> &
820 {
821  mooseAssert(hasMatrix(tag), "Cannot retrieve matrix with matrix_tag: " << tag);
822 
823  return *_tagged_matrices[tag];
824 }
825 
826 void
827 SystemBase::closeTaggedMatrices(const std::set<TagID> & tags)
828 {
829  for (auto tag : tags)
830  if (hasMatrix(tag))
831  getMatrix(tag).close();
832 }
833 
834 void
835 SystemBase::associateMatrixToTag(SparseMatrix<Number> & matrix, TagID tag)
836 {
837  mooseAssert(_subproblem.matrixTagExists(tag),
838  "Cannot associate Matrix with matrix_tag : " << tag << "that does not exist");
839 
840  if (_tagged_matrices.size() < tag + 1)
841  _tagged_matrices.resize(tag + 1);
842 
843  _tagged_matrices[tag] = &matrix;
844 }
845 
846 void
847 SystemBase::disassociateMatrixFromTag(SparseMatrix<Number> & matrix, TagID tag)
848 {
849  mooseAssert(_subproblem.matrixTagExists(tag),
850  "Cannot disassociate Matrix with matrix_tag : " << tag << "that does not exist");
851 
852  if (_tagged_matrices.size() < tag + 1)
853  _tagged_matrices.resize(tag + 1);
854 
855  if (_tagged_matrices[tag] != &matrix)
856  mooseError("You can not disassociate a matrix from a tag which it was not associated to");
857 
858  _tagged_matrices[tag] = nullptr;
859 }
860 
861 void
863 {
864  mooseAssert(_subproblem.matrixTagExists(tag),
865  "Cannot active Matrix with matrix_tag : " << tag << "that does not exist");
866 
867  if (_matrix_tag_active_flags.size() < tag + 1)
868  _matrix_tag_active_flags.resize(tag + 1);
869 
870  _matrix_tag_active_flags[tag] = true;
871 }
872 
873 void
875 {
876  mooseAssert(_subproblem.matrixTagExists(tag),
877  "Cannot deactivate Matrix with matrix_tag : " << tag << "that does not exist");
878 
879  if (_matrix_tag_active_flags.size() < tag + 1)
880  _matrix_tag_active_flags.resize(tag + 1);
881 
882  _matrix_tag_active_flags[tag] = false;
883 }
884 
885 void
887 {
888  auto num_matrix_tags = _subproblem.numMatrixTags();
889 
890  _matrix_tag_active_flags.resize(num_matrix_tags);
891 
892  for (decltype(num_matrix_tags) tag = 0; tag < num_matrix_tags; tag++)
893  _matrix_tag_active_flags[tag] = false;
894 }
895 
896 void
898 {
899  auto num_matrix_tags = _subproblem.numMatrixTags();
900 
901  _matrix_tag_active_flags.resize(num_matrix_tags);
902 
903  for (decltype(num_matrix_tags) tag = 0; tag < num_matrix_tags; tag++)
904  if (hasMatrix(tag))
905  _matrix_tag_active_flags[tag] = true;
906  else
907  _matrix_tag_active_flags[tag] = false;
908 }
909 
910 bool
912 {
913  mooseAssert(_subproblem.matrixTagExists(tag), "Matrix tag " << tag << " does not exist");
914 
915  return tag < _matrix_tag_active_flags.size() && _matrix_tag_active_flags[tag];
916 }
917 
918 void
920 {
921  for (auto & matrix : _tagged_matrices)
922  matrix = nullptr;
923 }
924 
925 unsigned int
927 {
928  return system().number();
929 }
930 
931 DofMap &
933 {
934  return system().get_dof_map();
935 }
936 
937 const DofMap &
939 {
940  return system().get_dof_map();
941 }
942 
943 void
944 SystemBase::addVariableToCopy(const std::string & dest_name,
945  const std::string & source_name,
946  const std::string & timestep)
947 {
948  _var_to_copy.push_back(VarCopyInfo(dest_name, source_name, timestep));
949 }
950 
951 void
952 SystemBase::copyVars(ExodusII_IO & io)
953 {
954  int n_steps = io.get_num_time_steps();
955 
956  bool did_copy = false;
957  for (const auto & vci : _var_to_copy)
958  {
959  int timestep = -1;
960 
961  if (vci._timestep == "LATEST")
962  // Use the last time step in the file from which to retrieve the solution
963  timestep = n_steps;
964  else
965  {
966  timestep = MooseUtils::convert<int>(vci._timestep);
967  if (timestep > n_steps)
968  mooseError("Invalid value passed as \"initial_from_file_timestep\". Expected \"LATEST\" or "
969  "a valid integer between 1 and ",
970  n_steps,
971  " inclusive, received ",
972  vci._timestep);
973  }
974 
975  did_copy = true;
976 
977  if (hasVariable(vci._dest_name))
978  {
979  if (getVariable(0, vci._dest_name).isNodal())
980  io.copy_nodal_solution(system(), vci._dest_name, vci._source_name, timestep);
981 
982  else
983  io.copy_elemental_solution(system(), vci._dest_name, vci._source_name, timestep);
984  }
985  else if (hasScalarVariable(vci._dest_name))
986  {
987  auto rank = comm().rank();
988  auto size = comm().size();
989 
990  // Read solution on rank 0 only and send data to rank "size - 1" where scalar DOFs are
991  // stored
992  std::vector<Real> global_values;
993  if (rank == 0)
994  {
995  // Read the scalar value then set that value in the current solution
996  io.read_global_variable({vci._source_name}, timestep, global_values);
997  if (size > 1)
998  comm().send(size - 1, global_values);
999  }
1000  if (rank == size - 1)
1001  {
1002  if (size > 1)
1003  comm().receive(0, global_values);
1004  const unsigned int var_num = system().variable_number(vci._dest_name);
1005  system().solution->set(var_num, global_values[0]);
1006  }
1007  }
1008  }
1009 
1010  if (did_copy)
1011  solution().close();
1012 }
1013 
1014 void
1016 {
1017 }
1018 
1019 void
1021 {
1022  system().update();
1023 }
1024 
1025 void
1027 {
1028  system().solve();
1029 }
1030 
1034 void
1036 {
1037  system().update();
1040  if (solutionUDotOld())
1041  *solutionUDotOld() = *solutionUDot();
1042  if (solutionUDotDotOld())
1044  if (solutionPreviousNewton())
1046 }
1047 
1051 void
1053 {
1054  solutionOlder() = solutionOld();
1056  if (solutionUDotOld())
1057  *solutionUDotOld() = *solutionUDot();
1058  if (solutionUDotDotOld())
1060  if (solutionPreviousNewton())
1062 }
1063 
1067 void
1069 {
1070  *(const_cast<NumericVector<Number> *&>(currentSolution())) = solutionOld();
1071  solution() = solutionOld();
1072  if (solutionUDotOld())
1073  *solutionUDot() = *solutionUDotOld();
1074  if (solutionUDotDotOld())
1076  if (solutionPreviousNewton())
1078  system().update();
1079 }
1080 
1081 void
1082 SystemBase::removeVector(const std::string & name)
1083 {
1084  system().remove_vector(name);
1085 }
1086 
1087 const std::string &
1089 {
1090  return system().name();
1091 }
1092 
1093 template MooseVariableFE<Real> & SystemBase::getFieldVariable<Real>(THREAD_ID tid,
1094  const std::string & var_name);
1095 
1097 SystemBase::getFieldVariable<RealVectorValue>(THREAD_ID tid, const std::string & var_name);
1098 
1099 template MooseVariableFE<Real> & SystemBase::getFieldVariable<Real>(THREAD_ID tid,
1100  unsigned int var_number);
1101 
1103 SystemBase::getFieldVariable<RealVectorValue>(THREAD_ID tid, unsigned int var_number);
virtual const std::set< SubdomainID > * getVariableBlocks(unsigned int var_number)
Get the block where a variable of this system is defined.
Definition: SystemBase.C:168
virtual bool hasMatrix(TagID tag) const
Check if the tagged matrix exists in the system.
Definition: SystemBase.C:805
virtual void reinitNode(const Node *node, THREAD_ID tid)
Reinit nodal assembly info.
Definition: SystemBase.C:372
virtual const NumericVector< Number > *const & currentSolution() const =0
The solution vector that is currently being operated on.
ConstElemRange * getActiveLocalElementRange()
Return pointers to range objects for various types of ranges (local nodes, boundary elems...
Definition: MooseMesh.C:737
virtual NumericVector< Number > * solutionUDot()=0
virtual void disassociateVectorFromTag(NumericVector< Number > &vec, TagID tag)
Associate a vector for a given tag.
Definition: SystemBase.C:784
virtual TagName vectorTagName(TagID tag)
Retrieve the name associated with a TagID.
Definition: SubProblem.C:106
virtual TagID systemMatrixTag()
Return the Matrix Tag ID for System.
Definition: SystemBase.C:721
virtual void reinitScalars(THREAD_ID tid)
Reinit scalar varaibles.
Definition: SystemBase.C:422
virtual void reinitNeighborFace(const Elem *elem, unsigned int side, BoundaryID bnd_id, THREAD_ID tid)
Compute the values of the variables at all the current points.
Definition: SystemBase.C:345
virtual void zeroTaggedVectors(const std::set< TagID > &tags)
Zero all vectors for given tags.
Definition: SystemBase.C:586
bool hasVector(const std::string &tag_name) const
Check if the named vector exists in the system.
Definition: SystemBase.C:701
virtual void copyOldSolutions()
Shifts the solutions backwards in time.
Definition: SystemBase.C:1052
unsigned int TagID
Definition: MooseTypes.h:162
virtual Elem * elemPtr(const dof_id_type i)
Definition: MooseMesh.C:2267
virtual void reinitElemFace(const Elem *elem, unsigned int side, BoundaryID bnd_id, THREAD_ID tid)
Reinit assembly info for a side of an element.
Definition: SystemBase.C:334
virtual NumericVector< Number > & addVector(const std::string &vector_name, const bool project, const ParallelType type)
Adds a solution length vector to the system.
Definition: SystemBase.C:542
virtual bool hasActiveElementalMooseVariables(THREAD_ID tid) const
Whether or not a list of active elemental moose variables has been set.
Definition: SubProblem.C:253
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:207
std::vector< std::string > _vars_to_be_zeroed_on_residual
Definition: SystemBase.h:749
virtual void addVariable(const std::string &var_name, const FEType &type, Real scale_factor, const std::set< SubdomainID > *const active_subdomains=NULL)
Adds a variable to the system.
Definition: SystemBase.C:613
void dataStore(std::ostream &stream, SystemBase &system_base, void *context)
IO Methods for restart, backup and restore.
Definition: SystemBase.C:50
virtual NumericVector< Number > & solutionOld()=0
std::vector< SparseMatrix< Number > * > _tagged_matrices
Tagged matrices (pointer)
Definition: SystemBase.h:758
virtual void reinitElem(const Elem *elem, THREAD_ID tid)
Reinit an element assembly info.
Definition: SystemBase.C:314
virtual void reinitLowerD(THREAD_ID tid)
Compute the values of the variables on the lower dimensional element.
Definition: SystemBase.C:364
virtual Assembly & assembly(THREAD_ID tid)=0
virtual void associateVectorToTag(NumericVector< Number > &vec, TagID tag)
Associate a vector for a given tag.
Definition: SystemBase.C:773
virtual Order getMinQuadratureOrder()
Get minimal quadrature order needed for integrating variables in this system.
Definition: SystemBase.C:227
virtual NumericVector< Number > * solutionUDotDotOld()=0
virtual void addVariableToCopy(const std::string &dest_name, const std::string &source_name, const std::string &timestep)
Add info about variable that will be copied.
Definition: SystemBase.C:944
virtual void prepareFace(THREAD_ID tid, bool resize_data)
Prepare the system for use on sides.
Definition: SystemBase.C:265
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
MooseVariableFE< Real > MooseVariable
Definition: Adaptivity.h:29
Base class for a system (of equations)
Definition: SystemBase.h:92
virtual void reinitNodes(const std::vector< dof_id_type > &nodes, THREAD_ID tid)
Reinit variables at a set of nodes.
Definition: SystemBase.C:400
virtual const std::set< MooseVariableFEBase * > & getActiveElementalMooseVariables(THREAD_ID tid) const
Get the MOOSE variables to be reinited on each element.
Definition: SubProblem.C:247
virtual void reinitNodesNeighbor(const std::vector< dof_id_type > &nodes, THREAD_ID tid)
Reinit variables at a set of neighbor nodes.
Definition: SystemBase.C:411
NumericVector< Real > * _saved_older
Definition: SystemBase.h:764
virtual TagID nonTimeVectorTag()
Definition: SystemBase.C:728
virtual NumericVector< Number > * solutionPreviousNewton()=0
virtual void update()
Update the system (doing libMesh magic)
Definition: SystemBase.C:1020
virtual bool matrixTagExists(const TagName &tag_name)
Check to see if a particular Tag exists.
Definition: SubProblem.C:129
virtual unsigned int nVariables() const
Get the number of variables in this system.
Definition: SystemBase.C:692
MooseVariableFEBase & getVariable(THREAD_ID tid, const std::string &var_name)
Gets a reference to a variable of with specified name.
Definition: SystemBase.C:105
Grab all the local dof indices for the variables passed in, in the system passed in.
virtual const std::string & name() const
Definition: SystemBase.C:1088
virtual bool isNodal() const =0
Is this variable nodal.
void closeTaggedMatrices(const std::set< TagID > &tags)
Close all matrices associated the tags.
Definition: SystemBase.C:827
virtual void deactiveAllMatrixTags()
Make matrices inactive.
Definition: SystemBase.C:886
std::vector< VarCopyInfo > _var_to_copy
Definition: SystemBase.h:773
virtual DofMap & dofMap()
Gets writeable reference to the dof map.
Definition: SystemBase.C:932
virtual TagID timeVectorTag()
Ideally, we should not need this API.
Definition: SystemBase.C:707
int convert< int >(const std::string &str, bool throw_on_failure)
Definition: MooseUtils.C:610
virtual void disassociateAllTaggedVectors()
Disassociate all vectors, and then hasVector() will return false.
Definition: SystemBase.C:798
void prepareVariableNonlocal(MooseVariableFEBase *var)
Definition: Assembly.C:2393
virtual void zeroVariablesForResidual()
Zero out the solution for the variables that were registered as needing to have their solutions zeroe...
Definition: SystemBase.C:215
std::set< dof_id_type > _all_dof_indices
virtual void disassociateAllTaggedMatrices()
Clear all tagged matrices.
Definition: SystemBase.C:919
std::vector< bool > _matrix_tag_active_flags
Active flags for tagged matrices.
Definition: SystemBase.h:760
std::vector< std::string > _vars_to_be_zeroed_on_jacobian
Definition: SystemBase.h:750
boundary_id_type BoundaryID
Information about variables that will be copied.
Definition: SystemBase.h:74
VarKindType
Framework-wide stuff.
Definition: MooseTypes.h:481
virtual void prepareNeighbor(THREAD_ID tid)
Prepare the system for use.
Definition: SystemBase.C:298
DofMap & dof_map
const std::vector< numeric_index_type > & n_nz
virtual void associateMatrixToTag(SparseMatrix< Number > &matrix, TagID tag)
associate a matirx to a tag
Definition: SystemBase.C:835
SubProblem & _subproblem
Definition: SystemBase.h:735
virtual void augmentSparsity(SparsityPattern::Graph &sparsity, std::vector< dof_id_type > &n_nz, std::vector< dof_id_type > &n_oz)=0
Will modify the sparsity pattern to add logical geometric connections.
Moose::VarKindType _var_kind
default kind of variables in this system
Definition: SystemBase.h:771
virtual bool matrixTagActive(TagID tag) const
If or not a matrix tag is active.
Definition: SystemBase.C:911
virtual bool checkNonlocalCouplingRequirement()
Definition: SubProblem.h:69
void prepareVariable(MooseVariableFEBase *var)
Used for preparing the dense residual and jacobian blocks for one particular variable.
Definition: Assembly.C:2364
virtual std::set< dof_id_type > & ghostedElems()
Return the list of elements that should have their DoFs ghosted to this processor.
Definition: SubProblem.h:561
NumericVector< Real > * _saved_dot_old
Definition: SystemBase.h:767
virtual bool vectorTagExists(TagID tag)
Check to see if a particular Tag exists.
Definition: SubProblem.h:112
virtual System & system()=0
Get the reference to the libMesh system.
virtual void activeAllMatrixTags()
Make all exsiting matrices ative.
Definition: SystemBase.C:897
virtual unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:926
std::string stringify(const T &t)
conversion to string
Definition: Conversion.h:60
void copyVars(ExodusII_IO &io)
Definition: SystemBase.C:952
virtual void closeTaggedVectors(const std::set< TagID > &tags)
Close all vectors for given tags.
Definition: SystemBase.C:576
virtual void reinitNeighbor(const Elem *elem, THREAD_ID tid)
Compute the values of the variables at all the current points.
Definition: SystemBase.C:356
void extraSendList(std::vector< dof_id_type > &send_list, void *context)
Free function used for a libMesh callback.
Definition: SystemBase.C:31
virtual bool hasVariable(const std::string &var_name) const
Query a system for a variable.
Definition: SystemBase.C:668
std::map< unsigned int, std::set< SubdomainID > > _var_map
Map of variables (variable id -> array of subdomains where it lives)
Definition: SystemBase.h:747
virtual void copySolutionsBackwards()
Copy current solution into old and older.
Definition: SystemBase.C:1035
virtual void disassociateMatrixFromTag(SparseMatrix< Number > &matrix, TagID tag)
disassociate a matirx from a tag
Definition: SystemBase.C:847
virtual void solve()
Solve the system (using libMesh magic)
Definition: SystemBase.C:1026
virtual unsigned int numMatrixTags() const
The total number of tags.
Definition: SubProblem.h:157
virtual void activeMatrixTag(TagID tag)
Active a matrix for tag.
Definition: SystemBase.C:862
virtual void saveOldSolutions()
Save the old and older solutions.
Definition: SystemBase.C:488
std::vector< VariableWarehouse > _vars
Variable warehouses (one for each thread)
Definition: SystemBase.h:745
MatType type
virtual void addExtraVectors()
Method called during initialSetup to add extra system vector if they are required by the simulation...
Definition: SystemBase.C:1015
SystemBase(SubProblem &subproblem, const std::string &name, Moose::VarKindType var_kind)
Definition: SystemBase.C:82
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:59
virtual void removeVector(const std::string &name)
Remove a vector from the system with the given name.
Definition: SystemBase.C:1082
MooseMesh & _mesh
Definition: SystemBase.h:740
virtual void addVariableToZeroOnResidual(std::string var_name)
Adds this variable to the list of variables to be zeroed during each residual evaluation.
Definition: SystemBase.C:178
MooseVariableFE< T > & getFieldVariable(THREAD_ID tid, const std::string &var_name)
Gets a reference to a variable of with specified name.
Definition: SystemBase.C:136
PetscInt n
virtual SparseMatrix< Number > & getMatrix(TagID tag)
Get a raw SparseMatrix.
Definition: SystemBase.C:811
virtual void reinitNodeFace(const Node *node, BoundaryID bnd_id, THREAD_ID tid)
Reinit nodal assembly info on a face.
Definition: SystemBase.C:386
virtual void zeroVariablesForJacobian()
Zero out the solution for the variables that were registered as needing to have their solutions zeroe...
Definition: SystemBase.C:221
NumericVector< Real > * _saved_old
Definition: SystemBase.h:763
Class for scalar variables (they are different).
virtual NumericVector< Number > & solutionOlder()=0
MPI_Comm comm
virtual NumericVector< Number > & solution()=0
NumericVector< Real > * _saved_dotdot_old
Definition: SystemBase.h:768
virtual void prepare(THREAD_ID tid)
Prepare the system for use.
Definition: SystemBase.C:242
virtual void addVariableToZeroOnJacobian(std::string var_name)
Adds this variable to the list of variables to be zeroed during each Jacobian evaluation.
Definition: SystemBase.C:184
virtual NumericVector< Number > * solutionUDotOld()=0
virtual NumericVector< Number > * solutionUDotDot()=0
virtual bool isScalarVariable(unsigned int var_name) const
Definition: SystemBase.C:686
MooseVariableFE< VectorValue< Real > > VectorMooseVariable
Definition: Adaptivity.h:31
virtual void prepareLowerD(THREAD_ID tid)
Prepare the system for use for lower dimensional elements.
Definition: SystemBase.C:306
virtual void deactiveMatrixTag(TagID tag)
deactive a matrix for tag
Definition: SystemBase.C:874
std::vector< NumericVector< Number > * > _tagged_vectors
Tagged vectors (pointer)
Definition: SystemBase.h:756
virtual bool hasScalarVariable(const std::string &var_name) const
Definition: SystemBase.C:677
virtual void augmentSendList(std::vector< dof_id_type > &send_list)
Will modify the send_list to add all of the extra ghosted dofs for this system.
Definition: SystemBase.C:430
virtual void restoreSolutions()
Restore current solutions (call after your solve failed)
Definition: SystemBase.C:1068
virtual NumericVector< Number > & getVector(const std::string &name)
Get a raw NumericVector.
Definition: SystemBase.C:751
void extraSparsity(SparsityPattern::Graph &sparsity, std::vector< dof_id_type > &n_nz, std::vector< dof_id_type > &n_oz, void *context)
Free function used for a libMesh callback.
Definition: SystemBase.C:39
void dataLoad(std::istream &stream, SystemBase &system_base, void *context)
IO Methods for restart, backup and restore.
Definition: SystemBase.C:66
virtual MooseVariableScalar & getScalarVariable(THREAD_ID tid, const std::string &var_name)
Gets a reference to a scalar variable with specified number.
Definition: SystemBase.C:149
virtual TagID residualVectorTag()
Definition: SystemBase.C:735
virtual void zeroVariables(std::vector< std::string > &vars_to_be_zeroed)
Zero out the solution for the list of variables passed in.
Definition: SystemBase.C:190
virtual void restoreOldSolutions()
Restore the old and older solutions when the saved solutions present.
Definition: SystemBase.C:513
virtual TagID timeMatrixTag()
Return the Matrix Tag ID for Time.
Definition: SystemBase.C:714
const std::vector< numeric_index_type > & n_oz
virtual void addScalarVariable(const std::string &var_name, Order order, Real scale_factor, const std::set< SubdomainID > *const active_subdomains=NULL)
Adds a scalar variable.
Definition: SystemBase.C:644
unsigned int THREAD_ID
Definition: MooseTypes.h:161
void scalingFactor(Real factor)
Set the scaling factor for this variable.