| 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_01.cpp |
| 11 |
\brief Shows how to solve the stuctural topology optimization problem. |
| 12 |
|
| 13 |
*/ |
| 14 |
|
| 15 |
#include "Teuchos_Comm.hpp" |
| 16 |
#include "Teuchos_Time.hpp" |
| 17 |
#include "Teuchos_GlobalMPISession.hpp" |
| 18 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 19 |
|
| 20 |
#include "Tpetra_Core.hpp" |
| 21 |
#include "Tpetra_Version.hpp" |
| 22 |
|
| 23 |
#include <iostream> |
| 24 |
#include <algorithm> |
| 25 |
|
| 26 |
#include "ROL_Solver.hpp" |
| 27 |
#include "opfactory.hpp" |
| 28 |
//#include <fenv.h> |
| 29 |
|
| 30 |
typedef double RealT; |
| 31 |
|
| 32 |
int main(int argc, char *argv[]) { |
| 33 |
//feenableexcept(FE_OVERFLOW | FE_DIVBYZERO | FE_INVALID); |
| 34 |
// This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. |
| 35 |
int iprint = argc - 1; |
| 36 |
ROL::Ptr<std::ostream> outStream; |
| 37 |
ROL::nullstream bhs; // outputs nothing |
| 38 |
|
| 39 |
/*** Initialize communicator. ***/ |
| 40 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs); |
| 41 |
ROL::Ptr<const Teuchos::Comm<int>> comm |
| 42 |
= Tpetra::getDefaultComm(); |
| 43 |
const int myRank = comm->getRank(); |
| 44 |
if ((iprint> 0) && (myRank == 0)) { |
| 45 |
outStream = ROL::makePtrFromRef(std::cout); |
| 46 |
} |
| 47 |
else { |
| 48 |
outStream = ROL::makePtrFromRef(bhs); |
| 49 |
} |
| 50 |
int errorFlag = 0; |
| 51 |
|
| 52 |
// *** Example body. |
| 53 |
try { |
| 54 |
|
| 55 |
/*** Read in XML input ***/ |
| 56 |
std::string filename = "input_ex01.xml"; |
| 57 |
Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); |
| 58 |
Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); |
| 59 |
|
| 60 |
// Retrieve parameters. |
| 61 |
const std::string example = parlist->sublist("Problem").get("Example", "Default"); |
| 62 |
int probDim = parlist->sublist("Problem").get("Problem Dimension", 2); |
| 63 |
RealT maxWeight = parlist->sublist("Problem").get("Maximum Weight Fraction", 1.0); |
| 64 |
if (example == "2D Wheel" || |
| 65 |
example == "2D Truss" || |
| 66 |
example == "2D Cantilever with 1 Load" || |
| 67 |
example == "2D Cantilever with 3 Loads" || |
| 68 |
example == "2D Beams" || |
| 69 |
example == "2D Carrier Plate") { |
| 70 |
probDim = 2; |
| 71 |
} |
| 72 |
else if (example == "3D Cantilever") { |
| 73 |
probDim = 3; |
| 74 |
} |
| 75 |
*outStream << std::endl; |
| 76 |
*outStream << "Problem Data" << std::endl; |
| 77 |
*outStream << " Example: " << example << std::endl; |
| 78 |
*outStream << " Dimension: " << probDim << std::endl; |
| 79 |
*outStream << " Max Weight Fraction: " << maxWeight << std::endl; |
| 80 |
*outStream << std::endl; |
| 81 |
|
| 82 |
// Set up and solve. |
| 83 |
ROL::Ptr<ElasticityFactory<RealT>> factory |
| 84 |
= ROL::makePtr<ElasticityFactory<RealT>>(*parlist,comm,outStream); |
| 85 |
bool derivCheck = parlist->sublist("Problem").get("Check derivatives",false); |
| 86 |
if (derivCheck) { |
| 87 |
factory->check(); |
| 88 |
} |
| 89 |
Teuchos::Time algoTimer("Algorithm Time", true); |
| 90 |
ROL::Ptr<ROL::Problem<RealT>> problem = factory->build(); |
| 91 |
ROL::Solver<RealT> solver(problem,*parlist); |
| 92 |
solver.solve(*outStream); |
| 93 |
ROL::Ptr<ROL::Vector<RealT>> z = problem->getPrimalOptimizationVector(); |
| 94 |
algoTimer.stop(); |
| 95 |
*outStream << "Total optimization time = " << algoTimer.totalElapsedTime() << " seconds.\n"; |
| 96 |
|
| 97 |
// Output. |
| 98 |
factory->print(*z); |
| 99 |
|
| 100 |
// Get a summary from the time monitor. |
| 101 |
Teuchos::TimeMonitor::summarize(); |
| 102 |
} |
| 103 |
catch (std::logic_error& err) { |
| 104 |
*outStream << err.what() << "\n"; |
| 105 |
errorFlag = -1000; |
| 106 |
}; // end try |
| 107 |
|
| 108 |
if (errorFlag != 0) |
| 109 |
std::cout << "End Result: TEST FAILED\n"; |
| 110 |
else |
| 111 |
std::cout << "End Result: TEST PASSED\n"; |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |