Debian Codegraph @

include/deal.II/numerics/dof_print_solver_step.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2000 - 2023 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15 #ifndef dealii_dof_print_solver_step_h
16 #define dealii_dof_print_solver_step_h
17
18 #include <deal.II/base/config.h>
19
20 #include <deal.II/base/logstream.h>
21
22 #include <deal.II/lac/solver_control.h>
23 #include <deal.II/lac/vector_memory.h>
24
25 #include <deal.II/numerics/data_out.h>
26
27 #include <fstream>
28 #include <iomanip>
29 #include <sstream>
30
31 DEAL_II_NAMESPACE_OPEN
32
33
34 /**
35 * Print intermediate solutions in solvers. This is derived from a solver
36 * class provided as template argument. It implements the @p print_vector
37 * function of the solver using a DoFHandler. This way, the intermediate
38 * vectors can be viewed as finite element functions. This class might be used
39 * first to understand how solvers work (for example to visualize the
40 * smoothing properties of various solvers, e.g. in a multigrid context), and
41 * second to investigate why and how a solver fails to solve certain classes
42 * of problems.
43 *
44 * Objects of this class are provided with a solver class through a template
45 * argument, and with a file name (as a string), with which a new file is
46 * constructed in each iteration (named <tt>basename.[step].[suffix]</tt>) and
47 * into which the solution is written as a finite element field using the
48 * DataOut class. Please note that this class may produce enormous amounts of
49 * data!
50 *
51 * @ingroup output
52 */
53 template <int dim, typename SolverType, typename VectorType = Vector<double>>
54 class DoFPrintSolverStep : public SolverType
55 {
56 public:
57 /**
58 * Constructor. First, we take the arguments needed for the solver. @p
59 * data_out is the object doing the output as a finite element function.
60 *
61 * One output file with the name <tt>basename.[step].[suffix]</tt> will be
62 * produced for each iteration step.
63 */
64 DoFPrintSolverStep(SolverControl &control,
65 VectorMemory<VectorType> &mem,
66 DataOut<dim> &data_out,
67 const std::string &basename);
68
69 /**
70 * Call-back function for the iterative method.
71 */
72 virtual void
73 print_vectors(const unsigned int step,
74 const VectorType &x,
75 const VectorType &r,
76 const VectorType &d) const;
77
78 private:
79 /**
80 * Output object.
81 */
82 DataOut<dim> &out;
83
84 /**
85 * Base of filenames.
86 */
87 const std::string basename;
88 };
89
90
91 /* ----------------------- template functions --------------- */
92
93 template <int dim, typename SolverType, typename VectorType>
94 DoFPrintSolverStep<dim, SolverType, VectorType>::DoFPrintSolverStep(
95 SolverControl &control,
96 VectorMemory<VectorType> &mem,
97 DataOut<dim> &data_out,
98 const std::string &basename)
99 : SolverType(control, mem)
100 , out(data_out)
101 , basename(basename)
102 {}
103
104
105 template <int dim, typename SolverType, typename VectorType>
106 void
107 DoFPrintSolverStep<dim, SolverType, VectorType>::print_vectors(
108 const unsigned int step,
109 const VectorType &x,
110 const VectorType &r,
111 const VectorType &d) const
112 {
113 out.clear_data_vectors();
114 out.add_data_vector(x, "solution");
115 out.add_data_vector(r, "residual");
116 out.add_data_vector(d, "update");
117
118 std::ostringstream filename;
119 filename << basename << std::setw(3) << std::setfill('0') << step
120 << out.default_suffix();
121
122 const std::string fname = filename.str();
123
124 deallog << "Writing file:" << fname << std::endl;
125
126 out.build_patches();
127 std::ofstream of(fname);
128 out.write(of);
129 }
130
131 DEAL_II_NAMESPACE_CLOSE
132
133 #endif