Debian Codegraph @

packages/rol/example/PDE-OPT/0ld/elasticity/example_02.cpp

raw
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_02.cpp
11 \brief Shows how to solve the mother problem of PDE-constrained optimization:
12 */
13
14 #include "Teuchos_Comm.hpp"
15 #include "ROL_Stream.hpp"
16 #include "Teuchos_GlobalMPISession.hpp"
17 #include "Teuchos_XMLParameterListHelpers.hpp"
18
19 #include "Tpetra_Core.hpp"
20 #include "Tpetra_Version.hpp"
21
22 #include "ROL_Algorithm.hpp"
23 #include "ROL_TrustRegionStep.hpp"
24 #include "ROL_CompositeStep.hpp"
25 #include "ROL_Reduced_Objective_SimOpt.hpp"
26 #include "ROL_TpetraMultiVector.hpp"
27
28 #include <iostream>
29 #include <algorithm>
30
31 #include "dataSIMP.hpp"
32
33 typedef double RealT;
34
35 int main(int argc, char *argv[]) {
36
37 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
38 int iprint = argc - 1;
39 ROL::Ptr<std::ostream> outStream;
40 ROL::nullstream bhs; // outputs nothing
41
42 /*** Initialize communicator. ***/
43 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &bhs);
44 ROL::Ptr<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
45 const int myRank = comm->getRank();
46 if ((iprint > 0) && (myRank == 0)) {
47 outStream = ROL::makePtrFromRef(std::cout);
48 }
49 else {
50 outStream = ROL::makePtrFromRef(bhs);
51 }
52
53 int errorFlag = 0;
54
55 // *** Example body.
56 try {
57
58 /*** Read in XML input ***/
59 std::string filename = "input.xml";
60 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
61 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
62
63 /*** Initialize main data structure. ***/
64 ROL::Ptr<ElasticitySIMPData<RealT> > data = ROL::makePtr<ElasticitySIMPData<RealT>>(comm, parlist, outStream);
65 ROL::Ptr<Tpetra::MultiVector<RealT> > rhs = data->getVecF();
66 data->getSolver()->setB(rhs);
67 ROL::Ptr<Tpetra::MultiVector<RealT> > sol = ROL::makePtr<Tpetra::MultiVector<RealT>>(rhs->getMap(), 1, true);
68 //sol->putScalar(0.0);
69 data->getSolver()->setX(sol);
70 data->getSolver()->solve();
71 //std::cout<<sol->getData(0)(0, 100)<<std::endl;
72 data->outputTpetraVector(sol, "state.txt");
73
74 }
75 catch (std::logic_error& err) {
76 *outStream << err.what() << "\n";
77 errorFlag = -1000;
78 }; // end try
79
80 if (errorFlag != 0)
81 std::cout << "End Result: TEST FAILED\n";
82 else
83 std::cout << "End Result: TEST PASSED\n";
84
85 return 0;
86 }