EpetraExt  Development
EpetraExt_BlockVector.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 
42 #include "EpetraExt_BlockVector.h"
43 #include "EpetraExt_BlockUtility.h"
44 #include "Epetra_Map.h"
45 #include "Epetra_Comm.h"
46 
47 namespace EpetraExt {
48 
49 //=============================================================================
50 // EpetraExt::BlockVector Constructor
52  const Epetra_BlockMap & BaseMap,
53  const Epetra_BlockMap & GlobalMap)
54  : Epetra_Vector( GlobalMap ),
55  BaseMap_( BaseMap ),
56  Offset_( BlockUtility::CalculateOffset( BaseMap ) )
57 {
58 }
59 
60 //=============================================================================
61 // EpetraExt::BlockVector Constructor
63  Epetra_DataAccess CV,
64  const Epetra_BlockMap & BaseMap,
65  const Epetra_Vector & BlockVec)
66  : Epetra_Vector( CV, BlockVec, 0 ),
67  BaseMap_( BaseMap ),
68  Offset_( BlockUtility::CalculateOffset( BaseMap ) )
69 {
70 }
71 
72 //==========================================================================
73 // Copy Constructor
75  : Epetra_Vector( dynamic_cast<const Epetra_Vector &>(Source) ),
76  BaseMap_( Source.BaseMap_ ),
77  Offset_( Source.Offset_ )
78 {
79 }
80 
81 //=========================================================================
83 {
84 }
85 
86 //=========================================================================
87 int BlockVector::ExtractBlockValues(Epetra_Vector & BaseVector, int GlobalBlockRow) const
88 {
89  int IndexOffset = GlobalBlockRow * Offset_;
90  int localIndex=0;
91 
92  // For each entry in the base vector, translate its global ID
93  // by the IndexOffset and extract the value from this blockVector
94  for (int i=0; i<BaseMap_.NumMyElements(); i++) {
95  localIndex = this->Map().LID((IndexOffset + BaseMap_.GID(i)));
96  if (localIndex==-1) {
97  cout << "Error in BlockVector::GetBlock: " << i << " "
98  << IndexOffset << " " << BaseMap_.GID(i) << endl;
99  return -1;
100  }
101  BaseVector[i] = Values_[localIndex];
102  }
103 
104  return 0;
105 }
106 
107 //=========================================================================
108 int BlockVector::LoadBlockValues(const Epetra_Vector & BaseVector, int GlobalBlockRow)
109 {
110  int IndexOffset = GlobalBlockRow * Offset_;
111  int localIndex=0;
112 
113  // For each entry in the base vector, translate its global ID
114  // by the IndexOffset and load into this blockVector
115  for (int i=0; i<BaseMap_.NumMyElements(); i++) {
116  localIndex = this->Map().LID((IndexOffset + BaseMap_.GID(i)));
117  if (localIndex==-1) {
118  cout << "Error in BlockVector::GetBlock: " << i << " "
119  << IndexOffset << " " << BaseMap_.GID(i) << endl;
120  return -1;
121  }
122  (*this)[localIndex] = BaseVector[i];
123  }
124 
125  return 0;
126 }
127 //=========================================================================
128 int BlockVector::BlockSumIntoGlobalValues(int NumIndices, double* Values,
129  int* Indices, int GlobalBlockRow)
130 {
131  int IndexOffset = GlobalBlockRow * Offset_;
132  int localIndex=0;
133 
134  // For each entry in the base vector, translate its global ID
135  // by the IndexOffset and load into this blockVector
136  for (int i=0; i<NumIndices; i++) {
137  localIndex = this->Map().LID((IndexOffset + Indices[i]));
138  if (localIndex==-1) {
139  cout << "Error in BlockVector::BlockSumIntoGlobalValues: " << i
140  << " " << IndexOffset << " " << Indices[i] << endl;
141  return -1;
142  }
143  (*this)[localIndex] += Values[i];
144  }
145 
146  return 0;
147 }
148 //=========================================================================
149 int BlockVector::BlockReplaceGlobalValues(int NumIndices, double* Values,
150  int* Indices, int GlobalBlockRow)
151 {
152  int IndexOffset = GlobalBlockRow * Offset_;
153  int localIndex=0;
154 
155  // For each entry in the base vector, translate its global ID
156  // by the IndexOffset and load into this blockVector
157  for (int i=0; i<NumIndices; i++) {
158  localIndex = this->Map().LID((IndexOffset + Indices[i]));
159  if (localIndex==-1) {
160  cout << "Error in BlockVector::BlockReplaceGlobalValues: " << i
161  << " " << IndexOffset << " " << Indices[i] << endl;
162  return -1;
163  }
164  (*this)[localIndex] = Values[i];
165  }
166 
167  return 0;
168 }
169 
170 //=========================================================================
171 Teuchos::RCP<const Epetra_Vector>
172 BlockVector::GetBlock(int GlobalBlockRow) const
173 {
174  int offset = GlobalBlockRow * BaseMap_.NumMyElements();
175  return Teuchos::rcp(new Epetra_Vector(View, BaseMap_, Values_+offset));
176 }
177 
178 //=========================================================================
179 Teuchos::RCP<Epetra_Vector>
180 BlockVector::GetBlock(int GlobalBlockRow)
181 {
182  int offset = GlobalBlockRow * BaseMap_.NumMyElements();
183  return Teuchos::rcp(new Epetra_Vector(View, BaseMap_, Values_+offset));
184 }
185 
186 //=========================================================================
187 const Epetra_BlockMap&
189 {
190  return BaseMap_;
191 }
192 
193 
194 } //namespace EpetraExt
Teuchos::RCP< const Epetra_Vector > GetBlock(int BlockRow) const
Return Epetra_Vector for given block row.
int BlockSumIntoGlobalValues(int NumIndices, double *Values, int *Indices, int BlockRow)
Load entries into BlockVector with base vector indices offset by BlockRow.
int BlockReplaceGlobalValues(int NumIndices, double *Values, int *Indices, int BlockRow)
Load entries into BlockVector with base vector indices offset by BlockRow.
virtual ~BlockVector()
Destructor.
int ExtractBlockValues(Epetra_Vector &BaseVec, int BlockRow) const
Extract a single block from a Block Vector: block row is global, not a stencil value.
EpetraExt::BlockCrsMatrix: A class for constructing a distributed block matrix.
int LoadBlockValues(const Epetra_Vector &BaseVec, int BlockRow)
Load a single block into a Block Vector: block row is global, not a stencil value.
BlockVector(const Epetra_BlockMap &BaseMap, const Epetra_BlockMap &GlobalMap)
BlockVector constuctor with one block row per processor.
const Epetra_BlockMap & GetBaseMap() const
Return base map.