GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/nonlinear/primaryvariableswitchadapter.hh
Date: 2024-09-21 20:52:54
Exec Total Coverage
Lines: 21 25 84.0%
Functions: 115 405 28.4%
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 57 PrimaryVariableSwitchAdapter(const std::string& paramGroup = "")
49 57 {
50
1/4
✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
57 const int priVarSwitchVerbosity = getParamFromGroup<int>(paramGroup, "PrimaryVariableSwitch.Verbosity", 1);
51
3/6
✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 57 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 57 times.
57 priVarSwitch_ = std::make_unique<PrimaryVariableSwitch>(priVarSwitchVerbosity);
52 57 }
53
54 /*!
55 * \brief Initialize the privar switch
56 */
57 template<class SolutionVector>
58 2573 void initialize(SolutionVector& sol, Variables& vars)
59 {
60 6408 priVarSwitch_->reset(sol.size());
61 3204 priVarsSwitchedInLastIteration_ = false;
62 3204 const auto& problem = vars.curGridVolVars().problem();
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
3204 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.
6408 priVarSwitch_->updateDirichletConstraints(problem, gridGeometry, vars, sol);
65 2573 }
66
67 /*!
68 * \brief Switch primary variables if necessary
69 */
70 template<class SolutionVector>
71 13924 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 13924 const auto& problem = vars.curGridVolVars().problem();
76 13924 const auto& gridGeometry = problem.gridGeometry();
77
78 // invoke the primary variable switch
79 27848 priVarsSwitchedInLastIteration_ = priVarSwitch_->update(uCurrentIter, vars, problem, gridGeometry);
80
2/2
✓ Branch 0 taken 1167 times.
✓ Branch 1 taken 12734 times.
13901 if (priVarsSwitchedInLastIteration_)
81 {
82
11/14
✓ Branch 2 taken 202253 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.
5170858 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.
4007904 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.
5005428 priVarSwitch_->updateSwitchedFluxVarsCache(problem, element, gridGeometry, vars, uCurrentIter);
89 }
90 }
91 13901 }
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