Debian Codegraph @

packages/rol/example/PDE-OPT/topo-opt/navier-stokes/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 Navier-Stokes control problem.
12 */
13
14 #include "Teuchos_Comm.hpp"
15 #include "Teuchos_Time.hpp"
16 #include "ROL_Stream.hpp"
17 #include "Teuchos_GlobalMPISession.hpp"
18 #include "Teuchos_XMLParameterListHelpers.hpp"
19
20 #include "Tpetra_Core.hpp"
21 #include "Tpetra_Version.hpp"
22
23 #include <iostream>
24 #include <algorithm>
25
26 #include "ROL_TpetraMultiVector.hpp"
27 #include "ROL_Reduced_Objective_SimOpt.hpp"
28 #include "ROL_Bounds.hpp"
29 #include "ROL_Solver.hpp"
30 #include "ROL_SingletonVector.hpp"
31 #include "ROL_ConstraintFromObjective.hpp"
32
33 #include "../../TOOLS/meshreader.hpp"
34 #include "../../TOOLS/pdeconstraint.hpp"
35 #include "../../TOOLS/pdeobjective.hpp"
36 #include "../../TOOLS/pdevector.hpp"
37 #include "../../TOOLS/integralconstraint.hpp"
38
39 #include "pde_navier-stokes.hpp"
40 #include "obj_navier-stokes.hpp"
41
42 typedef double RealT;
43
44 int main(int argc, char *argv[]) {
45 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
46 int iprint = argc - 1;
47 ROL::Ptr<std::ostream> outStream;
48 ROL::nullstream bhs; // outputs nothing
49
50 /*** Initialize communicator. ***/
51 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
52 ROL::Ptr<const Teuchos::Comm<int>> comm
53 = Tpetra::getDefaultComm();
54 const int myRank = comm->getRank();
55 if ((iprint > 0) && (myRank == 0)) {
56 outStream = ROL::makePtrFromRef(std::cout);
57 }
58 else {
59 outStream = ROL::makePtrFromRef(bhs);
60 }
61 int errorFlag = 0;
62
63 // *** Example body.
64 try {
65
66 /*** Read in XML input ***/
67 std::string filename = "input.xml";
68 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
69 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
70
71 bool output = parlist->sublist("SimOpt").sublist("Solve").get("Output Iteration History",false);
72 output = (iprint > 0) && (myRank==0) && output;
73 parlist->sublist("SimOpt").sublist("Solve").set("Output Iteration History",output);
74
75 /*** Initialize main data structure. ***/
76 ROL::Ptr<MeshManager<RealT>>
77 meshMgr = ROL::makePtr<MeshReader<RealT>>(*parlist);
78 // Initialize PDE describing Navier-Stokes equations.
79 ROL::Ptr<PDE_NavierStokes<RealT>>
80 pde = ROL::makePtr<PDE_NavierStokes<RealT>>(*parlist);
81 ROL::Ptr<ROL::Constraint_SimOpt<RealT>>
82 con = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream);
83 // Cast the constraint and get the assembler.
84 ROL::Ptr<PDE_Constraint<RealT>>
85 pdecon = ROL::dynamicPtrCast<PDE_Constraint<RealT>>(con);
86 ROL::Ptr<Assembler<RealT>>
87 assembler = pdecon->getAssembler();
88 con->setSolveParameters(*parlist);
89
90 // Create state vector and set to zeroes
91 ROL::Ptr<Tpetra::MultiVector<>> u_ptr, p_ptr, z_ptr, r_ptr;
92 u_ptr = assembler->createStateVector(); u_ptr->randomize();
93 p_ptr = assembler->createStateVector(); p_ptr->randomize();
94 z_ptr = assembler->createControlVector(); z_ptr->randomize();
95 r_ptr = assembler->createResidualVector(); r_ptr->putScalar(0.0);
96 ROL::Ptr<ROL::Vector<RealT>> up, pp, zp, rp, xp;
97 up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler,*parlist);
98 pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler,*parlist);
99 zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler,*parlist);
100 rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler,*parlist);
101 xp = ROL::makePtr<ROL::Vector_SimOpt<RealT>>(up,zp);
102
103 // Initialize quadratic objective function.
104 ROL::Ptr<QoI<RealT>> qoi
105 = ROL::makePtr<QoI_Power_NavierStokes<RealT>>(*parlist,
106 pde->getVelocityFE(),
107 pde->getPressureFE(),
108 pde->getControlFE(),
109 pde->getStateFieldInfo(),
110 pde->getControlFieldInfo());
111 ROL::Ptr<ROL::Objective_SimOpt<RealT>>
112 obj = ROL::makePtr<PDE_Objective<RealT>>(qoi,assembler);
113 ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT>>
114 robj = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj,con,up,zp,pp,true,false);
115
116 // Initialize volume constraint.
117 RealT volFraction = parlist->sublist("Problem").get("Volume Fraction",0.5);
118 ROL::Ptr<QoI<RealT>>
119 qoi_vol = ROL::makePtr<QoI_Volume_NavierStokes<RealT>>(*parlist,
120 pde->getControlFE(),
121 pde->getControlFieldInfo());
122 ROL::Ptr<ROL::Objective<RealT>>
123 obj_vol = ROL::makePtr<IntegralOptObjective<RealT>>(qoi_vol,assembler);
124 ROL::Ptr<ROL::Constraint<RealT>>
125 icon = ROL::makePtr<ROL::ConstraintFromObjective<RealT>>(obj_vol);
126 ROL::Ptr<ROL::Vector<RealT>>
127 iup = ROL::makePtr<ROL::SingletonVector<RealT>>(0.0);
128 ROL::Ptr<ROL::Vector<RealT>>
129 imul = ROL::makePtr<ROL::SingletonVector<RealT>>(0.0);
130 ROL::Ptr<ROL::BoundConstraint<RealT>>
131 ibnd = ROL::makePtr<ROL::Bounds<RealT>>(*iup,false);
132
133 // Build bound constraint
134 ROL::Ptr<ROL::Vector<RealT>> lp = zp->clone(); lp->setScalar(0.0);
135 ROL::Ptr<ROL::Vector<RealT>> hp = zp->clone(); hp->setScalar(1.0);
136 ROL::Ptr<ROL::BoundConstraint<RealT>>
137 bnd = ROL::makePtr<ROL::Bounds<RealT>>(lp, hp);
138 // Build optimization problem
139 ROL::Ptr<ROL::Problem<RealT>> optProb
140 = ROL::makePtr<ROL::Problem<RealT>>(robj, zp);
141 optProb->addBoundConstraint(bnd);
142 optProb->addLinearConstraint("Volume",icon,imul); //, ibnd);
143 optProb->setProjectionAlgorithm(*parlist);
144 bool useProj = parlist->sublist("Problem").get("Project Linear Constraints",true);
145 optProb->finalize(!useProj,true,*outStream);
146
147 // Run derivative checks
148 bool checkDeriv = parlist->sublist("Problem").get("Check derivatives",false);
149 if ( checkDeriv ) {
150 ROL::Ptr<ROL::Vector<RealT>> dup = up->clone(); dup->randomize();
151 ROL::Ptr<ROL::Vector<RealT>> dzp = zp->clone(); dzp->randomize();
152 con->checkApplyJacobian_1(*up,*zp,*dup,*up,true,*outStream);
153 con->checkApplyJacobian_2(*up,*zp,*dzp,*up,true,*outStream);
154 con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream);
155 con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream);
156 con->checkApplyAdjointHessian_11(*up,*zp,*pp,*dup,*up,true,*outStream);
157 con->checkApplyAdjointHessian_21(*up,*zp,*pp,*dzp,*up,true,*outStream);
158 con->checkApplyAdjointHessian_12(*up,*zp,*pp,*dup,*zp,true,*outStream);
159 con->checkApplyAdjointHessian_22(*up,*zp,*pp,*dzp,*zp,true,*outStream);
160 obj->checkGradient_1(*up,*zp,*dup,true,*outStream);
161 obj->checkGradient_2(*up,*zp,*dzp,true,*outStream);
162 obj->checkHessVec_11(*up,*zp,*dup,true,*outStream);
163 obj->checkHessVec_12(*up,*zp,*dzp,true,*outStream);
164 obj->checkHessVec_21(*up,*zp,*dup,true,*outStream);
165 obj->checkHessVec_22(*up,*zp,*dzp,true,*outStream);
166 icon->checkApplyJacobian(*zp,*dzp,*imul,true,*outStream);
167 icon->checkApplyAdjointHessian(*zp,*imul,*dzp,*zp,true,*outStream);
168 robj->checkGradient(*zp,*dzp,true,*outStream);
169 robj->checkHessVec(*zp,*dzp,true,*outStream);
170 //optProb->check(*outStream);
171 }
172
173 // Solve optimization problem
174 up->zero(); pp->zero();
175 bool opt = parlist->sublist("Problem").get("Solve Optimization Problem",true);
176 if (opt) {
177 std::ifstream infile("control.txt");
178 if (infile.good()) assembler->inputTpetraVector(z_ptr,"control.txt");
179 else zp->setScalar(volFraction);
180 Teuchos::Time algoTimer("Algorithm Time", true);
181 ROL::Solver<RealT> optSolver(optProb,*parlist);
182 optSolver.solve(*outStream);
183 algoTimer.stop();
184 *outStream << "Total optimization time = " << algoTimer.totalElapsedTime() << " seconds.\n";
185 pdecon->outputTpetraVector(z_ptr,"control.txt");
186 }
187 else {
188 zp->setScalar(1.0);
189 }
190
191 // Output
192 assembler->printMeshData(*outStream);
193 RealT tol(1.e-8);
194 Teuchos::Array<RealT> res(1,0);
195 con->solve(*rp,*up,*zp,tol);
196 pdecon->outputTpetraVector(u_ptr,"state.txt");
197 con->value(*rp,*up,*zp,tol);
198 r_ptr->norm2(res.view(0,1));
199 *outStream << "Residual Norm: " << res[0] << std::endl;
200 errorFlag += (res[0] > 1.e-6 ? 1 : 0);
201 //pdecon->outputTpetraData();
202
203 // Get a summary from the time monitor.
204 Teuchos::TimeMonitor::summarize();
205 }
206 catch (std::logic_error& err) {
207 *outStream << err.what() << "\n";
208 errorFlag = -1000;
209 }; // end try
210
211 if (errorFlag != 0)
212 std::cout << "End Result: TEST FAILED\n";
213 else
214 std::cout << "End Result: TEST PASSED\n";
215
216 return 0;
217 }