Thyra Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
createTridiagEpetraLinearOp.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Thyra: Interfaces and Support for Abstract Numerical Algorithms
4 //
5 // Copyright 2004 NTESS and the Thyra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Thyra_EpetraLinearOp.hpp"
12 #include "Epetra_Map.h"
13 #include "Epetra_CrsMatrix.h"
14 #ifdef HAVE_MPI
15 # include "Epetra_MpiComm.h"
16 #else
17 # include "Epetra_SerialComm.h"
18 #endif
19 
22  const int globalDim
23 #ifdef HAVE_MPI
24  ,MPI_Comm mpiComm
25 #endif
26  ,const double diagScale
27  ,const bool verbose
28  ,std::ostream &out
29  )
30 {
31  using Teuchos::RCP;
32  using Teuchos::rcp;
33 
34  //
35  // (A) Create Epetra_Map
36  //
37 
38 #ifdef HAVE_MPI
39  if(verbose) out << "\nCreating Epetra_MpiComm ...\n";
40  Epetra_MpiComm epetra_comm(mpiComm); // Note, Epetra_MpiComm is just a handle class to Epetra_MpiCommData object!
41 #else
42  if(verbose) out << "\nCreating Epetra_SerialComm ...\n";
43  Epetra_SerialComm epetra_comm; // Note, Epetra_SerialComm is just a handle class to Epetra_SerialCommData object!
44 #endif
45  // Create the Epetra_Map object giving it the handle to the Epetra_Comm object
46  const Epetra_Map epetra_map(globalDim,0,epetra_comm); // Note, Epetra_Map is just a handle class to an Epetra_BlockMapData object!
47  // Note that the above Epetra_Map object "copies" the Epetra_Comm object in some
48  // since so that memory mangaement is performed safely.
49 
50  //
51  // (B) Create the tridiagonal Epetra object
52  //
53  // [ 2 -1 ]
54  // [ -1 2 -1 ]
55  // A = [ . . . ]
56  // [ -1 2 -1 ]
57  // [ -1 2 ]
58  //
59 
60  // (B.1) Allocate the Epetra_CrsMatrix object.
61  RCP<Epetra_CrsMatrix> A_epetra = rcp(new Epetra_CrsMatrix(::Copy,epetra_map,3));
62  // Note that Epetra_CrsMatrix is *not* a handle object so have to use RCP above.
63  // Also note that the Epetra_Map object is copied in some sence by the Epetra_CrsMatrix
64  // object so the memory managment is handled safely.
65 
66  // (B.2) Get the indexes of the rows on this processor
67  const int numMyElements = epetra_map.NumMyElements();
68  std::vector<int> myGlobalElements(numMyElements);
69  epetra_map.MyGlobalElements(&myGlobalElements[0]);
70 
71  // (B.3) Fill the local matrix entries one row at a time.
72  // Note, we set up Epetra_Map above to use zero-based indexing and that is what we must use below:
73  const double offDiag = -1.0, diag = 2.0*diagScale;
74  int numEntries; double values[3]; int indexes[3];
75  for( int k = 0; k < numMyElements; ++k ) {
76  const int rowIndex = myGlobalElements[k];
77  if( rowIndex == 0 ) { // First row
78  numEntries = 2;
79  values[0] = diag; values[1] = offDiag;
80  indexes[0] = 0; indexes[1] = 1;
81  }
82  else if( rowIndex == globalDim - 1 ) { // Last row
83  numEntries = 2;
84  values[0] = offDiag; values[1] = diag;
85  indexes[0] = globalDim-2; indexes[1] = globalDim-1;
86  }
87  else { // Middle rows
88  numEntries = 3;
89  values[0] = offDiag; values[1] = diag; values[2] = offDiag;
90  indexes[0] = rowIndex-1; indexes[1] = rowIndex; indexes[2] = rowIndex+1;
91  }
92  TEUCHOS_TEST_FOR_EXCEPT( 0!=A_epetra->InsertGlobalValues(rowIndex,numEntries,values,indexes) );
93  }
94 
95  // (B.4) Finish the construction of the Epetra_CrsMatrix
96  TEUCHOS_TEST_FOR_EXCEPT( 0!=A_epetra->FillComplete() );
97 
98  // Return the Epetra_Operator object
99  return A_epetra;
100 
101  // Note that when this function returns the returned RCP-wrapped
102  // Epetra_Operator object will own all of the Epetra objects that went into
103  // its construction and these objects will stay around until all of the
104  // RCP objects to the allocated Epetra_Operator object are removed
105  // and destruction occurs!
106 
107 } // end createTridiagLinearOp()
Teuchos::RCP< Epetra_Operator > createTridiagEpetraLinearOp(const int globalDim, const double diagScale, const bool verbose, std::ostream &out)
This function generates a tridiagonal linear operator using Epetra.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)