opm-simulators
vtkscalarfunction.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  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
27 #ifndef VTK_SCALAR_FUNCTION_HH
28 #define VTK_SCALAR_FUNCTION_HH
29 
30 #include <dune/common/fvector.hh>
31 #include <dune/common/version.hh>
32 #include <dune/grid/io/file/vtk/function.hh>
33 
34 #include <dune/istl/bvector.hh>
35 
37 
38 #include <stdexcept>
39 #include <string>
40 #include <string_view>
41 
42 namespace Opm {
43 
48 template <class GridView, class Mapper>
49 class VtkScalarFunction : public Dune::VTKFunction<GridView>
50 {
51  enum { dim = GridView::dimension };
52  using ctype = typename GridView::ctype;
53  using Element = typename GridView::template Codim<0>::Entity;
54 
55  using ScalarBuffer = BaseOutputWriter::ScalarBuffer;
56 
57 public:
58  VtkScalarFunction(std::string_view name,
59  const GridView& gridView,
60  const Mapper& mapper,
61  const ScalarBuffer& buf,
62  unsigned codim)
63  : name_(name)
64  , gridView_(gridView)
65  , mapper_(mapper)
66  , buf_(buf)
67  , codim_(codim)
68  { assert(int(buf_.size()) == int(mapper_.size())); }
69 
70  std::string name() const override
71  { return name_; }
72 
73  int ncomps() const override
74  { return 1; }
75 
76  double evaluate(int,
77  const Element& e,
78  const Dune::FieldVector<ctype, dim>& xi) const override
79  {
80  unsigned idx;
81  if (codim_ == 0) {
82  // cells. map element to the index
83  idx = static_cast<unsigned>(mapper_.index(e));
84  }
85  else if (codim_ == dim) {
86  // find vertex which is closest to xi in local
87  // coordinates. This code is based on Dune::P1VTKFunction
88  double min = 1e100;
89  int imin = -1;
90  Dune::GeometryType gt = e.type();
91  int n = static_cast<int>(e.subEntities(dim));
92  for (int i = 0; i < n; ++i) {
93  Dune::FieldVector<ctype, dim> local =
94  Dune::ReferenceElements<ctype, dim>::general(gt).position(i, dim);
95 
96  local -= xi;
97  if (local.infinity_norm() < min) {
98  min = local.infinity_norm();
99  imin = static_cast<int>(i);
100  }
101  }
102 
103  // map vertex to an index
104  idx = static_cast<unsigned>(mapper_.subIndex(e, imin, codim_));
105  }
106  else {
107  throw std::logic_error("Only element and vertex based vector fields are"
108  " supported so far.");
109  }
110 
111  return static_cast<double>(static_cast<float>(buf_[idx]));
112  }
113 
114 private:
115  const std::string name_;
116  const GridView gridView_;
117  const Mapper& mapper_;
118  const ScalarBuffer& buf_;
119  unsigned codim_;
120 };
121 
122 } // namespace Opm
123 
124 #endif
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
Provides a vector-valued function using Dune::FieldVectors as elements.
Definition: vtkscalarfunction.hh:49
The base class for all output writers.