Debian Codegraph @

packages/rol/example/PDE-OPT/topo-opt/multimat/src/con_volume.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /*! \file obj.hpp
11 \brief Provides the interface for local (cell-based) objective function computations.
12 */
13
14 #ifndef PDEOPT_QOI_VOLUME_MULTIMAT_HPP
15 #define PDEOPT_QOI_VOLUME_MULTIMAT_HPP
16
17 #include "ROL_StdConstraint.hpp"
18 #include "ROL_ParameterList.hpp"
19
20 template<class Real>
21 class Volume_TopoOpt : public ROL::StdConstraint<Real> {
22 private:
23 Real w0_, vol_;
24 std::vector<Real> w_;
25 int N_, M_, T_;
26
27 public:
28 Volume_TopoOpt(ROL::ParameterList &list) {
29 w_ = ROL::getArrayFromStringParameter<Real>(list.sublist("Problem"), "Density");
30 M_ = list.sublist("Problem").get("Number of Horizontal Cells",10);
31 N_ = list.sublist("Problem").get("Number of Vertical Cells",20);
32 T_ = w_.size();
33
34 Real width = list.sublist("Geometry").get("Width",2.0);
35 Real height = list.sublist("Geometry").get("Height",1.0);
36 vol_ = width*height/static_cast<Real>(M_*N_);
37
38 Real mw(0);
39 for (int i = 0; i < T_; ++i) {
40 mw = std::max(mw,w_[i]);
41 }
42 w0_ = list.sublist("Problem").get("Maximum Weight Fraction", 0.5) * mw * width * height;
43 }
44
45 void value( std::vector<Real> &c, const std::vector<Real> &x, Real &tol ) {
46 c[0] = -w0_;
47 for (int i=0; i<M_; ++i) {
48 for (int j=0; j<N_; ++j) {
49 for (int k=0; k<T_; ++k) {
50 c[0] += vol_ * w_[k] * x[i + M_*(j + N_*k)];
51 }
52 }
53 }
54 }
55
56 void applyJacobian( std::vector<Real> &jv, const std::vector<Real> &v,
57 const std::vector<Real> &x, Real &tol ) {
58 jv[0] = static_cast<Real>(0);
59 for (int i=0; i<M_; ++i) {
60 for (int j=0; j<N_; ++j) {
61 for (int k=0; k<T_; ++k) {
62 jv[0] += vol_ * w_[k] * v[i + M_*(j + N_*k)];
63 }
64 }
65 }
66 }
67
68 void applyAdjointJacobian( std::vector<Real> &ajv, const std::vector<Real> &v,
69 const std::vector<Real> &x, Real &tol ) {
70 for (int i=0; i<M_; ++i) {
71 for (int j=0; j<N_; ++j) {
72 for (int k=0; k<T_; ++k) {
73 ajv[i + M_*(j + N_*k)] = vol_ * w_[k] * v[0];
74 }
75 }
76 }
77 }
78
79 void applyAdjointHessian( std::vector<Real> &ahuv, const std::vector<Real> &u,
80 const std::vector<Real> &v, const std::vector<Real> &x,
81 Real &tol ) {
82 int size = ahuv.size();
83 ahuv.assign(size,static_cast<Real>(0));
84 }
85 };
86
87 template<class Real>
88 class Selection_TopoOpt : public ROL::StdConstraint<Real> {
89 private:
90 int N_, M_, T_;
91
92 public:
93 Selection_TopoOpt(ROL::ParameterList &list) {
94 std::vector<Real> w = ROL::getArrayFromStringParameter<Real>(list.sublist("Problem"), "Density");
95 M_ = list.sublist("Problem").get("Number of Horizontal Cells",10);
96 N_ = list.sublist("Problem").get("Number of Vertical Cells",20);
97 T_ = w.size();
98 }
99
100 void value( std::vector<Real> &c, const std::vector<Real> &x, Real &tol ) {
101 for (int i=0; i<M_; ++i) {
102 for (int j=0; j<N_; ++j) {
103 c[i + M_*j] = static_cast<Real>(-1);
104 for (int k=0; k<T_; ++k) {
105 c[i + M_*j] += x[i + M_*(j + N_*k)];
106 }
107 }
108 }
109 }
110
111 void applyJacobian( std::vector<Real> &jv, const std::vector<Real> &v,
112 const std::vector<Real> &x, Real &tol ) {
113 for (int i=0; i<M_; ++i) {
114 for (int j=0; j<N_; ++j) {
115 jv[i + M_*j] = static_cast<Real>(0);
116 for (int k=0; k<T_; ++k) {
117 jv[i + M_*j] += v[i + M_*(j + N_*k)];
118 }
119 }
120 }
121 }
122
123 void applyAdjointJacobian( std::vector<Real> &ajv, const std::vector<Real> &v,
124 const std::vector<Real> &x, Real &tol ) {
125 for (int i=0; i<M_; ++i) {
126 for (int j=0; j<N_; ++j) {
127 for (int k=0; k<T_; ++k) {
128 ajv[i + M_*(j + N_*k)] = v[i + M_*j];
129 }
130 }
131 }
132 }
133
134 void applyAdjointHessian( std::vector<Real> &ahuv, const std::vector<Real> &u,
135 const std::vector<Real> &v, const std::vector<Real> &x,
136 Real &tol ) {
137 int size = ahuv.size();
138 ahuv.assign(size,static_cast<Real>(0));
139 }
140 };
141
142 #endif