void linearSystemPETSc<scalar>::addToMatrix(int row, int col, const scalar &val) { if (!_entriesPreAllocated) preAllocateEntries(); PetscInt i = row, j = col; PetscScalar s = val; _try(MatSetValues(_a, 1, &i, 1, &j, &s, ADD_VALUES)); _valuesNotAssembled = true; }
void linearSystemPETSc<fullMatrix<double> >::addToRightHandSide(int row, const fullMatrix<double> &val) { if (!_entriesPreAllocated) preAllocateEntries(); int blockSize; _try(MatGetBlockSize(_a, &blockSize)); for (int ii = 0; ii < blockSize; ii++) { PetscInt i = row * blockSize + ii; PetscScalar v = val(ii, 0); VecSetValues(_b, 1, &i, &v, ADD_VALUES); } }
void linearSystemPETSc<scalar>::zeroMatrix() { if (_comm == PETSC_COMM_WORLD){ if (Msg::GetCommSize()>1){ int value = _entriesPreAllocated ? 1 : 0; int sumValue = 0; MPI_Allreduce((void*)&value, (void*)&sumValue, 1, MPI_INT, MPI_SUM, _comm); if ((sumValue >= 0) &&(sumValue < Msg::GetCommSize()) && !_entriesPreAllocated){ preAllocateEntries(); } } } if (_isAllocated && _entriesPreAllocated) { _assembleMatrixIfNeeded(); _try(MatZeroEntries(_a)); } }
void linearSystemPETSc<fullMatrix<double> >::addToMatrix(int row, int col, const fullMatrix<double> &val) { if (!_entriesPreAllocated) preAllocateEntries(); #ifdef PETSC_USE_COMPLEX fullMatrix<std::complex<double> > modval(val.size1(), val.size2()); for (int ii = 0; ii < val.size1(); ii++) { for (int jj = 0; jj < val.size1(); jj++) { modval(ii, jj) = val (jj, ii); modval(jj, ii) = val (ii, jj); } } #else fullMatrix<double> &modval = *const_cast<fullMatrix<double> *>(&val); for (int ii = 0; ii < val.size1(); ii++) { for (int jj = 0; jj < ii; jj++) { PetscScalar buff = modval(ii, jj); modval(ii, jj) = modval (jj, ii); modval(jj, ii) = buff; } } #endif PetscInt i = row, j = col; MatSetValuesBlocked(_a, 1, &i, 1, &j, &modval(0,0), ADD_VALUES); //transpose back so that the original matrix is not modified #ifndef PETSC_USE_COMPLEX for (int ii = 0; ii < val.size1(); ii++) for (int jj = 0; jj < ii; jj++) { PetscScalar buff = modval(ii,jj); modval(ii, jj) = modval (jj,ii); modval(jj, ii) = buff; } #endif _valuesNotAssembled = true; }