EpetraExt  Development
EpetraExt_SubCopy_CrsMatrix.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 
43 
44 #include <Epetra_CrsMatrix.h>
45 #include <Epetra_Map.h>
46 #include <Epetra_Import.h>
47 #include <Epetra_IntSerialDenseVector.h>
48 
49 #include <vector>
50 
51 namespace EpetraExt {
52 
55 {
56  if( newObj_ ) delete newObj_;
57 }
58 
62 {
63  origObj_ = &orig;
64 
65  //Error, must be local indices
66  assert( orig.Filled() );
67 
68  //test maps, new map must be subset of old
69  const Epetra_Map & oRowMap = orig.RowMap();
70  const Epetra_Map & oColMap = orig.ColMap();
71 
72  int oNumRows = oRowMap.NumMyElements();
73  (void) oNumRows; // Silence "unused variable" compiler warning.
74  int oNumCols = oColMap.NumMyElements();
75  int nNumRows = newRowMap_.NumMyElements();
76  int nNumDomain = newDomainMap_.NumMyElements();
77 
78  bool matched = true;
79 
80  // Make sure all rows in newRowMap are already on this processor
81  for( int i = 0; i < nNumRows; ++i )
82  matched = matched && ( oRowMap.MyGID(newRowMap_.GID(i)) );
83  if( !matched ) cerr << "EDT_CrsMatrix_SubCopy: Bad new_row_Map. GIDs of new row map must be GIDs of the original row map on the same processor.\n";
84 
85  // Make sure all GIDs in the new domain map are GIDs in the old domain map
86  if( !newRangeMap_.SameAs(newDomainMap_) ) {
87  Epetra_IntSerialDenseVector pidList(nNumDomain);
88  oColMap.RemoteIDList(newDomainMap_.NumMyElements(), newDomainMap_.MyGlobalElements(), pidList.Values(), 0);
89  for( int i = 0; i < nNumDomain; ++i )
90  matched = matched && ( pidList[i]>=0 );
91  }
92 
93  if( !matched ) cout << "EDT_CrsMatrix_SubCopy: Bad newDomainMap. One or more GIDs in new domain map are not part of original domain map.\n";
94  assert( matched );
95 
96 
97  // Next build new column map
98  Epetra_IntSerialDenseVector pidList(oNumCols);
99  Epetra_IntSerialDenseVector lidList(oNumCols);
100  Epetra_IntSerialDenseVector sizeList(oNumCols);
101  newDomainMap_.RemoteIDList(oColMap.NumMyElements(), oColMap.MyGlobalElements(), pidList.Values(), 0);
102  int numNewCols = 0;
103  Epetra_IntSerialDenseVector newColMapGidList(oNumCols);
104  int * origColGidList = oColMap.MyGlobalElements();
105  for( int i = 0; i < oNumCols; ++i )
106  if (pidList[i] >=0)
107  newColMapGidList[numNewCols++]= origColGidList[i];
108  newColMap_ = Epetra_Map(-1, numNewCols, newColMapGidList.Values(), 0, oColMap.Comm());
109 
110  importer_ = new Epetra_Import(newRowMap_, oRowMap);
111 
112  Epetra_CrsMatrix * newMatrix = new Epetra_CrsMatrix(Copy, newRowMap_, newColMap_, 0);
113 
114  newObj_ = newMatrix;
115 
116  newObj_->Import(*origObj_, *importer_, Add);
117 
118  newObj_->FillComplete();
119 
120  return *newObj_;
121 }
122 
123 //==============================================================================
125 {
126 
127  if (newObj_->Filled()) newObj_->PutScalar(0.0); // zero contents
128 
129  newObj_->Import(*origObj_, *importer_, Add);
130 
131  newObj_->FillComplete();
132 
133 
134  return (true);
135 }
136 
137 //==============================================================================
139 {
140  if (!newObj_->Filled()) return(false); // Must have fillCompleted
141 
142  origObj_->Export(*newObj_, *importer_, Add);
143 
144  origObj_->FillComplete();
145 
146  return (true);
147 }
148 
149 } // namespace EpetraExt
150 
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
bool rvs()
Reverse transfer of data from new object created in the operator() method call to the orig object inp...
bool fwd()
Forward transfer of data from orig object input in the operator() method call to the new object creat...
NewTypeRef operator()(OriginalTypeRef orig)
Transformation Operator.