Debian Codegraph @

packages/rol/example/PDE-OPT/topo-opt/multimat/opfactory.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 OPFACTORY_TOPOOPT_MULTIMAT_HPP
11 #define OPFACTORY_TOPOOPT_MULTIMAT_HPP
12
13 #include "ROL_Bounds.hpp"
14 #include "ROL_Problem.hpp"
15
16 #include "../../TOOLS/pdevector.hpp"
17 #include "../../TOOLS/meshreader.hpp"
18 #include "src/mesh_topo-opt.hpp"
19 #include "src/pde_elasticity.hpp"
20 #include "src/pde_filter.hpp"
21 #include "src/pde_selection.hpp"
22 #include "src/obj_volume.hpp"
23 #include "src/compliance_robj.hpp"
24 #include "src/filtered_compliance_robj.hpp"
25 #include "src/con_selection.hpp"
26 #include "src/volume_con.hpp"
27
28 template<typename Real>
29 class ElasticityFactory {
30 private:
31 ROL::ParameterList pl_;
32 ROL::Ptr<const Teuchos::Comm<int>> comm_;
33 ROL::Ptr<std::ostream> os_;
34
35 ROL::Ptr<MeshManager<Real>> mesh_;
36 ROL::Ptr<PDE_MultiMat_Elasticity<Real>> pde_;
37 ROL::Ptr<MultiMat_PDE_Filter<Real>> filter_;
38 bool useFilter_;
39
40 ROL::Ptr<PDE_MultiMat_Selection<Real>> spde_;
41 ROL::Ptr<ROL::Constraint<Real>> scon_;
42 ROL::Ptr<Assembler<Real>> sassembler_;
43 ROL::Ptr<ROL::Vector<Real>> smul_;
44 ROL::Ptr<ROL::Vector<Real>> slo_, sup_;
45 ROL::Ptr<ROL::BoundConstraint<Real>> sbnd_;
46
47 ROL::Ptr<QoI_MultiMat_Weight<Real>> vqoi_;
48 ROL::Ptr<ROL::Constraint<Real>> vcon_;
49 ROL::Ptr<ROL::Vector<Real>> vmul_;
50 ROL::Ptr<ROL::Vector<Real>> vlo_, vup_;
51 ROL::Ptr<ROL::BoundConstraint<Real>> vbnd_;
52
53 bool useIneq_;
54
55 ROL::Ptr<ROL::Vector<Real>> z_, zlo_, zup_;
56 ROL::Ptr<ROL::BoundConstraint<Real>> bnd_;
57
58 ROL::Ptr<ROL::Objective<Real>> obj_;
59
60 public:
61 ElasticityFactory(ROL::ParameterList &pl,
62 const ROL::Ptr<const Teuchos::Comm<int>> &comm,
63 const ROL::Ptr<std::ostream> &os)
64 : pl_(pl), comm_(comm), os_(os) {
65 // Elasticity PDE
66 int probDim = pl_.sublist("Problem").get("Problem Dimension",2);
67 if (probDim == 2) {
68 mesh_ = ROL::makePtr<MeshManager_MultiMat<Real>>(pl_);
69 } else if (probDim == 3) {
70 mesh_ = ROL::makePtr<MeshReader<Real>>(pl_);
71 }
72 else {
73 TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
74 ">>> PDE-OPT/topo-opt/multimat/example_01.cpp: Problem dim is not 2 or 3!");
75 }
76 pde_ = ROL::makePtr<PDE_MultiMat_Elasticity<Real>>(pl_);
77
78 // Selection constraint
79 spde_ = ROL::makePtr<PDE_MultiMat_Selection<Real>>(pl_);
80 scon_ = ROL::makePtr<MultiMat_Selection_Constraint<Real>>(spde_, mesh_, comm_, pl_, *os_);
81 sassembler_ = ROL::dynamicPtrCast<MultiMat_Selection_Constraint<Real>>(scon_)->getAssembler();
82 smul_ = ROL::makePtr<PDE_DualSimVector<Real>>(sassembler_->createStateVector(),spde_,sassembler_,pl_);
83 slo_ = smul_->dual().clone(); slo_->setScalar(static_cast<Real>(-1));
84 sup_ = smul_->dual().clone(); sup_->setScalar(static_cast<Real>(0));
85 sbnd_ = ROL::makePtr<ROL::Bounds<Real>>(slo_,sup_);
86
87 // Create density vector
88 z_ = ROL::makePtr<PDE_PrimalOptVector<Real>>(sassembler_->createControlVector(),spde_,sassembler_,pl_);
89 //z_->setScalar(static_cast<Real>(1.0/3.0));
90 //z_->zero();
91 z_->setScalar(static_cast<Real>(1));
92
93 // Volume constraint
94 Real tol(std::sqrt(ROL::ROL_EPSILON<Real>()));
95 ROL::Ptr<ROL::Vector<Real>> z0 = z_->clone(); z0->zero();
96 vqoi_ = ROL::makePtr<QoI_MultiMat_Weight<Real>>(spde_->getDensityFE(), spde_->getDensityFieldInfo(), pl_);
97 vcon_ = ROL::makePtr<MultiMat_Volume_Constraint<Real>>(vqoi_,sassembler_,z_);
98 vmul_ = ROL::makePtr<ROL::SingletonVector<Real>>();
99 vlo_ = vmul_->dual().clone(); vcon_->value(*vlo_,*z0,tol);
100 vup_ = vmul_->dual().clone(); vup_->setScalar(static_cast<Real>(0));
101 vbnd_ = ROL::makePtr<ROL::Bounds<Real>>(vlo_,vup_);
102
103 useIneq_ = pl_.sublist("Problem").get("Use Inequality", false);
104
105 // Density bound vectors
106 zlo_ = z_->clone(); zlo_->setScalar(static_cast<Real>(0));
107 zup_ = z_->clone(); zup_->setScalar(static_cast<Real>(1));
108 bnd_ = ROL::makePtr<ROL::Bounds<Real>>(zlo_,zup_);
109
110 // Objective function
111 useFilter_ = pl_.sublist("Problem").get("Use Filter", false);
112 if (useFilter_) {
113 filter_ = ROL::makePtr<MultiMat_PDE_Filter<Real>>(pl_);
114 pde_->setDensityFields(filter_->getFields());
115 obj_ = ROL::makePtr<MultiMat_Filtered_Compliance_Objective<Real>>(pde_, filter_, mesh_, comm_, pl_, *os_);
116 }
117 else {
118 obj_ = ROL::makePtr<MultiMat_Compliance_Objective<Real>>(pde_, mesh_, comm_, pl_, *os_);
119 }
120 }
121
122 void check(void) {
123 build()->check(true,*os_);
124 }
125
126 ROL::Ptr<ROL::Problem<Real>> build(void) {
127 ROL::Ptr<ROL::Problem<Real>> prob
128 = ROL::makePtr<ROL::Problem<Real>>(obj_,z_);
129 prob->addBoundConstraint(bnd_);
130 prob->addLinearConstraint("Weight",vcon_,vmul_,vbnd_);
131 if (!useIneq_) {
132 prob->addLinearConstraint("Selection",scon_,smul_);
133 }
134 else {
135 prob->addLinearConstraint("Selection",scon_,smul_,sbnd_);
136 }
137 prob->setProjectionAlgorithm(pl_);
138 prob->finalize(false,true,*os_);
139 return prob;
140 }
141
142 void print(const ROL::Vector<Real> &z) {
143 if (useFilter_)
144 ROL::dynamicPtrCast<MultiMat_Filtered_Compliance_Objective<Real>>(obj_)->printToFile(
145 *dynamic_cast<const ROL::PartitionedVector<Real>&>(z).get(0),*os_);
146 else
147 ROL::dynamicPtrCast<MultiMat_Compliance_Objective<Real>>(obj_)->printToFile(
148 *dynamic_cast<const ROL::PartitionedVector<Real>&>(z).get(0),*os_);
149 }
150 };
151
152 #endif