43 #include "libmesh/linear_solver.h" 44 #include "libmesh/quadrature_gauss.h" 45 #include "libmesh/dense_vector.h" 46 #include "libmesh/boundary_info.h" 47 #include "libmesh/petsc_matrix.h" 48 #include "libmesh/petsc_vector.h" 49 #include "libmesh/petsc_nonlinear_solver.h" 50 #include "libmesh/numeric_vector.h" 51 #include "libmesh/mesh.h" 52 #include "libmesh/dense_subvector.h" 53 #include "libmesh/dense_submatrix.h" 54 #include "libmesh/dof_map.h" 55 #include "libmesh/sparse_matrix.h" 56 #include "libmesh/petsc_matrix.h" 57 #include "libmesh/default_coupling.h" 58 #include "libmesh/diagonal_matrix.h" 59 #include "libmesh/petsc_solver_exception.h" 72 auto & lin_sys = sys.linearImplicitSystem();
73 auto & matrix = *(sys.linearImplicitSystem().matrix);
74 auto & rhs = *(sys.linearImplicitSystem().rhs);
85 _rhs_non_time_tag(-1),
111 const auto & var_names =
_vars[0].names();
112 for (
const auto &
name : var_names)
114 mooseError(
"You are trying to add a nonlinear variable to a linear system! The variable " 115 "which is assigned to the wrong system: ",
121 std::vector<LinearFVElementalKernel *> fv_elemental_kernels;
124 .template condition<AttribSystem>(
"LinearFVElementalKernel")
125 .
template condition<AttribThread>(tid)
128 for (
auto * fv_kernel : fv_elemental_kernels)
129 fv_kernel->initialSetup();
131 std::vector<LinearFVFluxKernel *> fv_flux_kernels;
134 .template condition<AttribSystem>(
"LinearFVFluxKernel")
135 .
template condition<AttribThread>(tid)
138 for (
auto * fv_kernel : fv_flux_kernels)
139 fv_kernel->initialSetup();
141 std::vector<LinearFVBoundaryCondition *> fv_bcs;
144 .template condition<AttribSystem>(
"LinearFVBoundaryCondition")
145 .
template condition<AttribThread>(tid)
148 for (
auto * fv_bc : fv_bcs)
149 fv_bc->initialSetup();
155 const std::set<TagID> & matrix_tags,
156 const bool compute_gradients)
158 parallel_object_only();
160 TIME_SECTION(
"LinearSystem::computeLinearSystemTags", 5);
172 _console <<
"Exception detected " << e.
what() << std::endl;
186 TIME_SECTION(
"LinearVariableFV_Gradients", 3 );
196 Threads::parallel_reduce(face_info_range, gradient_face_thread);
211 Threads::parallel_reduce(elem_info_range, gradient_volume_thread);
224 const std::set<TagID> & matrix_tags,
225 const bool compute_gradients)
227 TIME_SECTION(
"computeLinearSystemInternal", 3);
236 for (
auto tag : matrix_tags)
242 LibmeshPetscCall(MatSetOption(petsc_matrix->mat(),
243 MAT_KEEP_NONZERO_PATTERN,
247 MatSetOption(petsc_matrix->mat(), MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
251 if (compute_gradients)
257 TIME_SECTION(
"LinearFVKernels_FullSystem", 3 );
269 Threads::parallel_reduce(elem_info_range, elem_thread);
276 Threads::parallel_reduce(face_info_range, face_thread);
305 std::vector<dof_id_type> & ,
306 std::vector<dof_id_type> & )
308 mooseError(
"LinearSystem does not support AugmentSparsity!");
314 TIME_SECTION(
"LinearSystem::solve", 2,
"Solving linear system");
324 auto & linear_solver =
328 _converged = linear_solver.get_converged_reason() > 0;
330 _console <<
"System: " << this->
name() <<
" Initial residual: " << _initial_linear_residual
340 const std::set<TagID> & vector_tags_to_close)
351 std::vector<LinearFVKernel *> kernels;
354 .template condition<AttribSysNum>(this->
number())
355 .
template condition<AttribSystem>(
"LinearFVKernel")
358 bool contains_time_kernel =
false;
359 for (
const auto kernel : kernels)
362 if (contains_time_kernel)
366 return contains_time_kernel;
std::string name(const ElemQuality q)
virtual void stopSolve(const ExecFlagType &exec_flag, const std::set< TagID > &vector_tags_to_close) override
Quit the current solve as soon as possible.
The gradient in a volume using Green Gauss theorem and a cell-centered finite-volume approximation ca...
virtual const char * what() const
Get out the error message.
void computeGradients()
Compute the Green-Gauss gradients.
unsigned int _n_linear_iters
Number of linear iterations.
void checkInvalidSolution()
face_info_iterator ownedFaceInfoBegin()
Iterators to owned faceInfo objects.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
virtual bool containsTimeKernel() override
If the system has a kernel that corresponds to a time derivative.
The gradient in a volume using Green Gauss theorem and a cell-centered finite-volume approximation ca...
NumericVector< Number > & getRightHandSideNonTimeVector()
Return a numeric vector that is associated with the nontime tag.
virtual TagID addVectorTag(const TagName &tag_name, const Moose::VectorTagType type=Moose::VECTOR_TAG_RESIDUAL)
Create a Tag.
std::vector< T * > & queryInto(std::vector< T *> &results, Args &&... args)
queryInto executes the query and stores the results in the given vector.
virtual void associateVectorToTag(NumericVector< Number > &vec, TagID tag)
Associate a vector for a given tag.
void computeLinearSystemInternal(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags, const bool compute_gradients=true)
Compute the right hand side and system matrix for given tags.
virtual void compute(ExecFlagType type) override
Compute time derivatives, auxiliary variables, etc.
void compute_linear_system(libMesh::EquationSystems &es, const std::string &system_name)
NumericVector< Number > * rhs
virtual LinearSolver< Number > * get_linear_solver() const override
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
unsigned int _current_l_its
The linear iterations needed for convergence.
std::vector< std::unique_ptr< NumericVector< Number > > > _raw_grad_container
A cache for storing gradients at dof locations.
Real _initial_linear_residual
The initial linear residual.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
std::vector< std::unique_ptr< NumericVector< Number > > > _new_gradient
Vectors to store the new gradients during the computation.
Scope guard for starting and stopping Floating Point Exception Trapping.
elem_info_iterator ownedElemInfoBegin()
Iterators to owned faceInfo objects.
Adds contributions from volumetric terms discretized using the finite volume method to the matrix and...
virtual void initialSetup() override
Setup Functions.
virtual void computeLinearSystemSys(libMesh::LinearImplicitSystem &sys, libMesh::SparseMatrix< libMesh::Number > &system_matrix, NumericVector< libMesh::Number > &rhs, const bool compute_gradients=true)
Assemble both the right hand side and the system matrix of a given linear system. ...
virtual void activateAllMatrixTags()
Make all existing matrices active.
virtual const std::string & name() const
void closeTaggedMatrices(const std::set< TagID > &tags)
Close all matrices associated the tags.
void solutionInvalidAccumulation()
Pass the number of solution invalid occurrences from current iteration to cumulative counters...
void syncIteration()
Sync iteration counts to main processor.
libMesh::LinearImplicitSystem & _linear_implicit_system
Base class reference to the linear implicit system in libmesh.
NumericVector< Number > * _rhs_time
right hand side vector for time contributions
const T & get(std::string_view) const
Real final_linear_residual() const
TheWarehouse & theWarehouse() const
unsigned int n_linear_iterations() const
Real _final_linear_residual
The final linear residual.
SolutionInvalidity & solutionInvalidity()
Get the SolutionInvalidity for this app.
virtual TagID addMatrixTag(TagName tag_name)
Create a Tag.
TagID _rhs_tag
Used for the right hand side vector from PETSc.
bool errorOnJacobianNonzeroReallocation() const
Will return True if the user wants to get an error when a nonzero is reallocated in the Jacobian by P...
unsigned int number() const
Gets the number of this system.
void attach_assemble_function(void fptr(EquationSystems &es, const std::string &name))
virtual void solve() override
Solve the system (using libMesh magic)
void closeTaggedVectors(const std::set< TagID > &tags)
Close all vectors for given tags.
Interface for objects interacting with the PerfGraph.
LinearSystem & getLinearSystem(unsigned int sys_num)
Get non-constant reference to a linear system.
Kernel that adds contributions from a time derivative term to a linear system populated using the fin...
TagID _system_matrix_tag
Tag for every contribution to system matrix.
FEProblemBase & _fe_problem
the governing finite element/volume problem
bool _converged
If the solve on the linear system converged.
std::vector< VariableWarehouse > _vars
Variable warehouses (one for each thread)
Provides a way for users to bail out of the current solve.
NumericVector< Number > * _rhs_non_time
right hand side vector for non-time contributions
Class for containing MooseEnum item information.
NumericVector< Number > & getRightHandSideTimeVector()
Return a numeric vector that is associated with the time tag.
virtual libMesh::SparseMatrix< Number > & getMatrix(TagID tag)
Get a raw SparseMatrix.
SparseMatrix< Number > * matrix
void setCurrentLinearSystem(unsigned int sys_num)
Set the current linear system pointer.
Query query()
query creates and returns an initialized a query object for querying objects from the warehouse...
virtual MooseMesh & mesh() override
unsigned int linearSysNum(const LinearSystemName &linear_sys_name) const override
virtual void augmentSparsity(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.
Adds contributions from face terms discretized using the finite volume method to the matrix and right...
elem_info_iterator ownedElemInfoEnd()
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
const ConsoleStream _console
An instance of helper class to write streams to the Console objects.
face_info_iterator ownedFaceInfoEnd()
MooseVariableFieldBase & getVariable(THREAD_ID tid, const std::string &var_name) const
Gets a reference to a variable of with specified name.
virtual void initialSetup()
Setup Functions.
LinearSystem(FEProblemBase &problem, const std::string &name)
auto index_range(const T &sizable)
virtual System & system() override
Get the reference to the libMesh system.
void computeLinearSystemTags(const std::set< TagID > &vector_tags, const std::set< TagID > &matrix_tags, const bool compute_gradients=true)
Compute the right hand side and the system matrix of the system for given tags.