version 3.10-dev
nonlinear/newtonconvergencewriter.hh
Go to the documentation of this file.
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//
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>
20
21namespace Dumux {
22
30template <class SolutionVector, class ResidualVector>
32{
33 virtual ~ConvergenceWriterInterface() = default;
34
35 virtual void write(const SolutionVector &uLastIter, const ResidualVector &deltaU, const ResidualVector &residual) {}
36};
37
44template <class GridGeometry, class SolutionVector, class ResidualVector>
45class NewtonConvergenceWriter : public ConvergenceWriterInterface<SolutionVector, ResidualVector>
46{
47 using GridView = typename GridGeometry::GridView;
48 static constexpr auto numEq = SolutionVector::block_type::dimension;
49 using Scalar = typename SolutionVector::block_type::value_type;
50
51 static_assert(GridGeometry::discMethod != DiscretizationMethods::staggered,
52 "This convergence writer does not work for the staggered method, use the StaggeredNewtonConvergenceWriter instead");
53public:
59 NewtonConvergenceWriter(const GridGeometry& gridGeometry,
60 const std::string& name = "newton_convergence")
61 : gridGeometry_(gridGeometry)
62 , writer_(gridGeometry.gridView(), name, "", "")
63 {
64 resize();
65
66 if (GridGeometry::discMethod == DiscretizationMethods::box)
67 {
68 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
69 {
70 writer_.addVertexData(x_[eqIdx], "x_" + std::to_string(eqIdx));
71 writer_.addVertexData(delta_[eqIdx], "delta_" + std::to_string(eqIdx));
72 writer_.addVertexData(def_[eqIdx], "defect_" + std::to_string(eqIdx));
73 }
74 }
75 else
76 {
77 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
78 {
79 writer_.addCellData(x_[eqIdx], "x_" + std::to_string(eqIdx));
80 writer_.addCellData(delta_[eqIdx], "delta_" + std::to_string(eqIdx));
81 writer_.addCellData(def_[eqIdx], "defect_" + std::to_string(eqIdx));
82 }
83 }
84 }
85
87 void resize()
88 {
89 const auto numDofs = gridGeometry_.numDofs();
90
91 // resize the output fields
92 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
93 {
94 def_[eqIdx].resize(numDofs);
95 delta_[eqIdx].resize(numDofs);
96 x_[eqIdx].resize(numDofs);
97 }
98 }
99
102 void reset(std::size_t newId = 0UL)
103 { id_ = newId; iteration_ = 0UL; }
104
105 void write(const SolutionVector& uLastIter,
106 const ResidualVector& deltaU,
107 const ResidualVector& residual) override
108 {
109 assert(uLastIter.size() == deltaU.size() && uLastIter.size() == residual.size());
110
111 for (std::size_t dofIdxGlobal = 0; dofIdxGlobal < deltaU.size(); ++dofIdxGlobal)
112 {
113 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
114 {
115 x_[eqIdx][dofIdxGlobal] = uLastIter[dofIdxGlobal][eqIdx];
116 delta_[eqIdx][dofIdxGlobal] = - deltaU[dofIdxGlobal][eqIdx];
117 def_[eqIdx][dofIdxGlobal] = residual[dofIdxGlobal][eqIdx];
118 }
119 }
120
121 writer_.write(static_cast<double>(id_) + static_cast<double>(iteration_)/1000);
122 ++iteration_;
123 }
124
125private:
126 std::size_t id_ = 0UL;
127 std::size_t iteration_ = 0UL;
128
129 const GridGeometry& gridGeometry_;
130
131 Dune::VTKSequenceWriter<GridView> writer_;
132
133 std::array<std::vector<Scalar>, numEq> def_;
134 std::array<std::vector<Scalar>, numEq> delta_;
135 std::array<std::vector<Scalar>, numEq> x_;
136};
137
138} // end namespace Dumux
139
140#endif
Writes the intermediate solutions for every Newton iteration.
Definition: nonlinear/newtonconvergencewriter.hh:46
void resize()
Resizes the output fields. This has to be called whenever the grid changes.
Definition: nonlinear/newtonconvergencewriter.hh:87
void reset(std::size_t newId=0UL)
Definition: nonlinear/newtonconvergencewriter.hh:102
void write(const SolutionVector &uLastIter, const ResidualVector &deltaU, const ResidualVector &residual) override
Definition: nonlinear/newtonconvergencewriter.hh:105
NewtonConvergenceWriter(const GridGeometry &gridGeometry, const std::string &name="newton_convergence")
Constructor.
Definition: nonlinear/newtonconvergencewriter.hh:59
The available discretization methods in Dumux.
constexpr Box box
Definition: method.hh:147
constexpr Staggered staggered
Definition: method.hh:149
Definition: adapt.hh:17
A convergence writer interface Provide an interface that show the minimal requirements a convergence ...
Definition: nonlinear/newtonconvergencewriter.hh:32
virtual void write(const SolutionVector &uLastIter, const ResidualVector &deltaU, const ResidualVector &residual)
Definition: nonlinear/newtonconvergencewriter.hh:35
virtual ~ConvergenceWriterInterface()=default