55 #include "Teuchos_oblackholestream.hpp" 56 #include "Teuchos_XMLParameterListHelpers.hpp" 57 #include "Teuchos_GlobalMPISession.hpp" 58 #include "Teuchos_Comm.hpp" 59 #include "Teuchos_DefaultComm.hpp" 60 #include "Teuchos_CommHelpers.hpp" 76 int main(
int argc,
char *argv[]) {
78 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
79 Teuchos::RCP<const Teuchos::Comm<int> > comm
80 = Teuchos::DefaultComm<int>::getComm();
83 int iprint = argc - 1;
84 bool print = (iprint>0);
85 Teuchos::RCP<std::ostream> outStream;
86 Teuchos::oblackholestream bhs;
88 outStream = Teuchos::rcp(&std::cout,
false);
90 outStream = Teuchos::rcp(&bhs,
false);
92 bool print0 = print && !comm->getRank();
93 Teuchos::RCP<std::ostream> outStream0;
95 outStream0 = Teuchos::rcp(&std::cout,
false);
97 outStream0 = Teuchos::rcp(&bhs,
false);
112 Teuchos::RCP<BurgersFEM<RealT> > fem
114 fem->test_inverse_mass(*outStream0);
115 fem->test_inverse_H1(*outStream0);
119 Teuchos::RCP<std::vector<RealT> > ud_rcp
120 = Teuchos::rcp(
new std::vector<RealT> (nx, 1.0) );
121 Teuchos::RCP<ROL::Vector<RealT> > ud
123 Teuchos::RCP<ROL::ParametrizedObjective_SimOpt<RealT> > pobj
129 Teuchos::RCP<ROL::ParametrizedEqualityConstraint_SimOpt<RealT> > pcon
135 Teuchos::RCP<std::vector<RealT> > z_rcp
136 = Teuchos::rcp(
new std::vector<RealT> (nx+2, 1.0) );
137 Teuchos::RCP<std::vector<RealT> > gz_rcp
138 = Teuchos::rcp(
new std::vector<RealT> (nx+2, 1.0) );
139 Teuchos::RCP<std::vector<RealT> > yz_rcp
140 = Teuchos::rcp(
new std::vector<RealT> (nx+2, 1.0) );
141 for (
int i=0; i<nx+2; i++) {
142 (*yz_rcp)[i] = 2.0*random<RealT>(comm)-1.0;
144 Teuchos::RCP<ROL::Vector<RealT> > zp
146 Teuchos::RCP<ROL::Vector<RealT> > gzp
148 Teuchos::RCP<ROL::Vector<RealT> > yzp
151 Teuchos::RCP<std::vector<RealT> > u_rcp
152 = Teuchos::rcp(
new std::vector<RealT> (nx, 1.0) );
153 Teuchos::RCP<std::vector<RealT> > gu_rcp
154 = Teuchos::rcp(
new std::vector<RealT> (nx, 1.0) );
155 Teuchos::RCP<ROL::Vector<RealT> > up
157 Teuchos::RCP<ROL::Vector<RealT> > gup
160 Teuchos::RCP<std::vector<RealT> > c_rcp
161 = Teuchos::rcp(
new std::vector<RealT> (nx, 1.0) );
162 Teuchos::RCP<std::vector<RealT> > l_rcp
163 = Teuchos::rcp(
new std::vector<RealT> (nx, 1.0) );
164 for (
int i=0; i<nx; i++) {
165 (*l_rcp)[i] = random<RealT>(comm);
167 Teuchos::RCP<ROL::Vector<RealT> > cp
169 Teuchos::RCP<ROL::Vector<RealT> > lp
174 int dim = 4, nSamp = 1000;
175 std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
176 std::vector<std::vector<RealT> > bounds(dim,tmp);
177 Teuchos::RCP<ROL::BatchManager<RealT> > bman
179 Teuchos::RCP<ROL::SampleGenerator<RealT> > sampler
181 nSamp,bounds,bman,
false,
false,100));
185 bool storage =
true, fdhess =
false;
186 Teuchos::RCP<ROL::ParametrizedObjective<RealT> > robj
188 pobj,pcon,up,lp,gup,cp,storage,fdhess));
192 std::vector<RealT> Zlo(nx+2,0.0), Zhi(nx+2,10.0);
193 for (
int i = 0; i < nx+2; i++) {
194 if ( i < (
int)((nx+2)/3) ) {
198 if ( i >= (
int)((nx+2)/3) && i < (
int)(2*(nx+2)/3) ) {
202 if ( i >= (
int)(2*(nx+2)/3) ) {
207 Teuchos::RCP<ROL::BoundConstraint<RealT> > Zbnd
212 Teuchos::ParameterList SOLlist;
213 SOLlist.sublist(
"SOL").set(
"Stochastic Optimization Type",
"Risk Averse");
214 SOLlist.sublist(
"SOL").set(
"Store Sampled Value and Gradient",storage);
215 SOLlist.sublist(
"SOL").sublist(
"Risk Measure").set(
"Name",
"KL Divergence");
216 SOLlist.sublist(
"SOL").sublist(
"Risk Measure").sublist(
"KL Divergence").set(
"Threshold",1.e-2);
222 bool derivcheck =
false;
224 int nranks = sampler->numBatches();
225 for (
int pid = 0; pid < nranks; pid++) {
226 if ( pid == sampler->batchID() ) {
227 for (
int i = sampler->start(); i < sampler->numMySamples(); i++) {
228 *outStream <<
"Sample " << i <<
" Rank " << sampler->batchID() <<
"\n";
229 *outStream <<
"(" << sampler->getMyPoint(i)[0] <<
", " 230 << sampler->getMyPoint(i)[1] <<
", " 231 << sampler->getMyPoint(i)[2] <<
", " 232 << sampler->getMyPoint(i)[3] <<
")\n";
233 pcon->setParameter(sampler->getMyPoint(i));
234 pcon->checkSolve(*up,*zp,*cp,print,*outStream);
235 robj->setParameter(sampler->getMyPoint(i));
237 robj->checkGradient(*zp,*gzp,*yzp,print,*outStream);
238 robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream);
239 *outStream <<
"\n\n";
251 std::string filename =
"input.xml";
252 Teuchos::RCP<Teuchos::ParameterList> parlist
253 = Teuchos::rcp(
new Teuchos::ParameterList() );
254 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
258 algo.run(optProb,print0,*outStream0);
264 ofs.open(
"output_example_08.txt",std::ofstream::out);
265 for (
int i = 0; i < nx+2; i++ ) {
266 ofs << std::scientific << std::setprecision(10);
267 ofs << std::setw(20) << std::left << (
RealT)i/((
RealT)nx+1.0);
268 ofs << std::setw(20) << std::left << (*z_rcp)[i];
275 catch (std::logic_error err) {
276 *outStream << err.what() <<
"\n";
282 std::cout <<
"End Result: TEST FAILED\n";
284 std::cout <<
"End Result: TEST PASSED\n";
int main(int argc, char *argv[])
H1VectorPrimal< RealT > DualConstraintVector
L2VectorDual< RealT > DualControlVector
std::vector< std::vector< Real > > checkObjectiveGradient(const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
H1VectorDual< RealT > DualStateVector
Provides an interface to run optimization algorithms.
std::vector< std::vector< Real > > checkObjectiveHessVec(const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
H1VectorPrimal< RealT > PrimalStateVector
L2VectorPrimal< RealT > PrimalControlVector
Real getSolutionStatistic(void)
H1VectorDual< RealT > PrimalConstraintVector