version 3.11-dev
switchableprimaryvariables.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-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
13#ifndef DUMUX_SWITCHABLE_PRIMARY_VARIABLES_HH
14#define DUMUX_SWITCHABLE_PRIMARY_VARIABLES_HH
15
16#include <dune/common/ftraits.hh>
17#include <dune/common/exceptions.hh>
19
20namespace Dumux {
21
26template<class PrimaryVariables, class StateType>
27class SwitchablePrimaryVariables : public PrimaryVariables
28{
29 using ParentType = PrimaryVariables;
30public:
32 using ParentType::ParentType;
34 using ParentType::operator=;
35
37 StateType state() const
38 {
39 if (!stateIsSet_)
40 DUNE_THROW(Dune::InvalidStateException, "Model demands setting a primary variable state (like a phase presence)"
41 << " but none was set! Use its setState method to set the state.");
42
43 return state_;
44 }
45
47 void setState(StateType state)
48 {
49 // NOTE: we use a copy instead of a reference in the signature to
50 // avoid linker errors related to passing a static variable to this function
51 state_ = std::move(state);
52 stateIsSet_ = true;
53 }
54
56 bool stateIsSet() const
57 {
58 return stateIsSet_;
59 }
60
63 {
64 stateIsSet_ = false;
65 }
66
67private:
68 StateType state_;
69 bool stateIsSet_{false};
70};
71
76template<class PrimaryVariables, class StateType>
77struct NumEqVectorTraits<SwitchablePrimaryVariables<PrimaryVariables, StateType>>
78{
79 static constexpr std::size_t numEq = PrimaryVariables::size();
80 using type = PrimaryVariables;
81};
82
83} // end namespace Dumux
84
85// specialize field traits for this type
86namespace Dune {
87
88template <class PrimaryVariables, class StateType>
89struct FieldTraits<Dumux::SwitchablePrimaryVariables<PrimaryVariables, StateType>>
90: public FieldTraits<PrimaryVariables>
91{};
92
93} // end namespace Dune
94
95#endif
A primary variable vector with a state to allow variable switches.
Definition: switchableprimaryvariables.hh:28
void invalidateState()
Invalidate the state.
Definition: switchableprimaryvariables.hh:62
StateType state() const
Ask for the state of this primary variable object, e.g. the phase presence.
Definition: switchableprimaryvariables.hh:37
bool stateIsSet() const
Ask if the state has been set.
Definition: switchableprimaryvariables.hh:56
void setState(StateType state)
Set the state of this primary variable object, e.g. the phase presence.
Definition: switchableprimaryvariables.hh:47
Definition: adapt.hh:17
Definition: common/pdesolver.hh:24
A helper to deduce a vector with the same size as numbers of equations.
PrimaryVariables type
Definition: switchableprimaryvariables.hh:80
Definition: numeqvector.hh:21
static constexpr std::size_t numEq
Definition: numeqvector.hh:22