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