From a7d719d500af313e1aa3af6ac5e050d5961e98aa Mon Sep 17 00:00:00 2001
From: Timo Koch <timo.koch@iws.uni-stuttgart.de>
Date: Fri, 10 Nov 2017 15:06:57 +0100
Subject: [PATCH] [newton] Remove TypeTag dependency of NewtonMethod class

Adapt all tests accordingly. Classes that don't need a TypeTag and
only a few template arguments are more flexible.
---
 dumux/common/start/instationarynonlinear.hh   |  4 +--
 dumux/linear/amgbackend.hh                    |  1 -
 dumux/linear/solver.hh                        |  4 ++-
 dumux/nonlinear/newtonmethod.hh               | 34 ++++++-------------
 .../1p/implicit/compressible/test_1p.cc       |  2 +-
 .../compressible/test_1p_stationary.cc        |  2 +-
 .../2p/implicit/incompressible/test_2p_box.cc |  2 +-
 .../2p/implicit/incompressible/test_2p_cc.cc  |  2 +-
 .../2pnc/implicit/test_box2pnc.cc             |  2 +-
 .../2pnc/implicit/test_cc2pnc.cc              |  2 +-
 .../3p/implicit/test_box3p.cc                 |  2 +-
 .../3p/implicit/test_box3pniconduction.cc     |  2 +-
 .../3p/implicit/test_box3pniconvection.cc     |  2 +-
 .../porousmediumflow/3p/implicit/test_cc3p.cc |  2 +-
 .../3p/implicit/test_cc3pniconduction.cc      |  2 +-
 .../3p/implicit/test_cc3pniconvection.cc      |  2 +-
 16 files changed, 27 insertions(+), 40 deletions(-)

diff --git a/dumux/common/start/instationarynonlinear.hh b/dumux/common/start/instationarynonlinear.hh
index 9c69a1e240..8811872d64 100644
--- a/dumux/common/start/instationarynonlinear.hh
+++ b/dumux/common/start/instationarynonlinear.hh
@@ -152,7 +152,7 @@ struct InstationaryNonLinearSimulationImpl<TypeTag, DiscretizationMethods::CCTpf
 
         // the non-linear solver
         using NewtonController = typename GET_PROP_TYPE(TypeTag, NewtonController);
-        using NewtonMethod = Dumux::NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+        using NewtonMethod = Dumux::NewtonMethod<NewtonController, Assembler, LinearSolver>;
         auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
         NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
@@ -297,7 +297,7 @@ struct InstationaryNonLinearSimulationImpl<TypeTag, DiscretizationMethods::Box,
 
         // the non-linear solver
         using NewtonController = Dumux::NewtonController<TypeTag>;
-        using NewtonMethod = Dumux::NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+        using NewtonMethod = Dumux::NewtonMethod<NewtonController, Assembler, LinearSolver>;
         auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
         NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/dumux/linear/amgbackend.hh b/dumux/linear/amgbackend.hh
