Пример #1
0
void Test(const string what, Epetra_RowMatrix& A)
{
  T Prec(&A);

  bool UseTranspose = true;

  IFPACK_CHK_ERRV(Prec.Initialize());
  IFPACK_CHK_ERRV(Prec.Compute());
  IFPACK_CHK_ERRV(Prec.SetUseTranspose(UseTranspose));

  Epetra_MultiVector LHS_exact(A.OperatorDomainMap(), 2);
  Epetra_MultiVector LHS(A.OperatorDomainMap(), 2);
  Epetra_MultiVector RHS(A.OperatorRangeMap(), 2);

  LHS_exact.Random(); LHS.PutScalar(0.0);

  A.Multiply(UseTranspose, LHS_exact, RHS);

  Prec.ApplyInverse(RHS, LHS);

  LHS.Update(1.0, LHS_exact, -1.0);
  double norm[2];

  LHS.Norm2(norm);
  norm[0] += norm[1];

  if (norm[0] > 1e-5)
  {
    cout << what << ": Test failed: norm = " << norm[0] << endl;
    exit(EXIT_FAILURE);
  }

  cout << what << ": Test passed: norm = " << norm[0] << endl;
}
Пример #2
0
int RowMatrixToHandle(FILE * handle, const Epetra_RowMatrix & A) {

  Epetra_Map map = A.RowMatrixRowMap();
  const Epetra_Comm & comm = map.Comm();
  int numProc = comm.NumProc();

  if (numProc==1 || !A.Map().DistributedGlobal())
    writeRowMatrix(handle, A);
  else {
    int numRows = map.NumMyElements();
    
    Epetra_Map allGidsMap((int_type) -1, numRows, (int_type) 0,comm);
    
    typename Epetra_GIDTypeVector<int_type>::impl allGids(allGidsMap);
    for (int i=0; i<numRows; i++) allGids[i] = (int_type) map.GID64(i);
    
    // Now construct a RowMatrix on PE 0 by strip-mining the rows of the input matrix A.
    int numChunks = numProc;
    int stripSize = allGids.GlobalLength64()/numChunks;
    int remainder = allGids.GlobalLength64()%numChunks;
    int curStart = 0;
    int curStripSize = 0;
    typename Epetra_GIDTypeSerialDenseVector<int_type>::impl importGidList;
    if (comm.MyPID()==0) 
      importGidList.Size(stripSize+1); // Set size of vector to max needed
    for (int i=0; i<numChunks; i++) {
      if (comm.MyPID()==0) { // Only PE 0 does this part
	curStripSize = stripSize;
	if (i<remainder) curStripSize++; // handle leftovers
	for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
	curStart += curStripSize;
      }
      // The following import map will be non-trivial only on PE 0.
      if (comm.MyPID()>0) assert(curStripSize==0);
      Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm);
      Epetra_Import gidImporter(importGidMap, allGidsMap);
      typename Epetra_GIDTypeVector<int_type>::impl importGids(importGidMap);
      if (importGids.Import(allGids, gidImporter, Insert)!=0) {EPETRA_CHK_ERR(-1); }

      // importGids now has a list of GIDs for the current strip of matrix rows.
      // Use these values to build another importer that will get rows of the matrix.

      // The following import map will be non-trivial only on PE 0.
      Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), map.IndexBase64(), comm);
      Epetra_Import importer(importMap, map);
      Epetra_CrsMatrix importA(Copy, importMap, 0);
      if (importA.Import(A, importer, Insert)!=0) {EPETRA_CHK_ERR(-1); }
      if (importA.FillComplete(A.OperatorDomainMap(), importMap)!=0) {EPETRA_CHK_ERR(-1);}

      // Finally we are ready to write this strip of the matrix to ostream
      if (writeRowMatrix(handle, importA)!=0) {EPETRA_CHK_ERR(-1);}
    }
  }
  return(0);
}
Пример #3
0
//==============================================================================
int Komplex_LinearProblem::TestMaps (const Epetra_RowMatrix & A0, const Epetra_RowMatrix & A1,
				      const Epetra_MultiVector & Xr, const Epetra_MultiVector & Xi,
				      const Epetra_MultiVector & Br, const Epetra_MultiVector & Bi){

  TEUCHOS_TEST_FOR_EXCEPT(!A0.RowMatrixRowMap().SameAs(A1.RowMatrixRowMap()));
  TEUCHOS_TEST_FOR_EXCEPT(!A0.OperatorDomainMap().SameAs(A1.OperatorDomainMap()));
  TEUCHOS_TEST_FOR_EXCEPT(!A0.OperatorRangeMap().SameAs(A1.OperatorRangeMap()));
  TEUCHOS_TEST_FOR_EXCEPT(!A0.OperatorDomainMap().SameAs(Xr.Map()));
  TEUCHOS_TEST_FOR_EXCEPT(!A0.OperatorRangeMap().SameAs(Br.Map()));
  TEUCHOS_TEST_FOR_EXCEPT(!A0.OperatorDomainMap().SameAs(Xi.Map()));
  TEUCHOS_TEST_FOR_EXCEPT(!A0.OperatorRangeMap().SameAs(Bi.Map()));

  // Test number of vectors also
  TEUCHOS_TEST_FOR_EXCEPT(Xr.NumVectors()!=Xi.NumVectors());
  TEUCHOS_TEST_FOR_EXCEPT(Xr.NumVectors()!=Br.NumVectors());
  TEUCHOS_TEST_FOR_EXCEPT(Xr.NumVectors()!=Bi.NumVectors());

  return(0);

}
Пример #4
0
int check(Epetra_RowMatrix& A, Epetra_RowMatrix & B, bool verbose)  {

  int ierr = 0;
  EPETRA_TEST_ERR(!A.Comm().NumProc()==B.Comm().NumProc(),ierr);
  EPETRA_TEST_ERR(!A.Comm().MyPID()==B.Comm().MyPID(),ierr);
  EPETRA_TEST_ERR(!A.Filled()==B.Filled(),ierr);
  EPETRA_TEST_ERR(!A.HasNormInf()==B.HasNormInf(),ierr);
  EPETRA_TEST_ERR(!A.LowerTriangular()==B.LowerTriangular(),ierr);
  EPETRA_TEST_ERR(!A.Map().SameAs(B.Map()),ierr);
  EPETRA_TEST_ERR(!A.MaxNumEntries()==B.MaxNumEntries(),ierr);
  EPETRA_TEST_ERR(!A.NumGlobalCols64()==B.NumGlobalCols64(),ierr);
  EPETRA_TEST_ERR(!A.NumGlobalDiagonals64()==B.NumGlobalDiagonals64(),ierr);
  EPETRA_TEST_ERR(!A.NumGlobalNonzeros64()==B.NumGlobalNonzeros64(),ierr);
  EPETRA_TEST_ERR(!A.NumGlobalRows64()==B.NumGlobalRows64(),ierr);
  EPETRA_TEST_ERR(!A.NumMyCols()==B.NumMyCols(),ierr);
  EPETRA_TEST_ERR(!A.NumMyDiagonals()==B.NumMyDiagonals(),ierr);
  EPETRA_TEST_ERR(!A.NumMyNonzeros()==B.NumMyNonzeros(),ierr);
  for (int i=0; i<A.NumMyRows(); i++) {
    int nA, nB;
    A.NumMyRowEntries(i,nA); B.NumMyRowEntries(i,nB);
    EPETRA_TEST_ERR(!nA==nB,ierr);
  }
  EPETRA_TEST_ERR(!A.NumMyRows()==B.NumMyRows(),ierr);
  EPETRA_TEST_ERR(!A.OperatorDomainMap().SameAs(B.OperatorDomainMap()),ierr);
  EPETRA_TEST_ERR(!A.OperatorRangeMap().SameAs(B.OperatorRangeMap()),ierr);
  EPETRA_TEST_ERR(!A.RowMatrixColMap().SameAs(B.RowMatrixColMap()),ierr);
  EPETRA_TEST_ERR(!A.RowMatrixRowMap().SameAs(B.RowMatrixRowMap()),ierr);
  EPETRA_TEST_ERR(!A.UpperTriangular()==B.UpperTriangular(),ierr);
  EPETRA_TEST_ERR(!A.UseTranspose()==B.UseTranspose(),ierr);

  int NumVectors = 5;
  { // No transpose case
    Epetra_MultiVector X(A.OperatorDomainMap(), NumVectors);
    Epetra_MultiVector YA1(A.OperatorRangeMap(), NumVectors);
    Epetra_MultiVector YA2(YA1);
    Epetra_MultiVector YB1(YA1);
    Epetra_MultiVector YB2(YA1);
    X.Random();

    bool transA = false;
    A.SetUseTranspose(transA);
    B.SetUseTranspose(transA);
    A.Apply(X,YA1);
    A.Multiply(transA, X, YA2);
    EPETRA_TEST_ERR(checkMultiVectors(YA1,YA2,"A Multiply and A Apply", verbose),ierr);
    B.Apply(X,YB1);
    EPETRA_TEST_ERR(checkMultiVectors(YA1,YB1,"A Multiply and B Multiply", verbose),ierr);
    B.Multiply(transA, X, YB2);
    EPETRA_TEST_ERR(checkMultiVectors(YA1,YB2,"A Multiply and B Apply", verbose), ierr);

  }
  {// transpose case
    Epetra_MultiVector X(A.OperatorRangeMap(), NumVectors);
    Epetra_MultiVector YA1(A.OperatorDomainMap(), NumVectors);
    Epetra_MultiVector YA2(YA1);
    Epetra_MultiVector YB1(YA1);
    Epetra_MultiVector YB2(YA1);
    X.Random();

    bool transA = true;
    A.SetUseTranspose(transA);
    B.SetUseTranspose(transA);
    A.Apply(X,YA1);
    A.Multiply(transA, X, YA2);
    EPETRA_TEST_ERR(checkMultiVectors(YA1,YA2, "A Multiply and A Apply (transpose)", verbose),ierr);
    B.Apply(X,YB1);
    EPETRA_TEST_ERR(checkMultiVectors(YA1,YB1, "A Multiply and B Multiply (transpose)", verbose),ierr);
    B.Multiply(transA, X,YB2);
    EPETRA_TEST_ERR(checkMultiVectors(YA1,YB2, "A Multiply and B Apply (transpose)", verbose),ierr);

  }

  Epetra_Vector diagA(A.RowMatrixRowMap());
  EPETRA_TEST_ERR(A.ExtractDiagonalCopy(diagA),ierr);
  Epetra_Vector diagB(B.RowMatrixRowMap());
  EPETRA_TEST_ERR(B.ExtractDiagonalCopy(diagB),ierr);
  EPETRA_TEST_ERR(checkMultiVectors(diagA,diagB, "ExtractDiagonalCopy", verbose),ierr);

  Epetra_Vector rowA(A.RowMatrixRowMap());
  EPETRA_TEST_ERR(A.InvRowSums(rowA),ierr);
  Epetra_Vector rowB(B.RowMatrixRowMap());
  EPETRA_TEST_ERR(B.InvRowSums(rowB),ierr)
  EPETRA_TEST_ERR(checkMultiVectors(rowA,rowB, "InvRowSums", verbose),ierr);

  Epetra_Vector colA(A.RowMatrixColMap());
  EPETRA_TEST_ERR(A.InvColSums(colA),ierr);
  Epetra_Vector colB(B.RowMatrixColMap());
  EPETRA_TEST_ERR(B.InvColSums(colB),ierr);
  EPETRA_TEST_ERR(checkMultiVectors(colA,colB, "InvColSums", verbose),ierr);

  EPETRA_TEST_ERR(checkValues(A.NormInf(), B.NormInf(), "NormInf before scaling", verbose), ierr);
  EPETRA_TEST_ERR(checkValues(A.NormOne(), B.NormOne(), "NormOne before scaling", verbose),ierr);

  EPETRA_TEST_ERR(A.RightScale(colA),ierr);
  EPETRA_TEST_ERR(B.RightScale(colB),ierr);


  EPETRA_TEST_ERR(A.LeftScale(rowA),ierr);
  EPETRA_TEST_ERR(B.LeftScale(rowB),ierr);


  EPETRA_TEST_ERR(checkValues(A.NormInf(), B.NormInf(), "NormInf after scaling", verbose), ierr);
  EPETRA_TEST_ERR(checkValues(A.NormOne(), B.NormOne(), "NormOne after scaling", verbose),ierr);

  vector<double> valuesA(A.MaxNumEntries());
  vector<int> indicesA(A.MaxNumEntries());
  vector<double> valuesB(B.MaxNumEntries());
  vector<int> indicesB(B.MaxNumEntries());
  return(0);
  for (int i=0; i<A.NumMyRows(); i++) {
    int nA, nB;
    EPETRA_TEST_ERR(A.ExtractMyRowCopy(i, A.MaxNumEntries(), nA, &valuesA[0], &indicesA[0]),ierr);
    EPETRA_TEST_ERR(B.ExtractMyRowCopy(i, B.MaxNumEntries(), nB, &valuesB[0], &indicesB[0]),ierr);
    EPETRA_TEST_ERR(!nA==nB,ierr);
    for (int j=0; j<nA; j++) {
      double curVal = valuesA[j];
      int curIndex = indicesA[j];
      bool notfound = true;
      int jj = 0;
      while (notfound && jj< nB) {
	if (!checkValues(curVal, valuesB[jj])) notfound = false;
	jj++;
      }
      EPETRA_TEST_ERR(notfound, ierr);
      vector<int>::iterator p = find(indicesB.begin(),indicesB.end(),curIndex);  // find curIndex in indicesB
      EPETRA_TEST_ERR(p==indicesB.end(), ierr);
    }

  }
  if (verbose) cout << "RowMatrix Methods check OK" << endl;

  return (ierr);
}
Пример #5
0
//==============================================================================
int Komplex_LinearProblem::ProcessValues(double c0r, double c0i, const Epetra_RowMatrix & A0,
					 double c1r, double c1i, const Epetra_RowMatrix & A1,
					 const Epetra_MultiVector & Xr, const Epetra_MultiVector & Xi,
					 const Epetra_MultiVector & Br, const Epetra_MultiVector & Bi,
					 bool firstTime) {

  TEUCHOS_TEST_FOR_EXCEPT(TestMaps(A0, A1, Xr, Xi, Br, Bi)!=0);
  TEUCHOS_TEST_FOR_EXCEPT(InitMatrixAccess(A0, A1)!=0);
  TEUCHOS_TEST_FOR_EXCEPT(ConstructKomplexMaps(A0.OperatorDomainMap(), A0.OperatorRangeMap(), A0.RowMatrixRowMap())!=0);

  if (firstTime) {
    KomplexMatrix_ = Teuchos::rcp(new Epetra_CrsMatrix(Copy,*KomplexMatrixRowMap_,0));
    KomplexLHS_ = Teuchos::rcp(new Epetra_MultiVector(*KomplexMatrixDomainMap_, Xr.NumVectors()));
    KomplexRHS_ = Teuchos::rcp(new Epetra_MultiVector(*KomplexMatrixRangeMap_, Xr.NumVectors()));
  }

  int NumMyRows = A0.NumMyRows();
  // Process X and B values
  for (int j=0; j<Xr.NumVectors(); j++) {
    double *localKX = &((*KomplexLHS_)[j][0]);
    double *localKB = &((*KomplexRHS_)[j][0]);
    for (int i=0; i< NumMyRows; i++) {
      localKX[2*i] = Xr[j][i];
      localKX[2*i+1] = Xi[j][i];
      localKB[2*i] = Br[j][i];
      localKB[2*i+1] = Bi[j][i];
    }
  }

  int NumIndices0;
  int NumIndices1;
  int * Indices0;
  int * Indices1;
  double * Values0;
  double * Values1;

  std::vector<int> KIndices0(MaxNumMyEntries0_);
  std::vector<double> KValues0(MaxNumMyEntries0_);
  std::vector<int> KIndices1(MaxNumMyEntries1_);
  std::vector<double> KValues1(MaxNumMyEntries1_);
  
  int * A0_ColGids = A0.RowMatrixColMap().MyGlobalElements();
  int * A1_ColGids = A1.RowMatrixColMap().MyGlobalElements();

  // For each row of A0 and A1 find the real and imag parts and place entries:
  // A_r -A_i
  // A_i  A_r

  for ( int i=0; i<NumMyRows; i++) {
    // Get ith row
    TEUCHOS_TEST_FOR_EXCEPT(GetRow(i, A0, A1, NumIndices0, Values0, Indices0, NumIndices1, Values1, Indices1)<0);
    int globalRow = A0.RowMatrixRowMap().GID(i);

    // Process real part due to c0r*A0
    if (c0r!=0.0) {
      // Upper Left
      for (int j=0; j<NumIndices0; j++) {
	KIndices0[j] = 2*A0_ColGids[Indices0[j]];
	KValues0[j] = c0r*Values0[j];
      }
      TEUCHOS_TEST_FOR_EXCEPT(PutRow(2*globalRow, NumIndices0, &KValues0[0], &KIndices0[0], firstTime)<0);

      // Lower Right
      for (int j=0; j<NumIndices0; j++) {
	KIndices0[j]++;
	// KValues0[j] = c0r*Values0[j];
      }
      TEUCHOS_TEST_FOR_EXCEPT(PutRow(2*globalRow+1, NumIndices0, &KValues0[0], &KIndices0[0], firstTime)<0);
    }
    // Process imag part due to c0i*A0
    if (c0i!=0.0) {
      // Upper Right (negate values)
      for (int j=0; j<NumIndices0; j++) {
	KIndices0[j] = 2*A0_ColGids[Indices0[j]]+1;
	KValues0[j] = - c0i*Values0[j];
      }
      TEUCHOS_TEST_FOR_EXCEPT(PutRow(2*globalRow, NumIndices0, &KValues0[0],  &KIndices0[0], firstTime)<0);
      // Lower Left
      for (int j=0; j<NumIndices0; j++) {
	KIndices0[j]--;
	KValues0[j] *= -1.0;
      }
      TEUCHOS_TEST_FOR_EXCEPT(PutRow(2*globalRow+1, NumIndices0, &KValues0[0], &KIndices0[0], firstTime)<0);
    }
    // Process real part due to c1r*A1
    if (c1r!=0.0) {
      // Upper Left
      for (int j=0; j<NumIndices1; j++) {
	KIndices1[j] = 2*A1_ColGids[Indices1[j]];
	KValues1[j] = c1r*Values1[j];
      }
      TEUCHOS_TEST_FOR_EXCEPT(PutRow(2*globalRow, NumIndices1, &KValues1[0], &KIndices1[0], firstTime)<0);

      // Lower Right
      for (int j=0; j<NumIndices1; j++) {
	KIndices1[j]++;
	//KValues1[j] = c1r*Values1[j];
      }
      TEUCHOS_TEST_FOR_EXCEPT(PutRow(2*globalRow+1, NumIndices1, &KValues1[0], &KIndices1[0], firstTime)<0);
    }
    // Process imag part due to c1i*A1
    if (c1i!=0.0) {
      // Upper Right (negate values)
      for (int j=0; j<NumIndices1; j++) {
	KIndices1[j] = 2*A1_ColGids[Indices1[j]]+1;
	KValues1[j] = - c1i*Values1[j];
      }
      TEUCHOS_TEST_FOR_EXCEPT(PutRow(2*globalRow, NumIndices1, &KValues1[0], &KIndices1[0], firstTime)<0);
      // Lower Left
      for (int j=0; j<NumIndices1; j++) {
	KIndices1[j]--;
	KValues1[j] *= -1.0;
      }
      TEUCHOS_TEST_FOR_EXCEPT(PutRow(2*globalRow+1, NumIndices1, &KValues1[0], &KIndices1[0], firstTime)<0);
    }
  }
  return(0);
}