3.4
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
compositional/primaryvariableswitch.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
25#ifndef DUMUX_PRIMARY_VARIABLE_SWITCH_HH
26#define DUMUX_PRIMARY_VARIABLE_SWITCH_HH
27
28#include <iostream>
29
30#include <dune/common/exceptions.hh>
31#include <dune/common/fvector.hh>
34
35namespace Dumux {
36
42{
43public:
44 template<typename... Args>
46
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; }
50 template<typename... Args> void updateSwitchedVolVars(Args&&...) {}
51 template<typename... Args> void updateSwitchedFluxVarsCache(Args&&...) {}
52};
53
58template<class Implementation>
60{
61public:
63 : verbosity_(verbosity)
64 {}
65
67 bool wasSwitched(std::size_t dofIdxGlobal) const
68 {
69 return wasSwitched_[dofIdxGlobal];
70 }
71
73 void reset(const std::size_t numDofs)
74 {
75 wasSwitched_.resize(numDofs, false);
76 }
77
86 template<class SolutionVector, class GridVariables, class Problem>
87 bool update(SolutionVector& curSol,
88 GridVariables& gridVariables,
89 const Problem& problem,
90 const typename GridVariables::GridGeometry& gridGeometry)
91 {
92 bool switched = false;
93 visited_.assign(wasSwitched_.size(), false);
94 std::size_t countSwitched = 0;
95 for (const auto& element : elements(gridGeometry.gridView()))
96 {
97 // make sure FVElementGeometry is bound to the element
98 auto fvGeometry = localView(gridGeometry);
99 fvGeometry.bindElement(element);
100
101 auto elemVolVars = localView(gridVariables.curGridVolVars());
102 elemVolVars.bindElement(element, fvGeometry, curSol);
103
104 const auto curElemSol = elementSolution(element, curSol, gridGeometry);
105 for (auto&& scv : scvs(fvGeometry))
106 {
107 if (!asImp_().skipDof_(element, fvGeometry, scv, problem))
108 {
109 const auto dofIdxGlobal = scv.dofIndex();
110 // Note this implies that volume variables don't differ
111 // in any sub control volume associated with the dof!
112 visited_[dofIdxGlobal] = true;
113 // Compute volVars on which grounds we decide
114 // if we need to switch the primary variables
115 auto& volVars = getVolVarAccess_(gridVariables.curGridVolVars(), elemVolVars, scv);
116 volVars.update(curElemSol, problem, element, scv);
117
118 if (asImp_().update_(curSol[dofIdxGlobal], volVars, dofIdxGlobal, scv.dofPosition()))
119 {
120 switched = true;
121 ++countSwitched;
122 }
123 }
124 }
125 }
126
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;
130
131 // make sure that if there was a variable switch in an
132 // other partition we will also set the switch flag for our partition.
133 if (gridGeometry.gridView().comm().size() > 1)
134 switched = gridGeometry.gridView().comm().max(switched);
135
136 return switched;
137 }
138
145 template<class Problem, class GridVariables, class SolutionVector>
146 void updateSwitchedVolVars(const Problem& problem,
147 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
148 const typename GridVariables::GridGeometry& gridGeometry,
149 GridVariables& gridVariables,
150 const SolutionVector& sol)
151 {
152 if constexpr (GridVariables::GridVolumeVariables::cachingEnabled)
153 {
154 // make sure FVElementGeometry is bound to the element
155 auto fvGeometry = localView(gridGeometry);
156 fvGeometry.bindElement(element);
157
158 // update the secondary variables if global caching is enabled
159 for (auto&& scv : scvs(fvGeometry))
160 {
161 const auto dofIdxGlobal = scv.dofIndex();
162 if (asImp_().wasSwitched(dofIdxGlobal))
163 {
164 const auto elemSol = elementSolution(element, sol, gridGeometry);
165 auto& volVars = gridVariables.curGridVolVars().volVars(scv);
166 volVars.update(elemSol, problem, element, scv);
167 }
168 }
169 }
170 }
171
178 template<class Problem, class GridVariables, class SolutionVector>
179 void updateSwitchedFluxVarsCache(const Problem& problem,
180 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
181 const typename GridVariables::GridGeometry& gridGeometry,
182 GridVariables& gridVariables,
183 const SolutionVector& sol)
184 {
185 if constexpr (GridVariables::GridFluxVariablesCache::cachingEnabled
186 && GridVariables::GridGeometry::discMethod != DiscretizationMethod::box)
187 {
188 // update the flux variables if global caching is enabled
189 const auto dofIdxGlobal = gridGeometry.dofMapper().index(element);
190
191 if (asImp_().wasSwitched(dofIdxGlobal))
192 {
193 // make sure FVElementGeometry and the volume variables are bound
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);
199 }
200 }
201 }
202
208 template<class Problem, class GridVariables, class SolutionVector>
209 void updateDirichletConstraints(const Problem& problem,
210 const typename GridVariables::GridGeometry& gridGeometry,
211 GridVariables& gridVariables,
212 SolutionVector& sol)
213 {
214 if constexpr (GridVariables::GridGeometry::discMethod == DiscretizationMethod::box || Problem::enableInternalDirichletConstraints())
215 {
216 std::vector<bool> stateChanged(sol.size(), false);
217 std::size_t countChanged = 0;
218
219 for (const auto& element : elements(gridGeometry.gridView()))
220 {
221 auto fvGeometry = localView(gridGeometry);
222 fvGeometry.bindElement(element);
223
224 // skip if the element is not at a boundary or if no internal Dirichlet constraints are set
225 if (!Problem::enableInternalDirichletConstraints() && !fvGeometry.hasBoundaryScvf())
226 continue;
227
228 auto elemVolVars = localView(gridVariables.curGridVolVars());
229 elemVolVars.bindElement(element, fvGeometry, sol);
230
231 for (const auto& scv : scvs(fvGeometry))
232 {
233 const auto dofIdx = scv.dofIndex();
234
235 if (stateChanged[dofIdx])
236 continue;
237
238 if constexpr (GridVariables::GridGeometry::discMethod == DiscretizationMethod::box)
239 {
240 if (gridGeometry.dofOnBoundary(dofIdx))
241 {
242 if (handleDirichletBoundaryCondition_(problem, element, scv, sol))
243 {
244 stateChanged[dofIdx] = true;
245 ++countChanged;
246 continue;
247 }
248 }
249 }
250
251 if constexpr (Problem::enableInternalDirichletConstraints())
252 {
253 if (handleInternalDirichletConstraint_(problem, element, scv, sol))
254 {
255 stateChanged[dofIdx] = true;
256 ++countChanged;
257 }
258 }
259 }
260
261 // update the volVars if caching is enabled
262 if constexpr (GridVariables::GridVolumeVariables::cachingEnabled)
263 {
264 if (countChanged > 0)
265 {
266 const auto curElemSol = elementSolution(element, sol, gridGeometry);
267 for (const auto& scv : scvs(fvGeometry))
268 {
269 if (stateChanged[scv.dofIndex()])
270 {
271 auto& volVars = getVolVarAccess_(gridVariables.curGridVolVars(), elemVolVars, scv);
272 volVars.update(curElemSol, problem, element, scv);
273 }
274 }
275 }
276 }
277 }
278
279 if (verbosity_ > 0 && countChanged > 0)
280 std::cout << "Changed primary variable states and solution values to Dirichlet states and values at " << countChanged << " dof locations on processor "
281 << gridGeometry.gridView().comm().rank() << "." << std::endl;
282 }
283 }
284
286 int verbosity() const
287 { return verbosity_; }
288
289protected:
290
292 Implementation &asImp_()
293 { return *static_cast<Implementation*>(this); }
294
296 const Implementation &asImp_() const
297 { return *static_cast<const Implementation*>(this); }
298
299 // Perform variable switch at a degree of freedom location
300 template<class VolumeVariables, class GlobalPosition>
301 bool update_(typename VolumeVariables::PrimaryVariables& priVars,
302 const VolumeVariables& volVars,
303 std::size_t dofIdxGlobal,
304 const GlobalPosition& globalPos)
305 {
306 // evaluate if the primary variable switch would switch
307 // to be implemented by the deriving class
308 DUNE_THROW(Dune::NotImplemented, "This model seems to use a primary variable switch but none is implemented!");
309 }
310
311 // Maybe skip the degree of freedom (do not switch variables at this dof)
312 template<class Geometry, class Problem>
313 bool skipDof_(const typename Geometry::GridGeometry::GridView::template Codim<0>::Entity& element,
314 const Geometry& fvGeometry,
315 const typename Geometry::SubControlVolume& scv,
316 const Problem& problem)
317 {
318 if (visited_[scv.dofIndex()])
319 return true;
320
321 if (isConstrainedDof_(element, fvGeometry, scv, problem))
322 return true;
323
324 return false;
325 }
326
327 template<class Geometry, class Problem>
328 bool isConstrainedDof_(const typename Geometry::GridGeometry::GridView::template Codim<0>::Entity& element,
329 const Geometry& fvGeometry,
330 const typename Geometry::SubControlVolume& scv,
331 const Problem& problem)
332 {
333 // Dofs can be only constrained when using the Box method or when imposing internal Dirichlet constraints
334 if constexpr (Geometry::GridGeometry::discMethod != DiscretizationMethod::box && !Problem::enableInternalDirichletConstraints())
335 return false;
336
337 // check for internally constrained Dofs
338 const auto isInternallyConstrainedDof = [&]()
339 {
340 if constexpr (!Problem::enableInternalDirichletConstraints())
341 return false;
342 else
343 {
344 const auto internalDirichletConstraints = problem.hasInternalDirichletConstraint(element, scv);
345 return internalDirichletConstraints.any();
346 }
347 };
348
349 if (isInternallyConstrainedDof())
350 return true;
351
352 // check for a Dirichlet BC when using the Box method
353 if constexpr (Geometry::GridGeometry::discMethod == DiscretizationMethod::box)
354 {
355 if (!fvGeometry.hasBoundaryScvf())
356 return false;
357
358 const auto dofIdx = scv.dofIndex();
359 if (!fvGeometry.gridGeometry().dofOnBoundary(dofIdx))
360 return false;
361
362 const auto bcTypes = problem.boundaryTypes(element, scv);
363 if (bcTypes.hasDirichlet())
364 return true;
365 }
366
367 return false;
368 }
369
370 template<class Problem, class Element, class SubControlVolume, class SolutionVector>
371 bool handleDirichletBoundaryCondition_(const Problem& problem,
372 const Element& element,
373 const SubControlVolume& scv,
374 SolutionVector& sol)
375 {
376 bool changed = false;
377 const auto bcTypes = problem.boundaryTypes(element, scv);
378 if (bcTypes.hasDirichlet())
379 {
380 const auto dirichletValues = problem.dirichlet(element, scv);
381 const auto dofIdx = scv.dofIndex();
382
383 if (sol[dofIdx].state() != dirichletValues.state())
384 {
385 if (verbosity() > 1)
386 std::cout << "Changing primary variable state at boundary (" << sol[dofIdx].state()
387 << ") to the one given by the Dirichlet condition (" << dirichletValues.state() << ") at dof " << dofIdx
388 << ", coordinates: " << scv.dofPosition()
389 << std::endl;
390
391 // make sure the solution vector has the right state (given by the Dirichlet BC)
392 sol[dofIdx].setState(dirichletValues.state());
393 changed = true;
394
395 // overwrite initial with Dirichlet values
396 for (int eqIdx = 0; eqIdx < SolutionVector::block_type::dimension; ++eqIdx)
397 {
398 if (bcTypes.isDirichlet(eqIdx))
399 {
400 const auto pvIdx = bcTypes.eqToDirichletIndex(eqIdx);
401 sol[dofIdx][pvIdx] = dirichletValues[pvIdx];
402 }
403 }
404 }
405 }
406
407 return changed;
408 }
409
410 template<class Problem, class Element, class SubControlVolume, class SolutionVector>
411 bool handleInternalDirichletConstraint_(const Problem& problem,
412 const Element& element,
413 const SubControlVolume& scv,
414 SolutionVector& sol)
415 {
416 bool changed = false;
417
418 const auto internalDirichletConstraints = problem.hasInternalDirichletConstraint(element, scv);
419 if (internalDirichletConstraints.none())
420 return changed;
421
422 const auto dirichletValues = problem.internalDirichlet(element, scv);
423 const auto dofIdx = scv.dofIndex();
424
425 if (sol[dofIdx].state() != dirichletValues.state())
426 {
427 if (verbosity() > 1)
428 std::cout << "Changing primary variable state at internal DOF (" << sol[dofIdx].state()
429 << ") to the one given by the internal Dirichlet constraint (" << dirichletValues.state() << ") at dof " << dofIdx
430 << ", coordinates: " << scv.dofPosition()
431 << std::endl;
432
433 // make sure the solution vector has the right state (given by the Dirichlet constraint)
434 sol[dofIdx].setState(dirichletValues.state());
435 changed = true;
436
437 // overwrite initial with Dirichlet values
438 for (int pvIdx = 0; pvIdx < SolutionVector::block_type::dimension; ++pvIdx)
439 {
440 if (internalDirichletConstraints[pvIdx])
441 sol[dofIdx][pvIdx] = dirichletValues[pvIdx];
442 }
443 }
444
445 return changed;
446 }
447
448 std::vector<bool> wasSwitched_;
449 std::vector<bool> visited_;
450
451private:
452 template<class GridVolumeVariables, class ElementVolumeVariables, class SubControlVolume>
453 typename GridVolumeVariables::VolumeVariables&
454 getVolVarAccess_(GridVolumeVariables& gridVolVars, ElementVolumeVariables& elemVolVars, const SubControlVolume& scv) const
455 {
456 if constexpr (GridVolumeVariables::cachingEnabled)
457 return gridVolVars.volVars(scv);
458 else
459 return elemVolVars[scv];
460 }
461
462 int verbosity_;
463};
464
465} // end namespace dumux
466
467#endif
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:118
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:448
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:328
bool handleDirichletBoundaryCondition_(const Problem &problem, const Element &element, const SubControlVolume &scv, SolutionVector &sol)
Definition: compositional/primaryvariableswitch.hh:371
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:411
Implementation & asImp_()
Return actual implementation (static polymorphism)
Definition: compositional/primaryvariableswitch.hh:292
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:209
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:313
const Implementation & asImp_() const
Return actual implementation (static polymorphism)
Definition: compositional/primaryvariableswitch.hh:296
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:286
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:301
std::vector< bool > visited_
Definition: compositional/primaryvariableswitch.hh:449
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