Debian Codegraph @

packages/rol/example/PDE-OPT/topo-opt/elasticity/example_01.cpp

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 example_01.cpp
11 \brief Shows how to solve the stuctural topology optimization problem.
12
13 */
14
15 #include "Teuchos_Comm.hpp"
16 #include "Teuchos_Time.hpp"
17 #include "ROL_Stream.hpp"
18 #include "Teuchos_GlobalMPISession.hpp"
19 #include "Teuchos_XMLParameterListHelpers.hpp"
20
21 #include "Tpetra_Core.hpp"
22 #include "Tpetra_Version.hpp"
23
24 #include <iostream>
25 #include <algorithm>
26
27 #include "ROL_Solver.hpp"
28 #include "ROL_Bounds.hpp"
29 #include "ROL_ConstraintFromObjective.hpp"
30 #include "ROL_LinearCombinationObjective.hpp"
31
32 #include "../../TOOLS/pdevector.hpp"
33 #include "../../TOOLS/pdeobjective.hpp"
34 #include "../../TOOLS/meshreader.hpp"
35
36 #include "src/mesh_topo-opt.hpp"
37 #include "src/pde_elasticity.hpp"
38
39 #include "../src/pde_filter.hpp"
40 #include "../src/filtered_compliance_robj.hpp"
41 #include "../src/volume_con.hpp"
42 #include "../src/volume_obj.hpp"
43 #include "../src/obj_volume.hpp"
44 #include "../src/obj_phasefield.hpp"
45
46 typedef double RealT;
47
48 int main(int argc, char *argv[]) {
49 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
50 int iprint = argc - 1;
51 ROL::Ptr<std::ostream> outStream;
52 ROL::nullstream bhs; // outputs nothing
53
54 /*** Initialize communicator. ***/
55 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
56 ROL::Ptr<const Teuchos::Comm<int>> comm
57 = Tpetra::getDefaultComm();
58 const int myRank = comm->getRank();
59 if ((iprint > 0) && (myRank == 0)) {
60 outStream = ROL::makePtrFromRef(std::cout);
61 }
62 else {
63 outStream = ROL::makePtrFromRef(bhs);
64 }
65 int errorFlag = 0;
66
67 // *** Example body.
68 try {
69 RealT tol(1e-8);
70
71 /*** Read in XML input ***/
72 std::string filename = "input_ex01.xml";
73 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
74 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
75
76 // Retrieve parameters.
77 const std::string example = parlist->sublist("Problem").get("Example", "Default");
78 const RealT volFraction = parlist->sublist("Problem").get("Volume Fraction", 0.4);
79 const RealT initDens = parlist->sublist("Problem").get("Initial Density",volFraction);
80 const RealT cmpFactor = parlist->sublist("Problem").get("Compliance Factor", 1.1);
81 int probDim = parlist->sublist("Problem").get("Problem Dimension", 2);
82 const std::string minType = parlist->sublist("Problem").get("Minimization Type", "Volume");
83 const bool usePhaseField = parlist->sublist("Problem").get("Use Phase Field", false);
84 bool useFilter = parlist->sublist("Problem").get("Use Filter", true);
85 const bool normalizeObj = parlist->sublist("Problem").get("Normalize Compliance", true);
86 const bool volEq = parlist->sublist("Problem").get("Use Volume Equality Constraint",true);
87 std::string hessAppr = parlist->sublist("Problem").get("Hessian Approximation","None");
88 hessAppr = (minType == "Compliance" ? hessAppr : "None");
89 if (example == "2D Wheel" ||
90 example == "2D Truss" ||
91 example == "2D Cantilever with 1 Load" ||
92 example == "2D Cantilever with 3 Loads" ||
93 example == "2D Beams" ||
94 example == "2D Carrier Plate")
95 probDim = 2;
96 else if (example == "3D Cantilever" ||
97 example == "3D L Beam")
98 probDim = 3;
99 if (usePhaseField) useFilter = false;
100
101 /*** Initialize main data structure. ***/
102 TEUCHOS_TEST_FOR_EXCEPTION(probDim<2 || probDim>3, std::invalid_argument,
103 ">>> PDE-OPT/topo-opt/elasticity/example_01.cpp: Problem dim is not 2 or 3!");
104 ROL::Ptr<MeshManager<RealT>> meshMgr;
105 if (probDim == 2) meshMgr = ROL::makePtr<MeshManager_TopoOpt<RealT>>(*parlist);
106 else meshMgr = ROL::makePtr<MeshReader<RealT>>(*parlist);
107 // Initialize PDE describing elasticity equations.
108 ROL::Ptr<PDE_Elasticity<RealT>>
109 pde = ROL::makePtr<PDE_Elasticity<RealT>>(*parlist);
110
111 // Initialize the filter PDE.
112 ROL::Ptr<PDE<RealT>> pdeFilter = pde;
113
114 // Initialize reduced compliance objective function.
115 ROL::Ptr<ROL::Objective<RealT>> robj_com;
116 ROL::Ptr<Assembler<RealT>> assembler, assemblerFilter;
117 if (useFilter && !usePhaseField) {
118 pdeFilter = ROL::makePtr<PDE_Filter<RealT>>(*parlist);
119 pde->setDensityFields(pdeFilter->getFields());
120 robj_com = ROL::makePtr<TopOptFilteredComplianceObjective<RealT>>(
121 pde,pdeFilter,meshMgr,comm,*parlist,*outStream);
122 assembler = ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->getAssembler();
123 assemblerFilter = ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->getFilterAssembler();
124 }
125 else {
126 robj_com = ROL::makePtr<TopOptComplianceObjective<RealT>>(
127 pde,meshMgr,comm,*parlist,*outStream);
128 assembler = ROL::dynamicPtrCast<TopOptComplianceObjective<RealT>>(robj_com)->getAssembler();
129 assemblerFilter = assembler;
130 }
131
132 // Create vectors.
133 ROL::Ptr<Tpetra::MultiVector<>> u_ptr, p_ptr, r_ptr, z_ptr;
134 u_ptr = assembler->createStateVector(); u_ptr->putScalar(0.0);
135 p_ptr = assembler->createStateVector(); p_ptr->putScalar(0.0);
136 r_ptr = assembler->createResidualVector(); r_ptr->putScalar(0.0);
137 z_ptr = assemblerFilter->createControlVector(); z_ptr->putScalar(1.0);
138 ROL::Ptr<ROL::Vector<RealT>> up, pp, rp, zp;
139 up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist);
140 pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist);
141 rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist);
142 zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pdeFilter,assemblerFilter,*parlist);
143
144 // Build volume objective function.
145 ROL::Ptr<QoI<RealT>> qoi_vol;
146 if (minType == "Compliance" && volEq) {
147 if (useFilter && !usePhaseField)
148 qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(ROL::dynamicPtrCast<PDE_Filter<RealT>>(pdeFilter)->getDensityFE(),
149 volFraction);
150 else
151 qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(pde->getDensityFE(),
152 volFraction);
153 }
154 else {
155 if (useFilter && !usePhaseField)
156 qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(ROL::dynamicPtrCast<PDE_Filter<RealT>>(pdeFilter)->getDensityFE());
157 else
158 qoi_vol = ROL::makePtr<QoI_Volume_TopoOpt<RealT>>(pde->getDensityFE());
159 }
160 ROL::Ptr<TopOptVolumeObjective<RealT>>
161 obj_vol = ROL::makePtr<TopOptVolumeObjective<RealT>>(qoi_vol,assemblerFilter,zp);
162 ROL::Ptr<TopOptVolumeConstraint<RealT>>
163 con_vol = ROL::makePtr<TopOptVolumeConstraint<RealT>>(qoi_vol,assemblerFilter,zp);
164
165 // Compute volume
166 zp->setScalar(1.0);
167 RealT vol = obj_vol->value(*zp,tol);
168
169 // Normalize compliance objective function
170 RealT cs(1);
171 zp->setScalar(initDens);
172 if (normalizeObj) {
173 if (useFilter && !usePhaseField)
174 cs = ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->normalize(*zp,tol);
175 else
176 cs = ROL::dynamicPtrCast<TopOptComplianceObjective<RealT>>(robj_com)->normalize(*zp,tol);
177 }
178
179 // Output problem details
180 *outStream << std::endl;
181 *outStream << "Problem Data" << std::endl;
182 *outStream << " Example: " << example << std::endl;
183 *outStream << " Dimension: " << probDim << std::endl;
184 *outStream << " Minimize Type: " << minType << std::endl;
185 *outStream << " Use Phase Field: " << usePhaseField << std::endl;
186 if (!usePhaseField) {
187 *outStream << " SIMP Power: "
188 << parlist->sublist("Problem").get("SIMP Power",3.0) << std::endl;
189 *outStream << " Use Filter: " << useFilter << std::endl;
190 }
191 if (minType == "Volume") {
192 *outStream << " Compliance Factor: " << cmpFactor << std::endl;
193 }
194 else if (minType == "Compliance") {
195 *outStream << " Volume Fraction: " << volFraction << std::endl;
196 *outStream << " Initial Density: " << initDens << std::endl;
197 *outStream << " Use Equality: " << volEq << std::endl;
198 *outStream << " Hessian Approx: " << hessAppr << std::endl;
199 }
200 *outStream << " Domain Volume: " << vol << std::endl;
201 *outStream << " Compliance Scale: " << cs << std::endl;
202 *outStream << std::endl;
203
204 // Create objective, constraint, multiplier and bounds
205 ROL::Ptr<ROL::Objective<RealT>> obj;
206 ROL::Ptr<ROL::Constraint<RealT>> icon;
207 ROL::Ptr<ROL::Vector<RealT>> iup, ilp;
208 ROL::Ptr<ROL::Vector<RealT>> imul;
209 ROL::Ptr<ROL::BoundConstraint<RealT>> ibnd;
210 if (!usePhaseField) {
211 if (minType == "Volume") {
212 obj = obj_vol;
213 icon = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>(robj_com);
214 // Set upper bound to compliance for solid beam.
215 zp->setScalar(1.0);
216 RealT comp = robj_com->value(*zp,tol);
217 ilp = ROL::makePtr<ROL::SingletonVector<RealT>>(0);
218 iup = ROL::makePtr<ROL::SingletonVector<RealT>>(cmpFactor*comp);
219 imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0);
220 ibnd = ROL::makePtr<ROL::Bounds<RealT>>(ilp,iup);
221 }
222 else if (minType == "Compliance") {
223 obj = robj_com;
224 icon = con_vol;
225 imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0);
226 if (volEq) {
227 ilp = ROL::nullPtr;
228 iup = ROL::nullPtr;
229 ibnd = ROL::nullPtr;
230 }
231 else {
232 // Set upper bound to fraction of total volume.
233 ilp = ROL::makePtr<ROL::SingletonVector<RealT>>(0);
234 iup = ROL::makePtr<ROL::SingletonVector<RealT>>(volFraction*vol);
235 ibnd = ROL::makePtr<ROL::Bounds<RealT>>(ilp,iup);
236 }
237 }
238 else if (minType == "Total") {
239 std::vector<ROL::Ptr<ROL::Objective<RealT>>> obj_vec(2,ROL::nullPtr);
240 obj_vec[0] = robj_com;
241 obj_vec[1] = obj_vol;
242 std::vector<RealT> weights(2,0.0);
243 weights[0] = static_cast<RealT>(1);
244 weights[1] = parlist->sublist("Problem").get("Volume Objective Scale",0.04096);
245 obj = ROL::makePtr<ROL::LinearCombinationObjective<RealT>>(weights,obj_vec);
246 icon = ROL::nullPtr;
247 ilp = ROL::nullPtr;
248 iup = ROL::nullPtr;
249 imul = ROL::nullPtr;
250 ibnd = ROL::nullPtr;
251 }
252 else {
253 throw ROL::Exception::NotImplemented("Unknown minimization type!");
254 }
255 }
256 else {
257 // Build Modica-Mortola Energy objective function.
258 RealT penParam = parlist->sublist("Problem").get("Phase Field Penalty Parameter",1e-1);
259 ROL::Ptr<QoI<RealT>>
260 qoi_pfe = ROL::makePtr<QoI_ModicaMortolaEnergy_PhaseField<RealT>>(pde->getDensityFE(),
261 penParam);
262 ROL::Ptr<IntegralOptObjective<RealT>>
263 obj_pfe = ROL::makePtr<IntegralOptObjective<RealT>>(qoi_pfe,assembler);
264 // Get weights for linear combination objective.
265 std::vector<RealT> weights(1,0.0);
266 weights[0] = parlist->sublist("Problem").get("Phase Field Energy Objective Scale",0.00064);
267 std::vector<ROL::Ptr<ROL::Objective<RealT>>> obj_vec(1,ROL::nullPtr);
268 obj_vec[0] = obj_pfe;
269 if (minType == "Volume") {
270 weights.push_back(parlist->sublist("Problem").get("Volume Objective Scale",0.04096));
271 obj_vec.push_back(obj_vol);
272 obj = ROL::makePtr<ROL::LinearCombinationObjective<RealT>>(weights,obj_vec);
273 icon = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>(robj_com);
274 // Set upper bound to compliance for solid beam.
275 RealT comp = robj_com->value(*zp,tol);
276 ilp = ROL::makePtr<ROL::SingletonVector<RealT>>(0);
277 iup = ROL::makePtr<ROL::SingletonVector<RealT>>(cmpFactor*comp);
278 imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0);
279 ibnd = ROL::makePtr<ROL::Bounds<RealT>>(ilp,iup);
280 }
281 else if (minType == "Compliance") {
282 weights.push_back(static_cast<RealT>(1));
283 obj_vec.push_back(robj_com);
284 obj = ROL::makePtr<ROL::LinearCombinationObjective<RealT>>(weights,obj_vec);
285 icon = con_vol;
286 imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0);
287 if (volEq) {
288 ilp = ROL::nullPtr;
289 iup = ROL::nullPtr;
290 ibnd = ROL::nullPtr;
291 }
292 else {
293 // Set upper bound to fraction of total volume.
294 ilp = ROL::makePtr<ROL::SingletonVector<RealT>>(0);
295 iup = ROL::makePtr<ROL::SingletonVector<RealT>>(volFraction*vol);
296 ibnd = ROL::makePtr<ROL::Bounds<RealT>>(ilp,iup);
297 }
298 }
299 else if (minType == "Total") {
300 weights.push_back(static_cast<RealT>(1));
301 obj_vec.push_back(robj_com);
302 weights.push_back(parlist->sublist("Problem").get("Volume Objective Scale",0.04096));
303 obj_vec.push_back(obj_vol);
304 obj = ROL::makePtr<ROL::LinearCombinationObjective<RealT>>(weights,obj_vec);
305 icon = ROL::nullPtr;
306 ilp = ROL::nullPtr;
307 iup = ROL::nullPtr;
308 imul = ROL::nullPtr;
309 ibnd = ROL::nullPtr;
310 }
311 else {
312 throw ROL::Exception::NotImplemented("Unknown minimization type!");
313 }
314 }
315
316 // Initialize bound constraints.
317 RealT lval = (usePhaseField ? -1.0 : 0.0), uval = 1.0;
318 ROL::Ptr<ROL::Vector<RealT>> lop = zp->clone(); lop->setScalar(lval);
319 ROL::Ptr<ROL::Vector<RealT>> hip = zp->clone(); hip->setScalar(uval);
320 ROL::Ptr<ROL::BoundConstraint<RealT>>
321 bnd = ROL::makePtr<ROL::Bounds<RealT>>(lop,hip);
322 if (usePhaseField) bnd->deactivate();
323
324 // Set up optimization problem.
325 ROL::Ptr<ROL::Problem<RealT>>
326 prob = ROL::makePtr<ROL::Problem<RealT>>(obj,zp);
327 if (bnd->isActivated()) prob->addBoundConstraint(bnd);
328 if ( minType == "Compliance" ) {
329 if ( volEq ) prob->addLinearConstraint("Volume",icon,imul);
330 else prob->addLinearConstraint("Volume",icon,imul,ibnd);
331 }
332 else if ( minType == "Volume" )
333 prob->addConstraint("Compliance",icon,imul,ibnd);
334
335 bool expLin = parlist->sublist("Problem").get("Project Linear Constraints",true);
336 prob->setProjectionAlgorithm(*parlist);
337 prob->finalize(!expLin,true,*outStream);
338
339 // Check derivatives.
340 bool derivCheck = parlist->sublist("Problem").get("Check derivatives",false);
341 if (derivCheck) prob->check(true,*outStream);
342
343 // Solve optimization problem.
344 Teuchos::Time algoTimer("Algorithm Time", true);
345 ROL::Solver<RealT> solver(prob,*parlist);
346 solver.solve(*outStream);
347 algoTimer.stop();
348 *outStream << "Total optimization time = " << algoTimer.totalElapsedTime() << " seconds.\n";
349 // Output.
350 if (useFilter && !usePhaseField) {
351 ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->summarize(*outStream);
352 ROL::dynamicPtrCast<TopOptFilteredComplianceObjective<RealT>>(robj_com)->printToFile(*zp,*outStream);
353 }
354 else {
355 ROL::dynamicPtrCast<TopOptComplianceObjective<RealT>>(robj_com)->summarize(*outStream);
356 ROL::dynamicPtrCast<TopOptComplianceObjective<RealT>>(robj_com)->printToFile(*zp,*outStream);
357 }
358 if (minType == "Compliance") {
359 con_vol->summarize(*outStream);
360 }
361 else if (minType == "Volume") {
362 obj_vol->summarize(*outStream);
363 }
364
365 // Get a summary from the time monitor.
366 Teuchos::TimeMonitor::summarize();
367 }
368 catch (std::logic_error& err) {
369 *outStream << err.what() << "\n";
370 errorFlag = -1000;
371 }; // end try
372
373 if (errorFlag != 0)
374 std::cout << "End Result: TEST FAILED\n";
375 else
376 std::cout << "End Result: TEST PASSED\n";
377
378 return 0;
379 }