Debian Codegraph @

packages/rol/example/PDE-OPT/0ld/elasticitySIMP_topologyOptimization/volume_constraint.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 #ifndef ROL_PDEOPT_ELASTICITYSIMP_CONSTRAINT_VOLUME_H
11 #define ROL_PDEOPT_ELASTICITYSIMP_CONSTRAINT_VOLUME_H
12
13 #include "ROL_Constraint.hpp"
14 #include "ROL_TpetraMultiVector.hpp"
15 #include "ROL_StdVector.hpp"
16 #include "ROL_RiskVector.hpp"
17
18 template<class Real>
19 class EqualityConstraint_PDEOPT_ElasticitySIMP_Volume : public ROL::Constraint<Real> {
20 private:
21 Real volFrac_;
22 Real totalMeasure_;
23 ROL::Ptr<Tpetra::MultiVector<> > cellMeasures_;
24
25 ROL::Vector<Real> & castRiskVector(ROL::Vector<Real> &x) const {
26 try {
27 ROL::RiskVector<Real> & rx = dynamic_cast<ROL::RiskVector<Real>&>(x);
28 return *(rx.getVector());
29 }
30 catch (std::exception &e) {
31 return x;
32 }
33 }
34
35 const ROL::Vector<Real> & castConstRiskVector(const ROL::Vector<Real> &x) const {
36 try {
37 const ROL::RiskVector<Real> & rx = dynamic_cast<const ROL::RiskVector<Real>&>(x);
38 return *(rx.getVector());
39 }
40 catch (std::exception &e) {
41 return x;
42 }
43 }
44
45 public:
46 EqualityConstraint_PDEOPT_ElasticitySIMP_Volume(const ROL::Ptr<ElasticitySIMPOperators<Real> > &data,
47 const Teuchos::RCP<Teuchos::ParameterList> &parlist) {
48 volFrac_ = parlist->sublist("ElasticityTopoOpt").get<Real>("Volume Fraction");
49 cellMeasures_= data->getCellAreas();
50 Teuchos::Array<Real> sumM(1, 0);
51 cellMeasures_->norm1(sumM);
52 totalMeasure_ = sumM[0];
53 // std::cout << "Volume Constraints: volfrac_= " << volFrac_
54 // << ", totalMeasure_= " << totalMeasure_
55 // << std::endl;
56 }
57
58 using ROL::Constraint<Real>::value;
59 void value(ROL::Vector<Real> &c,
60 const ROL::Vector<Real> &z,
61 Real &tol) {
62 ROL::Ptr<std::vector<Real> > cp = (dynamic_cast<ROL::StdVector<Real>&>(c)).getVector();
63 const ROL::Vector<Real> & zr = castConstRiskVector(z);
64 ROL::Ptr<const Tpetra::MultiVector<> > zp
65 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(zr)).getVector();
66
67 // ROL::Ptr<Tpetra::MultiVector<> > unit
68 // = ROL::makePtr<Tpetra::MultiVector<>>(zp->getMap(), 1, true);
69 // unit->putScalar(1.0);
70 // Teuchos::Array<Real> sumZ(1, 0);
71 // zp->dot(*unit, sumZ);
72 // (*cp)[0] = sumZ[0] - static_cast<Real>(zp->getGlobalLength())*volFrac_;
73
74 Teuchos::Array<Real> sumZ(1, 0);
75 cellMeasures_->dot(*zp, sumZ);
76 (*cp)[0] = sumZ[0] - totalMeasure_ * volFrac_;
77 // std::cout << sumZ[0] << ", "
78 // << totalMeasure_ << ", "
79 // << volFrac_ << std::endl;
80 }
81
82 void applyJacobian(ROL::Vector<Real> &jv,
83 const ROL::Vector<Real> &v,
84 const ROL::Vector<Real> &z,
85 Real &tol) {
86 ROL::Ptr<std::vector<Real> > jvp = (dynamic_cast<ROL::StdVector<Real>&>(jv)).getVector();
87 const ROL::Vector<Real> & vr = castConstRiskVector(v);
88 ROL::Ptr<const Tpetra::MultiVector<> > vp
89 = (dynamic_cast<const ROL::TpetraMultiVector<Real>&>(vr)).getVector();
90
91 // ROL::Ptr<Tpetra::MultiVector<> > unit
92 // = ROL::makePtr<Tpetra::MultiVector<>>(vp->getMap(), 1, true);
93 // unit->putScalar(1.0);
94 // Teuchos::Array<Real> sumV(1, 0);
95 // vp->dot(*unit, sumV);
96 // (*jvp)[0] = sumV[0];
97
98 Teuchos::Array<Real> sumV(1, 0);
99 cellMeasures_->dot(*vp, sumV);
100 (*jvp)[0] = sumV[0];
101 }
102
103 void applyAdjointJacobian(ROL::Vector<Real> &ajv,
104 const ROL::Vector<Real> &v,
105 const ROL::Vector<Real> &z,
106 Real &tol) {
107 ROL::Vector<Real> & ajvr = castRiskVector(ajv);
108 ROL::Ptr<Tpetra::MultiVector<> > ajvp
109 = (dynamic_cast<ROL::TpetraMultiVector<Real>&>(ajvr)).getVector();
110 ROL::Ptr<const std::vector<Real> > vp
111 = (dynamic_cast<const ROL::StdVector<Real>&>(v)).getVector();
112
113 // ajvp->putScalar(1.0);
114 // ajvp->scale((*vp)[0]);
115
116 Tpetra::deep_copy(*ajvp, *cellMeasures_);
117 ajvp->scale((*vp)[0]);
118 }
119
120 void applyAdjointHessian(ROL::Vector<Real> &ahwv,
121 const ROL::Vector<Real> &w,
122 const ROL::Vector<Real> &v,
123 const ROL::Vector<Real> &z,
124 Real &tol) {
125 ahwv.zero();
126 }
127
128 };
129
130 #endif