44 #include "Teuchos_oblackholestream.hpp"
45 #include "Teuchos_GlobalMPISession.hpp"
57 double run_test(MPI_Comm comm,
const ROL::Ptr<std::ostream> & outStream,
int numSteps);
59 int main(
int argc,
char* argv[])
61 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
64 int iprint = argc - 1;
65 ROL::Ptr<std::ostream> outStream;
66 Teuchos::oblackholestream bhs;
68 outStream = ROL::makePtrFromRef(std::cout);
70 outStream = ROL::makePtrFromRef(bhs);
75 MPI_Comm_size(MPI_COMM_WORLD, &numRanks);
76 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
83 *outStream <<
"WARNING: This test is most effective with three processors" << std::endl;
86 MPI_Comm_split(MPI_COMM_WORLD,myRank==0,myRank,&subComm);
93 double all_final =
run_test(MPI_COMM_WORLD, outStream,numSteps);
95 MPI_Barrier(MPI_COMM_WORLD);
101 double sub_final =
run_test(subComm, outStream,numSteps);
103 *outStream <<
"ALL processors result = " << all_final << std::endl;
104 *outStream <<
"SUB processors result = " << sub_final << std::endl;
106 if(sub_final!=all_final) {
108 MPI_Comm_size(subComm, &subRanks);
110 std::stringstream ss;
111 ss <<
"ALL processors result does not match SUB(p=" << subRanks <<
") result: " << all_final <<
" != " << sub_final;
112 throw std::logic_error(ss.str());
116 *outStream <<
"ALL processors result = " << all_final << std::endl;
119 catch (std::logic_error err) {
120 *outStream << err.what() <<
"\n";
126 MPI_Comm_free(&subComm);
128 int errors = std::abs(errorFlag);
129 MPI_Allreduce(&errors,&errorFlag,1,MPI_INT,MPI_MAX,MPI_COMM_WORLD);
132 std::cout <<
"End Result: TEST FAILED\n";
134 std::cout <<
"End Result: TEST PASSED\n";
139 double run_test(MPI_Comm comm,
const ROL::Ptr<std::ostream> & outStream,
int numSteps)
141 typedef ROL::Ptr<ROL::Vector<Real>> PtrVector;
146 MPI_Comm_size(comm, &numRanks);
147 MPI_Comm_rank(comm, &myRank);
149 *outStream <<
"Proc " << myRank <<
"/" << numRanks << std::endl;
151 double totaltime = 1.75;
152 double dt = totaltime/numSteps;
153 double omega = 2.0*M_PI;
156 ROL::Ptr<const ROL::PinTCommunicators> communicators = ROL::makePtr<ROL::PinTCommunicators>(comm,1);
158 ROL::Ptr< ROL::PinTVector<Real>> state, state_unit;
159 ROL::Ptr< ROL::PinTVector<Real>> control, control_unit;
163 ROL::Ptr<std::vector<Real>> u_data_ptr = ROL::makePtr<std::vector<Real>>(3);
164 std::vector<Real> & u_data = *u_data_ptr;
168 PtrVector u = ROL::makePtr<ROL::StdVector<Real>>(u_data_ptr);
170 ROL::Ptr<std::vector<Real>> u_data_unit_ptr = ROL::makePtr<std::vector<Real>>(3);
171 std::vector<Real> & u_data_unit = *u_data_unit_ptr;
172 u_data_unit[0] = 1.0;
173 u_data_unit[1] = 1.0;
174 u_data_unit[2] = 1.0;
175 PtrVector u_unit = ROL::makePtr<ROL::StdVector<Real>>(u_data_unit_ptr);
179 std::vector<Real> z_data(1);
181 PtrVector z = ROL::makePtr<ROL::StdVector<Real>>(ROL::makePtr<std::vector<Real>>(z_data));
188 state->getVectorPtr(-1)->set(*u);
189 state_unit->getVectorPtr(-1)->zero();
192 ROL::Ptr<ROL::Constraint_TimeSimOpt<Real>> step_constraint = ROL::makePtr<ODE_Constraint<Real>>(dt,omega);
194 ROL::Ptr<ROL::Constraint_PinTSimOpt<Real>> pint_constraint = ROL::makePtr<ROL::Constraint_PinTSimOpt<Real>>(step_constraint);
196 PtrVector u = state_unit;
197 PtrVector z = control_unit->clone();
198 PtrVector c = state->clone();
199 PtrVector jv = c->clone();
200 PtrVector v_u = state_unit->clone();
201 PtrVector v_z = control_unit->clone();
202 PtrVector w_u = state->dual().clone();
203 PtrVector w_z = control->dual().clone();
204 PtrVector w_c = c->dual().clone();
205 PtrVector hv_u = state->dual().clone();
206 PtrVector hv_z = z->dual().clone();
208 ROL::RandomizeVector<RealT>(*u);
209 ROL::RandomizeVector<RealT>(*z);
210 ROL::RandomizeVector<RealT>(*v_u);
211 ROL::RandomizeVector<RealT>(*v_z);
212 ROL::RandomizeVector<RealT>(*w_u);
213 ROL::RandomizeVector<RealT>(*w_z);
214 ROL::RandomizeVector<RealT>(*w_c);
220 *outStream <<
"Checking solve" << std::endl;
222 double solveNorm = pint_constraint->checkSolve(*u,*z,*c,
true,*outStream);
225 std::stringstream ss;
226 ss <<
"Solve failed on problem with " << numRanks <<
" processors and " << numSteps <<
": residual = " << solveNorm
227 <<
" > " << tol << std::endl;
228 throw std::logic_error(ss.str());
231 *outStream <<
"Solve checked out for p=" << numRanks <<
" with residual = " << solveNorm << std::endl;
239 *outStream <<
"Checking apply Jacobian 1" << std::endl;
241 auto errors = pint_constraint->checkApplyJacobian_1(*u,*z,*v_u,*jv,
true,*outStream);
242 if(errors[6][3]/errors[6][1] >= 1e-6)
243 throw std::logic_error(
"Constraint apply jacobian 1 is incorrect");
250 *outStream <<
"Checking apply Jacobian 2" << std::endl;
252 auto errors = pint_constraint->checkApplyJacobian_2(*u,*z,*v_z,*jv,
true,*outStream);
253 if(errors[6][3]/errors[6][1] >= 1e-6)
254 throw std::logic_error(
"Constraint apply jacobian 2 is incorrect");
261 *outStream <<
"Checking apply Adjoint Jacobian 1" << std::endl;
263 auto error = pint_constraint->checkAdjointConsistencyJacobian_1(*w_u,*v_u,*u,*z,
true,*outStream);
265 throw std::logic_error(
"Constraint apply adjoint jacobian 1 is incorrect");
272 *outStream <<
"Checking apply Adjoint Jacobian 2" << std::endl;
274 auto error = pint_constraint->checkAdjointConsistencyJacobian_2(*w_u,*v_z,*u,*z,
true,*outStream);
276 throw std::logic_error(
"Constraint apply adjoint jacobian 2 is incorrect");
283 *outStream <<
"Checking apply Adjoint Hessian_11" << std::endl;
285 auto errors = pint_constraint->checkApplyAdjointHessian_11(*u,*z,*w_c,*v_u,*hv_u,
true,*outStream);
286 if(errors[6][3]/errors[6][1] >= 1e-4)
287 throw std::logic_error(
"Constraint apply Adjoint Hessian 11 is incorrect: " + std::to_string( errors[6][3]/errors[6][1]));
294 *outStream <<
"Checking apply Adjoint Hessian_12" << std::endl;
296 auto errors = pint_constraint->checkApplyAdjointHessian_12(*u,*z,*w_c,*v_u,*hv_z,
true,*outStream);
297 if(errors[6][3] >= 1e-5)
298 throw std::logic_error(
"Constraint apply Adjoint Hessian 12 is incorrect");
305 *outStream <<
"Checking apply Adjoint Hessian_21" << std::endl;
307 auto errors = pint_constraint->checkApplyAdjointHessian_21(*u,*z,*w_c,*v_z,*hv_u,
true,*outStream);
308 if(errors[6][3] >= 1e-5)
309 throw std::logic_error(
"Constraint apply Adjoint Hessian 12 is incorrect");
316 *outStream <<
"Checking apply Adjoint Hessian_22" << std::endl;
318 auto errors = pint_constraint->checkApplyAdjointHessian_22(*u,*z,*w_c,*v_z,*hv_z,
true,*outStream);
319 if(errors[6][3] >= 1e-5)
320 throw std::logic_error(
"Constraint apply Adjoint Hessian 12 is incorrect");
326 double final_result = 0.0;
328 PtrVector z_unit = control_unit->clone();
330 z->axpy(3.0,*z_unit);
332 *outStream <<
"ZNORM = " << z->norm() << std::endl;
334 pint_constraint->solve(*c,*u,*z,tol);
337 if(myRank==numRanks-1) {
338 PtrVector final_state = state->getVectorPtr(state->numOwnedSteps()-1);
343 final_result = fs_stdvec[0];
346 MPI_Bcast(&final_result,1,MPI_DOUBLE,numRanks-1,comm);
Ptr< PinTVector< Real > > buildPinTVector(const Ptr< const PinTCommunicators > &communicators, int steps, const std::vector< int > &stencil, const Ptr< Vector< Real >> &localVector)
double run_test(MPI_Comm comm, const ROL::Ptr< std::ostream > &outStream, int numSteps)
ROL::Ptr< const std::vector< Element > > getVector() const
int main(int argc, char *argv[])