|
Didasko
Development
|
// @HEADER // *********************************************************************** // // Didasko Tutorial Package // Copyright (2005) Sandia Corporation // // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive // license for use of this work by or on behalf of the U.S. Government. // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 // USA // // Questions about Didasko? Contact Marzio Sala (marzio.sala _AT_ gmail.com) // // *********************************************************************** // @HEADER // Create a Crs matrix corresponding to a 2D Laplacian problem // on a cartesian mesh. #include "Didasko_ConfigDefs.h" #if defined(HAVE_DIDASKO_EPETRA) #include "Epetra_ConfigDefs.h" #ifdef HAVE_MPI #include "mpi.h" #include "Epetra_MpiComm.h" #else #include "Epetra_SerialComm.h" #endif #include "Epetra_Map.h" #include "Epetra_Vector.h" #include "Epetra_CrsMatrix.h" // function declaration void get_neighbours( const int i, const int nx, const int ny, int & left, int & right, int & lower, int & upper); // =========== // // main driver // // =========== // int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc, &argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // number of nodes in the x- and y-direction int nx = 5; int ny = 6; int NumGlobalElements = nx * ny; // create a linear map Epetra_Map Map(NumGlobalElements,0,Comm); // local number of rows int NumMyElements = Map.NumMyElements(); // get update list int * MyGlobalElements = Map.MyGlobalElements( ); // Create an integer vector NumNz that is used to build the Petra Matrix. // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation // on this processor. // NOTE: NumNz can be specified to be an interfer, of value 5. // However, the procedure here reported is more general, and it is // representative of more complex situations, where the number of // nonzero per row can vary consistently. int * NumNz = new int[NumMyElements]; double off_left = -1.0; double off_right = -1.0; double off_lower = -1.0; double off_upper = -1.0; double diag = 4.0; int left, right, lower, upper; for ( int i=0; i<NumMyElements; i++) { NumNz[i] = 1; get_neighbours( MyGlobalElements[i], nx, ny, left, right, lower, upper); if( left != -1 ) ++NumNz[i]; if( right != -1 ) ++NumNz[i]; if( lower != -1 ) ++NumNz[i]; if( upper != -1 ) ++NumNz[i]; } // Create a Epetra_Matrix // create a CRS matrix Epetra_CrsMatrix A(Copy,Map,NumNz); // Add rows one-at-a-time double Values[4]; int Indices[4]; for( int i=0 ; i<NumMyElements; ++i ) { int NumEntries=0; get_neighbours( MyGlobalElements[i], nx, ny, left, right, lower, upper); if( left != -1 ) { Indices[NumEntries] = left; Values[NumEntries] = off_left; ++NumEntries; } if( right != -1 ) { Indices[NumEntries] = right; Values[NumEntries] = off_right; ++NumEntries; } if( lower != -1 ) { Indices[NumEntries] = lower; Values[NumEntries] = off_lower; ++NumEntries; } if( upper != -1 ) { Indices[NumEntries] = upper; Values[NumEntries] = off_upper; ++NumEntries; } // put the off-diagonal entries A.InsertGlobalValues(MyGlobalElements[i], NumEntries, Values, Indices); // Put in the diagonal entry A.InsertGlobalValues(MyGlobalElements[i], 1, &diag, MyGlobalElements+i); } cout << A; #ifdef HAVE_MPI MPI_Finalize(); #endif delete NumNz; return(EXIT_SUCCESS); } void get_neighbours( const int i, const int nx, const int ny, int & left, int & right, int & lower, int & upper) { int ix, iy; ix = i%nx; iy = (i - ix)/nx; if( ix == 0 ) left = -1; else left = i-1; if( ix == nx-1 ) right = -1; else right = i+1; if( iy == 0 ) lower = -1; else lower = i-nx; if( iy == ny-1 ) upper = -1; else upper = i+nx; return; } #else #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]) { puts("Please configure Didasko with:\n" "--enable-epetra"); return 0; } #endif
1.7.6.1