Debian Codegraph @

packages/rol/example/PDE-OPT/stefan-boltzmann/example_04.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_04.cpp
11 \brief Shows how to solve the Stefan-Boltzmann problem.
12 */
13
14 #include "Teuchos_Comm.hpp"
15 #include "ROL_Stream.hpp"
16 #include "Teuchos_GlobalMPISession.hpp"
17 #include "Teuchos_XMLParameterListHelpers.hpp"
18
19 #include "Tpetra_Core.hpp"
20 #include "Tpetra_Version.hpp"
21
22 #include <iostream>
23 #include <algorithm>
24 //#include <fenv.h>
25
26 #include "ROL_OptimizationSolver.hpp"
27 #include "ROL_Bounds.hpp"
28 #include "ROL_BoundConstraint_SimOpt.hpp"
29 #include "ROL_Reduced_Objective_SimOpt.hpp"
30
31 #include "../TOOLS/meshmanager.hpp"
32 #include "../TOOLS/pdeconstraint.hpp"
33 #include "../TOOLS/pdeobjective.hpp"
34 #include "../TOOLS/pdevector.hpp"
35 #include "pde_stoch_stefan_boltzmann.hpp"
36 #include "obj_stoch_stefan_boltzmann.hpp"
37 #include "mesh_stoch_stefan_boltzmann.hpp"
38
39 typedef double RealT;
40
41 int main(int argc, char *argv[]) {
42 //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
43
44 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
45 int iprint = argc - 1;
46 ROL::Ptr<std::ostream> outStream;
47 ROL::nullstream bhs; // outputs nothing
48
49 /*** Initialize communicator. ***/
50 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
51 ROL::Ptr<const Teuchos::Comm<int> > comm
52 = Tpetra::getDefaultComm();
53 const int myRank = comm->getRank();
54 if ((iprint > 0) && (myRank == 0)) {
55 outStream = ROL::makePtrFromRef(std::cout);
56 }
57 else {
58 outStream = ROL::makePtrFromRef(bhs);
59 }
60 int errorFlag = 0;
61
62 // *** Example body.
63 try {
64
65 /*** Read in XML input ***/
66 std::string filename = "input_ex04.xml";
67 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
68 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
69
70 // Problem dimensions
71 const int controlDim = 1;
72 RealT tol(1e-8);
73
74 /*************************************************************************/
75 /***************** BUILD GOVERNING PDE ***********************************/
76 /*************************************************************************/
77 /*** Initialize main data structure. ***/
78 ROL::Ptr<MeshManager<RealT> > meshMgr
79 = ROL::makePtr<MeshManager_BackwardFacingStepChannel<RealT>>(*parlist);
80 // Initialize PDE describing advection-diffusion equation
81 ROL::Ptr<StochasticStefanBoltzmannPDE<RealT> > pde
82 = ROL::makePtr<StochasticStefanBoltzmannPDE<RealT>>(*parlist);
83 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con
84 = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,comm,*parlist,*outStream);
85 // Cast the constraint and get the assembler.
86 ROL::Ptr<PDE_Constraint<RealT> > pdecon
87 = ROL::dynamicPtrCast<PDE_Constraint<RealT> >(con);
88 ROL::Ptr<Assembler<RealT> > assembler = pdecon->getAssembler();
89 con->setSolveParameters(*parlist);
90
91 /*************************************************************************/
92 /***************** BUILD VECTORS *****************************************/
93 /*************************************************************************/
94 ROL::Ptr<Tpetra::MultiVector<> > u_ptr, p_ptr, r_ptr, zbc_ptr;
95 u_ptr = assembler->createStateVector(); u_ptr->putScalar(0.0);
96 p_ptr = assembler->createStateVector(); p_ptr->putScalar(0.0);
97 r_ptr = assembler->createResidualVector(); r_ptr->putScalar(0.0);
98 zbc_ptr = assembler->createControlVector(); zbc_ptr->putScalar(280.0);
99 ROL::Ptr<std::vector<RealT> > zp_ptr;
100 zp_ptr = ROL::makePtr<std::vector<RealT>>(controlDim,0.0);
101 ROL::Ptr<ROL::Vector<RealT> > up, pp, rp, zp, xp;
102 up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler);
103 pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler);
104 rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler);
105 ROL::Ptr<ROL::TpetraMultiVector<RealT>> zbc;
106 zbc = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zbc_ptr,pde,assembler);
107 ROL::Ptr<ROL::StdVector<RealT>> zparam;
108 zparam = ROL::makePtr<ROL::StdVector<RealT>>(zp_ptr);
109 zp = ROL::makePtr<PDE_OptVector<RealT>>(zbc, zparam);
110 xp = ROL::makePtr<ROL::Vector_SimOpt<RealT>>(up,zp);
111
112 /*************************************************************************/
113 /***************** BUILD COST FUNCTIONAL *********************************/
114 /*************************************************************************/
115 std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(3,ROL::nullPtr);
116 qoi_vec[0] = ROL::makePtr<QoI_StateCost<RealT>>(pde->getVolFE(),*parlist);
117 qoi_vec[1] = ROL::makePtr<QoI_ControlCost<RealT>>(
118 pde->getVolFE(),pde->getBdryFE(0),pde->getBdryCellLocIds(0),*parlist);
119 qoi_vec[2] = ROL::makePtr<QoI_AdvectionCost<RealT>>();
120 ROL::Ptr<StochasticStefanBoltzmannStdObjective3<RealT> > std_obj
121 = ROL::makePtr<StochasticStefanBoltzmannStdObjective3<RealT>>(*parlist);
122 ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj
123 = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,assembler);
124 ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT> > objReduced
125 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, up, zp, pp, true, false);
126
127 /*************************************************************************/
128 /***************** BUILD BOUND CONSTRAINT ********************************/
129 /*************************************************************************/
130 // Bounds for boundary control
131 RealT lower_bc = parlist->sublist("Problem").get("Lower Control Bound", 280.0);
132 RealT upper_bc = parlist->sublist("Problem").get("Upper Control Bound", 370.0);
133 ROL::Ptr<Tpetra::MultiVector<> > zlo_bc_ptr
134 = assembler->createControlVector();
135 ROL::Ptr<Tpetra::MultiVector<> > zhi_bc_ptr
136 = assembler->createControlVector();
137 zlo_bc_ptr->putScalar(static_cast<RealT>(lower_bc));
138 zhi_bc_ptr->putScalar(static_cast<RealT>(upper_bc));
139 ROL::Ptr<ROL::TpetraMultiVector<RealT> > zlo_bc
140 = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zlo_bc_ptr,pde,assembler);
141 ROL::Ptr<ROL::TpetraMultiVector<RealT> > zhi_bc
142 = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zhi_bc_ptr,pde,assembler);
143 // Bounds for advection control
144 RealT lower = parlist->sublist("Problem").get("Lower Advection Bound",-100.0);
145 RealT upper = parlist->sublist("Problem").get("Upper Advection Bound", 100.0);
146 ROL::Ptr<std::vector<RealT> > zlo_param_ptr
147 = ROL::makePtr<std::vector<RealT>>(controlDim,lower);
148 ROL::Ptr<std::vector<RealT> > zhi_param_ptr
149 = ROL::makePtr<std::vector<RealT>>(controlDim,upper);
150 ROL::Ptr<ROL::StdVector<RealT> > zlo_adv
151 = ROL::makePtr<ROL::StdVector<RealT>>(zlo_param_ptr);
152 ROL::Ptr<ROL::StdVector<RealT> > zhi_adv
153 = ROL::makePtr<ROL::StdVector<RealT>>(zhi_param_ptr);
154 // Combined bounds
155 ROL::Ptr<ROL::Vector<RealT> > zlop
156 = ROL::makePtr<PDE_OptVector<RealT>>(zlo_bc, zlo_adv);
157 ROL::Ptr<ROL::Vector<RealT> > zhip
158 = ROL::makePtr<PDE_OptVector<RealT>>(zhi_bc, zhi_adv);
159 ROL::Ptr<ROL::BoundConstraint<RealT> > zbnd
160 = ROL::makePtr<ROL::Bounds<RealT>>(zlop,zhip);
161 ROL::Ptr<Tpetra::MultiVector<> > ulo_ptr = assembler->createStateVector();
162 ROL::Ptr<Tpetra::MultiVector<> > uhi_ptr = assembler->createStateVector();
163 ulo_ptr->putScalar(ROL::ROL_NINF<RealT>()); uhi_ptr->putScalar(ROL::ROL_INF<RealT>());
164 ROL::Ptr<ROL::Vector<RealT> > ulop
165 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(ulo_ptr,pde,assembler,*parlist);
166 ROL::Ptr<ROL::Vector<RealT> > uhip
167 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(uhi_ptr,pde,assembler,*parlist);
168 ROL::Ptr<ROL::BoundConstraint<RealT> > ubnd
169 = ROL::makePtr<ROL::Bounds<RealT>>(ulop,uhip);
170 ROL::Ptr<ROL::BoundConstraint<RealT> > bnd
171 = ROL::makePtr<ROL::BoundConstraint_SimOpt<RealT> >(ubnd,zbnd);
172
173 /*************************************************************************/
174 /***************** BUILD OPTIMIZATION PROBLEM ****************************/
175 /*************************************************************************/
176 ROL::Ptr<ROL::OptimizationProblem<RealT>> problem;
177 bool useReducedSpace = parlist->sublist("Problem").get("Use Reduced Space",true);
178 if (useReducedSpace) {
179 problem = ROL::makePtr<ROL::OptimizationProblem<RealT>>(objReduced,zp,zbnd);
180 }
181 else {
182 problem = ROL::makePtr<ROL::OptimizationProblem<RealT>>(obj,xp,bnd,con,pp);
183 }
184 bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false);
185 if ( checkDeriv ) {
186 problem->check(*outStream);
187 }
188
189 /*************************************************************************/
190 /***************** SOLVE OPTIMIZATION PROBLEM ****************************/
191 /*************************************************************************/
192 ROL::OptimizationSolver<RealT> solver(*problem,*parlist);
193 (*zp_ptr)[0] = parlist->sublist("Problem").get("Advection Magnitude",0.0);
194 u_ptr->putScalar(450.0);
195
196 bool solveFS = parlist->sublist("Problem").get("Initial Solve for Full Space",true);
197 if (solveFS) {
198 con->solve(*rp,*up,*zp,tol);
199 }
200
201 std::clock_t timer = std::clock();
202 solver.solve(*outStream);
203 *outStream << "Optimization time: "
204 << static_cast<RealT>(std::clock()-timer)/static_cast<RealT>(CLOCKS_PER_SEC)
205 << " seconds." << std::endl << std::endl;
206
207 /*************************************************************************/
208 /***************** OUTPUT RESULTS ****************************************/
209 /*************************************************************************/
210 std::clock_t timer_print = std::clock();
211 assembler->printMeshData(*outStream);
212 // Output control to file
213 pdecon->outputTpetraVector(zbc_ptr,"control.txt");
214 pdecon->outputTpetraVector(u_ptr,"state.txt");
215 *outStream << std::endl << "Advection value: " << (*zp_ptr)[0] << std::endl;
216 *outStream << "Output time: "
217 << static_cast<RealT>(std::clock()-timer_print)/static_cast<RealT>(CLOCKS_PER_SEC)
218 << " seconds." << std::endl << std::endl;
219
220 Teuchos::Array<RealT> res(1,0);
221 con->solve(*rp,*up,*zp,tol);
222 r_ptr->norm2(res.view(0,1));
223
224 /*************************************************************************/
225 /***************** CHECK RESIDUAL NORM ***********************************/
226 /*************************************************************************/
227 *outStream << "Residual Norm: " << res[0] << std::endl << std::endl;
228 errorFlag += (res[0] > 1.e-6 ? 1 : 0);
229 }
230 catch (std::logic_error& err) {
231 *outStream << err.what() << "\n";
232 errorFlag = -1000;
233 }; // end try
234
235 if (errorFlag != 0)
236 std::cout << "End Result: TEST FAILED\n";
237 else
238 std::cout << "End Result: TEST PASSED\n";
239
240 return 0;
241 }