GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/nonlinear/primaryvariableswitchadapter.hh
Date: 2024-05-04 19:09:25
Exec Total Coverage
Lines: 21 25 84.0%
Functions: 115 403 28.5%
Branches: 24 42 57.1%

Line Branch Exec Source
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 //
7 /*!
8 * \file
9 * \ingroup Nonlinear
10 * \brief An adapter for the Newton to manage models with primary variable switch
11 */
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>
17 #include <dumux/common/parameters.hh>
18
19 namespace Dumux {
20 namespace Detail {
21
22 //! helper aliases to extract a primary variable switch from the VolumeVariables (if defined, yields int otherwise)
23 template<class Variables>
24 using DetectPVSwitch = typename Variables::VolumeVariables::PrimaryVariableSwitch;
25
26 template<class Variables>
27 using PrimaryVariableSwitch = Dune::Std::detected_or_t<int, DetectPVSwitch, Variables>;
28
29 } // end namespace Detail
30
31 /*!
32 * \ingroup Nonlinear
33 * \brief Helper boolean to check if the given variables involve primary variable switching.
34 */
35 template<class Variables>
36 inline constexpr bool hasPriVarsSwitch = Dune::Std::is_detected<Detail::DetectPVSwitch, Variables>();
37
38 /*!
39 * \ingroup Nonlinear
40 * \brief An adapter for the Newton to manage models with primary variable switch
41 */
42 template <class Variables, bool isValid = hasPriVarsSwitch<Variables>>
43 class PrimaryVariableSwitchAdapter
44 {
45 using PrimaryVariableSwitch = typename Detail::PrimaryVariableSwitch<Variables>;
46
47 public:
48 59 PrimaryVariableSwitchAdapter(const std::string& paramGroup = "")
49 59 {
50
1/4
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
59 const int priVarSwitchVerbosity = getParamFromGroup<int>(paramGroup, "PrimaryVariableSwitch.Verbosity", 1);
51
3/6
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 59 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 59 times.
59 priVarSwitch_ = std::make_unique<PrimaryVariableSwitch>(priVarSwitchVerbosity);
52 59 }
53
54 /*!
55 * \brief Initialize the privar switch
56 */
57 template<class SolutionVector>
58 2582 void initialize(SolutionVector& sol, Variables& vars)
59 {
60 6426 priVarSwitch_->reset(sol.size());
61 3213 priVarsSwitchedInLastIteration_ = false;
62 3213 const auto& problem = vars.curGridVolVars().problem();
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
3213 const auto& gridGeometry = problem.gridGeometry();
64
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 631 times.
6426 priVarSwitch_->updateDirichletConstraints(problem, gridGeometry, vars, sol);
65 2582 }
66
67 /*!
68 * \brief Switch primary variables if necessary
69 */
70 template<class SolutionVector>
71 13984 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 13984 const auto& problem = vars.curGridVolVars().problem();
76 13984 const auto& gridGeometry = problem.gridGeometry();
77
78 // invoke the primary variable switch
79 27968 priVarsSwitchedInLastIteration_ = priVarSwitch_->update(uCurrentIter, vars, problem, gridGeometry);
80
2/2
✓ Branch 0 taken 1171 times.
✓ Branch 1 taken 12790 times.
13961 if (priVarsSwitchedInLastIteration_)
81 {
82
11/14
✓ Branch 2 taken 197825 times.
✓ Branch 3 taken 986 times.
✓ Branch 4 taken 496 times.
✓ Branch 5 taken 124 times.
✓ Branch 6 taken 862 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2300642 times.
✓ Branch 9 taken 862 times.
✓ Branch 10 taken 2300642 times.
✓ Branch 11 taken 862 times.
✓ Branch 13 taken 2300642 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 216353 times.
✗ Branch 17 not taken.
5166438 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
2/4
✓ Branch 1 taken 2300642 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1359142 times.
✗ Branch 5 not taken.
4003472 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
2/4
✓ Branch 1 taken 2300642 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2300642 times.
✗ Branch 5 not taken.
4996564 priVarSwitch_->updateSwitchedFluxVarsCache(problem, element, gridGeometry, vars, uCurrentIter);
89 }
90 }
91 13961 }
92
93 /*!
94 * \brief Whether the primary variables have been switched in the last call to invoke
95 */
96 bool switched() const
97 { return priVarsSwitchedInLastIteration_; }
98
99 private:
100 //! the class handling the primary variable switch
101 std::unique_ptr<PrimaryVariableSwitch> priVarSwitch_;
102 //! if we switched primary variables in the last iteration
103 bool priVarsSwitchedInLastIteration_ = false;
104 };
105
106 /*!
107 * \ingroup Nonlinear
108 * \brief An empty adapter for the Newton for models without primary variable switch
109 */
110 template <class Variables>
111 class PrimaryVariableSwitchAdapter<Variables, false>
112 {
113 public:
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
128