diff --git a/dune/vtk/vtkfunction.hh b/dune/vtk/vtkfunction.hh index c71d9f498cd708cb9e19e0350c75ae9d396716be..11ab864d4f966e04508f5f9f7c02d284a0eb4f97 100644 --- a/dune/vtk/vtkfunction.hh +++ b/dune/vtk/vtkfunction.hh @@ -48,14 +48,25 @@ namespace Dune public: /// Constructor VtkFunction from legacy VTKFunction - VtkFunction (std::shared_ptr<VTKFunction<GridView> const> const& fct) + VtkFunction (std::shared_ptr<VTKFunction<GridView> const> const& fct, + Std::optional<Vtk::DataTypes> type = {}) : localFct_(fct) , name_(fct->name()) , ncomps_(fct->ncomps()) - , type_(Vtk::FLOAT64) + , type_(type ? *type : Vtk::FLOAT64) {} /// Construct VtkFunction from dune-functions GridFunction with Signature + // NOTE: Stores the localFunction(fct) by value. + /** + * \param fct A Grid(View)-function, providing a `localFunction(fct)` + * \param name The name to use component identification in the VTK file + * \param ncomps Number of components of the pointwise data. Is extracted + * from the range type of the GridFunction if not given. + * \param type The \ref Vtk::DataTypes used in the output. E.g. FLOAT32, + * or FLOAT64. Is extracted from the range type of the + * GridFunction if not given. + **/ template <class F, std::enable_if_t<Std::is_detected<HasLocalFunction,F>::value, int> = 0> VtkFunction (F&& fct, std::string name, @@ -70,6 +81,13 @@ namespace Dune type_ = type ? *type : Vtk::Map::type<R>(); } + template <class F, + std::enable_if_t<Std::is_detected<HasLocalFunction,F>::value, int> = 0> + VtkFunction (F&& fct, Vtk::FieldInfo fieldInfo, + Std::optional<Vtk::DataTypes> type = {}) + : VtkFunction(std::forward<F>(fct), fieldInfo.name(), fieldInfo.ncomps(), type) + {} + VtkFunction () = default; /// Create a LocalFunction @@ -79,7 +97,7 @@ namespace Dune } /// Return a name associated with the function - std::string name () const + std::string const& name () const { return name_; } diff --git a/dune/vtk/vtktypes.hh b/dune/vtk/vtktypes.hh index 435b75f729a10474628dda7292bcb337591257c0..3d41852e549ee00648a0d22b3f5a3549e64fdd7e 100644 --- a/dune/vtk/vtktypes.hh +++ b/dune/vtk/vtktypes.hh @@ -113,5 +113,31 @@ namespace Dune bool noPermutation_; }; + + class FieldInfo + { + public: + FieldInfo (std::string name, int ncomps) + : name_(std::move(name)) + , ncomps_(ncomps) + {} + + /// The name of the data field + std::string const& name () const + { + return name_; + } + + /// The number of components in the data field. + int ncomps () const + { + return ncomps_; + } + + private: + std::string name_; + int ncomps_; + }; + } // end namespace Vtk } // end namespace Dune