3.5-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
primaryvariableswitchadapter.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 *****************************************************************************/
24#ifndef DUMUX_NONLINEAR_PRIMARY_VARIABLE_SWITCH_ADAPTER_HH
25#define DUMUX_NONLINEAR_PRIMARY_VARIABLE_SWITCH_ADAPTER_HH
26
27#include <memory>
28#include <dune/common/std/type_traits.hh>
30
31namespace Dumux {
32namespace Detail {
33
35template<class Variables>
37
38template<class Variables>
39using GetPVSwitch = Dune::Std::detected_or<int, DetectPVSwitch, Variables>;
40
41template<class Variables>
43
44} // end namespace Detail
45
50template<class Variables>
52
57template <class Variables, bool isValid = hasPriVarsSwitch<Variables>>
59{
60 using PrimaryVariableSwitch = typename Detail::PrimaryVariableSwitch<Variables>;
61
62public:
63 PrimaryVariableSwitchAdapter(const std::string& paramGroup = "")
64 {
65 const int priVarSwitchVerbosity = getParamFromGroup<int>(paramGroup, "PrimaryVariableSwitch.Verbosity", 1);
66 priVarSwitch_ = std::make_unique<PrimaryVariableSwitch>(priVarSwitchVerbosity);
67 }
68
72 template<class SolutionVector>
73 void initialize(SolutionVector& sol, Variables& vars)
74 {
75 priVarSwitch_->reset(sol.size());
76 priVarsSwitchedInLastIteration_ = false;
77 const auto& problem = vars.curGridVolVars().problem();
78 const auto& gridGeometry = problem.gridGeometry();
79 priVarSwitch_->updateDirichletConstraints(problem, gridGeometry, vars, sol);
80 }
81
85 template<class SolutionVector>
86 void invoke(SolutionVector& uCurrentIter, Variables& vars)
87 {
88 // update the variable switch (returns true if the pri vars at at least one dof were switched)
89 // for disabled grid variable caching
90 const auto& problem = vars.curGridVolVars().problem();
91 const auto& gridGeometry = problem.gridGeometry();
92
93 // invoke the primary variable switch
94 priVarsSwitchedInLastIteration_ = priVarSwitch_->update(uCurrentIter, vars, problem, gridGeometry);
95 if (priVarsSwitchedInLastIteration_)
96 {
97 for (const auto& element : elements(gridGeometry.gridView()))
98 {
99 // if the volume variables are cached globally, we need to update those where the primary variables have been switched
100 priVarSwitch_->updateSwitchedVolVars(problem, element, gridGeometry, vars, uCurrentIter);
101
102 // if the flux variables are cached globally, we need to update those where the primary variables have been switched
103 priVarSwitch_->updateSwitchedFluxVarsCache(problem, element, gridGeometry, vars, uCurrentIter);
104 }
105 }
106 }
107
111 bool switched() const
112 { return priVarsSwitchedInLastIteration_; }
113
114private:
116 std::unique_ptr<PrimaryVariableSwitch> priVarSwitch_;
118 bool priVarsSwitchedInLastIteration_ = false;
119};
120
125template <class Variables>
126class PrimaryVariableSwitchAdapter<Variables, false>
127{
128public:
129 PrimaryVariableSwitchAdapter(const std::string& paramGroup = "") {}
130
131 template<class SolutionVector>
132 void initialize(SolutionVector&, Variables&) {}
133
134 template<class SolutionVector>
135 void invoke(SolutionVector&, Variables&) {}
136
137 bool switched() const { return false; }
138};
139
140} // end namespace Dumux
141
142#endif
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
constexpr bool hasPriVarsSwitch
Helper boolean to check if the given variables involve primary variable switching.
Definition: primaryvariableswitchadapter.hh:51
Definition: adapt.hh:29
typename GetPVSwitch< Variables >::type PrimaryVariableSwitch
Definition: primaryvariableswitchadapter.hh:42
Dune::Std::detected_or< int, DetectPVSwitch, Variables > GetPVSwitch
Definition: primaryvariableswitchadapter.hh:39
typename Variables::VolumeVariables::PrimaryVariableSwitch DetectPVSwitch
helper aliases to extract a primary variable switch from the VolumeVariables (if defined,...
Definition: primaryvariableswitchadapter.hh:36
An adapter for the Newton to manage models with primary variable switch.
Definition: primaryvariableswitchadapter.hh:59
void invoke(SolutionVector &uCurrentIter, Variables &vars)
Switch primary variables if necessary.
Definition: primaryvariableswitchadapter.hh:86
PrimaryVariableSwitchAdapter(const std::string &paramGroup="")
Definition: primaryvariableswitchadapter.hh:63
void initialize(SolutionVector &sol, Variables &vars)
Initialize the privar switch.
Definition: primaryvariableswitchadapter.hh:73
bool switched() const
Whether the primary variables have been switched in the last call to invoke.
Definition: primaryvariableswitchadapter.hh:111
void initialize(SolutionVector &, Variables &)
Definition: primaryvariableswitchadapter.hh:132
void invoke(SolutionVector &, Variables &)
Definition: primaryvariableswitchadapter.hh:135
bool switched() const
Definition: primaryvariableswitchadapter.hh:137
PrimaryVariableSwitchAdapter(const std::string &paramGroup="")
Definition: primaryvariableswitchadapter.hh:129