25#ifndef DUMUX_PRIMARY_VARIABLE_SWITCH_HH
26#define DUMUX_PRIMARY_VARIABLE_SWITCH_HH
30#include <dune/common/exceptions.hh>
31#include <dune/common/fvector.hh>
44 template<
typename... Args>
47 template<
typename... Args>
void reset(Args&&...) {}
48 template<
typename... Args>
bool wasSwitched(Args&&...)
const {
return false; }
49 template<
typename... Args>
bool update(Args&&...) {
return false; }
58template<
class Implementation>
73 void reset(
const std::size_t numDofs)
86 template<
class SolutionVector,
class Gr
idVariables,
class Problem>
88 GridVariables& gridVariables,
89 const Problem& problem,
90 const typename GridVariables::GridGeometry& gridGeometry)
92 bool switched =
false;
94 std::size_t countSwitched = 0;
95 for (
const auto& element : elements(gridGeometry.gridView()))
98 auto fvGeometry =
localView(gridGeometry);
99 fvGeometry.bindElement(element);
101 auto elemVolVars =
localView(gridVariables.curGridVolVars());
102 elemVolVars.bindElement(element, fvGeometry, curSol);
104 const auto curElemSol =
elementSolution(element, curSol, gridGeometry);
105 for (
auto&& scv : scvs(fvGeometry))
109 const auto dofIdxGlobal = scv.dofIndex();
115 auto& volVars = getVolVarAccess_(gridVariables.curGridVolVars(), elemVolVars, scv);
116 volVars.update(curElemSol, problem, element, scv);
118 if (
asImp_().
update_(curSol[dofIdxGlobal], volVars, dofIdxGlobal, scv.dofPosition()))
127 if (verbosity_ > 0 && countSwitched > 0)
128 std::cout <<
"Switched primary variables at " << countSwitched <<
" dof locations on processor "
129 << gridGeometry.gridView().comm().rank() <<
"." << std::endl;
133 if (gridGeometry.gridView().comm().size() > 1)
134 switched = gridGeometry.gridView().comm().max(switched);
145 template<
class Problem,
class Gr
idVariables,
class SolutionVector>
147 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
148 const typename GridVariables::GridGeometry& gridGeometry,
149 GridVariables& gridVariables,
150 const SolutionVector& sol)
152 if constexpr (GridVariables::GridVolumeVariables::cachingEnabled)
155 auto fvGeometry =
localView(gridGeometry);
156 fvGeometry.bindElement(element);
159 for (
auto&& scv : scvs(fvGeometry))
161 const auto dofIdxGlobal = scv.dofIndex();
165 auto& volVars = gridVariables.curGridVolVars().volVars(scv);
166 volVars.update(elemSol, problem, element, scv);
178 template<
class Problem,
class Gr
idVariables,
class SolutionVector>
180 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
181 const typename GridVariables::GridGeometry& gridGeometry,
182 GridVariables& gridVariables,
183 const SolutionVector& sol)
185 if constexpr (GridVariables::GridFluxVariablesCache::cachingEnabled
189 const auto dofIdxGlobal = gridGeometry.dofMapper().index(element);
191 if (
asImp_().wasSwitched(dofIdxGlobal))
194 auto fvGeometry =
localView(gridGeometry);
195 fvGeometry.bind(element);
196 auto curElemVolVars =
localView(gridVariables.curGridVolVars());
197 curElemVolVars.bind(element, fvGeometry, sol);
198 gridVariables.gridFluxVarsCache().updateElement(element, fvGeometry, curElemVolVars);
208 template<
class Problem,
class Gr
idVariables,
class SolutionVector>
210 const typename GridVariables::GridGeometry& gridGeometry,
211 GridVariables& gridVariables,
216 std::vector<bool> stateChanged(sol.size(),
false);
217 std::size_t countChanged = 0;
219 for (
const auto& element : elements(gridGeometry.gridView()))
221 auto fvGeometry =
localView(gridGeometry);
222 fvGeometry.bindElement(element);
225 if (!fvGeometry.hasBoundaryScvf())
228 auto elemVolVars =
localView(gridVariables.curGridVolVars());
229 elemVolVars.bindElement(element, fvGeometry, sol);
231 for (
const auto& scv : scvs(fvGeometry))
234 const auto dofIdx = scv.dofIndex();
235 if (!gridGeometry.dofOnBoundary(dofIdx) || stateChanged[dofIdx])
238 const auto bcTypes = problem.boundaryTypes(element, scv);
239 if (bcTypes.hasDirichlet())
241 const auto dirichletValues = problem.dirichlet(element, scv);
243 if (sol[dofIdx].state() != dirichletValues.state())
246 std::cout <<
"Changing primary variable state at boundary (" << sol[dofIdx].state()
247 <<
") to the one given by the Dirichlet condition (" << dirichletValues.state() <<
") at dof " << dofIdx
248 <<
", coordinates: " << scv.dofPosition()
252 sol[dofIdx].setState(dirichletValues.state());
253 stateChanged[dofIdx] =
true;
257 for (
int eqIdx = 0; eqIdx < SolutionVector::block_type::dimension; ++eqIdx)
259 if (bcTypes.isDirichlet(eqIdx))
261 const auto pvIdx = bcTypes.eqToDirichletIndex(eqIdx);
262 sol[dofIdx][pvIdx] = dirichletValues[pvIdx];
270 if constexpr (GridVariables::GridVolumeVariables::cachingEnabled)
272 if (countChanged > 0)
275 for (
const auto& scv : scvs(fvGeometry))
277 if (stateChanged[scv.dofIndex()])
279 auto& volVars = getVolVarAccess_(gridVariables.curGridVolVars(), elemVolVars, scv);
280 volVars.update(curElemSol, problem, element, scv);
287 if (verbosity_ > 0 && countChanged > 0)
288 std::cout <<
"Changed primary variable states and solution values at boundary to Dirichlet states and values at " << countChanged <<
" dof locations on processor "
289 << gridGeometry.gridView().comm().rank() <<
"." << std::endl;
295 {
return verbosity_; }
301 {
return *
static_cast<Implementation*
>(
this); }
305 {
return *
static_cast<const Implementation*
>(
this); }
308 template<
class VolumeVariables,
class GlobalPosition>
309 bool update_(
typename VolumeVariables::PrimaryVariables& priVars,
310 const VolumeVariables& volVars,
311 std::size_t dofIdxGlobal,
312 const GlobalPosition& globalPos)
316 DUNE_THROW(Dune::NotImplemented,
"This model seems to use a primary variable switch but none is implemented!");
320 template<
class Geometry,
class Problem>
321 bool skipDof_(
const typename Geometry::GridGeometry::GridView::template Codim<0>::Entity& element,
322 const Geometry& fvGeometry,
323 const typename Geometry::SubControlVolume& scv,
324 const Problem& problem)
335 template<
class Geometry,
class Problem>
336 bool isConstrainedDof_(
const typename Geometry::GridGeometry::GridView::template Codim<0>::Entity& element,
337 const Geometry& fvGeometry,
338 const typename Geometry::SubControlVolume& scv,
339 const Problem& problem)
346 if (!fvGeometry.hasBoundaryScvf())
349 const auto dofIdx = scv.dofIndex();
350 if (!fvGeometry.gridGeometry().dofOnBoundary(dofIdx))
353 const auto bcTypes = problem.boundaryTypes(element, scv);
354 if (bcTypes.hasDirichlet())
365 template<
class Gr
idVolumeVariables,
class ElementVolumeVariables,
class SubControlVolume>
366 typename GridVolumeVariables::VolumeVariables&
367 getVolVarAccess_(GridVolumeVariables& gridVolVars, ElementVolumeVariables& elemVolVars,
const SubControlVolume& scv)
const
369 if constexpr (GridVolumeVariables::cachingEnabled)
370 return gridVolVars.volVars(scv);
372 return elemVolVars[scv];
Element solution classes and factory functions.
The available discretization methods in Dumux.
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:38
auto elementSolution(const Element &element, const SolutionVector &sol, const GridGeometry &gg) -> std::enable_if_t< GridGeometry::discMethod==DiscretizationMethod::box, BoxElementSolution< typename GridGeometry::LocalView, std::decay_t< decltype(std::declval< SolutionVector >()[0])> > >
Make an element solution for box schemes.
Definition: box/elementsolution.hh:115
Empty class for models without pri var switch.
Definition: compositional/primaryvariableswitch.hh:42
void reset(Args &&...)
Definition: compositional/primaryvariableswitch.hh:47
void updateSwitchedVolVars(Args &&...)
Definition: compositional/primaryvariableswitch.hh:50
NoPrimaryVariableSwitch(Args &&...)
Definition: compositional/primaryvariableswitch.hh:45
bool wasSwitched(Args &&...) const
Definition: compositional/primaryvariableswitch.hh:48
bool update(Args &&...)
Definition: compositional/primaryvariableswitch.hh:49
void updateSwitchedFluxVarsCache(Args &&...)
Definition: compositional/primaryvariableswitch.hh:51
The primary variable switch controlling the phase presence state variable.
Definition: compositional/primaryvariableswitch.hh:60
bool wasSwitched(std::size_t dofIdxGlobal) const
If the primary variables were recently switched.
Definition: compositional/primaryvariableswitch.hh:67
std::vector< bool > wasSwitched_
Definition: compositional/primaryvariableswitch.hh:361
void updateSwitchedVolVars(const Problem &problem, const typename GridVariables::GridGeometry::GridView::template Codim< 0 >::Entity &element, const typename GridVariables::GridGeometry &gridGeometry, GridVariables &gridVariables, const SolutionVector &sol)
Updates the volume variables whose primary variables were switched.
Definition: compositional/primaryvariableswitch.hh:146
bool isConstrainedDof_(const typename Geometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const Geometry &fvGeometry, const typename Geometry::SubControlVolume &scv, const Problem &problem)
Definition: compositional/primaryvariableswitch.hh:336
void updateBoundary(const Problem &problem, const typename GridVariables::GridGeometry &gridGeometry, GridVariables &gridVariables, SolutionVector &sol)
Updates the the primary variables state at the boundary.
Definition: compositional/primaryvariableswitch.hh:209
PrimaryVariableSwitch(int verbosity=1)
Definition: compositional/primaryvariableswitch.hh:62
Implementation & asImp_()
Return actual implementation (static polymorphism)
Definition: compositional/primaryvariableswitch.hh:300
bool skipDof_(const typename Geometry::GridGeometry::GridView::template Codim< 0 >::Entity &element, const Geometry &fvGeometry, const typename Geometry::SubControlVolume &scv, const Problem &problem)
Definition: compositional/primaryvariableswitch.hh:321
const Implementation & asImp_() const
Return actual implementation (static polymorphism)
Definition: compositional/primaryvariableswitch.hh:304
bool update(SolutionVector &curSol, GridVariables &gridVariables, const Problem &problem, const typename GridVariables::GridGeometry &gridGeometry)
Updates the variable switch / phase presence.
Definition: compositional/primaryvariableswitch.hh:87
int verbosity() const
The verbosity level.
Definition: compositional/primaryvariableswitch.hh:294
void reset(const std::size_t numDofs)
Reset all flags.
Definition: compositional/primaryvariableswitch.hh:73
bool update_(typename VolumeVariables::PrimaryVariables &priVars, const VolumeVariables &volVars, std::size_t dofIdxGlobal, const GlobalPosition &globalPos)
Definition: compositional/primaryvariableswitch.hh:309
std::vector< bool > visited_
Definition: compositional/primaryvariableswitch.hh:362
void updateSwitchedFluxVarsCache(const Problem &problem, const typename GridVariables::GridGeometry::GridView::template Codim< 0 >::Entity &element, const typename GridVariables::GridGeometry &gridGeometry, GridVariables &gridVariables, const SolutionVector &sol)
Updates the fluxVars cache for dof whose primary variables were switched.
Definition: compositional/primaryvariableswitch.hh:179