24#ifndef DUMUX_IO_LOADSOLUTION_HH
25#define DUMUX_IO_LOADSOLUTION_HH
30#include <unordered_set>
31#include <unordered_map>
35#include <dune/common/version.hh>
36#include <dune/common/exceptions.hh>
37#include <dune/common/indices.hh>
38#include <dune/grid/common/partitionset.hh>
54template <
class Container,
class EntityMapper,
int codim>
56:
public Dune::CommDataHandleIF< LoadSolutionDataHandle<Container, EntityMapper, codim>,
57 std::decay_t<decltype(std::declval<Container>()[0])> >
59 using FieldType = std::decay_t<decltype(std::declval<Container>()[0])>;
62 const EntityMapper& mapper)
64 , container_(container)
68 {
return cd == codim; }
70#if DUNE_VERSION_GTE(DUNE_GRID,2,7)
72 bool fixedSize(
int dim,
int cd)
const
80 template<
class EntityType>
81 size_t size (
const EntityType &e)
const
84 template<
class MessageBufferImp,
class EntityType>
85 void gather(MessageBufferImp& buff,
const EntityType& e)
const
87 const auto vIdx = mapper_.index(e);
88 buff.write(container_[vIdx]);
91 template<
class MessageBufferImp,
class EntityType>
92 void scatter(MessageBufferImp& buff,
const EntityType& e,
size_t n)
94 const auto vIdx = mapper_.index(e);
97 container_[vIdx] = tmp;
101 EntityMapper mapper_;
102 Container& container_;
109template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
111 const std::string fileName,
112 PvNameFunc&& pvNameFunc,
113 const GridGeometry& gridGeometry,
118 using PrimaryVariables =
typename SolutionVector::block_type;
119 using Scalar =
typename PrimaryVariables::field_type;
120 constexpr auto dim = GridGeometry::GridView::dimension;
122 for (
size_t pvIdx = 0; pvIdx < PrimaryVariables::dimension; ++pvIdx)
124 const auto pvName = pvNameFunc(pvIdx, 0);
125 auto vec = vtu.
readData<std::vector<Scalar>>(pvName, dataType);
130 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
132 const auto eIdx = gridGeometry.elementMapper().index(element);
133 sol[eIdx][pvIdx] = vec[i++];
139 if (sol.size() != vec.size())
140 DUNE_THROW(Dune::InvalidStateException,
"Solution size (" << sol.size() <<
") does not match input size (" << vec.size() <<
")!");
142 for (std::size_t i = 0; i < sol.size(); ++i)
143 sol[i][pvIdx] = vec[i];
148 std::vector<bool> visited(gridGeometry.gridView().size(dim),
false);
149 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
151 for (
int vIdxLocal = 0; vIdxLocal < element.subEntities(dim); ++vIdxLocal)
153 const auto vIdxGlobal = gridGeometry.vertexMapper().subIndex(element, vIdxLocal, dim);
154 if (!visited[vIdxGlobal])
156 sol[vIdxGlobal][pvIdx] = vec[i++];
157 visited[vIdxGlobal] =
true;
169template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
171 const std::string fileName,
172 PvNameFunc&& pvNameFunc,
173 const GridGeometry& gridGeometry,
175->
typename std::enable_if_t<
decltype(
isValid(Detail::hasState())(sol[0]))::value, void>
177 VTKReader vtu(fileName);
180 const auto stateAtDof = vtu.readData<std::vector<int>>(
"phase presence", dataType);
183 std::unordered_set<int> states;
184 for (
size_t i = 0; i < stateAtDof.size(); ++i)
185 states.insert(stateAtDof[i]);
187 using PrimaryVariables =
typename SolutionVector::block_type;
188 using Scalar =
typename PrimaryVariables::field_type;
189 for (
size_t pvIdx = 0; pvIdx < PrimaryVariables::dimension; ++pvIdx)
191 std::unordered_map<int, std::vector<Scalar>> data;
192 for (
const auto& state : states)
193 data[state] = vtu.readData<std::vector<Scalar>>(pvNameFunc(pvIdx, state), dataType);
198 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
200 const auto eIdx = gridGeometry.elementMapper().index(element);
201 const auto state = stateAtDof[i];
202 sol[eIdx][pvIdx] = data[state][i++];
203 sol[eIdx].setState(state);
209 constexpr int dim = GridGeometry::GridView::dimension;
210 std::vector<bool> visited(gridGeometry.gridView().size(dim),
false);
211 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
213 for (
int vIdxLocal = 0; vIdxLocal < element.subEntities(dim); ++vIdxLocal)
215 const auto vIdxGlobal = gridGeometry.vertexMapper().subIndex(element, vIdxLocal, dim);
216 if (!visited[vIdxGlobal])
218 const auto state = stateAtDof[i];
219 sol[vIdxGlobal][pvIdx] = data[state][i++];
220 sol[vIdxGlobal].setState(state);
221 visited[vIdxGlobal] =
true;
234template<
class IOFields,
class PrimaryVariables,
class ModelTraits =
void,
class Flu
idSystem =
void,
class Sol
idSystem =
void>
236->
typename std::enable_if_t<
decltype(
isValid(
Detail::hasState())(PrimaryVariables(0)))::value, std::function<std::string(int,int)>>
238 return [paramGroup](
int pvIdx,
int state = 0)
240 static auto numStates = (1 << ModelTraits::numFluidPhases()) - 1;
241 const auto paramNameWithState =
"LoadSolution.PriVarNamesState" + std::to_string(state);
244 DUNE_THROW(Dune::NotImplemented,
"please provide LoadSolution.PriVarNamesState1..." << numStates
245 <<
" or remove LoadSolution.PriVarNames to use the model's default primary variable names");
249 const auto pvName = getParamFromGroup<std::vector<std::string>>(paramGroup, paramNameWithState);
250 return pvName[pvIdx];
253 return IOFields::template primaryVariableName<ModelTraits, FluidSystem, SolidSystem>(pvIdx, state);
262template<
class IOFields,
class PrimaryVariables,
class ModelTraits =
void,
class Flu
idSystem =
void,
class Sol
idSystem =
void>
264->
typename std::enable_if_t<!
decltype(
isValid(Detail::hasState())(PrimaryVariables(0)))::value, std::function<std::string(
int,
int)>>
268 const auto pvName = getParamFromGroup<std::vector<std::string>>(paramGroup,
"LoadSolution.PriVarNames");
269 return [n = std::move(pvName)](
int pvIdx,
int state = 0){
return n[pvIdx]; };
272 return [](
int pvIdx,
int state = 0){
return IOFields::template primaryVariableName<ModelTraits, FluidSystem, SolidSystem>(pvIdx, state); };
285template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
287 const std::string& fileName,
288 PvNameFunc&& pvNameFunc,
289 const GridGeometry& gridGeometry)
291 const auto extension = fileName.substr(fileName.find_last_of(
".") + 1);
292 auto dataType = GridGeometry::discMethod == DiscretizationMethod::box ?
293 VTKReader::DataType::pointData : VTKReader::DataType::cellData;
295 if (extension ==
"vtu" || extension ==
"vtp")
297 if (GridGeometry::discMethod == DiscretizationMethod::staggered && extension ==
"vtp")
298 dataType = VTKReader::DataType::pointData;
302 else if (extension ==
"pvtu" || extension ==
"pvtp")
304 if (GridGeometry::discMethod == DiscretizationMethod::staggered)
305 DUNE_THROW(Dune::NotImplemented,
"reading staggered solution from a parallel vtk file");
310 DUNE_THROW(Dune::NotImplemented,
"loadSolution for file with extension " << extension);
313 if (gridGeometry.gridView().comm().size() > 1)
315 using GridView =
typename GridGeometry::GridView;
316 if (dataType == VTKReader::DataType::cellData)
319 dataHandle(sol, gridGeometry.elementMapper());
320 gridGeometry.gridView().communicate(dataHandle,
321 Dune::InteriorBorder_All_Interface,
322 Dune::ForwardCommunication);
327 dataHandle(sol, gridGeometry.vertexMapper());
328 gridGeometry.gridView().communicate(dataHandle,
329 Dune::InteriorBorder_All_Interface,
330 Dune::ForwardCommunication);
A helper function for class member function introspection.
Type traits to be used with matrix types.
Type traits to be used with vector types.
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
The available discretization methods in Dumux.
A vtk file reader using tinyxml2 as xml backend.
bool hasParamInGroup(const std::string ¶mGroup, const std::string ¶m)
Check whether a key exists in the parameter tree with a model group prefix.
Definition: parameters.hh:391
auto loadSolutionFromVtkFile(SolutionVector &sol, const std::string fileName, PvNameFunc &&pvNameFunc, const GridGeometry &gridGeometry, const VTKReader::DataType &dataType) -> typename std::enable_if_t<!decltype(isValid(Detail::hasState())(sol[0]))::value, void >
read from a vtk file into a solution vector with primary variables without state
Definition: loadsolution.hh:110
void loadSolution(SolutionVector &sol, const std::string &fileName, PvNameFunc &&pvNameFunc, const GridGeometry &gridGeometry)
load a solution vector from file
Definition: loadsolution.hh:286
auto createPVNameFunction(const std::string ¶mGroup="") -> typename std::enable_if_t< decltype(isValid(Detail::hasState())(PrimaryVariables(0)))::value, std::function< std::string(int, int)> >
helper function to determine the primary variable names of a model with privar state
Definition: loadsolution.hh:235
constexpr auto isValid(const Expression &t)
A function that creates a test functor to do class member introspection at compile time.
Definition: isvalid.hh:93
helper struct detecting if a PrimaryVariables object has a state() function
Definition: state.hh:32
a data handle to communicate the solution on ghosts and overlaps when reading from vtk file in parall...
Definition: loadsolution.hh:58
LoadSolutionDataHandle(Container &container, const EntityMapper &mapper)
Definition: loadsolution.hh:61
bool contains(int dim, int cd) const
Definition: loadsolution.hh:67
size_t size(const EntityType &e) const
Definition: loadsolution.hh:81
void scatter(MessageBufferImp &buff, const EntityType &e, size_t n)
Definition: loadsolution.hh:92
void gather(MessageBufferImp &buff, const EntityType &e) const
Definition: loadsolution.hh:85
bool fixedsize(int dim, int cd) const
returns true if size per entity of given dim and codim is a constant
Definition: loadsolution.hh:76
A vtk file reader using tinyxml2 as xml backend.
Definition: vtkreader.hh:49
Container readData(const std::string &name, const DataType &type) const
read data from the vtk file to a container, e.g. std::vector<double>
Definition: vtkreader.hh:79
DataType
The data array types.
Definition: vtkreader.hh:54