Debian Codegraph @

packages/rol/example/PDE-OPT/thermal-fluids/example_07.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 "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_TpetraMultiVector.hpp"
27 #include "ROL_Reduced_Objective_SimOpt.hpp"
28 #include "ROL_OptimizationSolver.hpp"
29 #include "ROL_MonteCarloGenerator.hpp"
30 #include "ROL_TpetraTeuchosBatchManager.hpp"
31
32 #include "../TOOLS/pdeconstraint.hpp"
33 #include "../TOOLS/pdeobjective.hpp"
34 #include "../TOOLS/pdevector.hpp"
35 #include "../TOOLS/meshreader.hpp"
36 #include "pde_thermal-fluids_ex07.hpp"
37 #include "obj_thermal-fluids_ex03.hpp"
38
39 typedef double RealT;
40
41 template<class Real>
42 void print(ROL::Objective<Real> &obj,
43 const ROL::Vector<Real> &z,
44 ROL::SampleGenerator<Real> &sampler,
45 const int ngsamp,
46 const ROL::Ptr<const Teuchos::Comm<int> > &comm,
47 const std::string &filename) {
48 Real tol(1e-8);
49 // Build objective function distribution
50 int nsamp = sampler.numMySamples();
51 std::vector<Real> myvalues(nsamp), myzerovec(nsamp, 0);
52 std::vector<double> gvalues(ngsamp), gzerovec(ngsamp, 0);
53 std::vector<Real> sample = sampler.getMyPoint(0);
54 int sdim = sample.size();
55 std::vector<std::vector<Real> > mysamples(sdim, myzerovec);
56 std::vector<std::vector<double> > gsamples(sdim, gzerovec);
57 for (int i = 0; i < nsamp; ++i) {
58 sample = sampler.getMyPoint(i);
59 obj.setParameter(sample);
60 myvalues[i] = static_cast<double>(obj.value(z,tol));
61 for (int j = 0; j < sdim; ++j) {
62 mysamples[j][i] = static_cast<double>(sample[j]);
63 }
64 }
65
66 // Send data to root processor
67 #ifdef HAVE_MPI
68 ROL::Ptr<const Teuchos::MpiComm<int> > mpicomm
69 = ROL::dynamicPtrCast<const Teuchos::MpiComm<int> >(comm);
70 int nproc = Teuchos::size<int>(*mpicomm);
71 std::vector<int> sampleCounts(nproc, 0), sampleDispls(nproc, 0);
72 MPI_Gather(&nsamp,1,MPI_INT,&sampleCounts[0],1,MPI_INT,0,*(mpicomm->getRawMpiComm())());
73 for (int i = 1; i < nproc; ++i) {
74 sampleDispls[i] = sampleDispls[i-1] + sampleCounts[i-1];
75 }
76 MPI_Gatherv(&myvalues[0],nsamp,MPI_DOUBLE,&gvalues[0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())());
77 for (int j = 0; j < sdim; ++j) {
78 MPI_Gatherv(&mysamples[j][0],nsamp,MPI_DOUBLE,&gsamples[j][0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())());
79 }
80 #else
81 gvalues.assign(myvalues.begin(),myvalues.end());
82 for (int j = 0; j < sdim; ++j) {
83 gsamples[j].assign(mysamples[j].begin(),mysamples[j].end());
84 }
85 #endif
86
87 // Print
88 int rank = Teuchos::rank<int>(*comm);
89 if ( rank==0 ) {
90 std::ofstream file;
91 file.open(filename);
92 file << std::scientific << std::setprecision(15);
93 for (int i = 0; i < ngsamp; ++i) {
94 for (int j = 0; j < sdim; ++j) {
95 file << std::setw(25) << std::left << gsamples[j][i];
96 }
97 file << std::setw(25) << std::left << gvalues[i] << std::endl;
98 }
99 file.close();
100 }
101 }
102
103 int main(int argc, char *argv[]) {
104 //feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
105
106 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
107 int iprint = argc - 1;
108 ROL::Ptr<std::ostream> outStream;
109 ROL::nullstream bhs; // outputs nothing
110
111 /*** Initialize communicator. ***/
112 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
113 ROL::Ptr<const Teuchos::Comm<int> > comm
114 = Tpetra::getDefaultComm();
115 ROL::Ptr<const Teuchos::Comm<int> > serial_comm
116 = ROL::makePtr<Teuchos::SerialComm<int>>();
117 const int myRank = comm->getRank();
118 if ((iprint > 0) && (myRank == 0)) {
119 outStream = ROL::makePtrFromRef(std::cout);
120 }
121 else {
122 outStream = ROL::makePtrFromRef(bhs);
123 }
124 int errorFlag = 0;
125
126 // *** Example body.
127 try {
128
129 /*** Read in XML input ***/
130 std::string filename = "input_ex07.xml";
131 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
132 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
133
134 /*** Initialize main data structure. ***/
135 ROL::Ptr<MeshManager<RealT> > meshMgr
136 = ROL::makePtr<MeshReader<RealT>>(*parlist);
137 // Initialize PDE describing Navier-Stokes equations.
138 ROL::Ptr<PDE_ThermalFluids_ex07<RealT> > pde
139 = ROL::makePtr<PDE_ThermalFluids_ex07<RealT>>(*parlist);
140 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con
141 = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,serial_comm,*parlist,*outStream);
142 // Cast the constraint and get the assembler.
143 ROL::Ptr<PDE_Constraint<RealT> > pdecon
144 = ROL::dynamicPtrCast<PDE_Constraint<RealT> >(con);
145 ROL::Ptr<Assembler<RealT> > assembler = pdecon->getAssembler();
146 con->setSolveParameters(*parlist);
147 pdecon->outputTpetraData();
148
149 // Create state vector and set to zeroes
150 ROL::Ptr<Tpetra::MultiVector<> > u_ptr, p_ptr, r_ptr, z_ptr;
151 u_ptr = assembler->createStateVector();
152 p_ptr = assembler->createStateVector();
153 r_ptr = assembler->createResidualVector();
154 z_ptr = assembler->createControlVector();
155 ROL::Ptr<ROL::Vector<RealT> > up, pp, rp, zp;
156 up = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,assembler);
157 pp = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,assembler);
158 rp = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,assembler);
159 zp = ROL::makePtr<PDE_PrimalOptVector<RealT>>(z_ptr,pde,assembler);
160
161 // Create ROL SimOpt vectors
162 //ROL::Vector_SimOpt<RealT> x(up,zp);
163
164 // Initialize objective function.
165 std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(2,ROL::nullPtr);
166 qoi_vec[0] = ROL::makePtr<QoI_State_ThermalFluids<RealT>>(*parlist,
167 pde->getVelocityFE(),
168 pde->getPressureFE(),
169 pde->getThermalFE(),
170 pde->getFieldHelper());
171 qoi_vec[1] = ROL::makePtr<QoI_L2Penalty_ThermalFluids<RealT>>(pde->getVelocityFE(),
172 pde->getPressureFE(),
173 pde->getThermalFE(),
174 pde->getThermalBdryFE(),
175 pde->getBdryCellLocIds(),
176 pde->getFieldHelper());
177 ROL::Ptr<StdObjective_ThermalFluids<RealT> > std_obj
178 = ROL::makePtr<StdObjective_ThermalFluids<RealT>>(*parlist);
179 ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj
180 = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,std_obj,assembler);
181 ROL::Ptr<ROL::VectorController<RealT> > stateStore
182 = ROL::makePtr<ROL::VectorController<RealT>>();
183 ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT> > robj
184 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, stateStore, up, zp, pp, true, false);
185
186 /*************************************************************************/
187 /***************** BUILD SAMPLER *****************************************/
188 /*************************************************************************/
189 int Nbottom = parlist->sublist("Problem").get("Bottom KL Truncation Order",5);
190 int N0 = parlist->sublist("Problem").get("Side 0 KL Truncation Order",5);
191 int N1 = parlist->sublist("Problem").get("Side 1 KL Truncation Order",5);
192 int N2 = parlist->sublist("Problem").get("Side 2 KL Truncation Order",5);
193 int N3 = parlist->sublist("Problem").get("Side 3 KL Truncation Order",5);
194 int stochDim = 2*(Nbottom + N0 + N1 + N2 + N3) + 3;
195 int nsamp = parlist->sublist("Problem").get("Number of samples",100);
196 int nsamp_dist = parlist->sublist("Problem").get("Number of output samples",100);
197 // Build vector of distributions
198 std::vector<ROL::Ptr<ROL::Distribution<RealT> > > distVec(stochDim);
199 Teuchos::ParameterList UList;
200 UList.sublist("Distribution").set("Name","Uniform");
201 UList.sublist("Distribution").sublist("Uniform").set("Lower Bound",-1.0);
202 UList.sublist("Distribution").sublist("Uniform").set("Upper Bound", 1.0);
203 for (int i = 0; i < stochDim; ++i) {
204 distVec[i] = ROL::DistributionFactory<RealT>(UList);
205 }
206 // Sampler
207 ROL::Ptr<ROL::BatchManager<RealT> > bman
208 = ROL::makePtr<ROL::TpetraTeuchosBatchManager<RealT>>(comm);
209 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
210 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp,distVec,bman);
211 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler_dist
212 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp_dist,distVec,bman);
213
214 // Set up optimization problem, check derivatives and solve
215 zp->zero();
216 ROL::OptimizationProblem<RealT> optProb(robj,zp);
217 bool isStoch = parlist->sublist("Problem").get("Is stochastic?",false);
218 if (isStoch) {
219 optProb.setStochasticObjective(*parlist,sampler);
220 }
221 if ( parlist->sublist("Problem").get("Check derivatives",false) ) {
222 optProb.check(*outStream);
223 ROL::Ptr<ROL::Vector<RealT> > xp
224 = ROL::makePtr<ROL::Vector_SimOpt<RealT>>(up,zp);
225 ROL::OptimizationProblem<RealT> op(obj,xp,con,pp);
226 op.check(*outStream);
227 }
228 ROL::OptimizationSolver<RealT> optSolver(optProb,*parlist);
229 optSolver.solve(*outStream);
230
231 // Output.
232 RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
233 if (isStoch) {
234 std::vector<RealT> pt;
235 RealT wt;
236 up->zero(); pp->zero();
237 for (int i = 0; i < sampler->numMySamples(); ++i) {
238 pt = sampler->getMyPoint(i);
239 wt = sampler->getMyWeight(i);
240 con->setParameter(pt);
241 con->solve(*rp,*up,*zp,tol);
242 pp->axpy(wt,*up);
243 }
244 up->zero();
245 sampler->sumAll(*pp,*up);
246 } else {
247 con->solve(*rp,*up,*zp,tol);
248 }
249 pdecon->outputTpetraVector(u_ptr,"state.txt");
250
251 pdecon->outputTpetraVector(z_ptr,"control.txt");
252 assembler->printMeshData(*outStream);
253 if (isStoch) {
254 print<RealT>(*robj,*zp,*sampler_dist,nsamp_dist,comm,"obj_samples.txt");
255 }
256
257 // Get a summary from the time monitor.
258 Teuchos::TimeMonitor::summarize();
259 }
260 catch (std::logic_error& err) {
261 *outStream << err.what() << "\n";
262 errorFlag = -1000;
263 }; // end try
264
265 if (errorFlag != 0)
266 std::cout << "End Result: TEST FAILED\n";
267 else
268 std::cout << "End Result: TEST PASSED\n";
269
270 return 0;
271 }