Teuchos::RCP<Epetra_CrsMatrix> buildH( const Teuchos::RCP<Epetra_CrsMatrix> &A) { Teuchos::RCP<Epetra_CrsMatrix> H = Teuchos::rcp( new Epetra_CrsMatrix(Copy, A->RowMap(), A->GlobalMaxNumEntries() ) ); int N = A->NumGlobalRows(); std::vector<double> A_values( N ); std::vector<int> A_indices( N ); int A_size = 0; double local_H; bool found_diag = false; for ( int i = 0; i < N; ++i ) { A->ExtractGlobalRowCopy( i, N, A_size, &A_values[0], &A_indices[0] ); for ( int j = 0; j < A_size; ++j ) { if ( i == A_indices[j] ) { local_H = 1.0 - A_values[j]; H->InsertGlobalValues( i, 1, &local_H, &A_indices[j] ); found_diag = true; } else { local_H = -A_values[j]; H->InsertGlobalValues( i, 1, &local_H, &A_indices[j] ); } } if ( !found_diag ) { local_H = 1.0; H->InsertGlobalValues( i, 1, &local_H, &i ); } } H->FillComplete(); return H; }
// quad tests TEUCHOS_UNIT_TEST(tEpetraLinearObjFactory, buildTest_quad) { // build global (or serial communicator) #ifdef HAVE_MPI stk::ParallelMachine Comm = MPI_COMM_WORLD; Teuchos::RCP<Epetra_Comm> eComm = Teuchos::rcp(new Epetra_MpiComm(MPI_COMM_WORLD)); #else stk::ParallelMachine Comm = WHAT_TO_DO_COMM; Teuchos::RCP<Epetra_Comm> eComm = Teuchos::rcp(new Epetra_SerialComm()); #endif int numProcs = stk::parallel_machine_size(Comm); int myRank = stk::parallel_machine_rank(Comm); TEUCHOS_ASSERT(numProcs<=2); // build a geometric pattern from a single basis RCP<const panzer::FieldPattern> patternC1 = buildFieldPattern<Intrepid::Basis_HGRAD_QUAD_C1_FEM<double,FieldContainer> >(); RCP<panzer::ConnManager<int,int> > connManager = buildQuadMesh(Comm,2,2,1,1); RCP<panzer::DOFManager<int,int> > dofManager = rcp(new panzer::DOFManager<int,int>()); dofManager->setConnManager(connManager,MPI_COMM_WORLD); dofManager->addField("u",patternC1); dofManager->buildGlobalUnknowns(); panzer::EpetraLinearObjFactory<panzer::Traits,int> laFactory(eComm.getConst(),dofManager); Teuchos::RCP<Epetra_Map> map = laFactory.getMap(); Teuchos::RCP<Epetra_Map> gMap = laFactory.getGhostedMap(); Teuchos::RCP<Epetra_CrsGraph> graph = laFactory.getGraph(); Teuchos::RCP<Epetra_CrsGraph> gGraph = laFactory.getGhostedGraph(); std::vector<int> owned,ownedAndShared; dofManager->getOwnedIndices(owned); dofManager->getOwnedAndSharedIndices(ownedAndShared); // test maps { TEST_EQUALITY(map->NumMyElements(),(int) owned.size()); TEST_EQUALITY(gMap->NumMyElements(),(int) ownedAndShared.size()); // test indices for(std::size_t i=0;i<owned.size();i++) TEST_ASSERT(map->MyGID(owned[i])); for(std::size_t i=0;i<ownedAndShared.size();i++) TEST_ASSERT(gMap->MyGID(ownedAndShared[i])); } // test ograph { TEST_ASSERT(gGraph->Filled()); TEST_EQUALITY(gGraph->NumMyRows(),(int) ownedAndShared.size()); TEST_EQUALITY(gGraph->MaxNumIndices(),numProcs==2 ? 6 : 9); std::vector<int> indices(10); int numIndices = 0; // Take degree of freedom in middle of mesh: Then look at ghosted graph int err = gGraph->ExtractGlobalRowCopy(5,10,numIndices,&indices[0]); TEST_EQUALITY(err,0); indices.resize(numIndices); std::sort(indices.begin(),indices.end()); if(numProcs==1) { TEST_EQUALITY(numIndices,9); std::vector<int> compare(9); compare[0] = 0; compare[1] = 1; compare[2] = 2; compare[3] = 3; compare[4] = 4; compare[5] = 5; compare[3] = 6; compare[4] = 7; compare[5] = 8; TEST_EQUALITY(compare.size(),indices.size()); TEST_ASSERT(std::equal(compare.begin(),compare.end(),indices.begin())); } else if(numProcs==2 && myRank==0) { TEST_EQUALITY(numIndices,6); std::vector<int> compare(6); compare[0] = 0; compare[1] = 1; compare[2] = 2; compare[3] = 3; compare[4] = 5; compare[5] = 7; TEST_EQUALITY(compare.size(),indices.size()); TEST_ASSERT(std::equal(compare.begin(),compare.end(),indices.begin())); } else if(numProcs==2 && myRank==1) { TEST_EQUALITY(numIndices,6); std::vector<int> compare(6); compare[0] = 3; compare[1] = 4; compare[2] = 5; compare[3] = 6; compare[4] = 7; compare[5] = 8; TEST_EQUALITY(compare.size(),indices.size()); TEST_ASSERT(std::equal(compare.begin(),compare.end(),indices.begin())); } } // test graph { TEST_ASSERT(graph->Filled()); TEST_EQUALITY(graph->NumMyRows(),(int) owned.size()); TEST_EQUALITY(graph->MaxNumIndices(),myRank==0 ? 6 : 9); } }