https://mooseframework.inl.gov
NonlinearSystemBase.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
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 "NonlinearSystemBase.h"
11 #include "AuxiliarySystem.h"
12 #include "Problem.h"
13 #include "FEProblem.h"
14 #include "MooseVariableFE.h"
15 #include "MooseVariableScalar.h"
16 #include "PetscSupport.h"
17 #include "Factory.h"
18 #include "ParallelUniqueId.h"
19 #include "ThreadedElementLoop.h"
20 #include "MaterialData.h"
21 #include "ComputeResidualThread.h"
23 #include "ComputeFVFluxThread.h"
24 #include "ComputeJacobianThread.h"
28 #include "ComputeDiracThread.h"
35 #include "TimeKernel.h"
36 #include "BoundaryCondition.h"
37 #include "DirichletBCBase.h"
38 #include "NodalBCBase.h"
39 #include "IntegratedBCBase.h"
40 #include "DGKernel.h"
41 #include "InterfaceKernelBase.h"
42 #include "ElementDamper.h"
43 #include "NodalDamper.h"
44 #include "GeneralDamper.h"
45 #include "DisplacedProblem.h"
46 #include "NearestNodeLocator.h"
47 #include "PenetrationLocator.h"
48 #include "NodalConstraint.h"
49 #include "NodeFaceConstraint.h"
50 #include "NodeElemConstraintBase.h"
51 #include "MortarConstraint.h"
52 #include "ElemElemConstraint.h"
53 #include "ScalarKernelBase.h"
54 #include "Parser.h"
55 #include "Split.h"
57 #include "MooseMesh.h"
58 #include "MooseUtils.h"
59 #include "MooseApp.h"
60 #include "NodalKernelBase.h"
61 #include "DiracKernelBase.h"
62 #include "TimeIntegrator.h"
63 #include "Predictor.h"
64 #include "Assembly.h"
65 #include "ElementPairLocator.h"
66 #include "ODETimeKernel.h"
69 #include "ADKernel.h"
70 #include "ADDirichletBCBase.h"
71 #include "Moose.h"
72 #include "ConsoleStream.h"
73 #include "MooseError.h"
74 #include "FVElementalKernel.h"
77 #include "FVFluxKernel.h"
79 #include "UserObject.h"
81 #include "HDGKernel.h"
83 
84 // libMesh
85 #include "libmesh/nonlinear_solver.h"
86 #include "libmesh/quadrature_gauss.h"
87 #include "libmesh/dense_vector.h"
88 #include "libmesh/boundary_info.h"
89 #include "libmesh/petsc_matrix.h"
90 #include "libmesh/petsc_vector.h"
91 #include "libmesh/petsc_nonlinear_solver.h"
92 #include "libmesh/numeric_vector.h"
93 #include "libmesh/mesh.h"
94 #include "libmesh/dense_subvector.h"
95 #include "libmesh/dense_submatrix.h"
96 #include "libmesh/dof_map.h"
97 #include "libmesh/sparse_matrix.h"
98 #include "libmesh/petsc_matrix.h"
99 #include "libmesh/default_coupling.h"
100 #include "libmesh/diagonal_matrix.h"
101 #include "libmesh/fe_interface.h"
102 #include "libmesh/petsc_solver_exception.h"
103 
104 #include <ios>
105 
106 #include "petscsnes.h"
107 #include <PetscDMMoose.h>
108 EXTERN_C_BEGIN
109 extern PetscErrorCode DMCreate_Moose(DM);
110 EXTERN_C_END
111 
112 using namespace libMesh;
113 
115  System & sys,
116  const std::string & name)
117  : SolverSystem(fe_problem, fe_problem, name, Moose::VAR_SOLVER),
118  PerfGraphInterface(fe_problem.getMooseApp().perfGraph(), "NonlinearSystemBase"),
119  _sys(sys),
120  _last_nl_rnorm(0.),
121  _current_nl_its(0),
122  _residual_ghosted(NULL),
123  _Re_time_tag(-1),
124  _Re_time(NULL),
125  _Re_non_time_tag(-1),
126  _Re_non_time(NULL),
127  _scalar_kernels(/*threaded=*/false),
128  _nodal_bcs(/*threaded=*/false),
129  _preset_nodal_bcs(/*threaded=*/false),
130  _ad_preset_nodal_bcs(/*threaded=*/false),
131 #ifdef MOOSE_KOKKOS_ENABLED
132  _kokkos_kernels(/*threaded=*/false),
133  _kokkos_integrated_bcs(/*threaded=*/false),
134  _kokkos_nodal_bcs(/*threaded=*/false),
135  _kokkos_preset_nodal_bcs(/*threaded=*/false),
136  _kokkos_nodal_kernels(/*threaded=*/false),
137 #endif
138  _general_dampers(/*threaded=*/false),
139  _splits(/*threaded=*/false),
140  _increment_vec(NULL),
141  _use_finite_differenced_preconditioner(false),
142  _fdcoloring(nullptr),
143  _fsp(nullptr),
144  _add_implicit_geometric_coupling_entries_to_jacobian(false),
145  _assemble_constraints_separately(false),
146  _need_residual_ghosted(false),
147  _debugging_residuals(false),
148  _doing_dg(false),
149  _n_iters(0),
150  _n_linear_iters(0),
151  _n_residual_evaluations(0),
152  _final_residual(0.),
153  _computing_pre_smo_residual(false),
154  _pre_smo_residual(0),
155  _initial_residual(0),
156  _use_pre_smo_residual(false),
157  _print_all_var_norms(false),
158  _has_save_in(false),
159  _has_diag_save_in(false),
160  _has_nodalbc_save_in(false),
161  _has_nodalbc_diag_save_in(false),
162  _computed_scaling(false),
163  _compute_scaling_once(true),
164  _resid_vs_jac_scaling_param(0),
165  _off_diagonals_in_auto_scaling(false),
166  _auto_scaling_initd(false)
167 {
169  // Don't need to add the matrix - it already exists (for now)
171 
172  // The time matrix tag is not normally used - but must be added to the system
173  // in case it is so that objects can have 'time' in their matrix tags by default
174  _fe_problem.addMatrixTag("TIME");
175 
176  _Re_tag = _fe_problem.addVectorTag("RESIDUAL");
177 
179 
181  {
182  auto & dof_map = _sys.get_dof_map();
183  dof_map.remove_algebraic_ghosting_functor(dof_map.default_algebraic_ghosting());
184  dof_map.set_implicit_neighbor_dofs(false);
185  }
186 }
187 
189 
190 void
192 {
194 
195  if (_fe_problem.hasDampers())
196  setupDampers();
197 
198  if (_residual_copy.get())
199  _residual_copy->init(_sys.n_dofs(), false, SERIAL);
200 
201 #ifdef MOOSE_KOKKOS_ENABLED
204 #endif
205 }
206 
207 void
209 {
210  // reinit is called on meshChanged() in FEProblemBase. We could implement meshChanged() instead.
211  // Subdomains might have changed
212  for (auto & functor : _displaced_mortar_functors)
213  functor.second.setupMortarMaterials();
214  for (auto & functor : _undisplaced_mortar_functors)
215  functor.second.setupMortarMaterials();
216 }
217 
218 void
220 {
222  nonlinearSolver()->jacobian = NULL;
223 }
224 
225 void
227 {
228  TIME_SECTION("nlInitialSetup", 2, "Setting Up Nonlinear System");
229 
231 
232  {
233  TIME_SECTION("kernelsInitialSetup", 2, "Setting Up Kernels/BCs/Constraints");
234 
235  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
236  {
237  _kernels.initialSetup(tid);
240  if (_doing_dg)
243 
247 
248  if (_fe_problem.haveFV())
249  {
250  std::vector<FVElementalKernel *> fv_elemental_kernels;
252  .query()
253  .template condition<AttribSystem>("FVElementalKernel")
254  .template condition<AttribThread>(tid)
255  .queryInto(fv_elemental_kernels);
256 
257  for (auto * fv_kernel : fv_elemental_kernels)
258  fv_kernel->initialSetup();
259 
260  std::vector<FVFluxKernel *> fv_flux_kernels;
262  .query()
263  .template condition<AttribSystem>("FVFluxKernel")
264  .template condition<AttribThread>(tid)
265  .queryInto(fv_flux_kernels);
266 
267  for (auto * fv_kernel : fv_flux_kernels)
268  fv_kernel->initialSetup();
269  }
270  }
271 
278 
279 #ifdef MOOSE_KOKKOS_ENABLED
284 #endif
285  }
286 
287  {
288  TIME_SECTION("mortarSetup", 2, "Initializing Mortar Interfaces");
289 
290  auto create_mortar_functors = [this](const bool displaced)
291  {
292  // go over mortar interfaces and construct functors
293  const auto & mortar_interfaces = _fe_problem.getMortarInterfaces(displaced);
294  for (const auto & [primary_secondary_boundary_pair, mortar_generation_ptr] :
295  mortar_interfaces)
296  {
297  if (!_constraints.hasActiveMortarConstraints(primary_secondary_boundary_pair, displaced))
298  continue;
299 
300  auto & mortar_constraints =
301  _constraints.getActiveMortarConstraints(primary_secondary_boundary_pair, displaced);
302 
303  auto & subproblem = displaced
304  ? static_cast<SubProblem &>(*_fe_problem.getDisplacedProblem())
305  : static_cast<SubProblem &>(_fe_problem);
306 
307  auto & mortar_functors =
309 
310  mortar_functors.emplace(primary_secondary_boundary_pair,
311  ComputeMortarFunctor(mortar_constraints,
312  *mortar_generation_ptr,
313  subproblem,
314  _fe_problem,
315  displaced,
316  subproblem.assembly(0, number())));
317  }
318  };
319 
320  create_mortar_functors(false);
321  create_mortar_functors(true);
322  }
323 
324  if (_automatic_scaling)
325  {
327  _scaling_matrix = std::make_unique<OffDiagonalScalingMatrix<Number>>(_communicator);
328  else
329  _scaling_matrix = std::make_unique<DiagonalMatrix<Number>>(_communicator);
330  }
331 
332  if (_preconditioner)
333  _preconditioner->initialSetup();
334 }
335 
336 void
338 {
340 
341  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
342  {
343  _kernels.timestepSetup(tid);
346  if (_doing_dg)
352 
353  if (_fe_problem.haveFV())
354  {
355  std::vector<FVFluxBC *> bcs;
357  .query()
358  .template condition<AttribSystem>("FVFluxBC")
359  .template condition<AttribThread>(tid)
360  .queryInto(bcs);
361 
362  std::vector<FVInterfaceKernel *> iks;
364  .query()
365  .template condition<AttribSystem>("FVInterfaceKernel")
366  .template condition<AttribThread>(tid)
367  .queryInto(iks);
368 
369  std::vector<FVFluxKernel *> kernels;
371  .query()
372  .template condition<AttribSystem>("FVFluxKernel")
373  .template condition<AttribThread>(tid)
374  .queryInto(kernels);
375 
376  for (auto * bc : bcs)
377  bc->timestepSetup();
378  for (auto * ik : iks)
379  ik->timestepSetup();
380  for (auto * kernel : kernels)
381  kernel->timestepSetup();
382  }
383  }
390 
391 #ifdef MOOSE_KOKKOS_ENABLED
396 #endif
397 }
398 
399 void
401 {
402  SolverSystem::customSetup(exec_type);
403 
404  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
405  {
406  _kernels.customSetup(exec_type, tid);
407  _nodal_kernels.customSetup(exec_type, tid);
408  _dirac_kernels.customSetup(exec_type, tid);
409  if (_doing_dg)
410  _dg_kernels.customSetup(exec_type, tid);
411  _interface_kernels.customSetup(exec_type, tid);
412  _element_dampers.customSetup(exec_type, tid);
413  _nodal_dampers.customSetup(exec_type, tid);
414  _integrated_bcs.customSetup(exec_type, tid);
415 
416  if (_fe_problem.haveFV())
417  {
418  std::vector<FVFluxBC *> bcs;
420  .query()
421  .template condition<AttribSystem>("FVFluxBC")
422  .template condition<AttribThread>(tid)
423  .queryInto(bcs);
424 
425  std::vector<FVInterfaceKernel *> iks;
427  .query()
428  .template condition<AttribSystem>("FVInterfaceKernel")
429  .template condition<AttribThread>(tid)
430  .queryInto(iks);
431 
432  std::vector<FVFluxKernel *> kernels;
434  .query()
435  .template condition<AttribSystem>("FVFluxKernel")
436  .template condition<AttribThread>(tid)
437  .queryInto(kernels);
438 
439  for (auto * bc : bcs)
440  bc->customSetup(exec_type);
441  for (auto * ik : iks)
442  ik->customSetup(exec_type);
443  for (auto * kernel : kernels)
444  kernel->customSetup(exec_type);
445  }
446  }
447  _scalar_kernels.customSetup(exec_type);
448  _constraints.customSetup(exec_type);
449  _general_dampers.customSetup(exec_type);
450  _nodal_bcs.customSetup(exec_type);
451  _preset_nodal_bcs.customSetup(exec_type);
453 
454 #ifdef MOOSE_KOKKOS_ENABLED
455  _kokkos_kernels.customSetup(exec_type);
458  _kokkos_nodal_bcs.customSetup(exec_type);
459 #endif
460 }
461 
462 void
464 {
465  if (_fsp)
466  _fsp->setupDM();
467 }
468 
469 void
470 NonlinearSystemBase::addKernel(const std::string & kernel_name,
471  const std::string & name,
472  InputParameters & parameters)
473 {
474  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
475  {
476  // Create the kernel object via the factory and add to warehouse
477  std::shared_ptr<KernelBase> kernel =
478  _factory.create<KernelBase>(kernel_name, name, parameters, tid);
479  _kernels.addObject(kernel, tid);
480  postAddResidualObject(*kernel);
481  // Add to theWarehouse, a centralized storage for all moose objects
482  _fe_problem.theWarehouse().add(kernel);
483  }
484 
485  if (parameters.get<std::vector<AuxVariableName>>("save_in").size() > 0)
486  _has_save_in = true;
487  if (parameters.get<std::vector<AuxVariableName>>("diag_save_in").size() > 0)
488  _has_diag_save_in = true;
489 }
490 
491 void
492 NonlinearSystemBase::addHDGKernel(const std::string & kernel_name,
493  const std::string & name,
494  InputParameters & parameters)
495 {
496  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
497  {
498  // Create the kernel object via the factory and add to warehouse
499  auto kernel = _factory.create<HDGKernel>(kernel_name, name, parameters, tid);
500  _kernels.addObject(kernel, tid);
501  _hybridized_kernels.addObject(kernel, tid);
502  // Add to theWarehouse, a centralized storage for all moose objects
503  _fe_problem.theWarehouse().add(kernel);
504  postAddResidualObject(*kernel);
505  }
506 }
507 
508 void
509 NonlinearSystemBase::addNodalKernel(const std::string & kernel_name,
510  const std::string & name,
511  InputParameters & parameters)
512 {
513  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
514  {
515  // Create the kernel object via the factory and add to the warehouse
516  std::shared_ptr<NodalKernelBase> kernel =
517  _factory.create<NodalKernelBase>(kernel_name, name, parameters, tid);
518  _nodal_kernels.addObject(kernel, tid);
519  // Add to theWarehouse, a centralized storage for all moose objects
520  _fe_problem.theWarehouse().add(kernel);
521  postAddResidualObject(*kernel);
522  }
523 
524  if (parameters.have_parameter<std::vector<AuxVariableName>>("save_in") &&
525  parameters.get<std::vector<AuxVariableName>>("save_in").size() > 0)
526  _has_save_in = true;
527  if (parameters.have_parameter<std::vector<AuxVariableName>>("save_in") &&
528  parameters.get<std::vector<AuxVariableName>>("diag_save_in").size() > 0)
529  _has_diag_save_in = true;
530 }
531 
532 void
533 NonlinearSystemBase::addScalarKernel(const std::string & kernel_name,
534  const std::string & name,
535  InputParameters & parameters)
536 {
537  std::shared_ptr<ScalarKernelBase> kernel =
538  _factory.create<ScalarKernelBase>(kernel_name, name, parameters);
539  postAddResidualObject(*kernel);
540  // Add to theWarehouse, a centralized storage for all moose objects
541  _fe_problem.theWarehouse().add(kernel);
542  _scalar_kernels.addObject(kernel);
543 }
544 
545 void
546 NonlinearSystemBase::addBoundaryCondition(const std::string & bc_name,
547  const std::string & name,
548  InputParameters & parameters)
549 {
550  // ThreadID
551  THREAD_ID tid = 0;
552 
553  // Create the object
554  std::shared_ptr<BoundaryCondition> bc =
555  _factory.create<BoundaryCondition>(bc_name, name, parameters, tid);
557 
558  // Active BoundaryIDs for the object
559  const std::set<BoundaryID> & boundary_ids = bc->boundaryIDs();
560  auto bc_var = dynamic_cast<const MooseVariableFieldBase *>(&bc->variable());
561  _vars[tid].addBoundaryVar(boundary_ids, bc_var);
562 
563  // Cast to the various types of BCs
564  std::shared_ptr<NodalBCBase> nbc = std::dynamic_pointer_cast<NodalBCBase>(bc);
565  std::shared_ptr<IntegratedBCBase> ibc = std::dynamic_pointer_cast<IntegratedBCBase>(bc);
566 
567  // NodalBCBase
568  if (nbc)
569  {
570  if (nbc->checkNodalVar() && !nbc->variable().isNodal())
571  mooseError("Trying to use nodal boundary condition '",
572  nbc->name(),
573  "' on a non-nodal variable '",
574  nbc->variable().name(),
575  "'.");
576 
577  _nodal_bcs.addObject(nbc);
578  // Add to theWarehouse, a centralized storage for all moose objects
580  _vars[tid].addBoundaryVars(boundary_ids, nbc->getCoupledVars());
581 
582  if (parameters.get<std::vector<AuxVariableName>>("save_in").size() > 0)
583  _has_nodalbc_save_in = true;
584  if (parameters.get<std::vector<AuxVariableName>>("diag_save_in").size() > 0)
586 
587  // DirichletBCs that are preset
588  std::shared_ptr<DirichletBCBase> dbc = std::dynamic_pointer_cast<DirichletBCBase>(bc);
589  if (dbc && dbc->preset())
591 
592  std::shared_ptr<ADDirichletBCBase> addbc = std::dynamic_pointer_cast<ADDirichletBCBase>(bc);
593  if (addbc && addbc->preset())
595  }
596 
597  // IntegratedBCBase
598  else if (ibc)
599  {
600  _integrated_bcs.addObject(ibc, tid);
601  // Add to theWarehouse, a centralized storage for all moose objects
603  _vars[tid].addBoundaryVars(boundary_ids, ibc->getCoupledVars());
604 
605  if (parameters.get<std::vector<AuxVariableName>>("save_in").size() > 0)
606  _has_save_in = true;
607  if (parameters.get<std::vector<AuxVariableName>>("diag_save_in").size() > 0)
608  _has_diag_save_in = true;
609 
610  for (tid = 1; tid < libMesh::n_threads(); tid++)
611  {
612  // Create the object
613  bc = _factory.create<BoundaryCondition>(bc_name, name, parameters, tid);
614 
615  // Give users opportunity to set some parameters
617 
618  // Active BoundaryIDs for the object
619  const std::set<BoundaryID> & boundary_ids = bc->boundaryIDs();
620  _vars[tid].addBoundaryVar(boundary_ids, bc_var);
621 
622  ibc = std::static_pointer_cast<IntegratedBCBase>(bc);
623 
624  _integrated_bcs.addObject(ibc, tid);
625  _vars[tid].addBoundaryVars(boundary_ids, ibc->getCoupledVars());
626  }
627  }
628 
629  else
630  mooseError("Unknown BoundaryCondition type for object named ", bc->name());
631 }
632 
633 void
634 NonlinearSystemBase::addConstraint(const std::string & c_name,
635  const std::string & name,
636  InputParameters & parameters)
637 {
638  std::shared_ptr<Constraint> constraint = _factory.create<Constraint>(c_name, name, parameters);
639  _constraints.addObject(constraint);
640  postAddResidualObject(*constraint);
641 
643  if (constraint && constraint->addCouplingEntriesToJacobian())
645 }
646 
647 void
648 NonlinearSystemBase::addDiracKernel(const std::string & kernel_name,
649  const std::string & name,
650  InputParameters & parameters)
651 {
652  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
653  {
654  std::shared_ptr<DiracKernelBase> kernel =
655  _factory.create<DiracKernelBase>(kernel_name, name, parameters, tid);
656  postAddResidualObject(*kernel);
657  _dirac_kernels.addObject(kernel, tid);
658  // Add to theWarehouse, a centralized storage for all moose objects
659  _fe_problem.theWarehouse().add(kernel);
660  }
661 }
662 
663 void
664 NonlinearSystemBase::addDGKernel(std::string dg_kernel_name,
665  const std::string & name,
666  InputParameters & parameters)
667 {
668  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
669  {
670  auto dg_kernel = _factory.create<DGKernelBase>(dg_kernel_name, name, parameters, tid);
671  _dg_kernels.addObject(dg_kernel, tid);
672  // Add to theWarehouse, a centralized storage for all moose objects
673  _fe_problem.theWarehouse().add(dg_kernel);
674  postAddResidualObject(*dg_kernel);
675  }
676 
677  _doing_dg = true;
678 
679  if (parameters.get<std::vector<AuxVariableName>>("save_in").size() > 0)
680  _has_save_in = true;
681  if (parameters.get<std::vector<AuxVariableName>>("diag_save_in").size() > 0)
682  _has_diag_save_in = true;
683 }
684 
685 void
686 NonlinearSystemBase::addInterfaceKernel(std::string interface_kernel_name,
687  const std::string & name,
688  InputParameters & parameters)
689 {
690  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
691  {
692  std::shared_ptr<InterfaceKernelBase> interface_kernel =
693  _factory.create<InterfaceKernelBase>(interface_kernel_name, name, parameters, tid);
694  postAddResidualObject(*interface_kernel);
695 
696  const std::set<BoundaryID> & boundary_ids = interface_kernel->boundaryIDs();
697  auto ik_var = dynamic_cast<const MooseVariableFieldBase *>(&interface_kernel->variable());
698  _vars[tid].addBoundaryVar(boundary_ids, ik_var);
699 
700  _interface_kernels.addObject(interface_kernel, tid);
701  // Add to theWarehouse, a centralized storage for all moose objects
702  _fe_problem.theWarehouse().add(interface_kernel);
703  _vars[tid].addBoundaryVars(boundary_ids, interface_kernel->getCoupledVars());
704  }
705 }
706 
707 void
708 NonlinearSystemBase::addDamper(const std::string & damper_name,
709  const std::string & name,
710  InputParameters & parameters)
711 {
712  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
713  {
714  std::shared_ptr<Damper> damper = _factory.create<Damper>(damper_name, name, parameters, tid);
715 
716  // Attempt to cast to the damper types
717  std::shared_ptr<ElementDamper> ed = std::dynamic_pointer_cast<ElementDamper>(damper);
718  std::shared_ptr<NodalDamper> nd = std::dynamic_pointer_cast<NodalDamper>(damper);
719  std::shared_ptr<GeneralDamper> gd = std::dynamic_pointer_cast<GeneralDamper>(damper);
720 
721  if (gd)
722  {
724  break; // not threaded
725  }
726  else if (ed)
727  _element_dampers.addObject(ed, tid);
728  else if (nd)
729  _nodal_dampers.addObject(nd, tid);
730  else
731  mooseError("Invalid damper type");
732  }
733 }
734 
735 void
736 NonlinearSystemBase::addSplit(const std::string & split_name,
737  const std::string & name,
738  InputParameters & parameters)
739 {
740  std::shared_ptr<Split> split = _factory.create<Split>(split_name, name, parameters);
742  // Add to theWarehouse, a centralized storage for all moose objects
744 }
745 
746 std::shared_ptr<Split>
747 NonlinearSystemBase::getSplit(const std::string & name)
748 {
749  return _splits.getActiveObject(name);
750 }
751 
752 bool
754 {
756  return false;
757 
758  // The legacy behavior (#10464) _always_ performs the pre-SMO residual evaluation
759  // regardless of whether it is needed.
760  //
761  // This is not ideal and has been fixed by #23472. This legacy option ensures a smooth transition
762  // to the new behavior. Modules and Apps that want to migrate to the new behavior should set this
763  // parameter to false.
764  if (_app.parameters().get<bool>("use_legacy_initial_residual_evaluation_behavior"))
765  return true;
766 
767  return _use_pre_smo_residual;
768 }
769 
770 Real
772 {
774 }
775 
776 Real
778 {
780  mooseError("pre-SMO residual is requested but not evaluated.");
781 
782  return _pre_smo_residual;
783 }
784 
785 Real
787 {
788  return _initial_residual;
789 }
790 
791 void
793 {
794  _initial_residual = r;
795 }
796 
797 void
798 NonlinearSystemBase::zeroVectorForResidual(const std::string & vector_name)
799 {
800  for (unsigned int i = 0; i < _vecs_to_zero_for_residual.size(); ++i)
801  if (vector_name == _vecs_to_zero_for_residual[i])
802  return;
803 
804  _vecs_to_zero_for_residual.push_back(vector_name);
805 }
806 
807 void
809 {
810  _nl_vector_tags.clear();
811  _nl_vector_tags.insert(tag_id);
813 
815 
817 
819 }
820 
821 void
823 {
824  mooseDeprecated(" Please use computeResidualTag");
825 
826  computeResidualTag(residual, tag_id);
827 }
828 
829 void
830 NonlinearSystemBase::computeResidualTags(const std::set<TagID> & tags)
831 {
832  parallel_object_only();
833 
834  TIME_SECTION("nl::computeResidualTags", 5);
835 
838 
839  bool required_residual = tags.find(residualVectorTag()) == tags.end() ? false : true;
840 
842 
843  // not suppose to do anythin on matrix
845 
847 
848  for (const auto & numeric_vec : _vecs_to_zero_for_residual)
849  if (hasVector(numeric_vec))
850  {
851  NumericVector<Number> & vec = getVector(numeric_vec);
852  vec.close();
853  vec.zero();
854  }
855 
856  try
857  {
858  zeroTaggedVectors(tags);
860  closeTaggedVectors(tags);
861 
862  if (required_residual)
863  {
864  auto & residual = getVector(residualVectorTag());
865  if (!_time_integrators.empty())
866  {
867  for (auto & ti : _time_integrators)
868  ti->postResidual(residual);
869  }
870  else
871  residual += *_Re_non_time;
872  residual.close();
873  }
875  // We don't want to do nodal bcs or anything else
876  return;
877 
879  closeTaggedVectors(tags);
880 
881  // If we are debugging residuals we need one more assignment to have the ghosted copy up to
882  // date
883  if (_need_residual_ghosted && _debugging_residuals && required_residual)
884  {
885  auto & residual = getVector(residualVectorTag());
886 
887  *_residual_ghosted = residual;
889  }
890  // Need to close and update the aux system in case residuals were saved to it.
893  if (hasSaveIn())
895  }
896  catch (MooseException & e)
897  {
898  // The buck stops here, we have already handled the exception by
899  // calling stopSolve(), it is now up to PETSc to return a
900  // "diverged" reason during the next solve.
901  }
902 
903  // not supposed to do anything on matrix
905 
907 }
908 
909 void
910 NonlinearSystemBase::computeResidualAndJacobianTags(const std::set<TagID> & vector_tags,
911  const std::set<TagID> & matrix_tags)
912 {
913  const bool required_residual =
914  vector_tags.find(residualVectorTag()) == vector_tags.end() ? false : true;
915 
916  try
917  {
918  zeroTaggedVectors(vector_tags);
919  computeResidualAndJacobianInternal(vector_tags, matrix_tags);
920  closeTaggedVectors(vector_tags);
921  closeTaggedMatrices(matrix_tags);
922 
923  if (required_residual)
924  {
925  auto & residual = getVector(residualVectorTag());
926  if (!_time_integrators.empty())
927  {
928  for (auto & ti : _time_integrators)
929  ti->postResidual(residual);
930  }
931  else
932  residual += *_Re_non_time;
933  residual.close();
934  }
935 
936  computeNodalBCsResidualAndJacobian(vector_tags, matrix_tags);
937  closeTaggedVectors(vector_tags);
938  closeTaggedMatrices(matrix_tags);
939  }
940  catch (MooseException & e)
941  {
942  // The buck stops here, we have already handled the exception by
943  // calling stopSolve(), it is now up to PETSc to return a
944  // "diverged" reason during the next solve.
945  }
946 }
947 
948 void
950 {
951  for (auto & ti : _time_integrators)
952  ti->preSolve();
953  if (_predictor.get())
954  _predictor->timestepSetup();
955 }
956 
957 void
959 {
961 
962  NumericVector<Number> & initial_solution(solution());
963  if (_predictor.get())
964  {
965  if (_predictor->shouldApply())
966  {
967  TIME_SECTION("applyPredictor", 2, "Applying Predictor");
968 
969  _predictor->apply(initial_solution);
970  _fe_problem.predictorCleanup(initial_solution);
971  }
972  else
973  _console << " Skipping predictor this step" << std::endl;
974  }
975 
976  // do nodal BC
977  {
978  TIME_SECTION("initialBCs", 2, "Applying BCs To Initial Condition");
979 
981  for (const auto & bnode : bnd_nodes)
982  {
983  BoundaryID boundary_id = bnode->_bnd_id;
984  Node * node = bnode->_node;
985 
986  if (node->processor_id() == processor_id())
987  {
988  bool has_preset_nodal_bcs = _preset_nodal_bcs.hasActiveBoundaryObjects(boundary_id);
989  bool has_ad_preset_nodal_bcs = _ad_preset_nodal_bcs.hasActiveBoundaryObjects(boundary_id);
990 
991  // reinit variables in nodes
992  if (has_preset_nodal_bcs || has_ad_preset_nodal_bcs)
993  _fe_problem.reinitNodeFace(node, boundary_id, 0);
994 
995  if (has_preset_nodal_bcs)
996  {
997  const auto & preset_bcs = _preset_nodal_bcs.getActiveBoundaryObjects(boundary_id);
998  for (const auto & preset_bc : preset_bcs)
999  preset_bc->computeValue(initial_solution);
1000  }
1001  if (has_ad_preset_nodal_bcs)
1002  {
1003  const auto & preset_bcs_res = _ad_preset_nodal_bcs.getActiveBoundaryObjects(boundary_id);
1004  for (const auto & preset_bc : preset_bcs_res)
1005  preset_bc->computeValue(initial_solution);
1006  }
1007  }
1008  }
1009  }
1010 
1011 #ifdef MOOSE_KOKKOS_ENABLED
1014 #endif
1015 
1016  _sys.solution->close();
1017  update();
1018 
1019  // Set constraint secondary values
1020  setConstraintSecondaryValues(initial_solution, false);
1021 
1023  setConstraintSecondaryValues(initial_solution, true);
1024 }
1025 
1026 void
1027 NonlinearSystemBase::setPredictor(std::shared_ptr<Predictor> predictor)
1028 {
1029  _predictor = predictor;
1030 }
1031 
1032 void
1034 {
1036 
1037  _kernels.subdomainSetup(subdomain, tid);
1038  _nodal_kernels.subdomainSetup(subdomain, tid);
1039  _element_dampers.subdomainSetup(subdomain, tid);
1040  _nodal_dampers.subdomainSetup(subdomain, tid);
1041 }
1042 
1045 {
1046  if (!_Re_time)
1047  {
1049 
1050  // Most applications don't need the expense of ghosting
1052  _Re_time = &addVector(_Re_time_tag, false, ptype);
1053  }
1054  else if (_need_residual_ghosted && _Re_time->type() == PARALLEL)
1055  {
1056  const auto vector_name = _subproblem.vectorTagName(_Re_time_tag);
1057 
1058  // If an application changes its mind, the libMesh API lets us
1059  // change the vector.
1060  _Re_time = &system().add_vector(vector_name, false, GHOSTED);
1061  }
1062 
1063  return *_Re_time;
1064 }
1065 
1068 {
1069  if (!_Re_non_time)
1070  {
1072 
1073  // Most applications don't need the expense of ghosting
1075  _Re_non_time = &addVector(_Re_non_time_tag, false, ptype);
1076  }
1078  {
1079  const auto vector_name = _subproblem.vectorTagName(_Re_non_time_tag);
1080 
1081  // If an application changes its mind, the libMesh API lets us
1082  // change the vector.
1083  _Re_non_time = &system().add_vector(vector_name, false, GHOSTED);
1084  }
1085 
1086  return *_Re_non_time;
1087 }
1088 
1091 {
1092  mooseDeprecated("Please use getVector()");
1093  switch (tag)
1094  {
1095  case 0:
1096  return getResidualNonTimeVector();
1097 
1098  case 1:
1099  return getResidualTimeVector();
1100 
1101  default:
1102  mooseError("The required residual vector is not available");
1103  }
1104 }
1105 
1106 void
1108 {
1109  THREAD_ID tid = 0; // constraints are going to be done single-threaded
1110  residual.close();
1112  {
1113  const auto & ncs = _constraints.getActiveNodalConstraints();
1114  for (const auto & nc : ncs)
1115  {
1116  std::vector<dof_id_type> & secondary_node_ids = nc->getSecondaryNodeId();
1117  std::vector<dof_id_type> & primary_node_ids = nc->getPrimaryNodeId();
1118 
1119  if ((secondary_node_ids.size() > 0) && (primary_node_ids.size() > 0))
1120  {
1121  _fe_problem.reinitNodes(primary_node_ids, tid);
1122  _fe_problem.reinitNodesNeighbor(secondary_node_ids, tid);
1123  nc->computeResidual(residual);
1124  }
1125  }
1126  _fe_problem.addCachedResidualDirectly(residual, tid);
1127  residual.close();
1128  }
1129 }
1130 
1131 bool
1133 {
1134  if (!hasMatrix(systemMatrixTag()))
1135  mooseError(" A system matrix is required");
1136 
1137  THREAD_ID tid = 0; // constraints are going to be done single-threaded
1138 
1140  {
1141  const auto & ncs = _constraints.getActiveNodalConstraints();
1142  for (const auto & nc : ncs)
1143  {
1144  std::vector<dof_id_type> & secondary_node_ids = nc->getSecondaryNodeId();
1145  std::vector<dof_id_type> & primary_node_ids = nc->getPrimaryNodeId();
1146 
1147  if ((secondary_node_ids.size() > 0) && (primary_node_ids.size() > 0))
1148  {
1149  _fe_problem.reinitNodes(primary_node_ids, tid);
1150  _fe_problem.reinitNodesNeighbor(secondary_node_ids, tid);
1151  nc->computeJacobian(jacobian_to_view);
1152  }
1153  }
1155 
1156  return true;
1157  }
1158  else
1159  return false;
1160 }
1161 
1162 void
1164  const BoundaryID secondary_boundary,
1165  const PenetrationInfo & info,
1166  const bool displaced)
1167 {
1168  auto & subproblem = displaced ? static_cast<SubProblem &>(*_fe_problem.getDisplacedProblem())
1169  : static_cast<SubProblem &>(_fe_problem);
1170 
1171  const Elem * primary_elem = info._elem;
1172  unsigned int primary_side = info._side_num;
1173  std::vector<Point> points;
1174  points.push_back(info._closest_point);
1175 
1176  // *These next steps MUST be done in this order!*
1177  // ADL: This is a Chesterton's fence situation. I don't know which calls exactly the above comment
1178  // is referring to. If I had to guess I would guess just the reinitNodeFace and prepareAssembly
1179  // calls since the former will size the variable's dof indices and then the latter will resize the
1180  // residual/Jacobian based off the variable's cached dof indices size
1181 
1182  // This reinits the variables that exist on the secondary node
1183  _fe_problem.reinitNodeFace(&secondary_node, secondary_boundary, 0);
1184 
1185  // This will set aside residual and jacobian space for the variables that have dofs on
1186  // the secondary node
1188 
1189  _fe_problem.setNeighborSubdomainID(primary_elem, 0);
1190 
1191  //
1192  // Reinit material on undisplaced mesh
1193  //
1194 
1195  const Elem * const undisplaced_primary_elem =
1196  displaced ? _mesh.elemPtr(primary_elem->id()) : primary_elem;
1197  const Point undisplaced_primary_physical_point =
1198  [&points, displaced, primary_elem, undisplaced_primary_elem]()
1199  {
1200  if (displaced)
1201  {
1202  const Point reference_point =
1203  FEMap::inverse_map(primary_elem->dim(), primary_elem, points[0]);
1204  return FEMap::map(primary_elem->dim(), undisplaced_primary_elem, reference_point);
1205  }
1206  else
1207  // If our penetration locator is on the reference mesh, then our undisplaced
1208  // physical point is simply the point coming from the penetration locator
1209  return points[0];
1210  }();
1211 
1213  undisplaced_primary_elem, primary_side, {undisplaced_primary_physical_point}, 0);
1214  // Stateful material properties are only initialized for neighbor material data for internal faces
1215  // for discontinuous Galerkin methods or for conforming interfaces for interface kernels. We don't
1216  // have either of those use cases here where we likely have disconnected meshes
1217  _fe_problem.reinitMaterialsNeighbor(primary_elem->subdomain_id(), 0, /*swap_stateful=*/false);
1218 
1219  // Reinit points for constraint enforcement
1220  if (displaced)
1221  subproblem.reinitNeighborPhys(primary_elem, primary_side, points, 0);
1222 }
1223 
1224 void
1226 {
1227 
1228  if (displaced)
1229  mooseAssert(_fe_problem.getDisplacedProblem(),
1230  "If we're calling this method with displaced = true, then we better well have a "
1231  "displaced problem");
1232  auto & subproblem = displaced ? static_cast<SubProblem &>(*_fe_problem.getDisplacedProblem())
1233  : static_cast<SubProblem &>(_fe_problem);
1234  const auto & penetration_locators = subproblem.geomSearchData()._penetration_locators;
1235 
1236  bool constraints_applied = false;
1237 
1238  for (const auto & it : penetration_locators)
1239  {
1240  PenetrationLocator & pen_loc = *(it.second);
1241 
1242  std::vector<dof_id_type> & secondary_nodes = pen_loc._nearest_node._secondary_nodes;
1243 
1244  BoundaryID secondary_boundary = pen_loc._secondary_boundary;
1245  BoundaryID primary_boundary = pen_loc._primary_boundary;
1246 
1247  if (_constraints.hasActiveNodeFaceConstraints(secondary_boundary, displaced))
1248  {
1249  const auto & constraints =
1250  _constraints.getActiveNodeFaceConstraints(secondary_boundary, displaced);
1251  std::unordered_set<unsigned int> needed_mat_props;
1252  for (const auto & constraint : constraints)
1253  {
1254  const auto & mp_deps = constraint->getMatPropDependencies();
1255  needed_mat_props.insert(mp_deps.begin(), mp_deps.end());
1256  }
1257  _fe_problem.setActiveMaterialProperties(needed_mat_props, /*tid=*/0);
1258 
1259  for (unsigned int i = 0; i < secondary_nodes.size(); i++)
1260  {
1261  dof_id_type secondary_node_num = secondary_nodes[i];
1262  Node & secondary_node = _mesh.nodeRef(secondary_node_num);
1263 
1264  if (secondary_node.processor_id() == processor_id())
1265  {
1266  if (pen_loc._penetration_info[secondary_node_num])
1267  {
1268  PenetrationInfo & info = *pen_loc._penetration_info[secondary_node_num];
1269 
1270  reinitNodeFace(secondary_node, secondary_boundary, info, displaced);
1271 
1272  for (const auto & nfc : constraints)
1273  {
1274  if (nfc->isExplicitConstraint())
1275  continue;
1276  // Return if this constraint does not correspond to the primary-secondary pair
1277  // prepared by the outer loops.
1278  // This continue statement is required when, e.g. one secondary surface constrains
1279  // more than one primary surface.
1280  if (nfc->secondaryBoundary() != secondary_boundary ||
1281  nfc->primaryBoundary() != primary_boundary)
1282  continue;
1283 
1284  if (nfc->shouldApply())
1285  {
1286  constraints_applied = true;
1287  nfc->computeSecondaryValue(solution);
1288  }
1289 
1290  if (nfc->hasWritableCoupledVariables())
1291  {
1292  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
1293  for (auto * var : nfc->getWritableCoupledVariables())
1294  {
1295  if (var->isNodalDefined())
1296  var->insert(_fe_problem.getAuxiliarySystem().solution());
1297  }
1298  }
1299  }
1300  }
1301  }
1302  }
1303  }
1304  }
1305 
1306  // go over NodeELemConstraints
1307  std::set<dof_id_type> unique_secondary_node_ids;
1308 
1309  for (const auto & secondary_id : _mesh.meshSubdomains())
1310  {
1311  for (const auto & primary_id : _mesh.meshSubdomains())
1312  {
1313  if (_constraints.hasActiveNodeElemConstraints(secondary_id, primary_id, displaced))
1314  {
1315  const auto & constraints =
1316  _constraints.getActiveNodeElemConstraints(secondary_id, primary_id, displaced);
1317 
1318  // get unique set of ids of all nodes on current block
1319  unique_secondary_node_ids.clear();
1320  const MeshBase & meshhelper = _mesh.getMesh();
1321  for (const auto & elem : as_range(meshhelper.active_subdomain_elements_begin(secondary_id),
1322  meshhelper.active_subdomain_elements_end(secondary_id)))
1323  {
1324  for (auto & n : elem->node_ref_range())
1325  unique_secondary_node_ids.insert(n.id());
1326  }
1327 
1328  for (auto secondary_node_id : unique_secondary_node_ids)
1329  {
1330  Node & secondary_node = _mesh.nodeRef(secondary_node_id);
1331 
1332  // check if secondary node is on current processor
1333  if (secondary_node.processor_id() == processor_id())
1334  {
1335  // This reinits the variables that exist on the secondary node
1336  _fe_problem.reinitNodeFace(&secondary_node, secondary_id, 0);
1337 
1338  // This will set aside residual and jacobian space for the variables that have dofs
1339  // on the secondary node
1341 
1342  for (const auto & nec : constraints)
1343  {
1344  if (nec->shouldApply())
1345  {
1346  constraints_applied = true;
1347  nec->computeSecondaryValue(solution);
1348  }
1349  }
1350  }
1351  }
1352  }
1353  }
1354  }
1355 
1356  // See if constraints were applied anywhere
1357  _communicator.max(constraints_applied);
1358 
1359  if (constraints_applied)
1360  {
1361  solution.close();
1362  update();
1363  }
1364 }
1365 
1366 void
1368 {
1369  // Make sure the residual is in a good state
1370  residual.close();
1371 
1372  if (displaced)
1373  mooseAssert(_fe_problem.getDisplacedProblem(),
1374  "If we're calling this method with displaced = true, then we better well have a "
1375  "displaced problem");
1376  auto & subproblem = displaced ? static_cast<SubProblem &>(*_fe_problem.getDisplacedProblem())
1377  : static_cast<SubProblem &>(_fe_problem);
1378  const auto & penetration_locators = subproblem.geomSearchData()._penetration_locators;
1379 
1380  bool constraints_applied;
1381  bool residual_has_inserted_values = false;
1383  constraints_applied = false;
1384  for (const auto & it : penetration_locators)
1385  {
1387  {
1388  // Reset the constraint_applied flag before each new constraint, as they need to be
1389  // assembled separately
1390  constraints_applied = false;
1391  }
1392  PenetrationLocator & pen_loc = *(it.second);
1393 
1394  std::vector<dof_id_type> & secondary_nodes = pen_loc._nearest_node._secondary_nodes;
1395 
1396  BoundaryID secondary_boundary = pen_loc._secondary_boundary;
1397  BoundaryID primary_boundary = pen_loc._primary_boundary;
1398 
1399  bool has_writable_variables(false);
1400 
1401  if (_constraints.hasActiveNodeFaceConstraints(secondary_boundary, displaced))
1402  {
1403  const auto & constraints =
1404  _constraints.getActiveNodeFaceConstraints(secondary_boundary, displaced);
1405 
1406  for (unsigned int i = 0; i < secondary_nodes.size(); i++)
1407  {
1408  dof_id_type secondary_node_num = secondary_nodes[i];
1409  Node & secondary_node = _mesh.nodeRef(secondary_node_num);
1410 
1411  if (secondary_node.processor_id() == processor_id())
1412  {
1413  if (pen_loc._penetration_info[secondary_node_num])
1414  {
1415  PenetrationInfo & info = *pen_loc._penetration_info[secondary_node_num];
1416 
1417  reinitNodeFace(secondary_node, secondary_boundary, info, displaced);
1418 
1419  for (const auto & nfc : constraints)
1420  {
1421  // Return if this constraint does not correspond to the primary-secondary pair
1422  // prepared by the outer loops.
1423  // This continue statement is required when, e.g. one secondary surface constrains
1424  // more than one primary surface.
1425  if (nfc->secondaryBoundary() != secondary_boundary ||
1426  nfc->primaryBoundary() != primary_boundary)
1427  continue;
1428 
1429  if (nfc->shouldApply())
1430  {
1431  constraints_applied = true;
1432  nfc->computeResidual();
1433 
1434  if (nfc->overwriteSecondaryResidual())
1435  {
1436  // The below will actually overwrite the residual for every single dof that
1437  // lives on the node. We definitely don't want to do that!
1438  // _fe_problem.setResidual(residual, 0);
1439 
1440  const auto & secondary_var = nfc->variable();
1441  const auto & secondary_dofs = secondary_var.dofIndices();
1442  mooseAssert(secondary_dofs.size() == secondary_var.count(),
1443  "We are on a node so there should only be one dof per variable (for "
1444  "an ArrayVariable we should have a number of dofs equal to the "
1445  "number of components");
1446 
1447  // Assume that if the user is overwriting the secondary residual, then they are
1448  // supplying residuals that do not correspond to their other physics
1449  // (e.g. Kernels), hence we should not apply a scalingFactor that is normally
1450  // based on the order of their other physics (e.g. Kernels)
1451  std::vector<Number> values = {nfc->secondaryResidual()};
1452  residual.insert(values, secondary_dofs);
1453  residual_has_inserted_values = true;
1454  }
1455  else
1458  }
1459  if (nfc->hasWritableCoupledVariables())
1460  {
1461  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
1462  has_writable_variables = true;
1463  for (auto * var : nfc->getWritableCoupledVariables())
1464  {
1465  if (var->isNodalDefined())
1466  var->insert(_fe_problem.getAuxiliarySystem().solution());
1467  }
1468  }
1469  }
1470  }
1471  }
1472  }
1473  }
1474  _communicator.max(has_writable_variables);
1475 
1476  if (has_writable_variables)
1477  {
1478  // Explicit contact dynamic constraints write to auxiliary variables and update the old
1479  // displacement solution on the constraint boundaries. Close solutions and update system
1480  // accordingly.
1483  solutionOld().close();
1484  }
1485 
1487  {
1488  // Make sure that secondary contribution to primary are assembled, and ghosts have been
1489  // exchanged, as current primaries might become secondaries on next iteration and will need to
1490  // contribute their former secondaries' contributions to the future primaries. See if
1491  // constraints were applied anywhere
1492  _communicator.max(constraints_applied);
1493 
1494  if (constraints_applied)
1495  {
1496  // If any of the above constraints inserted values in the residual, it needs to be
1497  // assembled before adding the cached residuals below.
1498  _communicator.max(residual_has_inserted_values);
1499  if (residual_has_inserted_values)
1500  {
1501  residual.close();
1502  residual_has_inserted_values = false;
1503  }
1505  residual.close();
1506 
1508  *_residual_ghosted = residual;
1509  }
1510  }
1511  }
1513  {
1514  _communicator.max(constraints_applied);
1515 
1516  if (constraints_applied)
1517  {
1518  // If any of the above constraints inserted values in the residual, it needs to be assembled
1519  // before adding the cached residuals below.
1520  _communicator.max(residual_has_inserted_values);
1521  if (residual_has_inserted_values)
1522  residual.close();
1523 
1525  residual.close();
1526 
1528  *_residual_ghosted = residual;
1529  }
1530  }
1531 
1532  // go over element-element constraint interface
1533  THREAD_ID tid = 0;
1534  const auto & element_pair_locators = subproblem.geomSearchData()._element_pair_locators;
1535  for (const auto & it : element_pair_locators)
1536  {
1537  ElementPairLocator & elem_pair_loc = *(it.second);
1538 
1539  if (_constraints.hasActiveElemElemConstraints(it.first, displaced))
1540  {
1541  // ElemElemConstraint objects
1542  const auto & element_constraints =
1543  _constraints.getActiveElemElemConstraints(it.first, displaced);
1544 
1545  // go over pair elements
1546  const std::list<std::pair<const Elem *, const Elem *>> & elem_pairs =
1547  elem_pair_loc.getElemPairs();
1548  for (const auto & pr : elem_pairs)
1549  {
1550  const Elem * elem1 = pr.first;
1551  const Elem * elem2 = pr.second;
1552 
1553  if (elem1->processor_id() != processor_id())
1554  continue;
1555 
1556  const ElementPairInfo & info = elem_pair_loc.getElemPairInfo(pr);
1557 
1558  // for each element process constraints on the
1559  for (const auto & ec : element_constraints)
1560  {
1561  _fe_problem.setCurrentSubdomainID(elem1, tid);
1562  subproblem.reinitElemPhys(elem1, info._elem1_constraint_q_point, tid);
1563  _fe_problem.setNeighborSubdomainID(elem2, tid);
1564  subproblem.reinitNeighborPhys(elem2, info._elem2_constraint_q_point, tid);
1565 
1566  ec->prepareShapes(ec->variable().number());
1567  ec->prepareNeighborShapes(ec->variable().number());
1568 
1569  ec->reinit(info);
1570  ec->computeResidual();
1573  }
1575  }
1576  }
1577  }
1578 
1579  // go over NodeElemConstraints
1580  std::set<dof_id_type> unique_secondary_node_ids;
1581 
1582  constraints_applied = false;
1583  residual_has_inserted_values = false;
1584  bool has_writable_variables = false;
1585  for (const auto & secondary_id : _mesh.meshSubdomains())
1586  {
1587  for (const auto & primary_id : _mesh.meshSubdomains())
1588  {
1589  if (_constraints.hasActiveNodeElemConstraints(secondary_id, primary_id, displaced))
1590  {
1591  const auto & constraints =
1592  _constraints.getActiveNodeElemConstraints(secondary_id, primary_id, displaced);
1593 
1594  // get unique set of ids of all nodes on current block
1595  unique_secondary_node_ids.clear();
1596  const MeshBase & meshhelper = _mesh.getMesh();
1597  for (const auto & elem : as_range(meshhelper.active_subdomain_elements_begin(secondary_id),
1598  meshhelper.active_subdomain_elements_end(secondary_id)))
1599  {
1600  for (auto & n : elem->node_ref_range())
1601  unique_secondary_node_ids.insert(n.id());
1602  }
1603 
1604  for (auto secondary_node_id : unique_secondary_node_ids)
1605  {
1606  Node & secondary_node = _mesh.nodeRef(secondary_node_id);
1607  // check if secondary node is on current processor
1608  if (secondary_node.processor_id() == processor_id())
1609  {
1610  // This reinits the variables that exist on the secondary node
1611  _fe_problem.reinitNodeFace(&secondary_node, secondary_id, 0);
1612 
1613  // This will set aside residual and jacobian space for the variables that have dofs
1614  // on the secondary node
1616 
1617  for (const auto & nec : constraints)
1618  {
1619  if (nec->shouldApply())
1620  {
1621  constraints_applied = true;
1622  nec->computeResidual();
1623 
1624  if (nec->overwriteSecondaryResidual())
1625  {
1626  _fe_problem.setResidual(residual, 0);
1627  residual_has_inserted_values = true;
1628  }
1629  else
1632  }
1633  if (nec->hasWritableCoupledVariables())
1634  {
1635  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
1636  has_writable_variables = true;
1637  for (auto * var : nec->getWritableCoupledVariables())
1638  {
1639  if (var->isNodalDefined())
1640  var->insert(_fe_problem.getAuxiliarySystem().solution());
1641  }
1642  }
1643  }
1645  }
1646  }
1647  }
1648  }
1649  }
1650  _communicator.max(constraints_applied);
1651 
1652  if (constraints_applied)
1653  {
1654  // If any of the above constraints inserted values in the residual, it needs to be assembled
1655  // before adding the cached residuals below.
1656  _communicator.max(residual_has_inserted_values);
1657  if (residual_has_inserted_values)
1658  residual.close();
1659 
1661  residual.close();
1662 
1664  *_residual_ghosted = residual;
1665  }
1666  _communicator.max(has_writable_variables);
1667 
1668  if (has_writable_variables)
1669  {
1670  // Explicit contact dynamic constraints write to auxiliary variables and update the old
1671  // displacement solution on the constraint boundaries. Close solutions and update system
1672  // accordingly.
1675  solutionOld().close();
1676  }
1677 
1678  // We may have additional tagged vectors that also need to be accumulated
1680 }
1681 
1682 void
1684 {
1685  // Overwrite results from integrator in case we have explicit dynamics contact constraints
1687  ? static_cast<SubProblem &>(*_fe_problem.getDisplacedProblem())
1688  : static_cast<SubProblem &>(_fe_problem);
1689  const auto & penetration_locators = subproblem.geomSearchData()._penetration_locators;
1690 
1691  for (const auto & it : penetration_locators)
1692  {
1693  PenetrationLocator & pen_loc = *(it.second);
1694 
1695  const auto & secondary_nodes = pen_loc._nearest_node._secondary_nodes;
1696  const BoundaryID secondary_boundary = pen_loc._secondary_boundary;
1697  const BoundaryID primary_boundary = pen_loc._primary_boundary;
1698 
1699  if (_constraints.hasActiveNodeFaceConstraints(secondary_boundary, true))
1700  {
1701  const auto & constraints =
1702  _constraints.getActiveNodeFaceConstraints(secondary_boundary, true);
1703  for (const auto i : index_range(secondary_nodes))
1704  {
1705  const auto secondary_node_num = secondary_nodes[i];
1706  const Node & secondary_node = _mesh.nodeRef(secondary_node_num);
1707 
1708  if (secondary_node.processor_id() == processor_id())
1709  if (pen_loc._penetration_info[secondary_node_num])
1710  for (const auto & nfc : constraints)
1711  {
1712  if (!nfc->isExplicitConstraint())
1713  continue;
1714 
1715  // Return if this constraint does not correspond to the primary-secondary pair
1716  // prepared by the outer loops.
1717  // This continue statement is required when, e.g. one secondary surface constrains
1718  // more than one primary surface.
1719  if (nfc->secondaryBoundary() != secondary_boundary ||
1720  nfc->primaryBoundary() != primary_boundary)
1721  continue;
1722 
1723  nfc->overwriteBoundaryVariables(soln, secondary_node);
1724  }
1725  }
1726  }
1727  }
1728  soln.close();
1729 }
1730 
1731 void
1733 {
1734  TIME_SECTION("residualSetup", 3);
1735 
1737 
1738  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
1739  {
1740  _kernels.residualSetup(tid);
1743  if (_doing_dg)
1749  }
1756 
1757 #ifdef MOOSE_KOKKOS_ENABLED
1762 #endif
1763 
1764  // Avoid recursion
1765  if (this == &_fe_problem.currentNonlinearSystem())
1767 }
1768 
1769 void
1771 {
1772  parallel_object_only();
1773 
1774  TIME_SECTION("computeResidualInternal", 3);
1775 
1776  residualSetup();
1777 
1778  // Residual contributions from UOs - for now this is used for ray tracing
1779  // and ray kernels that contribute to the residual (think line sources)
1780  std::vector<UserObject *> uos;
1782  .query()
1783  .condition<AttribSystem>("UserObject")
1784  .condition<AttribExecOns>(EXEC_PRE_KERNELS)
1785  .queryInto(uos);
1786  for (auto & uo : uos)
1787  uo->residualSetup();
1788  for (auto & uo : uos)
1789  {
1790  uo->initialize();
1791  uo->execute();
1792  uo->finalize();
1793  }
1794 
1795  // reinit scalar variables
1796  for (unsigned int tid = 0; tid < libMesh::n_threads(); tid++)
1798 
1799 #ifdef MOOSE_KOKKOS_ENABLED
1801  computeKokkosResidual(tags);
1802 #endif
1803 
1804  // residual contributions from the domain
1805  PARALLEL_TRY
1806  {
1807  TIME_SECTION("Kernels", 3 /*, "Computing Kernels"*/);
1808 
1810 
1812  Threads::parallel_reduce(elem_range, cr);
1813 
1814  // We pass face information directly to FV residual objects for their evaluation. Consequently
1815  // we must make sure to do separate threaded loops for 1) undisplaced face information objects
1816  // and undisplaced residual objects and 2) displaced face information objects and displaced
1817  // residual objects
1819  if (_fe_problem.haveFV())
1820  {
1822  _fe_problem, this->number(), tags, /*on_displaced=*/false);
1824  Threads::parallel_reduce(faces, fvr);
1825  }
1827  displaced_problem && displaced_problem->haveFV())
1828  {
1830  _fe_problem, this->number(), tags, /*on_displaced=*/true);
1831  FVRange faces(displaced_problem->mesh().ownedFaceInfoBegin(),
1832  displaced_problem->mesh().ownedFaceInfoEnd());
1833  Threads::parallel_reduce(faces, fvr);
1834  }
1835 
1836  unsigned int n_threads = libMesh::n_threads();
1837  for (unsigned int i = 0; i < n_threads;
1838  i++) // Add any cached residuals that might be hanging around
1840  }
1841  PARALLEL_CATCH;
1842 
1843  // residual contributions from the scalar kernels
1844  PARALLEL_TRY
1845  {
1846  // do scalar kernels (not sure how to thread this)
1848  {
1849  TIME_SECTION("ScalarKernels", 3 /*, "Computing ScalarKernels"*/);
1850 
1851  MooseObjectWarehouse<ScalarKernelBase> * scalar_kernel_warehouse;
1852  // This code should be refactored once we can do tags for scalar
1853  // kernels
1854  // Should redo this based on Warehouse
1855  if (!tags.size() || tags.size() == _fe_problem.numVectorTags(Moose::VECTOR_TAG_RESIDUAL))
1856  scalar_kernel_warehouse = &_scalar_kernels;
1857  else if (tags.size() == 1)
1858  scalar_kernel_warehouse =
1859  &(_scalar_kernels.getVectorTagObjectWarehouse(*(tags.begin()), 0));
1860  else
1861  // scalar_kernels is not threading
1862  scalar_kernel_warehouse = &(_scalar_kernels.getVectorTagsObjectWarehouse(tags, 0));
1863 
1864  bool have_scalar_contributions = false;
1865  const auto & scalars = scalar_kernel_warehouse->getActiveObjects();
1866  for (const auto & scalar_kernel : scalars)
1867  {
1868  scalar_kernel->reinit();
1869  const std::vector<dof_id_type> & dof_indices = scalar_kernel->variable().dofIndices();
1870  const DofMap & dof_map = scalar_kernel->variable().dofMap();
1871  const dof_id_type first_dof = dof_map.first_dof();
1872  const dof_id_type end_dof = dof_map.end_dof();
1873  for (dof_id_type dof : dof_indices)
1874  {
1875  if (dof >= first_dof && dof < end_dof)
1876  {
1877  scalar_kernel->computeResidual();
1878  have_scalar_contributions = true;
1879  break;
1880  }
1881  }
1882  }
1883  if (have_scalar_contributions)
1885  }
1886  }
1887  PARALLEL_CATCH;
1888 
1889  // residual contributions from Block NodalKernels
1890  PARALLEL_TRY
1891  {
1893  {
1894  TIME_SECTION("NodalKernels", 3 /*, "Computing NodalKernels"*/);
1895 
1897 
1899 
1900  if (range.begin() != range.end())
1901  {
1902  _fe_problem.reinitNode(*range.begin(), 0);
1903 
1904  Threads::parallel_reduce(range, cnk);
1905 
1906  unsigned int n_threads = libMesh::n_threads();
1907  for (unsigned int i = 0; i < n_threads;
1908  i++) // Add any cached residuals that might be hanging around
1910  }
1911  }
1912  }
1913  PARALLEL_CATCH;
1914 
1916  // We computed the volumetric objects. We can return now before we get into
1917  // any strongly enforced constraint conditions or penalty-type objects
1918  // (DGKernels, IntegratedBCs, InterfaceKernels, Constraints)
1919  return;
1920 
1921  // residual contributions from boundary NodalKernels
1922  PARALLEL_TRY
1923  {
1925  {
1926  TIME_SECTION("NodalKernelBCs", 3 /*, "Computing NodalKernelBCs"*/);
1927 
1929 
1931 
1932  Threads::parallel_reduce(bnd_node_range, cnk);
1933 
1934  unsigned int n_threads = libMesh::n_threads();
1935  for (unsigned int i = 0; i < n_threads;
1936  i++) // Add any cached residuals that might be hanging around
1938  }
1939  }
1940  PARALLEL_CATCH;
1941 
1943 
1944  if (_residual_copy.get())
1945  {
1946  _Re_non_time->close();
1948  }
1949 
1951  {
1952  _Re_non_time->close();
1955  }
1956 
1957  PARALLEL_TRY { computeDiracContributions(tags, false); }
1958  PARALLEL_CATCH;
1959 
1961  {
1962  PARALLEL_TRY { enforceNodalConstraintsResidual(*_Re_non_time); }
1963  PARALLEL_CATCH;
1964  _Re_non_time->close();
1965  }
1966 
1967  // Add in Residual contributions from other Constraints
1969  {
1970  PARALLEL_TRY
1971  {
1972  // Undisplaced Constraints
1974 
1975  // Displaced Constraints
1978 
1981  }
1982  PARALLEL_CATCH;
1983  _Re_non_time->close();
1984  }
1985 
1986  // Accumulate the occurrence of solution invalid warnings for the current iteration cumulative
1987  // counters
1990 }
1991 
1992 void
1994  const std::set<TagID> & matrix_tags)
1995 {
1996  TIME_SECTION("computeResidualAndJacobianInternal", 3);
1997 
1998  // Make matrix ready to use
2000 
2001  for (auto tag : matrix_tags)
2002  {
2003  if (!hasMatrix(tag))
2004  continue;
2005 
2006  auto & jacobian = getMatrix(tag);
2007  // Necessary for speed
2008  if (auto petsc_matrix = dynamic_cast<PetscMatrix<Number> *>(&jacobian))
2009  {
2010  LibmeshPetscCall(MatSetOption(petsc_matrix->mat(),
2011  MAT_KEEP_NONZERO_PATTERN, // This is changed in 3.1
2012  PETSC_TRUE));
2014  LibmeshPetscCall(
2015  MatSetOption(petsc_matrix->mat(), MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
2017  LibmeshPetscCall(MatSetOption(static_cast<PetscMatrix<Number> &>(jacobian).mat(),
2018  MAT_IGNORE_ZERO_ENTRIES,
2019  PETSC_TRUE));
2020  }
2021  }
2022 
2023  residualSetup();
2024 
2025  // Residual contributions from UOs - for now this is used for ray tracing
2026  // and ray kernels that contribute to the residual (think line sources)
2027  std::vector<UserObject *> uos;
2029  .query()
2030  .condition<AttribSystem>("UserObject")
2031  .condition<AttribExecOns>(EXEC_PRE_KERNELS)
2032  .queryInto(uos);
2033  for (auto & uo : uos)
2034  uo->residualSetup();
2035  for (auto & uo : uos)
2036  {
2037  uo->initialize();
2038  uo->execute();
2039  uo->finalize();
2040  }
2041 
2042  // reinit scalar variables
2043  for (unsigned int tid = 0; tid < libMesh::n_threads(); tid++)
2045 
2046 #ifdef MOOSE_KOKKOS_ENABLED
2048  computeKokkosResidualAndJacobian(vector_tags, matrix_tags);
2049 #endif
2050 
2051  // residual contributions from the domain
2052  PARALLEL_TRY
2053  {
2054  TIME_SECTION("Kernels", 3 /*, "Computing Kernels"*/);
2055 
2057 
2058  ComputeResidualAndJacobianThread crj(_fe_problem, vector_tags, matrix_tags);
2059  Threads::parallel_reduce(elem_range, crj);
2060 
2062  if (_fe_problem.haveFV())
2063  {
2065  _fe_problem, this->number(), vector_tags, matrix_tags, /*on_displaced=*/false);
2067  Threads::parallel_reduce(faces, fvrj);
2068  }
2070  displaced_problem && displaced_problem->haveFV())
2071  {
2073  _fe_problem, this->number(), vector_tags, matrix_tags, /*on_displaced=*/true);
2074  FVRange faces(displaced_problem->mesh().ownedFaceInfoBegin(),
2075  displaced_problem->mesh().ownedFaceInfoEnd());
2076  Threads::parallel_reduce(faces, fvr);
2077  }
2078 
2080 
2081  unsigned int n_threads = libMesh::n_threads();
2082  for (unsigned int i = 0; i < n_threads;
2083  i++) // Add any cached residuals that might be hanging around
2084  {
2087  }
2088  }
2089  PARALLEL_CATCH;
2090 }
2091 
2092 void
2094 {
2095  _nl_vector_tags.clear();
2096 
2097  const auto & residual_vector_tags = _fe_problem.getVectorTags(Moose::VECTOR_TAG_RESIDUAL);
2098  for (const auto & residual_vector_tag : residual_vector_tags)
2099  _nl_vector_tags.insert(residual_vector_tag._id);
2100 
2104 }
2105 
2106 void
2108  const std::set<TagID> & tags)
2109 {
2111 
2113 
2115 }
2116 
2117 void
2119 {
2120 #ifdef MOOSE_KOKKOS_ENABLED
2123 #endif
2124 
2125  // We need to close the diag_save_in variables on the aux system before NodalBCBases clear the
2126  // dofs on boundary nodes
2127  if (_has_save_in)
2129 
2130  // Select nodal kernels
2131  MooseObjectWarehouse<NodalBCBase> * nbc_warehouse;
2132 
2133  if (tags.size() == _fe_problem.numVectorTags(Moose::VECTOR_TAG_RESIDUAL) || !tags.size())
2134  nbc_warehouse = &_nodal_bcs;
2135  else if (tags.size() == 1)
2136  nbc_warehouse = &(_nodal_bcs.getVectorTagObjectWarehouse(*(tags.begin()), 0));
2137  else
2138  nbc_warehouse = &(_nodal_bcs.getVectorTagsObjectWarehouse(tags, 0));
2139 
2140  // Return early if there is no nodal kernel
2141  if (!nbc_warehouse->hasActiveObjects())
2142  return;
2143 
2144  PARALLEL_TRY
2145  {
2147 
2148  if (!bnd_nodes.empty())
2149  {
2150  TIME_SECTION("NodalBCs", 3 /*, "Computing NodalBCs"*/);
2151 
2152  for (const auto & bnode : bnd_nodes)
2153  {
2154  BoundaryID boundary_id = bnode->_bnd_id;
2155  Node * node = bnode->_node;
2156 
2157  if (node->processor_id() == processor_id() &&
2158  nbc_warehouse->hasActiveBoundaryObjects(boundary_id))
2159  {
2160  // reinit variables in nodes
2161  _fe_problem.reinitNodeFace(node, boundary_id, 0);
2162 
2163  const auto & bcs = nbc_warehouse->getActiveBoundaryObjects(boundary_id);
2164  for (const auto & nbc : bcs)
2165  if (nbc->shouldApply())
2166  nbc->computeResidual();
2167  }
2168  }
2169  }
2170  }
2171  PARALLEL_CATCH;
2172 
2173  if (_Re_time)
2174  _Re_time->close();
2175  _Re_non_time->close();
2176 }
2177 
2178 void
2180 {
2181  // We need to close the save_in variables on the aux system before NodalBCBases clear the dofs
2182  // on boundary nodes
2183  if (_has_diag_save_in)
2185 
2186  MooseObjectWarehouse<NodalBCBase> * nbc_warehouse;
2187 
2188  // Select nodal kernels
2189  if (tags.size() == _fe_problem.numMatrixTags() || !tags.size())
2190  nbc_warehouse = &_nodal_bcs;
2191  else if (tags.size() == 1)
2192  nbc_warehouse = &(_nodal_bcs.getMatrixTagObjectWarehouse(*(tags.begin()), 0));
2193  else
2194  nbc_warehouse = &(_nodal_bcs.getMatrixTagsObjectWarehouse(tags, 0));
2195 
2196  // Return early if there is no nodal kernel
2197  if (!nbc_warehouse->hasActiveObjects())
2198  return;
2199 
2200  PARALLEL_TRY
2201  {
2202  // We may be switching from add to set. Moreover, we rely on a call to MatZeroRows to enforce
2203  // the nodal boundary condition constraints which requires that the matrix be truly assembled
2204  // as opposed to just flushed. Consequently we can't do the following despite any desire to
2205  // keep our initial sparsity pattern honored (see https://gitlab.com/petsc/petsc/-/issues/852)
2206  //
2207  // flushTaggedMatrices(tags);
2208  closeTaggedMatrices(tags);
2209 
2210  // Cache the information about which BCs are coupled to which
2211  // variables, so we don't have to figure it out for each node.
2212  std::map<std::string, std::set<unsigned int>> bc_involved_vars;
2213  const std::set<BoundaryID> & all_boundary_ids = _mesh.getBoundaryIDs();
2214  for (const auto & bid : all_boundary_ids)
2215  {
2216  // Get reference to all the NodalBCs for this ID. This is only
2217  // safe if there are NodalBCBases there to be gotten...
2218  if (nbc_warehouse->hasActiveBoundaryObjects(bid))
2219  {
2220  const auto & bcs = nbc_warehouse->getActiveBoundaryObjects(bid);
2221  for (const auto & bc : bcs)
2222  {
2223  const std::vector<MooseVariableFEBase *> & coupled_moose_vars = bc->getCoupledMooseVars();
2224 
2225  // Create the set of "involved" MOOSE nonlinear vars, which includes all coupled vars
2226  // and the BC's own variable
2227  std::set<unsigned int> & var_set = bc_involved_vars[bc->name()];
2228  for (const auto & coupled_var : coupled_moose_vars)
2229  if (coupled_var->kind() == Moose::VAR_SOLVER)
2230  var_set.insert(coupled_var->number());
2231 
2232  var_set.insert(bc->variable().number());
2233  }
2234  }
2235  }
2236 
2237  // reinit scalar variables again. This reinit does not re-fill any of the scalar variable
2238  // solution arrays because that was done above. It only will reorder the derivative
2239  // information for AD calculations to be suitable for NodalBC calculations
2240  for (unsigned int tid = 0; tid < libMesh::n_threads(); tid++)
2241  _fe_problem.reinitScalars(tid, true);
2242 
2243  // Get variable coupling list. We do all the NodalBCBase stuff on
2244  // thread 0... The couplingEntries() data structure determines
2245  // which variables are "coupled" as far as the preconditioner is
2246  // concerned, not what variables a boundary condition specifically
2247  // depends on.
2248  auto & coupling_entries = _fe_problem.couplingEntries(/*_tid=*/0, this->number());
2249 
2250  // Compute Jacobians for NodalBCBases
2252  for (const auto & bnode : bnd_nodes)
2253  {
2254  BoundaryID boundary_id = bnode->_bnd_id;
2255  Node * node = bnode->_node;
2256 
2257  if (nbc_warehouse->hasActiveBoundaryObjects(boundary_id) &&
2258  node->processor_id() == processor_id())
2259  {
2260  _fe_problem.reinitNodeFace(node, boundary_id, 0);
2261 
2262  const auto & bcs = nbc_warehouse->getActiveBoundaryObjects(boundary_id);
2263  for (const auto & bc : bcs)
2264  {
2265  // Get the set of involved MOOSE vars for this BC
2266  std::set<unsigned int> & var_set = bc_involved_vars[bc->name()];
2267 
2268  // Loop over all the variables whose Jacobian blocks are
2269  // actually being computed, call computeOffDiagJacobian()
2270  // for each one which is actually coupled (otherwise the
2271  // value is zero.)
2272  for (const auto & it : coupling_entries)
2273  {
2274  unsigned int ivar = it.first->number(), jvar = it.second->number();
2275 
2276  // We are only going to call computeOffDiagJacobian() if:
2277  // 1.) the BC's variable is ivar
2278  // 2.) jvar is "involved" with the BC (including jvar==ivar), and
2279  // 3.) the BC should apply.
2280  if ((bc->variable().number() == ivar) && var_set.count(jvar) && bc->shouldApply())
2281  bc->computeOffDiagJacobian(jvar);
2282  }
2283 
2284  const auto & coupled_scalar_vars = bc->getCoupledMooseScalarVars();
2285  for (const auto & jvariable : coupled_scalar_vars)
2286  if (hasScalarVariable(jvariable->name()))
2287  bc->computeOffDiagJacobianScalar(jvariable->number());
2288  }
2289  }
2290  } // end loop over boundary nodes
2291 
2292  // Set the cached NodalBCBase values in the Jacobian matrix
2294  }
2295  PARALLEL_CATCH;
2296 }
2297 
2298 void
2300  [[maybe_unused]] const std::set<TagID> & vector_tags,
2301  [[maybe_unused]] const std::set<TagID> & matrix_tags)
2302 {
2303 #ifdef MOOSE_KOKKOS_ENABLED
2305  computeKokkosNodalBCsResidual(vector_tags);
2306 #endif
2307 
2308  // Return early if there is no nodal kernel
2310  return;
2311 
2312  PARALLEL_TRY
2313  {
2315 
2316  if (!bnd_nodes.empty())
2317  {
2318  TIME_SECTION("NodalBCs", 3 /*, "Computing NodalBCs"*/);
2319 
2320  for (const auto & bnode : bnd_nodes)
2321  {
2322  BoundaryID boundary_id = bnode->_bnd_id;
2323  Node * node = bnode->_node;
2324 
2325  if (node->processor_id() == processor_id())
2326  {
2327  // reinit variables in nodes
2328  _fe_problem.reinitNodeFace(node, boundary_id, 0);
2329  if (_nodal_bcs.hasActiveBoundaryObjects(boundary_id))
2330  {
2331  const auto & bcs = _nodal_bcs.getActiveBoundaryObjects(boundary_id);
2332  for (const auto & nbc : bcs)
2333  if (nbc->shouldApply())
2334  nbc->computeResidualAndJacobian();
2335  }
2336  }
2337  }
2338  }
2339  }
2340  PARALLEL_CATCH;
2341 
2342  // Set the cached NodalBCBase values in the Jacobian matrix
2344 }
2345 
2346 void
2347 NonlinearSystemBase::getNodeDofs(dof_id_type node_id, std::vector<dof_id_type> & dofs)
2348 {
2349  const Node & node = _mesh.nodeRef(node_id);
2350  unsigned int s = number();
2351  if (node.has_dofs(s))
2352  {
2353  for (unsigned int v = 0; v < nVariables(); v++)
2354  for (unsigned int c = 0; c < node.n_comp(s, v); c++)
2355  dofs.push_back(node.dof_number(s, v, c));
2356  }
2357 }
2358 
2359 void
2361  GeometricSearchData & geom_search_data,
2362  std::unordered_map<dof_id_type, std::vector<dof_id_type>> & graph)
2363 {
2364  const auto & node_to_elem_map = _mesh.nodeToElemMap();
2365  const auto & nearest_node_locators = geom_search_data._nearest_node_locators;
2366  for (const auto & it : nearest_node_locators)
2367  {
2368  std::vector<dof_id_type> & secondary_nodes = it.second->_secondary_nodes;
2369 
2370  for (const auto & secondary_node : secondary_nodes)
2371  {
2372  std::set<dof_id_type> unique_secondary_indices;
2373  std::set<dof_id_type> unique_primary_indices;
2374 
2375  auto node_to_elem_pair = node_to_elem_map.find(secondary_node);
2376  if (node_to_elem_pair != node_to_elem_map.end())
2377  {
2378  const std::vector<dof_id_type> & elems = node_to_elem_pair->second;
2379 
2380  // Get the dof indices from each elem connected to the node
2381  for (const auto & cur_elem : elems)
2382  {
2383  std::vector<dof_id_type> dof_indices;
2384  dofMap().dof_indices(_mesh.elemPtr(cur_elem), dof_indices);
2385 
2386  for (const auto & dof : dof_indices)
2387  unique_secondary_indices.insert(dof);
2388  }
2389  }
2390 
2391  std::vector<dof_id_type> primary_nodes = it.second->_neighbor_nodes[secondary_node];
2392 
2393  for (const auto & primary_node : primary_nodes)
2394  {
2395  auto primary_node_to_elem_pair = node_to_elem_map.find(primary_node);
2396  mooseAssert(primary_node_to_elem_pair != node_to_elem_map.end(),
2397  "Missing entry in node to elem map");
2398  const std::vector<dof_id_type> & primary_node_elems = primary_node_to_elem_pair->second;
2399 
2400  // Get the dof indices from each elem connected to the node
2401  for (const auto & cur_elem : primary_node_elems)
2402  {
2403  std::vector<dof_id_type> dof_indices;
2404  dofMap().dof_indices(_mesh.elemPtr(cur_elem), dof_indices);
2405 
2406  for (const auto & dof : dof_indices)
2407  unique_primary_indices.insert(dof);
2408  }
2409  }
2410 
2411  for (const auto & secondary_id : unique_secondary_indices)
2412  for (const auto & primary_id : unique_primary_indices)
2413  {
2414  graph[secondary_id].push_back(primary_id);
2415  graph[primary_id].push_back(secondary_id);
2416  }
2417  }
2418  }
2419 
2420  // handle node-to-node constraints
2421  const auto & ncs = _constraints.getActiveNodalConstraints();
2422  for (const auto & nc : ncs)
2423  {
2424  std::vector<dof_id_type> primary_dofs;
2425  std::vector<dof_id_type> & primary_node_ids = nc->getPrimaryNodeId();
2426  for (const auto & node_id : primary_node_ids)
2427  {
2428  Node * node = _mesh.queryNodePtr(node_id);
2429  if (node && node->processor_id() == this->processor_id())
2430  {
2431  getNodeDofs(node_id, primary_dofs);
2432  }
2433  }
2434 
2435  _communicator.allgather(primary_dofs);
2436 
2437  std::vector<dof_id_type> secondary_dofs;
2438  std::vector<dof_id_type> & secondary_node_ids = nc->getSecondaryNodeId();
2439  for (const auto & node_id : secondary_node_ids)
2440  {
2441  Node * node = _mesh.queryNodePtr(node_id);
2442  if (node && node->processor_id() == this->processor_id())
2443  {
2444  getNodeDofs(node_id, secondary_dofs);
2445  }
2446  }
2447 
2448  _communicator.allgather(secondary_dofs);
2449 
2450  for (const auto & primary_id : primary_dofs)
2451  for (const auto & secondary_id : secondary_dofs)
2452  {
2453  graph[primary_id].push_back(secondary_id);
2454  graph[secondary_id].push_back(primary_id);
2455  }
2456  }
2457 
2458  // Make every entry sorted and unique
2459  for (auto & it : graph)
2460  {
2461  std::vector<dof_id_type> & row = it.second;
2462  std::sort(row.begin(), row.end());
2463  std::vector<dof_id_type>::iterator uit = std::unique(row.begin(), row.end());
2464  row.resize(uit - row.begin());
2465  }
2466 }
2467 
2468 void
2470 {
2471  if (!hasMatrix(systemMatrixTag()))
2472  mooseError("Need a system matrix ");
2473 
2474  // At this point, have no idea how to make
2475  // this work with tag system
2476  auto & jacobian = getMatrix(systemMatrixTag());
2477 
2478  std::unordered_map<dof_id_type, std::vector<dof_id_type>> graph;
2479 
2480  findImplicitGeometricCouplingEntries(geom_search_data, graph);
2481 
2482  for (const auto & it : graph)
2483  {
2484  dof_id_type dof = it.first;
2485  const auto & row = it.second;
2486 
2487  for (const auto & coupled_dof : row)
2488  jacobian.add(dof, coupled_dof, 0);
2489  }
2490 }
2491 
2492 void
2494  bool displaced)
2495 {
2496  if (!hasMatrix(systemMatrixTag()))
2497  mooseError("A system matrix is required");
2498 
2499  auto & jacobian = getMatrix(systemMatrixTag());
2500 
2502  LibmeshPetscCall(MatSetOption(static_cast<PetscMatrix<Number> &>(jacobian).mat(),
2503  MAT_NEW_NONZERO_ALLOCATION_ERR,
2504  PETSC_FALSE));
2506  LibmeshPetscCall(MatSetOption(
2507  static_cast<PetscMatrix<Number> &>(jacobian).mat(), MAT_IGNORE_ZERO_ENTRIES, PETSC_TRUE));
2508 
2509  std::vector<numeric_index_type> zero_rows;
2510 
2511  if (displaced)
2512  mooseAssert(_fe_problem.getDisplacedProblem(),
2513  "If we're calling this method with displaced = true, then we better well have a "
2514  "displaced problem");
2515  auto & subproblem = displaced ? static_cast<SubProblem &>(*_fe_problem.getDisplacedProblem())
2516  : static_cast<SubProblem &>(_fe_problem);
2517  const auto & penetration_locators = subproblem.geomSearchData()._penetration_locators;
2518 
2519  bool constraints_applied;
2521  constraints_applied = false;
2522  for (const auto & it : penetration_locators)
2523  {
2525  {
2526  // Reset the constraint_applied flag before each new constraint, as they need to be
2527  // assembled separately
2528  constraints_applied = false;
2529  }
2530  PenetrationLocator & pen_loc = *(it.second);
2531 
2532  std::vector<dof_id_type> & secondary_nodes = pen_loc._nearest_node._secondary_nodes;
2533 
2534  BoundaryID secondary_boundary = pen_loc._secondary_boundary;
2535  BoundaryID primary_boundary = pen_loc._primary_boundary;
2536 
2537  zero_rows.clear();
2538  if (_constraints.hasActiveNodeFaceConstraints(secondary_boundary, displaced))
2539  {
2540  const auto & constraints =
2541  _constraints.getActiveNodeFaceConstraints(secondary_boundary, displaced);
2542 
2543  for (const auto & secondary_node_num : secondary_nodes)
2544  {
2545  Node & secondary_node = _mesh.nodeRef(secondary_node_num);
2546 
2547  if (secondary_node.processor_id() == processor_id())
2548  {
2549  if (pen_loc._penetration_info[secondary_node_num])
2550  {
2551  PenetrationInfo & info = *pen_loc._penetration_info[secondary_node_num];
2552 
2553  reinitNodeFace(secondary_node, secondary_boundary, info, displaced);
2555 
2556  for (const auto & nfc : constraints)
2557  {
2558  if (nfc->isExplicitConstraint())
2559  continue;
2560  // Return if this constraint does not correspond to the primary-secondary pair
2561  // prepared by the outer loops.
2562  // This continue statement is required when, e.g. one secondary surface constrains
2563  // more than one primary surface.
2564  if (nfc->secondaryBoundary() != secondary_boundary ||
2565  nfc->primaryBoundary() != primary_boundary)
2566  continue;
2567 
2568  nfc->_jacobian = &jacobian_to_view;
2569 
2570  if (nfc->shouldApply())
2571  {
2572  constraints_applied = true;
2573 
2574  nfc->prepareShapes(nfc->variable().number());
2575  nfc->prepareNeighborShapes(nfc->variable().number());
2576 
2577  nfc->computeJacobian();
2578 
2579  if (nfc->overwriteSecondaryJacobian())
2580  {
2581  // Add this variable's dof's row to be zeroed
2582  zero_rows.push_back(nfc->variable().nodalDofIndex());
2583  }
2584 
2585  std::vector<dof_id_type> secondary_dofs(1, nfc->variable().nodalDofIndex());
2586 
2587  // Assume that if the user is overwriting the secondary Jacobian, then they are
2588  // supplying Jacobians that do not correspond to their other physics
2589  // (e.g. Kernels), hence we should not apply a scalingFactor that is normally
2590  // based on the order of their other physics (e.g. Kernels)
2591  Real scaling_factor =
2592  nfc->overwriteSecondaryJacobian() ? 1. : nfc->variable().scalingFactor();
2593 
2594  // Cache the jacobian block for the secondary side
2595  nfc->addJacobian(_fe_problem.assembly(0, number()),
2596  nfc->_Kee,
2597  secondary_dofs,
2598  nfc->_connected_dof_indices,
2599  scaling_factor);
2600 
2601  // Cache Ken, Kne, Knn
2602  if (nfc->addCouplingEntriesToJacobian())
2603  {
2604  // Make sure we use a proper scaling factor (e.g. don't use an interior scaling
2605  // factor when we're overwriting secondary stuff)
2606  nfc->addJacobian(_fe_problem.assembly(0, number()),
2607  nfc->_Ken,
2608  secondary_dofs,
2609  nfc->primaryVariable().dofIndicesNeighbor(),
2610  scaling_factor);
2611 
2612  // Use _connected_dof_indices to get all the correct columns
2613  nfc->addJacobian(_fe_problem.assembly(0, number()),
2614  nfc->_Kne,
2615  nfc->primaryVariable().dofIndicesNeighbor(),
2616  nfc->_connected_dof_indices,
2617  nfc->primaryVariable().scalingFactor());
2618 
2619  // We've handled Ken and Kne, finally handle Knn
2621  }
2622 
2623  // Do the off-diagonals next
2624  const std::vector<MooseVariableFEBase *> coupled_vars = nfc->getCoupledMooseVars();
2625  for (const auto & jvar : coupled_vars)
2626  {
2627  // Only compute jacobians for nonlinear variables
2628  if (jvar->kind() != Moose::VAR_SOLVER)
2629  continue;
2630 
2631  // Only compute Jacobian entries if this coupling is being used by the
2632  // preconditioner
2633  if (nfc->variable().number() == jvar->number() ||
2635  nfc->variable().number(), jvar->number(), this->number()))
2636  continue;
2637 
2638  // Need to zero out the matrices first
2640 
2641  nfc->prepareShapes(nfc->variable().number());
2642  nfc->prepareNeighborShapes(jvar->number());
2643 
2644  nfc->computeOffDiagJacobian(jvar->number());
2645 
2646  // Cache the jacobian block for the secondary side
2647  nfc->addJacobian(_fe_problem.assembly(0, number()),
2648  nfc->_Kee,
2649  secondary_dofs,
2650  nfc->_connected_dof_indices,
2651  scaling_factor);
2652 
2653  // Cache Ken, Kne, Knn
2654  if (nfc->addCouplingEntriesToJacobian())
2655  {
2656  // Make sure we use a proper scaling factor (e.g. don't use an interior scaling
2657  // factor when we're overwriting secondary stuff)
2658  nfc->addJacobian(_fe_problem.assembly(0, number()),
2659  nfc->_Ken,
2660  secondary_dofs,
2661  jvar->dofIndicesNeighbor(),
2662  scaling_factor);
2663 
2664  // Use _connected_dof_indices to get all the correct columns
2665  nfc->addJacobian(_fe_problem.assembly(0, number()),
2666  nfc->_Kne,
2667  nfc->variable().dofIndicesNeighbor(),
2668  nfc->_connected_dof_indices,
2669  nfc->variable().scalingFactor());
2670 
2671  // We've handled Ken and Kne, finally handle Knn
2673  }
2674  }
2675  }
2676  }
2677  }
2678  }
2679  }
2680  }
2682  {
2683  // See if constraints were applied anywhere
2684  _communicator.max(constraints_applied);
2685 
2686  if (constraints_applied)
2687  {
2688  LibmeshPetscCall(MatSetOption(static_cast<PetscMatrix<Number> &>(jacobian).mat(),
2689  MAT_KEEP_NONZERO_PATTERN, // This is changed in 3.1
2690  PETSC_TRUE));
2691 
2692  jacobian.close();
2693  jacobian.zero_rows(zero_rows, 0.0);
2694  jacobian.close();
2696  jacobian.close();
2697  }
2698  }
2699  }
2701  {
2702  // See if constraints were applied anywhere
2703  _communicator.max(constraints_applied);
2704 
2705  if (constraints_applied)
2706  {
2707  LibmeshPetscCall(MatSetOption(static_cast<PetscMatrix<Number> &>(jacobian).mat(),
2708  MAT_KEEP_NONZERO_PATTERN, // This is changed in 3.1
2709  PETSC_TRUE));
2710 
2711  jacobian.close();
2712  jacobian.zero_rows(zero_rows, 0.0);
2713  jacobian.close();
2715  jacobian.close();
2716  }
2717  }
2718 
2719  THREAD_ID tid = 0;
2720  // go over element-element constraint interface
2721  const auto & element_pair_locators = subproblem.geomSearchData()._element_pair_locators;
2722  for (const auto & it : element_pair_locators)
2723  {
2724  ElementPairLocator & elem_pair_loc = *(it.second);
2725 
2726  if (_constraints.hasActiveElemElemConstraints(it.first, displaced))
2727  {
2728  // ElemElemConstraint objects
2729  const auto & element_constraints =
2730  _constraints.getActiveElemElemConstraints(it.first, displaced);
2731 
2732  // go over pair elements
2733  const std::list<std::pair<const Elem *, const Elem *>> & elem_pairs =
2734  elem_pair_loc.getElemPairs();
2735  for (const auto & pr : elem_pairs)
2736  {
2737  const Elem * elem1 = pr.first;
2738  const Elem * elem2 = pr.second;
2739 
2740  if (elem1->processor_id() != processor_id())
2741  continue;
2742 
2743  const ElementPairInfo & info = elem_pair_loc.getElemPairInfo(pr);
2744 
2745  // for each element process constraints on the
2746  for (const auto & ec : element_constraints)
2747  {
2748  _fe_problem.setCurrentSubdomainID(elem1, tid);
2749  subproblem.reinitElemPhys(elem1, info._elem1_constraint_q_point, tid);
2750  _fe_problem.setNeighborSubdomainID(elem2, tid);
2751  subproblem.reinitNeighborPhys(elem2, info._elem2_constraint_q_point, tid);
2752 
2753  ec->prepareShapes(ec->variable().number());
2754  ec->prepareNeighborShapes(ec->variable().number());
2755 
2756  ec->reinit(info);
2757  ec->computeJacobian();
2760  }
2762  }
2763  }
2764  }
2765 
2766  // go over NodeElemConstraints
2767  std::set<dof_id_type> unique_secondary_node_ids;
2768  constraints_applied = false;
2769  for (const auto & secondary_id : _mesh.meshSubdomains())
2770  {
2771  for (const auto & primary_id : _mesh.meshSubdomains())
2772  {
2773  if (_constraints.hasActiveNodeElemConstraints(secondary_id, primary_id, displaced))
2774  {
2775  const auto & constraints =
2776  _constraints.getActiveNodeElemConstraints(secondary_id, primary_id, displaced);
2777 
2778  // get unique set of ids of all nodes on current block
2779  unique_secondary_node_ids.clear();
2780  const MeshBase & meshhelper = _mesh.getMesh();
2781  for (const auto & elem : as_range(meshhelper.active_subdomain_elements_begin(secondary_id),
2782  meshhelper.active_subdomain_elements_end(secondary_id)))
2783  {
2784  for (auto & n : elem->node_ref_range())
2785  unique_secondary_node_ids.insert(n.id());
2786  }
2787 
2788  for (auto secondary_node_id : unique_secondary_node_ids)
2789  {
2790  const Node & secondary_node = _mesh.nodeRef(secondary_node_id);
2791  // check if secondary node is on current processor
2792  if (secondary_node.processor_id() == processor_id())
2793  {
2794  // This reinits the variables that exist on the secondary node
2795  _fe_problem.reinitNodeFace(&secondary_node, secondary_id, 0);
2796 
2797  // This will set aside residual and jacobian space for the variables that have dofs
2798  // on the secondary node
2801 
2802  for (const auto & nec : constraints)
2803  {
2804  if (nec->shouldApply())
2805  {
2806  constraints_applied = true;
2807 
2808  nec->_jacobian = &jacobian_to_view;
2809  nec->prepareShapes(nec->variable().number());
2810  nec->prepareNeighborShapes(nec->variable().number());
2811 
2812  nec->computeJacobian();
2813 
2814  if (nec->overwriteSecondaryJacobian())
2815  {
2816  // Add this variable's dof's row to be zeroed
2817  zero_rows.push_back(nec->variable().nodalDofIndex());
2818  }
2819 
2820  std::vector<dof_id_type> secondary_dofs(1, nec->variable().nodalDofIndex());
2821 
2822  // Cache the jacobian block for the secondary side
2823  nec->addJacobian(_fe_problem.assembly(0, number()),
2824  nec->_Kee,
2825  secondary_dofs,
2826  nec->_connected_dof_indices,
2827  nec->variable().scalingFactor());
2828 
2829  // Cache the jacobian block for the primary side
2830  nec->addJacobian(_fe_problem.assembly(0, number()),
2831  nec->_Kne,
2832  nec->primaryVariable().dofIndicesNeighbor(),
2833  nec->_connected_dof_indices,
2834  nec->primaryVariable().scalingFactor());
2835 
2838 
2839  // Do the off-diagonals next
2840  const std::vector<MooseVariableFEBase *> coupled_vars = nec->getCoupledMooseVars();
2841  for (const auto & jvar : coupled_vars)
2842  {
2843  // Only compute jacobians for nonlinear variables
2844  if (jvar->kind() != Moose::VAR_SOLVER)
2845  continue;
2846 
2847  // Only compute Jacobian entries if this coupling is being used by the
2848  // preconditioner
2849  if (nec->variable().number() == jvar->number() ||
2851  nec->variable().number(), jvar->number(), this->number()))
2852  continue;
2853 
2854  // Need to zero out the matrices first
2856 
2857  nec->prepareShapes(nec->variable().number());
2858  nec->prepareNeighborShapes(jvar->number());
2859 
2860  nec->computeOffDiagJacobian(jvar->number());
2861 
2862  // Cache the jacobian block for the secondary side
2863  nec->addJacobian(_fe_problem.assembly(0, number()),
2864  nec->_Kee,
2865  secondary_dofs,
2866  nec->_connected_dof_indices,
2867  nec->variable().scalingFactor());
2868 
2869  // Cache the jacobian block for the primary side
2870  nec->addJacobian(_fe_problem.assembly(0, number()),
2871  nec->_Kne,
2872  nec->variable().dofIndicesNeighbor(),
2873  nec->_connected_dof_indices,
2874  nec->variable().scalingFactor());
2875 
2878  }
2879  }
2880  }
2881  }
2882  }
2883  }
2884  }
2885  }
2886  // See if constraints were applied anywhere
2887  _communicator.max(constraints_applied);
2888 
2889  if (constraints_applied)
2890  {
2891  LibmeshPetscCall(MatSetOption(static_cast<PetscMatrix<Number> &>(jacobian).mat(),
2892  MAT_KEEP_NONZERO_PATTERN, // This is changed in 3.1
2893  PETSC_TRUE));
2894 
2895  jacobian.close();
2896  jacobian.zero_rows(zero_rows, 0.0);
2897  jacobian.close();
2899  jacobian.close();
2900  }
2901 }
2902 
2903 void
2905 {
2906  MooseObjectWarehouse<ScalarKernelBase> * scalar_kernel_warehouse;
2907 
2908  if (!tags.size() || tags.size() == _fe_problem.numMatrixTags())
2909  scalar_kernel_warehouse = &_scalar_kernels;
2910  else if (tags.size() == 1)
2911  scalar_kernel_warehouse = &(_scalar_kernels.getMatrixTagObjectWarehouse(*(tags.begin()), 0));
2912  else
2913  scalar_kernel_warehouse = &(_scalar_kernels.getMatrixTagsObjectWarehouse(tags, 0));
2914 
2915  // Compute the diagonal block for scalar variables
2916  if (scalar_kernel_warehouse->hasActiveObjects())
2917  {
2918  const auto & scalars = scalar_kernel_warehouse->getActiveObjects();
2919 
2920  _fe_problem.reinitScalars(/*tid=*/0);
2921 
2922  _fe_problem.reinitOffDiagScalars(/*_tid*/ 0);
2923 
2924  bool have_scalar_contributions = false;
2925  for (const auto & kernel : scalars)
2926  {
2927  if (!kernel->computesJacobian())
2928  continue;
2929 
2930  kernel->reinit();
2931  const std::vector<dof_id_type> & dof_indices = kernel->variable().dofIndices();
2932  const DofMap & dof_map = kernel->variable().dofMap();
2933  const dof_id_type first_dof = dof_map.first_dof();
2934  const dof_id_type end_dof = dof_map.end_dof();
2935  for (dof_id_type dof : dof_indices)
2936  {
2937  if (dof >= first_dof && dof < end_dof)
2938  {
2939  kernel->computeJacobian();
2940  _fe_problem.addJacobianOffDiagScalar(kernel->variable().number());
2941  have_scalar_contributions = true;
2942  break;
2943  }
2944  }
2945  }
2946 
2947  if (have_scalar_contributions)
2949  }
2950 }
2951 
2952 void
2954 {
2956 
2957  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
2958  {
2959  _kernels.jacobianSetup(tid);
2962  if (_doing_dg)
2968  }
2975 
2976 #ifdef MOOSE_KOKKOS_ENABLED
2981 #endif
2982 
2983  // Avoid recursion
2984  if (this == &_fe_problem.currentNonlinearSystem())
2986 }
2987 
2988 void
2990 {
2991  TIME_SECTION("computeJacobianInternal", 3);
2992 
2994 
2995  // Make matrix ready to use
2997 
2998  for (auto tag : tags)
2999  {
3000  if (!hasMatrix(tag))
3001  continue;
3002 
3003  auto & jacobian = getMatrix(tag);
3004  // Necessary for speed
3005  if (auto petsc_matrix = dynamic_cast<PetscMatrix<Number> *>(&jacobian))
3006  {
3007  LibmeshPetscCall(MatSetOption(petsc_matrix->mat(),
3008  MAT_KEEP_NONZERO_PATTERN, // This is changed in 3.1
3009  PETSC_TRUE));
3011  LibmeshPetscCall(
3012  MatSetOption(petsc_matrix->mat(), MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
3014  LibmeshPetscCall(MatSetOption(static_cast<PetscMatrix<Number> &>(jacobian).mat(),
3015  MAT_IGNORE_ZERO_ENTRIES,
3016  PETSC_TRUE));
3017  }
3018  }
3019 
3020  jacobianSetup();
3021 
3022  // Jacobian contributions from UOs - for now this is used for ray tracing
3023  // and ray kernels that contribute to the Jacobian (think line sources)
3024  std::vector<UserObject *> uos;
3026  .query()
3027  .condition<AttribSystem>("UserObject")
3028  .condition<AttribExecOns>(EXEC_PRE_KERNELS)
3029  .queryInto(uos);
3030  for (auto & uo : uos)
3031  uo->jacobianSetup();
3032  for (auto & uo : uos)
3033  {
3034  uo->initialize();
3035  uo->execute();
3036  uo->finalize();
3037  }
3038 
3039  // reinit scalar variables
3040  for (unsigned int tid = 0; tid < libMesh::n_threads(); tid++)
3042 
3043 #ifdef MOOSE_KOKKOS_ENABLED
3045  computeKokkosJacobian(tags);
3046 #endif
3047 
3048  PARALLEL_TRY
3049  {
3050  // We would like to compute ScalarKernels, block NodalKernels, FVFluxKernels, and mortar objects
3051  // up front because we want these included whether we are computing an ordinary Jacobian or a
3052  // Jacobian for determining variable scaling factors
3054 
3055  // Block restricted Nodal Kernels
3057  {
3060  Threads::parallel_reduce(range, cnkjt);
3061 
3062  unsigned int n_threads = libMesh::n_threads();
3063  for (unsigned int i = 0; i < n_threads;
3064  i++) // Add any cached jacobians that might be hanging around
3066  }
3067 
3069  if (_fe_problem.haveFV())
3070  {
3071  // the same loop works for both residual and jacobians because it keys
3072  // off of FEProblem's _currently_computing_jacobian parameter
3074  _fe_problem, this->number(), tags, /*on_displaced=*/false);
3076  Threads::parallel_reduce(faces, fvj);
3077  }
3079  displaced_problem && displaced_problem->haveFV())
3080  {
3082  _fe_problem, this->number(), tags, /*on_displaced=*/true);
3083  FVRange faces(displaced_problem->mesh().ownedFaceInfoBegin(),
3084  displaced_problem->mesh().ownedFaceInfoEnd());
3085  Threads::parallel_reduce(faces, fvr);
3086  }
3087 
3089 
3090  // Get our element range for looping over
3092 
3094  {
3095  // Only compute Jacobians corresponding to the diagonals of volumetric compute objects
3096  // because this typically gives us a good representation of the physics. NodalBCs and
3097  // Constraints can introduce dramatically different scales (often order unity).
3098  // IntegratedBCs and/or InterfaceKernels may use penalty factors. DGKernels may be ok, but
3099  // they are almost always used in conjunction with Kernels
3101  Threads::parallel_reduce(elem_range, cj);
3102  unsigned int n_threads = libMesh::n_threads();
3103  for (unsigned int i = 0; i < n_threads;
3104  i++) // Add any Jacobian contributions still hanging around
3106 
3107  // Check whether any exceptions were thrown and propagate this information for parallel
3108  // consistency before
3109  // 1) we do parallel communication when closing tagged matrices
3110  // 2) early returning before reaching our PARALLEL_CATCH below
3112 
3113  closeTaggedMatrices(tags);
3114 
3115  return;
3116  }
3117 
3118  switch (_fe_problem.coupling())
3119  {
3120  case Moose::COUPLING_DIAG:
3121  {
3123  Threads::parallel_reduce(elem_range, cj);
3124 
3125  unsigned int n_threads = libMesh::n_threads();
3126  for (unsigned int i = 0; i < n_threads;
3127  i++) // Add any Jacobian contributions still hanging around
3129 
3130  // Boundary restricted Nodal Kernels
3132  {
3135 
3136  Threads::parallel_reduce(bnd_range, cnkjt);
3137  unsigned int n_threads = libMesh::n_threads();
3138  for (unsigned int i = 0; i < n_threads;
3139  i++) // Add any cached jacobians that might be hanging around
3141  }
3142  }
3143  break;
3144 
3145  default:
3147  {
3149  Threads::parallel_reduce(elem_range, cj);
3150  unsigned int n_threads = libMesh::n_threads();
3151 
3152  for (unsigned int i = 0; i < n_threads; i++)
3154 
3155  // Boundary restricted Nodal Kernels
3157  {
3160 
3161  Threads::parallel_reduce(bnd_range, cnkjt);
3162  unsigned int n_threads = libMesh::n_threads();
3163  for (unsigned int i = 0; i < n_threads;
3164  i++) // Add any cached jacobians that might be hanging around
3166  }
3167  }
3168  break;
3169  }
3170 
3171  computeDiracContributions(tags, true);
3172 
3173  static bool first = true;
3174 
3175  // This adds zeroes into geometric coupling entries to ensure they stay in the matrix
3176  if ((_fe_problem.restoreOriginalNonzeroPattern() || first) &&
3178  {
3179  first = false;
3181 
3184  }
3185  }
3186  PARALLEL_CATCH;
3187 
3188  // Have no idea how to have constraints work
3189  // with the tag system
3190  PARALLEL_TRY
3191  {
3192  // Add in Jacobian contributions from other Constraints
3193  if (_fe_problem._has_constraints && tags.count(systemMatrixTag()))
3194  {
3195  // Some constraints need to be able to read values from the Jacobian, which requires that it
3196  // be closed/assembled
3197  auto & system_matrix = getMatrix(systemMatrixTag());
3198  std::unique_ptr<SparseMatrix<Number>> hash_copy;
3199  const SparseMatrix<Number> * view_jac_ptr;
3200  auto make_readable_jacobian = [&]()
3201  {
3202 #if PETSC_RELEASE_GREATER_EQUALS(3, 23, 0)
3203  if (system_matrix.use_hash_table())
3204  {
3205  hash_copy = libMesh::cast_ref<PetscMatrix<Number> &>(system_matrix).copy_from_hash();
3206  view_jac_ptr = hash_copy.get();
3207  }
3208  else
3209  view_jac_ptr = &system_matrix;
3210 #else
3211  view_jac_ptr = &system_matrix;
3212 #endif
3213  if (view_jac_ptr == &system_matrix)
3214  system_matrix.close();
3215  };
3216 
3217  make_readable_jacobian();
3218 
3219  // Nodal Constraints
3220  const bool had_nodal_constraints = enforceNodalConstraintsJacobian(*view_jac_ptr);
3221  if (had_nodal_constraints)
3222  // We have to make a new readable Jacobian
3223  make_readable_jacobian();
3224 
3225  // Undisplaced Constraints
3226  constraintJacobians(*view_jac_ptr, false);
3227 
3228  // Displaced Constraints
3230  constraintJacobians(*view_jac_ptr, true);
3231  }
3232  }
3233  PARALLEL_CATCH;
3234 
3236  closeTaggedMatrices(tags);
3237 
3238  // We need to close the save_in variables on the aux system before NodalBCBases clear the dofs
3239  // on boundary nodes
3242 
3243  if (hasDiagSaveIn())
3245 
3246  // Accumulate the occurrence of solution invalid warnings for the current iteration cumulative
3247  // counters
3250 }
3251 
3252 void
3254 {
3255  _nl_matrix_tags.clear();
3256 
3257  auto & tags = _fe_problem.getMatrixTags();
3258 
3259  for (auto & tag : tags)
3260  _nl_matrix_tags.insert(tag.second);
3261 
3262  computeJacobian(jacobian, _nl_matrix_tags);
3263 }
3264 
3265 void
3266 NonlinearSystemBase::computeJacobian(SparseMatrix<Number> & jacobian, const std::set<TagID> & tags)
3267 {
3269 
3270  computeJacobianTags(tags);
3271 
3273 }
3274 
3275 void
3276 NonlinearSystemBase::computeJacobianTags(const std::set<TagID> & tags)
3277 {
3278  TIME_SECTION("computeJacobianTags", 5);
3279 
3280  FloatingPointExceptionGuard fpe_guard(_app);
3281 
3282  try
3283  {
3285  }
3286  catch (MooseException & e)
3287  {
3288  // The buck stops here, we have already handled the exception by
3289  // calling stopSolve(), it is now up to PETSc to return a
3290  // "diverged" reason during the next solve.
3291  }
3292 }
3293 
3294 void
3296 {
3297  _nl_matrix_tags.clear();
3298 
3299  auto & tags = _fe_problem.getMatrixTags();
3300  for (auto & tag : tags)
3301  _nl_matrix_tags.insert(tag.second);
3302 
3304 }
3305 
3306 void
3308  const std::set<TagID> & tags)
3309 {
3310  TIME_SECTION("computeJacobianBlocks", 3);
3311  FloatingPointExceptionGuard fpe_guard(_app);
3312 
3313  for (unsigned int i = 0; i < blocks.size(); i++)
3314  {
3315  SparseMatrix<Number> & jacobian = blocks[i]->_jacobian;
3316 
3317  LibmeshPetscCall(MatSetOption(static_cast<PetscMatrix<Number> &>(jacobian).mat(),
3318  MAT_KEEP_NONZERO_PATTERN, // This is changed in 3.1
3319  PETSC_TRUE));
3321  LibmeshPetscCall(MatSetOption(static_cast<PetscMatrix<Number> &>(jacobian).mat(),
3322  MAT_NEW_NONZERO_ALLOCATION_ERR,
3323  PETSC_TRUE));
3324 
3325  jacobian.zero();
3326  }
3327 
3328  for (unsigned int tid = 0; tid < libMesh::n_threads(); tid++)
3330 
3331  PARALLEL_TRY
3332  {
3335  Threads::parallel_reduce(elem_range, cjb);
3336  }
3337  PARALLEL_CATCH;
3338 
3339  for (unsigned int i = 0; i < blocks.size(); i++)
3340  blocks[i]->_jacobian.close();
3341 
3342  for (unsigned int i = 0; i < blocks.size(); i++)
3343  {
3344  libMesh::System & precond_system = blocks[i]->_precond_system;
3345  SparseMatrix<Number> & jacobian = blocks[i]->_jacobian;
3346 
3347  unsigned int ivar = blocks[i]->_ivar;
3348  unsigned int jvar = blocks[i]->_jvar;
3349 
3350  // Dirichlet BCs
3351  std::vector<numeric_index_type> zero_rows;
3352  PARALLEL_TRY
3353  {
3355  for (const auto & bnode : bnd_nodes)
3356  {
3357  BoundaryID boundary_id = bnode->_bnd_id;
3358  Node * node = bnode->_node;
3359 
3360  if (_nodal_bcs.hasActiveBoundaryObjects(boundary_id))
3361  {
3362  const auto & bcs = _nodal_bcs.getActiveBoundaryObjects(boundary_id);
3363 
3364  if (node->processor_id() == processor_id())
3365  {
3366  _fe_problem.reinitNodeFace(node, boundary_id, 0);
3367 
3368  for (const auto & bc : bcs)
3369  if (bc->variable().number() == ivar && bc->shouldApply())
3370  {
3371  // The first zero is for the variable number... there is only one variable in
3372  // each mini-system The second zero only works with Lagrange elements!
3373  zero_rows.push_back(node->dof_number(precond_system.number(), 0, 0));
3374  }
3375  }
3376  }
3377  }
3378  }
3379  PARALLEL_CATCH;
3380 
3381  jacobian.close();
3382 
3383  // This zeroes the rows corresponding to Dirichlet BCs and puts a 1.0 on the diagonal
3384  if (ivar == jvar)
3385  jacobian.zero_rows(zero_rows, 1.0);
3386  else
3387  jacobian.zero_rows(zero_rows, 0.0);
3388 
3389  jacobian.close();
3390  }
3391 }
3392 
3393 void
3395 {
3402  _kernels.updateActive(tid);
3404 
3405  if (tid == 0)
3406  {
3414 
3415 #ifdef MOOSE_KOKKOS_ENABLED
3421 #endif
3422  }
3423 }
3424 
3425 Real
3427  const NumericVector<Number> & update)
3428 {
3429  // Default to no damping
3430  Real damping = 1.0;
3431  bool has_active_dampers = false;
3432 
3433  try
3434  {
3436  {
3437  PARALLEL_TRY
3438  {
3439  TIME_SECTION("computeDampers", 3, "Computing Dampers");
3440  has_active_dampers = true;
3443  Threads::parallel_reduce(_fe_problem.getCurrentAlgebraicElementRange(), cid);
3444  damping = std::min(cid.damping(), damping);
3445  }
3446  PARALLEL_CATCH;
3447  }
3448 
3450  {
3451  PARALLEL_TRY
3452  {
3453  TIME_SECTION("computeDamping::element", 3, "Computing Element Damping");
3454 
3455  has_active_dampers = true;
3458  Threads::parallel_reduce(_fe_problem.getCurrentAlgebraicNodeRange(), cndt);
3459  damping = std::min(cndt.damping(), damping);
3460  }
3461  PARALLEL_CATCH;
3462  }
3463 
3465  {
3466  PARALLEL_TRY
3467  {
3468  TIME_SECTION("computeDamping::general", 3, "Computing General Damping");
3469 
3470  has_active_dampers = true;
3471  const auto & gdampers = _general_dampers.getActiveObjects();
3472  for (const auto & damper : gdampers)
3473  {
3474  Real gd_damping = damper->computeDamping(solution, update);
3475  try
3476  {
3477  damper->checkMinDamping(gd_damping);
3478  }
3479  catch (MooseException & e)
3480  {
3482  }
3483  damping = std::min(gd_damping, damping);
3484  }
3485  }
3486  PARALLEL_CATCH;
3487  }
3488  }
3489  catch (MooseException & e)
3490  {
3491  // The buck stops here, we have already handled the exception by
3492  // calling stopSolve(), it is now up to PETSc to return a
3493  // "diverged" reason during the next solve.
3494  }
3495  catch (std::exception & e)
3496  {
3497  // Allow the libmesh error/exception on negative jacobian
3498  const std::string & message = e.what();
3499  if (message.find("Jacobian") == std::string::npos)
3500  throw;
3501  }
3502 
3503  _communicator.min(damping);
3504 
3505  if (has_active_dampers && damping < 1.0)
3506  _console << " Damping factor: " << damping << std::endl;
3507 
3508  return damping;
3509 }
3510 
3511 void
3512 NonlinearSystemBase::computeDiracContributions(const std::set<TagID> & tags, bool is_jacobian)
3513 {
3515 
3516  std::set<const Elem *> dirac_elements;
3517 
3519  {
3520  TIME_SECTION("computeDirac", 3, "Computing DiracKernels");
3521 
3522  // TODO: Need a threading fix... but it's complicated!
3523  for (THREAD_ID tid = 0; tid < libMesh::n_threads(); ++tid)
3524  {
3525  const auto & dkernels = _dirac_kernels.getActiveObjects(tid);
3526  for (const auto & dkernel : dkernels)
3527  {
3528  dkernel->clearPoints();
3529  dkernel->addPoints();
3530  }
3531  }
3532 
3533  ComputeDiracThread cd(_fe_problem, tags, is_jacobian);
3534 
3535  _fe_problem.getDiracElements(dirac_elements);
3536 
3537  DistElemRange range(dirac_elements.begin(), dirac_elements.end(), 1);
3538  // TODO: Make Dirac work thread!
3539  // Threads::parallel_reduce(range, cd);
3540 
3541  cd(range);
3542 
3543  if (is_jacobian)
3544  for (const auto tid : make_range(libMesh::n_threads()))
3546  }
3547 }
3548 
3551 {
3552  if (!_residual_copy.get())
3554 
3555  return *_residual_copy;
3556 }
3557 
3560 {
3561  _need_residual_ghosted = true;
3562  if (!_residual_ghosted)
3563  {
3564  // The first time we realize we need a ghosted residual vector,
3565  // we add it.
3566  _residual_ghosted = &addVector("residual_ghosted", false, GHOSTED);
3567 
3568  // If we've already realized we need time and/or non-time
3569  // residual vectors, but we haven't yet realized they need to be
3570  // ghosted, fix that now.
3571  //
3572  // If an application changes its mind, the libMesh API lets us
3573  // change the vector.
3574  if (_Re_time)
3575  {
3576  const auto vector_name = _subproblem.vectorTagName(_Re_time_tag);
3577  _Re_time = &system().add_vector(vector_name, false, GHOSTED);
3578  }
3579  if (_Re_non_time)
3580  {
3581  const auto vector_name = _subproblem.vectorTagName(_Re_non_time_tag);
3582  _Re_non_time = &system().add_vector(vector_name, false, GHOSTED);
3583  }
3584  }
3585  return *_residual_ghosted;
3586 }
3587 
3588 void
3590  std::vector<dof_id_type> & n_nz,
3591  std::vector<dof_id_type> & n_oz)
3592 {
3594  {
3596 
3597  std::unordered_map<dof_id_type, std::vector<dof_id_type>> graph;
3598 
3600 
3603  graph);
3604 
3605  const dof_id_type first_dof_on_proc = dofMap().first_dof(processor_id());
3606  const dof_id_type end_dof_on_proc = dofMap().end_dof(processor_id());
3607 
3608  // The total number of dofs on and off processor
3609  const dof_id_type n_dofs_on_proc = dofMap().n_local_dofs();
3610  const dof_id_type n_dofs_not_on_proc = dofMap().n_dofs() - dofMap().n_local_dofs();
3611 
3612  for (const auto & git : graph)
3613  {
3614  dof_id_type dof = git.first;
3615  dof_id_type local_dof = dof - first_dof_on_proc;
3616 
3617  if (dof < first_dof_on_proc || dof >= end_dof_on_proc)
3618  continue;
3619 
3620  const auto & row = git.second;
3621 
3622  SparsityPattern::Row & sparsity_row = sparsity[local_dof];
3623 
3624  unsigned int original_row_length = sparsity_row.size();
3625 
3626  sparsity_row.insert(sparsity_row.end(), row.begin(), row.end());
3627 
3628  SparsityPattern::sort_row(
3629  sparsity_row.begin(), sparsity_row.begin() + original_row_length, sparsity_row.end());
3630 
3631  // Fix up nonzero arrays
3632  for (const auto & coupled_dof : row)
3633  {
3634  if (coupled_dof < first_dof_on_proc || coupled_dof >= end_dof_on_proc)
3635  {
3636  if (n_oz[local_dof] < n_dofs_not_on_proc)
3637  n_oz[local_dof]++;
3638  }
3639  else
3640  {
3641  if (n_nz[local_dof] < n_dofs_on_proc)
3642  n_nz[local_dof]++;
3643  }
3644  }
3645  }
3646  }
3647 }
3648 
3649 void
3651 {
3652  *_u_dot = u_dot;
3653 }
3654 
3655 void
3657 {
3658  *_u_dotdot = u_dotdot;
3659 }
3660 
3661 void
3663 {
3664  *_u_dot_old = u_dot_old;
3665 }
3666 
3667 void
3669 {
3670  *_u_dotdot_old = u_dotdot_old;
3671 }
3672 
3673 void
3674 NonlinearSystemBase::setPreconditioner(std::shared_ptr<MoosePreconditioner> pc)
3675 {
3676  if (_preconditioner.get() != nullptr)
3677  mooseError("More than one active Preconditioner detected");
3678 
3679  _preconditioner = pc;
3680 }
3681 
3682 MoosePreconditioner const *
3684 {
3685  return _preconditioner.get();
3686 }
3687 
3688 void
3690 {
3691  _increment_vec = &_sys.add_vector("u_increment", true, GHOSTED);
3692 }
3693 
3694 void
3696  const std::set<MooseVariable *> & damped_vars)
3697 {
3698  for (const auto & var : damped_vars)
3699  var->computeIncrementAtQps(*_increment_vec);
3700 }
3701 
3702 void
3704  const std::set<MooseVariable *> & damped_vars)
3705 {
3706  for (const auto & var : damped_vars)
3707  var->computeIncrementAtNode(*_increment_vec);
3708 }
3709 
3710 void
3711 NonlinearSystemBase::checkKernelCoverage(const std::set<SubdomainID> & mesh_subdomains) const
3712 {
3713  // Obtain all blocks and variables covered by all kernels
3714  std::set<SubdomainID> input_subdomains;
3715  std::set<std::string> kernel_variables;
3716 
3717  bool global_kernels_exist = false;
3718  global_kernels_exist |= _scalar_kernels.hasActiveObjects();
3719  global_kernels_exist |= _nodal_kernels.hasActiveObjects();
3720 
3721  _kernels.subdomainsCovered(input_subdomains, kernel_variables);
3722  _dg_kernels.subdomainsCovered(input_subdomains, kernel_variables);
3723  _nodal_kernels.subdomainsCovered(input_subdomains, kernel_variables);
3724  _scalar_kernels.subdomainsCovered(input_subdomains, kernel_variables);
3725  _constraints.subdomainsCovered(input_subdomains, kernel_variables);
3726 
3727 #ifdef MOOSE_KOKKOS_ENABLED
3728  _kokkos_kernels.subdomainsCovered(input_subdomains, kernel_variables);
3729  _kokkos_nodal_kernels.subdomainsCovered(input_subdomains, kernel_variables);
3730 #endif
3731 
3732  if (_fe_problem.haveFV())
3733  {
3734  std::vector<FVElementalKernel *> fv_elemental_kernels;
3736  .query()
3737  .template condition<AttribSystem>("FVElementalKernel")
3738  .queryInto(fv_elemental_kernels);
3739 
3740  for (auto fv_kernel : fv_elemental_kernels)
3741  {
3742  if (fv_kernel->blockRestricted())
3743  for (auto block_id : fv_kernel->blockIDs())
3744  input_subdomains.insert(block_id);
3745  else
3746  global_kernels_exist = true;
3747  kernel_variables.insert(fv_kernel->variable().name());
3748 
3749  // Check for lagrange multiplier
3750  if (dynamic_cast<FVScalarLagrangeMultiplierConstraint *>(fv_kernel))
3751  kernel_variables.insert(dynamic_cast<FVScalarLagrangeMultiplierConstraint *>(fv_kernel)
3752  ->lambdaVariable()
3753  .name());
3754  }
3755 
3756  std::vector<FVFluxKernel *> fv_flux_kernels;
3758  .query()
3759  .template condition<AttribSystem>("FVFluxKernel")
3760  .queryInto(fv_flux_kernels);
3761 
3762  for (auto fv_kernel : fv_flux_kernels)
3763  {
3764  if (fv_kernel->blockRestricted())
3765  for (auto block_id : fv_kernel->blockIDs())
3766  input_subdomains.insert(block_id);
3767  else
3768  global_kernels_exist = true;
3769  kernel_variables.insert(fv_kernel->variable().name());
3770  }
3771 
3772  std::vector<FVInterfaceKernel *> fv_interface_kernels;
3774  .query()
3775  .template condition<AttribSystem>("FVInterfaceKernel")
3776  .queryInto(fv_interface_kernels);
3777 
3778  for (auto fvik : fv_interface_kernels)
3779  if (auto scalar_fvik = dynamic_cast<FVScalarLagrangeMultiplierInterface *>(fvik))
3780  kernel_variables.insert(scalar_fvik->lambdaVariable().name());
3781 
3782  std::vector<FVFluxBC *> fv_flux_bcs;
3784  .query()
3785  .template condition<AttribSystem>("FVFluxBC")
3786  .queryInto(fv_flux_bcs);
3787 
3788  for (auto fvbc : fv_flux_bcs)
3789  if (auto scalar_fvbc = dynamic_cast<FVBoundaryScalarLagrangeMultiplierConstraint *>(fvbc))
3790  kernel_variables.insert(scalar_fvbc->lambdaVariable().name());
3791  }
3792 
3793  for (const auto & ibc : _integrated_bcs.getActiveObjects())
3794  {
3795  const auto additional_variables_covered = ibc->additionalROVariables();
3796  kernel_variables.insert(additional_variables_covered.begin(),
3797  additional_variables_covered.end());
3798  }
3799 
3800  // Check kernel coverage of subdomains (blocks) in your mesh
3801  if (!global_kernels_exist)
3802  {
3803  std::set<SubdomainID> difference;
3804  std::set_difference(mesh_subdomains.begin(),
3805  mesh_subdomains.end(),
3806  input_subdomains.begin(),
3807  input_subdomains.end(),
3808  std::inserter(difference, difference.end()));
3809 
3810  // there supposed to be no kernels on this lower-dimensional subdomain
3811  for (const auto & id : _mesh.interiorLowerDBlocks())
3812  difference.erase(id);
3813  for (const auto & id : _mesh.boundaryLowerDBlocks())
3814  difference.erase(id);
3815 
3816  if (!difference.empty())
3817  {
3818  std::vector<SubdomainID> difference_vec =
3819  std::vector<SubdomainID>(difference.begin(), difference.end());
3820  std::vector<SubdomainName> difference_names = _mesh.getSubdomainNames(difference_vec);
3821  std::stringstream missing_block_names;
3822  std::copy(difference_names.begin(),
3823  difference_names.end(),
3824  std::ostream_iterator<std::string>(missing_block_names, " "));
3825  std::stringstream missing_block_ids;
3826  std::copy(difference.begin(),
3827  difference.end(),
3828  std::ostream_iterator<unsigned int>(missing_block_ids, " "));
3829 
3830  mooseError("Each subdomain must contain at least one Kernel.\nThe following block(s) lack an "
3831  "active kernel: " +
3832  missing_block_names.str(),
3833  " (ids: ",
3834  missing_block_ids.str(),
3835  ")");
3836  }
3837  }
3838 
3839  // Check kernel use of variables
3840  std::set<VariableName> variables(getVariableNames().begin(), getVariableNames().end());
3841 
3842  std::set<VariableName> difference;
3843  std::set_difference(variables.begin(),
3844  variables.end(),
3845  kernel_variables.begin(),
3846  kernel_variables.end(),
3847  std::inserter(difference, difference.end()));
3848 
3849  // skip checks for varaibles defined on lower-dimensional subdomain
3850  std::set<VariableName> vars(difference);
3851  for (auto & var_name : vars)
3852  {
3853  auto blks = getSubdomainsForVar(var_name);
3854  for (const auto & id : blks)
3855  if (_mesh.interiorLowerDBlocks().count(id) > 0 || _mesh.boundaryLowerDBlocks().count(id) > 0)
3856  difference.erase(var_name);
3857  }
3858 
3859  if (!difference.empty())
3860  {
3861  std::stringstream missing_kernel_vars;
3862  std::copy(difference.begin(),
3863  difference.end(),
3864  std::ostream_iterator<std::string>(missing_kernel_vars, " "));
3865  mooseError("Each variable must be referenced by at least one active Kernel.\nThe following "
3866  "variable(s) lack an active kernel: " +
3867  missing_kernel_vars.str());
3868  }
3869 }
3870 
3871 bool
3873 {
3874  auto & time_kernels = _kernels.getVectorTagObjectWarehouse(timeVectorTag(), 0);
3875 
3876  return time_kernels.hasActiveObjects();
3877 }
3878 
3879 std::vector<std::string>
3881 {
3882  std::vector<std::string> variable_names;
3883  const auto & time_kernels = _kernels.getVectorTagObjectWarehouse(timeVectorTag(), 0);
3884  if (time_kernels.hasActiveObjects())
3885  for (const auto & kernel : time_kernels.getObjects())
3886  variable_names.push_back(kernel->variable().name());
3887 
3888  return variable_names;
3889 }
3890 
3891 bool
3893 {
3894  // IntegratedBCs are for now the only objects we consider to be consuming
3895  // matprops on boundaries.
3896  if (_integrated_bcs.hasActiveBoundaryObjects(bnd_id, tid))
3897  for (const auto & bc : _integrated_bcs.getActiveBoundaryObjects(bnd_id, tid))
3898  if (std::static_pointer_cast<MaterialPropertyInterface>(bc)->getMaterialPropertyCalled())
3899  return true;
3900 
3901  // Thin layer heat transfer in the heat_transfer module is being used on a boundary even though
3902  // it's an interface kernel. That boundary is external, on both sides of a gap in a mesh
3904  for (const auto & ik : _interface_kernels.getActiveBoundaryObjects(bnd_id, tid))
3905  if (std::static_pointer_cast<MaterialPropertyInterface>(ik)->getMaterialPropertyCalled())
3906  return true;
3907 
3908  // Because MortarConstraints do not inherit from BoundaryRestrictable, they are not sorted
3909  // by boundary in the MooseObjectWarehouse. So for now, we return true for all boundaries
3910  // Note: constraints are not threaded at this time
3911  if (_constraints.hasActiveObjects(/*tid*/ 0))
3912  for (const auto & ct : _constraints.getActiveObjects(/*tid*/ 0))
3913  if (auto mpi = std::dynamic_pointer_cast<MaterialPropertyInterface>(ct);
3914  mpi && mpi->getMaterialPropertyCalled())
3915  return true;
3916  return false;
3917 }
3918 
3919 bool
3921 {
3922  // InterfaceKernels are for now the only objects we consider to be consuming matprops on internal
3923  // boundaries.
3925  for (const auto & ik : _interface_kernels.getActiveBoundaryObjects(bnd_id, tid))
3926  if (std::static_pointer_cast<MaterialPropertyInterface>(ik)->getMaterialPropertyCalled())
3927  return true;
3928  return false;
3929 }
3930 
3931 bool
3933 {
3934  // DGKernels are for now the only objects we consider to be consuming matprops on
3935  // internal sides.
3936  if (_dg_kernels.hasActiveBlockObjects(subdomain_id, tid))
3937  for (const auto & dg : _dg_kernels.getActiveBlockObjects(subdomain_id, tid))
3938  if (std::static_pointer_cast<MaterialPropertyInterface>(dg)->getMaterialPropertyCalled())
3939  return true;
3940  // NOTE:
3941  // HDG kernels do not require face material properties on internal sides at this time.
3942  // The idea is to have element locality of HDG for hybridization
3943  return false;
3944 }
3945 
3946 bool
3948 {
3949  return _doing_dg;
3950 }
3951 
3952 void
3954 {
3957 }
3958 
3959 void
3961  const std::set<TagID> & vector_tags,
3962  const std::set<TagID> & matrix_tags)
3963 {
3964  parallel_object_only();
3965 
3966  try
3967  {
3968  for (auto & map_pr : _undisplaced_mortar_functors)
3969  map_pr.second(compute_type, vector_tags, matrix_tags);
3970 
3971  for (auto & map_pr : _displaced_mortar_functors)
3972  map_pr.second(compute_type, vector_tags, matrix_tags);
3973  }
3974  catch (MetaPhysicL::LogicError &)
3975  {
3976  mooseError(
3977  "We caught a MetaPhysicL error in NonlinearSystemBase::mortarConstraints. This is very "
3978  "likely due to AD not having a sufficiently large derivative container size. Please run "
3979  "MOOSE configure with the '--with-derivative-size=<n>' option");
3980  }
3981 }
3982 
3983 void
3985 {
3986  if (_auto_scaling_initd)
3987  return;
3988 
3989  // Want the libMesh count of variables, not MOOSE, e.g. I don't care about array variable counts
3990  const auto n_vars = system().n_vars();
3991 
3992  if (_scaling_group_variables.empty())
3993  {
3994  _var_to_group_var.reserve(n_vars);
3996 
3997  for (const auto var_number : make_range(n_vars))
3998  _var_to_group_var.emplace(var_number, var_number);
3999  }
4000  else
4001  {
4002  std::set<unsigned int> var_numbers, var_numbers_covered, var_numbers_not_covered;
4003  for (const auto var_number : make_range(n_vars))
4004  var_numbers.insert(var_number);
4005 
4007 
4008  for (const auto group_index : index_range(_scaling_group_variables))
4009  for (const auto & var_name : _scaling_group_variables[group_index])
4010  {
4011  if (!hasVariable(var_name) && !hasScalarVariable(var_name))
4012  mooseError("'",
4013  var_name,
4014  "', provided to the 'scaling_group_variables' parameter, does not exist in "
4015  "the nonlinear system.");
4016 
4017  const MooseVariableBase & var =
4018  hasVariable(var_name)
4019  ? static_cast<MooseVariableBase &>(getVariable(0, var_name))
4020  : static_cast<MooseVariableBase &>(getScalarVariable(0, var_name));
4021  auto map_pair = _var_to_group_var.emplace(var.number(), group_index);
4022  if (!map_pair.second)
4023  mooseError("Variable ", var_name, " is contained in multiple scaling grouplings");
4024  var_numbers_covered.insert(var.number());
4025  }
4026 
4027  std::set_difference(var_numbers.begin(),
4028  var_numbers.end(),
4029  var_numbers_covered.begin(),
4030  var_numbers_covered.end(),
4031  std::inserter(var_numbers_not_covered, var_numbers_not_covered.begin()));
4032 
4033  _num_scaling_groups = _scaling_group_variables.size() + var_numbers_not_covered.size();
4034 
4035  auto index = static_cast<unsigned int>(_scaling_group_variables.size());
4036  for (auto var_number : var_numbers_not_covered)
4037  _var_to_group_var.emplace(var_number, index++);
4038  }
4039 
4040  _variable_autoscaled.resize(n_vars, true);
4041  const auto & number_to_var_map = _vars[0].numberToVariableMap();
4042 
4044  for (const auto i : index_range(_variable_autoscaled))
4047  libmesh_map_find(number_to_var_map, i)->name()) !=
4049  _variable_autoscaled[i] = false;
4050 
4051  _auto_scaling_initd = true;
4052 }
4053 
4054 bool
4056 {
4058  return true;
4059 
4060  _console << "\nPerforming automatic scaling calculation\n" << std::endl;
4061 
4062  TIME_SECTION("computeScaling", 3, "Computing Automatic Scaling");
4063 
4064  // It's funny but we need to assemble our vector of scaling factors here otherwise we will be
4065  // applying scaling factors of 0 during Assembly of our scaling Jacobian
4067 
4068  // container for repeated access of element global dof indices
4069  std::vector<dof_id_type> dof_indices;
4070 
4071  if (!_auto_scaling_initd)
4072  setupScalingData();
4073 
4074  std::vector<Real> inverse_scaling_factors(_num_scaling_groups, 0);
4075  std::vector<Real> resid_inverse_scaling_factors(_num_scaling_groups, 0);
4076  std::vector<Real> jac_inverse_scaling_factors(_num_scaling_groups, 0);
4077  auto & dof_map = dofMap();
4078 
4079  // what types of scaling do we want?
4080  bool jac_scaling = _resid_vs_jac_scaling_param < 1. - TOLERANCE;
4081  bool resid_scaling = _resid_vs_jac_scaling_param > TOLERANCE;
4082 
4083  const NumericVector<Number> & scaling_residual = RHS();
4084 
4085  if (jac_scaling)
4086  {
4087  // if (!_auto_scaling_initd)
4088  // We need to reinit this when the number of dofs changes
4089  // but there is no good way to track that
4090  // In theory, it is the job of libmesh system to track this,
4091  // but this special matrix is not owned by libMesh system
4092  // Let us reinit eveytime since it is not expensive
4093  {
4094  auto init_vector = NumericVector<Number>::build(this->comm());
4095  init_vector->init(system().n_dofs(), system().n_local_dofs(), /*fast=*/false, PARALLEL);
4096 
4097  _scaling_matrix->clear();
4098  _scaling_matrix->init(*init_vector);
4099  }
4100 
4102  // Dispatch to derived classes to ensure that we use the correct matrix tag
4105  }
4106 
4107  if (resid_scaling)
4108  {
4111  // Dispatch to derived classes to ensure that we use the correct vector tag
4115  }
4116 
4117  // Did something bad happen during residual/Jacobian scaling computation?
4119  return false;
4120 
4121  auto examine_dof_indices = [this,
4122  jac_scaling,
4123  resid_scaling,
4124  &dof_map,
4125  &jac_inverse_scaling_factors,
4126  &resid_inverse_scaling_factors,
4127  &scaling_residual](const auto & dof_indices, const auto var_number)
4128  {
4129  for (auto dof_index : dof_indices)
4130  if (dof_map.local_index(dof_index))
4131  {
4132  if (jac_scaling)
4133  {
4134  // For now we will use the diagonal for determining scaling
4135  auto mat_value = (*_scaling_matrix)(dof_index, dof_index);
4136  auto & factor = jac_inverse_scaling_factors[_var_to_group_var[var_number]];
4137  factor = std::max(factor, std::abs(mat_value));
4138  }
4139  if (resid_scaling)
4140  {
4141  auto vec_value = scaling_residual(dof_index);
4142  auto & factor = resid_inverse_scaling_factors[_var_to_group_var[var_number]];
4143  factor = std::max(factor, std::abs(vec_value));
4144  }
4145  }
4146  };
4147 
4148  // Compute our scaling factors for the spatial field variables
4149  for (const auto & elem : _fe_problem.getCurrentAlgebraicElementRange())
4150  for (const auto i : make_range(system().n_vars()))
4152  {
4153  dof_map.dof_indices(elem, dof_indices, i);
4154  examine_dof_indices(dof_indices, i);
4155  }
4156 
4157  for (const auto i : make_range(system().n_vars()))
4158  if (_variable_autoscaled[i] && system().variable_type(i).family == SCALAR)
4159  {
4160  dof_map.SCALAR_dof_indices(dof_indices, i);
4161  examine_dof_indices(dof_indices, i);
4162  }
4163 
4164  if (resid_scaling)
4165  _communicator.max(resid_inverse_scaling_factors);
4166  if (jac_scaling)
4167  _communicator.max(jac_inverse_scaling_factors);
4168 
4169  if (jac_scaling && resid_scaling)
4170  for (MooseIndex(inverse_scaling_factors) i = 0; i < inverse_scaling_factors.size(); ++i)
4171  {
4172  // Be careful not to take log(0)
4173  if (!resid_inverse_scaling_factors[i])
4174  {
4175  if (!jac_inverse_scaling_factors[i])
4176  inverse_scaling_factors[i] = 1;
4177  else
4178  inverse_scaling_factors[i] = jac_inverse_scaling_factors[i];
4179  }
4180  else if (!jac_inverse_scaling_factors[i])
4181  // We know the resid is not zero
4182  inverse_scaling_factors[i] = resid_inverse_scaling_factors[i];
4183  else
4184  inverse_scaling_factors[i] =
4185  std::exp(_resid_vs_jac_scaling_param * std::log(resid_inverse_scaling_factors[i]) +
4186  (1 - _resid_vs_jac_scaling_param) * std::log(jac_inverse_scaling_factors[i]));
4187  }
4188  else if (jac_scaling)
4189  inverse_scaling_factors = jac_inverse_scaling_factors;
4190  else if (resid_scaling)
4191  inverse_scaling_factors = resid_inverse_scaling_factors;
4192  else
4193  mooseError("We shouldn't be calling this routine if we're not performing any scaling");
4194 
4195  // We have to make sure that our scaling values are not zero
4196  for (auto & scaling_factor : inverse_scaling_factors)
4197  if (scaling_factor == 0)
4198  scaling_factor = 1;
4199 
4200  // Now flatten the group scaling factors to the individual variable scaling factors
4201  std::vector<Real> flattened_inverse_scaling_factors(system().n_vars());
4202  for (const auto i : index_range(flattened_inverse_scaling_factors))
4203  flattened_inverse_scaling_factors[i] = inverse_scaling_factors[_var_to_group_var[i]];
4204 
4205  // Now set the scaling factors for the variables
4206  applyScalingFactors(flattened_inverse_scaling_factors);
4208  displaced_problem->systemBaseNonlinear(number()).applyScalingFactors(
4209  flattened_inverse_scaling_factors);
4210 
4211  _computed_scaling = true;
4212  return true;
4213 }
4214 
4215 void
4217 {
4218  if (!hasVector("scaling_factors"))
4219  // No variables have indicated they need scaling
4220  return;
4221 
4222  auto & scaling_vector = getVector("scaling_factors");
4223 
4224  const auto & lm_mesh = _mesh.getMesh();
4225  const auto & dof_map = dofMap();
4226 
4227  const auto & field_variables = _vars[0].fieldVariables();
4228  const auto & scalar_variables = _vars[0].scalars();
4229 
4230  std::vector<dof_id_type> dof_indices;
4231 
4232  for (const Elem * const elem :
4233  as_range(lm_mesh.active_local_elements_begin(), lm_mesh.active_local_elements_end()))
4234  for (const auto * const field_var : field_variables)
4235  {
4236  const auto & factors = field_var->arrayScalingFactor();
4237  for (const auto i : make_range(field_var->count()))
4238  {
4239  dof_map.dof_indices(elem, dof_indices, field_var->number() + i);
4240  for (const auto dof : dof_indices)
4241  scaling_vector.set(dof, factors[i]);
4242  }
4243  }
4244 
4245  for (const auto * const scalar_var : scalar_variables)
4246  {
4247  mooseAssert(scalar_var->count() == 1,
4248  "Scalar variables should always have only one component.");
4249  dof_map.SCALAR_dof_indices(dof_indices, scalar_var->number());
4250  for (const auto dof : dof_indices)
4251  scaling_vector.set(dof, scalar_var->scalingFactor());
4252  }
4253 
4254  // Parallel assemble
4255  scaling_vector.close();
4256 
4257  if (auto * displaced_problem = _fe_problem.getDisplacedProblem().get())
4258  // copy into the corresponding displaced system vector because they should be the exact same
4259  displaced_problem->systemBaseNonlinear(number()).getVector("scaling_factors") = scaling_vector;
4260 }
4261 
4262 bool
4264 {
4265  // Clear the iteration counters
4266  _current_l_its.clear();
4267  _current_nl_its = 0;
4268 
4269  // Initialize the solution vector using a predictor and known values from nodal bcs
4271 
4272  // Now that the initial solution has ben set, potentially perform a residual/Jacobian evaluation
4273  // to determine variable scaling factors
4274  if (_automatic_scaling)
4275  {
4276  const bool scaling_succeeded = computeScaling();
4277  if (!scaling_succeeded)
4278  return false;
4279  }
4280 
4281  // We do not know a priori what variable a global degree of freedom corresponds to, so we need a
4282  // map from global dof to scaling factor. We just use a ghosted NumericVector for that mapping
4284 
4285  return true;
4286 }
4287 
4288 void
4290 {
4291  if (matrixFromColoring())
4292  LibmeshPetscCall(MatFDColoringDestroy(&_fdcoloring));
4293 }
4294 
4297 {
4298  if (!_fsp)
4299  mooseError("No field split preconditioner is present for this system");
4300 
4301  return *_fsp;
4302 }
std::string name(const ElemQuality q)
std::vector< std::shared_ptr< TimeIntegrator > > _time_integrators
Time integrator.
Definition: SystemBase.h:1049
virtual void setSolutionUDotDotOld(const NumericVector< Number > &u_dotdot_old)
virtual void reinitNeighborPhys(const Elem *neighbor, unsigned int neighbor_side, const std::vector< Point > &physical_points, const THREAD_ID tid)=0
virtual void residualSetup(THREAD_ID tid=0) const
NumericVector< Number > & getResidualTimeVector()
Return a numeric vector that is associated with the time tag.
void setActiveMaterialProperties(const std::unordered_set< unsigned int > &mat_prop_ids, const THREAD_ID tid)
Record and set the material properties required by the current computing thread.
MooseObjectTagWarehouse< NodalKernelBase > _nodal_kernels
NodalKernels for each thread.
NumericVector< Number > * _Re_time
residual vector for time contributions
void computeJacobianBlocks(std::vector< JacobianBlock *> &blocks)
Computes several Jacobian blocks simultaneously, summing their contributions into smaller preconditio...
virtual void insert(const T *v, const std::vector< numeric_index_type > &dof_indices)
MetaPhysicL::DualNumber< V, D, asd > abs(const MetaPhysicL::DualNumber< V, D, asd > &a)
Definition: EigenADReal.h:50
TagID _Re_time_tag
Tag for time contribution residual.
void allgather(const T &send_data, std::vector< T, A > &recv_data) const
std::map< std::pair< BoundaryID, BoundaryID >, PenetrationLocator * > _penetration_locators
dof_id_type end_dof(const processor_id_type proc) const
bool empty() const
virtual void addKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds a kernel.
virtual void setSolutionUDotDot(const NumericVector< Number > &udotdot)
Set transient term used by residual and Jacobian evaluation.
dof_id_type dof_number(const unsigned int s, const unsigned int var, const unsigned int comp) const
A kernel for hybridized finite element formulations.
Definition: HDGKernel.h:17
MooseObjectTagWarehouse< ResidualObject > _kokkos_nodal_kernels
KOKKOS_INLINE_FUNCTION const T * find(const T &target, const T *const begin, const T *const end)
Find a value in an array.
Definition: KokkosUtils.h:40
void reinitIncrementAtNodeForDampers(THREAD_ID tid, const std::set< MooseVariable *> &damped_vars)
Compute the incremental change in variables at nodes for dampers.
void overwriteNodeFace(NumericVector< Number > &soln)
Called from explicit time stepping to overwrite boundary positions (explicit dynamics).
Base class for deriving general dampers.
Definition: GeneralDamper.h:21
bool _use_pre_smo_residual
Whether to use the pre-SMO initial residual in the relative convergence check.
MoosePreconditioner const * getPreconditioner() const
virtual const char * what() const
Get out the error message.
std::vector< std::pair< MooseVariableFEBase *, MooseVariableFEBase * > > & couplingEntries(const THREAD_ID tid, const unsigned int nl_sys_num)
void findImplicitGeometricCouplingEntries(GeometricSearchData &geom_search_data, std::unordered_map< dof_id_type, std::vector< dof_id_type >> &graph)
Finds the implicit sparsity graph between geometrically related dofs.
void setupDampers()
Setup damping stuff (called before we actually start)
Real _initial_residual
The initial (i.e., 0th nonlinear iteration) residual, see setPreSMOResidual for a detailed explanatio...
unsigned int n_threads()
std::vector< bool > _variable_autoscaled
Container to hold flag if variable is to participate in autoscaling.
void zeroVectorForResidual(const std::string &vector_name)
virtual void cacheResidualNeighbor(const THREAD_ID tid) override
bool identifyVariableGroupsInNL() const
Whether to identify variable groups in nonlinear systems.
void zeroTaggedVectors(const std::set< TagID > &tags)
Zero all vectors for given tags.
Definition: SystemBase.C:694
const std::set< SubdomainID > & interiorLowerDBlocks() const
Definition: MooseMesh.h:1549
Base class for split-based preconditioners.
Definition: Split.h:25
bool hasActiveBlockObjects(THREAD_ID tid=0) const
std::shared_ptr< DisplacedProblem > displaced_problem
bool hasVector(const std::string &tag_name) const
Check if the named vector exists in the system.
Definition: SystemBase.C:925
virtual void checkExceptionAndStopSolve(bool print_message=true)
Check to see if an exception has occurred on any processor and, if possible, force the solve to fail...
unsigned int n_comp(const unsigned int s, const unsigned int var) const
void applyScalingFactors(const std::vector< Real > &inverse_scaling_factors)
Applies scaling factors to the system&#39;s variables.
Definition: SystemBase.C:1497
bool _debugging_residuals
true if debugging residuals
SCALAR
BoundaryID _secondary_boundary
NumericVector< Number > * _Re_non_time
residual vector for non-time contributions
const std::map< SubdomainID, std::vector< std::shared_ptr< T > > > & getActiveBlockObjects(THREAD_ID tid=0) const
MooseObjectTagWarehouse< ResidualObject > _kokkos_kernels
unsigned int TagID
Definition: MooseTypes.h:238
Real computeDamping(const NumericVector< Number > &solution, const NumericVector< Number > &update)
Compute damping.
virtual void reinitNode(const Node *node, const THREAD_ID tid) override
virtual void setPreviousNewtonSolution(const NumericVector< Number > &soln)
virtual void predictorCleanup(NumericVector< libMesh::Number > &ghosted_solution)
Perform cleanup tasks after application of predictor to solution vector.
bool _assemble_constraints_separately
Whether or not to assemble the residual and Jacobian after the application of each constraint...
virtual Elem * elemPtr(const dof_id_type i)
Definition: MooseMesh.C:3240
auto exp(const T &)
TagID systemMatrixTag() const override
Return the Matrix Tag ID for System.
MPI_Info info
NumericVector< Number > & solution()
Definition: SystemBase.h:197
bool hasObjects(THREAD_ID tid=0) const
Convenience functions for determining if objects exist.
MooseObjectTagWarehouse< DGKernelBase > _dg_kernels
virtual bool haveFV() const override
returns true if this problem includes/needs finite volume functionality.
char ** blocks
face_info_iterator ownedFaceInfoBegin()
Iterators to owned faceInfo objects.
Definition: MooseMesh.C:1527
PARALLEL
void reinitIncrementAtQpsForDampers(THREAD_ID tid, const std::set< MooseVariable *> &damped_vars)
Compute the incremental change in variables at QPs for dampers.
void computeResidualAndJacobianInternal(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags)
Compute residual and Jacobian from contributions not related to constraints, such as nodal boundary c...
bool useHashTableMatrixAssembly() const
void addImplicitGeometricCouplingEntriesToJacobian(bool add=true)
If called with true this will add entries into the jacobian to link together degrees of freedom that ...
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
bool areCoupled(const unsigned int ivar, const unsigned int jvar, const unsigned int nl_sys_num) const
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:311
void accumulateIterationIntoTimeStepOccurences()
Pass the number of solution invalid occurrences from current iteration to cumulative counters...
unsigned int number() const
Get variable number coming from libMesh.
virtual void initialSetup() override
Setup Functions.
Data structure used to hold penetration information.
std::vector< std::pair< R1, R2 > > get(const std::string &param1, const std::string &param2) const
Combine two vector parameters into a single vector of pairs.
const std::vector< std::shared_ptr< NodalConstraint > > & getActiveNodalConstraints() const
Access methods for active objects.
NumericVector< Number > * _u_dot_old
old solution vector for u^dot
Definition: SystemBase.h:1011
bool _has_nodalbc_diag_save_in
If there is a nodal BC having diag_save_in.
virtual void reinitScalars(const THREAD_ID tid, bool reinit_for_derivative_reordering=false) override
fills the VariableValue arrays for scalar variables from the solution vector
Base class for automatic differentiation Dirichlet BCs.
void add(std::shared_ptr< MooseObject > obj)
add adds a new object to the warehouse and stores attributes/metadata about it for running queries/fi...
Definition: TheWarehouse.C:116
virtual void getDiracElements(std::set< const Elem *> &elems) override
Fills "elems" with the elements that should be looped over for Dirac Kernels.
void setupDM()
Setup the PETSc DM object (when appropriate)
void setCurrentlyComputingResidual(bool currently_computing_residual) final
Set whether or not the problem is in the process of computing the residual.
void checkKernelCoverage(const std::set< SubdomainID > &mesh_subdomains) const
void addDGKernel(std::string dg_kernel_name, const std::string &name, InputParameters &parameters)
Adds a DG kernel.
void computeJacobian(libMesh::SparseMatrix< Number > &jacobian, const std::set< TagID > &tags)
Associate jacobian to systemMatrixTag, and then form a matrix for all the tags.
const InputParameters & parameters() const
Get the parameters of the object.
Definition: MooseBase.h:131
char ** vars
virtual void reinitNeighborPhys(const Elem *neighbor, unsigned int neighbor_side, const std::vector< Point > &physical_points, const THREAD_ID tid) override
void computeKokkosResidualAndJacobian(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags)
virtual TagID addVectorTag(const TagName &tag_name, const Moose::VectorTagType type=Moose::VECTOR_TAG_RESIDUAL)
Create a Tag.
Definition: SubProblem.C:93
std::vector< T * > & queryInto(std::vector< T *> &results, Args &&... args)
queryInto executes the query and stores the results in the given vector.
Definition: TheWarehouse.h:312
void getNodeDofs(dof_id_type node_id, std::vector< dof_id_type > &dofs)
Base class for all Constraint types.
Definition: Constraint.h:19
std::set< TagID > _nl_vector_tags
Vector tags to temporarily store all tags associated with the current system.
std::vector< std::string > _ignore_variables_for_autoscaling
A container for variables that do not partipate in autoscaling.
void residualSetup() override
Base boundary condition of a Dirichlet type.
virtual void associateVectorToTag(NumericVector< Number > &vec, TagID tag)
Associate a vector for a given tag.
Definition: SystemBase.C:982
virtual void reinitNodes(const std::vector< dof_id_type > &nodes, const THREAD_ID tid) override
MooseObjectTagWarehouse< NodalBCBase > _nodal_bcs
virtual void addJacobianOffDiagScalar(unsigned int ivar, const THREAD_ID tid=0)
virtual void setException(const std::string &message)
Set an exception, which is stored at this point by toggling a member variable in this class...
const std::vector< std::shared_ptr< NodeFaceConstraint > > & getActiveNodeFaceConstraints(BoundaryID boundary_id, bool displaced) const
virtual void customSetup(const ExecFlagType &exec_type, THREAD_ID tid=0) const
Factory & _factory
Definition: SystemBase.h:989
MooseObjectWarehouse< T > & getVectorTagsObjectWarehouse(const std::set< TagID > &tags, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object at least has one of the given vector ta...
virtual const Node * queryNodePtr(const dof_id_type i) const
Definition: MooseMesh.C:860
const ElementPairInfo & getElemPairInfo(std::pair< const Elem *, const Elem *> elem_pair) const
MooseObjectWarehouseBase< Split > _splits
Decomposition splits.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
NumericVector< Number > * _u_dotdot
solution vector for u^dotdot
Definition: SystemBase.h:1008
bool hasActiveMortarConstraints(const std::pair< BoundaryID, BoundaryID > &mortar_interface_key, bool displaced) const
const Parallel::Communicator & comm() const
NumericVector< Number > & add_vector(std::string_view vec_name, const bool projections=true, const ParallelType type=PARALLEL)
Solving a linear problem.
Definition: MooseTypes.h:897
bool _has_nodalbc_save_in
If there is a nodal BC having save_in.
bool hasActiveNodalConstraints() const
Deterimine if active objects exist.
virtual bool hasMatrix(TagID tag) const
Check if the tagged matrix exists in the system.
Definition: SystemBase.h:361
MooseObjectWarehouse< NodalDamper > _nodal_dampers
Nodal Dampers for each thread.
void computeNodalBCsResidualAndJacobian(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags)
Compute the residual and Jacobian together for nodal boundary conditions.
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
virtual libMesh::NonlinearSolver< Number > * nonlinearSolver()=0
dof_id_type n_dofs(const unsigned int vn) const
Real preSMOResidual() const
The pre-SMO residual.
std::unique_ptr< libMesh::DiagonalMatrix< Number > > _scaling_matrix
A diagonal matrix used for computing scaling.
virtual void setSolutionUDotOld(const NumericVector< Number > &u_dot_old)
virtual void associateMatrixToTag(libMesh::SparseMatrix< Number > &matrix, TagID tag)
Associate a matrix to a tag.
Definition: SystemBase.C:1077
bool hasDiagSaveIn() const
Weather or not the nonlinear system has diagonal Jacobian save-ins.
This class provides an interface for common operations on field variables of both FE and FV types wit...
const Parallel::Communicator & _communicator
void updateActive(THREAD_ID tid=0) override
Update the various active lists.
Real initialResidual() const
The initial residual.
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
bool needInterfaceMaterialOnSide(BoundaryID bnd_id, THREAD_ID tid) const
Indicated whether this system needs material properties on interfaces.
const libMesh::ConstElemRange & getCurrentAlgebraicElementRange()
These are the element and nodes that contribute to the jacobian and residual for this local processor...
MooseObjectWarehouse< ResidualObject > _kokkos_preset_nodal_bcs
std::size_t _num_scaling_groups
The number of scaling groups.
const libMesh::ConstNodeRange & getCurrentAlgebraicNodeRange()
void computingScalingJacobian(bool computing_scaling_jacobian)
Setter for whether we&#39;re computing the scaling jacobian.
dof_id_type n_local_dofs(const unsigned int vn) const
virtual void updateActive(THREAD_ID tid=0) override
Update the active status of Kernels.
bool has_dofs(const unsigned int s=libMesh::invalid_uint) const
std::map< dof_id_type, PenetrationInfo * > & _penetration_info
Data structure of nodes and their associated penetration information.
bool hasActiveNodeElemConstraints(SubdomainID secondary_id, SubdomainID primary_id, bool displaced) const
Real _pre_smo_residual
The pre-SMO residual, see setPreSMOResidual for a detailed explanation.
bool hasDampers()
Whether or not this system has dampers.
const std::vector< std::shared_ptr< NodeElemConstraintBase > > & getActiveNodeElemConstraints(SubdomainID secondary_id, SubdomainID primary_id, bool displaced) const
bool _compute_scaling_once
Whether the scaling factors should only be computed once at the beginning of the simulation through a...
void computeResidualTags(const std::set< TagID > &tags)
Form multiple tag-associated residual vectors for all the given tags.
virtual void cacheJacobianNeighbor(const THREAD_ID tid) override
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
bool _has_save_in
If there is any Kernel or IntegratedBC having save_in.
virtual const Node & nodeRef(const dof_id_type i) const
Definition: MooseMesh.C:834
TagID _Ke_system_tag
Tag for system contribution Jacobian.
virtual void setResidual(NumericVector< libMesh::Number > &residual, const THREAD_ID tid) override
dof_id_type n_dofs() const
virtual void disassociateMatrixFromTag(libMesh::SparseMatrix< Number > &matrix, TagID tag)
Disassociate a matrix from a tag.
Definition: SystemBase.C:1089
Scope guard for starting and stopping Floating Point Exception Trapping.
virtual void zero()=0
auto max(const L &left, const R &right)
Serves as a base class for DGKernel and ADDGKernel.
Definition: DGKernelBase.h:32
const Variable & variable(const unsigned int c) const override
void constraintResiduals(NumericVector< Number > &residual, bool displaced)
Add residual contributions from Constraints.
Base class for MOOSE preconditioners.
NumericVector< Number > & addVector(const std::string &vector_name, const bool project, const libMesh::ParallelType type)
Adds a solution length vector to the system.
virtual GeometricSearchData & geomSearchData() override
std::unordered_map< std::pair< BoundaryID, BoundaryID >, ComputeMortarFunctor > _undisplaced_mortar_functors
Functors for computing undisplaced mortar constraints.
Specialization for filling multiple "small" preconditioning matrices simulatenously.
virtual bool matrixFromColoring() const
Whether a system matrix is formed from coloring.
Definition: SolverSystem.h:117
void update()
Update the system (doing libMesh magic)
Definition: SystemBase.C:1244
virtual Assembly & assembly(const THREAD_ID tid, const unsigned int sys_num) override
void addScalarKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds a scalar kernel.
void addBoundaryCondition(const std::string &bc_name, const std::string &name, InputParameters &parameters)
Adds a boundary condition.
bool hasActiveNodeFaceConstraints(BoundaryID boundary_id, bool displaced) const
void computeResidual(NumericVector< Number > &residual, TagID tag_id)
Form a residual vector for a given tag.
virtual void addCachedResidualDirectly(NumericVector< libMesh::Number > &residual, const THREAD_ID tid)
Allows for all the residual contributions that are currently cached to be added directly into the vec...
virtual unsigned int nVariables() const
Get the number of variables in this system.
Definition: SystemBase.C:892
EXTERN_C_BEGIN PetscErrorCode DMCreate_Moose(DM)
bool hasActiveBoundaryObjects(THREAD_ID tid=0) const
bool _need_residual_ghosted
Whether or not a ghosted copy of the residual needs to be made.
virtual void activateAllMatrixTags()
Make all existing matrices active.
Definition: SystemBase.C:1132
virtual const std::string & name() const
Definition: SystemBase.C:1342
virtual void jacobianSetup()
Definition: SystemBase.C:1595
virtual bool containsTimeKernel() override
If the system has a kernel that corresponds to a time derivative.
void onTimestepBegin()
Called at the beginning of the time step.
const ConstBndNodeRange & getCurrentAlgebraicBndNodeRange()
std::vector< dof_id_type, Threads::scalable_allocator< dof_id_type > > Row
MooseObjectTagWarehouse< DiracKernelBase > _dirac_kernels
Dirac Kernel storage for each thread.
void closeTaggedMatrices(const std::set< TagID > &tags)
Close all matrices associated the tags.
Definition: SystemBase.C:1061
TagID _Re_non_time_tag
Tag for non-time contribution residual.
std::set< TagID > _nl_matrix_tags
Matrix tags to temporarily store all tags associated with the current system.
void set_basic_system_only()
std::map< std::pair< BoundaryID, BoundaryID >, NearestNodeLocator * > _nearest_node_locators
void setPredictor(std::shared_ptr< Predictor > predictor)
Real _resid_vs_jac_scaling_param
The param that indicates the weighting of the residual vs the Jacobian in determining variable scalin...
bool _auto_scaling_initd
Whether we&#39;ve initialized the automatic scaling data structures.
virtual void computeScalingResidual()=0
Compute a "residual" for automatic scaling purposes.
bool _doing_dg
true if DG is active (optimization reasons)
virtual libMesh::DofMap & dofMap()
Gets writeable reference to the dof map.
Definition: SystemBase.C:1164
void syncIteration()
Sync iteration counts to main processor Sum across all processors.
virtual void deactivateAllMatrixTags()
Make matrices inactive.
Definition: SystemBase.C:1120
void computeNodalBCsResidual(NumericVector< Number > &residual)
Enforces nodal boundary conditions.
unsigned int number() const
void computeResidualAndJacobianTags(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags)
Form possibly multiple tag-associated vectors and matrices.
SERIAL
bool needBoundaryMaterialOnSide(BoundaryID bnd_id, THREAD_ID tid) const
Indicated whether this system needs material properties on boundaries.
MooseObjectWarehouse< DirichletBCBase > _preset_nodal_bcs
std::unordered_map< unsigned int, unsigned int > _var_to_group_var
A map from variable index to group variable index and it&#39;s associated (inverse) scaling factor...
const std::vector< std::shared_ptr< T > > & getActiveObjects(THREAD_ID tid=0) const
Retrieve complete vector to the active all/block/boundary restricted objects for a given thread...
std::vector< unsigned int > _current_l_its
virtual void zero()=0
virtual std::unique_ptr< Base > create()=0
void setCurrentNonlinearSystem(const unsigned int nl_sys_num)
std::shared_ptr< T > getActiveObject(const std::string &name, THREAD_ID tid=0) const
This is the common base class for the three main kernel types implemented in MOOSE, Kernel, VectorKernel and ArrayKernel.
Definition: KernelBase.h:23
dof_id_type id() const
std::vector< dof_id_type > _secondary_nodes
MeshBase & getMesh()
Accessor for the underlying libMesh Mesh object.
Definition: MooseMesh.C:3575
void min(const T &r, T &o, Request &req) const
void computeDiracContributions(const std::set< TagID > &tags, bool is_jacobian)
void updateActive(THREAD_ID tid)
Update active objects of Warehouses owned by NonlinearSystemBase.
TheWarehouse & theWarehouse() const
unsigned int n_vars
void reinitMaterialsNeighbor(SubdomainID blk_id, const THREAD_ID tid, bool swap_stateful=true, const std::deque< MaterialBase *> *reinit_mats=nullptr)
reinit materials on the neighboring element face
const ElementPairList & getElemPairs() const
FieldSplitPreconditionerBase & getFieldSplitPreconditioner()
std::unordered_map< std::pair< BoundaryID, BoundaryID >, ComputeMortarFunctor > _displaced_mortar_functors
Functors for computing displaced mortar constraints.
bool enforceNodalConstraintsJacobian(const SparseMatrix< Number > &jacobian)
Enforce nodal constraints in the Jacobian.
const std::unordered_map< std::pair< BoundaryID, BoundaryID >, std::unique_ptr< AutomaticMortarGeneration > > & getMortarInterfaces(bool on_displaced) const
GHOSTED
boundary_id_type BoundaryID
Real referenceResidual() const
The reference residual used in relative convergence check.
void addSplit(const std::string &split_name, const std::string &name, InputParameters &parameters)
Adds a split.
bool _automatic_scaling
Whether to automatically scale the variables.
Definition: SystemBase.h:1055
void computeJacobianTags(const std::set< TagID > &tags)
Computes multiple (tag associated) Jacobian matricese.
SolutionInvalidity & solutionInvalidity()
Get the SolutionInvalidity for this app.
Definition: MooseApp.h:184
std::shared_ptr< MoosePreconditioner > _preconditioner
Preconditioner.
virtual void timestepSetup(THREAD_ID tid=0) const
NonlinearSystemBase & currentNonlinearSystem()
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
void computingScalingResidual(bool computing_scaling_residual)
Setter for whether we&#39;re computing the scaling residual.
virtual void setupDM()=0
setup the data management data structure that manages the field split
SubProblem & subproblem()
Definition: SystemBase.h:102
std::vector< std::string > _vecs_to_zero_for_residual
vectors that will be zeroed before a residual computation
virtual TagID addMatrixTag(TagName tag_name)
Create a Tag.
Definition: SubProblem.C:312
std::shared_ptr< Split > getSplit(const std::string &name)
Retrieves a split by name.
std::unique_ptr< NumericVector< Number > > solution
SubProblem & _subproblem
The subproblem for whom this class holds variable data, etc; this can either be the governing finite ...
Definition: SystemBase.h:983
void subdomainsCovered(std::set< SubdomainID > &subdomains_covered, std::set< std::string > &unique_variables, THREAD_ID tid=0) const
Populates a set of covered subdomains and the associated variable names.
void addImplicitGeometricCouplingEntries(GeometricSearchData &geom_search_data)
Adds entries to the Jacobian in the correct positions for couplings coming from dofs being coupled th...
virtual void addHDGKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds a hybridized discontinuous Galerkin (HDG) kernel.
virtual void zero_rows(std::vector< numeric_index_type > &rows, T diag_value=0.0)
MooseObjectTagWarehouse< KernelBase > _kernels
MooseObjectWarehouse< T > & getMatrixTagObjectWarehouse(TagID tag_id, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object has the given matrix tag...
bool shouldEvaluatePreSMOResidual() const
We offer the option to check convergence against the pre-SMO residual.
std::vector< VectorTag > getVectorTags(const std::set< TagID > &tag_ids) const
Definition: SubProblem.C:173
Moose::SolveType _type
Definition: SolverParams.h:19
Base class for deriving nodal dampers.
Definition: NodalDamper.h:27
virtual void disassociateVectorFromTag(NumericVector< Number > &vec, TagID tag)
Disassociate a given vector from a given tag.
virtual void cacheResidual(const THREAD_ID tid) override
void mooseDeprecated(Args &&... args)
Emit a deprecated code/feature message with the given stringified, concatenated args.
Definition: MooseError.h:363
virtual void computeScalingJacobian()=0
Compute a "Jacobian" for automatic scaling purposes.
bool errorOnJacobianNonzeroReallocation() const
Will return True if the user wants to get an error when a nonzero is reallocated in the Jacobian by P...
This is the ElementPairLocator class.
This is the ElementPairInfo class.
std::map< BoundaryID, std::shared_ptr< ElementPairLocator > > _element_pair_locators
Moose::CouplingType coupling() const
unsigned int number() const
Gets the number of this system.
Definition: SystemBase.C:1158
void setupScalingData()
Setup group scaling containers.
virtual GeometricSearchData & geomSearchData()=0
const bool & usePreSMOResidual() const
Whether we are using pre-SMO residual in relative convergence checks.
MooseObjectTagWarehouse< HDGKernel > _hybridized_kernels
const std::set< SubdomainID > & boundaryLowerDBlocks() const
Definition: MooseMesh.h:1553
std::vector< SubdomainName > getSubdomainNames(const std::vector< SubdomainID > &subdomain_ids) const
Get the associated subdomainNames for the subdomain ids that are passed in.
Definition: MooseMesh.C:1791
auto log(const T &)
AuxiliarySystem & getAuxiliarySystem()
virtual void initialSetup(THREAD_ID tid=0) const
Convenience methods for calling object setup methods.
virtual void updateGeomSearch(GeometricSearchData::GeometricSearchType type=GeometricSearchData::ALL) override
void closeTaggedVectors(const std::set< TagID > &tags)
Close all vectors for given tags.
Definition: SystemBase.C:668
Base class for deriving any boundary condition that works at nodes.
Definition: NodalBCBase.h:26
void computeResidualInternal(const std::set< TagID > &tags)
Compute the residual for a given tag.
virtual void prepareAssembly(const THREAD_ID tid) override
bool computeScaling()
Method used to obtain scaling factors for variables.
Interface for objects interacting with the PerfGraph.
virtual std::map< TagName, TagID > & getMatrixTags()
Return all matrix tags in the system, where a tag is represented by a map from name to ID...
Definition: SubProblem.h:253
virtual bool hasVariable(const std::string &var_name) const
Query a system for a variable.
Definition: SystemBase.C:852
virtual void setCurrentSubdomainID(const Elem *elem, const THREAD_ID tid) override
virtual void clearDiracInfo() override
Gets called before Dirac Kernels are asked to add the points they are supposed to be evaluated in...
void destroyColoring()
Destroy the coloring object if it exists.
virtual void reinitNodesNeighbor(const std::vector< dof_id_type > &nodes, const THREAD_ID tid) override
bool identify_variable_groups() const
virtual void close()=0
virtual void jacobianSetup(THREAD_ID tid=0) const
ConstraintWarehouse _constraints
Constraints storage object.
virtual void turnOffJacobian()
Turn off the Jacobian (must be called before equation system initialization)
void computeKokkosJacobian(const std::set< TagID > &tags)
Compute Jacobian with Kokkos objects.
TagID residualVectorTag() const override
const std::map< BoundaryID, std::vector< std::shared_ptr< T > > > & getActiveBoundaryObjects(THREAD_ID tid=0) const
void computingNonlinearResid(bool computing_nonlinear_residual) final
Set whether or not the problem is in the process of computing the nonlinear residual.
virtual void customSetup(const ExecFlagType &exec_type) override
ComputeType
The type of nonlinear computation being performed.
Definition: MooseTypes.h:829
Base class for deriving element dampers.
Definition: ElementDamper.h:33
Base interface for field split preconditioner.
bool _add_implicit_geometric_coupling_entries_to_jacobian
Whether or not to add implicit geometric couplings to the Jacobian for FDP.
const_iterator end() const
MooseObjectTagWarehouse< ResidualObject > _kokkos_integrated_bcs
void computeKokkosNodalBCsResidual(const std::set< TagID > &tags)
Compute Kokkos nodal BCs.
bool have_parameter(std::string_view name) const
A wrapper around the Parameters base class method.
virtual void addNodalKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds a NodalKernel.
virtual unsigned int numMatrixTags() const
The total number of tags.
Definition: SubProblem.h:248
Base class for creating new types of boundary conditions.
tbb::split split
MooseApp & _app
Definition: SystemBase.h:988
FEProblemBase & _fe_problem
the governing finite element/volume problem
Definition: SystemBase.h:986
virtual void reinitElemPhys(const Elem *elem, const std::vector< Point > &phys_points_in_elem, const THREAD_ID tid)=0
virtual MooseVariableScalar & getScalarVariable(THREAD_ID tid, const std::string &var_name) const
Gets a reference to a scalar variable with specified number.
Definition: SystemBase.C:146
ParallelType type() const
virtual NumericVector< Number > & RHS()=0
std::vector< VariableWarehouse > _vars
Variable warehouses (one for each thread)
Definition: SystemBase.h:996
Provides a way for users to bail out of the current solve.
unsigned int _n_residual_evaluations
Total number of residual evaluations that have been performed.
virtual void update()
void addDiracKernel(const std::string &kernel_name, const std::string &name, InputParameters &parameters)
Adds a Dirac kernel.
virtual void close()=0
void computeJacobianInternal(const std::set< TagID > &tags)
Form multiple matrices for all the tags.
bool _has_diag_save_in
If there is any Kernel or IntegratedBC having diag_save_in.
Base class for creating new types of nodal kernels.
const FEType & variable_type(const unsigned int i) const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual std::shared_ptr< const DisplacedProblem > getDisplacedProblem() const
std::unique_ptr< NumericVector< Number > > _residual_copy
Copy of the residual vector, or nullptr if a copy is not needed.
virtual void subdomainSetup()
Definition: SystemBase.C:1581
bool hasSaveIn() const
Weather or not the nonlinear system has save-ins.
Generic class for solving transient nonlinear problems.
Definition: SubProblem.h:78
TagID timeVectorTag() const override
Ideally, we should not need this API.
Class for containing MooseEnum item information.
Definition: MooseEnumItem.h:18
MooseMesh & _mesh
Definition: SystemBase.h:991
virtual std::vector< std::string > timeKernelVariableNames() override
Returns the names of the variables that have time derivative kernels in the system.
bool hasActiveObjects(THREAD_ID tid=0) const
NonlinearSystemBase(FEProblemBase &problem, libMesh::System &sys, const std::string &name)
void max(const T &r, T &o, Request &req) const
NumericVector< Number > * _u_dot
solution vector for u^dot
Definition: SystemBase.h:1006
MooseObjectWarehouse< ElementDamper > _element_dampers
Element Dampers for each thread.
void addObject(std::shared_ptr< Constraint > object, THREAD_ID tid=0, bool recurse=true) override
Add Constraint object to the warehouse.
virtual void setSolutionUDot(const NumericVector< Number > &udot)
Set transient term used by residual and Jacobian evaluation.
virtual libMesh::SparseMatrix< Number > & getMatrix(TagID tag)
Get a raw SparseMatrix.
Definition: SystemBase.C:1025
bool hasKokkosResidualObjects() const
virtual void augmentSparsity(libMesh::SparsityPattern::Graph &sparsity, std::vector< dof_id_type > &n_nz, std::vector< dof_id_type > &n_oz) override
Will modify the sparsity pattern to add logical geometric connections.
const std::vector< std::shared_ptr< MortarConstraintBase > > & getActiveMortarConstraints(const std::pair< BoundaryID, BoundaryID > &mortar_interface_key, bool displaced) const
MooseObjectWarehouse< T > & getMatrixTagsObjectWarehouse(const std::set< TagID > &tags, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object has one of the given matrix tags...
void computeScalarKernelsJacobians(const std::set< TagID > &tags)
void setInitialResidual(Real r)
Record the initial residual (for later relative convergence check)
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
Definition: TheWarehouse.h:467
virtual Assembly & assembly(const THREAD_ID tid, const unsigned int sys_num)=0
const_iterator begin() const
bool needInternalNeighborSideMaterial(SubdomainID subdomain_id, THREAD_ID tid) const
Indicates whether this system needs material properties on internal sides.
void computeKokkosResidual(const std::set< TagID > &tags)
Compute residual with Kokkos objects.
Base class for deriving dampers.
Definition: Damper.h:24
bool getFailNextNonlinearConvergenceCheck() const
Whether it will skip further residual evaluations and fail the next nonlinear convergence check(s) ...
Base class shared by AD and non-AD scalar kernels.
void addDamper(const std::string &damper_name, const std::string &name, InputParameters &parameters)
Adds a damper.
IntRange< T > make_range(T beg, T end)
virtual MooseMesh & mesh() override
virtual void postAddResidualObject(ResidualObject &)
Called after any ResidualObject-derived objects are added to the system.
const std::vector< VariableName > & getVariableNames() const
Definition: SystemBase.h:863
virtual void preInit() override
This is called prior to the libMesh system has been init&#39;d.
Definition: SolverSystem.C:32
virtual void updateActive(THREAD_ID tid=0) override
Update the active status of Kernels.
virtual void timestepSetup() override
bool hasActiveElemElemConstraints(const InterfaceID interface_id, bool displaced) const
bool _off_diagonals_in_auto_scaling
Whether to include off diagonals when determining automatic scaling factors.
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.
Definition: SubProblem.C:196
void reinitNodeFace(const Node &secondary_node, const BoundaryID secondary_boundary, const PenetrationInfo &info, const bool displaced)
Reinitialize quantities such as variables, residuals, Jacobians, materials for node-face constraints...
Base class for deriving any boundary condition of a integrated type.
SolverParams & solverParams(unsigned int solver_sys_num=0)
Get the solver parameters.
NumericVector< Number > * _residual_ghosted
ghosted form of the residual
void setCachedJacobian(GlobalDataKey)
Sets previously-cached Jacobian values via SparseMatrix::set() calls.
Definition: Assembly.C:4478
TagID _Re_tag
Used for the residual vector from PETSc.
virtual void customSetup(const ExecFlagType &exec_type)
Definition: SystemBase.C:1574
MooseObjectWarehouse< T > & getVectorTagObjectWarehouse(TagID tag_id, THREAD_ID tid)
Retrieve a moose object warehouse in which every moose object has the given vector tag...
const std::set< SubdomainID > & getSubdomainsForVar(unsigned int var_number) const
Definition: SystemBase.h:764
virtual void addObject(std::shared_ptr< T > object, THREAD_ID tid=0, bool recurse=true)
Adds an object to the storage structure.
bool ignoreZerosInJacobian() const
Will return true if zeros in the Jacobian are to be dropped from the sparsity pattern.
void computeNodalBCsJacobian(const std::set< TagID > &tags)
Compute the Jacobian for nodal boundary conditions.
const ExecFlagType EXEC_PRE_KERNELS
Definition: Moose.C:56
libMesh::System & _sys
void mortarConstraints(Moose::ComputeType compute_type, const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags)
Do mortar constraint residual/jacobian computations.
NumericVector< Number > * _increment_vec
increment vector
InterfaceKernelBase is the base class for all InterfaceKernel type classes.
QueryCache & condition(Args &&... args)
Adds a new condition to the query.
Definition: TheWarehouse.h:285
bool doingDG() const
Getter for _doing_dg.
void computeResidualTag(NumericVector< Number > &residual, TagID tag_id)
Computes residual for a given tag.
void addConstraint(const std::string &c_name, const std::string &name, InputParameters &parameters)
Adds a Constraint.
virtual TagName vectorTagName(const TagID tag) const
Retrieve the name associated with a TagID.
Definition: SubProblem.C:222
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
bool hasKokkosObjects() const
bool restoreOriginalNonzeroPattern() const
face_info_iterator ownedFaceInfoEnd()
Definition: MooseMesh.C:1536
void constraintJacobians(const SparseMatrix< Number > &jacobian_to_view, bool displaced)
Add jacobian contributions from Constraints.
virtual libMesh::System & system() override
Get the reference to the libMesh system.
MooseVariableFieldBase & getVariable(THREAD_ID tid, const std::string &var_name) const
Gets a reference to a variable of with specified name.
Definition: SystemBase.C:91
std::vector< BoundaryID > getBoundaryIDs(const Elem *const elem, const unsigned short int side) const
Returns a vector of boundary IDs for the requested element on the requested side. ...
bool preSolve()
Perform some steps to get ready for the solver.
void full_sparsity_pattern_needed()
bool _has_constraints
Whether or not this system has any Constraints.
dof_id_type first_dof(const processor_id_type proc) const
bool _computed_scaling
Flag used to indicate whether we have already computed the scaling Jacobian.
void remove_algebraic_ghosting_functor(GhostingFunctor &evaluable_functor)
MooseObjectTagWarehouse< InterfaceKernelBase > _interface_kernels
unsigned int n_vars() const
NumericVector< Number > & residualVector(TagID tag)
Return a residual vector that is associated with the residual tag.
NumericVector< Number > & solutionOld()
Definition: SystemBase.h:198
NumericVector< Number > & getResidualNonTimeVector()
Return a numeric vector that is associated with the nontime tag.
virtual bool hasScalarVariable(const std::string &var_name) const
Definition: SystemBase.C:877
const TagName PREVIOUS_NL_SOLUTION_TAG
Definition: MooseTypes.C:28
processor_id_type processor_id() const
std::shared_ptr< Predictor > _predictor
If predictor is active, this is non-NULL.
void enforceNodalConstraintsResidual(NumericVector< Number > &residual)
Enforce nodal constraints.
void subdomainsCovered(std::set< SubdomainID > &subdomains_covered, std::set< std::string > &unique_variables, THREAD_ID tid=0) const
Update supplied subdomain and variable coverate containters.
MooseObjectWarehouse< GeneralDamper > _general_dampers
General Dampers.
const std::vector< std::shared_ptr< ElemElemConstraint > > & getActiveElemElemConstraints(InterfaceID interface_id, bool displaced) const
virtual void initialSetup()
Setup Functions.
Definition: SystemBase.C:1560
bool defaultGhosting()
Whether or not the user has requested default ghosting ot be on.
Definition: SubProblem.h:144
void addCachedJacobian(GlobalDataKey)
Adds the values that have been cached by calling cacheJacobian() and or cacheJacobianNeighbor() to th...
Definition: Assembly.C:3801
virtual void cacheJacobian(const THREAD_ID tid) override
auto min(const L &left, const R &right)
void jacobianSetup() override
void setKokkosInitialSolution()
virtual NumericVector< Number > & getVector(const std::string &name)
Get a raw NumericVector by name.
Definition: SystemBase.C:934
MooseObjectTagWarehouse< IntegratedBCBase > _integrated_bcs
const DofMap & get_dof_map() const
virtual void reinitNodeFace(const Node *node, BoundaryID bnd_id, const THREAD_ID tid) override
virtual void residualSetup()
Definition: SystemBase.C:1588
virtual void reinitOffDiagScalars(const THREAD_ID tid) override
processor_id_type processor_id() const
virtual void addObject(std::shared_ptr< T > object, THREAD_ID tid=0, bool recurse=true) override
Adds an object to the storage structure.
virtual void setNeighborSubdomainID(const Elem *elem, unsigned int side, const THREAD_ID tid) override
virtual void addResidualScalar(const THREAD_ID tid=0)
virtual void subdomainSetup(THREAD_ID tid=0) const
virtual void jacobianSetup() override
virtual void addCachedResidual(const THREAD_ID tid) override
std::vector< std::vector< std::string > > _scaling_group_variables
A container of variable groupings that can be used in scaling calculations.
void addInterfaceKernel(std::string interface_kernel_name, const std::string &name, InputParameters &parameters)
Adds an interface kernel.
FieldSplitPreconditionerBase * _fsp
The field split preconditioner if this sytem is using one.
auto index_range(const T &sizable)
virtual NumericVector< Number > & residualCopy() override
void reinitMortarFunctors()
Update the mortar functors if the mesh has changed.
virtual NumericVector< Number > & residualGhosted() override
DiracKernelBase is the base class for all DiracKernel type classes.
virtual void updateActive(THREAD_ID tid=0)
Updates the active objects storage.
MooseObjectTagWarehouse< ResidualObject > _kokkos_nodal_bcs
void assembleScalingVector()
Assemble the numeric vector of scaling factors such that it can be used during assembly of the system...
BoundaryID _primary_boundary
NumericVector< Number > * _u_dotdot_old
old solution vector for u^dotdot
Definition: SystemBase.h:1013
Base variable class.
MooseObjectTagWarehouse< ScalarKernelBase > _scalar_kernels
virtual void residualEnd(THREAD_ID tid=0) const
void setConstraintSecondaryValues(NumericVector< Number > &solution, bool displaced)
Sets the value of constrained variables in the solution vector.
unsigned int THREAD_ID
Definition: MooseTypes.h:237
uint8_t dof_id_type
virtual void addJacobianScalar(const THREAD_ID tid=0)
NearestNodeLocator & _nearest_node
const std::map< dof_id_type, std::vector< dof_id_type > > & nodeToElemMap()
If not already created, creates a map from every node to all elements to which they are connected...
Definition: MooseMesh.C:1201
const std::set< SubdomainID > & meshSubdomains() const
Returns a read-only reference to the set of subdomains currently present in the Mesh.
Definition: MooseMesh.C:3298
ParallelType
virtual void addCachedJacobian(const THREAD_ID tid) override
virtual void timestepSetup()
Definition: SystemBase.C:1567
virtual ~NonlinearSystemBase()
virtual void preInit() override
This is called prior to the libMesh system has been init&#39;d.
virtual void residualSetup() override
void setPreconditioner(std::shared_ptr< MoosePreconditioner > pc)
Sets a preconditioner.
virtual void localize(std::vector< T > &v_local) const=0
MooseObjectWarehouse< ADDirichletBCBase > _ad_preset_nodal_bcs
virtual libMesh::System & system() override
Get the reference to the libMesh system.
Key structure for APIs manipulating global vectors/matrices.
Definition: Assembly.h:844