00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "EpetraExt_BlockUtility.h"
00030 #include "Epetra_Map.h"
00031 #include "Epetra_Comm.h"
00032
00033 namespace EpetraExt {
00034
00035 using std::vector;
00036
00037
00038 Epetra_CrsGraph * BlockUtility::GenerateBlockGraph(
00039 const Epetra_CrsGraph & BaseGraph,
00040 const vector< vector<int> > & RowStencil,
00041 const vector<int> & RowIndices,
00042 const Epetra_Comm & GlobalComm )
00043 {
00044
00045 const Epetra_BlockMap & BaseMap = BaseGraph.RowMap();
00046 int BaseIndex = BaseMap.IndexBase();
00047 int Offset = BlockUtility::CalculateOffset(BaseMap);
00048
00049
00050 int NumBlockRows = RowIndices.size();
00051 int Size = BaseMap.NumMyElements();
00052 int TotalSize = NumBlockRows * Size;
00053 vector<int> GIDs(Size);
00054 BaseMap.MyGlobalElements( &GIDs[0] );
00055
00056 vector<int> GlobalGIDs( TotalSize );
00057 for( int i = 0; i < NumBlockRows; ++i )
00058 {
00059 for( int j = 0; j < Size; ++j )
00060 GlobalGIDs[i*Size+j] = GIDs[j] + RowIndices[i] * Offset;
00061 }
00062
00063 int GlobalSize;
00064 GlobalComm.SumAll( &TotalSize, &GlobalSize, 1 );
00065
00066 Epetra_Map GlobalMap( GlobalSize, TotalSize, &GlobalGIDs[0], BaseIndex, GlobalComm );
00067
00068 int MaxIndices = BaseGraph.MaxNumIndices();
00069 vector<int> Indices(MaxIndices);
00070 int NumIndices;
00071
00072 Epetra_CrsGraph * GlobalGraph = new Epetra_CrsGraph( Copy,
00073 dynamic_cast<Epetra_BlockMap&>(GlobalMap),
00074 0 );
00075
00076 for( int i = 0; i < NumBlockRows; ++i )
00077 {
00078 int StencilSize = RowStencil[i].size();
00079 for( int j = 0; j < Size; ++j )
00080 {
00081 int BaseRow = BaseMap.GID(j);
00082 int GlobalRow = GlobalMap.GID(j+i*Size);
00083
00084 BaseGraph.ExtractGlobalRowCopy( BaseRow, MaxIndices, NumIndices, &Indices[0] );
00085 for( int k = 0; k < StencilSize; ++k )
00086 {
00087 int ColOffset = (RowIndices[i]+RowStencil[i][k]) * Offset;
00088 if( k > 0 ) ColOffset -= (RowIndices[i]+RowStencil[i][k-1]) * Offset;
00089
00090 for( int l = 0; l < NumIndices; ++l )
00091 Indices[l] += ColOffset;
00092
00093 GlobalGraph->InsertGlobalIndices( GlobalRow, NumIndices, &Indices[0] );
00094 }
00095 }
00096 }
00097
00098 GlobalGraph->FillComplete();
00099
00100 return GlobalGraph;
00101 }
00102
00103
00104 Epetra_CrsGraph * BlockUtility::GenerateBlockGraph(
00105 const Epetra_RowMatrix & BaseMatrix,
00106 const vector< vector<int> > & RowStencil,
00107 const vector<int> & RowIndices,
00108 const Epetra_Comm & GlobalComm )
00109 {
00110
00111 const Epetra_BlockMap & BaseMap = BaseMatrix.RowMatrixRowMap();
00112 const Epetra_BlockMap & BaseColMap = BaseMatrix.RowMatrixColMap();
00113 int BaseIndex = BaseMap.IndexBase();
00114 int Offset = BlockUtility::CalculateOffset(BaseMap);
00115
00116
00117 int NumBlockRows = RowIndices.size();
00118 int Size = BaseMap.NumMyElements();
00119 int TotalSize = NumBlockRows * Size;
00120 vector<int> GIDs(Size);
00121 BaseMap.MyGlobalElements( &GIDs[0] );
00122
00123 vector<int> GlobalGIDs( TotalSize );
00124 for( int i = 0; i < NumBlockRows; ++i )
00125 {
00126 for( int j = 0; j < Size; ++j )
00127 GlobalGIDs[i*Size+j] = GIDs[j] + RowIndices[i] * Offset;
00128 }
00129
00130 int GlobalSize;
00131 GlobalComm.SumAll( &TotalSize, &GlobalSize, 1 );
00132
00133 Epetra_Map GlobalMap( GlobalSize, TotalSize, &GlobalGIDs[0], BaseIndex, GlobalComm );
00134
00135 int MaxIndices = BaseMatrix.MaxNumEntries();
00136 vector<int> Indices(MaxIndices);
00137 vector<double> Values(MaxIndices);
00138 int NumIndices;
00139
00140 Epetra_CrsGraph * GlobalGraph = new Epetra_CrsGraph( Copy,
00141 dynamic_cast<Epetra_BlockMap&>(GlobalMap),
00142 0 );
00143
00144 for( int i = 0; i < NumBlockRows; ++i )
00145 {
00146 int StencilSize = RowStencil[i].size();
00147 for( int j = 0; j < Size; ++j )
00148 {
00149 int GlobalRow = GlobalMap.GID(j+i*Size);
00150
00151 BaseMatrix.ExtractMyRowCopy( j, MaxIndices, NumIndices, &Values[0], &Indices[0] );
00152 for( int l = 0; l < NumIndices; ++l ) Indices[l] = BaseColMap.GID(Indices[l]);
00153
00154 for( int k = 0; k < StencilSize; ++k )
00155 {
00156 int ColOffset = (RowIndices[i]+RowStencil[i][k]) * Offset;
00157 if( k > 0 ) ColOffset -= (RowIndices[i]+RowStencil[i][k-1]) * Offset;
00158
00159 for( int l = 0; l < NumIndices; ++l )
00160 Indices[l] += ColOffset;
00161
00162 GlobalGraph->InsertGlobalIndices( GlobalRow, NumIndices, &Indices[0] );
00163 }
00164 }
00165 }
00166
00167 GlobalGraph->FillComplete();
00168
00169 return GlobalGraph;
00170 }
00171
00172
00173 int BlockUtility::CalculateOffset(const Epetra_BlockMap & BaseMap)
00174 {
00175 int MaxGID = BaseMap.MaxAllGID();
00176
00177 int Offset = 1;
00178 while( Offset <= MaxGID ) Offset *= 10;
00179
00180 return Offset;
00181 }
00182
00183
00184 }