28#ifndef VULPES_ASSEMBLE_DG_UTILS_HPP_
29#define VULPES_ASSEMBLE_DG_UTILS_HPP_
75 pcout <<
"Warning: square root of negative value " << x << std::endl;
77 return std::sqrt(std::max(x, 0.0));
108template <
typename MeshType,
typename FeSpaceType,
typename ExprType>
110 const MeshType& mesh,
112 const ExprType& expression,
113 std::optional<uint32_t> quad_degree = std::nullopt) {
114 constexpr std::size_t Dim = MeshType::self_dim;
115 constexpr std::size_t max_tabled = Dim == 3
118 const uint32_t degree = quad_degree.value_or(
119 static_cast<uint32_t
>(1 + expression.computeOrder()));
120 if (Dim > 1 && degree > max_tabled) {
121 pcout <<
"Warning: quadrature degree " << degree
122 <<
" is above the tabled simplex rules" << std::endl;
125 integrator(mesh, cell_manager, expression, degree);
127 if constexpr (MeshType::parallel()) {
129 MPI_IN_PLACE, &result, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
156 typename FeSpaceType,
157 typename DofHandlerType,
160 const MeshType& mesh,
162 const DofHandlerType& dof_handler,
164 const ExprType& u_exact_expr,
165 std::optional<uint32_t> quad_degree = std::nullopt) {
166 constexpr std::size_t Dim = MeshType::self_dim;
172 auto diff_expr = u_h_expr - u_exact_expr;
175 auto diff_squared = diff_expr * diff_expr;
178 const std::size_t p = cell_manager.
getFeSpace().order();
180 mesh, cell_manager, diff_squared,
181 quad_degree.value_or(
182 std::max(1 + diff_squared.computeOrder(), 2 * p + 7)));
208 typename FeSpaceType,
209 typename DofHandlerType,
212 const MeshType& mesh,
214 const DofHandlerType& dof_handler,
216 const ExprType& grad_u_exact_expr,
217 std::optional<uint32_t> quad_degree = std::nullopt) {
218 constexpr std::size_t Dim = MeshType::self_dim;
223 auto diff_expr = grad_uh_expr - grad_u_exact_expr;
224 auto diff_squared =
fe::inner(diff_expr, diff_expr);
226 const std::size_t p = cell_manager.
getFeSpace().order();
228 mesh, cell_manager, diff_squared,
229 quad_degree.value_or(
230 std::max(1 + diff_squared.computeOrder(), 2 * p + 7)));
245template <
typename MeshType>
251 matrix.
vmult(u_h, temp);
264template <std::
size_t Dim>
269 const auto& cell = *cells[fe_values.
getCellId()];
270 const double penalty = gamma * order * order;
271 const double p_this = penalty / cell.diameter();
274 const auto neigh_cell_id = fe_values.
neighValues().getCellId();
275 const auto& neigh_cell = *cells[neigh_cell_id];
276 const double p_neigh = penalty / neigh_cell.diameter();
277 return std::max(p_this, p_neigh);
288template <std::
size_t Dim,
typename CellType>
290 if constexpr (Dim == 3) {
291 assert(cell.faceMeasures().size() > 0);
292 return cell.faceMeasures()[face_id];
294 return cell.faceDiam()[face_id];
309template <std::
size_t Dim>
314 const auto& cell = *cells[fe_values.
getCellId()];
315 const auto face_id = fe_values.
getFaceId();
316 const double penalty = gamma * order * order;
317 const double Cinv = cell.measure() / cell.faceKb()[face_id];
319 const double p_this = penalty * Cinv * (facet_measure / cell.measure());
322 const auto neigh_cell_id = fe_values.
neighValues().getCellId();
323 const auto& neigh_cell = *cells[neigh_cell_id];
324 const auto neigh_face_id = fe_values.
neighValues().getFaceId();
325 const double Cinv_neigh =
326 neigh_cell.measure() / neigh_cell.faceKb()[neigh_face_id];
327 const double p_neigh =
328 penalty * Cinv_neigh * (facet_measure / neigh_cell.measure());
329 return std::max(p_this, p_neigh);
350template <std::
size_t Dim>
355 const auto& cell = *cells[fe_values.
getCellId()];
356 const auto face_id = fe_values.
getFaceId();
358 const double penalty = gamma * order * order;
359 const double Cinv = cell.measure() / cell.faceKb()[face_id];
360 const double p_this = penalty * Cinv * (facet_measure / cell.measure());
363 const auto neigh_cell_id = fe_values.
neighValues().getCellId();
364 const auto& neigh_cell = *cells[neigh_cell_id];
365 const auto neigh_face_id = fe_values.
neighValues().getFaceId();
366 const double Cinv_neigh =
367 neigh_cell.measure() / neigh_cell.faceKb()[neigh_face_id];
368 const double p_neigh =
369 penalty * Cinv_neigh * (facet_measure / neigh_cell.measure());
371 return 2.0 * p_this * p_neigh / (p_this + p_neigh);
384template <std::
size_t Dim,
typename VelocityFunc>
388 FeExpressionUpwindFromFunctionWrapper<Dim, VelocityFunc>> {
394 const VelocityFunc& velocity_func)
405 template <std::
size_t I = 0, std::
size_t J = 0>
418 const Eigen::Index nqpts = fe_values.
weights().size();
419 assert(fe_values.
qpts().cols() == nqpts);
422 for (Eigen::Index q = 0; q < nqpts; ++q) {
424 const double vel_dot_n = vel.dot(normal);
425 m_evals[q] = 0.5 * (std::abs(vel_dot_n) + vel_dot_n);
444template <std::
size_t Dim,
typename VelocityFunc>
461template <
typename MeshType>
469 error_u.
add(-1.0, u_h);
470 error_u.
add(1.0, u_exact);
475 matrix.
vmult(error_u, temp);
494 double numerator =
la::dot(u, rhs_const);
495 return numerator / domain_measure;
509template <
typename MeshType>
512 double constant_value,
516 scaled_rhs.
scale(constant_value);
521 solver.
solve(scaled_rhs, c_proj);
539template <
typename MeshType>
546 double domain_measure) {
548 double avg_reference =
computeAverage(u_reference, rhs_const, domain_measure);
550 double shift_physical = avg_reference - avg_u;
553 dof_handler, shift_physical, mass_matrix, rhs_const);
558 return shift_physical;
571 typename CellManagerType,
572 typename DofHandlerType,
573 typename FeSpaceType,
591 const MeshType& mesh,
592 CellManagerType& cell_manager,
593 const DofHandlerType& dof_handler,
594 const FeSpaceType& fe_space,
595 const Expression& expr,
600 auto wf_projection = expr * v;
603 mesh, cell_manager, std::tuple(&wf_projection), std::tuple());
606 const std::size_t n_cells =
static_cast<std::size_t
>(mesh.numCells());
607 for (std::size_t cell_id = 0; cell_id < n_cells; ++cell_id) {
611 dof_handler.getCellToDof()[cell_id]);
618 solver.
solve(rhs, projected);
624 typename CellManagerType,
625 typename DofHandlerType,
626 typename FeSpaceType,
650 const MeshType& mesh,
651 CellManagerType& cell_manager,
652 const DofHandlerType& dof_handler,
653 const FeSpaceType& fe_space,
654 const Expression& expr,
656 std::size_t component) {
660 mesh, cell_manager, dof_handler, fe_space, expr, mass_scalar);
667 typename CellManagerType,
668 typename DofHandlerType,
669 typename FeSpaceType,
693 const MeshType& mesh,
694 CellManagerType& cell_manager,
695 const DofHandlerType& dof_handler,
696 const FeSpaceType& fe_space,
697 const Expression& expr,
698 std::size_t component_offset,
702 assert(component_offset + VecSize <= NFields);
708 mesh, cell_manager, std::tuple(&wf_projection), std::tuple());
711 const auto& cell_to_dof = dof_handler.getCellToDof();
712 const Eigen::Index ndof_comp = fe_space.getBasis().size();
713 const std::size_t n_cells =
static_cast<std::size_t
>(mesh.numCells());
715 std::array<PetscScalar, NFields> block_values{};
716 for (std::size_t cell_id = 0; cell_id < n_cells; ++cell_id) {
718 const auto local = int_smplx_projection.
getIntegral(&wf_projection);
720 for (Eigen::Index i = 0; i < ndof_comp; ++i) {
721 block_values.fill(0.0);
722 for (std::size_t c = 0; c < VecSize; ++c) {
723 block_values[component_offset + c] = local(
724 i *
static_cast<Eigen::Index
>(VecSize) +
725 static_cast<Eigen::Index
>(c),
728 const PetscInt block_index = cell_to_dof[cell_id][i];
737 solver.
solve(rhs, projected);
Manages geometrical information about the cell and its mapping to the reference element.
Definition cell_manager.hpp:62
const auto & getFeSpace() const
Get the finite element space associated to this cell manager.
Definition cell_manager.hpp:107
Specialized integrator for scalar expressions using simplification.
Definition integrator_simplexify.hpp:735
double integrate()
Integrate the scalar expression over the entire domain.
Definition integrator_simplexify.hpp:765
Q-point based upwind flux expression built from a velocity callable.
Definition dg_utils.hpp:388
FeExpressionUpwindFromFunctionWrapper(const FeExpressionUpwindFromFunctionWrapper &)=delete
FeExpressionUpwindFromFunctionWrapper & operator=(FeExpressionUpwindFromFunctionWrapper &&)=delete
constexpr std::size_t computeOrder() const
Definition dg_utils.hpp:414
constexpr fe::UpdateFlagsType computeUpdateFlags() const
Definition dg_utils.hpp:411
std::vector< double > m_evals
Definition dg_utils.hpp:432
static constexpr bool is_leaf
Definition dg_utils.hpp:390
FeExpressionUpwindFromFunctionWrapper(FeExpressionUpwindFromFunctionWrapper &&)=delete
constexpr FeExpressionUpwindFromFunctionWrapper(const VelocityFunc &velocity_func)
Definition dg_utils.hpp:393
decltype(auto) evaluate(const fe::FeValues< Dim > &, std::size_t, std::size_t, std::size_t q) const
Definition dg_utils.hpp:406
const VelocityFunc m_velocity_func
Definition dg_utils.hpp:431
FeExpressionUpwindFromFunctionWrapper & operator=(const FeExpressionUpwindFromFunctionWrapper &)=delete
constexpr bool precompute(const fe::FeValues< Dim > &fe_values, const fe::CellsType< Dim > &) const
Definition dg_utils.hpp:416
static constexpr uint8_t is_bilinear
Definition dg_utils.hpp:391
Compute and distribute the DoFs and sparsity pattern associated with a mesh and a certain finite elem...
Definition dof_handler.hpp:72
Class that wraps the projected of a given vector of coefficients on the FE space.
Definition fe_expr_func.hpp:664
Base class for Finite Element Expressions.
Definition fe_expr_base.hpp:210
Class that wraps a function which is cell constant and need the evaluation of neighbours (e....
Definition fe_expr_func.hpp:274
Physical gradient of a DG field given its DOF coefficient vector.
Definition fe_expr_func.hpp:809
This class works as a cache table for computing efficiently the value of the basis function and its d...
Definition fe_values.hpp:94
index_t getFaceId() const
Definition fe_values.hpp:154
constexpr const auto & qpts() const
Definition fe_values.hpp:107
constexpr const auto & weights() const
Definition fe_values.hpp:105
index_t getCellId() const
Definition fe_values.hpp:143
constexpr bool hasFaceNeigh() const
Definition fe_values.hpp:141
auto & neighValues()
Definition fe_values.hpp:264
constexpr const Eigen::Vector< real_t, Dim > & getFaceNormal() const
Definition fe_values.hpp:135
A linear solver class wrapping PETSc KSP solvers.
Definition linear_solver.hpp:47
@ PrecondOnly
Definition linear_solver.hpp:57
@ LU
Definition linear_solver.hpp:71
void solve(const Vector &b, Vector &x)
Solve the linear system Ax = b.
Definition linear_solver.cpp:231
void setOperators(const SparseMatrix &A)
Set the system matrix.
Definition linear_solver.cpp:201
Definition sparse_matrix.hpp:52
@ Plain
Definition sparse_matrix.hpp:56
void vmult(const Vector &in, Vector &out) const
Definition sparse_matrix.cpp:371
A wrapper around PETSc Vec for distributed vectors.
Definition vector.hpp:54
void insertValues(const PetscScalar *values, const PetscInt *indices, PetscInt n_values, InsertMode mode=ADD_VALUES)
Insert values into the vector at specified indices.
Definition vector.cpp:108
void add(const Vector &x)
Add a vector in place (this += x).
Definition vector.hpp:281
void insertValuesBlocked(const PetscScalar *values, const PetscInt *indices, PetscInt n_values, InsertMode mode=ADD_VALUES)
Insert blocked values into the vector at specified indices.
Definition vector.cpp:117
PetscInt getBlockSize() const
Get the block size of the vector.
Definition vector.cpp:270
void compress()
Compress the vector (do intracommunication).
Definition vector.cpp:49
void scale(double value)
Scale the vector by a constant.
Definition vector.cpp:284
Definition dg_utils.hpp:58
double sqrtNonNegative(double x, double tol=1e-12)
Square root of a quantity that is non-negative in exact arithmetic (squared norms,...
Definition dg_utils.hpp:70
double computeAverage(const la::Vector &u, const la::Vector &rhs_const, double domain_measure)
Compute the spatial average of a function in modal/DG space.
Definition dg_utils.hpp:492
double computeJumpError(const dofs::DofHandler< MeshType > &dof_handler, const la::Vector &u_h, const la::SparseMatrix &matrix)
Compute the total Jump error that is: sqrt(u_h^T matrix u_h). This is used only when the provided pro...
Definition dg_utils.hpp:246
double integrateFeExpression(const MeshType &mesh, assemble::CellManager< MeshType, FeSpaceType > &cell_manager, const ExprType &expression, std::optional< uint32_t > quad_degree=std::nullopt)
Integrate a scalar finite element expression over the entire domain.
Definition dg_utils.hpp:109
la::Vector l2ProjectExpressionBlockedSubset(const MeshType &mesh, CellManagerType &cell_manager, const DofHandlerType &dof_handler, const FeSpaceType &fe_space, const Expression &expr, std::size_t component_offset, const la::SparseMatrix &mass_blocked)
L2-project a vector expression into a subset of components of a blocked vector.
Definition dg_utils.hpp:692
double computeL2Error(const MeshType &mesh, assemble::CellManager< MeshType, FeSpaceType > &cell_manager, const DofHandlerType &dof_handler, const la::Vector &u_h, const ExprType &u_exact_expr, std::optional< uint32_t > quad_degree=std::nullopt)
Compute the L2 error integral between a numerical solution and an exact expression.
Definition dg_utils.hpp:159
auto createPenaltyExpression(std::size_t order, double gamma)
Create a penalty expression for DG methods.
Definition dg_utils.hpp:265
double faceMeasureForPenalty(const CellType &cell, std::size_t face_id)
Measure |F| of a face of a cell, as used by the Kb penalties: the face diameter in 2D (equal to its l...
Definition dg_utils.hpp:289
double forceEqualMean(const dofs::DofHandler< MeshType > &dof_handler, la::Vector &u, const la::Vector &u_reference, const la::SparseMatrix &mass_matrix, const la::Vector &rhs_const, double domain_measure)
Adjust a solution to match the average of a reference solution.
Definition dg_utils.hpp:540
auto createPenaltyExpressionHarmonic(std::size_t order, double gamma)
Create a penalty expression for DG methods using the harmonic mean between the two cells.
Definition dg_utils.hpp:351
la::Vector l2ProjectExpressionScalar(const MeshType &mesh, CellManagerType &cell_manager, const DofHandlerType &dof_handler, const FeSpaceType &fe_space, const Expression &expr, const la::SparseMatrix &mass_scalar)
L2-project a scalar expression using a precomputed scalar mass matrix.
Definition dg_utils.hpp:590
auto createPenaltyExpressionKb(std::size_t order, double gamma)
Create a penalty expression for DG methods using the face's Kb.
Definition dg_utils.hpp:310
double computeError(const dofs::DofHandler< MeshType > &dof_handler, const la::Vector &u_h, const la::Vector &u_exact, const la::SparseMatrix &matrix)
Compute the error between a discrete solution and an exact one.
Definition dg_utils.hpp:462
double computeDGError(double errH1int, double errJump)
compute the total DG error by combining the H1 semi-norm error and the jump error.
Definition dg_utils.hpp:87
la::Vector projectConstantOntoModalSpace(const dofs::DofHandler< MeshType > &dof_handler, double constant_value, const la::SparseMatrix &mass_matrix, const la::Vector &rhs_const)
Project a constant value onto modal DG space using L2 projection.
Definition dg_utils.hpp:510
auto createUpwindFlux(VelocityFunc velocity_func)
Create an upwind flux expression from a velocity callable.
Definition dg_utils.hpp:445
double computeH1SemiError(const MeshType &mesh, assemble::CellManager< MeshType, FeSpaceType > &cell_manager, const DofHandlerType &dof_handler, const la::Vector &u_h, const ExprType &grad_u_exact_expr, std::optional< uint32_t > quad_degree=std::nullopt)
Compute the H1 semi-norm error integral between a numerical solution and an exact gradient.
Definition dg_utils.hpp:211
Definition cell_manager.hpp:39
Definition dof_policy.hpp:45
std::underlying_type_t< UpdateFlags > UpdateFlagsType
Definition fe_values.hpp:65
std::vector< std::unique_ptr< CellType< Dim > > > CellsType
Definition fe_expr_base.hpp:44
FeExpressionFunc< IndexBegin, IndexEnd, FeSpace::dim, FeSpace, true > TestFunction
Definition fe_expr_basis.hpp:169
@ UpdateNormals
Definition fe_values.hpp:59
@ UpdateQuadraturePts
Definition fe_values.hpp:58
@ IsNothing
Definition fe_expr_base.hpp:52
FeExpressionInner< Dim, FeExpressionU, FeExpressionV > inner(const FeExpressionBase< Dim, FeExpressionU > &u, const FeExpressionBase< Dim, FeExpressionV > &v)
Definition fe_expr_op.hpp:224
double dot(const Vector &a, const Vector &b)
Definition vector.cpp:368
void extractDiagonalBlockComponent(const SparseMatrix &blocked_mat, std::size_t component, SparseMatrix &scalar_mat, double zero_tol=0.0)
Extract one scalar diagonal component from a blocked matrix.
Definition vector_block_utils.hpp:192
static constexpr std::size_t num_triangle_tabled_rules
Number of precomputed quadrature rules for triangles.
Definition simplex.hpp:168
static constexpr std::size_t num_tetrahedron_tabled_rules
Number of precomputed quadrature rules for tetrahedra.
Definition simplex.hpp:184
Definition cell_manager.hpp:38
vulpes::utils::pcout_t & pcout
Definition pcout.hpp:160