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
17#include <prismspf/core/types.h>
18
20
22
23#include <prismspf/config.h>
24
26
27#include <memory>
28#include <vector>
29
30#if DEAL_II_VERSION_MAJOR >= 9 && DEAL_II_VERSION_MINOR >= 7
31# include <deal.II/base/enable_observer_pointer.h>
32# define MATRIX_FREE_OPERATOR_BASE dealii::EnableObserverPointer
33#else
34# include <deal.II/base/subscriptor.h>
35# define MATRIX_FREE_OPERATOR_BASE dealii::Subscriptor
36#endif
37
39
53template <unsigned int dim, unsigned int degree, typename number>
55{
56public:
57 using ScalarValue = dealii::VectorizedArray<number>;
58 using VectorValue = dealii::Tensor<1, dim, ScalarValue>;
59
61 FieldContainer<dim, degree, number> &, /* variable_list */
62 const SimulationTimer &, /* sim_timer */
63 unsigned int /* solve_block_id */
64 ) const;
65
66 template <TensorRank Rank>
67 using Value = std::conditional_t<Rank == TensorRank::Scalar || dim == 1,
69 dealii::Tensor<int(Rank), dim, ScalarValue>>;
70
71 template <TensorRank Rank>
72 static Value<Rank>
74 {
75 if constexpr (Rank == TensorRank::Scalar || dim == 1)
76 {
77 return ScalarValue(1.0);
78 }
79 else
80 {
81 static Value<Rank> ident = []()
82 {
83 Value<Rank> obj;
84 for (int i = 0; i < Value<Rank>::n_independent_components; ++i)
85 {
87 }
88 return obj;
89 }();
90 return ident;
91 }
92 }
93
94 template <TensorRank Rank>
95 static Value<Rank>
97 {
98 if constexpr (Rank == TensorRank::Scalar || dim == 1)
99 {
100 return ScalarValue(0.0);
101 }
102 else
103 {
104 return Value<Rank>();
105 }
106 }
107
114 Operator oper,
115 const std::vector<FieldAttributes> &_field_attributes,
116 const SolutionIndexer<dim, number> &_solution_indexer,
117 const MatrixFreeManager<dim, number> &_matrix_free_manager,
118 DependencyMap _dependency_map,
119 const SimulationTimer &_sim_timer)
121 , pde_operator(&operator_owner)
122 , pde_op(oper)
123 , field_attributes(_field_attributes)
124 , solution_indexer(&_solution_indexer)
125 , matrix_free_manager(&_matrix_free_manager)
126 , dependency_map(std::move(_dependency_map))
127 , sim_timer(&_sim_timer)
128 {
130 }
131
146 void
148 {
149 solve_block = dst_solution.get_solve_block();
151 for (unsigned int field_index : solve_block.field_indices)
152 {
153 dependency_map[field_index]; // creates entry if not already present
154 }
155 }
156
157 void
158 set_relative_level(unsigned int _relative_level)
159 {
160 relative_level = _relative_level;
161 AssertThrow(matrix_free_manager != nullptr, dealii::ExcNotInitialized());
162 data = &(matrix_free_manager->get_shared_matrix_free(relative_level));
163 }
164
165 // public:
170 void
172 const BlockVector<number> &src = BlockVector<number>()) const;
173
174private:
179 void
180 compute_local_operator(const MatrixFree<dim, number> &_data,
182 const BlockVector<number> &src,
183 const std::pair<unsigned int, unsigned int> &cell_range) const;
184
185public:
189 void
191
192private:
196 void
197 compute_local_diagonal(const MatrixFree<dim, number> &_data,
198 BlockVector<number> &diagonal,
199 const BlockVector<number> &dummy_src,
200 const std::pair<unsigned int, unsigned int> &cell_range) const;
201
202 template <TensorRank Rank>
203 void
205 BlockVector<number> &diagonal,
206 unsigned int field_index) const;
207
208public:
212 void
214 const std::vector<const SolutionVector<number> *> &diagonal)
215 {
216 scaling_diagonal = diagonal;
217 scale_by_diagonal = scale;
218 }
219
223 dealii::types::global_dof_index
224 m() const;
225
231 number
232 el(const unsigned int &row, const unsigned int &col) const;
233
238 void
239 clear();
240
245 const MatrixFree<dim, number> *
246 get_matrix_free() const;
247
251 const std::shared_ptr<dealii::DiagonalMatrix<BlockVector<number>>> &
253
258 void
259 vmult(BlockVector<number> &dst, const BlockVector<number> &src) const;
260
261 // NOLINTBEGIN(readability-identifier-naming)
262
266 void
267 Tvmult(BlockVector<number> &dst, const BlockVector<number> &src) const;
268
269 // NOLINTEND(readability-identifier-naming)
270
274 void
276
280 void
282
287 bool read_plain = false;
288
289private:
293 std::vector<FieldAttributes> field_attributes;
294
299
308
313
318
322 const MatrixFree<dim, number> *data = nullptr;
323
327 unsigned int relative_level = 0;
328
333
337 const SimulationTimer *sim_timer = nullptr;
338
342 std::vector<const SolutionVector<number> *> scaling_diagonal;
343
347 bool scale_by_diagonal = false;
348
352 std::vector<unsigned int> field_to_block_index;
353
357 std::vector<std::vector<unsigned int>> edge_constrained_indices;
358
362 std::shared_ptr<dealii::DiagonalMatrix<BlockVector<number>>> diagonal_entries =
363 std::make_shared<dealii::DiagonalMatrix<BlockVector<number>>>();
364
368 std::shared_ptr<dealii::DiagonalMatrix<BlockVector<number>>> inverse_diagonal_entries =
369 std::make_shared<dealii::DiagonalMatrix<BlockVector<number>>>();
370};
371
372PRISMS_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
Class that manages solution initialization and swapping with old solutions.
Definition group_solution_handler.h:68
const SolveBlock & get_solve_block() const
Get the underlying solve block object.
Definition group_solution_handler.cc:145
const std::vector< unsigned int > & get_global_to_block_index() const
Get the block index from the global index.
Definition group_solution_handler.cc:152
dealii::types::global_dof_index m() const
Return the number of DoFs.
Definition mf_operator.cc:235
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:273
static Value< Rank > identity()
Definition mf_operator.h:73
Operator pde_op
The actual PDE operator function ptr (eg. compute_rhs) for user defined PDEs.
Definition mf_operator.h:307
std::shared_ptr< dealii::DiagonalMatrix< BlockVector< number > > > inverse_diagonal_entries
The inverse diagonal matrix.
Definition mf_operator.h:368
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:67
void Tvmult(BlockVector< number > &dst, const BlockVector< number > &src) const
Transpose matrix-vector multiplication.
Definition mf_operator.cc:292
dealii::VectorizedArray< number > ScalarValue
Definition mf_operator.h:57
unsigned int relative_level
Level so that correct fields are read from indexer.
Definition mf_operator.h:327
DependencyMap dependency_map
Which fields should be available to the solve.
Definition mf_operator.h:332
void eval_matrix_diagonal()
Evaluate matrix diagonal (and inverse).
Definition mf_operator.cc:221
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:342
dealii::Tensor< 1, dim, ScalarValue > VectorValue
Definition mf_operator.h:58
SolveBlock solve_block
The block being solved.
Definition mf_operator.h:298
std::vector< unsigned int > field_to_block_index
Mapping from field index to block index (only for dst).
Definition mf_operator.h:352
std::shared_ptr< dealii::DiagonalMatrix< BlockVector< number > > > diagonal_entries
The diagonal matrix.
Definition mf_operator.h:362
static Value< Rank > zero()
Definition mf_operator.h:96
void clear()
Release all memory and return to state like having called the default constructor.
Definition mf_operator.cc:257
void initialize(const GroupSolutionHandler< dim, number > &dst_solution)
Initialize.
Definition mf_operator.h:147
void compute_diagonal(BlockVector< number > &dst, const BlockVector< number > &src) const
Compute the diagonal of this operator.
Definition mf_operator.cc:81
MFOperator(const PDEOperatorBase< dim, degree, number > &operator_owner, Operator oper, const std::vector< FieldAttributes > &_field_attributes, const SolutionIndexer< dim, number > &_solution_indexer, const MatrixFreeManager< dim, number > &_matrix_free_manager, DependencyMap _dependency_map, const SimulationTimer &_sim_timer)
Constructor.
Definition mf_operator.h:113
const SimulationTimer * sim_timer
Simulation timer.
Definition mf_operator.h:337
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:357
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:266
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:60
const PDEOperatorBase< dim, degree, number > * pde_operator
PDE operator object (owning class instance of pde_op) for user defined PDEs.
Definition mf_operator.h:303
void reinit_matrix_diagonal(const BlockVector< number > &shape)
Reinit diagonal matrix to have the correct shape.
Definition mf_operator.cc:213
void set_scaling_diagonal(bool scale, const std::vector< const SolutionVector< number > * > &diagonal)
Set scaling diagonal.
Definition mf_operator.h:213
bool read_plain
Whether to read plain dof values from src, otherwise applies homogeneous part of constraints to the r...
Definition mf_operator.h:287
const MatrixFreeManager< dim, number > * matrix_free_manager
Matrix-free manager.
Definition mf_operator.h:317
bool scale_by_diagonal
Whether or not to scale after operator result.
Definition mf_operator.h:347
const SolutionIndexer< dim, number > * solution_indexer
Read-access to fields.
Definition mf_operator.h:312
void set_relative_level(unsigned int _relative_level)
Definition mf_operator.h:158
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:248
const MatrixFree< dim, number > * data
Matrix-free object.
Definition mf_operator.h:322
void vmult(BlockVector< number > &dst, const BlockVector< number > &src) const
Matrix-vector multiplication.
Definition mf_operator.cc:282
std::vector< FieldAttributes > field_attributes
The attribute list of the relevant variables.
Definition mf_operator.h:293
Containers for matrix free objects.
Definition matrix_free_manager.h:28
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 groups.
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 group_solution_handler.h:35
dealii::LinearAlgebra::distributed::BlockVector< number > BlockVector
Typedef for solution block vector.
Definition group_solution_handler.h:29
#define MATRIX_FREE_OPERATOR_BASE
Definition mf_operator.h:35
Definition conditional_ostreams.cc:20
Definition vectorized_operations.h:17
@ Scalar
Definition type_enums.h:54