]> https://gitweb.dealii.org/ - dealii.git/commitdiff
IndexSet::make_petsc_is(): assert indices are in range.
authorDavid Wells <drwells@email.unc.edu>
Sun, 11 Aug 2024 19:13:35 +0000 (13:13 -0600)
committerDavid Wells <drwells@email.unc.edu>
Mon, 12 Aug 2024 02:42:37 +0000 (20:42 -0600)
source/base/index_set.cc

index 35d91f9217cef6ab05b06d799fbf8390db6dfcf1..66885684ec24e3c6998f907cc720f8d6693d5734 100644 (file)
@@ -1112,12 +1112,20 @@ IndexSet::make_petsc_is(const MPI_Comm communicator) const
   std::vector<size_type> indices;
   fill_index_vector(indices);
 
-  PetscInt              n = indices.size();
-  std::vector<PetscInt> pindices(indices.begin(), indices.end());
+  // If the size of the index set can be converted to a PetscInt then every
+  // value can also be converted
+  AssertThrowIntegerConversion(static_cast<PetscInt>(size()), size());
+  const auto local_size = static_cast<PetscInt>(n_elements());
+  AssertIntegerConversion(local_size, n_elements());
+
+  size_type             i = 0;
+  std::vector<PetscInt> petsc_indices(n_elements());
+  for (const auto &index : *this)
+    petsc_indices[i] = static_cast<PetscInt>(index);
 
   IS             is;
-  PetscErrorCode ierr =
-    ISCreateGeneral(communicator, n, pindices.data(), PETSC_COPY_VALUES, &is);
+  PetscErrorCode ierr = ISCreateGeneral(
+    communicator, local_size, petsc_indices.data(), PETSC_COPY_VALUES, &is);
   AssertThrow(ierr == 0, ExcPETScError(ierr));
 
   return is;

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.