version 3.8
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-FileCopyrightInfo: 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
57 {
58 stateIsSet_ = false;
59 }
60
61private:
62 StateType state_;
63 bool stateIsSet_{false};
64};
65
70template<class PrimaryVariables, class StateType>
71struct NumEqVectorTraits<SwitchablePrimaryVariables<PrimaryVariables, StateType>>
72{
73 static constexpr std::size_t numEq = PrimaryVariables::size();
74 using type = PrimaryVariables;
75};
76
77} // end namespace Dumux
78
79// specialize field traits for this type
80namespace Dune {
81
82template <class PrimaryVariables, class StateType>
83struct FieldTraits<Dumux::SwitchablePrimaryVariables<PrimaryVariables, StateType>>
84: public FieldTraits<PrimaryVariables>
85{};
86
87} // end namespace Dune
88
89#endif
A primary variable vector with a state to allow variable switches.
Definition: switchableprimaryvariables.hh:28
void invalidateState()
Invalidate the state.
Definition: switchableprimaryvariables.hh:56
StateType state() const
Ask for the state of this primary variable object, e.g. the phase presence.
Definition: switchableprimaryvariables.hh:37
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:74
Definition: numeqvector.hh:21
static constexpr std::size_t numEq
Definition: numeqvector.hh:22