24#ifndef DUMUX_PNM_2P_INVASIONSTATE_HH
25#define DUMUX_PNM_2P_INVASIONSTATE_HH
29#include <dune/common/std/type_traits.hh>
45 using GlobalCapillaryPressureDetector =
decltype(std::declval<T>().globalCapillaryPressure());
48 static constexpr bool hasGlobalCapillaryPressure()
49 {
return Dune::Std::is_detected<GlobalCapillaryPressureDetector, T>::value; }
51 enum class EventType {invasion, snapOff, none};
58 invadedCurrentIteration_.resize(problem.gridGeometry().gridView().size(0));
59 invadedPreviousTimeStep_.resize(problem.gridGeometry().gridView().size(0));
61 for (
auto&& element : elements(problem.gridGeometry().gridView()))
63 const auto eIdx = problem.gridGeometry().elementMapper().index(element);
64 invadedCurrentIteration_[eIdx] = problem.initialInvasionState(element);
65 invadedPreviousTimeStep_[eIdx] = invadedCurrentIteration_[eIdx];
68 numThroatsInvaded_ = std::count(invadedCurrentIteration_.begin(), invadedCurrentIteration_.end(),
true);
69 verbose_ = getParamFromGroup<bool>(problem.paramGroup(),
"InvasionState.Verbosity",
true);
70 restrictToGlobalCapillaryPressure_ = getParamFromGroup<bool>(problem.paramGroup(),
"InvasionState.RestrictInvasionToGlobalCapillaryPressure",
false);
72 if constexpr (hasGlobalCapillaryPressure<Problem>())
74 if (restrictToGlobalCapillaryPressure_)
75 std::cout <<
"\n *** Invasion behavior is restricted by a global capillary pressure defined in the problem! *** \n" << std::endl;
77 std::cout <<
"\n *** WARNING: global capillary pressure defined in the problem but InvasionState.RestrictInvasionToGlobalCapillaryPressure is set to false.\n"
78 <<
" Invasion behavior will NOT be restricted! ***\n" << std::endl;
83 template<
class Element>
84 bool invaded(
const Element& element)
const
86 const auto eIdx = problem_.gridGeometry().elementMapper().index(element);
87 return invadedCurrentIteration_[eIdx];
92 {
return numThroatsInvaded_; }
95 template<
class SolutionVector,
class Gr
idVolumeVariables,
class Gr
idFluxVariablesCache>
96 bool update(
const SolutionVector& sol,
const GridVolumeVariables& gridVolVars, GridFluxVariablesCache& gridFluxVarsCache)
98 hasChangedInCurrentIteration_ =
false;
99 auto fvGeometry =
localView(problem_.gridGeometry());
100 auto elemVolVars =
localView(gridVolVars);
101 auto elemFluxVarsCache =
localView(gridFluxVarsCache);
102 for (
auto&& element : elements(problem_.gridGeometry().gridView()))
104 fvGeometry.bindElement(element);
105 elemVolVars.bind(element, fvGeometry, sol);
106 elemFluxVarsCache.bind(element, fvGeometry, elemVolVars);
108 for (
auto&& scvf : scvfs(fvGeometry))
111 if (
const auto invasionResult = invasionSwitch_(element, elemVolVars, elemFluxVarsCache[scvf]); invasionResult)
113 hasChangedInCurrentIteration_ =
true;
114 if constexpr (GridFluxVariablesCache::cachingEnabled)
116 const auto eIdx = problem_.gridGeometry().elementMapper().index(element);
117 gridFluxVarsCache.cache(eIdx, scvf.index()).
update(problem_, element, fvGeometry, elemVolVars, scvf, invadedCurrentIteration_[eIdx]);
122 numThroatsInvaded_ = std::count(invadedCurrentIteration_.begin(), invadedCurrentIteration_.end(),
true);
123 return hasChangedInCurrentIteration_;
129 hasChangedInCurrentIteration_ =
false;
130 invadedCurrentIteration_ = invadedPreviousTimeStep_;
135 {
return hasChangedComparedToPreviousTimestep_; }
139 {
return hasChangedInCurrentIteration_; }
144 hasChangedComparedToPreviousTimestep_ = (invadedPreviousTimeStep_ != invadedCurrentIteration_);
145 invadedPreviousTimeStep_ = invadedCurrentIteration_;
148 template<
class SolutionVector,
class Gr
idVolumeVariables,
class Gr
idFluxVariablesCache>
150 const GridVolumeVariables& gridVolVars,
151 const GridFluxVariablesCache& gridFluxVarsCache)
const
153 using Scalar =
typename SolutionVector::block_type::value_type;
154 static const Scalar accuracyCriterion = getParamFromGroup<Scalar>(problem_.paramGroup(),
"InvasionState.AccuracyCriterion", -1.0);
156 if (accuracyCriterion < 0.0)
159 auto fvGeometry =
localView(problem_.gridGeometry());
160 auto elemVolVars =
localView(gridVolVars);
161 auto elemFluxVarsCache =
localView(gridFluxVarsCache);
162 for (
auto&& element : elements(problem_.gridGeometry().gridView()))
165 const auto eIdx = problem_.gridGeometry().elementMapper().index(element);
166 if (!invadedCurrentIteration_[eIdx] || invadedPreviousTimeStep_[eIdx] == invadedCurrentIteration_[eIdx])
169 fvGeometry.bindElement(element);
170 elemVolVars.bind(element, fvGeometry, sol);
171 elemFluxVarsCache.bind(element, fvGeometry, elemVolVars);
173 for (
auto&& scvf : scvfs(fvGeometry))
176 const auto& fluxVarsCache = elemFluxVarsCache[scvf];
181 if (pc < accuracyCriterion * fluxVarsCache.pcEntry())
182 DUNE_THROW(
NumericalProblem,
"At element " << eIdx <<
": pc " << pc <<
" too far away form pcEntry " << fluxVarsCache.pcEntry());
190 template<
class Element,
class ElementVolumeVariables,
class FluxVariablesCache>
191 auto invasionSwitch_(
const Element& element,
192 const ElementVolumeVariables& elemVolVars,
193 const FluxVariablesCache& fluxVarsCache)
196 using Scalar =
typename ElementVolumeVariables::VolumeVariables::PrimaryVariables::value_type;
197 const auto& gridGeometry = problem_.gridGeometry();
198 const auto& spatialParams = problem_.spatialParams();
199 const auto eIdx = gridGeometry.elementMapper().index(element);
200 bool invadedBeforeSwitch = invadedCurrentIteration_[eIdx];
201 bool invadedAfterSwitch = invadedBeforeSwitch;
207 std::uint8_t localScvIdxWithCriticalPc;
209 EventType
event = EventType::none;
211 operator bool()
const
212 {
return event != EventType::none; }
216 static const auto blockNonwettingPhase = getParamFromGroup<std::vector<int>>(problem_.paramGroup(),
"InvasionState.BlockNonwettingPhaseAtThroatLabel", std::vector<int>{});
217 if (!blockNonwettingPhase.empty() && std::find(blockNonwettingPhase.begin(), blockNonwettingPhase.end(), gridGeometry.throatLabel(eIdx)) != blockNonwettingPhase.end())
219 invadedCurrentIteration_[eIdx] =
false;
224 const std::array<Scalar, 2> pc = { elemVolVars[0].capillaryPressure(), elemVolVars[1].capillaryPressure() };
225 const auto pcMax = std::max_element(pc.begin(), pc.end());
226 const Scalar
pcEntry = fluxVarsCache.pcEntry();
227 const Scalar
pcSnapoff = fluxVarsCache.pcSnapoff();
230 if (maybeRestrictToGlobalCapillaryPressure_(
pcEntry))
234 std::cout <<
"Throat " << eIdx <<
" would have been invaded by pc of " << *pcMax <<
"but a global capillary pressure restricion was set in the problem.";
235 std::cout <<
". pcEntry: " << spatialParams.pcEntry(element, elemVolVars) << std::endl;
238 invadedCurrentIteration_[eIdx] =
false;
243 invadedAfterSwitch =
true;
245 invadedAfterSwitch =
false;
247 invadedCurrentIteration_[eIdx] = invadedAfterSwitch;
249 if (invadedBeforeSwitch == invadedAfterSwitch)
254 result.localScvIdxWithCriticalPc =
std::distance(pc.begin(), pcMax);
255 result.criticalPc = *pcMax;
256 result.event = !invadedBeforeSwitch && invadedAfterSwitch ? EventType::invasion : EventType::snapOff;
260 const auto wPhaseIdx = spatialParams.template wettingPhase<typename ElementVolumeVariables::VolumeVariables::FluidSystem>(element, elemVolVars);
261 const std::array sw = { elemVolVars[0].saturation(wPhaseIdx), elemVolVars[1].saturation(wPhaseIdx) };
262 const auto vIdx = gridGeometry.gridView().indexSet().subIndex(element, result.localScvIdxWithCriticalPc, 1);
263 if (result.event == EventType::invasion)
265 std::cout <<
"Throat " << eIdx <<
" was invaded from pore " << vIdx <<
" :";
266 std::cout <<
" pc: " << *pcMax;
267 std::cout <<
", pcEntry: " << spatialParams.pcEntry(element, elemVolVars);
268 std::cout <<
", sw: " << sw[result.localScvIdxWithCriticalPc] << std::endl;
272 std::cout <<
"Snap-off occurred at throat " << eIdx <<
" from pore " << vIdx <<
" :";
273 std::cout <<
" pc: " << *pcMax;
274 std::cout <<
", pcSnapoff: " << spatialParams.pcSnapoff(element, elemVolVars);
275 std::cout <<
", sw: " << sw[result.localScvIdxWithCriticalPc] << std::endl;
285 template<
class Scalar>
286 bool maybeRestrictToGlobalCapillaryPressure_(
const Scalar
pcEntry)
const
288 if constexpr (hasGlobalCapillaryPressure<Problem>())
289 return restrictToGlobalCapillaryPressure_ && (
pcEntry > problem_.globalCapillaryPressure());
294 std::vector<bool> invadedCurrentIteration_;
295 std::vector<bool> invadedPreviousTimeStep_;
296 bool hasChangedInCurrentIteration_ =
false;
297 bool hasChangedComparedToPreviousTimestep_ =
false;
298 std::size_t numThroatsInvaded_;
300 bool restrictToGlobalCapillaryPressure_;
302 const Problem& problem_;
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
Defines labels for pores and throats.
static ctype distance(const Dune::FieldVector< ctype, dimWorld > &a, const Dune::FieldVector< ctype, dimWorld > &b)
Compute the shortest distance between two points.
Definition: distance.hh:294
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:38
Definition: discretization/porenetwork/fvelementgeometry.hh:34
std::string capillaryPressure() noexcept
I/O name of capillary pressure.
Definition: name.hh:135
Scalar pcSnapoff(const Scalar surfaceTension, const Scalar contactAngle, const Scalar inscribedRadius, const Throat::Shape shape)
The snap-off capillary pressure of a pore throat It checks if the cross section shape of the throat i...
Definition: thresholdcapillarypressures.hh:77
Scalar pcEntry(const Scalar surfaceTension, const Scalar contactAngle, const Scalar inscribedRadius, const Scalar shapeFactor) noexcept
The entry capillary pressure of a pore throat.
Definition: thresholdcapillarypressures.hh:94
Exception thrown if a fixable numerical problem occurs.
Definition: exceptions.hh:39
This class updates the invasion state for the two-phase PNM.
Definition: invasionstate.hh:41
void checkIfCapillaryPressureIsCloseToEntryPressure(const SolutionVector &sol, const GridVolumeVariables &gridVolVars, const GridFluxVariablesCache &gridFluxVarsCache) const
Definition: invasionstate.hh:149
bool hasChangedInCurrentIteration() const
Return whether an invasion or snap-off occurred anywhere during the current Newton iteration.
Definition: invasionstate.hh:138
bool invaded(const Element &element) const
Return whether a given throat is invaded or not.
Definition: invasionstate.hh:84
bool hasChanged() const
Return whether an invasion or snap-off occurred anywhere. Can be used, e.g., for output file writing ...
Definition: invasionstate.hh:134
bool update(const SolutionVector &sol, const GridVolumeVariables &gridVolVars, GridFluxVariablesCache &gridFluxVarsCache)
Update the invasion state of all throats. This is done after each Newton step by a call from the Newt...
Definition: invasionstate.hh:96
TwoPInvasionState(const Problem &problem)
Definition: invasionstate.hh:55
void advance()
This is called after the Newton method has successfully finished one time step.
Definition: invasionstate.hh:142
std::size_t numThroatsInvaded() const
Return the number of currently invaded throats.
Definition: invasionstate.hh:91
void reset()
Restore the old invasion state after a Newton iteration has failed.
Definition: invasionstate.hh:127