DiFfRG
Loading...
Searching...
No Matches
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/multithread_info.h>
8#include <deal.II/base/quadrature_lib.h>
9#include <deal.II/base/timer.h>
10#include <deal.II/dofs/dof_handler.h>
11#include <deal.II/fe/fe_interface_values.h>
12#include <deal.II/fe/fe_values.h>
13#include <deal.II/lac/full_matrix.h>
14#include <deal.II/lac/sparse_matrix.h>
15#include <deal.II/lac/vector.h>
16#include <deal.II/meshworker/mesh_loop.h>
17#include <deal.II/numerics/fe_field_function.h>
18#include <deal.II/numerics/matrix_tools.h>
19#include <deal.II/numerics/vector_tools.h>
20
21// DiFfRG
26
27namespace DiFfRG
28{
29 namespace Variables
30 {
31 using namespace dealii;
32 using std::array;
33
34 template <typename... T> auto fe_tie(T &&...t)
35 {
36 return named_tuple<std::tuple<T &...>, "variables">(std::tie(t...));
37 }
38
44 template <typename Model_> class Assembler : public AbstractAssembler<Vector<double>, SparseMatrix<double>, 0>
45 {
46 constexpr static int nothing = 0;
47
48 public:
49 using Model = Model_;
50 using NumberType = double;
51 using VectorType = Vector<double>;
52
53 using Components = typename Model_::Components;
54 static constexpr uint dim = 0;
55
56 Assembler(Model &model, const JSONValue &json) : model(model), threads(json.get_uint("/discretization/threads"))
57 {
58 if (threads == 0) threads = dealii::MultithreadInfo::n_threads() / 2;
59 static_assert(Components::count_fe_functions() == 0, "The pure variable assembler cannot handle FE functions!");
60 reinit();
61 }
62
63 virtual void reinit_vector(VectorType &vec) const override { vec.reinit(0); }
64
65 virtual IndexSet get_differential_indices() const override { return IndexSet(); }
66
67 virtual void attach_data_output(DataOutput<dim, VectorType> &data_out, const VectorType &solution,
68 const VectorType &variables, const VectorType &dt_solution = VectorType(),
70 {
71 (void)dt_solution;
72 (void)residual;
73 readouts(data_out, solution, variables);
74 }
75
76 virtual void reinit() override {}
77
78 virtual void set_time(double t) override { model.set_time(t); }
79
80 virtual const SparsityPattern &get_sparsity_pattern_jacobian() const override
81 {
83 }
84 virtual const SparseMatrix<NumberType> &get_mass_matrix() const override { return mass_matrix; }
85
86 virtual void residual_variables(VectorType &residual, const VectorType &variables, const VectorType &) override
87 {
88 Timer timer;
89 model.dt_variables(residual, fe_tie(variables));
90 timings_residual.push_back(timer.wall_time());
91 };
92
93 virtual void jacobian_variables(FullMatrix<NumberType> &jacobian, const VectorType &variables,
94 const VectorType &) override
95 {
96 Timer timer;
97 model.template jacobian_variables<0>(jacobian, fe_tie(variables));
98 timings_jacobian.push_back(timer.wall_time());
99 };
100
101 void readouts(DataOutput<dim, VectorType> &data_out, const VectorType &, const VectorType &variables) const
102 {
103 auto helper = [&](auto EoMfun, auto outputter) {
104 (void)EoMfun;
105 outputter(data_out, Point<0>(), fe_tie(variables));
106 };
107 model.template readouts_multiple(helper, data_out);
108 }
109
110 virtual void mass(VectorType &, const VectorType &, const VectorType &, NumberType) override {}
111
112 virtual void residual(VectorType &, const VectorType &, NumberType, const VectorType &, NumberType,
113 const VectorType &variables = VectorType()) override
114 {
115 (void)variables;
116 }
117
118 virtual void jacobian_mass(SparseMatrix<NumberType> &, const VectorType &, const VectorType &, NumberType,
119 NumberType) override
120 {
121 }
122
123 virtual void jacobian(SparseMatrix<NumberType> &, const VectorType &, NumberType, const VectorType &, NumberType,
124 NumberType, const VectorType &variables = VectorType()) override
125 {
126 (void)variables;
127 }
128
129 void log(const std::string logger) const
130 {
131 std::stringstream ss;
132 ss << "Variable Assembler: " << std::endl;
133 ss << " Residual: " << average_time_residual_assembly() * 1000 << "ms (" << num_residuals() << ")"
134 << std::endl;
135 ss << " Jacobian: " << average_time_jacobian_assembly() * 1000 << "ms (" << num_jacobians() << ")"
136 << std::endl;
137 spdlog::get(logger)->info(ss.str());
138 }
139
141 {
142 double t = 0.;
143 double n = timings_residual.size();
144 for (const auto &t_ : timings_residual)
145 t += t_ / n;
146 return t;
147 }
148 uint num_residuals() const { return timings_residual.size(); }
149
151 {
152 double t = 0.;
153 double n = timings_jacobian.size();
154 for (const auto &t_ : timings_jacobian)
155 t += t_ / n;
156 return t;
157 }
158 uint num_jacobians() const { return timings_jacobian.size(); }
159
160 private:
162
164
165 SparsityPattern sparsity_pattern_mass;
167 SparseMatrix<NumberType> mass_matrix;
168
169 std::vector<double> timings_residual;
170 std::vector<double> timings_jacobian;
171 };
172 } // namespace Variables
173} // namespace DiFfRG
This is the general assembler interface for any kind of discretization. An assembler is responsible f...
Definition abstract_assembler.hh:39
Class to manage writing to files. FEM functions are written to vtk files and other data is written to...
Definition data_output.hh:20
A wrapper around the boost json value class.
Definition json.hh:19
The basic assembler that can be used for any standard CG scheme with flux and source.
Definition variables.hh:45
virtual void reinit() override
Reinitialize the assembler. This is necessary if the mesh has changed, e.g. after a mesh refinement.
Definition variables.hh:76
virtual void reinit_vector(VectorType &vec) const override
Reinitialize an arbitrary vector so that it has the correct size and structure.
Definition variables.hh:63
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:86
std::vector< double > timings_jacobian
Definition variables.hh:170
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:78
virtual void residual(VectorType &, const VectorType &, NumberType, const VectorType &, NumberType, const VectorType &variables=VectorType()) override
Definition variables.hh:112
virtual IndexSet get_differential_indices() const override
Obtain the dofs which contain time derivatives.
Definition variables.hh:65
void log(const std::string logger) const
Definition variables.hh:129
virtual void attach_data_output(DataOutput< dim, VectorType > &data_out, const VectorType &solution, const VectorType &variables, const VectorType &dt_solution=VectorType(), const VectorType &residual=VectorType())
Attach any data output to the DataOutput object provided. This can be used to extract additional data...
Definition variables.hh:67
uint threads
Definition variables.hh:163
Model & model
Definition variables.hh:161
SparsityPattern sparsity_pattern_mass
Definition variables.hh:165
static constexpr uint dim
Definition variables.hh:54
uint num_jacobians() const
Definition variables.hh:158
uint num_residuals() const
Definition variables.hh:148
void readouts(DataOutput< dim, VectorType > &data_out, const VectorType &, const VectorType &variables) const
Definition variables.hh:101
virtual void jacobian_mass(SparseMatrix< NumberType > &, const VectorType &, const VectorType &, NumberType, NumberType) override
Definition variables.hh:118
virtual const SparseMatrix< NumberType > & get_mass_matrix() const override
Obtain the mass matrix.
Definition variables.hh:84
typename Model_::Components Components
Definition variables.hh:53
SparsityPattern sparsity_pattern_jacobian
Definition variables.hh:166
Model_ Model
Definition variables.hh:49
double average_time_jacobian_assembly()
Definition variables.hh:150
std::vector< double > timings_residual
Definition variables.hh:169
Assembler(Model &model, const JSONValue &json)
Definition variables.hh:56
SparseMatrix< NumberType > mass_matrix
Definition variables.hh:167
virtual void jacobian(SparseMatrix< NumberType > &, const VectorType &, NumberType, const VectorType &, NumberType, NumberType, const VectorType &variables=VectorType()) override
Definition variables.hh:123
virtual void mass(VectorType &, const VectorType &, const VectorType &, NumberType) override
Definition variables.hh:110
virtual const SparsityPattern & get_sparsity_pattern_jacobian() const override
Obtain the sparsity pattern of the jacobian matrix.
Definition variables.hh:80
Vector< double > VectorType
Definition variables.hh:51
static constexpr int nothing
Definition variables.hh:46
double NumberType
Definition variables.hh:50
double average_time_residual_assembly()
Definition variables.hh:140
virtual void jacobian_variables(FullMatrix< NumberType > &jacobian, const VectorType &variables, const VectorType &) override
Definition variables.hh:93
auto fe_tie(T &&...t)
Definition variables.hh:34
Definition complex_math.hh:14
unsigned int uint
Definition utils.hh:22
A class to store a tuple with elements that can be accessed by name. The names are stored as FixedStr...
Definition tuples.hh:27