### [newton][varsbackend] add backends for vectors & generic variables

```This commit also removes the interfaces related to the construction of
vectors for the solver, as this is currently done in Detail::assign in
newtonsolver.hh. The same holds for maxRelativeShift(), which is also
currently implemented in the Detail namespace in newtonsolver.hh-

In the future, we could think about outsourcing putting these two
functions into the backend.```
parent b7a01c11
 ... ... @@ -24,29 +24,41 @@ #ifndef DUMUX_NEWTON_VARIABLES_BACKEND_HH #define DUMUX_NEWTON_VARIABLES_BACKEND_HH #include #include #include #include #include #include #include #include // forward declaration namespace Dune { template class MultiTypeBlockVector; } // end namespace Dune namespace Dumux { /*! * \file * \ingroup Nonlinear * \brief Class providing operations with variables needed in the Newton solver * \brief Class providing operations with primary variable vectors */ template class NewtonVariablesBackend; template class NewtonDofBackend; /*! * \file * \ingroup Nonlinear * \brief Class providing Newton operations for scalar/number types * \brief Specialization providing Newton operations for scalar/number types */ template class NewtonVariablesBackend::value, Scalar>> class NewtonDofBackend::value, Scalar>> { public: using Variables = Scalar; //!< the type of the variables object using DofVector = Scalar; //!< the type of the dofs parametrizing the variables object static std::size_t size(const DofVector& d) ... ... @@ -57,30 +69,133 @@ public: static DofVector makeZeroDofVector(std::size_t size) { return 0.0; } }; /*! * \ingroup Nonlinear * \brief Specialization providing Newton operations for block vectors */ template class NewtonDofBackend> { public: using DofVector = Dune::BlockVector; //!< the type of the dofs parametrizing the variables object static Scalar makeDofVectorForSolver(const DofVector& d) static std::size_t size(const DofVector& d) { return d.size(); } static DofVector makeDofVector(const DofVector& d) { return d; } static Scalar makeZeroDofVectorForSolver(std::size_t size) { return 0.0; } static DofVector makeZeroDofVector(std::size_t size) { DofVector d; d.resize(size); return d; } }; /*! * \ingroup Nonlinear * \brief Specialization providing Newton operations for multitype block vectors */ template class NewtonDofBackend> { using DV = Dune::MultiTypeBlockVector; static constexpr auto numBlocks = DV::size(); using VectorSizeInfo = std::array; public: using DofVector = DV; //!< the type of the dofs parametrizing the variables object static VectorSizeInfo size(const DofVector& d) { VectorSizeInfo result; using namespace Dune::Hybrid; forEach(std::make_index_sequence{}, [&](auto i) { result[i] = d[Dune::index_constant{}].size(); }); return result; } static Scalar reconstructDofVectorFromSolver(const Scalar& d) static DofVector makeDofVector(const DofVector& d) { return d; } static Scalar maxRelativeShift(const DofVector& previous, const DofVector& delta) static DofVector makeZeroDofVector(const VectorSizeInfo& size) { const auto current = previous - delta; using std::abs; using std::max; auto error = abs(previous - current); error /= max(1.0, abs(previous + current)/2.0); return error; DofVector result; using namespace Dune::Hybrid; forEach(std::make_index_sequence{}, [&](auto i) { result[Dune::index_constant{}].resize(size[i]); }); return result; } }; namespace Impl { template using SolutionVectorType = typename Vars::SolutionVector; template class NewtonVariablesBackend; /*! * \ingroup Nonlinear * \brief Class providing Newton operations for scalar/number types * \note We assume the variables being simply a dof vector if we * do not find the variables class to export `SolutionVector`. */ template class NewtonVariablesBackend : public NewtonDofBackend { using ParentType = NewtonDofBackend; public: using Variables = Vars; using typename ParentType::DofVector; // //! operations on variables // static DofVector& getDofVector(Variables& v) // { return v; } static void update(Variables& v, const DofVector& dofs) { v = dofs; } //! operations on variables static const DofVector& getDofVector(const Variables& v) { return v; } }; /*! * \file * \ingroup Nonlinear * \brief Class providing Newton operations for generic variable * types, possibly containing also secondary variables. */ template class NewtonVariablesBackend : public NewtonDofBackend { public: using DofVector = typename Vars::SolutionVector; using Variables = Vars; //!< the type of the variables object static void update(Variables& v, const DofVector& dofs) { v.update(dofs); } //! operations on variables static const DofVector& getDofVector(const Variables& v) { return v.dofs(); } }; } // end namespace Impl /*! * \ingroup Nonlinear * \brief Class providing Newton operations for generic variable classes * that represent the state of a numerical solution, possibly * consisting of primary/secondary variables and information on * the time level. */ template using NewtonVariablesBackend = Impl::NewtonVariablesBackend>; } // end namespace Dumux #endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment