MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
scalar_constant.hpp
Go to the documentation of this file.
1 /*
2 * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3 * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #ifndef __mast_scalar_constant_h__
21 #define __mast_scalar_constant_h__
22 
23 namespace MAST {
24 namespace Base {
25 
26 template <typename ScalarType>
28 
29 public:
30 
31  using scalar_t = ScalarType;
32 
33  ScalarConstant(ScalarType v = 0.):
34  _v (v)
35  { }
36 
37  virtual ~ScalarConstant() {}
38 
39  inline ScalarType& operator= (const ScalarType& v) {
40  _v = v;
41  return _v;
42  }
43 
44  inline ScalarType& operator() () {
45  return _v;
46  }
47 
48  inline ScalarType operator() () const {
49  return _v;
50  }
51 
52  template <typename ContextType>
53  inline ScalarType value(ContextType& c) const {
54  return _v;
55  }
56 
57  template <typename ContextType>
58  inline void value(ContextType& c, ScalarType& v) const {
59  v = _v;
60  }
61 
62  template <typename ContextType, typename ScalarFieldType>
63  inline ScalarType derivative(ContextType& c,
64  const ScalarFieldType& f) const {
65  return &f==this?1.:0.;
66  }
67 
68  template <typename ContextType, typename ScalarFieldType>
69  inline void derivative(ContextType &c,
70  const ScalarFieldType &f,
71  ScalarType &v) const {
72  v = &f==this?1.:0.;
73  }
74 
75 private:
76 
77  ScalarType _v;
78 };
79 
80 } // namespace Base
81 } // namespace MAST
82 
83 #endif // __mast_scalar_constant_h__
ScalarType value(ContextType &c) const
void derivative(ContextType &c, const ScalarFieldType &f, ScalarType &v) const
ScalarType derivative(ContextType &c, const ScalarFieldType &f) const
void value(ContextType &c, ScalarType &v) const
ScalarType & operator=(const ScalarType &v)
ScalarConstant(ScalarType v=0.)