GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: dumux/dumux/nonlinear/primaryvariableswitchadapter.hh
Date: 2025-04-19 19:19:10
Exec Total Coverage
Lines: 25 25 100.0%
Functions: 115 115 100.0%
Branches: 27 44 61.4%

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-FileCopyrightText: 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
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
118 class PrimaryVariableSwitchAdapter
44 {
45 using PrimaryVariableSwitch = typename Detail::PrimaryVariableSwitch<Variables>;
46
47 public:
48 59 PrimaryVariableSwitchAdapter(const std::string& paramGroup = "")
49 59 {
50
1/2
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
59 const int priVarSwitchVerbosity = getParamFromGroup<int>(paramGroup, "PrimaryVariableSwitch.Verbosity", 1);
51
2/4
✓ Branch 1 taken 59 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 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 3213 void initialize(SolutionVector& sol, Variables& vars)
59 {
60 3213 priVarSwitch_->reset(sol.size());
61 3213 priVarsSwitchedInLastIteration_ = false;
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
3213 priVarSwitch_->updateDirichletConstraints(problem, gridGeometry, vars, sol);
65 2582 }
66
67 /*!
68 * \brief Switch primary variables if necessary
69 */
70 template<class SolutionVector>
71 13969 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 13969 const auto& problem = vars.curGridVolVars().problem();
76 13969 const auto& gridGeometry = problem.gridGeometry();
77
78 // invoke the primary variable switch
79 13969 priVarsSwitchedInLastIteration_ = priVarSwitch_->update(uCurrentIter, vars, problem, gridGeometry);
80
2/2
✓ Branch 0 taken 1175 times.
✓ Branch 1 taken 12771 times.
13946 if (priVarsSwitchedInLastIteration_)
81 {
82
7/10
✓ Branch 2 taken 986 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 862 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1359517 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1359142 times.
✓ Branch 11 taken 487 times.
✓ Branch 1 taken 204229 times.
✓ Branch 7 taken 941500 times.
4110580 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
1/2
✓ Branch 1 taken 2300642 times.
✗ Branch 2 not taken.
2504682 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
1/2
✓ Branch 1 taken 2300642 times.
✗ Branch 2 not taken.
2504682 priVarSwitch_->updateSwitchedFluxVarsCache(problem, element, gridGeometry, vars, uCurrentIter);
89 }
90 }
91 13946 }
92
93 /*!
94 * \brief Whether the primary variables have been switched in the last call to invoke
95 */
96 17500 bool switched() const
97
2/2
✓ Branch 0 taken 16352 times.
✓ Branch 1 taken 1148 times.
17500 { 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
7/12
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 12 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 0 taken 420 times.
✗ Branch 3 not taken.
461 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