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

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/DiFfRG/discretization/variables/assembler/variables.hh Source File
DiFfRG
Discretization Framework for functional Renormalization Group flows
variables.hh
Go to the documentation of this file.
1#pragma once
2
3// standard library
4#include <sstream>
5
6// external libraries
7#include <deal.II/base/timer.h>
8#include <deal.II/dofs/dof_handler.h>
9#include <deal.II/lac/full_matrix.h>
10#include <deal.II/lac/sparse_matrix.h>
11#include <deal.II/lac/vector.h>
12
13// DiFfRG
18
19namespace DiFfRG
20{
21 namespace Variables
22 {
23 using namespace dealii;
24
25 template <typename... T> auto fe_tie(T &&...t)
26 {
27 return named_tuple<std::tuple<T &...>, StringSet<"variables">>(std::tie(t...));
28 }
29
35 template <typename Model_> class Assembler : public AbstractAssembler<Vector<double>, SparseMatrix<double>, 0>
36 {
37 constexpr static int nothing = 0;
38
39 public:
40 using Model = Model_;
41 using NumberType = double;
42 using VectorType = Vector<double>;
43 using SparseMatrixType = SparseMatrix<double>;
44
45 using Components = typename Model_::Components;
46 static constexpr uint dim = 0;
47
50 Assembler(Model &model, const ConfigTree & /*config*/)
51 : model(model)
52 {
53 static_assert(Components::count_fe_functions() == 0, "The pure variable assembler cannot handle FE functions!");
54 reinit();
55 }
56
57 virtual void reinit_vector(VectorType &vec) const override { vec.reinit(0); }
58 // Serial by construction: this assembler has no FE space at all (dim == 0) and hard-codes
59 // dealii::Vector/SparseMatrix in its base-class list, so there is no distributed policy here.
60 virtual void reinit_matrix(SparseMatrixType &matrix) const override
61 {
62 matrix.reinit(get_sparsity_pattern_jacobian());
63 }
64 virtual MPI_Comm get_communicator() const override { return MPI_COMM_SELF; }
65 // dim == 0: no FE space, and the vector type is serial by construction, so the view is a
66 // passthrough and the layout arguments are ignored.
67 virtual void reinit_solution_view(SolutionView<VectorType> &view) const override
68 {
69 view.reinit(dealii::IndexSet(), dealii::IndexSet(), MPI_COMM_SELF);
70 }
71
72 virtual IndexSet get_differential_indices() const override { return IndexSet(); }
73
74 virtual void attach_data_output(OutputFrame<dim, VectorType> &data_out, const VectorType &solution,
75 const VectorType &variables, const VectorType &dt_solution = VectorType(),
76 const VectorType &residual = VectorType()) override
77 {
78 (void)dt_solution;
79 (void)residual;
80 readouts(data_out, solution, variables);
81 }
82
83 virtual void reinit() override {}
84
85 virtual void set_time(double t) override { model.set_time(t); }
86
87 virtual const SparsityPattern &get_sparsity_pattern_jacobian() const override
88 {
90 }
91 virtual const SparseMatrix<NumberType> &get_mass_matrix() const override { return mass_matrix; }
92
93 virtual void residual_variables(VectorType &residual, const VectorType &variables, const VectorType &) override
94 {
95 Timer timer;
96 model.dt_variables(residual, fe_tie(variables));
97 // Fences AND lands any map() results still sitting in pinned staging -- see MapCompletion.
98 flush_maps();
99 timings_residual.push_back(timer.wall_time());
100 };
101
102 virtual void jacobian_variables(FullMatrix<NumberType> &jacobian, const VectorType &variables,
103 const VectorType &) override
104 {
105 Timer timer;
106 model.template jacobian_variables<0>(jacobian, fe_tie(variables));
107 flush_maps();
108 timings_jacobian.push_back(timer.wall_time());
109 };
110
111 void readouts(OutputFrame<dim, VectorType> &data_out, const VectorType &, const VectorType &variables) const
112 {
113 auto helper = [&](auto &&...args) {
114 if constexpr (sizeof...(args) == 3) {
115 auto &&[id, EoMfun, outputter] = std::forward_as_tuple(std::forward<decltype(args)>(args)...);
116 data_out.register_readout(id);
117 (void)EoMfun;
118 outputter(data_out, Point<0>(), fe_tie(variables));
119 } else {
121 }
122 };
123 model.readouts_multiple(helper, data_out);
124 }
125
126 virtual void mass(VectorType &, const VectorType &, const VectorType &, NumberType) override {}
127
128 virtual void residual(VectorType &, const VectorType &, NumberType, const VectorType &, NumberType,
129 const VectorType &variables = VectorType()) override
130 {
131 (void)variables;
132 }
133
134 virtual void jacobian_mass(SparseMatrix<NumberType> &, const VectorType &, const VectorType &, NumberType,
135 NumberType) override
136 {
137 }
138
139 virtual void jacobian(SparseMatrix<NumberType> &, const VectorType &, NumberType, const VectorType &, NumberType,
140 NumberType, const VectorType &variables = VectorType()) override
141 {
142 (void)variables;
143 }
144 SummaryEvent summary() const override
145 {
146 SummaryEvent result{.component = "variables"};
147 result.timing("residual", average_time_residual_assembly() * 1000, num_residuals())
149 return result;
150 }
151
153 {
154 double t = 0.;
155 double n = timings_residual.size();
156 for (const auto &t_ : timings_residual)
157 t += t_ / n;
158 return t;
159 }
160 uint num_residuals() const { return timings_residual.size(); }
161
163 {
164 double t = 0.;
165 double n = timings_jacobian.size();
166 for (const auto &t_ : timings_jacobian)
167 t += t_ / n;
168 return t;
169 }
170 uint num_jacobians() const { return timings_jacobian.size(); }
171
172 private:
174
175 SparsityPattern sparsity_pattern_mass;
177 SparseMatrix<NumberType> mass_matrix;
178
179 std::vector<double> timings_residual;
180 std::vector<double> timings_jacobian;
181 };
182 } // namespace Variables
183} // namespace DiFfRG
This is the general assembler interface for any kind of discretization. An assembler is responsible f...
Definition abstract_assembler.hh:54
A hierarchical configuration tree, readable from JSON and TOML files.
Definition config_tree.hh:32
Definition output_session.hh:42
void register_readout(const std::string &id)
Definition output_session.hh:154
A read-only, fully-replicated view of the solution.
Definition solution_view.hh:48
void reinit(const dealii::IndexSet &, const dealii::IndexSet &, MPI_Comm)
Establish the layout. No-op for the serial policy.
Definition solution_view.hh:58
The basic assembler that can be used for any standard CG scheme with flux and source.
Definition variables.hh:36
virtual void reinit() override
Reinitialize the assembler. This is necessary if the mesh has changed, e.g. after a mesh refinement.
Definition variables.hh:83
virtual void reinit_vector(VectorType &vec) const override
Reinitialize an arbitrary vector so that it has the correct size and structure.
Definition variables.hh:57
virtual void residual_variables(VectorType &residual, const VectorType &variables, const VectorType &) override
When coupling the spatial discretization to additional variables, this function should calculate the ...
Definition variables.hh:93
std::vector< double > timings_jacobian
Definition variables.hh:180
virtual void attach_data_output(OutputFrame< dim, VectorType > &data_out, const VectorType &solution, const VectorType &variables, const VectorType &dt_solution=VectorType(), const VectorType &residual=VectorType()) override
Contribute the assembler's fields and model readouts to one scoped output frame.
Definition variables.hh:74
virtual void set_time(double t) override
Set the current time. The assembler should usually just forward this to the numerical model.
Definition variables.hh:85
virtual void residual(VectorType &, const VectorType &, NumberType, const VectorType &, NumberType, const VectorType &variables=VectorType()) override
Definition variables.hh:128
virtual IndexSet get_differential_indices() const override
Obtain the dofs which contain time derivatives.
Definition variables.hh:72
virtual void reinit_solution_view(SolutionView< VectorType > &view) const override
Establish the layout of a fully-replicated read-only view of the solution.
Definition variables.hh:67
virtual MPI_Comm get_communicator() const override
The communicator this assembler's linear algebra lives on.
Definition variables.hh:64
double average_time_jacobian_assembly() const
Definition variables.hh:162
Model & model
Definition variables.hh:173
SparsityPattern sparsity_pattern_mass
Definition variables.hh:175
static constexpr uint dim
Definition variables.hh:46
virtual void reinit_matrix(SparseMatrixType &matrix) const override
Reinitialize a matrix to the jacobian's sparsity pattern.
Definition variables.hh:60
uint num_jacobians() const
Definition variables.hh:170
uint num_residuals() const
Definition variables.hh:160
virtual void jacobian_mass(SparseMatrix< NumberType > &, const VectorType &, const VectorType &, NumberType, NumberType) override
Definition variables.hh:134
virtual const SparseMatrix< NumberType > & get_mass_matrix() const override
Obtain the mass matrix.
Definition variables.hh:91
Assembler(Model &model, const ConfigTree &)
Definition variables.hh:50
SummaryEvent summary() const override
Definition variables.hh:144
typename Model_::Components Components
Definition variables.hh:45
SparsityPattern sparsity_pattern_jacobian
Definition variables.hh:176
Model_ Model
Definition variables.hh:40
std::vector< double > timings_residual
Definition variables.hh:179
void readouts(OutputFrame< dim, VectorType > &data_out, const VectorType &, const VectorType &variables) const
Definition variables.hh:111
SparseMatrix< NumberType > mass_matrix
Definition variables.hh:177
virtual void jacobian(SparseMatrix< NumberType > &, const VectorType &, NumberType, const VectorType &, NumberType, NumberType, const VectorType &variables=VectorType()) override
Definition variables.hh:139
virtual void mass(VectorType &, const VectorType &, const VectorType &, NumberType) override
Definition variables.hh:126
SparseMatrix< double > SparseMatrixType
Definition variables.hh:43
virtual const SparsityPattern & get_sparsity_pattern_jacobian() const override
Obtain the sparsity pattern of the jacobian matrix.
Definition variables.hh:87
Vector< double > VectorType
Definition variables.hh:42
static constexpr int nothing
Definition variables.hh:37
double NumberType
Definition variables.hh:41
double average_time_residual_assembly() const
Definition variables.hh:152
virtual void jacobian_variables(FullMatrix< NumberType > &jacobian, const VectorType &variables, const VectorType &) override
Definition variables.hh:102
auto fe_tie(T &&...t)
Definition variables.hh:25
constexpr void validate_readout_helper_arity()
Definition abstract_assembler.hh:17
Definition complex_math.hh:10
void flush_maps()
Land all outstanding map() results. See MapCompletion.
Definition map_completion.hh:190
unsigned int uint
Definition utils.hh:24
Definition tuples.hh:34
Definition run_reporter.hh:73
SummaryEvent & timing(const std::string_view name, const double average_ms, const std::size_t calls)
Definition run_reporter.hh:78
A class to store a tuple with elements that can be accessed by name. The names are stored as FixedStr...
Definition tuples.hh:56