/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/discretization/data/data.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/discretization/data/data.hh Source File
DiFfRG
Discretization Framework for functional Renormalization Group flows
data.hh
Go to the documentation of this file.
1#pragma once
2
3// external libraries
4#include <deal.II/base/function.h>
5#include <deal.II/base/quadrature_lib.h>
6#include <deal.II/dofs/dof_handler.h>
7#include <deal.II/fe/fe_values.h>
8#include <deal.II/lac/vector.h>
9
10// DiFfRG
15#include <deal.II/numerics/vector_tools.h>
16
17#include <algorithm>
18#include <functional>
19#include <vector>
20
21namespace DiFfRG
22{
23 using namespace dealii;
24
25 namespace internal
26 {
27 template <uint dim, typename NumberType> class FunctionFromLambda : public dealii::Function<dim, NumberType>
28 {
29 using FUN = std::function<void(const Point<dim> &, Vector<NumberType> &)>;
30
31 public:
32 FunctionFromLambda(FUN &&fun_, uint components) : Function<dim, NumberType>(components), fun(std::move(fun_)) {}
33 virtual void vector_value(const Point<dim> &p, Vector<NumberType> &values) const override { fun(p, values); }
34
35 private:
37 };
38 } // namespace internal
39
47 template <typename NT = double> class FlowingVariables : public AbstractFlowingVariables<NT>
48 {
49 public:
50 using NumberType = NT;
51
56
63 template <typename Model> void interpolate(const Model &model)
64 {
65 std::vector<uint> block_structure{0};
66 block_structure.push_back(Model::Components::count_variables());
67 m_data = (block_structure);
68 if (m_data.n_blocks() > 1) model.initial_condition_variables(m_data.block(1));
69 }
70
76 virtual BlockVector<NumberType> &data() override { return m_data; }
77 virtual const BlockVector<NumberType> &data() const override { return m_data; }
78
84 virtual Vector<NumberType> &spatial_data() override { return m_data.block(0); }
85 virtual const Vector<NumberType> &spatial_data() const override { return m_data.block(0); }
86
92 virtual Vector<NumberType> &variable_data() override { return m_data.block(1); }
93 virtual const Vector<NumberType> &variable_data() const override { return m_data.block(1); }
94
95 private:
96 BlockVector<NumberType> m_data;
97 };
98
99 namespace FE
100 {
108 template <typename Discretization>
110 : public AbstractFlowingVariables<typename Discretization::NumberType, typename Discretization::VectorType>
111 {
112 public:
113 using NumberType = typename Discretization::NumberType;
114 using VectorType = typename Discretization::VectorType;
116 using Components = typename Discretization::Components;
117 static constexpr uint dim = Discretization::dim;
118
124 FlowingVariables(const Discretization &discretization)
126 {
127 }
128
135 template <typename Model> void interpolate(const Model &model)
136 {
137 auto block_structure = discretization.get_block_structure();
138 reinit_la_block_vector(m_data, block_structure, discretization.get_locally_owned_dofs(),
139 discretization.get_communicator());
140
141 if constexpr (Model::Components::count_fe_functions() > 0) {
142 auto interpolating_function = [&model](const auto &p, auto &values) { model.initial_condition(p, values); };
143 internal::FunctionFromLambda<dim, NumberType> initial_condition_function(std::move(interpolating_function),
144 dof_handler.get_fe().n_components());
145 VectorTools::interpolate(dof_handler, initial_condition_function, m_data.block(0));
146 }
147 if (m_data.n_blocks() > 1) model.initial_condition_variables(m_data.block(1));
148 }
149
155 virtual BlockVectorType &data() override { return m_data; }
156 virtual const BlockVectorType &data() const override { return m_data; }
157
163 virtual VectorType &spatial_data() override { return m_data.block(0); }
164 virtual const VectorType &spatial_data() const override { return m_data.block(0); }
165
171 virtual VectorType &variable_data() override { return m_data.block(1); }
172 virtual const VectorType &variable_data() const override { return m_data.block(1); }
173
174 private:
175 const Discretization &discretization;
176 const DoFHandler<dim> &dof_handler;
178 };
179 } // namespace FE
180
181 namespace FV
182 {
189 template <typename Discretization>
191 : public AbstractFlowingVariables<typename Discretization::NumberType, typename Discretization::VectorType>
192 {
193 public:
198 static constexpr uint dim = Discretization::dim;
199
209
216 template <typename Model> void interpolate(const Model &model)
217 {
218 auto block_structure = discretization.get_block_structure();
221
222 if constexpr (Model::Components::count_fe_functions() > 0) {
223 const auto &fe = discretization.get_fe();
224 const unsigned int n_components = fe.n_components();
225
226 QGauss<dim> quadrature(2);
227 FEValues<dim> fe_values(discretization.get_mapping(), fe, quadrature,
228 update_quadrature_points | update_JxW_values);
229
230 std::vector<types::global_dof_index> dof_indices(fe.dofs_per_cell);
231 Vector<NumberType> values(n_components);
232 std::vector<NumberType> averaged_values(n_components);
233
234 for (const auto &cell : dof_handler.active_cell_iterators()) {
235 // Always true on a serial mesh, so this costs the serial path nothing. On a
236 // partitioned one it stops every rank writing every cell average: those writes go
237 // through a single-valued insert, so without the filter the result is only correct by
238 // accident, and the compress() below would have nothing well-defined to do.
239 if (!cell->is_locally_owned()) continue;
240 fe_values.reinit(cell);
241 cell->get_dof_indices(dof_indices);
242 std::fill(averaged_values.begin(), averaged_values.end(), NumberType(0.0));
243
244 NumberType cell_measure = 0.0;
245 for (unsigned int q = 0; q < quadrature.size(); ++q) {
246 values = 0.0;
247 model.initial_condition(fe_values.quadrature_point(q), values);
248
249 const NumberType weight = fe_values.JxW(q);
250 cell_measure += weight;
251 for (unsigned int c = 0; c < n_components; ++c)
252 averaged_values[c] += weight * values[c];
253 }
254
255 AssertThrow(cell_measure > NumberType(0.0),
256 ExcMessage("Cannot initialize FV cell average on a cell with non-positive measure."));
257 for (auto &value : averaged_values)
258 value /= cell_measure;
259
260 for (unsigned int local_dof = 0; local_dof < fe.dofs_per_cell; ++local_dof) {
261 const auto component = fe.system_to_component_index(local_dof).first;
262 m_data.block(0)[dof_indices[local_dof]] = averaged_values[component];
263 }
264 }
265 // insert, not add: each owned cell's average is written exactly once by its owner. A
266 // no-op for the serial vector types.
267 m_data.block(0).compress(dealii::VectorOperation::insert);
268 }
269 if (m_data.n_blocks() > 1) model.initial_condition_variables(m_data.block(1));
270 }
271
277 virtual BlockVectorType &data() override { return m_data; }
278 virtual const BlockVectorType &data() const override { return m_data; }
279
285 virtual VectorType &spatial_data() override { return m_data.block(0); }
286 virtual const VectorType &spatial_data() const override { return m_data.block(0); }
287
293 virtual VectorType &variable_data() override { return m_data.block(1); }
294 virtual const VectorType &variable_data() const override { return m_data.block(1); }
295
296 private:
298 const DoFHandler<dim> &dof_handler;
300 };
301 } // namespace FV
302
303} // namespace DiFfRG
A class to set up initial data for whatever discretization we have chosen. Also used to switch/manage...
Definition abstract_data.hh:21
A class to set up initial data for whatever discretization we have chosen. Also used to switch/manage...
Definition data.hh:111
virtual VectorType & variable_data() override
Obtain the variable data vector.
Definition data.hh:171
void interpolate(const Model &model)
Interpolates the initial condition from a numerical model.
Definition data.hh:135
virtual const VectorType & variable_data() const override
Definition data.hh:172
BlockVectorType m_data
Definition data.hh:177
virtual VectorType & spatial_data() override
Obtain the spatial data vector.
Definition data.hh:163
typename Discretization::Components Components
Definition data.hh:116
typename Discretization::NumberType NumberType
Definition data.hh:113
const DoFHandler< dim > & dof_handler
Definition data.hh:176
static constexpr uint dim
Definition data.hh:117
const Discretization & discretization
Definition data.hh:175
virtual const VectorType & spatial_data() const override
Definition data.hh:164
virtual const BlockVectorType & data() const override
Definition data.hh:156
FlowingVariables(const Discretization &discretization)
Construct a new Flowing Variables object.
Definition data.hh:124
virtual BlockVectorType & data() override
Obtain the data vector holding both spatial (block 0) and variable (block 1) data.
Definition data.hh:155
typename Discretization::VectorType VectorType
Definition data.hh:114
get_type::BlockVectorType< VectorType > BlockVectorType
Definition data.hh:115
Class to manage the system on which we solve, i.e. fe spaces, grids, etc. This class is a System for ...
Definition discretization.hh:46
const auto & get_mapping() const
Definition discretization.hh:120
typename DiFfRG::internal::components_of< ModelOrComponents_ >::type Components
Definition discretization.hh:50
const auto & get_fe(uint i=0) const
Definition discretization.hh:115
static constexpr uint dim
Definition discretization.hh:55
std::vector< uint > get_block_structure() const
Definition discretization.hh:171
const dealii::IndexSet & get_locally_owned_dofs() const
The dof rows this rank owns. Complete on a serial mesh.
Definition discretization.hh:144
MPI_Comm get_communicator() const
MPI_COMM_SELF on a serial mesh, so callers never have to branch on the build type.
Definition discretization.hh:153
NumberType_ NumberType
Definition discretization.hh:51
VectorType_ VectorType
Definition discretization.hh:52
A class to set up cell-averaged initial data for finite-volume systems.
Definition data.hh:192
virtual VectorType & variable_data() override
Obtain the variable data vector.
Definition data.hh:293
typename Discretization::NumberType NumberType
Definition data.hh:194
virtual const VectorType & spatial_data() const override
Definition data.hh:286
BlockVectorType m_data
Definition data.hh:299
void interpolate(const Model &model)
Initializes FV data with cell averages of the model initial condition.
Definition data.hh:216
const Discretization & discretization
Definition data.hh:297
virtual BlockVectorType & data() override
Obtain the data vector holding both spatial (block 0) and variable (block 1) data.
Definition data.hh:277
virtual VectorType & spatial_data() override
Obtain the spatial data vector.
Definition data.hh:285
virtual const VectorType & variable_data() const override
Definition data.hh:294
FlowingVariables(const Discretization &discretization)
Construct a new Flowing Variables object.
Definition data.hh:205
const DoFHandler< dim > & dof_handler
Definition data.hh:298
typename Discretization::VectorType VectorType
Definition data.hh:195
get_type::BlockVectorType< VectorType > BlockVectorType
Definition data.hh:196
typename Discretization::Components Components
Definition data.hh:197
static constexpr uint dim
Definition data.hh:198
virtual const BlockVectorType & data() const override
Definition data.hh:278
A class to set up initial data for whatever discretization we have chosen. Also used to switch/manage...
Definition data.hh:48
void interpolate(const Model &model)
Interpolates the initial condition from a numerical model.
Definition data.hh:63
virtual Vector< NumberType > & variable_data() override
Obtain the variable data vector.
Definition data.hh:92
virtual const Vector< NumberType > & spatial_data() const override
Definition data.hh:85
virtual Vector< NumberType > & spatial_data() override
Obtain the spatial data vector.
Definition data.hh:84
BlockVector< NumberType > m_data
Definition data.hh:96
virtual const BlockVector< NumberType > & data() const override
Definition data.hh:77
virtual const Vector< NumberType > & variable_data() const override
Definition data.hh:93
FlowingVariables()
Construct a new Flowing Variables object.
Definition data.hh:55
virtual BlockVector< NumberType > & data() override
Obtain the data vector holding both spatial (block 0) and variable (block 1) data.
Definition data.hh:76
NT NumberType
Definition data.hh:50
FUN fun
Definition data.hh:36
virtual void vector_value(const Point< dim > &p, Vector< NumberType > &values) const override
Definition data.hh:33
std::function< void(const Point< dim > &, Vector< NumberType > &)> FUN
Definition data.hh:29
FunctionFromLambda(FUN &&fun_, uint components)
Definition data.hh:32
All compile-time knowledge about which linear algebra types DiFfRG uses.
typename internal::_BlockVectorType< VectorType >::value BlockVectorType
The block-vector type belonging to a given vector type.
Definition linear_algebra.hh:167
Definition complex_math.hh:10
unsigned int uint
Definition utils.hh:24
void reinit_la_block_vector(BlockVectorType &vec, const std::vector< uint > &block_structure, const dealii::IndexSet &locally_owned, MPI_Comm comm)
Size a block vector: block 0 is the FE dofs, block 1 (if present) the extra variables.
Definition la_policy.hh:183