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>
209 [[deprecated(
"Use updateDirichletConstraints() instead. Will be remove after 3.3")]]
211 const typename GridVariables::GridGeometry& gridGeometry,
212 GridVariables& gridVariables,
223 template<
class Problem,
class Gr
idVariables,
class SolutionVector>
225 const typename GridVariables::GridGeometry& gridGeometry,
226 GridVariables& gridVariables,
229 if constexpr (GridVariables::GridGeometry::discMethod ==
DiscretizationMethod::box || Problem::enableInternalDirichletConstraints())
231 std::vector<bool> stateChanged(sol.size(),
false);
232 std::size_t countChanged = 0;
234 for (
const auto& element : elements(gridGeometry.gridView()))
236 auto fvGeometry =
localView(gridGeometry);
237 fvGeometry.bindElement(element);
240 if (!Problem::enableInternalDirichletConstraints() && !fvGeometry.hasBoundaryScvf())
243 auto elemVolVars =
localView(gridVariables.curGridVolVars());
244 elemVolVars.bindElement(element, fvGeometry, sol);
246 for (
const auto& scv : scvs(fvGeometry))
248 const auto dofIdx = scv.dofIndex();
250 if (stateChanged[dofIdx])
255 if (gridGeometry.dofOnBoundary(dofIdx))
259 stateChanged[dofIdx] =
true;
266 if constexpr (Problem::enableInternalDirichletConstraints())
270 stateChanged[dofIdx] =
true;
277 if constexpr (GridVariables::GridVolumeVariables::cachingEnabled)
279 if (countChanged > 0)
282 for (
const auto& scv : scvs(fvGeometry))
284 if (stateChanged[scv.dofIndex()])
286 auto& volVars = getVolVarAccess_(gridVariables.curGridVolVars(), elemVolVars, scv);
287 volVars.update(curElemSol, problem, element, scv);
294 if (verbosity_ > 0 && countChanged > 0)
295 std::cout <<
"Changed primary variable states and solution values to Dirichlet states and values at " << countChanged <<
" dof locations on processor "
296 << gridGeometry.gridView().comm().rank() <<
"." << std::endl;
302 {
return verbosity_; }
308 {
return *
static_cast<Implementation*
>(
this); }
312 {
return *
static_cast<const Implementation*
>(
this); }
315 template<
class VolumeVariables,
class GlobalPosition>
316 bool update_(
typename VolumeVariables::PrimaryVariables& priVars,
317 const VolumeVariables& volVars,
318 std::size_t dofIdxGlobal,
319 const GlobalPosition& globalPos)
323 DUNE_THROW(Dune::NotImplemented,
"This model seems to use a primary variable switch but none is implemented!");
327 template<
class Geometry,
class Problem>
328 bool skipDof_(
const typename Geometry::GridGeometry::GridView::template Codim<0>::Entity& element,
329 const Geometry& fvGeometry,
330 const typename Geometry::SubControlVolume& scv,
331 const Problem& problem)
342 template<
class Geometry,
class Problem>
343 bool isConstrainedDof_(
const typename Geometry::GridGeometry::GridView::template Codim<0>::Entity& element,
344 const Geometry& fvGeometry,
345 const typename Geometry::SubControlVolume& scv,
346 const Problem& problem)
349 if constexpr (Geometry::GridGeometry::discMethod !=
DiscretizationMethod::box && !Problem::enableInternalDirichletConstraints())
353 const auto isInternallyConstrainedDof = [&]()
355 if constexpr (!Problem::enableInternalDirichletConstraints())
359 const auto internalDirichletConstraints = problem.hasInternalDirichletConstraint(element, scv);
360 return internalDirichletConstraints.any();
364 if (isInternallyConstrainedDof())
370 if (!fvGeometry.hasBoundaryScvf())
373 const auto dofIdx = scv.dofIndex();
374 if (!fvGeometry.gridGeometry().dofOnBoundary(dofIdx))
377 const auto bcTypes = problem.boundaryTypes(element, scv);
378 if (bcTypes.hasDirichlet())
385 template<
class Problem,
class Element,
class SubControlVolume,
class SolutionVector>
387 const Element& element,
388 const SubControlVolume& scv,
391 bool changed =
false;
392 const auto bcTypes = problem.boundaryTypes(element, scv);
393 if (bcTypes.hasDirichlet())
395 const auto dirichletValues = problem.dirichlet(element, scv);
396 const auto dofIdx = scv.dofIndex();
398 if (sol[dofIdx].state() != dirichletValues.state())
401 std::cout <<
"Changing primary variable state at boundary (" << sol[dofIdx].state()
402 <<
") to the one given by the Dirichlet condition (" << dirichletValues.state() <<
") at dof " << dofIdx
403 <<
", coordinates: " << scv.dofPosition()
407 sol[dofIdx].setState(dirichletValues.state());
411 for (
int eqIdx = 0; eqIdx < SolutionVector::block_type::dimension; ++eqIdx)
413 if (bcTypes.isDirichlet(eqIdx))
415 const auto pvIdx = bcTypes.eqToDirichletIndex(eqIdx);
416 sol[dofIdx][pvIdx] = dirichletValues[pvIdx];
425 template<
class Problem,
class Element,
class SubControlVolume,
class SolutionVector>
427 const Element& element,
428 const SubControlVolume& scv,
431 bool changed =
false;
433 const auto internalDirichletConstraints = problem.hasInternalDirichletConstraint(element, scv);
434 if (internalDirichletConstraints.none())
437 const auto dirichletValues = problem.internalDirichlet(element, scv);
438 const auto dofIdx = scv.dofIndex();
440 if (sol[dofIdx].state() != dirichletValues.state())
443 std::cout <<
"Changing primary variable state at internal DOF (" << sol[dofIdx].state()
444 <<
") to the one given by the internal Dirichlet constraint (" << dirichletValues.state() <<
") at dof " << dofIdx
445 <<
", coordinates: " << scv.dofPosition()
449 sol[dofIdx].setState(dirichletValues.state());
453 for (
int pvIdx = 0; pvIdx < SolutionVector::block_type::dimension; ++pvIdx)
455 if (internalDirichletConstraints[pvIdx])
456 sol[dofIdx][pvIdx] = dirichletValues[pvIdx];
467 template<
class Gr
idVolumeVariables,
class ElementVolumeVariables,
class SubControlVolume>
468 typename GridVolumeVariables::VolumeVariables&
469 getVolVarAccess_(GridVolumeVariables& gridVolVars, ElementVolumeVariables& elemVolVars,
const SubControlVolume& scv)
const
471 if constexpr (GridVolumeVariables::cachingEnabled)
472 return gridVolVars.volVars(scv);
474 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:463
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:343
bool handleDirichletBoundaryCondition_(const Problem &problem, const Element &element, const SubControlVolume &scv, SolutionVector &sol)
Definition: compositional/primaryvariableswitch.hh:386
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:210
PrimaryVariableSwitch(int verbosity=1)
Definition: compositional/primaryvariableswitch.hh:62
bool handleInternalDirichletConstraint_(const Problem &problem, const Element &element, const SubControlVolume &scv, SolutionVector &sol)
Definition: compositional/primaryvariableswitch.hh:426
Implementation & asImp_()
Return actual implementation (static polymorphism)
Definition: compositional/primaryvariableswitch.hh:307
void updateDirichletConstraints(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:224
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:328
const Implementation & asImp_() const
Return actual implementation (static polymorphism)
Definition: compositional/primaryvariableswitch.hh:311
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:301
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:316
std::vector< bool > visited_
Definition: compositional/primaryvariableswitch.hh:464
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