index 1cca191c98..5fb8dcbc34 100644
--- a/dumux/linear/amgbackend.hh
+++ b/dumux/linear/amgbackend.hh
@@ -76,7 +76,6 @@ class AMGBackend : public LinearSolver<TypeTag>
 {
     using GridView = typename GET_PROP_TYPE(TypeTag, GridView);
     using Grid = typename GET_PROP_TYPE(TypeTag, Grid);
-    using Scalar = typename GET_PROP_TYPE(TypeTag, Scalar);
     using AmgTraits = typename GET_PROP(TypeTag, AmgTraits);
     enum { numEq = AmgTraits::numEq };
     using LinearOperator = typename AmgTraits::LinearOperator;
diff --git a/dumux/linear/solver.hh b/dumux/linear/solver.hh
index 1c74fb10b7..5e0299603f 100644
--- a/dumux/linear/solver.hh
+++ b/dumux/linear/solver.hh
@@ -38,8 +38,10 @@ namespace Dumux
 template <class TypeTag>
 class LinearSolver
 {
-    using Scalar = typename GET_PROP_TYPE(TypeTag, Scalar);
 public:
+    //! export scalar type (might be needed to set parameters from output)
+    using Scalar = typename GET_PROP_TYPE(TypeTag, Scalar);
+
     //! default constructor sets some parameters
     LinearSolver()
     {
diff --git a/dumux/nonlinear/newtonmethod.hh b/dumux/nonlinear/newtonmethod.hh
index d40d2a042c..87f4da3d27 100644
--- a/dumux/nonlinear/newtonmethod.hh
+++ b/dumux/nonlinear/newtonmethod.hh
@@ -21,7 +21,7 @@
  *
  * \brief The algorithmic part of the multi dimensional newton method.
  *
- * In order to use the method you need a Newtoncontroller_->
+ * In order to use the method you need a Newtoncontroller
  */
 #ifndef DUMUX_NEWTONMETHOD_HH
 #define DUMUX_NEWTONMETHOD_HH
@@ -50,31 +50,26 @@ NEW_PROP_TAG(JacobianMatrix);
  * \ingroup Newton
  * \brief The algorithmic part of the multi dimensional newton method.
  *
- * In order to use the method you need a Newtoncontroller_->
+ * In order to use the method you need a Newtoncontroller
  */
-template <class TypeTag, class NewtonController, class JacobianAssembler, class LinearSolver>
+template <class NewtonController, class JacobianAssembler, class LinearSolver>
 class NewtonMethod
 {
-    using Scalar = typename GET_PROP_TYPE(TypeTag, Scalar);
-    using SolutionVector = typename GET_PROP_TYPE(TypeTag, SolutionVector);
-    using JacobianMatrix = typename GET_PROP_TYPE(TypeTag, JacobianMatrix);
-
 public:
     NewtonMethod(std::shared_ptr<NewtonController> controller,
                  std::shared_ptr<JacobianAssembler> assembler,
-                 std::shared_ptr<LinearSolver> linearSolver)
+                 std::shared_ptr<LinearSolver> linearSolver,
+                 const std::string& modelParamGroup = "")
     : controller_(controller)
     , assembler_(assembler)
     , linearSolver_(linearSolver)
-    , matrix_(std::make_shared<JacobianMatrix>())
-    , residual_(std::make_shared<SolutionVector>())
     {
         // set the linear system (matrix & residual) in the assembler
-        assembler_->setLinearSystem(matrix_, residual_);
+        assembler_->setLinearSystem();
 
         // set a different default for the linear solver residual reduction
         // within the Newton the linear solver doesn't need to solve too exact
-        static const std::string modelParamGroup = GET_PROP_VALUE(TypeTag, ModelParameterGroup);
+        using Scalar = typename LinearSolver::Scalar;
         linearSolver_->setResidualReduction(getParamFromGroup<Scalar>(modelParamGroup, "LinearSolver.ResidualReduction", 1e-6));
     }
 
@@ -82,6 +77,7 @@ public:
      * \brief Run the newton method to solve a non-linear system.
      *        The controller is responsible for all the strategic decisions.
      */
+    template<class SolutionVector>
     bool solve(SolutionVector& u)
     {
         try
@@ -146,9 +142,9 @@ public:
                 deltaU = 0;
                 // ask the controller to solve the linearized system
                 controller_->solveLinearSystem(*linearSolver_,
-                                               matrix(),
+                                               assembler_->jacobian(),
                                                deltaU,
-                                               residual());
+                                               assembler_->residual());
                 solveTimer.stop();
 
                 ///////////////
@@ -203,20 +199,10 @@ public:
         }
     }
 
-    //! The jacobian for the Newton method
-    JacobianMatrix& matrix()
-    { return *matrix_; }
-
-    //! The residual for the Newton method
-    SolutionVector& residual()
-    { return *residual_; }
-
 private:
     std::shared_ptr<NewtonController> controller_;
     std::shared_ptr<JacobianAssembler> assembler_;
     std::shared_ptr<LinearSolver> linearSolver_;
-    std::shared_ptr<JacobianMatrix> matrix_; //! The jacobian for the Newton method
-    std::shared_ptr<SolutionVector> residual_; //! The residual for the Newton method
 };
 
 } // end namespace Dumux
diff --git a/test/porousmediumflow/1p/implicit/compressible/test_1p.cc b/test/porousmediumflow/1p/implicit/compressible/test_1p.cc
index 203731222f..c8014ecbbd 100644
--- a/test/porousmediumflow/1p/implicit/compressible/test_1p.cc
+++ b/test/porousmediumflow/1p/implicit/compressible/test_1p.cc
@@ -137,7 +137,7 @@ int main(int argc, char** argv) try
     // the non-linear solver
     using NewtonController = NewtonController<TypeTag>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
-    NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver> nonLinearSolver(newtonController, assembler, linearSolver);
+    NewtonMethod<NewtonController, Assembler, LinearSolver> nonLinearSolver(newtonController, assembler, linearSolver);
 
     // set some check points for the time loop
     timeLoop->setPeriodicCheckPoint(tEnd/10.0);
diff --git a/test/porousmediumflow/1p/implicit/compressible/test_1p_stationary.cc b/test/porousmediumflow/1p/implicit/compressible/test_1p_stationary.cc
index 2d49aee7b9..5ff4f03429 100644
--- a/test/porousmediumflow/1p/implicit/compressible/test_1p_stationary.cc
+++ b/test/porousmediumflow/1p/implicit/compressible/test_1p_stationary.cc
@@ -116,7 +116,7 @@ int main(int argc, char** argv) try
     // the non-linear solver
     using NewtonController = NewtonController<TypeTag>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm());
-    NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver> nonLinearSolver(newtonController, assembler, linearSolver);
+    NewtonMethod<NewtonController, Assembler, LinearSolver> nonLinearSolver(newtonController, assembler, linearSolver);
 
     // linearize & solve
     Dune::Timer timer;
