MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
penalized_scalar.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_simp_penalized_scalar_h__
21 #define __mast_simp_penalized_scalar_h__
22 
23 // MAST includes
25 #include <mast/base/exceptions.hpp>
26 
27 namespace MAST {
28 namespace Optimization {
29 namespace Topology {
30 namespace SIMP {
31 
32 template <typename ScalarType, typename PenalizedDensityType>
34 
35 public:
36 
37  using scalar_t = ScalarType;
38 
40  _v0 (0.),
41  _v_min (0.),
42  _d (nullptr)
43  { }
44 
45  virtual ~PenalizedScalar() {}
46 
47  inline void set_density(const PenalizedDensityType &d) { _d = &d;}
48 
49  inline void set_scalar(const ScalarType v0,
50  const ScalarType vmin) {
51  _v0 = v0;
52  _v_min = vmin;
53  }
54 
55  template <typename ContextType>
56  inline ScalarType value(const ContextType& c) const {
57 
58  Assert0(_d, "Density field not initialized");
59 
60  return _v_min + _v0 * _d->value(c);
61  }
62 
63  template <typename ContextType, typename ScalarFieldType>
64  inline ScalarType derivative(const ContextType& c,
65  const ScalarFieldType& f) const {
66 
67  Assert0( _d, "Density field not initialized");
68 
69  return _v0 * _d->derivative(c, f);
70  }
71 
72 
73 private:
74 
75  ScalarType _v0;
76  ScalarType _v_min;
77  const PenalizedDensityType *_d;
78 };
79 } // namespace SIMP
80 } // namespace Topology
81 } // namespace Optimization
82 } // namespace MAST
83 
84 #endif // __mast_simp_penalized_scalar_h__
ScalarType value(const ContextType &c) const
void set_scalar(const ScalarType v0, const ScalarType vmin)
void set_density(const PenalizedDensityType &d)
ScalarType derivative(const ContextType &c, const ScalarFieldType &f) const
#define Assert0(cond, msg)
Definition: exceptions.hpp:134