12#ifndef DUMUX_MATERIAL_GSTAT_RANDOM_FIELD_HH
13#define DUMUX_MATERIAL_GSTAT_RANDOM_FIELD_HH
20#include <dune/common/exceptions.hh>
21#include <dune/grid/common/mcmgmapper.hh>
22#include <dune/grid/io/file/vtk.hh>
39template<
class Gr
idView,
class Scalar>
42 enum { dim = GridView::dimension };
43 enum { dimWorld = GridView::dimensionworld };
45 using DataVector = std::vector<Scalar>;
46 using Element =
typename GridView::Traits::template Codim<0>::Entity;
47 using ElementMapper = Dune::MultipleCodimMultipleGeomTypeMapper<GridView>;
61 , elementMapper_(elementMapper)
62 , data_(gridView.size(0))
81 void create(
const std::string& gstatControlFile,
82 const std::string& gstatInputFile =
"gstatInput.txt",
83 const std::string& gstatOutputFile =
"permeab.dat",
85 bool createNew =
true)
87 fieldType_ = fieldType;
91 DUNE_THROW(Dune::InvalidStateException,
"Requested data field generation with gstat"
92 <<
" but gstat was not found on your system. Set GSTAT_ROOT to the path where gstat "
93 <<
" is installed and pass it to CMake, e.g. through an opts file.");
95 std::ofstream gstatInput(gstatInputFile);
96 for (
const auto& element : elements(gridView_))
98 gstatInput << element.geometry().center() << std::endl;
103 syscom = GSTAT_EXECUTABLE;
105 syscom += gstatControlFile;
107 if (!gstatInput.good())
109 DUNE_THROW(Dune::IOError,
"Reading the gstat control file: "
110 << gstatControlFile <<
" failed." << std::endl);
113 if (system(syscom.c_str()))
115 DUNE_THROW(Dune::IOError,
"Executing gstat failed.");
120 std::ifstream gstatOutput(gstatOutputFile);
121 if (!gstatOutput.good())
123 DUNE_THROW(Dune::IOError,
"Reading from file: "
124 << gstatOutputFile <<
" failed." << std::endl);
128 std::getline(gstatOutput,
line);
130 Scalar trash, dataValue;
131 for (
const auto& element : elements(gridView_))
133 std::getline(gstatOutput,
line);
134 std::istringstream curLine(
line);
136 curLine >> trash >> dataValue;
138 curLine >> trash >> trash >> dataValue;
140 curLine >> trash >> trash >> trash >> dataValue;
142 DUNE_THROW(Dune::InvalidStateException,
"Invalid dimension " << dim);
144 data_[elementMapper_.index(element)] = dataValue;
150 if (fieldType_ == FieldType::log10)
151 std::for_each(data_.begin(), data_.end(), [](Scalar& s){ s = pow(10.0, s); });
155 Scalar
data(
const Element& e)
const
157 return data_[elementMapper_.index(e)];
168 const std::string& dataName =
"data")
const
170 Dune::VTKWriter<GridView> vtkwriter(gridView_);
171 vtkwriter.addCellData(data_, dataName);
174 if (fieldType_ == FieldType::log10)
178 std::for_each(logPerm.begin(), logPerm.end(), [](Scalar& s){ s = log10(s); });
179 vtkwriter.addCellData(logPerm,
"log10 of " + dataName);
181 vtkwriter.write(vtkName, Dune::VTK::OutputType::ascii);
185 const GridView gridView_;
186 const ElementMapper& elementMapper_;
Creating random fields using gstat.
Definition: gstatrandomfield.hh:41
void writeVtk(const std::string &vtkName, const std::string &dataName="data") const
Write the data to a vtk file.
Definition: gstatrandomfield.hh:167
Scalar data(const Element &e) const
Return an entry of the data vector.
Definition: gstatrandomfield.hh:155
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:81
GstatRandomField(const GridView &gridView, const ElementMapper &elementMapper)
Constructor.
Definition: gstatrandomfield.hh:59
const DataVector & data() const
Return the data vector for analysis or external vtk output.
Definition: gstatrandomfield.hh:161
FieldType
Definition: gstatrandomfield.hh:51
@ scalar
Definition: gstatrandomfield.hh:51
@ log10
Definition: gstatrandomfield.hh:51
constexpr Line line
Definition: couplingmanager1d3d_line.hh:31