PRISMS-PF Manual
Loading...
Searching...
No Matches
mf_operator.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: © 2025 PRISMS Center at the University of Michigan
2// SPDX-License-Identifier: GNU Lesser General Public Version 2.1
3
4#pragma once
5
6#include <deal.II/base/vectorization.h>
7#include <deal.II/matrix_free/matrix_free.h>
8#include <deal.II/matrix_free/operators.h>
9
18#include <prismspf/core/types.h>
19
21
23
24#include <prismspf/config.h>
25
26#include <memory>
27#include <vector>
28
29#if DEAL_II_VERSION_MAJOR >= 9 && DEAL_II_VERSION_MINOR >= 7
30# include <deal.II/base/enable_observer_pointer.h>
31# define MATRIX_FREE_OPERATOR_BASE dealii::EnableObserverPointer
32#else
33# include <deal.II/base/subscriptor.h>
34# define MATRIX_FREE_OPERATOR_BASE dealii::Subscriptor
35#endif
36
38
52template <unsigned int dim, unsigned int degree, typename number>
54{
55public:
56 using ScalarValue = dealii::VectorizedArray<number>;
57 using VectorValue = dealii::Tensor<1, dim, ScalarValue>;
58
60 FieldContainer<dim, degree, number> &, /* variable_list */
61 const SimulationTimer &, /* sim_timer */
62 unsigned int /* solve_block_id */
63 ) const;
64
65 template <TensorRank Rank>
66 using Value = std::conditional_t<Rank == TensorRank::Scalar || dim == 1,
68 dealii::Tensor<int(Rank), dim, ScalarValue>>;
69
70 template <TensorRank Rank>
71 static Value<Rank>
73 {
74 if constexpr (Rank == TensorRank::Scalar || dim == 1)
75 {
76 return ScalarValue(1.0);
77 }
78 else
79 {
80 static Value<Rank> ident = []()
81 {
82 Value<Rank> obj;
83 for (int i = 0; i < Value<Rank>::n_independent_components; ++i)
84 {
86 }
87 return obj;
88 }();
89 return ident;
90 }
91 }
92
93 template <TensorRank Rank>
94 static Value<Rank>
96 {
97 if constexpr (Rank == TensorRank::Scalar || dim == 1)
98 {
99 return ScalarValue(0.0);
100 }
101 else
102 {
103 return Value<Rank>();
104 }
105 }
106
110 MFOperator() = default;
111
116 void
118 Operator oper,
119 std::vector<FieldAttributes> _field_attributes,
120 const SolutionIndexer<dim, number> &_solution_indexer,
121 const MatrixFreeManager<dim, number> &_matrix_free_manager,
122 const SimulationTimer &_sim_timer,
123 SolveBlock _solve_block,
124 DependencyMap _dependency_map)
125 {
126 pde_operator = &operator_owner;
127 pde_op = oper;
128 field_attributes = std::move(_field_attributes);
129 solution_indexer = &_solution_indexer;
130 matrix_free_manager = &_matrix_free_manager;
131 sim_timer = &_sim_timer;
132 solve_block = std::move(_solve_block);
133 dependency_map = std::move(_dependency_map);
134 field_to_block_index = solution_indexer->get_block_indices();
135 for (unsigned int field_index : solve_block.field_indices)
136 {
137 dependency_map[field_index]; // creates entry if not already present
138 }
140 }
141
142 void
143 set_relative_level(unsigned int _relative_level)
144 {
145 relative_level = _relative_level;
146 AssertThrow(matrix_free_manager != nullptr, dealii::ExcNotInitialized());
147 if (relative_level == -1)
148 {
149 data = &(matrix_free_manager->get_shared_matrix_free());
150 }
151 else
152 {
153 data = &(matrix_free_manager->get_mg_shared_matrix_free(relative_level));
154 }
155 }
156
157 // public:
162 void
164 const BlockVector<number> &src = BlockVector<number>()) const;
165
166private:
171 void
172 compute_local_operator(const MatrixFree<dim, number> &_data,
174 const BlockVector<number> &src,
175 const std::pair<unsigned int, unsigned int> &cell_range) const;
176
177public:
181 void
183
184private:
188 void
189 compute_local_diagonal(const MatrixFree<dim, number> &_data,
190 BlockVector<number> &diagonal,
191 const BlockVector<number> &dummy_src,
192 const std::pair<unsigned int, unsigned int> &cell_range) const;
193
194 template <TensorRank Rank>
195 void
197 BlockVector<number> &diagonal,
198 unsigned int field_index) const;
199
200public:
204 void
206 const std::vector<const SolutionVector<number> *> &diagonal)
207 {
208 scaling_diagonal = diagonal;
209 scale_by_diagonal = scale;
210 }
211
215 dealii::types::global_dof_index
216 m() const;
217
223 number
224 el(const unsigned int &row, const unsigned int &col) const;
225
230 void
231 clear();
232
237 const MatrixFree<dim, number> *
238 get_matrix_free() const;
239
243 const std::shared_ptr<dealii::DiagonalMatrix<BlockVector<number>>> &
245
250 void
251 vmult(BlockVector<number> &dst, const BlockVector<number> &src) const;
252
253 // NOLINTBEGIN(readability-identifier-naming)
254
258 void
259 Tvmult(BlockVector<number> &dst, const BlockVector<number> &src) const;
260
261 // NOLINTEND(readability-identifier-naming)
262
266 void
268
272 void
274
279 bool read_plain = false;
280
281private:
285 std::vector<FieldAttributes> field_attributes;
286
291
299 Operator pde_op = nullptr;
300
305
310
314 const MatrixFree<dim, number> *data = nullptr;
315
319 unsigned int relative_level = -1;
320
325
329 const SimulationTimer *sim_timer = nullptr;
330
334 std::vector<const SolutionVector<number> *> scaling_diagonal;
335
339 bool scale_by_diagonal = false;
340
344 std::vector<unsigned int> field_to_block_index;
345
349 std::vector<std::vector<unsigned int>> edge_constrained_indices;
350
354 std::shared_ptr<dealii::DiagonalMatrix<BlockVector<number>>> diagonal_entries =
355 std::make_shared<dealii::DiagonalMatrix<BlockVector<number>>>();
356
360 std::shared_ptr<dealii::DiagonalMatrix<BlockVector<number>>> inverse_diagonal_entries =
361 std::make_shared<dealii::DiagonalMatrix<BlockVector<number>>>();
362};
363
364PRISMS_PF_END_NAMESPACE
This class permits the access of a subset of indexed fields and gives an error if any non-allowed fie...
Definition field_container.h:47
dealii::types::global_dof_index m() const
Return the number of DoFs.
Definition mf_operator.cc:250
const std::shared_ptr< dealii::DiagonalMatrix< BlockVector< number > > > & get_matrix_diagonal_inverse() const
Get read access to the inverse diagonal of this operator.
Definition mf_operator.cc:288
static Value< Rank > identity()
Definition mf_operator.h:72
Operator pde_op
The actual PDE operator function ptr (eg. compute_rhs) for user defined PDEs.
Definition mf_operator.h:299
void reinit_matrix_diagonal()
Reinit diagonal matrix to have the correct shape.
Definition mf_operator.cc:213
std::shared_ptr< dealii::DiagonalMatrix< BlockVector< number > > > inverse_diagonal_entries
The inverse diagonal matrix.
Definition mf_operator.h:360
MFOperator()=default
Constructor.
void compute_local_diagonal(const MatrixFree< dim, number > &_data, BlockVector< number > &diagonal, const BlockVector< number > &dummy_src, const std::pair< unsigned int, unsigned int > &cell_range) const
Local computation of the diagonal of the operator.
Definition mf_operator.cc:96
void compute_local_operator(const MatrixFree< dim, number > &_data, BlockVector< number > &dst, const BlockVector< number > &src, const std::pair< unsigned int, unsigned int > &cell_range) const
Calls user-defined operator.
Definition mf_operator.cc:33
std::conditional_t< Rank==TensorRank::Scalar||dim==1, ScalarValue, dealii::Tensor< int(Rank), dim, ScalarValue > > Value
Definition mf_operator.h:66
void Tvmult(BlockVector< number > &dst, const BlockVector< number > &src) const
Transpose matrix-vector multiplication.
Definition mf_operator.cc:307
dealii::VectorizedArray< number > ScalarValue
Definition mf_operator.h:56
unsigned int relative_level
Level so that correct fields are read from indexer.
Definition mf_operator.h:319
DependencyMap dependency_map
Which fields should be available to the solve.
Definition mf_operator.h:324
void eval_matrix_diagonal()
Evaluate matrix diagonal (and inverse).
Definition mf_operator.cc:236
void compute_local_field_diagonal(FieldContainer< dim, degree, number > &variable_list, BlockVector< number > &diagonal, unsigned int field_index) const
Definition mf_operator.cc:141
std::vector< const SolutionVector< number > * > scaling_diagonal
Result of operator gets scaled by this (invm for explicit fields)
Definition mf_operator.h:334
dealii::Tensor< 1, dim, ScalarValue > VectorValue
Definition mf_operator.h:57
SolveBlock solve_block
The block being solved.
Definition mf_operator.h:290
std::vector< unsigned int > field_to_block_index
Mapping from field index to block index (only for dst).
Definition mf_operator.h:344
std::shared_ptr< dealii::DiagonalMatrix< BlockVector< number > > > diagonal_entries
The diagonal matrix.
Definition mf_operator.h:354
static Value< Rank > zero()
Definition mf_operator.h:95
void clear()
Release all memory and return to state like having called the default constructor.
Definition mf_operator.cc:272
void compute_diagonal(BlockVector< number > &dst, const BlockVector< number > &src) const
Compute the diagonal of this operator.
Definition mf_operator.cc:81
const SimulationTimer * sim_timer
Simulation timer.
Definition mf_operator.h:329
std::vector< std::vector< unsigned int > > edge_constrained_indices
Indices of DoFs on edge in case the operator is used in GMG context.
Definition mf_operator.h:349
const MatrixFree< dim, number > * get_matrix_free() const
Get read access to the MatrixFree<dim, number> object stored with this operator.
Definition mf_operator.cc:281
void compute_operator(BlockVector< number > &dst, const BlockVector< number > &src=BlockVector< number >()) const
Calls cell_loop on function that calls user-defined operator.
Definition mf_operator.cc:18
void(PDEOperatorBase< dim, degree, number >::*)( FieldContainer< dim, degree, number > &, const SimulationTimer &, unsigned int) const Operator
Definition mf_operator.h:59
const PDEOperatorBase< dim, degree, number > * pde_operator
PDE operator object (owning class instance of pde_op) for user defined PDEs.
Definition mf_operator.h:295
void set_scaling_diagonal(bool scale, const std::vector< const SolutionVector< number > * > &diagonal)
Set scaling diagonal.
Definition mf_operator.h:205
bool read_plain
Whether to read plain dof values from src, otherwise applies homogeneous part of constraints to the r...
Definition mf_operator.h:279
const MatrixFreeManager< dim, number > * matrix_free_manager
Matrix-free manager.
Definition mf_operator.h:309
bool scale_by_diagonal
Whether or not to scale after operator result.
Definition mf_operator.h:339
const SolutionIndexer< dim, number > * solution_indexer
Read-access to fields.
Definition mf_operator.h:304
void set_relative_level(unsigned int _relative_level)
Definition mf_operator.h:143
number el(const unsigned int &row, const unsigned int &col) const
Return the value of the matrix entry. This function is only valid when row == col and when the diagon...
Definition mf_operator.cc:263
const MatrixFree< dim, number > * data
Matrix-free object.
Definition mf_operator.h:314
void init(const PDEOperatorBase< dim, degree, number > &operator_owner, Operator oper, std::vector< FieldAttributes > _field_attributes, const SolutionIndexer< dim, number > &_solution_indexer, const MatrixFreeManager< dim, number > &_matrix_free_manager, const SimulationTimer &_sim_timer, SolveBlock _solve_block, DependencyMap _dependency_map)
Initialize.
Definition mf_operator.h:117
void vmult(BlockVector< number > &dst, const BlockVector< number > &src) const
Matrix-vector multiplication.
Definition mf_operator.cc:297
std::vector< FieldAttributes > field_attributes
The attribute list of the relevant variables.
Definition mf_operator.h:285
Containers for matrix free objects.
Definition matrix_free_manager.h:45
This class contains the user implementation of each PDE operator.
Definition pde_operator_base.h:27
Definition simulation_timer.h:13
Class that provides access to solution vectors spread across different blocks.
Definition solution_indexer.h:20
Structure to hold the attributes of a solve-block.
Definition solve_block.h:59
std::map< Types::Index, Dependency > DependencyMap
Definition dependencies.h:129
@ Value
Use value of the variable as a criterion for refinement.
Definition grid_refiner_criterion.h:31
typename BlockVector< number >::BlockType SolutionVector
Typedef for solution vector.
Definition matrix_free_manager.h:38
dealii::LinearAlgebra::distributed::BlockVector< number > BlockVector
Typedef for solution block vector.
Definition matrix_free_manager.h:32
#define MATRIX_FREE_OPERATOR_BASE
Definition mf_operator.h:34
Definition conditional_ostreams.cc:20
@ Scalar
Definition type_enums.h:54