From f0fe3915a72d61337b8d5b1545c69f3f4c0c0094 Mon Sep 17 00:00:00 2001
From: Kilian Weishaupt <kilian.weishaupt@iws.uni-stuttgart.de>
Date: Thu, 2 Feb 2017 15:23:16 +0100
Subject: [PATCH] [staggeredGrid] Add property for convergenceWriter

* Current one is not compatible with staggerdGrid
---
 dumux/implicit/properties.hh                  |   1 +
 dumux/implicit/propertydefaults.hh            |   3 +
 dumux/implicit/staggered/newtoncontroller.hh  |   2 +
 .../staggered/newtonconvergencewriter.hh      | 140 ++++++++++++++++++
 dumux/implicit/staggered/propertydefaults.hh  |   4 +
 dumux/nonlinear/newtoncontroller.hh           |   2 +-
 6 files changed, 151 insertions(+), 1 deletion(-)
 create mode 100644 dumux/implicit/staggered/newtonconvergencewriter.hh

diff --git a/dumux/implicit/properties.hh b/dumux/implicit/properties.hh
index ddb6ce0acf..df725d2781 100644
--- a/dumux/implicit/properties.hh
+++ b/dumux/implicit/properties.hh
@@ -111,6 +111,7 @@ NEW_PROP_TAG(VtkAddProcessRank); //!< specifies if the process rank should be ad
 NEW_PROP_TAG(TimeManager);  //!< Manages the simulation time
 NEW_PROP_TAG(NewtonMethod);     //!< The type of the newton method
 NEW_PROP_TAG(NewtonController); //!< The type of the newton controller
+NEW_PROP_TAG(NewtonConvergenceWriter); //!< The type of the newton convergence writer
 
 //! Specify whether the jacobian matrix of the last iteration of a
 //! time step should be re-used as the jacobian of the first iteration
diff --git a/dumux/implicit/propertydefaults.hh b/dumux/implicit/propertydefaults.hh
index 318406d45b..adb63c701e 100644
--- a/dumux/implicit/propertydefaults.hh
+++ b/dumux/implicit/propertydefaults.hh
@@ -32,6 +32,7 @@
 
 #include <dumux/nonlinear/newtonmethod.hh>
 #include <dumux/nonlinear/newtoncontroller.hh>
+#include <dumux/nonlinear/newtonconvergencewriter.hh>
 #include <dumux/common/boundarytypes.hh>
 #include <dumux/common/timemanager.hh>
 #include <dumux/linear/amgbackend.hh>
@@ -80,6 +81,8 @@ SET_TYPE_PROP(ImplicitBase, NewtonMethod, NewtonMethod<TypeTag>);
 //! use the plain newton controller by default
 SET_TYPE_PROP(ImplicitBase, NewtonController, NewtonController<TypeTag>);
 
