27 #include "Teuchos_GlobalMPISession.hpp"
56 Teuchos::SerialDenseMatrix<int, Real>
H_;
78 void apply_mass(std::vector<Real> &Mz,
const std::vector<Real> &z ) {
82 Mz[i] =
hu_/6.0*(2.0*z[i] + z[i+1]);
84 else if ( i == nu_-1 ) {
85 Mz[i] =
hu_/6.0*(z[i-1] + 2.0*z[i]);
88 Mz[i] =
hu_/6.0*(z[i-1] + 4.0*z[i] + z[i+1]);
94 return (x <= 0.5) ? 1.0 : 0.0;
98 const std::vector<Real> &d,
const std::vector<Real> &u,
102 for (
uint i = 0; i <
nu_; i++) {
104 Bd[i] = 1.0/
hu_*( u[i]*d[i] + (u[i]-u[i+1])*d[i+1] );
106 else if ( i == nu_-1 ) {
107 Bd[i] = 1.0/
hu_*( (u[i]-u[i-1])*d[i] + u[i]*d[i+1] );
110 Bd[i] = 1.0/
hu_*( (u[i]-u[i-1])*d[i] + (u[i]-u[i+1])*d[i+1] );
120 const std::vector<Real> &d,
const std::vector<Real> &u,
121 bool addBC =
true ) {
124 for (
uint i = 0; i <
nz_; i++) {
126 Bd[i] = 1.0/
hu_*u[i]*d[i];
128 else if ( i == nz_-1 ) {
129 Bd[i] = 1.0/
hu_*u[i-1]*d[i-1];
132 Bd[i] = 1.0/
hu_*( (u[i]-u[i-1])*(d[i]-d[i-1]) );
144 std::vector<Real> d(
nu_,1.0);
145 std::vector<Real> o(
nu_-1,1.0);
146 for (
uint i = 0; i <
nu_; i++ ) {
147 d[i] = (z[i] + z[i+1])/
hu_;
158 Teuchos::LAPACK<int,Real> lp;
162 lp.PTTRF(nu_,&d[0],&o[0],&info);
163 lp.PTTRS(nu_,nhrs,&d[0],&o[0],&u[0],ldb,&info);
170 for (
uint i = 0; i <
nu_; i++ ) {
171 d[i] = (z[i] + z[i+1])/
hu_;
178 for (
uint i = 0; i <
nu_; i++) {
185 Teuchos::LAPACK<int,Real> lp;
189 lp.PTTRF(nu_,&d[0],&o[0],&info);
190 lp.PTTRS(nu_,nhrs,&d[0],&o[0],&p[0],ldb,&info);
194 const std::vector<Real> &u,
const std::vector<Real> &z) {
198 for (
uint i = 0; i <
nu_; i++ ) {
199 d[i] = (z[i] + z[i+1])/
hu_;
209 Teuchos::LAPACK<int,Real> lp;
213 lp.PTTRF(nu_,&d[0],&o[0],&info);
214 lp.PTTRS(nu_,nhrs,&d[0],&o[0],&w[0],ldb,&info);
218 const std::vector<Real> &v,
const std::vector<Real> &p,
219 const std::vector<Real> &u,
const std::vector<Real> &z) {
223 for (
uint i = 0; i <
nu_; i++ ) {
224 d[i] = (z[i] + z[i+1])/
hu_;
233 std::vector<Real> res(nu_,0.0);
235 for (
uint i = 0; i <
nu_; i++) {
239 Teuchos::LAPACK<int,Real> lp;
243 lp.PTTRF(nu_,&d[0],&o[0],&info);
244 lp.PTTRS(nu_,nhrs,&d[0],&o[0],&q[0],ldb,&info);
252 Real tol = std::sqrt(ROL::ROL_EPSILON<Real>());
254 ROL::Ptr<V> e = z.
clone();
255 ROL::Ptr<V> h = z.
clone();
256 for (
uint i = 0; i <
nz_; i++ ) {
259 for (
uint j = 0; j <
nz_; j++ ) {
261 (
H_)(j,i) = e->dot(*h);
264 std::vector<vector> eigenvals = ROL::computeEigenvalues<Real>(
H_);
265 std::sort((eigenvals[0]).begin(), (eigenvals[0]).end());
266 Real inertia = (eigenvals[0])[0];
267 Real correction = 0.0;
268 if ( inertia <= 0.0 ) {
269 correction = (1.0+std::sqrt(ROL::ROL_EPSILON<Real>()))*std::abs(inertia);
270 if ( inertia == 0.0 ) {
272 while ( eigenvals[0][cnt] == 0.0 ) {
275 correction = std::sqrt(ROL::ROL_EPSILON<Real>())*eigenvals[0][cnt];
276 if ( cnt == nz_-1 ) {
280 for (
uint i = 0; i <
nz_; i++ ) {
281 (
H_)(i,i) += correction;
291 ROL::Ptr<const vector> zp =
getVector(z);
310 res =
hu_/6.0*(2.0*res1 + res2)*res1;
312 else if ( i == nu_-1 ) {
315 res = hu_/6.0*(res1 + 2.0*res2)*res2;
321 res = hu_/6.0*(res1 + 4.0*res2 + res3)*res2;
332 ROL::Ptr<const vector> zp =
getVector(z);
346 for (
uint i = 0; i <
nz_; i++ ) {
372 ROL::Ptr<const vector> vp =
getVector(v);
373 ROL::Ptr<const vector> zp =
getVector(z);
395 std::vector<Real> tmp(
nz_,0.0);
409 using ROL::constPtrCast;
414 ROL::Ptr<vector> vp = ROL::constPtrCast<vector>(
getVector(v));
416 Teuchos::SerialDenseVector<int, Real> hv_teuchos(Teuchos::View, &((*hvp)[0]), static_cast<int>(
nz_));
417 Teuchos::SerialDenseVector<int, Real> v_teuchos(Teuchos::View, &(( *vp)[0]), static_cast<int>(
nz_));
418 hv_teuchos.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0,
H_, v_teuchos, 0.0);
427 int main(
int argc,
char *argv[]) {
429 typedef std::vector<RealT> vector;
437 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
440 int iprint = argc - 1;
441 ROL::Ptr<std::ostream> outStream;
444 outStream = ROL::makePtrFromRef(std::cout);
446 outStream = ROL::makePtrFromRef(bhs);
459 ROL::Ptr<vector> x_ptr = ROL::makePtr<vector>(
dim, 0.0);
460 ROL::Ptr<vector> y_ptr = ROL::makePtr<vector>(
dim, 0.0);
463 for (uint i=0; i<
dim; i++) {
464 (*x_ptr)[i] = (
RealT)rand()/(
RealT)RAND_MAX + 1.e2;
465 (*y_ptr)[i] = (
RealT)rand()/(
RealT)RAND_MAX + 1.e2;
474 ROL::Ptr<vector> l_ptr = ROL::makePtr<vector>(
dim,1.0);
475 ROL::Ptr<vector> u_ptr = ROL::makePtr<vector>(
dim,10.0);
477 ROL::Ptr<V> lo = ROL::makePtr<SV>(l_ptr);
478 ROL::Ptr<V> up = ROL::makePtr<SV>(u_ptr);
482 ROL::ParameterList parlist;
485 parlist.sublist(
"General").sublist(
"Krylov").set(
"Absolute Tolerance",1.e-8);
486 parlist.sublist(
"General").sublist(
"Krylov").set(
"Relative Tolerance",1.e-4);
487 parlist.sublist(
"General").sublist(
"Krylov").set(
"Iteration Limit",static_cast<int>(dim));
489 parlist.sublist(
"Step").sublist(
"Primal Dual Active Set").set(
"Relative Step Tolerance",1.e-8);
490 parlist.sublist(
"Step").sublist(
"Primal Dual Active Set").set(
"Relative Gradient Tolerance",1.e-6);
491 parlist.sublist(
"Step").sublist(
"Primal Dual Active Set").set(
"Iteration Limit", 10);
492 parlist.sublist(
"Step").sublist(
"Primal Dual Active Set").set(
"Dual Scaling",(alpha>0.0)?alpha:1.e-4);
493 parlist.sublist(
"General").sublist(
"Secant").set(
"Use as Hessian",
true);
495 parlist.sublist(
"Status Test").set(
"Gradient Tolerance",1.e-12);
496 parlist.sublist(
"Status Test").set(
"Step Tolerance",1.e-14);
497 parlist.sublist(
"Status Test").set(
"Iteration Limit",100);
499 ROL::Ptr<ROL::Step<RealT>> step;
500 ROL::Ptr<ROL::StatusTest<RealT>> status;
503 step = ROL::makePtr<ROL::PrimalDualActiveSetStep<RealT>>(parlist);
504 status = ROL::makePtr<ROL::StatusTest<RealT>>(parlist);
509 algo.run(x,obj,icon,
true,*outStream);
513 file.open(
"control_PDAS.txt");
514 for ( uint i = 0; i <
dim; i++ ) {
515 file << (*x_ptr)[i] <<
"\n";
521 parlist.sublist(
"General").sublist(
"Secant").set(
"Use as Hessian",
false);
522 parlist.sublist(
"Step").sublist(
"Trust Region").set(
"Subproblem Solver",
"Truncated CG");
523 parlist.sublist(
"Step").sublist(
"Trust Region").set(
"Initial Radius", 1e3);
524 parlist.sublist(
"Step").sublist(
"Trust Region").set(
"Maximum Radius", 1e8);
525 step = ROL::makePtr<ROL::TrustRegionStep<RealT>>(parlist);
526 status = ROL::makePtr<ROL::StatusTest<RealT>>(parlist);
531 algo_tr.run(y,obj,icon,
true,*outStream);
533 std::ofstream file_tr;
534 file_tr.open(
"control_TR.txt");
535 for ( uint i = 0; i <
dim; i++ ) {
536 file_tr << (*y_ptr)[i] <<
"\n";
540 std::vector<RealT> u;
542 std::ofstream file_u;
543 file_u.open(
"state.txt");
544 for ( uint i = 0; i < (dim-1); i++ ) {
545 file_u << u[i] <<
"\n";
549 ROL::Ptr<V> diff = x.clone();
552 RealT error = diff->norm()/std::sqrt((
RealT)dim-1.0);
553 std::cout <<
"\nError between PDAS solution and TR solution is " << error <<
"\n";
554 errorFlag = ((error > 1.e2*std::sqrt(ROL::ROL_EPSILON<RealT>())) ? 1 : 0);
557 catch (std::logic_error& err) {
558 *outStream << err.what() <<
"\n";
563 std::cout <<
"End Result: TEST FAILED\n";
565 std::cout <<
"End Result: TEST PASSED\n";
Provides the interface to evaluate objective functions.
typename PV< Real >::size_type size_type
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
std::vector< Real > vector
virtual ROL::Ptr< Vector > basis(const int i) const
Return i-th basis vector.
void solve_state_equation(std::vector< Real > &u, const std::vector< Real > &z)
void apply_transposed_linearized_control_operator(std::vector< Real > &Bd, const std::vector< Real > &z, const std::vector< Real > &d, const std::vector< Real > &u, bool addBC=true)
Contains definitions of custom data types in ROL.
Contains definitions for helper functions in ROL.
Defines the linear algebra or vector space interface.
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
void solve_adjoint_sensitivity_equation(std::vector< Real > &q, const std::vector< Real > &w, const std::vector< Real > &v, const std::vector< Real > &p, const std::vector< Real > &u, const std::vector< Real > &z)
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
basic_nullstream< char, std::char_traits< char >> nullstream
void apply_mass(std::vector< Real > &Mz, const std::vector< Real > &z)
Real value(const ROL::Vector< Real > &z, Real &tol)
Compute value.
void update(const ROL::Vector< Real > &z, bool flag, int iter)
Update objective function.
void deactivateInertia(void)
Provides an interface to run optimization algorithms.
Provides the elementwise interface to apply upper and lower bound constraints.
void hessVec_true(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &z, Real &tol)
ROL::StdVector< Real > SV
void solve_adjoint_equation(std::vector< Real > &p, const std::vector< Real > &u, const std::vector< Real > &z)
void solve_state_sensitivity_equation(std::vector< Real > &w, const std::vector< Real > &v, const std::vector< Real > &u, const std::vector< Real > &z)
void hessVec(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &z, Real &tol)
Apply Hessian approximation to vector.
int main(int argc, char *argv[])
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
void apply_linearized_control_operator(std::vector< Real > &Bd, const std::vector< Real > &z, const std::vector< Real > &d, const std::vector< Real > &u, bool addBC=true)
void activateInertia(void)
void hessVec_inertia(ROL::Vector< Real > &hv, const ROL::Vector< Real > &v, const ROL::Vector< Real > &z, Real &tol)
Objective_PoissonInversion(int nz=32, Real alpha=1.e-4)
void gradient(ROL::Vector< Real > &g, const ROL::Vector< Real > &z, Real &tol)
Compute gradient.
Real evaluate_target(Real x)
ROL::Ptr< const vector > getVector(const V &x)
Teuchos::SerialDenseMatrix< int, Real > H_
ROL::Ptr< vector > getVector(V &x)