1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43
44 /*! \file example_02.cpp
45 \brief Shows how to solve the stochastic advection-diffusion problem.
46 */
47
48 #include "Teuchos_Comm.hpp"
49 #include "ROL_Stream.hpp"
50 #include "Teuchos_GlobalMPISession.hpp"
51 #include "Teuchos_XMLParameterListHelpers.hpp"
52
53 #include "Tpetra_Core.hpp"
54 #include "Tpetra_Version.hpp"
55
56 #include <iostream>
57 #include <algorithm>
58 //#include <fenv.h>
59
60 #include "ROL_Bounds.hpp"
61 #include "ROL_Reduced_Objective_SimOpt.hpp"
62 #include "ROL_MonteCarloGenerator.hpp"
63 #include "ROL_OptimizationSolver.hpp"
64 #include "ROL_TpetraTeuchosBatchManager.hpp"
65
66 #include "../TOOLS/meshmanager.hpp"
67 #include "../TOOLS/pdeconstraint.hpp"
68 #include "../TOOLS/pdeobjective.hpp"
69 #include "../TOOLS/pdevector.hpp"
70 #include "../TOOLS/batchmanager.hpp"
71 #include "pde_stoch_adv_diff.hpp"
72 #include "obj_stoch_adv_diff.hpp"
73 #include "mesh_stoch_adv_diff.hpp"
74
75 typedef double RealT;
76
77 template<class Real>
random(const Teuchos::Comm<int> & comm,const Real a=-1,const Real b=1)78 Real random(const Teuchos::Comm<int> &comm,
79 const Real a = -1, const Real b = 1) {
80 Real val(0), u(0);
81 if ( Teuchos::rank<int>(comm)==0 ) {
82 u = static_cast<Real>(rand())/static_cast<Real>(RAND_MAX);
83 val = (b-a)*u + a;
84 }
85 Teuchos::broadcast<int,Real>(comm,0,1,&val);
86 return val;
87 }
88
89 template<class Real>
print(ROL::Objective<Real> & obj,const ROL::Vector<Real> & z,ROL::SampleGenerator<Real> & sampler,const ROL::Ptr<const Teuchos::Comm<int>> & comm,const std::string & filename)90 void print(ROL::Objective<Real> &obj,
91 const ROL::Vector<Real> &z,
92 ROL::SampleGenerator<Real> &sampler,
93 const ROL::Ptr<const Teuchos::Comm<int> > &comm,
94 const std::string &filename) {
95 Real tol(1e-8);
96 int ngsamp = sampler.numGlobalSamples();
97 // Build objective function distribution
98 int nsamp = sampler.numMySamples();
99 std::vector<Real> myvalues(nsamp), myzerovec(nsamp, 0);
100 std::vector<double> gvalues(ngsamp), gzerovec(ngsamp, 0);
101 std::vector<Real> sample = sampler.getMyPoint(0);
102 int sdim = sample.size();
103 std::vector<std::vector<Real> > mysamples(sdim, myzerovec);
104 std::vector<std::vector<double> > gsamples(sdim, gzerovec);
105 for (int i = 0; i < nsamp; ++i) {
106 sample = sampler.getMyPoint(i);
107 obj.setParameter(sample);
108 myvalues[i] = static_cast<double>(obj.value(z,tol));
109 for (int j = 0; j < sdim; ++j) {
110 mysamples[j][i] = static_cast<double>(sample[j]);
111 }
112 }
113
114 // Send data to root processor
115 #ifdef HAVE_MPI
116 ROL::Ptr<const Teuchos::MpiComm<int> > mpicomm
117 = ROL::dynamicPtrCast<const Teuchos::MpiComm<int> >(comm);
118 int nproc = Teuchos::size<int>(*mpicomm);
119 std::vector<int> sampleCounts(nproc, 0), sampleDispls(nproc, 0);
120 MPI_Gather(&nsamp,1,MPI_INT,&sampleCounts[0],1,MPI_INT,0,*(mpicomm->getRawMpiComm())());
121 for (int i = 1; i < nproc; ++i) {
122 sampleDispls[i] = sampleDispls[i-1] + sampleCounts[i-1];
123 }
124 MPI_Gatherv(&myvalues[0],nsamp,MPI_DOUBLE,&gvalues[0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())());
125 for (int j = 0; j < sdim; ++j) {
126 MPI_Gatherv(&mysamples[j][0],nsamp,MPI_DOUBLE,&gsamples[j][0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())());
127 }
128 #else
129 gvalues.assign(myvalues.begin(),myvalues.end());
130 for (int j = 0; j < sdim; ++j) {
131 gsamples[j].assign(mysamples[j].begin(),mysamples[j].end());
132 }
133 #endif
134
135 // Print
136 int rank = Teuchos::rank<int>(*comm);
137 if ( rank==0 ) {
138 std::ofstream file;
139 file.open(filename);
140 file << std::scientific << std::setprecision(15);
141 for (int i = 0; i < ngsamp; ++i) {
142 for (int j = 0; j < sdim; ++j) {
143 file << std::setw(25) << std::left << gsamples[j][i];
144 }
145 file << std::setw(25) << std::left << gvalues[i] << std::endl;
146 }
147 file.close();
148 }
149 }
150
151 template<class Real>
printSampler(ROL::SampleGenerator<Real> & sampler,const ROL::Ptr<const Teuchos::Comm<int>> & comm,const std::string & filename)152 void printSampler(ROL::SampleGenerator<Real> &sampler,
153 const ROL::Ptr<const Teuchos::Comm<int> > &comm,
154 const std::string &filename) {
155 int ngsamp = sampler.numGlobalSamples();
156 // Build objective function distribution
157 int nsamp = sampler.numMySamples();
158 std::vector<Real> myzerovec(nsamp, 0);
159 std::vector<double> gzerovec(ngsamp, 0);
160 std::vector<Real> sample = sampler.getMyPoint(0);
161 int sdim = sample.size();
162 std::vector<std::vector<Real> > mysamples(sdim, myzerovec);
163 std::vector<std::vector<double> > gsamples(sdim, gzerovec);
164 for (int i = 0; i < nsamp; ++i) {
165 sample = sampler.getMyPoint(i);
166 for (int j = 0; j < sdim; ++j) {
167 mysamples[j][i] = static_cast<double>(sample[j]);
168 }
169 }
170
171 // Send data to root processor
172 #ifdef HAVE_MPI
173 ROL::Ptr<const Teuchos::MpiComm<int> > mpicomm
174 = ROL::dynamicPtrCast<const Teuchos::MpiComm<int> >(comm);
175 int nproc = Teuchos::size<int>(*mpicomm);
176 std::vector<int> sampleCounts(nproc, 0), sampleDispls(nproc, 0);
177 MPI_Gather(&nsamp,1,MPI_INT,&sampleCounts[0],1,MPI_INT,0,*(mpicomm->getRawMpiComm())());
178 for (int i = 1; i < nproc; ++i) {
179 sampleDispls[i] = sampleDispls[i-1] + sampleCounts[i-1];
180 }
181 for (int j = 0; j < sdim; ++j) {
182 MPI_Gatherv(&mysamples[j][0],nsamp,MPI_DOUBLE,&gsamples[j][0],&sampleCounts[0],&sampleDispls[0],MPI_DOUBLE,0,*(mpicomm->getRawMpiComm())());
183 }
184 #else
185 for (int j = 0; j < sdim; ++j) {
186 gsamples[j].assign(mysamples[j].begin(),mysamples[j].end());
187 }
188 #endif
189
190 // Print
191 int rank = Teuchos::rank<int>(*comm);
192 if ( rank==0 ) {
193 std::ofstream file;
194 file.open(filename);
195 file << std::scientific << std::setprecision(15);
196 for (int i = 0; i < ngsamp; ++i) {
197 for (int j = 0; j < sdim; ++j) {
198 file << std::setw(25) << std::left << gsamples[j][i];
199 }
200 file << std::endl;
201 }
202 file.close();
203 }
204 }
205
main(int argc,char * argv[])206 int main(int argc, char *argv[]) {
207 // feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
208
209 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
210 int iprint = argc - 1;
211 ROL::Ptr<std::ostream> outStream;
212 ROL::nullstream bhs; // outputs nothing
213
214 /*** Initialize communicator. ***/
215 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
216 ROL::Ptr<const Teuchos::Comm<int> > comm
217 = Tpetra::getDefaultComm();
218 ROL::Ptr<const Teuchos::Comm<int> > serial_comm
219 = ROL::makePtr<Teuchos::SerialComm<int>>();
220 const int myRank = comm->getRank();
221 if ((iprint > 0) && (myRank == 0)) {
222 outStream = ROL::makePtrFromRef(std::cout);
223 }
224 else {
225 outStream = ROL::makePtrFromRef(bhs);
226 }
227 int errorFlag = 0;
228
229 // *** Example body.
230 try {
231
232 /*** Read in XML input ***/
233 std::string filename = "input.xml";
234 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
235 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
236
237 // Problem dimensions
238 const int controlDim = 9, stochDim = 37;
239 const RealT one(1);
240
241 /*************************************************************************/
242 /***************** BUILD GOVERNING PDE ***********************************/
243 /*************************************************************************/
244 /*** Initialize main data structure. ***/
245 ROL::Ptr<MeshManager<RealT> > meshMgr
246 = ROL::makePtr<MeshManager_stoch_adv_diff<RealT>>(*parlist);
247 // Initialize PDE describing advection-diffusion equation
248 ROL::Ptr<PDE_stoch_adv_diff<RealT> > pde
249 = ROL::makePtr<PDE_stoch_adv_diff<RealT>>(*parlist);
250 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > con
251 = ROL::makePtr<PDE_Constraint<RealT>>(pde,meshMgr,serial_comm,*parlist,*outStream);
252 ROL::Ptr<PDE_Constraint<RealT> > pdeCon
253 = ROL::dynamicPtrCast<PDE_Constraint<RealT> >(con);
254 pdeCon->getAssembler()->printMeshData(*outStream);
255 con->setSolveParameters(*parlist);
256
257 /*************************************************************************/
258 /***************** BUILD VECTORS *****************************************/
259 /*************************************************************************/
260 ROL::Ptr<Tpetra::MultiVector<> > u_ptr = pdeCon->getAssembler()->createStateVector();
261 ROL::Ptr<Tpetra::MultiVector<> > p_ptr = pdeCon->getAssembler()->createStateVector();
262 ROL::Ptr<Tpetra::MultiVector<> > du_ptr = pdeCon->getAssembler()->createStateVector();
263 u_ptr->randomize(); //u_ptr->putScalar(static_cast<RealT>(1));
264 p_ptr->randomize(); //p_ptr->putScalar(static_cast<RealT>(1));
265 du_ptr->randomize(); //du_ptr->putScalar(static_cast<RealT>(0));
266 ROL::Ptr<ROL::Vector<RealT> > up
267 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u_ptr,pde,pdeCon->getAssembler());
268 ROL::Ptr<ROL::Vector<RealT> > pp
269 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(p_ptr,pde,pdeCon->getAssembler());
270 ROL::Ptr<ROL::Vector<RealT> > dup
271 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(du_ptr,pde,pdeCon->getAssembler());
272 // Create residual vectors
273 ROL::Ptr<Tpetra::MultiVector<> > r_ptr = pdeCon->getAssembler()->createResidualVector();
274 r_ptr->randomize(); //r_ptr->putScalar(static_cast<RealT>(1));
275 ROL::Ptr<ROL::Vector<RealT> > rp
276 = ROL::makePtr<PDE_DualSimVector<RealT>>(r_ptr,pde,pdeCon->getAssembler());
277 // Create control vector and set to ones
278 ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(controlDim);
279 ROL::Ptr<std::vector<RealT> > dz_ptr = ROL::makePtr<std::vector<RealT>>(controlDim);
280 ROL::Ptr<std::vector<RealT> > yz_ptr = ROL::makePtr<std::vector<RealT>>(controlDim);
281 // Create control direction vector and set to random
282 for (int i = 0; i < controlDim; ++i) {
283 (*z_ptr)[i] = random<RealT>(*comm);
284 (*dz_ptr)[i] = random<RealT>(*comm);
285 (*yz_ptr)[i] = random<RealT>(*comm);
286 }
287 ROL::Ptr<ROL::Vector<RealT> > zp
288 = ROL::makePtr<PDE_OptVector<RealT>>(ROL::makePtr<ROL::StdVector<RealT>>(z_ptr));
289 ROL::Ptr<ROL::Vector<RealT> > dzp
290 = ROL::makePtr<PDE_OptVector<RealT>>(ROL::makePtr<ROL::StdVector<RealT>>(dz_ptr));
291 ROL::Ptr<ROL::Vector<RealT> > yzp
292 = ROL::makePtr<PDE_OptVector<RealT>>(ROL::makePtr<ROL::StdVector<RealT>>(yz_ptr));
293 // Create ROL SimOpt vectors
294 ROL::Vector_SimOpt<RealT> x(up,zp);
295 ROL::Vector_SimOpt<RealT> d(dup,dzp);
296
297 ROL::Ptr<Tpetra::MultiVector<> > dualu_ptr = pdeCon->getAssembler()->createStateVector();
298 ROL::Ptr<ROL::Vector<RealT> > dualup
299 = ROL::makePtr<PDE_DualSimVector<RealT>>(dualu_ptr,pde,pdeCon->getAssembler());
300
301 /*************************************************************************/
302 /***************** BUILD COST FUNCTIONAL *********************************/
303 /*************************************************************************/
304 std::vector<ROL::Ptr<QoI<RealT> > > qoi_vec(2,ROL::nullPtr);
305 qoi_vec[0] = ROL::makePtr<QoI_State_Cost_stoch_adv_diff<RealT>>(pde->getFE());
306 qoi_vec[1] = ROL::makePtr<QoI_Control_Cost_stoch_adv_diff<RealT>>();
307 RealT stateCost = parlist->sublist("Problem").get("State Cost",1.e5);
308 RealT controlCost = parlist->sublist("Problem").get("Control Cost",1.e0);
309 std::vector<RealT> wts = {stateCost, controlCost};
310 ROL::Ptr<ROL::Objective_SimOpt<RealT> > obj
311 = ROL::makePtr<PDE_Objective<RealT>>(qoi_vec,wts,pdeCon->getAssembler());
312 bool storage = parlist->sublist("Problem").get("Use State and Adjoint Storage",true);
313 ROL::Ptr<ROL::Reduced_Objective_SimOpt<RealT> > objReduced
314 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(obj, con, up, zp, pp, storage, false);
315
316 /*************************************************************************/
317 /***************** BUILD BOUND CONSTRAINT ********************************/
318 /*************************************************************************/
319 ROL::Ptr<std::vector<RealT> > zlo_ptr = ROL::makePtr<std::vector<RealT>>(controlDim,0);
320 ROL::Ptr<std::vector<RealT> > zhi_ptr = ROL::makePtr<std::vector<RealT>>(controlDim,1);
321 ROL::Ptr<ROL::Vector<RealT> > zlop
322 = ROL::makePtr<PDE_OptVector<RealT>>(ROL::makePtr<ROL::StdVector<RealT>>(zlo_ptr));
323 ROL::Ptr<ROL::Vector<RealT> > zhip
324 = ROL::makePtr<PDE_OptVector<RealT>>(ROL::makePtr<ROL::StdVector<RealT>>(zhi_ptr));
325 ROL::Ptr<ROL::BoundConstraint<RealT> > bnd
326 = ROL::makePtr<ROL::Bounds<RealT>>(zlop,zhip);
327
328 /*************************************************************************/
329 /***************** BUILD SAMPLER *****************************************/
330 /*************************************************************************/
331 int nsamp = parlist->sublist("Problem").get("Number of Samples",100);
332 std::vector<RealT> tmp = {-one,one};
333 std::vector<std::vector<RealT> > bounds(stochDim,tmp);
334 ROL::Ptr<ROL::BatchManager<RealT> > bman
335 = ROL::makePtr<PDE_OptVector_BatchManager<RealT>>(comm);
336 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
337 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp,bounds,bman);
338
339 /*************************************************************************/
340 /***************** BUILD STOCHASTIC PROBLEM ******************************/
341 /*************************************************************************/
342 ROL::OptimizationProblem<RealT> opt(objReduced,zp,bnd);
343 parlist->sublist("SOL").set("Initial Statistic",one);
344 opt.setStochasticObjective(*parlist,sampler);
345
346 /*************************************************************************/
347 /***************** RUN VECTOR AND DERIVATIVE CHECKS **********************/
348 /*************************************************************************/
349 bool checkDeriv = parlist->sublist("Problem").get("Check Derivatives",false);
350 if ( checkDeriv ) {
351 up->checkVector(*pp,*dup,true,*outStream);
352 zp->checkVector(*yzp,*dzp,true,*outStream);
353 std::vector<RealT> param(stochDim,0);
354 objReduced->setParameter(param);
355 *outStream << "\n\nCheck Gradient of Full Objective Function\n";
356 obj->checkGradient(x,d,true,*outStream);
357 *outStream << "\n\nCheck Hessian of Full Objective Function\n";
358 obj->checkHessVec(x,d,true,*outStream);
359 *outStream << "\n\nCheck Full Jacobian of PDE Constraint\n";
360 con->checkApplyJacobian(x,d,*rp,true,*outStream);
361 *outStream << "\n\nCheck Jacobian_1 of PDE Constraint\n";
362 con->checkApplyJacobian_1(*up,*zp,*dup,*rp,true,*outStream);
363 *outStream << "\n\nCheck Jacobian_2 of PDE Constraint\n";
364 con->checkApplyJacobian_2(*up,*zp,*dzp,*rp,true,*outStream);
365 *outStream << "\n\nCheck Full Hessian of PDE Constraint\n";
366 con->checkApplyAdjointHessian(x,*pp,d,x,true,*outStream);
367 *outStream << "\n\nCheck Hessian_11 of PDE Constraint\n";
368 con->checkApplyAdjointHessian_11(*up,*zp,*pp,*dup,*dualup,true,*outStream);
369 *outStream << "\n\nCheck Hessian_21 of PDE Constraint\n";
370 con->checkApplyAdjointHessian_21(*up,*zp,*pp,*dzp,*dualup,true,*outStream);
371 *outStream << "\n\nCheck Hessian_12 of PDE Constraint\n";
372 con->checkApplyAdjointHessian_12(*up,*zp,*pp,*dup,*dzp,true,*outStream);
373 *outStream << "\n\nCheck Hessian_22 of PDE Constraint\n";
374 con->checkApplyAdjointHessian_22(*up,*zp,*pp,*dzp,*dzp,true,*outStream);
375 *outStream << "\n";
376 con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream);
377 *outStream << "\n";
378 con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream);
379 *outStream << "\n";
380 *outStream << "\n\nCheck Gradient of Reduced Objective Function\n";
381 objReduced->checkGradient(*zp,*dzp,true,*outStream);
382 *outStream << "\n\nCheck Hessian of Reduced Objective Function\n";
383 objReduced->checkHessVec(*zp,*dzp,true,*outStream);
384
385 opt.check(*outStream);
386 }
387
388 /*************************************************************************/
389 /***************** SOLVE OPTIMIZATION PROBLEM ****************************/
390 /*************************************************************************/
391 parlist->sublist("Step").set("Type","Trust Region");
392 ROL::OptimizationSolver<RealT> solver(opt,*parlist);
393 zp->zero();
394 std::clock_t timer = std::clock();
395 solver.solve(*outStream);
396 *outStream << "Optimization time: "
397 << static_cast<RealT>(std::clock()-timer)/static_cast<RealT>(CLOCKS_PER_SEC)
398 << " seconds." << std::endl << std::endl;
399
400 /*************************************************************************/
401 /***************** OUTPUT RESULTS ****************************************/
402 /*************************************************************************/
403 std::clock_t timer_print = std::clock();
404 // Output control to file
405 if ( myRank == 0 ) {
406 std::ofstream zfile;
407 zfile.open("control.txt");
408 for (int i = 0; i < controlDim; i++) {
409 zfile << std::scientific << std::setprecision(15);
410 zfile << (*z_ptr)[i] << std::endl;
411 }
412 zfile.close();
413 }
414 // Output statisitic to file
415 std::vector<RealT> stat = opt.getObjectiveStatistic();
416 if ( myRank == 0 && stat.size() > 0 ) {
417 std::ofstream sfile;
418 sfile.open("stat.txt");
419 for (int i = 0; i < static_cast<int>(stat.size()); ++i) {
420 sfile << std::scientific << std::setprecision(15);
421 sfile << stat[i] << std::endl;
422 }
423 }
424 // Output expected state and samples to file
425 up->zero(); pp->zero(); dup->zero();
426 RealT tol(1.e-8);
427 ROL::Ptr<ROL::BatchManager<RealT> > bman_Eu
428 = ROL::makePtr<ROL::TpetraTeuchosBatchManager<RealT>>(comm);
429 std::vector<RealT> sample(stochDim);
430 for (int i = 0; i < sampler->numMySamples(); ++i) {
431 sample = sampler->getMyPoint(i);
432 con->setParameter(sample);
433 con->solve(*rp,*dup,*zp,tol);
434 up->axpy(sampler->getMyWeight(i),*dup);
435 }
436 bman_Eu->sumAll(*up,*pp);
437 pdeCon->getAssembler()->outputTpetraVector(p_ptr,"mean_state.txt");
438 // Print samples
439 printSampler<RealT>(*sampler,comm,"samples.txt");
440 // Build objective function distribution
441 int nsamp_dist = parlist->sublist("Problem").get("Number of Output Samples",100);
442 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler_dist
443 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nsamp_dist,bounds,bman);
444 print<RealT>(*objReduced,*zp,*sampler_dist,comm,"obj_samples.txt");
445 *outStream << "Output time: "
446 << static_cast<RealT>(std::clock()-timer_print)/static_cast<RealT>(CLOCKS_PER_SEC)
447 << " seconds." << std::endl << std::endl;
448
449 Teuchos::Array<RealT> res(1,0);
450 pdeCon->solve(*rp,*up,*zp,tol);
451 r_ptr->norm2(res.view(0,1));
452
453 /*************************************************************************/
454 /***************** CHECK RESIDUAL NORM ***********************************/
455 /*************************************************************************/
456 *outStream << "Residual Norm: " << res[0] << std::endl << std::endl;
457 errorFlag += (res[0] > 1.e-6 ? 1 : 0);
458 }
459 catch (std::logic_error& err) {
460 *outStream << err.what() << "\n";
461 errorFlag = -1000;
462 }; // end try
463
464 if (errorFlag != 0)
465 std::cout << "End Result: TEST FAILED\n";
466 else
467 std::cout << "End Result: TEST PASSED\n";
468
469 return 0;
470 }
471