// @HEADER // ***************************************************************************** // Rapid Optimization Library (ROL) Package // // Copyright 2014 NTESS and the ROL contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /*! \file example_01.cpp \brief Shows how to solve the mother problem of PDE-constrained optimization: */ #include "Teuchos_Comm.hpp" #include "ROL_Stream.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Teuchos_XMLParameterListHelpers.hpp" #include "Tpetra_Core.hpp" #include "Tpetra_Version.hpp" #include "ROL_Reduced_Objective_SimOpt.hpp" #include "ROL_OptimizationProblem.hpp" #include "ROL_ScaledTpetraMultiVector.hpp" #include "ROL_ScaledStdVector.hpp" #include "ROL_Bounds.hpp" #include "ROL_AugmentedLagrangianStep.hpp" #include "ROL_AugmentedLagrangian.hpp" #include "ROL_Algorithm.hpp" #include "ROL_Elementwise_Reduce.hpp" #include #include #include "data.hpp" #include "filter.hpp" #include "parametrized_objective.hpp" #include "parametrized_constraint.hpp" #include "volume_constraint.hpp" #include "build_sampler.hpp" //#include typedef double RealT; ROL::Ptr > createTpetraVector(const ROL::Ptr > &map) { return ROL::makePtr>(map, 1, true); } int main(int argc, char *argv[]) { //feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; ROL::Ptr outStream; ROL::nullstream bhs; // outputs nothing /*** Initialize communicator. ***/ Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); ROL::Ptr > comm = Tpetra::getDefaultComm(); const int myRank = comm->getRank(); if ((iprint > 0) && (myRank == 0)) { outStream = ROL::makePtrFromRef(std::cout); } else { outStream = ROL::makePtrFromRef(bhs); } int errorFlag = 0; // *** Example body. try { /*** Read in XML input ***/ std::string filename = "input.xml"; Teuchos::RCP parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); std::string stoch_filename = "stochastic.xml"; Teuchos::RCP stoch_parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( stoch_filename, stoch_parlist.ptr() ); /*** Initialize main data structure. ***/ ROL::Ptr > serial_comm = ROL::makePtr>(); ROL::Ptr > data = ROL::makePtr>(serial_comm, parlist, outStream); /*** Initialize density filter. ***/ ROL::Ptr > filter = ROL::makePtr>(serial_comm, parlist, outStream); /*** Build vectors and dress them up as ROL vectors. ***/ ROL::Ptr > vecmap_u = data->getDomainMapA(); ROL::Ptr > vecmap_z = data->getCellMap(); ROL::Ptr > u_ptr = createTpetraVector(vecmap_u); ROL::Ptr > z_ptr = createTpetraVector(vecmap_z); ROL::Ptr > du_ptr = createTpetraVector(vecmap_u); ROL::Ptr > dw_ptr = createTpetraVector(vecmap_u); ROL::Ptr > dz_ptr = createTpetraVector(vecmap_z); ROL::Ptr > dz2_ptr = createTpetraVector(vecmap_z); ROL::Ptr > vc_ptr = ROL::makePtr>(1, 0); ROL::Ptr > vc_lam_ptr = ROL::makePtr>(1, 0); ROL::Ptr > vscale_ptr = ROL::makePtr>(1, 0); // Set all values to 1 in u, z. RealT one(1), two(2); u_ptr->putScalar(one); // Set z to gray solution. RealT volFrac = parlist->sublist("ElasticityTopoOpt").get("Volume Fraction"); z_ptr->putScalar(volFrac); // Set scaling vector for constraint RealT W = parlist->sublist("Geometry").get("Width"); RealT H = parlist->sublist("Geometry").get("Height"); (*vscale_ptr)[0] = one/std::pow(W*H*(one-volFrac),two); // Set Scaling vector for density bool useZscale = parlist->sublist("Problem").get("Use Scaled Density Vectors"); RealT densityScaling = parlist->sublist("Problem").get("Density Scaling"); ROL::Ptr > scaleVec = createTpetraVector(vecmap_z); scaleVec->putScalar(densityScaling); if ( !useZscale ) { scaleVec->putScalar(one); } ROL::Ptr > zscale_ptr = scaleVec->getVector(0); // Randomize d vectors. du_ptr->randomize(); //du_ptr->scale(0); dw_ptr->randomize(); dz_ptr->randomize(); //dz_ptr->scale(0); dz2_ptr->randomize(); // Create ROL::TpetraMultiVectors. ROL::Ptr > up = ROL::makePtr>(u_ptr); ROL::Ptr > dup = ROL::makePtr>(du_ptr); ROL::Ptr > dwp = ROL::makePtr>(dw_ptr); ROL::Ptr > zp = ROL::makePtr>(z_ptr,zscale_ptr); ROL::Ptr > dzp = ROL::makePtr>(dz_ptr,zscale_ptr); ROL::Ptr > dz2p = ROL::makePtr>(dz2_ptr,zscale_ptr); ROL::Ptr > vcp = ROL::makePtr>(vc_ptr,vscale_ptr); ROL::Ptr > vc_lamp = ROL::makePtr>(vc_lam_ptr,vscale_ptr); // Create ROL SimOpt vectors. ROL::Vector_SimOpt x(up,zp); ROL::Vector_SimOpt d(dup,dzp); ROL::Vector_SimOpt d2(dwp,dz2p); /*** Build sampler. ***/ BuildSampler buildSampler(comm,*stoch_parlist,*parlist); buildSampler.print("samples"); /*** Compute compliance objective function scaling. ***/ RealT min(ROL::ROL_INF()), gmin(0), max(0), gmax(0), sum(0), gsum(0), tmp(0); Teuchos::Array dotF(1, 0); RealT minDensity = parlist->sublist("ElasticitySIMP").get("Minimum Density"); for (int i = 0; i < buildSampler.get()->numMySamples(); ++i) { data->updateF(buildSampler.get()->getMyPoint(i)); (data->getVecF())->dot(*(data->getVecF()),dotF.view(0,1)); tmp = minDensity/dotF[0]; min = ((min < tmp) ? min : tmp); max = ((max > tmp) ? max : tmp); sum += buildSampler.get()->getMyWeight(i) * tmp; } ROL::Elementwise::ReductionMin ROLmin; buildSampler.getBatchManager()->reduceAll(&min,&gmin,1,ROLmin); ROL::Elementwise::ReductionMax ROLmax; buildSampler.getBatchManager()->reduceAll(&max,&gmax,1,ROLmax); buildSampler.getBatchManager()->sumAll(&sum,&gsum,1); bool useExpValScale = stoch_parlist->sublist("Problem").get("Use Expected Value Scaling",false); RealT scale = (useExpValScale ? gsum : gmin); /*** Build objective function, constraint and reduced objective function. ***/ ROL::Ptr > obj = ROL::makePtr>(data, filter, parlist,scale); ROL::Ptr > con = ROL::makePtr>(data, filter, parlist); ROL::Ptr > objReduced = ROL::makePtr>(obj, con, up, zp, dwp); ROL::Ptr > volcon = ROL::makePtr>(data, parlist); /*** Build bound constraint ***/ ROL::Ptr > lo_ptr = ROL::makePtr>(vecmap_z, 1, true); ROL::Ptr > hi_ptr = ROL::makePtr>(vecmap_z, 1, true); lo_ptr->putScalar(0.0); hi_ptr->putScalar(1.0); ROL::Ptr > lop = ROL::makePtr>(lo_ptr, zscale_ptr); ROL::Ptr > hip = ROL::makePtr>(hi_ptr, zscale_ptr); ROL::Ptr > bnd = ROL::makePtr>(lop,hip); /*** Build Stochastic Functionality. ***/ ROL::OptimizationProblem opt(objReduced,zp,bnd); opt.setStochasticObjective(*stoch_parlist,buildSampler.get()); /*** Check functional interface. ***/ bool checkDeriv = parlist->sublist("Problem").get("Derivative Check",false); if ( checkDeriv ) { // *outStream << "Checking Objective:" << "\n"; // obj->checkGradient(x,d,true,*outStream); // obj->checkHessVec(x,d,true,*outStream); // obj->checkHessSym(x,d,d2,true,*outStream); // *outStream << "Checking Constraint:" << "\n"; // con->checkAdjointConsistencyJacobian(*dup,d,x,true,*outStream); // con->checkAdjointConsistencyJacobian_1(*dwp, *dup, *up, *zp, true, *outStream); // con->checkAdjointConsistencyJacobian_2(*dwp, *dzp, *up, *zp, true, *outStream); // con->checkInverseJacobian_1(*up,*up,*up,*zp,true,*outStream); // con->checkInverseAdjointJacobian_1(*up,*up,*up,*zp,true,*outStream); // con->checkApplyJacobian(x,d,*up,true,*outStream); // con->checkApplyAdjointHessian(x,*dup,d,x,true,*outStream); *outStream << "Checking Reduced Objective:" << "\n"; opt.check(*outStream); *outStream << "Checking Volume Constraint:" << "\n"; volcon->checkAdjointConsistencyJacobian(*vcp,*dzp,*zp,true,*outStream); volcon->checkApplyJacobian(*zp,*dzp,*vcp,true,*outStream); volcon->checkApplyAdjointHessian(*zp,*vcp,*dzp,*zp,true,*outStream); } /*** Run optimization ***/ ROL::Ptr> step = ROL::makePtr>(*parlist); ROL::Ptr> status = ROL::makePtr>(*parlist); ROL::AugmentedLagrangian augLag(opt.getObjective(),volcon,*vc_lamp,1.0, *opt.getSolutionVector(),*vcp,*parlist); ROL::Algorithm algo(step,status,false); std::clock_t timer = std::clock(); algo.run(*opt.getSolutionVector(),*vc_lamp,augLag,*volcon,*opt.getBoundConstraint(),true,*outStream); *outStream << "Optimization time: " << static_cast(std::clock()-timer)/static_cast(CLOCKS_PER_SEC) << " seconds." << std::endl; data->outputTpetraVector(z_ptr, "density.txt"); data->outputTpetraVector(u_ptr, "state.txt"); data->outputTpetraVector(zscale_ptr, "weights.txt"); // Build objective function distribution RealT val(0); int nSamp = stoch_parlist->sublist("Problem").get("Number of Output Samples",10); stoch_parlist->sublist("Problem").set("Number of Samples",nSamp); BuildSampler buildSampler_dist(comm,*stoch_parlist,*parlist); std::stringstream name; name << "samples_" << buildSampler_dist.getBatchManager()->batchID() << ".txt"; std::ofstream file; file.open(name.str()); std::vector sample; RealT tol = 1.e-8; std::clock_t timer_print = std::clock(); for (int i = 0; i < buildSampler_dist.get()->numMySamples(); ++i) { sample = buildSampler_dist.get()->getMyPoint(i); objReduced->setParameter(sample); val = objReduced->value(*zp,tol); for (int j = 0; j < static_cast(sample.size()); ++j) { file << sample[j] << " "; } file << val << "\n"; } file.close(); *outStream << "Output time: " << static_cast(std::clock()-timer_print)/static_cast(CLOCKS_PER_SEC) << " seconds." << std::endl; } catch (std::logic_error& err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }