Debian Codegraph @

packages/rol/adapters/teuchos/test/sol/test_05.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_03.cpp
11 \brief Shows how to solve a steady Burgers' optimal control problem using
12 full-space methods.
13 */
14
15 #include "ROL_MonteCarloGenerator.hpp"
16
17 #include "ROL_Stream.hpp"
18
19 #include "Teuchos_GlobalMPISession.hpp"
20 #include "Teuchos_Comm.hpp"
21 #include "Teuchos_DefaultComm.hpp"
22 #include "Teuchos_CommHelpers.hpp"
23
24 #include <iostream>
25 #include <fstream>
26 #include <algorithm>
27
28 #include "test_05.hpp"
29
30 typedef double RealT;
31
32 typedef H1VectorPrimal<RealT> PrimalStateVector;
33 typedef H1VectorDual<RealT> DualStateVector;
34 typedef L2VectorPrimal<RealT> PrimalControlVector;
35 typedef L2VectorDual<RealT> DualControlVector;
36 typedef H1VectorDual<RealT> PrimalConstraintVector;
37 typedef H1VectorPrimal<RealT> DualConstraintVector;
38
39 int main(int argc, char *argv[]) {
40
41 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
42 ROL::Ptr<const Teuchos::Comm<int> > comm
43 = ROL::toPtr(Teuchos::DefaultComm<int>::getComm());
44
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 bool print = (iprint>0);
48 ROL::Ptr<std::ostream> outStream;
49 ROL::nullstream bhs; // outputs nothing
50 if (print)
51 outStream = ROL::makePtrFromRef(std::cout);
52 else
53 outStream = ROL::makePtrFromRef(bhs);
54
55 int errorFlag = 0;
56
57 // *** Example body.
58
59 try {
60 /*************************************************************************/
61 /************* INITIALIZE BURGERS FEM CLASS ******************************/
62 /*************************************************************************/
63 int nx = 512; // Set spatial discretization.
64 RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear).
65 RealT cH1 = 1.0; // Scale for derivative term in H1 norm.
66 RealT cL2 = 0.0; // Scale for mass term in H1 norm.
67 ROL::Ptr<BurgersFEM<RealT> > fem
68 = ROL::makePtr<BurgersFEM<RealT>>(nx,nl,cH1,cL2);
69 fem->test_inverse_mass(*outStream);
70 fem->test_inverse_H1(*outStream);
71 /*************************************************************************/
72 /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/
73 /*************************************************************************/
74 bool hess = true;
75 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pcon
76 = ROL::makePtr<Constraint_BurgersControl<RealT>>(fem,hess);
77 /*************************************************************************/
78 /************* INITIALIZE VECTOR STORAGE *********************************/
79 /*************************************************************************/
80 // INITIALIZE CONTROL VECTORS
81 ROL::Ptr<std::vector<RealT> > z_ptr
82 = ROL::makePtr<std::vector<RealT>>(nx+2, 0.0);
83 ROL::Ptr<ROL::Vector<RealT> > zp
84 = ROL::makePtr<PrimalControlVector>(z_ptr,fem);
85 // INITIALIZE STATE VECTORS
86 ROL::Ptr<std::vector<RealT> > u_ptr
87 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
88 ROL::Ptr<ROL::Vector<RealT> > up
89 = ROL::makePtr<PrimalStateVector>(u_ptr,fem);
90 // INITIALIZE CONSTRAINT VECTORS
91 ROL::Ptr<std::vector<RealT> > c_ptr
92 = ROL::makePtr<std::vector<RealT>>(nx, 1.0);
93 ROL::Ptr<ROL::Vector<RealT> > cp
94 = ROL::makePtr<PrimalConstraintVector>(c_ptr,fem);
95 /*************************************************************************/
96 /************* INITIALIZE SAMPLE GENERATOR *******************************/
97 /*************************************************************************/
98 int dim = 4, nSamp = 10000;
99 std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
100 std::vector<std::vector<RealT> > bounds(dim,tmp);
101 ROL::Ptr<ROL::BatchManager<RealT> > bman
102 = ROL::makePtr<L2VectorBatchManager<RealT,int>>(comm);
103 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
104 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(
105 nSamp,bounds,bman,false,false,100);
106 /*************************************************************************/
107 /************* CHECK DERIVATIVES AND CONSISTENCY *************************/
108 /*************************************************************************/
109 RealT tol = std::sqrt(ROL::ROL_EPSILON<RealT>());
110 for (int i = sampler->start(); i < sampler->numMySamples(); i++) {
111 // *outStream << "(" << sampler->getMyPoint(i)[0] << ", "
112 // << sampler->getMyPoint(i)[1] << ", "
113 // << sampler->getMyPoint(i)[2] << ", "
114 // << sampler->getMyPoint(i)[3] << ")\n";
115 pcon->setParameter(sampler->getMyPoint(i));
116 pcon->solve(*cp,*up,*zp,tol);
117 RealT rnorm = cp->norm();
118 pcon->value(*cp,*up,*zp,tol);
119 RealT cnorm = cp->norm();
120 errorFlag += ((cnorm > tol) ? 1 : 0) + ((rnorm > tol) ? 1 : 0);
121 *outStream << "Sample " << i << " Rank " << sampler->batchID() << "\n";
122 *outStream << std::scientific << std::setprecision(8);
123 *outStream << "Test SimOpt solve at feasible (u,z):\n";
124 *outStream << " Solver Residual = " << rnorm << "\n";
125 *outStream << " ||c(u,z)|| = " << cnorm;
126 *outStream << "\n\n";
127 }
128 }
129 catch (std::logic_error& err) {
130 *outStream << err.what() << "\n";
131 errorFlag = -1000;
132 }; // end try
133
134 comm->barrier();
135 if (errorFlag != 0)
136 std::cout << "End Result: TEST FAILED\n";
137 else
138 std::cout << "End Result: TEST PASSED\n";
139
140 return 0;
141 }