BlockGmres/createEpetraProblem.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                 Belos: Block Linear Solvers Package
00005 //                 Copyright (2004) 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 "createEpetraProblem.hpp"
00030 #include "Teuchos_Workspace.hpp"
00031 #include "Trilinos_Util.h"
00032 #include "Epetra_CrsMatrix.h"
00033 #include "Epetra_MultiVector.h"
00034 #include "Epetra_Map.h"
00035 #ifdef EPETRA_MPI
00036 #include "Epetra_MpiComm.h"
00037 #else
00038 #include "Epetra_SerialComm.h"
00039 #endif
00040 #include "Epetra_Map.h"
00041 
00042 int Belos::createEpetraProblem(
00043              std::string                      &filename
00044              ,RefCountPtr<Epetra_Map>         *rowMap
00045              ,RefCountPtr<Epetra_CrsMatrix>   *A
00046              ,RefCountPtr<Epetra_MultiVector> *B
00047              ,RefCountPtr<Epetra_MultiVector> *X
00048              ,int                             *MyPID_out
00049              )
00050 {
00051   //
00052   int &MyPID = *MyPID_out;
00053   //
00054   int i;
00055   int n_nonzeros, N_update;
00056   int *bindx=0, *update=0, *col_inds=0;
00057   double *val=0, *row_vals=0;
00058   double *xguess=0, *b=0, *xexact=0;
00059 
00060   RefCountPtr<Epetra_Comm> epetraComm;
00061 #ifdef EPETRA_MPI 
00062   epetraComm = rcp(new Epetra_MpiComm( MPI_COMM_WORLD ) );  
00063 #else 
00064   epetraComm = rcp(new Epetra_SerialComm());
00065 #endif
00066   
00067   MyPID = epetraComm->MyPID();
00068   //
00069   // **********************************************************************
00070   // ******************Set up the problem to be solved*********************
00071   // **********************************************************************
00072   //
00073   int NumGlobalElements;  // total # of rows in matrix
00074   //
00075   // *****Read in matrix from HB file******
00076   //
00077   Trilinos_Util_read_hb(const_cast<char *>(filename.c_str()), MyPID, &NumGlobalElements, &n_nonzeros,
00078       &val, &bindx, &xguess, &b, &xexact);
00079   // 
00080   // *****Distribute data among processors*****
00081   //
00082   Trilinos_Util_distrib_msr_matrix(*epetraComm, &NumGlobalElements, &n_nonzeros, &N_update, 
00083            &update, &val, &bindx, &xguess, &b, &xexact);
00084   //
00085   // *****Construct the matrix*****
00086   //
00087   int NumMyElements = N_update; // # local rows of matrix on processor
00088   //
00089   // Create an integer vector NumNz that is used to build the Petra Matrix.
00090   // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation 
00091   // on this processor
00092   //
00093   int * NumNz = new int[NumMyElements];
00094   for (i=0; i<NumMyElements; i++) {
00095     NumNz[i] = bindx[i+1] - bindx[i] + 1;
00096   }
00097   //
00098   RefCountPtr<Epetra_Map> epetraMap = rcp(new Epetra_Map(NumGlobalElements, NumMyElements, update, 0, *epetraComm));
00099   Teuchos::set_extra_data( epetraComm, "Map::Comm", &epetraMap );
00100   if(rowMap) *rowMap = epetraMap;
00101   //
00102   // Create a Epetra_Matrix
00103   //
00104   *A = rcp(new Epetra_CrsMatrix(Copy, *epetraMap, NumNz));
00105   Teuchos::set_extra_data( epetraMap, "Operator::Map", A );
00106   //
00107   // Add rows one-at-a-time
00108   //
00109   int NumEntries;
00110   for (i=0; i<NumMyElements; i++) {
00111     row_vals = val + bindx[i];
00112     col_inds = bindx + bindx[i];
00113     NumEntries = bindx[i+1] - bindx[i];
00114     assert((*A)->InsertGlobalValues(update[i], NumEntries, row_vals, col_inds)==0);
00115     assert((*A)->InsertGlobalValues(update[i], 1, val+i, update+i)==0);
00116   }
00117   //
00118   // Finish up
00119   //
00120   assert((*A)->FillComplete()==0);
00121   assert((*A)->OptimizeStorage()==0);
00122   (*A)->SetTracebackMode(1); // Shutdown Epetra Warning tracebacks
00123   //
00124   // Construct the right-hand side and solution multivectors.
00125   //
00126   if(B) {
00127     *B = rcp(new Epetra_MultiVector(::Copy, *epetraMap, b, NumMyElements, 1 ));
00128     Teuchos::set_extra_data( epetraMap, "B::Map", B );
00129   }
00130   if(X) {
00131     *X = rcp(new Epetra_MultiVector(*epetraMap, 1 ));
00132     Teuchos::set_extra_data( epetraMap, "X::Map", X );
00133   }
00134   //
00135   // Create workspace
00136   //
00137   Teuchos::set_default_workspace_store(
00138     Teuchos::rcp(new Teuchos::WorkspaceStoreInitializeable(static_cast<size_t>(2e+6)))
00139     );
00140   //
00141   // Free up memory
00142   //
00143   delete [] NumNz;
00144   free(update);
00145   free(val);
00146   free(bindx);
00147   if (xexact) free(xexact);
00148   if (xguess) free(xguess);
00149   if (b) free(b);
00150 
00151   return (0);
00152 }

Generated on Thu Sep 18 12:30:21 2008 for Belos Package Browser (Single Doxygen Collection) by doxygen 1.3.9.1