24#ifndef DUMUX_IO_LOADSOLUTION_HH
25#define DUMUX_IO_LOADSOLUTION_HH
30#include <unordered_set>
31#include <unordered_map>
35#include <dune/common/exceptions.hh>
36#include <dune/common/indices.hh>
37#include <dune/grid/common/partitionset.hh>
53template <
class Container,
class EntityMapper,
int codim>
55:
public Dune::CommDataHandleIF< LoadSolutionDataHandle<Container, EntityMapper, codim>,
56 std::decay_t<decltype(std::declval<Container>()[0])> >
58 using FieldType = std::decay_t<decltype(std::declval<Container>()[0])>;
61 const EntityMapper& mapper)
63 , container_(container)
67 {
return cd == codim; }
73 template<
class EntityType>
74 std::size_t
size (
const EntityType &e)
const
77 template<
class MessageBufferImp,
class EntityType>
78 void gather(MessageBufferImp& buff,
const EntityType& e)
const
80 const auto vIdx = mapper_.index(e);
81 buff.write(container_[vIdx]);
84 template<
class MessageBufferImp,
class EntityType>
85 void scatter(MessageBufferImp& buff,
const EntityType& e, std::size_t n)
87 const auto vIdx = mapper_.index(e);
90 container_[vIdx] = tmp;
95 Container& container_;
102template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
104 const std::string fileName,
105 PvNameFunc&& targetPvNameFunc,
106 const GridGeometry& gridGeometry,
112 using PrimaryVariables =
typename SolutionVector::block_type;
113 using Scalar =
typename PrimaryVariables::field_type;
114 constexpr auto dim = GridGeometry::GridView::dimension;
115 const std::size_t targetSolutionSize = PrimaryVariables::dimension;
117 std::size_t matchingLoadedArrays = 0;
118 for (std::size_t i = 0; i < targetSolutionSize; i++)
119 if (vtu.
hasData(targetPvNameFunc(i,0), dataType))
120 matchingLoadedArrays++;
122 if (matchingLoadedArrays < targetSolutionSize)
123 std::cout <<
"The loaded solution does not provide a data array for each of the primary variables. \n"
124 <<
"The target solution has "<< targetSolutionSize <<
" entries, "
125 <<
"whereas the loaded solution provides only " << matchingLoadedArrays <<
" data array(s). \n"
126 <<
"Make sure that the model concepts are compatible, "
127 <<
"and be sure to provide initial conditions for the missing primary variables. \n";
129 for (std::size_t targetPvIdx = 0; targetPvIdx < targetSolutionSize; ++targetPvIdx)
131 std::vector<Scalar> vec;
132 const auto targetPvName = targetPvNameFunc(targetPvIdx, 0);
134 if (vtu.
hasData(targetPvName, dataType))
135 vec = vtu.
readData<std::vector<Scalar>>(targetPvName, dataType);
138 std::cout <<
"The loaded solution does not have a field named \"" << targetPvName <<
"\". "
139 <<
"Make sure this field is filled using the initial method in the problem definition. \n";
146 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
148 const auto eIdx = gridGeometry.elementMapper().index(element);
149 sol[eIdx][targetPvIdx] = vec[i++];
155 if (sol.size() != vec.size())
156 DUNE_THROW(Dune::InvalidStateException,
"Solution size (" << sol.size() <<
") does not match input size (" << vec.size() <<
")!");
158 for (std::size_t i = 0; i < sol.size(); ++i)
159 sol[i][targetPvIdx] = vec[i];
164 std::vector<bool> visited(gridGeometry.gridView().size(dim),
false);
165 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
167 for (
int vIdxLocal = 0; vIdxLocal < element.subEntities(dim); ++vIdxLocal)
169 const auto vIdxGlobal = gridGeometry.vertexMapper().subIndex(element, vIdxLocal, dim);
170 if (!visited[vIdxGlobal])
172 sol[vIdxGlobal][targetPvIdx] = vec[i++];
173 visited[vIdxGlobal] =
true;
185template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
187 const std::string fileName,
188 PvNameFunc&& targetPvNameFunc,
189 const GridGeometry& gridGeometry,
191->
typename std::enable_if_t<
decltype(
isValid(Detail::hasState())(sol[0]))::value, void>
193 VTKReader vtu(fileName);
195 const auto stateAtDof = vtu.readData<std::vector<int>>(
"phase presence", dataType);
198 std::unordered_set<int> states;
199 for (std::size_t i = 0; i < stateAtDof.size(); ++i)
200 states.insert(stateAtDof[i]);
202 using PrimaryVariables =
typename SolutionVector::block_type;
203 using Scalar =
typename PrimaryVariables::field_type;
204 const std::size_t targetSolutionSize = PrimaryVariables::dimension;
206 std::unordered_set<std::string> matchingNames;
207 for (std::size_t i = 0; i < targetSolutionSize; i++)
208 for (
const auto& state : states)
209 if ( vtu.hasData(targetPvNameFunc(i,state), dataType))
210 matchingNames.insert(targetPvNameFunc(i,state));
212 const std::size_t matchingLoadedArrays = matchingNames.size() - (states.size()-1);
214 if (matchingLoadedArrays < targetSolutionSize)
215 std::cout <<
"The loaded solution does not provide a data array for each of the primary variables. \n"
216 <<
"The target solution has "<< targetSolutionSize <<
" entries, "
217 <<
"whereas the loaded solution provides only " << matchingLoadedArrays <<
" data array(s). \n"
218 <<
"Make sure that the model concepts are compatible, "
219 <<
"and be sure to provide initial conditions for the missing primary variables. \n";
221 for (std::size_t targetPvIdx = 0; targetPvIdx < targetSolutionSize; ++targetPvIdx)
223 std::unordered_map<int, std::vector<Scalar>> data;
224 for (
const auto& state : states)
226 const auto targetPvName = targetPvNameFunc(targetPvIdx, state);
228 if (vtu.hasData(targetPvName, dataType))
229 data[state] = vtu.readData<std::vector<Scalar>>(targetPvName, dataType);
232 std::cout <<
"Loaded Solution does not have a field named \"" << targetPvName <<
"\". "
233 <<
"Make sure this field is filled using the initial method in the problem definition. \n";
241 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
243 const auto eIdx = gridGeometry.elementMapper().index(element);
244 const auto state = stateAtDof[i];
245 sol[eIdx][targetPvIdx] = data[state][i++];
246 sol[eIdx].setState(state);
252 constexpr int dim = GridGeometry::GridView::dimension;
253 std::vector<bool> visited(gridGeometry.gridView().size(dim),
false);
254 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
256 for (
int vIdxLocal = 0; vIdxLocal <
element.subEntities(dim); ++vIdxLocal)
258 const auto vIdxGlobal = gridGeometry.vertexMapper().subIndex(element, vIdxLocal, dim);
259 if (!visited[vIdxGlobal])
261 const auto state = stateAtDof[i];
262 sol[vIdxGlobal][targetPvIdx] = data[state][i++];
263 sol[vIdxGlobal].setState(state);
264 visited[vIdxGlobal] =
true;
277template<
class IOFields,
class PrimaryVariables,
class ModelTraits =
void,
class Flu
idSystem =
void,
class Sol
idSystem =
void>
279->
typename std::enable_if_t<
decltype(
isValid(
Detail::hasState())(PrimaryVariables(0)))::value, std::function<std::string(int,int)>>
281 return [paramGroup](
int pvIdx,
int state = 0)
283 static auto numStates = (1 << ModelTraits::numFluidPhases()) - 1;
284 const auto paramNameWithState =
"LoadSolution.PriVarNamesState" + std::to_string(state);
287 DUNE_THROW(Dune::NotImplemented,
"please provide LoadSolution.PriVarNamesState1..." << numStates
288 <<
" or remove LoadSolution.PriVarNames to use the model's default primary variable names");
292 const auto pvName = getParamFromGroup<std::vector<std::string>>(paramGroup, paramNameWithState);
293 return pvName[pvIdx];
296 return IOFields::template primaryVariableName<ModelTraits, FluidSystem, SolidSystem>(pvIdx, state);
305template<
class IOFields,
class PrimaryVariables,
class ModelTraits =
void,
class Flu
idSystem =
void,
class Sol
idSystem =
void>
307->
typename std::enable_if_t<!
decltype(
isValid(Detail::hasState())(PrimaryVariables(0)))::value, std::function<std::string(
int,
int)>>
311 const auto pvName = getParamFromGroup<std::vector<std::string>>(paramGroup,
"LoadSolution.PriVarNames");
312 return [n = std::move(pvName)](
int pvIdx,
int state = 0){
return n[pvIdx]; };
315 return [](
int pvIdx,
int state = 0){
return IOFields::template primaryVariableName<ModelTraits, FluidSystem, SolidSystem>(pvIdx, state); };
328template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
330 const std::string& fileName,
331 PvNameFunc&& targetPvNameFunc,
332 const GridGeometry& gridGeometry)
334 const auto extension = fileName.substr(fileName.find_last_of(
".") + 1);
335 auto dataType = GridGeometry::discMethod == DiscretizationMethod::box ?
336 VTKReader::DataType::pointData : VTKReader::DataType::cellData;
338 if (extension ==
"vtu" || extension ==
"vtp")
340 if (GridGeometry::discMethod == DiscretizationMethod::staggered && extension ==
"vtp")
341 dataType = VTKReader::DataType::pointData;
345 else if (extension ==
"pvtu" || extension ==
"pvtp")
347 if (GridGeometry::discMethod == DiscretizationMethod::staggered)
348 DUNE_THROW(Dune::NotImplemented,
"reading staggered solution from a parallel vtk file");
353 DUNE_THROW(Dune::NotImplemented,
"loadSolution for file with extension " << extension);
356 if (gridGeometry.gridView().comm().size() > 1)
358 using GridView =
typename GridGeometry::GridView;
359 if (dataType == VTKReader::DataType::cellData)
362 dataHandle(sol, gridGeometry.elementMapper());
363 gridGeometry.gridView().communicate(dataHandle,
364 Dune::InteriorBorder_All_Interface,
365 Dune::ForwardCommunication);
370 dataHandle(sol, gridGeometry.vertexMapper());
371 gridGeometry.gridView().communicate(dataHandle,
372 Dune::InteriorBorder_All_Interface,
373 Dune::ForwardCommunication);
Type traits to be used with matrix types.
Type traits to be used with vector types.
A helper function for class member function introspection.
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:374
auto loadSolutionFromVtkFile(SolutionVector &sol, const std::string fileName, PvNameFunc &&targetPvNameFunc, 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:103
void loadSolution(SolutionVector &sol, const std::string &fileName, PvNameFunc &&targetPvNameFunc, const GridGeometry &gridGeometry)
load a solution vector from file
Definition: loadsolution.hh:329
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:278
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:57
LoadSolutionDataHandle(Container &container, const EntityMapper &mapper)
Definition: loadsolution.hh:60
bool contains(int dim, int cd) const
Definition: loadsolution.hh:66
std::size_t size(const EntityType &e) const
Definition: loadsolution.hh:74
void scatter(MessageBufferImp &buff, const EntityType &e, std::size_t n)
Definition: loadsolution.hh:85
void gather(MessageBufferImp &buff, const EntityType &e) const
Definition: loadsolution.hh:78
bool fixedSize(int dim, int cd) const
returns true if size per entity of given dim and codim is a constant
Definition: loadsolution.hh:70
A vtk file reader using tinyxml2 as xml backend.
Definition: vtkreader.hh:51
bool hasData(const std::string &name, const DataType &type) const
Reviews data from the vtk file to check if there is a data array with a specified name.
Definition: vtkreader.hh:84
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:107
DataType
The data array types.
Definition: vtkreader.hh:56