GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/nonlinear/newtonconvergencewriter.hh
Date: 2024-09-21 20:52:54
Exec Total Coverage
Lines: 30 33 90.9%
Functions: 12 46 26.1%
Branches: 59 136 43.4%

Line Branch Exec Source
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 //
4 // SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup Nonlinear
10 * \brief This class provides the infrastructure to write the
11 * convergence behaviour of the newton method into a VTK file.
12 */
13 #ifndef DUMUX_NEWTON_CONVERGENCE_WRITER_HH
14 #define DUMUX_NEWTON_CONVERGENCE_WRITER_HH
15
16 #include <string>
17
18 #include <dune/grid/io/file/vtk/vtksequencewriter.hh>
19 #include <dumux/discretization/method.hh>
20
21 namespace Dumux {
22
23 //! provide an interface as a form of type erasure
24 //! this is the minimal requirements a convergence write passed to a newton method has to fulfill
25 template <class SolutionVector, class ResidualVector>
26 struct ConvergenceWriterInterface
27 {
28 virtual ~ConvergenceWriterInterface() = default;
29
30 virtual void write(const SolutionVector &uLastIter, const ResidualVector &deltaU, const ResidualVector &residual) {}
31 };
32
33 /*!
34 * \ingroup Nonlinear
35 * \brief Writes the intermediate solutions for every Newton iteration
36 * \note To use this create a shared_ptr to an instance of this class in the main file
37 * and pass it to newton.solve(x, convergencewriter). You can use the reset method
38 * to write out multiple Newton solves with a unique id, if you don't call use all
39 * Newton iterations just come after each other in the pvd file.
40 */
41 template <class GridGeometry, class SolutionVector, class ResidualVector>
42 class NewtonConvergenceWriter : public ConvergenceWriterInterface<SolutionVector, ResidualVector>
43 {
44 using GridView = typename GridGeometry::GridView;
45 static constexpr auto numEq = SolutionVector::block_type::dimension;
46 using Scalar = typename SolutionVector::block_type::value_type;
47
48 static_assert(GridGeometry::discMethod != DiscretizationMethods::staggered,
49 "This convergence writer does not work for the staggered method, use the StaggeredNewtonConvergenceWriter instead");
50 public:
51 /*!
52 * \brief Constructor
53 * \param gridGeometry The finite-volume grid geometry
54 * \param name Base name of the vtk output
55 */
56 4 NewtonConvergenceWriter(const GridGeometry& gridGeometry,
57 const std::string& name = "newton_convergence")
58 : gridGeometry_(gridGeometry)
59
9/22
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 4 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 4 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✓ Branch 19 taken 4 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 4 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 4 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
16 , writer_(gridGeometry.gridView(), name, "", "")
60 {
61
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 resize();
62
63 if (GridGeometry::discMethod == DiscretizationMethods::box)
64 {
65
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
66 {
67
6/16
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 6 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
6 writer_.addVertexData(x_[eqIdx], "x_" + std::to_string(eqIdx));
68
6/16
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 6 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
6 writer_.addVertexData(delta_[eqIdx], "delta_" + std::to_string(eqIdx));
69
6/16
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 6 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
6 writer_.addVertexData(def_[eqIdx], "defect_" + std::to_string(eqIdx));
70 }
71 }
72 else
73 {
74
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
75 {
76
6/16
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 6 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
6 writer_.addCellData(x_[eqIdx], "x_" + std::to_string(eqIdx));
77
6/16
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 6 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
6 writer_.addCellData(delta_[eqIdx], "delta_" + std::to_string(eqIdx));
78
6/16
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 6 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 6 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
6 writer_.addCellData(def_[eqIdx], "defect_" + std::to_string(eqIdx));
79 }
80 }
81 4 }
82
83 //! Resizes the output fields. This has to be called whenever the grid changes
84 4 void resize()
85 {
86 4 const auto numDofs = gridGeometry_.numDofs();
87
88 // resize the output fields
89
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
90 {
91 24 def_[eqIdx].resize(numDofs);
92 24 delta_[eqIdx].resize(numDofs);
93 24 x_[eqIdx].resize(numDofs);
94 }
95 4 }
96
97 //! Reset the convergence writer for a possible next Newton step
98 //! You may set a different id in case you don't want the output to be overwritten by the next step
99 void reset(std::size_t newId = 0UL)
100
1/2
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
31 { id_ = newId; iteration_ = 0UL; }
101
102 135 void write(const SolutionVector& uLastIter,
103 const ResidualVector& deltaU,
104 const ResidualVector& residual) override
105 {
106
2/4
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 135 times.
✗ Branch 3 not taken.
135 assert(uLastIter.size() == deltaU.size() && uLastIter.size() == residual.size());
107
108
2/2
✓ Branch 0 taken 68329 times.
✓ Branch 1 taken 135 times.
68464 for (std::size_t dofIdxGlobal = 0; dofIdxGlobal < deltaU.size(); ++dofIdxGlobal)
109 {
110
2/2
✓ Branch 0 taken 204987 times.
✓ Branch 1 taken 68329 times.
273316 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
111 {
112 819948 x_[eqIdx][dofIdxGlobal] = uLastIter[dofIdxGlobal][eqIdx];
113 819948 delta_[eqIdx][dofIdxGlobal] = - deltaU[dofIdxGlobal][eqIdx];
114 1024935 def_[eqIdx][dofIdxGlobal] = residual[dofIdxGlobal][eqIdx];
115 }
116 }
117
118 135 writer_.write(static_cast<double>(id_) + static_cast<double>(iteration_)/1000);
119 135 ++iteration_;
120 135 }
121
122 private:
123 std::size_t id_ = 0UL;
124 std::size_t iteration_ = 0UL;
125
126 const GridGeometry& gridGeometry_;
127
128 Dune::VTKSequenceWriter<GridView> writer_;
129
130 std::array<std::vector<Scalar>, numEq> def_;
131 std::array<std::vector<Scalar>, numEq> delta_;
132 std::array<std::vector<Scalar>, numEq> x_;
133 };
134
135 } // end namespace Dumux
136
137 #endif
138