-
Notifications
You must be signed in to change notification settings - Fork 1
/
ode_dg.cc
295 lines (246 loc) · 9.04 KB
/
ode_dg.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* ---------------------------------------------------------------------
*
* Copyright (C) 1999 - 2021 by the deal.II authors
*
* This file is based on step-3 of the deal.II library.
*
* The deal.II library is free software; you can use it, redistribute
* it, and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* The full text of the license can be found in the file LICENSE.md at
* the top level directory of deal.II.
*
* ---------------------------------------------------------------------
*
* Authors: Jan Philipp Thiele, Julian Roth 2022
*
*/
//Most of these should be known if you ran the Poisson example (step-3)
#include <deal.II/grid/tria.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/grid/grid_generator.h>
// This file contains the description of the Lagrange interpolation
// discontinuous finite element:
#include <deal.II/fe/fe_dgq.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/function.h>
#include <deal.II/numerics/vector_tools.h>
#include <deal.II/numerics/matrix_tools.h>
#include <deal.II/lac/vector.h>
#include <deal.II/lac/full_matrix.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/sparse_direct.h>
#include <deal.II/lac/precondition.h>
#include <fstream>
#include <iostream>
using namespace dealii;
// This class is based on the main class of step-3, but here we want to model
// the ODE y' = lambda * y.
// The main differences lie in using discontinuous Galerkin (DG) elements,
// i.e. FE_DGQ and in including the ODE parameter lambda.
class ODE
{
public:
ODE(int degree = 1,double lambda = 5., double T = 1.);
void run();
private:
void make_grid();
void setup_system();
void assemble_system();
void solve();
void output_results() const;
Triangulation<1> triangulation;
FE_DGQ<1> fe;
DoFHandler<1> dof_handler;
SparsityPattern sparsity_pattern;
SparseMatrix<double> system_matrix;
Vector<double> solution;
Vector<double> system_rhs;
double lambda;
double T;
};
ODE::ODE(int degree, double lambda, double T)
: fe(degree)
, dof_handler(triangulation)
, lambda(lambda)
, T(T)
{}
void ODE::make_grid()
{
GridGenerator::hyper_cube(triangulation, 0, T);
triangulation.refine_global(2);
std::cout << "Number of active cells: " << triangulation.n_active_cells()
<< std::endl;
}
void ODE::setup_system()
{
dof_handler.distribute_dofs(fe);
std::cout << "Number of degrees of freedom: " << dof_handler.n_dofs()
<< std::endl;
// generate the dynamic sparsity pattern as usual
DynamicSparsityPattern dsp(dof_handler.n_dofs());
DoFTools::make_sparsity_pattern(dof_handler, dsp);
// to account for the jump terms we need to add the index to the sparsity pattern
for (types::global_dof_index i = 1; i< dof_handler.n_dofs() ; i++ )
{
dsp.add(i,i-1);
}
sparsity_pattern.copy_from(dsp);
system_matrix.reinit(sparsity_pattern);
solution.reinit(dof_handler.n_dofs());
system_rhs.reinit(dof_handler.n_dofs());
}
void ODE::assemble_system()
{
QGauss<1> quadrature_formula(fe.degree + 2);
FEValues<1> fe_values(fe,
quadrature_formula,
update_values | update_gradients | update_JxW_values);
const unsigned int dofs_per_cell = fe.n_dofs_per_cell();
FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
std::vector<types::global_dof_index> neighbor_dof_indices(dofs_per_cell);
// As we have an ODE shape_grad does not coincide with the
// nabla Operator which usually only contains spatial derivatives
// But as our single dimension is time the shape_grad contains the
// temporal derivative
for (const auto &cell : dof_handler.active_cell_iterators())
{
fe_values.reinit(cell);
cell_matrix = 0;
for (const unsigned int q_index : fe_values.quadrature_point_indices())
{
for (const unsigned int i : fe_values.dof_indices())
for (const unsigned int j : fe_values.dof_indices())
{
// (y',phi)
cell_matrix(i, j) +=
(fe_values.shape_value(i, q_index) * // phi_i(x_q)
fe_values.shape_grad(j, q_index)[0] * // dt phi_j(x_q)
fe_values.JxW(q_index));// dx
// -(lambda*y,phi)
cell_matrix(i,j) -=
(lambda*
fe_values.shape_value(i,q_index) *
fe_values.shape_value(j,q_index) *
fe_values.JxW(q_index));
}
}
cell->get_dof_indices(local_dof_indices);
for (const unsigned int i : fe_values.dof_indices())
for (const unsigned int j : fe_values.dof_indices())
system_matrix.add(local_dof_indices[i],
local_dof_indices[j],
cell_matrix(i, j));
// Additionally we need the jump terms [y]_{m-1}*phi^+(t_m)
// which evaluate to 1*y_{m-1}^+ in the row of phi_m^+
system_matrix.add(local_dof_indices[0],
local_dof_indices[0],
1.0);
// and either
if (cell->active_cell_index() == 0)
{
//y0 = 1 in the rhs for the initial edge at t=0
system_rhs[0]=1.;
}
else {
//or -1*y_{m-1}^- for all other element edges with t>0
//apart from the final edge
std::prev(cell)->get_dof_indices(neighbor_dof_indices);
system_matrix.add(local_dof_indices[0],
neighbor_dof_indices[dofs_per_cell-1],
-1.0);
}
}
}
// As our Matrix is no longer symmetrical we cannot use the CG solver
// the easiest approach is using a direct solver which for a single dimension
// is still viable in terms of computational and memory costs
void ODE::solve()
{
SparseDirectUMFPACK solver;
solver.initialize(system_matrix);
solver.vmult(solution, system_rhs);
}
// As the normal output basically interpolates the solution
// back to a linear finite element, we cannot properly view
// the solution for a higher polynomial degree.
// For PDEs in more dimension this is mostly fine, but for an ODE
// it obstructs the shape of the solution.
// For this reason the solution is evaluated at multiple inner points
// as well as on the boundary of each element.
// The timepoints and matching solutions are then written to the console
// in a matlab compatible format and can be plotted using plot(t,u)
void ODE::output_results() const
{
unsigned int n_points_per_cell = 11;
if ( fe.get_degree() < 2)
{
n_points_per_cell = 2; //for constant and linear only use 2 points.
}
//Iterated formula splits element into (n_points_per_cell-1) subintervals on which the trapezoidal rule is applied.
//This results in n_points_per_cell uniformly distributed quadrature points
QIterated<1> quad(QTrapez<1>(), n_points_per_cell-1); // need to use QTrapezoid for deal.II 9.3.0
FEValues<1> fe_values(fe, quad, update_values | update_quadrature_points);
std::vector< std::vector< Point<1> > > q_points(triangulation.n_active_cells(), std::vector< Point<1> >(n_points_per_cell, Point<1>(0.)));
std::vector< std::vector<double> > u_values(triangulation.n_active_cells(), std::vector<double>(n_points_per_cell));
//Get all "quadrature"-points and corresponding solution values.
for (const auto &cell : dof_handler.active_cell_iterators())
{
fe_values.reinit(cell);
fe_values.get_function_values(solution,u_values[cell->index()]);
q_points[cell->index()] = fe_values.get_quadrature_points();
}
//Output points as a Matlab matrix
std::cout << "t = [";
for (const auto &cell : dof_handler.active_cell_iterators())
{
unsigned int i = 0;
for ( ; i < n_points_per_cell-1 ; i++ )
{
std::cout << q_points[cell->index()][i] << ", ";
}
std::cout << q_points[cell->index()][i];
if (std::next(cell) != dof_handler.end()) {
std::cout << "; ";
}
}
std::cout << "];" << std::endl;
//Output corresponding solutions as a Matlab matrix
std::cout << "u = [";
for (const auto &cell : dof_handler.active_cell_iterators())
{
unsigned int i = 0;
for ( ; i < n_points_per_cell-1 ; i++ )
{
std::cout << u_values[cell->index()][i] << ", ";
}
std::cout << u_values[cell->index()][i];
if (std::next(cell) != dof_handler.end()) {
std::cout << "; ";
}
}
std::cout << "];" << std::endl;
}
void ODE::run()
{
make_grid();
setup_system();
assemble_system();
solve();
output_results();
}
int main()
{
deallog.depth_console(2);
int fe_degree = 1;
double lambda = 5.0;
double T = 1.0;
ODE laplace_problem(fe_degree,lambda,T);
laplace_problem.run();
return 0;
}