MAST3
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
penalized_density.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_density_h__
21 #define __mast_simp_penalized_density_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 DensityFieldType>
34 
35 public:
36 
38  _p (0),
39  _d (nullptr)
40  { }
41 
42  virtual ~PenalizedDensity() {}
43 
44  inline void set_density_field(const DensityFieldType& d) { _d = &d;}
45 
46  inline void set_penalty(const real_t p) { _p = p;}
47 
48  template <typename ContextType>
49  inline ScalarType value(const ContextType& c) const {
50 
51  Assert0(_p, "Penalty value not initialized");
52  Assert0(_d, "Density field not initialized");
53 
54  return pow(_d->value(c), _p);
55  }
56 
57  template <typename ContextType, typename ScalarFieldType>
58  inline ScalarType derivative(const ContextType& c,
59  const ScalarFieldType& f) const {
60 
61  Assert0(_p, "Penalty value not initialized");
62  Assert0(_d, "Density field not initialized");
63 
64  return _p * pow(_d->value(c), _p-1.) * _d->derivative(c, f);
65  }
66 
67 
68 private:
69 
71  const DensityFieldType *_d;
72 };
73 } // namespace SIMP
74 } // namespace Topology
75 } // namespace Optimization
76 } // namespace MAST
77 
78 #endif // __mast_simp_penalized_density_h__
ScalarType derivative(const ContextType &c, const ScalarFieldType &f) const
ScalarType value(const ContextType &c) const
#define Assert0(cond, msg)
Definition: exceptions.hpp:134
double real_t