Debian Codegraph @

packages/rol/example/PDE-OPT/0ld/poisson/data.hpp

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 data.hpp
11 \brief Generates and manages data for the Poisson example, including
12 all mesh and discretization data, matrices, etc.
13 */
14
15 #ifndef ROL_PDEOPT_POISSON_DATA_H
16 #define ROL_PDEOPT_POISSON_DATA_H
17
18 #include "Teuchos_GlobalMPISession.hpp"
19 #include "Teuchos_TimeMonitor.hpp"
20
21 #include "Tpetra_MultiVector.hpp"
22 #include "Tpetra_Vector.hpp"
23 #include "Tpetra_CrsGraph.hpp"
24 #include "Tpetra_CrsMatrix.hpp"
25 #include "Tpetra_Version.hpp"
26 #include "Tpetra_RowMatrixTransposer.hpp"
27 #include "MatrixMarket_Tpetra.hpp"
28
29 #include "Intrepid_HGRAD_QUAD_C2_FEM.hpp"
30 #include "Intrepid_HGRAD_QUAD_C1_FEM.hpp"
31 #include "Intrepid_DefaultCubatureFactory.hpp"
32 #include "Intrepid_FunctionSpaceTools.hpp"
33 #include "Intrepid_CellTools.hpp"
34
35 #include "Amesos2.hpp"
36
37 #include "../TOOLS/dofmanager.hpp"
38
39 template<class Real>
40 class PoissonData {
41
42 private:
43 using GO = typename Tpetra::Map<>::global_ordinal_type;
44
45 ROL::Ptr<MeshManager<Real> > meshMgr_;
46 ROL::Ptr<DofManager<Real> > dofMgr_;
47 std::vector<ROL::Ptr<Intrepid::Basis<Real, Intrepid::FieldContainer<Real> > > > basisPtrs_;
48
49 ROL::Ptr<const Teuchos::Comm<int> > commPtr_;
50 int myRank_;
51 int numProcs_;
52
53 Real alpha_;
54 int basisOrder_;
55
56 ROL::Ptr<const Tpetra::Map<> > myOverlapMap_;
57 ROL::Ptr<const Tpetra::Map<> > myUniqueMap_;
58 ROL::Ptr<Tpetra::CrsGraph<> > matGraph_;
59 ROL::Ptr<Tpetra::CrsMatrix<> > matA_;
60 ROL::Ptr<Tpetra::CrsMatrix<> > matA_dirichlet_;
61 ROL::Ptr<Tpetra::CrsMatrix<> > matA_dirichlet_trans_;
62 ROL::Ptr<Tpetra::CrsMatrix<> > matM_;
63 ROL::Ptr<Tpetra::CrsMatrix<> > matM_dirichlet_;
64 ROL::Ptr<Tpetra::CrsMatrix<> > matM_dirichlet_trans_;
65 ROL::Ptr<Tpetra::MultiVector<> > vecUd_;
66 ROL::Ptr<Tpetra::MultiVector<> > vecF_;
67 ROL::Ptr<Tpetra::MultiVector<> > vecF_overlap_;
68 ROL::Ptr<Tpetra::MultiVector<> > vecF_dirichlet_;
69
70 Teuchos::Array<GO> myCellIds_;
71
72 ROL::Ptr<Amesos2::Solver< Tpetra::CrsMatrix<>, Tpetra::MultiVector<> > > solverA_;
73 ROL::Ptr<Amesos2::Solver< Tpetra::CrsMatrix<>, Tpetra::MultiVector<> > > solverA_trans_;
74
75 shards::CellTopology cellType_;
76 int spaceDim_;
77 int numNodesPerCell_;
78 int numCubPoints_;
79
80 GO totalNumCells_;
81 GO totalNumDofs_;
82 GO numCells_;
83
84 ROL::Ptr<Intrepid::FieldContainer<Real> > cubPoints_;
85 ROL::Ptr<Intrepid::FieldContainer<Real> > cubWeights_;
86 ROL::Ptr<Intrepid::FieldContainer<Real> > cellNodes_;
87 ROL::Ptr<Intrepid::FieldContainer<Real> > cellJac_;
88 ROL::Ptr<Intrepid::FieldContainer<Real> > cellJacInv_;
89 ROL::Ptr<Intrepid::FieldContainer<Real> > cellJacDet_;
90 ROL::Ptr<Intrepid::FieldContainer<Real> > cellWeightedMeasure_;
91 ROL::Ptr<Intrepid::FieldContainer<Real> > valReference_;
92 ROL::Ptr<Intrepid::FieldContainer<Real> > gradReference_;
93 ROL::Ptr<Intrepid::FieldContainer<Real> > valPhysical_;
94 ROL::Ptr<Intrepid::FieldContainer<Real> > gradPhysical_;
95 ROL::Ptr<Intrepid::FieldContainer<Real> > valPhysicalWeighted_;
96 ROL::Ptr<Intrepid::FieldContainer<Real> > gradPhysicalWeighted_;
97 ROL::Ptr<Intrepid::FieldContainer<Real> > gradgradMats_;
98 ROL::Ptr<Intrepid::FieldContainer<Real> > valvalMats_;
99 ROL::Ptr<Intrepid::FieldContainer<Real> > cubPointsPhysical_;
100 ROL::Ptr<Intrepid::FieldContainer<Real> > dataF_;
101 ROL::Ptr<Intrepid::FieldContainer<Real> > datavalVecF_;
102 ROL::Ptr<Intrepid::FieldContainer<Real> > dofPoints_;
103 ROL::Ptr<Intrepid::FieldContainer<Real> > dofPointsPhysical_;
104 ROL::Ptr<Intrepid::FieldContainer<Real> > dataUd_;
105
106 public:
107
108 PoissonData(const ROL::Ptr<const Teuchos::Comm<int> > &comm,
109 const Teuchos::RCP<Teuchos::ParameterList> &parlist,
110 const ROL::Ptr<std::ostream> &outStream) {
111
112 /************************************/
113 /*** Retrieve communication data. ***/
114 /************************************/
115 commPtr_ = comm;
116 myRank_ = commPtr_->getRank();
117 numProcs_ = commPtr_->getSize();
118 *outStream << "Total number of processors: " << numProcs_ << std::endl;
119 /************************************/
120 /************************************/
121
122 /*************************************/
123 /*** Retrieve parameter list data. ***/
124 /*************************************/
125 alpha_ = parlist->sublist("Problem").get("Control penalty parameter", 1e-2);
126 basisOrder_ = parlist->sublist("Problem").get("Order of FE discretization", 1);
127 int cellSplit = parlist->sublist("Geometry").get("Partition type", 1);
128 /*************************************/
129 /*************************************/
130
131 /****************************************************************************/
132 /*** Initialize mesh / finite element fields / degree-of-freedom manager. ***/
133 /****************************************************************************/
134
135 // Mesh manager.
136 meshMgr_ = ROL::makePtr<MeshManager_Rectangle<Real>>(*parlist);
137 printMeshData(*outStream);
138 // Finite element fields.
139 ROL::Ptr<Intrepid::Basis<Real, Intrepid::FieldContainer<Real> > > basisPtr;
140 if (basisOrder_ == 1) {
141 basisPtr = ROL::makePtr<Intrepid::Basis_HGRAD_QUAD_C1_FEM<Real, Intrepid::FieldContainer<Real> >>();
142 }
143 else if (basisOrder_ == 2) {
144 basisPtr = ROL::makePtr<Intrepid::Basis_HGRAD_QUAD_C2_FEM<Real, Intrepid::FieldContainer<Real> >>();
145 }
146 basisPtrs_.resize(1, ROL::nullPtr);
147 basisPtrs_[0] = basisPtr;
148 // DOF coordinate interface.
149 ROL::Ptr<Intrepid::DofCoordsInterface<Intrepid::FieldContainer<Real> > > coord_iface =
150 ROL::dynamicPtrCast<Intrepid::DofCoordsInterface<Intrepid::FieldContainer<Real> > >(basisPtrs_[0]);
151 // Degree-of-freedom manager.
152 dofMgr_ = ROL::makePtr<DofManager<Real>>(meshMgr_, basisPtrs_);
153 // Retrieve total number of cells in the mesh.
154 totalNumCells_ = meshMgr_->getNumCells();
155 // Retrieve total number of degrees of freedom in the mesh.
156 totalNumDofs_ = dofMgr_->getNumDofs();
157
158 /****************************************************************************/
159 /****************************************************************************/
160
161
162 /****************************************************/
163 /*** Build parallel communication infrastructure. ***/
164 /****************************************************/
165
166 // Partition the cells in the mesh. We use a basic quasi-equinumerous partitioning,
167 // where the remainder, if any, is assigned to the last processor.
168 Teuchos::Array<GO> myGlobIds_;
169 Teuchos::Array<GO> cellOffsets_(numProcs_, 0);
170 GO cellsPerProc = totalNumCells_ / numProcs_;
171 numCells_ = cellsPerProc;
172 switch(cellSplit) {
173 case 0:
174 if (myRank_ == 0) { // remainder in the first
175 numCells_ += totalNumCells_ % numProcs_;
176 }
177 for (int i=1; i<numProcs_; ++i) {
178 cellOffsets_[i] = cellOffsets_[i-1] + cellsPerProc + (static_cast<int>(i==1))*(totalNumCells_ % numProcs_);
179 }
180 break;
181 case 1:
182 if (myRank_ == numProcs_-1) { // remainder in the last
183 numCells_ += totalNumCells_ % numProcs_;
184 }
185 for (int i=1; i<numProcs_; ++i) {
186 cellOffsets_[i] = cellOffsets_[i-1] + cellsPerProc;
187 }
188 break;
189 case 2:
190 if (myRank_ < (totalNumCells_%numProcs_)) { // spread remainder, starting from the first
191 numCells_++;
192 }
193 for (int i=1; i<numProcs_; ++i) {
194 cellOffsets_[i] = cellOffsets_[i-1] + cellsPerProc + (static_cast<int>(i-1<(totalNumCells_%numProcs_)));
195 }
196 break;
197 }
198 Intrepid::FieldContainer<GO> &cellDofs = *(dofMgr_->getCellDofs());
199 int numLocalDofs = cellDofs.dimension(1);
200 *outStream << "Cell offsets across processors: " << cellOffsets_ << std::endl;
201 for (int i=0; i<numCells_; ++i) {
202 myCellIds_.push_back(cellOffsets_[myRank_]+i);
203 for (int j=0; j<numLocalDofs; ++j) {
204 myGlobIds_.push_back( cellDofs(cellOffsets_[myRank_]+i,j) );
205 }
206 }
207 std::sort(myGlobIds_.begin(), myGlobIds_.end());
208 myGlobIds_.erase( std::unique(myGlobIds_.begin(), myGlobIds_.end()), myGlobIds_.end() );
209
210 // Build maps.
211 myOverlapMap_ = ROL::makePtr<Tpetra::Map<>>(Teuchos::OrdinalTraits<Tpetra::global_size_t>::invalid(),
212 myGlobIds_, 0, comm);
213 //std::cout << std::endl << myOverlapMap_->getLocalElementList();
214 /** One can also use the non-member function:
215 myOverlapMap_ = Tpetra::createNonContigMap<int,int>(myGlobIds_, comm);
216 to build the overlap map.
217 **/
218 myUniqueMap_ = Tpetra::createOneToOne(myOverlapMap_);
219 //std::cout << std::endl << myUniqueMap_->getLocalElementList() << std::endl;
220
221 /****************************************************/
222 /****************************************************/
223
224
225 /****************************************************/
226 /*** Set up local discretization data and arrays. ***/
227 /****************************************************/
228
229 // Retrieve some basic cell information.
230 cellType_ = (basisPtrs_[0])->getBaseCellTopology(); // get the cell type from any basis
231 spaceDim_ = cellType_.getDimension(); // retrieve spatial dimension
232 numNodesPerCell_ = cellType_.getNodeCount(); // retrieve number of nodes per cell
233
234 // Cubature data.
235 Intrepid::DefaultCubatureFactory<Real> cubFactory; // create cubature factory
236 int cubDegree = 4; // set cubature degree, e.g., 2
237 ROL::Ptr<Intrepid::Cubature<Real> > cellCub = cubFactory.create(cellType_, cubDegree); // create default cubature
238 numCubPoints_ = cellCub->getNumPoints(); // retrieve number of cubature points
239
240 int lfs = dofMgr_->getLocalFieldSize(0);
241
242 // Discretization data.
243 cubPoints_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCubPoints_, spaceDim_);
244 cubWeights_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCubPoints_);
245 cubPointsPhysical_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, numCubPoints_, spaceDim_);
246 dofPoints_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(lfs, spaceDim_);
247 dofPointsPhysical_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs, spaceDim_);
248 cellNodes_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, numNodesPerCell_, spaceDim_);
249 cellJac_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, numCubPoints_, spaceDim_, spaceDim_);
250 cellJacInv_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, numCubPoints_, spaceDim_, spaceDim_);
251 cellJacDet_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, numCubPoints_);
252 cellWeightedMeasure_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, numCubPoints_);
253 valReference_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(lfs, numCubPoints_);
254 gradReference_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(lfs, numCubPoints_, spaceDim_);
255 valPhysical_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs, numCubPoints_);
256 gradPhysical_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs, numCubPoints_, spaceDim_);
257 valPhysicalWeighted_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs, numCubPoints_);
258 gradPhysicalWeighted_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs, numCubPoints_, spaceDim_);
259 gradgradMats_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs, lfs);
260 valvalMats_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs, lfs);
261 dataF_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, numCubPoints_);
262 datavalVecF_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs);
263 dataUd_ = ROL::makePtr<Intrepid::FieldContainer<Real>>(numCells_, lfs);
264
265 // Geometric definition of the cells in the mesh, based on the cell-to-node map and the domain partition.
266 Intrepid::FieldContainer<Real> &nodes = *meshMgr_->getNodes();
267 Intrepid::FieldContainer<GO> &ctn = *meshMgr_->getCellToNodeMap();
268 for (GO i=0; i<numCells_; ++i) {
269 for (int j=0; j<numNodesPerCell_; ++j) {
270 for (int k=0; k<spaceDim_; ++k) {
271 (*cellNodes_)(i, j, k) = nodes(ctn(myCellIds_[i],j), k);
272 }
273 }
274 }
275
276 /****************************************************/
277 /****************************************************/
278
279
280 /****************************************************************/
281 /*** Assemble cellwise contributions to vectors and matrices. ***/
282 /****************************************************************/
283
284 cellCub->getCubature(*cubPoints_, *cubWeights_); // retrieve cubature points and weights
285 (*basisPtrs_[0]).getValues(*gradReference_, *cubPoints_, Intrepid::OPERATOR_GRAD); // evaluate grad operator at cubature points
286 (*basisPtrs_[0]).getValues(*valReference_, *cubPoints_, Intrepid::OPERATOR_VALUE); // evaluate value operator at cubature points
287
288 Intrepid::CellTools<Real>::setJacobian(*cellJac_, *cubPoints_, *cellNodes_, cellType_); // compute cell Jacobians
289 Intrepid::CellTools<Real>::setJacobianInv(*cellJacInv_, *cellJac_); // compute inverses of cell Jacobians
290 Intrepid::CellTools<Real>::setJacobianDet(*cellJacDet_, *cellJac_); // compute determinants of cell Jacobians
291
292 Intrepid::FunctionSpaceTools::computeCellMeasure<Real>(*cellWeightedMeasure_, // compute weighted cell measure
293 *cellJacDet_,
294 *cubWeights_);
295
296 Intrepid::FunctionSpaceTools::HGRADtransformGRAD<Real>(*gradPhysical_, // transform reference gradients into physical space
297 *cellJacInv_,
298 *gradReference_);
299 Intrepid::FunctionSpaceTools::multiplyMeasure<Real>(*gradPhysicalWeighted_, // multiply with weighted measure
300 *cellWeightedMeasure_,
301 *gradPhysical_);
302 Intrepid::FunctionSpaceTools::integrate<Real>(*gradgradMats_, // compute local grad.grad (stiffness) matrices
303 *gradPhysical_,
304 *gradPhysicalWeighted_,
305 Intrepid::COMP_CPP);
306
307 Intrepid::FunctionSpaceTools::HGRADtransformVALUE<Real>(*valPhysical_, // transform reference values into physical space
308 *valReference_);
309 Intrepid::FunctionSpaceTools::multiplyMeasure<Real>(*valPhysicalWeighted_, // multiply with weighted measure
310 *cellWeightedMeasure_,
311 *valPhysical_);
312 Intrepid::FunctionSpaceTools::integrate<Real>(*valvalMats_, // compute local val.val (mass) matrices
313 *valPhysical_,
314 *valPhysicalWeighted_,
315 Intrepid::COMP_CPP);
316
317 Intrepid::CellTools<Real>::mapToPhysicalFrame(*cubPointsPhysical_, // map reference cubature points to physical space
318 *cubPoints_,
319 *cellNodes_,
320 cellType_);
321 for (GO i=0; i<numCells_; ++i) { // evaluate functions at these points
322 for (int j=0; j<numCubPoints_; ++j) {
323 (*dataF_)(i, j) = funcRHS((*cubPointsPhysical_)(i, j, 0), (*cubPointsPhysical_)(i, j, 1));
324 }
325 }
326 Intrepid::FunctionSpaceTools::integrate<Real>(*datavalVecF_, // compute local data.val vectors for RHS F
327 *dataF_,
328 *valPhysicalWeighted_,
329 Intrepid::COMP_CPP);
330
331 coord_iface->getDofCoords(*dofPoints_); // get coordinates of DOFs in reference cell
332 Intrepid::CellTools<Real>::mapToPhysicalFrame(*dofPointsPhysical_, // map reference DOF locations to physical space
333 *dofPoints_,
334 *cellNodes_,
335 cellType_);
336 for (GO i=0; i<numCells_; ++i) { // evaluate functions at these points
337 for (int j=0; j<lfs; ++j) {
338 (*dataUd_)(i, j) = funcTarget((*dofPointsPhysical_)(i, j, 0), (*dofPointsPhysical_)(i, j, 1));
339 }
340 }
341
342
343 /****************************************************************/
344 /****************************************************************/
345
346 /****************************************/
347 /*** Assemble global data structures. ***/
348 /****************************************/
349
350 // Assemble graph.
351 Teuchos::ArrayRCP<const GO> cellDofsArrayRCP = cellDofs.getData();
352 Teuchos::Array<size_t> graphEntriesPerRow(myUniqueMap_->getLocalNumElements());
353 for (GO i=0; i<numCells_; ++i) {
354 for (int j=0; j<numLocalDofs; ++j) {
355 GO gid = cellDofs(myCellIds_[i],j);
356 if(myUniqueMap_->isNodeGlobalElement(gid))
357 graphEntriesPerRow[myUniqueMap_->getLocalElement(gid)] += numLocalDofs;
358 }
359 }
360 matGraph_ = ROL::makePtr<Tpetra::CrsGraph<>>(myUniqueMap_, graphEntriesPerRow());
361 for (GO i=0; i<numCells_; ++i) {
362 for (int j=0; j<numLocalDofs; ++j) {
363 matGraph_->insertGlobalIndices(cellDofs(myCellIds_[i],j), cellDofsArrayRCP(myCellIds_[i]*numLocalDofs, numLocalDofs));
364 }
365 }
366 matGraph_->fillComplete();
367
368 // Assemble matrices.
369 // Stiffness matrix A.
370 matA_ = ROL::makePtr<Tpetra::CrsMatrix<>>(matGraph_);
371 int numLocalMatEntries = numLocalDofs*numLocalDofs;
372 Teuchos::ArrayRCP<const Real> gradgradArrayRCP = gradgradMats_->getData();
373 for (GO i=0; i<numCells_; ++i) {
374 for (int j=0; j<numLocalDofs; ++j) {
375 matA_->sumIntoGlobalValues(cellDofs(myCellIds_[i],j),
376 cellDofsArrayRCP(myCellIds_[i]*numLocalDofs, numLocalDofs),
377 gradgradArrayRCP(i*numLocalMatEntries+j*numLocalDofs, numLocalDofs));
378 }
379 }
380 matA_->fillComplete();
381
382 // Mass matrix M.
383 matM_ = ROL::makePtr<Tpetra::CrsMatrix<>>(matGraph_);
384 Teuchos::ArrayRCP<const Real> valvalArrayRCP = valvalMats_->getData();
385 for (GO i=0; i<numCells_; ++i) {
386 for (int j=0; j<numLocalDofs; ++j) {
387 matM_->sumIntoGlobalValues(cellDofs(myCellIds_[i],j),
388 cellDofsArrayRCP(myCellIds_[i]*numLocalDofs, numLocalDofs),
389 valvalArrayRCP(i*numLocalMatEntries+j*numLocalDofs, numLocalDofs));
390 }
391 }
392 matM_->fillComplete();
393
394 // Assemble vectors.
395 // vecF_ requires assembly using vecF_overlap_ and redistribution
396 vecF_ = ROL::makePtr<Tpetra::MultiVector<>>(matA_->getRangeMap(), 1, true);
397 vecF_overlap_ = ROL::makePtr<Tpetra::MultiVector<>>(myOverlapMap_, 1, true);
398 for (GO i=0; i<numCells_; ++i) { // assembly on the overlap map
399 for (int j=0; j<numLocalDofs; ++j) {
400 vecF_overlap_->sumIntoGlobalValue(cellDofs(myCellIds_[i],j),
401 0,
402 (*datavalVecF_)[i*numLocalDofs+j]);
403 }
404 }
405 Tpetra::Export<> exporter(vecF_overlap_->getMap(), vecF_->getMap()); // redistribution:
406 vecF_->doExport(*vecF_overlap_, exporter, Tpetra::ADD); // from the overlap map to the unique map
407 // vecUd_ does not require assembly
408 vecUd_ = ROL::makePtr<Tpetra::MultiVector<>>(matA_->getDomainMap(), 1, true);
409 for (GO i=0; i<numCells_; ++i) {
410 for (int j=0; j<numLocalDofs; ++j) {
411 if (vecUd_->getMap()->isNodeGlobalElement(cellDofs(myCellIds_[i],j))) {
412 vecUd_->replaceGlobalValue(cellDofs(myCellIds_[i],j),
413 0,
414 (*dataUd_)[i*numLocalDofs+j]);
415 }
416 }
417 }
418
419 // Apply Dirichlet conditions.
420 // Stiffness matrix with Dirichlet conditions:
421 // AD = [ A11 A12 ] where A = [ A11 A12 ]
422 // [ 0 I ] [ A21 A22 ]
423 // Mass matrix with Dirichlet conditions:
424 // MD = [ M11 M12 ] where M = [ M11 M12 ]
425 // [ 0 0 ] [ M21 M22 ]
426 // Vector F with Dirichlet conditions G:
427 // FD = [ F1 ] where F = [ F1 ]
428 // [ G ] [ F2 ]
429 matA_dirichlet_ = ROL::makePtr<Tpetra::CrsMatrix<>>(*matA_, Teuchos::DataAccess::Copy);
430 matM_dirichlet_ = ROL::makePtr<Tpetra::CrsMatrix<>>(*matM_, Teuchos::DataAccess::Copy);
431 vecF_dirichlet_ = ROL::makePtr<Tpetra::MultiVector<>>(matA_->getRangeMap(), 1, true);
432 Tpetra::deep_copy(*vecF_dirichlet_, *vecF_);
433 ROL::Ptr<std::vector<std::vector<Intrepid::FieldContainer<GO> > > > dirichletSideSets = meshMgr_->getSideSets();
434 std::vector<std::vector<Intrepid::FieldContainer<GO> > > &dss = *dirichletSideSets;
435 Teuchos::Array<GO> mySortedCellIds_(myCellIds_);
436 std::sort(mySortedCellIds_.begin(), mySortedCellIds_.end());
437 mySortedCellIds_.erase( std::unique(mySortedCellIds_.begin(), mySortedCellIds_.end()), mySortedCellIds_.end() );
438 std::vector<Teuchos::Array<GO> > myDirichletCellIds_(dss[0].size());
439 for (int i=0; i<static_cast<int>(dss[0].size()); ++i) {
440 for (int j=0; j<dss[0][i].dimension(0); ++j) {
441 if (std::binary_search(mySortedCellIds_.begin(), mySortedCellIds_.end(), dss[0][i](j))) {
442 myDirichletCellIds_[i].push_back(dss[0][i](j));
443 }
444 }
445 }
446 Intrepid::FieldContainer<GO> &cte = *(meshMgr_->getCellToEdgeMap());
447 Intrepid::FieldContainer<GO> &nodeDofs = *(dofMgr_->getNodeDofs());
448 Intrepid::FieldContainer<GO> &edgeDofs = *(dofMgr_->getEdgeDofs());
449 std::vector<std::vector<int> > dofTags = (basisPtrs_[0])->getAllDofTags();
450 int numDofsPerNode = 0;
451 int numDofsPerEdge = 0;
452 for (int j=0; j<(basisPtrs_[0])->getCardinality(); ++j) {
453 if (dofTags[j][0] == 0) {
454 numDofsPerNode = dofTags[j][3];
455 }
456 if (dofTags[j][0] == 1) {
457 numDofsPerEdge = dofTags[j][3];
458 }
459 }
460 Teuchos::Array<GO> myDirichletDofs_;
461 for (int i=0; i<static_cast<int>(myDirichletCellIds_.size()); ++i) {
462 for (int j=0; j<myDirichletCellIds_[i].size(); ++j) {
463 for (int k=0; k<numDofsPerNode; ++k) {
464 const CellTopologyData * ctd = cellType_.getCellTopologyData();
465 Teuchos::ArrayView<unsigned> locNodes(const_cast<unsigned *>(ctd->subcell[spaceDim_-1][i].node), cellType_.getVertexCount(spaceDim_-1, i));
466 for (int l=0; l<static_cast<int>(cellType_.getVertexCount(spaceDim_-1, i)); ++l) {
467 myDirichletDofs_.push_back(nodeDofs(ctn(myDirichletCellIds_[i][j], locNodes[l]), k));
468 }
469 }
470 for (int k=0; k<numDofsPerEdge; ++k) {
471 myDirichletDofs_.push_back(edgeDofs(cte(myDirichletCellIds_[i][j], i), k));
472 }
473 }
474 }
475 std::sort(myDirichletDofs_.begin(), myDirichletDofs_.end());
476 myDirichletDofs_.erase( std::unique(myDirichletDofs_.begin(), myDirichletDofs_.end()), myDirichletDofs_.end() );
477 matA_dirichlet_->resumeFill();
478 matM_dirichlet_->resumeFill();
479 for (int i=0; i<myDirichletDofs_.size(); ++i) {
480 if (myUniqueMap_->isNodeGlobalElement(myDirichletDofs_[i])) {
481 size_t numRowEntries = matA_dirichlet_->getNumEntriesInGlobalRow(myDirichletDofs_[i]);
482 Teuchos::Array<GO> indices(numRowEntries, 0);
483 Teuchos::Array<Real> values(numRowEntries, 0);
484 Teuchos::Array<Real> canonicalValues(numRowEntries, 0);
485 Teuchos::Array<Real> zeroValues(numRowEntries, 0);
486 matA_dirichlet_->getGlobalRowCopy(myDirichletDofs_[i], indices, values, numRowEntries);
487 matM_dirichlet_->getGlobalRowCopy(myDirichletDofs_[i], indices, values, numRowEntries);
488 for (int j=0; j<indices.size(); ++j) {
489 if (myDirichletDofs_[i] == indices[j]) {
490 canonicalValues[j] = 1.0;
491 }
492 }
493 matA_dirichlet_->replaceGlobalValues(myDirichletDofs_[i], indices, canonicalValues);
494 matM_dirichlet_->replaceGlobalValues(myDirichletDofs_[i], indices, zeroValues);
495 vecF_dirichlet_->replaceGlobalValue(myDirichletDofs_[i], 0, 0);
496 }
497 }
498 matA_dirichlet_->fillComplete();
499 matM_dirichlet_->fillComplete();
500
501 // Create matrix transposes.
502 Tpetra::RowMatrixTransposer<> transposerA(matA_dirichlet_);
503 Tpetra::RowMatrixTransposer<> transposerM(matM_dirichlet_);
504 matA_dirichlet_trans_ = transposerA.createTranspose();
505 matM_dirichlet_trans_ = transposerM.createTranspose();
506
507
508 /*********************************/
509 /*** Construct solver objects. ***/
510 /*********************************/
511
512 // Construct solver using Amesos2 factory.
513 try{
514 solverA_ = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("KLU2", matA_dirichlet_);
515 } catch (std::invalid_argument& e) {
516 std::cout << e.what() << std::endl;
517 }
518 try{
519 solverA_trans_ = Amesos2::create< Tpetra::CrsMatrix<>,Tpetra::MultiVector<> >("KLU2", matA_dirichlet_trans_);
520 } catch (std::invalid_argument& e) {
521 std::cout << e.what() << std::endl;
522 }
523 solverA_->numericFactorization();
524 solverA_trans_->numericFactorization();
525
526
527 /****************************************/
528 /****************************************/
529
530 //outputTpetraData();
531
532 }
533
534
535 ROL::Ptr<Tpetra::CrsMatrix<> > getMatA(const bool &transpose = false) const {
536 if (transpose) {
537 return matA_dirichlet_trans_;
538 }
539 else {
540 return matA_dirichlet_;
541 }
542 }
543
544
545 ROL::Ptr<Tpetra::CrsMatrix<> > getMatB(const bool &transpose = false) const {
546 if (transpose) {
547 return matM_dirichlet_trans_;
548 }
549 else {
550 return matM_dirichlet_;
551 }
552 }
553
554
555 ROL::Ptr<Tpetra::CrsMatrix<> > getMatM() const {
556 return matM_;
557 }
558
559
560 ROL::Ptr<Tpetra::CrsMatrix<> > getMatR() const {
561 return matM_;
562 }
563
564
565 ROL::Ptr<Tpetra::MultiVector<> > getVecUd() const {
566 return vecUd_;
567 }
568
569
570 ROL::Ptr<Tpetra::MultiVector<> > getVecF() const {
571 return vecF_dirichlet_;
572 }
573
574
575 ROL::Ptr<Amesos2::Solver< Tpetra::CrsMatrix<>, Tpetra::MultiVector<> > > getSolver(const bool &transpose = false) const {
576 if (transpose) {
577 return solverA_trans_;
578 }
579 else {
580 return solverA_;
581 }
582 }
583
584
585 Real funcRHS(const Real &x1, const Real &x2) const {
586 return 2.0*M_PI*M_PI*std::sin(M_PI*x1)*std::sin(M_PI*x2) + (1.0/(alpha_*128.0*M_PI*M_PI))*std::sin(8.0*M_PI*x1)*std::sin(8.0*M_PI*x2);
587 }
588
589
590 Real funcTarget(const Real &x1, const Real &x2) const {
591 return std::sin(M_PI*x1)*std::sin(M_PI*x2) - std::sin(8.0*M_PI*x1)*std::sin(8.0*M_PI*x2);
592 }
593
594
595 Real funcStateSolution(const Real &x1, const Real &x2) const {
596 return std::sin(M_PI*x1)*std::sin(M_PI*x2);
597 }
598
599
600 Real computeStateError(const ROL::Ptr<const Tpetra::MultiVector<> > &soln) const {
601
602 ROL::Ptr<Tpetra::MultiVector<> > soln_overlap =
603 ROL::makePtr<Tpetra::MultiVector<>>(vecF_overlap_->getMap(), 1, true);
604 Tpetra::Import<> importer(vecUd_->getMap(), soln_overlap->getMap()); // redistribution:
605 soln_overlap->doImport(*soln, importer, Tpetra::REPLACE); // from the unique map to the overlap map
606
607 Intrepid::DefaultCubatureFactory<Real> cubFactory; // create cubature factory
608 int cubDeg = 6; // set cubature degree, e.g., 6
609 ROL::Ptr<Intrepid::Cubature<Real> > cellCub = cubFactory.create(cellType_, cubDeg); // create cubature for error computation
610 int numCubPts = cellCub->getNumPoints(); // retrieve number of cubature points
611 int lfs = dofMgr_->getLocalFieldSize(0);
612 Intrepid::FieldContainer<Real> cubPts(numCubPts, spaceDim_);
613 Intrepid::FieldContainer<Real> cubWts(numCubPts);
614 Intrepid::FieldContainer<Real> cubPtsPhys(numCells_, numCubPts, spaceDim_);
615 Intrepid::FieldContainer<Real> jac(numCells_, numCubPts, spaceDim_, spaceDim_);
616 Intrepid::FieldContainer<Real> jacDet(numCells_, numCubPts);
617 Intrepid::FieldContainer<Real> valRef(lfs, numCubPts);
618 Intrepid::FieldContainer<Real> valPhys(numCells_, lfs, numCubPts);
619 Intrepid::FieldContainer<Real> wtMeas(numCells_, numCubPts);
620 Intrepid::FieldContainer<Real> inCoeffs(numCells_, lfs);
621 Intrepid::FieldContainer<Real> funcVals(numCells_, numCubPts);
622 Intrepid::FieldContainer<Real> funcValsWt(numCells_, numCubPts);
623 Intrepid::FieldContainer<Real> normSquaredError(numCells_);
624
625 cellCub->getCubature(cubPts, cubWts); // retrieve cubature points and weights
626 (*basisPtrs_[0]).getValues(valRef, cubPts, Intrepid::OPERATOR_VALUE); // evaluate value operator at cubature points
627
628 Intrepid::CellTools<Real>::setJacobian(jac, cubPts, *cellNodes_, cellType_); // compute cell Jacobians
629 Intrepid::CellTools<Real>::setJacobianDet(jacDet, jac); // compute determinants of cell Jacobians
630
631 Intrepid::FunctionSpaceTools::HGRADtransformVALUE<Real>(valPhys, // transform reference values into physical space
632 valRef);
633
634 Intrepid::FunctionSpaceTools::computeCellMeasure<Real>(wtMeas, // compute weighted cell measure
635 jacDet,
636 cubWts);
637
638 Intrepid::CellTools<Real>::mapToPhysicalFrame(cubPtsPhys, // map reference cubature points to physical space
639 cubPts,
640 *cellNodes_,
641 cellType_);
642
643 Intrepid::FieldContainer<GO> &cellDofs = *(dofMgr_->getCellDofs());
644 Teuchos::ArrayRCP<const Real> soln_data = soln_overlap->get1dView(); // populate inCoeffs
645 for (int i=0; i<numCells_; ++i) {
646 for (int j=0; j<lfs; ++j) {
647 inCoeffs(i, j) = soln_data[soln_overlap->getMap()->getLocalElement(cellDofs(myCellIds_[i],j))];
648 }
649 }
650
651 Intrepid::FunctionSpaceTools::evaluate<Real>(funcVals, inCoeffs, valPhys);
652
653 for (int i=0; i<numCells_; ++i) { // compute error
654 for (int j=0; j<numCubPts; ++j) {
655 funcVals(i, j) -= funcStateSolution(cubPtsPhys(i, j, 0), cubPtsPhys(i, j, 1));
656 }
657 }
658
659 Intrepid::FunctionSpaceTools::scalarMultiplyDataData<Real>(funcValsWt, // multiply with weighted measure
660 wtMeas,
661 funcVals);
662
663 Intrepid::FunctionSpaceTools::integrate<Real>(normSquaredError, // compute norm squared of local error
664 funcVals,
665 funcValsWt,
666 Intrepid::COMP_CPP);
667
668 Real localErrorSum(0);
669 Real globalErrorSum(0);
670 for (int i=0; i<numCells_; ++i) {
671 localErrorSum += normSquaredError(i);
672 }
673 ROL::Ptr<const Teuchos::Comm<int> > comm = soln_overlap->getMap()->getComm();
674 Teuchos::reduceAll<int, Real>(*comm, Teuchos::REDUCE_SUM, 1, &localErrorSum, &globalErrorSum);
675
676 return globalErrorSum;
677 }
678
679
680 void printMeshData(std::ostream &outStream) const {
681 ROL::Ptr<Intrepid::FieldContainer<Real> > nodesPtr = meshMgr_->getNodes();
682 ROL::Ptr<Intrepid::FieldContainer<GO> > cellToNodeMapPtr = meshMgr_->getCellToNodeMap();
683 Intrepid::FieldContainer<Real> &nodes = *nodesPtr;
684 Intrepid::FieldContainer<GO> &cellToNodeMap = *cellToNodeMapPtr;
685 outStream << "Number of nodes = " << meshMgr_->getNumNodes() << std::endl;
686 outStream << "Number of cells = " << meshMgr_->getNumCells() << std::endl;
687 outStream << "Number of edges = " << meshMgr_->getNumEdges() << std::endl;
688 // Print mesh to file.
689 if ((myRank_ == 0)) {
690 std::ofstream meshfile;
691 meshfile.open("cell_to_node_quad.txt");
692 for (int i=0; i<cellToNodeMap.dimension(0); ++i) {
693 for (int j=0; j<cellToNodeMap.dimension(1); ++j) {
694 meshfile << cellToNodeMap(i,j) << " ";
695 }
696 meshfile << std::endl;
697 }
698 meshfile.close();
699 meshfile.open("cell_to_node_tri.txt");
700 for (int i=0; i<cellToNodeMap.dimension(0); ++i) {
701 for (int j=0; j<3; ++j) {
702 meshfile << cellToNodeMap(i,j) << " ";
703 }
704 meshfile << std::endl;
705 for (int j=2; j<5; ++j) {
706 meshfile << cellToNodeMap(i,j%4) << " ";
707 }
708 meshfile << std::endl;
709 }
710 meshfile.close();
711 meshfile.open("nodes.txt");
712 meshfile.precision(16);
713 for (int i=0; i<nodes.dimension(0); ++i) {
714 for (int j=0; j<nodes.dimension(1); ++j) {
715 meshfile << std::scientific << nodes(i,j) << " ";
716 }
717 meshfile << std::endl;
718 }
719 meshfile.close();
720 /* This somewhat clunky output is for gnuplot.
721 meshfile.open("mesh.txt");
722 for (int i=0; i<cellToNodeMap.dimension(0); ++i) {
723 meshfile << nodes(cellToNodeMap(i,0), 0) << " " << nodes(cellToNodeMap(i,0), 1) << std::endl;
724 meshfile << nodes(cellToNodeMap(i,1), 0) << " " << nodes(cellToNodeMap(i,1), 1) << std::endl;
725 meshfile << nodes(cellToNodeMap(i,2), 0) << " " << nodes(cellToNodeMap(i,2), 1) << std::endl;
726 meshfile << nodes(cellToNodeMap(i,3), 0) << " " << nodes(cellToNodeMap(i,3), 1) << std::endl;
727 meshfile << nodes(cellToNodeMap(i,0), 0) << " " << nodes(cellToNodeMap(i,0), 1) << std::endl;
728 meshfile << nodes(cellToNodeMap(i,1), 0) << " " << nodes(cellToNodeMap(i,1), 1) << std::endl;
729 meshfile << nodes(cellToNodeMap(i,2), 0) << " " << nodes(cellToNodeMap(i,2), 1) << std::endl;
730 }
731 meshfile.close();
732 */
733 }
734 }
735
736
737 void outputTpetraData() const {
738 Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<> > matWriter;
739 matWriter.writeSparseFile("stiffness_mat", matA_);
740 matWriter.writeSparseFile("dirichlet_mat", matA_dirichlet_);
741 matWriter.writeSparseFile("mass_mat", matM_);
742 matWriter.writeDenseFile("Ud_vec", vecUd_);
743 }
744
745
746 void outputTpetraVector(const ROL::Ptr<const Tpetra::MultiVector<> > &vec,
747 const std::string &filename) const {
748 Tpetra::MatrixMarket::Writer< Tpetra::CrsMatrix<> > vecWriter;
749 vecWriter.writeDenseFile(filename, vec);
750 }
751
752 }; // class PoissonData
753
754 #endif