EpetraExt Development
EpetraExt_Reindex_CrsMatrix.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2001) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <EpetraExt_Reindex_CrsMatrix.h>
00030 
00031 #include <vector>
00032 
00033 #include <Epetra_CrsMatrix.h>
00034 #include <Epetra_Map.h>
00035 #include <Epetra_IntVector.h>
00036 
00037 #include <Epetra_Export.h>
00038 #include <Epetra_Import.h>
00039 
00040 namespace EpetraExt {
00041 
00042 CrsMatrix_Reindex::
00043 ~CrsMatrix_Reindex()
00044 {
00045   if( newObj_ ) delete newObj_;
00046   if( NewColMap_ ) delete NewColMap_;
00047 }
00048 
00049 CrsMatrix_Reindex::NewTypeRef
00050 CrsMatrix_Reindex::
00051 operator()( OriginalTypeRef orig )
00052 {
00053   origObj_ = &orig;
00054 
00055   //test std::map, must have same number of local and global elements as original row std::map
00056   Epetra_Map & OldRowMap = const_cast<Epetra_Map&>(orig.RowMap());
00057   Epetra_Map & OldColMap = const_cast<Epetra_Map&>(orig.ColMap());
00058   int NumMyElements = OldRowMap.NumMyElements();
00059   assert( OldRowMap.NumMyElements() == NewRowMap_.NumMyElements() );
00060 
00061   //Construct new Column Map
00062   Epetra_IntVector Cols( OldRowMap );
00063   Epetra_IntVector NewCols( OldColMap );
00064   Epetra_Import Importer( OldColMap, OldRowMap );
00065 
00066   for( int i = 0; i < NumMyElements; ++i )
00067     Cols[i] = NewRowMap_.GID(i);
00068 
00069   NewCols.Import( Cols, Importer, Insert );
00070 
00071   std::vector<int*> NewColIndices(1);
00072   NewCols.ExtractView( &NewColIndices[0] );
00073 
00074   int NumMyColElements = OldColMap.NumMyElements();
00075   int NumGlobalColElements = OldColMap.NumGlobalElements();
00076 
00077   NewColMap_ = new Epetra_Map( NumGlobalColElements, NumMyColElements, NewColIndices[0], OldColMap.IndexBase(), OldColMap.Comm() );
00078 
00079   //intial construction of matrix 
00080   Epetra_CrsMatrix * NewMatrix = new Epetra_CrsMatrix( View, NewRowMap_, *NewColMap_, 0 );
00081 
00082   //insert views of row values
00083   int * myIndices;
00084   double * myValues;
00085   int indicesCnt;
00086   int numMyRows = NewMatrix->NumMyRows();
00087   for( int i = 0; i < numMyRows; ++i )
00088   {
00089     orig.ExtractMyRowView( i, indicesCnt, myValues, myIndices );
00090     NewMatrix->InsertMyValues( i, indicesCnt, myValues, myIndices );
00091   }
00092 
00093   NewMatrix->FillComplete();
00094 
00095   newObj_ = NewMatrix;
00096 
00097   return *NewMatrix;
00098 }
00099 
00100 } // namespace EpetraExt
00101 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines