12#ifndef DUMUX_IO_LOADSOLUTION_HH
13#define DUMUX_IO_LOADSOLUTION_HH
18#include <unordered_set>
19#include <unordered_map>
23#include <dune/common/exceptions.hh>
24#include <dune/common/indices.hh>
25#include <dune/grid/common/partitionset.hh>
42template <
class Container,
class EntityMapper,
int codim>
44:
public Dune::CommDataHandleIF< LoadSolutionDataHandle<Container, EntityMapper, codim>,
45 std::decay_t<decltype(std::declval<Container>()[0])> >
47 using FieldType = std::decay_t<decltype(std::declval<Container>()[0])>;
50 const EntityMapper& mapper)
52 , container_(container)
56 {
return cd == codim; }
62 template<
class EntityType>
63 std::size_t
size (
const EntityType &e)
const
66 template<
class MessageBufferImp,
class EntityType>
67 void gather(MessageBufferImp& buff,
const EntityType& e)
const
69 const auto vIdx = mapper_.index(e);
70 buff.write(container_[vIdx]);
73 template<
class MessageBufferImp,
class EntityType>
74 void scatter(MessageBufferImp& buff,
const EntityType& e, std::size_t n)
76 const auto vIdx = mapper_.index(e);
79 container_[vIdx] = tmp;
84 Container& container_;
91template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
93 const std::string fileName,
94 PvNameFunc&& targetPvNameFunc,
95 const GridGeometry& gridGeometry,
101 using PrimaryVariables =
typename SolutionVector::block_type;
102 using Scalar =
typename PrimaryVariables::field_type;
103 constexpr auto dim = GridGeometry::GridView::dimension;
104 const std::size_t targetSolutionSize = PrimaryVariables::dimension;
106 std::size_t matchingLoadedArrays = 0;
107 for (std::size_t i = 0; i < targetSolutionSize; i++)
108 if (vtu.
hasData(targetPvNameFunc(i,0), dataType))
109 matchingLoadedArrays++;
111 if (matchingLoadedArrays < targetSolutionSize)
112 std::cout <<
"The loaded solution does not provide a data array for each of the primary variables. \n"
113 <<
"The target solution has "<< targetSolutionSize <<
" entries, "
114 <<
"whereas the loaded solution provides only " << matchingLoadedArrays <<
" data array(s). \n"
115 <<
"Make sure that the model concepts are compatible, "
116 <<
"and be sure to provide initial conditions for the missing primary variables. \n";
118 for (std::size_t targetPvIdx = 0; targetPvIdx < targetSolutionSize; ++targetPvIdx)
120 std::vector<Scalar> vec;
121 const auto targetPvName = targetPvNameFunc(targetPvIdx, 0);
123 if (vtu.
hasData(targetPvName, dataType))
124 vec = vtu.
readData<std::vector<Scalar>>(targetPvName, dataType);
127 std::cout <<
"The loaded solution does not have a field named \"" << targetPvName <<
"\". "
128 <<
"Make sure this field is filled using the initial method in the problem definition. \n";
135 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
137 const auto eIdx = gridGeometry.elementMapper().index(element);
138 sol[eIdx][targetPvIdx] = vec[i++];
145 if (sol.size() != vec.size())
146 DUNE_THROW(Dune::InvalidStateException,
"Solution size (" << sol.size() <<
") does not match input size (" << vec.size() <<
")!");
148 for (std::size_t i = 0; i < sol.size(); ++i)
149 sol[i][targetPvIdx] = vec[i];
153 else if (
const auto extension = fileName.substr(fileName.find_last_of(
".") + 1); extension ==
"vti" || extension ==
"pvti")
156 for (
const auto& vertex : vertices(gridGeometry.gridView(), Dune::Partitions::interior + Dune::Partitions::border))
158 const auto vIdx = gridGeometry.vertexMapper().index(vertex);
159 sol[vIdx][targetPvIdx] = vec[i++];
167 std::vector<bool> visited(gridGeometry.gridView().size(dim),
false);
168 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
170 for (
int vIdxLocal = 0; vIdxLocal < element.subEntities(dim); ++vIdxLocal)
172 const auto vIdxGlobal = gridGeometry.vertexMapper().subIndex(element, vIdxLocal, dim);
173 if (!visited[vIdxGlobal])
175 sol[vIdxGlobal][targetPvIdx] = vec[i++];
176 visited[vIdxGlobal] =
true;
188template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
190 const std::string fileName,
191 PvNameFunc&& targetPvNameFunc,
192 const GridGeometry& gridGeometry,
194->
typename std::enable_if_t<
decltype(
isValid(Detail::hasState())(sol[0]))::value, void>
196 VTKReader vtu(fileName);
198 const auto stateAtDof = vtu.readData<std::vector<int>>(
"phase presence", dataType);
201 std::unordered_set<int> states;
202 for (std::size_t i = 0; i < stateAtDof.size(); ++i)
203 states.insert(stateAtDof[i]);
205 using PrimaryVariables =
typename SolutionVector::block_type;
206 using Scalar =
typename PrimaryVariables::field_type;
207 const std::size_t targetSolutionSize = PrimaryVariables::dimension;
209 std::unordered_set<std::string> matchingNames;
210 for (std::size_t i = 0; i < targetSolutionSize; i++)
211 for (
const auto& state : states)
212 if ( vtu.hasData(targetPvNameFunc(i,state), dataType))
213 matchingNames.insert(targetPvNameFunc(i,state));
215 const std::size_t matchingLoadedArrays = matchingNames.size() - (states.size()-1);
217 if (matchingLoadedArrays < targetSolutionSize)
218 std::cout <<
"The loaded solution does not provide a data array for each of the primary variables. \n"
219 <<
"The target solution has "<< targetSolutionSize <<
" entries, "
220 <<
"whereas the loaded solution provides only " << matchingLoadedArrays <<
" data array(s). \n"
221 <<
"Make sure that the model concepts are compatible, "
222 <<
"and be sure to provide initial conditions for the missing primary variables. \n";
224 for (std::size_t targetPvIdx = 0; targetPvIdx < targetSolutionSize; ++targetPvIdx)
226 std::unordered_map<int, std::vector<Scalar>> data;
227 for (
const auto& state : states)
229 const auto targetPvName = targetPvNameFunc(targetPvIdx, state);
231 if (vtu.hasData(targetPvName, dataType))
232 data[state] = vtu.readData<std::vector<Scalar>>(targetPvName, dataType);
235 std::cout <<
"Loaded Solution does not have a field named \"" << targetPvName <<
"\". "
236 <<
"Make sure this field is filled using the initial method in the problem definition. \n";
244 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
246 const auto eIdx = gridGeometry.elementMapper().index(element);
247 const auto state = stateAtDof[i];
248 sol[eIdx][targetPvIdx] = data[state][i++];
249 sol[eIdx].setState(state);
255 constexpr int dim = GridGeometry::GridView::dimension;
256 std::vector<bool> visited(gridGeometry.gridView().size(dim),
false);
257 for (
const auto& element : elements(gridGeometry.gridView(), Dune::Partitions::interior))
259 for (
int vIdxLocal = 0; vIdxLocal <
element.subEntities(dim); ++vIdxLocal)
261 const auto vIdxGlobal = gridGeometry.vertexMapper().subIndex(element, vIdxLocal, dim);
262 if (!visited[vIdxGlobal])
264 const auto state = stateAtDof[i];
265 sol[vIdxGlobal][targetPvIdx] = data[state][i++];
266 sol[vIdxGlobal].setState(state);
267 visited[vIdxGlobal] =
true;
280template<
class IOFields,
class PrimaryVariables,
class ModelTraits =
void,
class Flu
idSystem =
void,
class Sol
idSystem =
void>
282->
typename std::enable_if_t<
decltype(
isValid(
Detail::hasState())(PrimaryVariables(0)))::value, std::function<std::string(int,int)>>
284 return [paramGroup](
int pvIdx,
int state = 0)
286 static auto numStates = (1 << ModelTraits::numFluidPhases()) - 1;
287 const auto paramNameWithState =
"LoadSolution.PriVarNamesState" + std::to_string(state);
290 DUNE_THROW(Dune::NotImplemented,
"please provide LoadSolution.PriVarNamesState1..." << numStates
291 <<
" or remove LoadSolution.PriVarNames to use the model's default primary variable names");
295 const auto pvName = getParamFromGroup<std::vector<std::string>>(paramGroup, paramNameWithState);
296 return pvName[pvIdx];
299 return IOFields::template primaryVariableName<ModelTraits, FluidSystem, SolidSystem>(pvIdx, state);
308template<
class IOFields,
class PrimaryVariables,
class ModelTraits =
void,
class Flu
idSystem =
void,
class Sol
idSystem =
void>
310->
typename std::enable_if_t<!
decltype(
isValid(Detail::hasState())(PrimaryVariables(0)))::value, std::function<std::string(
int,
int)>>
314 const auto pvName = getParamFromGroup<std::vector<std::string>>(paramGroup,
"LoadSolution.PriVarNames");
315 return [n = std::move(pvName)](
int pvIdx,
int state = 0){
return n[pvIdx]; };
318 return [](
int pvIdx,
int state = 0){
return IOFields::template primaryVariableName<ModelTraits, FluidSystem, SolidSystem>(pvIdx, state); };
331template <
class SolutionVector,
class PvNameFunc,
class Gr
idGeometry>
333 const std::string& fileName,
334 PvNameFunc&& targetPvNameFunc,
335 const GridGeometry& gridGeometry)
337 const auto extension = fileName.substr(fileName.find_last_of(
".") + 1);
339 VTKReader::DataType::pointData : VTKReader::DataType::cellData;
341 if (extension ==
"vtu" || extension ==
"vtp" || extension ==
"vti")
344 dataType = VTKReader::DataType::pointData;
348 else if (extension ==
"pvtu" || extension ==
"pvtp" || extension ==
"pvti")
351 DUNE_THROW(Dune::NotImplemented,
"reading staggered solution from a parallel vtk file");
356 DUNE_THROW(Dune::NotImplemented,
"loadSolution for file with extension " << extension);
359 if (gridGeometry.gridView().comm().size() > 1)
361 using GridView =
typename GridGeometry::GridView;
362 if (dataType == VTKReader::DataType::cellData)
365 dataHandle(sol, gridGeometry.elementMapper());
367 if constexpr (Dune::Capabilities::canCommunicate<typename GridView::Traits::Grid, 0>::v)
368 gridGeometry.gridView().communicate(dataHandle,
369 Dune::InteriorBorder_All_Interface,
370 Dune::ForwardCommunication);
372 DUNE_THROW(Dune::InvalidStateException,
"Cannot call loadSolution on multiple processes for a grid that cannot communicate codim-" << 0 <<
"-entities.");
377 dataHandle(sol, gridGeometry.vertexMapper());
379 if constexpr (Dune::Capabilities::canCommunicate<typename GridView::Traits::Grid, GridView::dimension>::v)
380 gridGeometry.gridView().communicate(dataHandle,
381 Dune::InteriorBorder_All_Interface,
382 Dune::ForwardCommunication);
384 DUNE_THROW(Dune::InvalidStateException,
"Cannot call loadSolution on multiple processes for a grid that cannot communicate codim-" << GridView::dimension <<
"-entities.");
a data handle to communicate the solution on ghosts and overlaps when reading from vtk file in parall...
Definition: loadsolution.hh:46
LoadSolutionDataHandle(Container &container, const EntityMapper &mapper)
Definition: loadsolution.hh:49
bool contains(int dim, int cd) const
Definition: loadsolution.hh:55
std::size_t size(const EntityType &e) const
Definition: loadsolution.hh:63
void scatter(MessageBufferImp &buff, const EntityType &e, std::size_t n)
Definition: loadsolution.hh:74
void gather(MessageBufferImp &buff, const EntityType &e) const
Definition: loadsolution.hh:67
bool fixedSize(int dim, int cd) const
returns true if size per entity of given dim and codim is a constant
Definition: loadsolution.hh:59
A vtk file reader using tinyxml2 as xml backend.
Definition: vtkreader.hh:342
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:379
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:402
DataType
The data array types.
Definition: vtkreader.hh:347
dune-grid capabilities compatibility layer
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:165
constexpr auto isValid(const Expression &t)
A function that creates a test functor to do class member introspection at compile time.
Definition: isvalid.hh:81
A helper function for class member function introspection.
The available discretization methods in Dumux.
constexpr Box box
Definition: method.hh:147
constexpr Staggered staggered
Definition: method.hh:149
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Type traits to be used with matrix types.
helper struct detecting if a PrimaryVariables object has a state() function
Definition: state.hh:21
Type traits to be used with vector types.
A vtk file reader using tinyxml2 as xml backend.