Commit 9227f850 authored by Timo Koch's avatar Timo Koch
Browse files

[io][loadsolution] Make privarnames functions consistent

* We require the signature std::string(int, int) signature for all pv
names functions passed to load solution. You can still wrap a function
that just takes a privar index into a lambda with these two arguments.

* We make FluidSystem an optional template parameter for all functions
that don't make use of it. This way they can be also used directly in
loadSolution when not using the convenience free function pvNames.

* We return a function from the convenience free function now to be able
to include the param group
parent 46acf605
......@@ -109,7 +109,7 @@ struct NavierStokesNCModelTraits : NavierStokesModelTraits<dimension>
{
const std::string xString = useMoles() ? "x" : "X";
if (pvIdx == 0)
return NavierStokesModelTraits<dimension>::primaryVariableNameCell(pvIdx, state);
return NavierStokesModelTraits<dimension>::template primaryVariableNameCell<FluidSystem>(pvIdx, state);
else
return xString + "^" + FluidSystem::componentName(pvIdx)
+ "_" + FluidSystem::phaseName(0);
......
......@@ -105,13 +105,15 @@ struct NavierStokesModelTraits
using Indices = NavierStokesIndices<dim()>;
//! return the names of the primary variables in cells
static std::string primaryVariableNameCell(int pvIdx = 0)
template <class FluidSystem = void>
static std::string primaryVariableNameCell(int pvIdx = 0, int state = 0)
{
return "p";
}
//! return the names of the primary variables on faces
static std::string primaryVariableNameFace(int pvIdx = 0)
template <class FluidSystem = void>
static std::string primaryVariableNameFace(int pvIdx = 0, int state = 0)
{
return "v";
}
......
......@@ -63,15 +63,7 @@ struct FreeflowNIModelTraits : public IsothermalTraits
//! the indices
using Indices = FreeflowNonIsothermalIndices<typename IsothermalTraits::Indices, numEq()>;
static std::string primaryVariableName(int pvIdx, int state = 0)
{
if (pvIdx < numEq() - 1)
return IsothermalTraits::primaryVariableName(pvIdx, state);
else
return "T";
}
template <class FluidSystem>
template <class FluidSystem = void>
static std::string primaryVariableName(int pvIdx, int state = 0)
{
if (pvIdx < numEq() - 1)
......
......@@ -114,11 +114,12 @@ struct OneEqModelTraits : RANSModelTraits<dimension>
using Indices = OneEqIndices<dim(), numComponents()>;
//! return the names of the primary variables in cells
template <class FluidSystem = void>
static std::string primaryVariableNameCell(int pvIdx, int state = 0)
{
using ParentType = RANSModelTraits<dimension>;
if (pvIdx == 0)
return ParentType::primaryVariableNameCell(pvIdx, state);
return ParentType::template primaryVariableNameCell<FluidSystem>(pvIdx, state);
else
return "nu_tilde";
}
......
......@@ -101,12 +101,13 @@ struct KEpsilonModelTraits : RANSModelTraits<dimension>
using Indices = KEpsilonIndices<dim(), numComponents()>;
//! return the names of the primary variables in cells
template<class FluidSystem = void>
static std::string primaryVariableNameCell(int pvIdx, int state = 0)
{
using ParentType = RANSModelTraits<dimension>;
switch (pvIdx) {
case 0:
return ParentType::primaryVariableNameCell(pvIdx, state);
return ParentType::template primaryVariableNameCell<FluidSystem>(pvIdx, state);
case 1:
return "k";
default:
......
......@@ -108,12 +108,13 @@ struct KOmegaModelTraits : RANSModelTraits<dimension>
using Indices = KOmegaIndices<dim(), numComponents()>;
//! return the names of the primary variables in cells
template<class FluidSystem = void>
static std::string primaryVariableNameCell(int pvIdx, int state = 0)
{
using ParentType = RANSModelTraits<dimension>;
switch (pvIdx) {
case 0:
return ParentType::primaryVariableNameCell(pvIdx, state);
return ParentType::template primaryVariableNameCell<FluidSystem>(pvIdx, state);
case 1:
return "k";
default:
......
......@@ -117,12 +117,13 @@ struct LowReKEpsilonModelTraits : RANSModelTraits<dimension>
using Indices = LowReKEpsilonIndices<dim(), numComponents()>;
//! return the names of the primary variables in cells
template<class FluidSystem = void>
static std::string primaryVariableNameCell(int pvIdx, int state = 0)
{
using ParentType = RANSModelTraits<dimension>;
switch (pvIdx) {
case 0:
return ParentType::primaryVariableNameCell(pvIdx, state);
return ParentType::template primaryVariableNameCell<FluidSystem>(pvIdx, state);
case 1:
return "k";
default:
......
......@@ -30,6 +30,7 @@
#include <unordered_set>
#include <unordered_map>
#include <type_traits>
#include <functional>
#include <dune/common/exceptions.hh>
#include <dune/common/indices.hh>
......@@ -121,10 +122,8 @@ auto loadSolutionFromVtkFile(SolutionVector& sol,
for (size_t pvIdx = 0; pvIdx < PrimaryVariables::dimension; ++pvIdx)
{
const auto pvName = pvNameFunc(pvIdx);
const auto pvName = pvNameFunc(pvIdx, 0);
auto vec = vtu.readData<std::vector<Scalar>>(pvName, dataType);
if (vec.size() != sol.size())
DUNE_THROW(Dune::IOError, "Size mismatch between solution vector and read data (" << sol.size() << " != " << vec.size() << ")");
if (dataType == VTKReader::DataType::cellData)
{
......@@ -190,25 +189,9 @@ auto loadSolutionFromVtkFile(SolutionVector& sol,
using Scalar = typename PrimaryVariables::field_type;
for (size_t pvIdx = 0; pvIdx < PrimaryVariables::dimension; ++pvIdx)
{
// check if the primary variable is state invariant
bool isStateInvariant = true;
for (const auto& state : states)
isStateInvariant = isStateInvariant && pvNameFunc(pvIdx, state) == pvNameFunc(pvIdx, *states.begin());
std::unordered_map<int, std::vector<Scalar>> data;
if (isStateInvariant)
data[0] = vtu.readData<std::vector<Scalar>>(pvNameFunc(pvIdx, *states.begin()), dataType);
// the primary variable changes with the state
// read the data for all occuring states from the file
else
for (const auto& state : states)
data[state] = vtu.readData<std::vector<Scalar>>(pvNameFunc(pvIdx, state), dataType);
// sanity check
for (const auto& d : data)
if (d.second.size() != sol.size())
DUNE_THROW(Dune::IOError, "Size mismatch between solution vector and read data (" << sol.size() << " != " << d.second.size() << ")");
for (const auto& state : states)
data[state] = vtu.readData<std::vector<Scalar>>(pvNameFunc(pvIdx, state), dataType);
if (dataType == VTKReader::DataType::cellData)
{
......@@ -216,9 +199,9 @@ auto loadSolutionFromVtkFile(SolutionVector& sol,
for (const auto& element : elements(fvGridGeometry.gridView(), Dune::Partitions::interior))
{
const auto eIdx = fvGridGeometry.elementMapper().index(element);
const auto state = isStateInvariant ? 0 : stateAtDof[i];
const auto state = stateAtDof[i];
sol[eIdx][pvIdx] = data[state][i++];
sol[eIdx][pvIdx].setState(state);
sol[eIdx].setState(state);
}
}
else
......@@ -233,9 +216,9 @@ auto loadSolutionFromVtkFile(SolutionVector& sol,
const auto vIdxGlobal = fvGridGeometry.vertexMapper().subIndex(element, vIdxLocal, dim);
if (!visited[vIdxGlobal])
{
const auto state = isStateInvariant ? 0 : stateAtDof[i];
const auto state = stateAtDof[i];
sol[vIdxGlobal][pvIdx] = data[state][i++];
sol[vIdxGlobal][pvIdx].setState(state);
sol[vIdxGlobal].setState(state);
visited[vIdxGlobal] = true;
}
}
......@@ -249,24 +232,26 @@ auto loadSolutionFromVtkFile(SolutionVector& sol,
* \brief helper function to determine the primary variable names of a model with privar state
* \note use this as input for the load solution function
*/
template<class ModelTraits, class FluidSystem>
std::string pvNameWithState(int pvIdx, int state, const std::string& paramGroup = "")
template<class ModelTraits, class FluidSystem = void>
std::function<std::string(int,int)> createPVNameFunctionWithState(const std::string& paramGroup = "")
{
static auto numStates = (1 << ModelTraits::numPhases()) - 1;
const auto paramNameWithState = "LoadSolution.PriVarNamesState" + std::to_string(state);
if (hasParamInGroup(paramGroup, "LoadSolution.PriVarNames") && !hasParamInGroup(paramGroup, paramNameWithState))
DUNE_THROW(Dune::NotImplemented, "please provide LoadSolution.PriVarNamesState1..." << numStates
<< " or remove LoadSolution.PriVarNames to use the model's default primary variable names");
return [paramGroup](int pvIdx, int state = 0)
{
static auto numStates = (1 << ModelTraits::numPhases()) - 1;
const auto paramNameWithState = "LoadSolution.PriVarNamesState" + std::to_string(state);
else if (hasParamInGroup(paramGroup, paramNameWithState))
{
static const auto pvNames = getParamFromGroup<std::vector<std::string>>(paramGroup, paramNameWithState);
return pvNames[pvIdx];
}
if (hasParamInGroup(paramGroup, "LoadSolution.PriVarNames") && !hasParamInGroup(paramGroup, paramNameWithState))
DUNE_THROW(Dune::NotImplemented, "please provide LoadSolution.PriVarNamesState1..." << numStates
<< " or remove LoadSolution.PriVarNames to use the model's default primary variable names");
else
return ModelTraits::template primaryVariableName<FluidSystem>(pvIdx, state);
else if (hasParamInGroup(paramGroup, paramNameWithState))
{
const auto pvName = getParamFromGroup<std::vector<std::string>>(paramGroup, paramNameWithState);
return pvName[pvIdx];
}
else
return ModelTraits::template primaryVariableName<FluidSystem>(pvIdx, state);
};
}
/*!
......@@ -274,16 +259,16 @@ std::string pvNameWithState(int pvIdx, int state, const std::string& paramGroup
* \brief helper function to determine the primary variable names of a model without state
* \note use this as input for the load solution function
*/
template<class ModelTraits>
std::string pvName(int pvIdx, int state, const std::string& paramGroup = "")
template<class ModelTraits, class FluidSystem = void>
std::function<std::string(int,int)> createPVNameFunction(const std::string& paramGroup = "")
{
if (hasParamInGroup(paramGroup, "LoadSolution.PriVarNames"))
{
static const auto pvNames = getParamFromGroup<std::vector<std::string>>(paramGroup, "LoadSolution.PriVarNames");
return pvNames[pvIdx];
const auto pvName = getParamFromGroup<std::vector<std::string>>(paramGroup, "LoadSolution.PriVarNames");
return [n = std::move(pvName)](int pvIdx, int state = 0){ return n[pvIdx]; };
}
else
return ModelTraits::primaryVariableName(pvIdx, 0);
return [](int pvIdx, int state = 0){ return ModelTraits::template primaryVariableName<FluidSystem>(pvIdx, state); };
}
/*!
......
......@@ -75,6 +75,7 @@ struct OnePModelTraits
static constexpr bool enableMolecularDiffusion() { return false; }
static constexpr bool enableEnergyBalance() { return false; }
template <class FluidSystem = void>
static std::string primaryVariableName(int pvIdx = 0, int state = 0)
{
return "p";
......
......@@ -99,6 +99,7 @@ struct TwoPModelTraits
static constexpr bool enableMolecularDiffusion() { return false; }
static constexpr bool enableEnergyBalance() { return false; }
template <class FluidSystem = void>
static std::string primaryVariableName(int pvIdx, int state = 0)
{
if (priVarFormulation() == TwoPFormulation::p0s1)
......
......@@ -96,7 +96,7 @@ struct TwoPOneCModelTraits
static constexpr bool enableMolecularDiffusion() { return false; }
static constexpr bool enableEnergyBalance() { return false; }
template <class FluidSystem>
template <class FluidSystem = void>
static std::string primaryVariableName(int pvIdx, int state)
{
if (pvIdx == 0)
......
......@@ -90,15 +90,14 @@ struct ThreePModelTraits
static constexpr bool enableMolecularDiffusion() { return false; }
static constexpr bool enableEnergyBalance() { return false; }
template <class FluidSystem = void>
static std::string primaryVariableName(int pvIdx, int state = 0)
{
switch (pvIdx) {
case 0:
return "p_g";
case 1:
return "S_w";
default:
return "S_n";
switch (pvIdx)
{
case 0: return "p_g";
case 1: return "S_w";
default: return "S_n";
}
}
};
......
......@@ -123,9 +123,10 @@ struct ThreePThreeCModelTraits
static constexpr bool useMoles() { return useMol; }
template <class FluidSystem>
static std::string primaryVariableName(int pvIdx, int state = 0)
static std::string primaryVariableName(int pvIdx, int state)
{
switch (state) {
switch (state)
{
case Indices::threePhases:
const std::vector<std::string> s1 = {"p_g",
"S_w",
......
......@@ -113,9 +113,10 @@ struct ThreePWaterOilModelTraits
static constexpr bool onlyGasPhaseCanDisappear() { return onlyGasPhase; }
template <class FluidSystem>
static std::string primaryVariableName(int pvIdx, int state = 0)
static std::string primaryVariableName(int pvIdx, int state)
{
switch (state) {
switch (state)
{
case Indices::threePhases:
const std::vector<std::string> s1 = {"p_g",
"S_w",
......
......@@ -75,15 +75,7 @@ struct PorousMediumFlowNIModelTraits : public IsothermalTraits
//! The indices related to the non-isothermal model
using Indices = EnergyIndices< typename IsothermalTraits::Indices, numEq()>;
static std::string primaryVariableName(int pvIdx, int state = 0)
{
if (pvIdx < numEq() - 1)
return IsothermalTraits::primaryVariableName(pvIdx, state);
else
return "T";
}
template <class FluidSystem>
template <class FluidSystem = void>
static std::string primaryVariableName(int pvIdx, int state = 0)
{
if (pvIdx < numEq() - 1)
......
......@@ -136,6 +136,7 @@ struct RichardsModelTraits
static constexpr bool enableMolecularDiffusion() { return enableDiff; }
static constexpr bool enableEnergyBalance() { return false; }
template<class FluidSystem = void>
static std::string primaryVariableName(int pvIdx, int state)
{
if (state == Indices::gasPhaseOnly)
......
......@@ -16,8 +16,6 @@
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#ifndef DUMUX_TRACER_MODEL_HH
#define DUMUX_TRACER_MODEL_HH
/*!
* \file
......@@ -50,6 +48,9 @@
* The velocity output is fully compatible with the tracer model if you want to write the velocity field to vtk.
*/
#ifndef DUMUX_TRACER_MODEL_HH
#define DUMUX_TRACER_MODEL_HH
#include <dumux/common/properties.hh>
#include <dumux/material/spatialparams/fv1p.hh>
#include <dumux/discretization/stationaryvelocityfield.hh>
......
......@@ -145,7 +145,7 @@ int main(int argc, char** argv) try
auto fileNameCell = getParamFromGroup<std::string>("CellCenter", "Restart.File");
loadSolution(x[FVGridGeometry::cellCenterIdx()], fileNameCell,
[](int pvIdx){ return "p"; }, // test option with lambda
[](int pvIdx, int state){ return "p"; }, // test option with lambda
*fvGridGeometry);
auto fileNameFace = getParamFromGroup<std::string>("Face", "Restart.File");
......
......@@ -133,7 +133,7 @@ int main(int argc, char** argv) try
{
using ModelTraits = typename GET_PROP_TYPE(TypeTag, ModelTraits);
const auto fileName = getParam<std::string>("Restart.File");
loadSolution(x, fileName, primaryVariableName<ModelTraits>, *fvGridGeometry);
loadSolution(x, fileName, createPVNameFunction<ModelTraits>(), *fvGridGeometry);
}
else
problem->applyInitialSolution(x);
......
......@@ -138,7 +138,7 @@ int main(int argc, char** argv) try
{
using ModelTraits = typename GET_PROP_TYPE(TypeTag, ModelTraits);
const auto fileName = getParam<std::string>("Restart.File");
loadSolution(x, fileName, primaryVariableName<ModelTraits>, *fvGridGeometry);
loadSolution(x, fileName, createPVNameFunction<ModelTraits>(), *fvGridGeometry);
}
else
problem->applyInitialSolution(x);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment