diff --git a/MathLib/LinAlg/PETSc/PETScMatrix.cpp b/MathLib/LinAlg/PETSc/PETScMatrix.cpp index 8fc8563682cb443e288d8881c73aae741f70edda..66cb4dc1c4175eb612cb3e188d13eca1b575fc71 100644 --- a/MathLib/LinAlg/PETSc/PETScMatrix.cpp +++ b/MathLib/LinAlg/PETSc/PETScMatrix.cpp @@ -73,7 +73,7 @@ PETScMatrix& PETScMatrix::operator=(PETScMatrix const& A) start_rank_ = A.start_rank_; end_rank_ = A.end_rank_; - if (A_) + if (A_ != nullptr) { // TODO this is the slowest option for copying MatCopy(A.A_, A_, DIFFERENT_NONZERO_PATTERN); @@ -104,9 +104,13 @@ void PETScMatrix::setRowsColumnsZero(std::vector<PetscInt> const& row_pos) MatSetOption(A_, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE); if (nrows > 0) + { MatZeroRows(A_, nrows, &row_pos[0], one, PETSC_NULL, PETSC_NULL); + } else + { MatZeroRows(A_, 0, PETSC_NULL, one, PETSC_NULL, PETSC_NULL); + } } void PETScMatrix::viewer(const std::string& file_name, diff --git a/MathLib/LinAlg/PETSc/PETScMatrix.h b/MathLib/LinAlg/PETSc/PETScMatrix.h index 0fa33f78eb02be9d97ca2b7887c49ff890d374db..04ec1cc90256ae18099acc95e6c46e4689e9780b 100644 --- a/MathLib/LinAlg/PETSc/PETScMatrix.h +++ b/MathLib/LinAlg/PETSc/PETScMatrix.h @@ -156,9 +156,13 @@ public: { // Ghost entries, and its original index is 0. if (col == -ncols_) + { cols.push_back(0); + } else + { cols.push_back(std::abs(col)); + } } add(indices.rows, cols, sub_matrix); @@ -211,8 +215,10 @@ public: private: void destroy() { - if (A_) + if (A_ != nullptr) + { MatDestroy(&A_); + } A_ = nullptr; } diff --git a/MathLib/LinAlg/PETSc/PETScVector.cpp b/MathLib/LinAlg/PETSc/PETScVector.cpp index ac7a6394ea7f47a8a79606298fb4b7e31ecc476d..06cb2c01cc3c6bc81dbc50a59a3dd1f4e51c84b0 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.cpp +++ b/MathLib/LinAlg/PETSc/PETScVector.cpp @@ -66,7 +66,9 @@ PETScVector::PETScVector(const PetscInt vec_size, config(); for (PetscInt i = 0; i < nghosts; i++) + { global_ids2local_ids_ghost_.emplace(ghost_ids[i], size_loc_ + i); + } } PETScVector::PETScVector(const PETScVector& existing_vec, const bool deep_copy) @@ -176,22 +178,7 @@ void PETScVector::getGlobalVector(std::vector<PetscScalar>& u) const void PETScVector::setLocalAccessibleVector() const { - if (entry_array_.empty()) - { - const PetscInt array_size = global_ids2local_ids_ghost_.empty() - ? size_ - : size_loc_ + size_ghosts_; - entry_array_.resize(array_size); - } - - if (!global_ids2local_ids_ghost_.empty()) - { - PetscScalar* loc_x = getLocalVector(); - std::copy_n(loc_x, size_loc_ + size_ghosts_, entry_array_.begin()); - restoreArray(loc_x); - } - else - getGlobalVector(entry_array_); + copyValues(entry_array_); } void PETScVector::copyValues(std::vector<PetscScalar>& u) const @@ -255,14 +242,19 @@ PetscScalar* PETScVector::getLocalVector() const VecGetArray(v_loc_, &loc_array); } else + { VecGetArray(v_, &loc_array); + } + return loc_array; } PetscInt PETScVector::getLocalIndex(const PetscInt global_index) const { if (global_index >= 0) // non-ghost entry. + { return global_index - start_rank_; + } // A special case for a ghost location with global index equal to // the size of the local vector: @@ -279,7 +271,9 @@ void PETScVector::restoreArray(PetscScalar* array) const VecGhostRestoreLocalForm(v_, &v_loc_); } else + { VecRestoreArray(v_, &array); + } } void PETScVector::viewer(const std::string& file_name, diff --git a/MathLib/LinAlg/PETSc/PETScVector.h b/MathLib/LinAlg/PETSc/PETScVector.h index 8c1546fad89545fc5699a9b515e7a17cede277f7..f31f1e35ede25609c8c23f58dade939a7e89f2a8 100644 --- a/MathLib/LinAlg/PETSc/PETScVector.h +++ b/MathLib/LinAlg/PETSc/PETScVector.h @@ -221,8 +221,10 @@ public: private: void destroy() { - if (v_) + if (v_ != nullptr) + { VecDestroy(&v_); + } v_ = nullptr; }