3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
gstatrandomfield.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 * See the file COPYING for full copying permissions. *
5 * *
6 * This program 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 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef DUMUX_MATERIAL_GSTAT_RANDOM_FIELD_HH
25#define DUMUX_MATERIAL_GSTAT_RANDOM_FIELD_HH
26
27#include <iostream>
28#include <algorithm>
29#include <string>
30#include <cmath>
31
32#include <dune/common/exceptions.hh>
33#include <dune/grid/common/mcmgmapper.hh>
34#include <dune/grid/io/file/vtk.hh>
35
36namespace Dumux {
37
51template<class GridView, class Scalar>
53{
54 enum { dim = GridView::dimension };
55 enum { dimWorld = GridView::dimensionworld };
56
57 using DataVector = std::vector<Scalar>;
58 using Element = typename GridView::Traits::template Codim<0>::Entity;
59 using ElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
60
61public:
62 // Add field types if you want to implement e.g. tensor permeabilities.
64
71 GstatRandomField(const GridView& gridView, const ElementMapper& elementMapper)
72 : gridView_(gridView)
73 , elementMapper_(elementMapper)
74 , data_(gridView.size(0))
75 {}
76
93 void create(const std::string& gstatControlFile,
94 const std::string& gstatInputFile = "gstatInput.txt",
95 const std::string& gstatOutputFile = "permeab.dat",
96 FieldType fieldType = FieldType::scalar,
97 bool createNew = true)
98 {
99 fieldType_ = fieldType;
100 if (createNew)
101 {
102#if !HAVE_GSTAT
103 DUNE_THROW(Dune::InvalidStateException, "Requested data field generation with gstat"
104 << " but gstat was not found on your system. Set GSTAT_ROOT to the path where gstat "
105 << " is installed and pass it to CMake, e.g. through an opts file.");
106#else
107 std::ofstream gstatInput(gstatInputFile);
108 for (const auto& element : elements(gridView_))
109 // get global coordinates of cell centers
110 gstatInput << element.geometry().center() << std::endl;
111
112 gstatInput.close();
113
114 std::string syscom;
115 syscom = GSTAT_EXECUTABLE;
116 syscom += " ";
117 syscom += gstatControlFile;
118
119 if (!gstatInput.good())
120 {
121 DUNE_THROW(Dune::IOError, "Reading the gstat control file: "
122 << gstatControlFile << " failed." << std::endl);
123 }
124
125 if (system(syscom.c_str()))
126 {
127 DUNE_THROW(Dune::IOError, "Executing gstat failed.");
128 }
129#endif
130 }
131
132 std::ifstream gstatOutput(gstatOutputFile);
133 if (!gstatOutput.good())
134 {
135 DUNE_THROW(Dune::IOError, "Reading from file: "
136 << gstatOutputFile << " failed." << std::endl);
137 }
138
139 std::string line;
140 std::getline(gstatOutput, line);
141
142 Scalar trash, dataValue;
143 for (const auto& element : elements(gridView_))
144 {
145 std::getline(gstatOutput, line);
146 std::istringstream curLine(line);
147 if (dim == 1)
148 curLine >> trash >> dataValue;
149 else if (dim == 2)
150 curLine >> trash >> trash >> dataValue;
151 else if (dim == 3)
152 curLine >> trash >> trash >> trash >> dataValue;
153 else
154 DUNE_THROW(Dune::InvalidStateException, "Invalid dimension " << dim);
155
156 data_[elementMapper_.index(element)] = dataValue;
157 }
158 gstatOutput.close();
159
160 // post processing
161 using std::pow;
162 if (fieldType_ == FieldType::log10)
163 std::for_each(data_.begin(), data_.end(), [](Scalar& s){ s = pow(10.0, s); });
164 }
165
167 Scalar data(const Element& e) const
168 {
169 return data_[elementMapper_.index(e)];
170 }
171
173 const DataVector& data() const
174 {
175 return data_;
176 }
177
179 void writeVtk(const std::string& vtkName,
180 const std::string& dataName = "data") const
181 {
182 Dune::VTKWriter<GridView> vtkwriter(gridView_);
183 vtkwriter.addCellData(data_, dataName);
184
185 DataVector logPerm;
186 if (fieldType_ == FieldType::log10)
187 {
188 logPerm = data_;
189 using std::log10;
190 std::for_each(logPerm.begin(), logPerm.end(), [](Scalar& s){ s = log10(s); });
191 vtkwriter.addCellData(logPerm, "log10 of " + dataName);
192 }
193 vtkwriter.write(vtkName, Dune::VTK::OutputType::ascii);
194 }
195
196private:
197 const GridView gridView_;
198 const ElementMapper& elementMapper_;
199 DataVector data_;
200 FieldType fieldType_;
201};
202
203} // end namespace Dumux
204
205#endif
Definition: adapt.hh:29
constexpr Line line
Definition: couplingmanager1d3d_line.hh:43
Creating random fields using gstat.
Definition: gstatrandomfield.hh:53
void writeVtk(const std::string &vtkName, const std::string &dataName="data") const
Write the data to a vtk file.
Definition: gstatrandomfield.hh:179
Scalar data(const Element &e) const
Return an entry of the data vector.
Definition: gstatrandomfield.hh:167
void create(const std::string &gstatControlFile, const std::string &gstatInputFile="gstatInput.txt", const std::string &gstatOutputFile="permeab.dat", FieldType fieldType=FieldType::scalar, bool createNew=true)
Creates a new field with random variables, if desired. Otherwise creates a data field from already av...
Definition: gstatrandomfield.hh:93
GstatRandomField(const GridView &gridView, const ElementMapper &elementMapper)
Constructor.
Definition: gstatrandomfield.hh:71
const DataVector & data() const
Return the data vector for analysis or external vtk output.
Definition: gstatrandomfield.hh:173
FieldType
Definition: gstatrandomfield.hh:63
@ scalar
Definition: gstatrandomfield.hh:63
@ log10
Definition: gstatrandomfield.hh:63