diff --git a/test/porousmediumflow/2p/implicit/incompressible/test_2p_box.cc b/test/porousmediumflow/2p/implicit/incompressible/test_2p_box.cc
index 79088fd7f4..f95fc37f09 100644
--- a/test/porousmediumflow/2p/implicit/incompressible/test_2p_box.cc
+++ b/test/porousmediumflow/2p/implicit/incompressible/test_2p_box.cc
@@ -165,7 +165,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = NewtonController<TypeTag>;
-    using NewtonMethod = NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/2p/implicit/incompressible/test_2p_cc.cc b/test/porousmediumflow/2p/implicit/incompressible/test_2p_cc.cc
index 8a6bfa29bc..d1a30de101 100644
--- a/test/porousmediumflow/2p/implicit/incompressible/test_2p_cc.cc
+++ b/test/porousmediumflow/2p/implicit/incompressible/test_2p_cc.cc
@@ -165,7 +165,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = NewtonController<TypeTag>;
-    using NewtonMethod = NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/2pnc/implicit/test_box2pnc.cc b/test/porousmediumflow/2pnc/implicit/test_box2pnc.cc
index fccc63e4a6..fe2d17a33d 100644
--- a/test/porousmediumflow/2pnc/implicit/test_box2pnc.cc
+++ b/test/porousmediumflow/2pnc/implicit/test_box2pnc.cc
@@ -160,7 +160,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = PriVarSwitchNewtonController<TypeTag>;
-    using NewtonMethod = NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/2pnc/implicit/test_cc2pnc.cc b/test/porousmediumflow/2pnc/implicit/test_cc2pnc.cc
index 4fab0c76bc..7ba7e44d6c 100644
--- a/test/porousmediumflow/2pnc/implicit/test_cc2pnc.cc
+++ b/test/porousmediumflow/2pnc/implicit/test_cc2pnc.cc
@@ -159,7 +159,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = PriVarSwitchNewtonController<TypeTag>;
-    using NewtonMethod = NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/3p/implicit/test_box3p.cc b/test/porousmediumflow/3p/implicit/test_box3p.cc
index 0af33e750d..1f2ae7a3d7 100644
--- a/test/porousmediumflow/3p/implicit/test_box3p.cc
+++ b/test/porousmediumflow/3p/implicit/test_box3p.cc
@@ -162,7 +162,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = Dumux::NewtonController<TypeTag>;
-    using NewtonMethod = Dumux::NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = Dumux::NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/3p/implicit/test_box3pniconduction.cc b/test/porousmediumflow/3p/implicit/test_box3pniconduction.cc
index ec1298f9a0..bb5ab3447f 100644
--- a/test/porousmediumflow/3p/implicit/test_box3pniconduction.cc
+++ b/test/porousmediumflow/3p/implicit/test_box3pniconduction.cc
@@ -165,7 +165,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = Dumux::NewtonController<TypeTag>;
-    using NewtonMethod = Dumux::NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = Dumux::NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/3p/implicit/test_box3pniconvection.cc b/test/porousmediumflow/3p/implicit/test_box3pniconvection.cc
index f6ea0553f8..7cca342837 100644
--- a/test/porousmediumflow/3p/implicit/test_box3pniconvection.cc
+++ b/test/porousmediumflow/3p/implicit/test_box3pniconvection.cc
@@ -165,7 +165,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = Dumux::NewtonController<TypeTag>;
-    using NewtonMethod = Dumux::NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = Dumux::NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/3p/implicit/test_cc3p.cc b/test/porousmediumflow/3p/implicit/test_cc3p.cc
index d7c1e3d44e..5f09e831f7 100644
--- a/test/porousmediumflow/3p/implicit/test_cc3p.cc
+++ b/test/porousmediumflow/3p/implicit/test_cc3p.cc
@@ -162,7 +162,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = Dumux::NewtonController<TypeTag>;
-    using NewtonMethod = Dumux::NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = Dumux::NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/3p/implicit/test_cc3pniconduction.cc b/test/porousmediumflow/3p/implicit/test_cc3pniconduction.cc
index df082b7796..858dbea7e6 100644
--- a/test/porousmediumflow/3p/implicit/test_cc3pniconduction.cc
+++ b/test/porousmediumflow/3p/implicit/test_cc3pniconduction.cc
@@ -165,7 +165,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = Dumux::NewtonController<TypeTag>;
-    using NewtonMethod = Dumux::NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = Dumux::NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
diff --git a/test/porousmediumflow/3p/implicit/test_cc3pniconvection.cc b/test/porousmediumflow/3p/implicit/test_cc3pniconvection.cc
index f00dacb3d6..77ee4c8b20 100644
--- a/test/porousmediumflow/3p/implicit/test_cc3pniconvection.cc
+++ b/test/porousmediumflow/3p/implicit/test_cc3pniconvection.cc
@@ -165,7 +165,7 @@ int main(int argc, char** argv) try
 
     // the non-linear solver
     using NewtonController = Dumux::NewtonController<TypeTag>;
-    using NewtonMethod = Dumux::NewtonMethod<TypeTag, NewtonController, Assembler, LinearSolver>;
+    using NewtonMethod = Dumux::NewtonMethod<NewtonController, Assembler, LinearSolver>;
     auto newtonController = std::make_shared<NewtonController>(leafGridView.comm(), timeLoop);
     NewtonMethod nonLinearSolver(newtonController, assembler, linearSolver);
 
-- 
GitLab