version 3.10-dev
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// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_NONLINEAR_PRIMARY_VARIABLE_SWITCH_ADAPTER_HH
13#define DUMUX_NONLINEAR_PRIMARY_VARIABLE_SWITCH_ADAPTER_HH
14
15#include <memory>
16#include <dune/common/std/type_traits.hh>
18
19namespace Dumux {
20namespace Detail {
21
23template<class Variables>
25
26template<class Variables>
27using PrimaryVariableSwitch = Dune::Std::detected_or_t<int, DetectPVSwitch, Variables>;
28
29} // end namespace Detail
30
35template<class Variables>
36inline constexpr bool hasPriVarsSwitch = Dune::Std::is_detected<Detail::DetectPVSwitch, Variables>();
37
42template <class Variables, bool isValid = hasPriVarsSwitch<Variables>>
44{
45 using PrimaryVariableSwitch = typename Detail::PrimaryVariableSwitch<Variables>;
46
47public:
48 PrimaryVariableSwitchAdapter(const std::string& paramGroup = "")
49 {
50 const int priVarSwitchVerbosity = getParamFromGroup<int>(paramGroup, "PrimaryVariableSwitch.Verbosity", 1);
51 priVarSwitch_ = std::make_unique<PrimaryVariableSwitch>(priVarSwitchVerbosity);
52 }
53
57 template<class SolutionVector>
58 void initialize(SolutionVector& sol, Variables& vars)
59 {
60 priVarSwitch_->reset(sol.size());
61 priVarsSwitchedInLastIteration_ = false;
62 const auto& problem = vars.curGridVolVars().problem();
63 const auto& gridGeometry = problem.gridGeometry();
64 priVarSwitch_->updateDirichletConstraints(problem, gridGeometry, vars, sol);
65 }
66
70 template<class SolutionVector>
71 void invoke(SolutionVector& uCurrentIter, Variables& vars)
72 {
73 // update the variable switch (returns true if the pri vars at at least one dof were switched)
74 // for disabled grid variable caching
75 const auto& problem = vars.curGridVolVars().problem();
76 const auto& gridGeometry = problem.gridGeometry();
77
78 // invoke the primary variable switch
79 priVarsSwitchedInLastIteration_ = priVarSwitch_->update(uCurrentIter, vars, problem, gridGeometry);
80 if (priVarsSwitchedInLastIteration_)
81 {
82 for (const auto& element : elements(gridGeometry.gridView()))
83 {
84 // if the volume variables are cached globally, we need to update those where the primary variables have been switched
85 priVarSwitch_->updateSwitchedVolVars(problem, element, gridGeometry, vars, uCurrentIter);
86
87 // if the flux variables are cached globally, we need to update those where the primary variables have been switched
88 priVarSwitch_->updateSwitchedFluxVarsCache(problem, element, gridGeometry, vars, uCurrentIter);
89 }
90 }
91 }
92
96 bool switched() const
97 { return priVarsSwitchedInLastIteration_; }
98
99private:
101 std::unique_ptr<PrimaryVariableSwitch> priVarSwitch_;
103 bool priVarsSwitchedInLastIteration_ = false;
104};
105
110template <class Variables>
111class PrimaryVariableSwitchAdapter<Variables, false>
112{
113public:
114 PrimaryVariableSwitchAdapter(const std::string& paramGroup = "") {}
115
116 template<class SolutionVector>
117 void initialize(SolutionVector&, Variables&) {}
118
119 template<class SolutionVector>
120 void invoke(SolutionVector&, Variables&) {}
121
122 bool switched() const { return false; }
123};
124
125} // end namespace Dumux
126
127#endif
void initialize(SolutionVector &, Variables &)
Definition: primaryvariableswitchadapter.hh:117
void invoke(SolutionVector &, Variables &)
Definition: primaryvariableswitchadapter.hh:120
bool switched() const
Definition: primaryvariableswitchadapter.hh:122
PrimaryVariableSwitchAdapter(const std::string &paramGroup="")
Definition: primaryvariableswitchadapter.hh:114
An adapter for the Newton to manage models with primary variable switch.
Definition: primaryvariableswitchadapter.hh:44
void invoke(SolutionVector &uCurrentIter, Variables &vars)
Switch primary variables if necessary.
Definition: primaryvariableswitchadapter.hh:71
PrimaryVariableSwitchAdapter(const std::string &paramGroup="")
Definition: primaryvariableswitchadapter.hh:48
void initialize(SolutionVector &sol, Variables &vars)
Initialize the privar switch.
Definition: primaryvariableswitchadapter.hh:58
bool switched() const
Whether the primary variables have been switched in the last call to invoke.
Definition: primaryvariableswitchadapter.hh:96
constexpr bool hasPriVarsSwitch
Helper boolean to check if the given variables involve primary variable switching.
Definition: primaryvariableswitchadapter.hh:36
Dune::Std::detected_or_t< int, DetectPVSwitch, Variables > PrimaryVariableSwitch
Definition: primaryvariableswitchadapter.hh:27
typename Variables::VolumeVariables::PrimaryVariableSwitch DetectPVSwitch
helper aliases to extract a primary variable switch from the VolumeVariables (if defined,...
Definition: primaryvariableswitchadapter.hh:24
Definition: adapt.hh:17
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.