18#include "libmesh/dof_object.h"
19#include "libmesh/petsc_vector.h"
28 std::vector<std::unique_ptr<NumericVector<Number>>> & gradient,
30 const std::unordered_set<unsigned int> & requested_variables)
31 : _fe_problem(fe_problem),
32 _dim(_fe_problem.
mesh().dimension()),
34 _libmesh_system(system.system()),
35 _system_number(_libmesh_system.number()),
37 _gradient_data(gradient.size()),
38 _owns_gradient_data(true),
39 _limiter_type(limiter_type),
40 _requested_variables(requested_variables)
42 for (
const auto dim_index : index_range(
_gradient))
44 auto & gradient_vector =
45 libMesh::cast_ref<libMesh::PetscVector<Number> &>(*
_gradient[dim_index]);
52 : _fe_problem(x._fe_problem),
55 _libmesh_system(x._libmesh_system),
56 _system_number(x._system_number),
57 _gradient(x._gradient),
58 _gradient_data(x._gradient_data),
59 _owns_gradient_data(false),
60 _limiter_type(x._limiter_type),
61 _requested_variables(x._requested_variables)
68 for (
const auto dim_index : index_range(
_gradient))
70 auto & gradient_vector =
71 libMesh::cast_ref<libMesh::PetscVector<Number> &>(*
_gradient[dim_index]);
72 gradient_vector.restore_array();
83 mooseError(
"ComputeLinearFVLimitedGradientThread currently supports only the Venkatakrishnan "
87 "Gradient container has fewer components than mesh dimension.");
91 auto & first_gradient_vector =
92 libMesh::cast_ref<libMesh::PetscVector<Number> &>(*
_gradient.front());
108 for (
auto elem_iterator = range.begin(); elem_iterator != range.end(); ++elem_iterator)
110 const auto & elem_info = *elem_iterator;
119 const auto local_dof = first_gradient_vector.map_global_to_local_index(dof);
121 const Real phi_elem = solution_reader(dof);
122 Real max_value = phi_elem;
123 Real min_value = phi_elem;
126 const Elem *
const elem = elem_info->elem();
127 for (
const auto side : make_range(elem->n_sides()))
129 const Elem *
const neighbor = elem->neighbor_ptr(side);
137 const dof_id_type neighbor_dof =
142 const Real phi_neighbor = solution_reader(neighbor_dof);
143 max_value = std::max(max_value, phi_neighbor);
144 min_value = std::min(min_value, phi_neighbor);
148 VectorValue<Real> raw_grad;
150 for (
const auto dim_index : make_range(
_dim))
155 if (std::abs(max_value - min_value) < 1e-14)
159 const Point & elem_centroid = elem_info->centroid();
161 for (
const auto side : make_range(elem->n_sides()))
163 const Elem *
const neighbor = elem->neighbor_ptr(side);
171 const dof_id_type neighbor_dof =
177 const Elem *
const fi_elem = elem_has_face_info ? elem : neighbor;
178 const unsigned int fi_side =
179 elem_has_face_info ? side : neighbor->which_neighbor_am_i(elem);
183 "Missing FaceInfo for neighboring elements with centroid " +
186 " while computing limited gradients.");
188 const Point face_point = fi->faceCentroid();
189 const Real delta_face = raw_grad * (face_point - elem_centroid);
191 const Real h = elem->hmin();
192 const Real grad_mag = raw_grad.norm();
194 const Real eps = 0.1 * (grad_mag * h) * (grad_mag * h) + 1e-20;
196 const Real delta_max = std::abs(max_value - phi_elem) + eps;
197 const Real delta_min = std::abs(min_value - phi_elem) + eps;
199 const Real rf = (delta_face >= 0.0) ? std::abs(delta_face) / delta_max
200 : std::abs(delta_face) / delta_min;
202 const Real beta = (2.0 * rf + 1.0) / (rf * (2.0 * rf + 1.0) + 1.0);
203 alpha = std::min(alpha, beta);
208 for (
const auto dim_index : make_range(
_dim))
209 _gradient_data[dim_index][local_dof] = alpha * raw_grad(dim_index);
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Compute limited cell gradients for linear FV variables.
const std::unordered_set< unsigned int > & _requested_variables
Variable numbers that requested the current limiter.
const unsigned int _system_number
Global system number in the libMesh equation system.
std::vector< std::unique_ptr< NumericVector< Number > > > & _gradient
Gradient storage limited in place.
MooseLinearVariableFV< Real > * _current_var
Pointer to the current variable we are operating on.
SystemBase & _system
The system wrapper this thread operates on.
const bool _owns_gradient_data
True only for the original thread object that acquired and must restore the arrays.
FEProblemBase & _fe_problem
Reference to the problem.
StoredRange< MooseMesh::const_elem_info_iterator, const ElemInfo * > ElemInfoRange
ComputeLinearFVLimitedGradientThread(FEProblemBase &fe_problem, SystemBase &system, std::vector< std::unique_ptr< NumericVector< Number > > > &gradient, const Moose::FV::GradientLimiterType limiter_type, const std::unordered_set< unsigned int > &requested_variables)
Class constructor.
const libMesh::System & _libmesh_system
Reference to the libMesh system backing the wrapper system.
std::vector< Number * > _gradient_data
Writable local arrays, one per gradient component.
const Moose::FV::GradientLimiterType _limiter_type
The type of limiter requested.
~ComputeLinearFVLimitedGradientThread()
void join(const ComputeLinearFVLimitedGradientThread &y)
Join threads at the end of the execution.
const unsigned int _dim
The dimension of the domain.
void operator()(const ElemInfoRange &range)
Apply the limiter over the provided element range.
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual MooseMesh & mesh() override
This class provides variable solution interface for linear finite volume problems.
virtual bool needsGradientVectorStorage() const override
Check if cell gradient computations were requested for this variable.
const std::vector< const FaceInfo * > & faceInfo() const
Accessor for local FaceInfo objects.
const ElemInfo & elemInfo(const dof_id_type id) const
Accessor for the elemInfo object for a given element ID.
unsigned int number() const
Get variable number coming from libMesh.
bool hasBlocks(const SubdomainID id) const override
Returns whether the functor is defined on this block.
A class which helps with repeated reading from a petsc vector.
Base class for a system (of equations)
const std::vector< MooseVariableFieldBase * > & getVariables(THREAD_ID tid)
static constexpr dof_id_type invalid_id
std::unique_ptr< NumericVector< Number > > current_local_solution
bool elemHasFaceInfo(const Elem &elem, const Elem *const neighbor)
This function infers based on elements if the faceinfo between them belongs to the element or not.
GradientLimiterType
Cell-gradient limiter variants used for MUSCL-style reconstructions.
@ Venkatakrishnan
Venkatakrishnan limiter (smooth, multidimensional).
std::string stringify(const T &t)
conversion to string