3.2-git
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 updateBoundary(const Problem& problem,
210 const typename GridVariables::GridGeometry& gridGeometry,
211 GridVariables& gridVariables,
212 SolutionVector& sol)
213 {
214 if constexpr (GridVariables::GridGeometry::discMethod == DiscretizationMethod::box)
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
225 if (!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 // this implies that state is set equal for all scvs associated with the dof
234 const auto dofIdx = scv.dofIndex();
235 if (!gridGeometry.dofOnBoundary(dofIdx) || stateChanged[dofIdx])
236 continue;
237
238 const auto bcTypes = problem.boundaryTypes(element, scv);
239 if (bcTypes.hasDirichlet())
240 {
241 const auto dirichletValues = problem.dirichlet(element, scv);
242
243 if (sol[dofIdx].state() != dirichletValues.state())
244 {
245 if (verbosity() > 1)
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()
249 << std::endl;
250
251 // make sure the solution vector has the right state (given by the Dirichlet BC)
252 sol[dofIdx].setState(dirichletValues.state());
253 stateChanged[dofIdx] = true;
254 ++countChanged;
255
256 // overwrite initial with Dirichlet values
257 for (int eqIdx = 0; eqIdx < SolutionVector::block_type::dimension; ++eqIdx)
258 {
259 if (bcTypes.isDirichlet(eqIdx))
260 {
261 const auto pvIdx = bcTypes.eqToDirichletIndex(eqIdx);
262 sol[dofIdx][pvIdx] = dirichletValues[pvIdx];
263 }
264 }
265 }
266 }
267 }
268
269 // update the volVars if caching is enabled
270 if constexpr (GridVariables::GridVolumeVariables::cachingEnabled)
271 {
272 if (countChanged > 0)
273 {
274 const auto curElemSol = elementSolution(element, sol, gridGeometry);
275 for (const auto& scv : scvs(fvGeometry))
276 {
277 if (stateChanged[scv.dofIndex()])
278 {
279 auto& volVars = getVolVarAccess_(gridVariables.curGridVolVars(), elemVolVars, scv);
280 volVars.update(curElemSol, problem, element, scv);
281 }
282 }
283 }
284 }
285 }
286
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;
290 }
291 }
292
294 int verbosity() const
295 { return verbosity_; }
296
297protected:
298
300 Implementation &asImp_()
301 { return *static_cast<Implementation*>(this); }
302
304 const Implementation &asImp_() const
305 { return *static_cast<const Implementation*>(this); }
306
307 // Perform variable switch at a degree of freedom location
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)
313 {
314 // evaluate if the primary variable switch would switch
315 // to be implemented by the deriving class
316 DUNE_THROW(Dune::NotImplemented, "This model seems to use a primary variable switch but none is implemented!");
317 }
318
319 // Maybe skip the degree of freedom (do not switch variables at this dof)
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)
325 {
326 if (visited_[scv.dofIndex()])
327 return true;
328
329 if (isConstrainedDof_(element, fvGeometry, scv, problem))
330 return true;
331
332 return false;
333 }
334
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)
340 {
341 if constexpr (Geometry::GridGeometry::discMethod != DiscretizationMethod::box)
342 return false;
343
344 else
345 {
346 if (!fvGeometry.hasBoundaryScvf())
347 return false;
348
349 const auto dofIdx = scv.dofIndex();
350 if (!fvGeometry.gridGeometry().dofOnBoundary(dofIdx))
351 return false;
352
353 const auto bcTypes = problem.boundaryTypes(element, scv);
354 if (bcTypes.hasDirichlet())
355 return true;
356
357 return false;
358 }
359 }
360
361 std::vector<bool> wasSwitched_;
362 std::vector<bool> visited_;
363
364private:
365 template<class GridVolumeVariables, class ElementVolumeVariables, class SubControlVolume>
366 typename GridVolumeVariables::VolumeVariables&
367 getVolVarAccess_(GridVolumeVariables& gridVolVars, ElementVolumeVariables& elemVolVars, const SubControlVolume& scv) const
368 {
369 if constexpr (GridVolumeVariables::cachingEnabled)
370 return gridVolVars.volVars(scv);
371 else
372 return elemVolVars[scv];
373 }
374
375 int verbosity_;
376};
377
378} // end namespace dumux
379
380#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:115
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: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