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))
107 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 GridVariables,
class SolutionVector,
146 std::enable_if_t<GridVariables::GridVolumeVariables::cachingEnabled, int> = 0>
148 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
149 const typename GridVariables::GridGeometry& gridGeometry,
150 GridVariables& gridVariables,
151 const SolutionVector& sol)
154 auto fvGeometry =
localView(gridGeometry);
155 fvGeometry.bindElement(element);
158 for (
auto&& scv : scvs(fvGeometry))
160 const auto dofIdxGlobal = scv.dofIndex();
164 auto& volVars = gridVariables.curGridVolVars().volVars(scv);
165 volVars.update(elemSol, problem, element, scv);
176 template<
class Problem,
class GridVariables,
class SolutionVector,
177 std::enable_if_t<(GridVariables::GridFluxVariablesCache::cachingEnabled &&
180 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
181 const typename GridVariables::GridGeometry& gridGeometry,
182 GridVariables& gridVariables,
183 const SolutionVector& sol)
186 const auto dofIdxGlobal = gridGeometry.dofMapper().index(element);
188 if (
asImp_().wasSwitched(dofIdxGlobal))
191 auto fvGeometry =
localView(gridGeometry);
192 fvGeometry.bind(element);
193 auto curElemVolVars =
localView(gridVariables.curGridVolVars());
194 curElemVolVars.bind(element, fvGeometry, sol);
195 gridVariables.gridFluxVarsCache().updateElement(element, fvGeometry, curElemVolVars);
204 template<
class Problem,
class GridVariables,
class SolutionVector,
207 const typename GridVariables::GridGeometry& gridGeometry,
208 GridVariables& gridVariables,
211 std::vector<bool> stateChanged(sol.size(),
false);
212 std::size_t countChanged = 0;
214 for (
const auto& element : elements(gridGeometry.gridView()))
216 auto fvGeometry =
localView(gridGeometry);
217 fvGeometry.bindElement(element);
220 if (!fvGeometry.hasBoundaryScvf())
223 auto elemVolVars =
localView(gridVariables.curGridVolVars());
224 elemVolVars.bindElement(element, fvGeometry, sol);
226 for (
const auto& scv : scvs(fvGeometry))
229 const auto dofIdx = scv.dofIndex();
230 if (!gridGeometry.dofOnBoundary(dofIdx) || stateChanged[dofIdx])
233 const auto bcTypes = problem.boundaryTypes(element, scv);
234 if (bcTypes.hasDirichlet())
236 const auto dirichletValues = problem.dirichlet(element, scv);
238 if (sol[dofIdx].state() != dirichletValues.state())
241 std::cout <<
"Changing primary variable state at boundary (" << sol[dofIdx].state()
242 <<
") to the one given by the Dirichlet condition (" << dirichletValues.state() <<
") at dof " << dofIdx
243 <<
", coordinates: " << scv.dofPosition()
247 sol[dofIdx].setState(dirichletValues.state());
248 stateChanged[dofIdx] =
true;
252 for (
int eqIdx = 0; eqIdx < SolutionVector::block_type::dimension; ++eqIdx)
254 if (bcTypes.isDirichlet(eqIdx))
256 const auto pvIdx = bcTypes.eqToDirichletIndex(eqIdx);
257 sol[dofIdx][pvIdx] = dirichletValues[pvIdx];
265 if (GridVariables::GridVolumeVariables::cachingEnabled && countChanged > 0)
268 for (
const auto& scv : scvs(fvGeometry))
270 if (stateChanged[scv.dofIndex()])
272 auto& volVars = getVolVarAccess(gridVariables.curGridVolVars(), elemVolVars, scv);
273 volVars.update(curElemSol, problem, element, scv);
279 if (verbosity_ > 0 && countChanged > 0)
280 std::cout <<
"Changed primary variable states and solution values at boundary to Dirichlet states and values at " << countChanged <<
" dof locations on processor "
281 << gridGeometry.gridView().comm().rank() <<
"." << std::endl;
285 template<
class Problem,
class GridVariables,
class SolutionVector,
286 std::enable_if_t<!GridVariables::GridVolumeVariables::cachingEnabled, int> = 0>
288 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
289 const typename GridVariables::GridGeometry& gridGeometry,
290 GridVariables& gridVariables,
291 const SolutionVector &uCurrentIter)
const {}
294 template<
class Problem,
class GridVariables,
class SolutionVector,
295 std::enable_if_t<(!GridVariables::GridFluxVariablesCache::cachingEnabled ||
298 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
299 const typename GridVariables::GridGeometry& gridGeometry,
300 GridVariables& gridVariables,
301 const SolutionVector& sol)
const {}
304 template<
class Problem,
class GridVariables,
class SolutionVector,
307 const typename GridVariables::GridGeometry& gridGeometry,
308 GridVariables& gridVariables,
309 SolutionVector& sol)
const {}
313 {
return verbosity_; }
319 {
return *
static_cast<Implementation*
>(
this); }
323 {
return *
static_cast<const Implementation*
>(
this); }
326 template<
class VolumeVariables,
class GlobalPosition>
327 bool update_(
typename VolumeVariables::PrimaryVariables& priVars,
328 const VolumeVariables& volVars,
329 std::size_t dofIdxGlobal,
330 const GlobalPosition& globalPos)
334 DUNE_THROW(Dune::NotImplemented,
"This model seems to use a primary variable switch but none is implemented!");
341 template<
class Gr
idVolumeVariables,
class ElementVolumeVariables,
class SubControlVolume>
342 static auto getVolVarAccess(GridVolumeVariables& gridVolVars, ElementVolumeVariables& elemVolVars,
const SubControlVolume& scv)
343 -> std::enable_if_t<!GridVolumeVariables::cachingEnabled,
decltype(elemVolVars[scv])>
344 {
return elemVolVars[scv]; }
346 template<
class Gr
idVolumeVariables,
class ElementVolumeVariables,
class SubControlVolume>
347 static auto getVolVarAccess(GridVolumeVariables& gridVolVars, ElementVolumeVariables& elemVolVars,
const SubControlVolume& scv)
348 -> std::enable_if_t<GridVolumeVariables::cachingEnabled,
decltype(gridVolVars.volVars(scv))>
349 {
return gridVolVars.volVars(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
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
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:337
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:147
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) const
Do nothing when flux variables are not cached globally or the box method is used.
Definition: compositional/primaryvariableswitch.hh:297
PrimaryVariableSwitch(int verbosity=1)
Definition: compositional/primaryvariableswitch.hh:62
Implementation & asImp_()
Return actual implementation (static polymorphism)
Definition: compositional/primaryvariableswitch.hh:318
void updateBoundary(const Problem &problem, const typename GridVariables::GridGeometry &gridGeometry, GridVariables &gridVariables, SolutionVector &sol) const
Do nothing when the box method is not used.
Definition: compositional/primaryvariableswitch.hh:306
const Implementation & asImp_() const
Return actual implementation (static polymorphism)
Definition: compositional/primaryvariableswitch.hh:322
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:206
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
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:312
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:327
std::vector< bool > visited_
Definition: compositional/primaryvariableswitch.hh:338
void updateSwitchedVolVars(const Problem &problem, const typename GridVariables::GridGeometry::GridView::template Codim< 0 >::Entity &element, const typename GridVariables::GridGeometry &gridGeometry, GridVariables &gridVariables, const SolutionVector &uCurrentIter) const
Do nothing when volume variables are not cached globally.
Definition: compositional/primaryvariableswitch.hh:287