3.1-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 auto dofIdxGlobal = scv.dofIndex();
108 if (!visited_[dofIdxGlobal])
109 {
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 std::enable_if_t<GridVariables::GridVolumeVariables::cachingEnabled, int> = 0>
147 void updateSwitchedVolVars(const Problem& problem,
148 const typename GridVariables::GridGeometry::GridView::template Codim<0>::Entity& element,
149 const typename GridVariables::GridGeometry& gridGeometry,
150 GridVariables& gridVariables,
151 const SolutionVector& sol)
152 {
153 // make sure FVElementGeometry is bound to the element
154 auto fvGeometry = localView(gridGeometry);
155 fvGeometry.bindElement(element);
156
157 // update the secondary variables if global caching is enabled
158 for (auto&& scv : scvs(fvGeometry))
159 {
160 const auto dofIdxGlobal = scv.dofIndex();
161 if (asImp_().wasSwitched(dofIdxGlobal))
162 {
163 const auto elemSol = elementSolution(element, sol, gridGeometry);
164 auto& volVars = gridVariables.curGridVolVars().volVars(scv);
165 volVars.update(elemSol, problem, element, scv);
166 }
167 }
168 }
169
176 template<class Problem, class GridVariables, class SolutionVector,
177 std::enable_if_t<(GridVariables::GridFluxVariablesCache::cachingEnabled &&
178 GridVariables::GridGeometry::discMethod != DiscretizationMethod::box), int> = 0>
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 // update the flux variables if global caching is enabled
186 const auto dofIdxGlobal = gridGeometry.dofMapper().index(element);
187
188 if (asImp_().wasSwitched(dofIdxGlobal))
189 {
190 // make sure FVElementGeometry and the volume variables are bound
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);
196 }
197 }
198
204 template<class Problem, class GridVariables, class SolutionVector,
205 std::enable_if_t<(GridVariables::GridGeometry::discMethod == DiscretizationMethod::box), int> = 0>
206 void updateBoundary(const Problem& problem,
207 const typename GridVariables::GridGeometry& gridGeometry,
208 GridVariables& gridVariables,
209 SolutionVector& sol)
210 {
211 std::vector<bool> stateChanged(sol.size(), false);
212 std::size_t countChanged = 0;
213
214 for (const auto& element : elements(gridGeometry.gridView()))
215 {
216 auto fvGeometry = localView(gridGeometry);
217 fvGeometry.bindElement(element);
218
219 // skip if the element is not at a boundary
220 if (!fvGeometry.hasBoundaryScvf())
221 continue;
222
223 auto elemVolVars = localView(gridVariables.curGridVolVars());
224 elemVolVars.bindElement(element, fvGeometry, sol);
225
226 for (const auto& scv : scvs(fvGeometry))
227 {
228 // this implies that state is set equal for all scvs associated with the dof
229 const auto dofIdx = scv.dofIndex();
230 if (!gridGeometry.dofOnBoundary(dofIdx) || stateChanged[dofIdx])
231 continue;
232
233 const auto bcTypes = problem.boundaryTypes(element, scv);
234 if (bcTypes.hasDirichlet())
235 {
236 const auto dirichletValues = problem.dirichlet(element, scv);
237
238 if (sol[dofIdx].state() != dirichletValues.state())
239 {
240 if (verbosity() > 1)
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()
244 << std::endl;
245
246 // make sure the solution vector has the right state (given by the Dirichlet BC)
247 sol[dofIdx].setState(dirichletValues.state());
248 stateChanged[dofIdx] = true;
249 ++countChanged;
250
251 // overwrite initial with Dirichlet values
252 for (int eqIdx = 0; eqIdx < SolutionVector::block_type::dimension; ++eqIdx)
253 {
254 if (bcTypes.isDirichlet(eqIdx))
255 {
256 const auto pvIdx = bcTypes.eqToDirichletIndex(eqIdx);
257 sol[dofIdx][pvIdx] = dirichletValues[pvIdx];
258 }
259 }
260 }
261 }
262 }
263
264 // update the volVars if caching is enabled
265 if (GridVariables::GridVolumeVariables::cachingEnabled && countChanged > 0)
266 {
267 const auto curElemSol = elementSolution(element, sol, gridGeometry);
268 for (const auto& scv : scvs(fvGeometry))
269 {
270 if (stateChanged[scv.dofIndex()])
271 {
272 auto& volVars = getVolVarAccess(gridVariables.curGridVolVars(), elemVolVars, scv);
273 volVars.update(curElemSol, problem, element, scv);
274 }
275 }
276 }
277 }
278
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;
282 }
283
285 template<class Problem, class GridVariables, class SolutionVector,
286 std::enable_if_t<!GridVariables::GridVolumeVariables::cachingEnabled, int> = 0>
287 void updateSwitchedVolVars(const Problem& problem,
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 {}
292
294 template<class Problem, class GridVariables, class SolutionVector,
295 std::enable_if_t<(!GridVariables::GridFluxVariablesCache::cachingEnabled ||
296 GridVariables::GridGeometry::discMethod == DiscretizationMethod::box), int> = 0>
297 void updateSwitchedFluxVarsCache(const Problem& problem,
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 {}
302
304 template<class Problem, class GridVariables, class SolutionVector,
305 std::enable_if_t<(GridVariables::GridGeometry::discMethod != DiscretizationMethod::box), int> = 0>
306 void updateBoundary(const Problem& problem,
307 const typename GridVariables::GridGeometry& gridGeometry,
308 GridVariables& gridVariables,
309 SolutionVector& sol) const {}
310
312 int verbosity() const
313 { return verbosity_; }
314
315protected:
316
318 Implementation &asImp_()
319 { return *static_cast<Implementation*>(this); }
320
322 const Implementation &asImp_() const
323 { return *static_cast<const Implementation*>(this); }
324
325 // Perform variable switch at a degree of freedom location
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)
331 {
332 // evaluate if the primary variable switch would switch
333 // to be implemented by the deriving class
334 DUNE_THROW(Dune::NotImplemented, "This model seems to use a primary variable switch but none is implemented!");
335 }
336
337 std::vector<bool> wasSwitched_;
338 std::vector<bool> visited_;
339
340private:
341 template<class GridVolumeVariables, 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]; }
345
346 template<class GridVolumeVariables, 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); }
350
351 int verbosity_;
352};
353
354} // end namespace dumux
355
356#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
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