+//! use the plain newton convergence writer by default
+SET_TYPE_PROP(ImplicitBase, NewtonConvergenceWriter, NewtonConvergenceWriter<TypeTag>);
 //! Mapper for the grid view's vertices.
 SET_TYPE_PROP(ImplicitBase,
               VertexMapper,
diff --git a/dumux/implicit/staggered/newtoncontroller.hh b/dumux/implicit/staggered/newtoncontroller.hh
index ba4031ba27..3425808ecb 100644
--- a/dumux/implicit/staggered/newtoncontroller.hh
+++ b/dumux/implicit/staggered/newtoncontroller.hh
@@ -30,6 +30,7 @@
 #include "properties.hh"
 
 #include <dumux/nonlinear/newtoncontroller.hh>
+#include "newtonconvergencewriter.hh"
 
 namespace Dumux {
 /*!
@@ -44,6 +45,7 @@ template <class TypeTag>
 class StaggeredNewtonController : public NewtonController<TypeTag>
 {
     typedef NewtonController<TypeTag> ParentType;
+    typedef NewtonConvergenceWriter<TypeTag> StaggeredNewtonConvergenceWriter;
     typedef typename GET_PROP_TYPE(TypeTag, Problem) Problem;
     typedef typename GET_PROP_TYPE(TypeTag, SolutionVector) SolutionVector;
     typedef typename GET_PROP_TYPE(TypeTag, JacobianMatrix) JacobianMatrix;
diff --git a/dumux/implicit/staggered/newtonconvergencewriter.hh b/dumux/implicit/staggered/newtonconvergencewriter.hh
new file mode 100644
index 0000000000..cd4700a331
--- /dev/null
+++ b/dumux/implicit/staggered/newtonconvergencewriter.hh
@@ -0,0 +1,140 @@
+// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+// vi: set et ts=4 sw=4 sts=4:
+/*****************************************************************************
+ *   See the file COPYING for full copying permissions.                      *
+ *                                                                           *
+ *   This program is free software: you can redistribute it and/or modify    *
+ *   it under the terms of the GNU General Public License as published by    *
+ *   the Free Software Foundation, either version 2 of the License, or       *
+ *   (at your option) any later version.                                     *
+ *                                                                           *
+ *   This program is distributed in the hope that it will be useful,         *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
+ *   GNU General Public License for more details.                            *
+ *                                                                           *
+ *   You should have received a copy of the GNU General Public License       *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
+ *****************************************************************************/
+/*!
+ * \file
+ *
+ * \brief This class provides the infrastructure to write the
+ *        convergence behaviour of the newton method into a VTK file.
+ */
+#ifndef DUMUX_STAGGERED_NEWTON_CONVERGENCE_WRITER_HH
+#define DUMUX_STAGGERED_NEWTON_CONVERGENCE_WRITER_HH
+
+#include <dune/grid/io/file/vtk/vtksequencewriter.hh>
+
+#include <dumux/common/basicproperties.hh>
+
+#include "newtoncontroller.hh"
+
+namespace Dumux
+{
+
+namespace Properties
+{
+NEW_PROP_TAG(NewtonController);
+NEW_PROP_TAG(SolutionVector);
+}
+
+/*!
+ * \ingroup Newton
+ * \brief Writes the intermediate solutions during
+ *        the Newton scheme
+ */
+template <class TypeTag>
+class StaggeredNewtonConvergenceWriter
+{
+    using Scalar = typename GET_PROP_TYPE(TypeTag, Scalar);
+    using GridView = typename GET_PROP_TYPE(TypeTag, GridView);
+    using NewtonController = typename GET_PROP_TYPE(TypeTag, NewtonController);
+    using SolutionVector = typename GET_PROP_TYPE(TypeTag, SolutionVector);
+
+    static constexpr int numEq = GET_PROP_VALUE(TypeTag, NumEq);
+    static constexpr bool isBox = GET_PROP_VALUE(TypeTag, ImplicitIsBox);
+
+public:
+    StaggeredNewtonConvergenceWriter(NewtonController &ctl, const GridView& gridView)
+    : ctl_(ctl),
+      writer_(gridView, "convergence", "", "")
+    {
+        DUNE_THROW(Dune::NotImplemented, "StaggeredNewtonConvergenceWriter not implemented! Do it yourself!");
+//         timeStepIndex_ = 0;
+//         iteration_ = 0;
+//
+//         const auto numDofs = ctl_.method().model().numDofs();
+//         for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
+//         {
+//             def_[eqIdx].resize(numDofs);
+//             delta_[eqIdx].resize(numDofs);
+//             x_[eqIdx].resize(numDofs);
+//         }
+//
+//         if (isBox)
+//         {
+//             for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
+//             {
+//                 writer_.addVertexData(x_[eqIdx], "x_" + std::to_string(eqIdx));
+//                 writer_.addVertexData(delta_[eqIdx], "delta_" + std::to_string(eqIdx));
+//                 writer_.addVertexData(def_[eqIdx], "defect_" + std::to_string(eqIdx));
+//             }
+//         }
+//         else
+//         {
+//             for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
+//             {
+//                 writer_.addCellData(x_[eqIdx], "x_" + std::to_string(eqIdx));
+//                 writer_.addCellData(delta_[eqIdx], "delta_" + std::to_string(eqIdx));
+//                 writer_.addCellData(def_[eqIdx], "defect_" + std::to_string(eqIdx));
+//             }
+//         }
+    }
+
+    void advanceTimeStep()
+    {
+        ++timeStepIndex_;
+        iteration_ = 0;
+    }
+
+    void advanceIteration()
+    {
+        ++iteration_;
+    }
+
+    void write(const SolutionVector &uLastIter,
+               const SolutionVector &deltaU)
+    {
+//         SolutionVector residual(uLastIter);
+//         ctl_.method().model().globalResidual(residual, uLastIter);
+//
+//         for (unsigned int dofIdxGlobal = 0; dofIdxGlobal < deltaU.size(); dofIdxGlobal++)
+//         {
+// //             for (int eqIdx = 0; eqIdx < numEq; ++eqIdx)
+// //             {
+// //                 x_[eqIdx][dofIdxGlobal] = uLastIter[dofIdxGlobal][eqIdx];
+// //                 delta_[eqIdx][dofIdxGlobal] = - deltaU[dofIdxGlobal][eqIdx];
+// //                 def_[eqIdx][dofIdxGlobal] = residual[dofIdxGlobal][eqIdx];
+// //             }
+//         }
+//
+//         writer_.write(timeStepIndex_ + iteration_ / 100.0);
+    }
+
+private:
+    int timeStepIndex_;
+    int iteration_;
+
+    std::array<std::vector<Scalar>, numEq> def_;
+    std::array<std::vector<Scalar>, numEq> delta_;
+    std::array<std::vector<Scalar>, numEq> x_;
+
+    NewtonController &ctl_;
+    Dune::VTKSequenceWriter<GridView> writer_;
+};
+
+} // namespace Dumux
+
+#endif
diff --git a/dumux/implicit/staggered/propertydefaults.hh b/dumux/implicit/staggered/propertydefaults.hh
index 2bbca38ded..84953f578f 100644
--- a/dumux/implicit/staggered/propertydefaults.hh
+++ b/dumux/implicit/staggered/propertydefaults.hh
@@ -56,6 +56,7 @@
 #include "localjacobian.hh"
 #include "properties.hh"
 #include "newtoncontroller.hh"
+#include "newtonconvergencewriter.hh"
 #include "model.hh"
 #include "primaryvariables.hh"
 
@@ -238,6 +239,9 @@ public:
     using type = StaggeredPrimaryVariables<TypeTag, CellCenterPrimaryVariables, FacePrimaryVariables>;
 };
 
+//! use the plain newton convergence writer by default
+SET_TYPE_PROP(StaggeredModel, NewtonConvergenceWriter, StaggeredNewtonConvergenceWriter<TypeTag>);
+
 } // namespace Properties
 
 } // namespace Dumux
diff --git a/dumux/nonlinear/newtoncontroller.hh b/dumux/nonlinear/newtoncontroller.hh
index bba5964629..4d74c38eb7 100644
--- a/dumux/nonlinear/newtoncontroller.hh
+++ b/dumux/nonlinear/newtoncontroller.hh
@@ -156,7 +156,7 @@ class NewtonController
 
     typedef typename GET_PROP_TYPE(TypeTag, SolutionVector) SolutionVector;
 
-    typedef NewtonConvergenceWriter<TypeTag> ConvergenceWriter;
+    typedef typename GET_PROP_TYPE(TypeTag, NewtonConvergenceWriter) ConvergenceWriter;
 
     typedef typename GET_PROP_TYPE(TypeTag, LinearSolver) LinearSolver;
 
-- 
GitLab