24#ifndef DUMUX_MATERIAL_GSTAT_RANDOM_FIELD_HH
25#define DUMUX_MATERIAL_GSTAT_RANDOM_FIELD_HH
32#include <dune/common/exceptions.hh>
33#include <dune/grid/common/mcmgmapper.hh>
34#include <dune/grid/io/file/vtk.hh>
51template<
class Gr
idView,
class Scalar>
54 enum { dim = GridView::dimension };
55 enum { dimWorld = GridView::dimensionworld };
57 using DataVector = std::vector<Scalar>;
58 using Element =
typename GridView::Traits::template Codim<0>::Entity;
59 using ElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
73 , elementMapper_(elementMapper)
74 , data_(gridView.size(0))
93 void create(
const std::string& gstatControlFile,
94 const std::string& gstatInputFile =
"gstatInput.txt",
95 const std::string& gstatOutputFile =
"permeab.dat",
97 bool createNew =
true)
99 fieldType_ = fieldType;
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.");
107 std::ofstream gstatInput(gstatInputFile);
108 for (
const auto& element : elements(gridView_))
110 gstatInput << element.geometry().center() << std::endl;
115 syscom = GSTAT_EXECUTABLE;
117 syscom += gstatControlFile;
119 if (!gstatInput.good())
121 DUNE_THROW(Dune::IOError,
"Reading the gstat control file: "
122 << gstatControlFile <<
" failed." << std::endl);
125 if (system(syscom.c_str()))
127 DUNE_THROW(Dune::IOError,
"Executing gstat failed.");
132 std::ifstream gstatOutput(gstatOutputFile);
133 if (!gstatOutput.good())
135 DUNE_THROW(Dune::IOError,
"Reading from file: "
136 << gstatOutputFile <<
" failed." << std::endl);
140 std::getline(gstatOutput,
line);
142 Scalar trash, dataValue;
143 for (
const auto& element : elements(gridView_))
145 std::getline(gstatOutput,
line);
146 std::istringstream curLine(
line);
148 curLine >> trash >> dataValue;
150 curLine >> trash >> trash >> dataValue;
152 curLine >> trash >> trash >> trash >> dataValue;
154 DUNE_THROW(Dune::InvalidStateException,
"Invalid dimension " << dim);
156 data_[elementMapper_.index(element)] = dataValue;
162 if (fieldType_ == FieldType::log10)
163 std::for_each(data_.begin(), data_.end(), [](Scalar& s){ s = pow(10.0, s); });
167 Scalar
data(
const Element& e)
const
169 return data_[elementMapper_.index(e)];
180 const std::string& dataName =
"data")
const
182 Dune::VTKWriter<GridView> vtkwriter(gridView_);
183 vtkwriter.addCellData(data_, dataName);
186 if (fieldType_ == FieldType::log10)
190 std::for_each(logPerm.begin(), logPerm.end(), [](Scalar& s){ s = log10(s); });
191 vtkwriter.addCellData(logPerm,
"log10 of " + dataName);
193 vtkwriter.write(vtkName, Dune::VTK::OutputType::ascii);
197 const GridView gridView_;
198 const ElementMapper& elementMapper_;
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