27 #ifdef LIBMESH_HAVE_PETSC 43 this->_restore_array();
44 libmesh_assert(this->
closed());
46 PetscErrorCode
ierr=0;
52 return static_cast<T
>(
value);
59 this->_restore_array();
60 libmesh_assert(this->
closed());
62 PetscErrorCode
ierr=0;
76 this->_restore_array();
77 libmesh_assert(this->
closed());
79 PetscErrorCode
ierr=0;
94 this->_restore_array();
95 libmesh_assert(this->
closed());
97 PetscErrorCode
ierr=0;
100 ierr = VecNorm (_vec, NORM_INFINITY, &
value);
101 LIBMESH_CHKERR(
ierr);
109 template <
typename T>
113 this->_restore_array();
114 libmesh_assert(this->
closed());
123 template <
typename T>
127 this->_restore_array();
128 libmesh_assert(this->
closed());
137 template <
typename T>
140 this->_restore_array();
141 libmesh_assert_less (i, size());
143 PetscErrorCode
ierr=0;
144 PetscInt i_val =
static_cast<PetscInt
>(i);
145 PetscScalar petsc_value =
static_cast<PetscScalar
>(
value);
147 ierr = VecSetValues (_vec, 1, &i_val, &petsc_value, INSERT_VALUES);
148 LIBMESH_CHKERR(
ierr);
150 this->_is_closed =
false;
155 template <
typename T>
158 PetscErrorCode
ierr = 0;
161 ierr = VecReciprocal(_vec);
162 LIBMESH_CHKERR(
ierr);
167 template <
typename T>
170 PetscErrorCode
ierr = 0;
173 ierr = VecConjugate(_vec);
174 LIBMESH_CHKERR(
ierr);
179 template <
typename T>
182 this->_restore_array();
183 libmesh_assert_less (i, size());
185 PetscErrorCode
ierr=0;
186 PetscInt i_val =
static_cast<PetscInt
>(i);
187 PetscScalar petsc_value =
static_cast<PetscScalar
>(
value);
189 ierr = VecSetValues (_vec, 1, &i_val, &petsc_value, ADD_VALUES);
190 LIBMESH_CHKERR(
ierr);
192 this->_is_closed =
false;
197 template <
typename T>
199 const std::vector<numeric_index_type> & dof_indices)
202 if (dof_indices.empty())
205 this->_restore_array();
207 PetscErrorCode
ierr=0;
208 const PetscInt * i_val =
reinterpret_cast<const PetscInt *
>(dof_indices.data());
209 const PetscScalar * petsc_value =
static_cast<const PetscScalar *
>(v);
211 ierr = VecSetValues (_vec, cast_int<PetscInt>(dof_indices.size()),
212 i_val, petsc_value, ADD_VALUES);
213 LIBMESH_CHKERR(
ierr);
215 this->_is_closed =
false;
220 template <
typename T>
224 this->_restore_array();
226 const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
229 PetscErrorCode
ierr=0;
234 libmesh_warning(
"Matrix A must be assembled before calling PetscVector::add_vector(v, A).\n" 235 "Please update your code, as this warning will become an error in a future release.");
236 libmesh_deprecated();
243 LIBMESH_CHKERR(
ierr);
248 template <
typename T>
252 this->_restore_array();
254 const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
257 PetscErrorCode
ierr=0;
262 libmesh_warning(
"Matrix A must be assembled before calling PetscVector::add_vector_transpose(v, A).\n" 263 "Please update your code, as this warning will become an error in a future release.");
264 libmesh_deprecated();
271 LIBMESH_CHKERR(
ierr);
275 #if PETSC_VERSION_LESS_THAN(3,1,0) 276 template <
typename T>
280 libmesh_error_msg(
"MatMultHermitianTranspose was introduced in PETSc 3.1.0," \
281 <<
"No one has made it backwards compatible with older " \
282 <<
"versions of PETSc so far.");
287 template <
typename T>
291 this->_restore_array();
293 const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
299 libmesh_warning(
"Matrix A must be assembled before calling PetscVector::add_vector_conjugate_transpose(v, A).\n" 300 "Please update your code, as this warning will become an error in a future release.");
301 libmesh_deprecated();
307 std::unique_ptr<NumericVector<Number>> this_clone = this->clone();
311 PetscErrorCode
ierr = MatMultHermitianTranspose(
const_cast<PetscMatrix<T> *
>(
A)->mat(), v->
_vec, _vec);
312 LIBMESH_CHKERR(
ierr);
315 this->add(1., *this_clone);
320 template <
typename T>
323 this->_get_array(
false);
331 template <
typename T>
339 template <
typename T>
342 this->_restore_array();
344 PetscErrorCode
ierr = 0;
345 PetscScalar a =
static_cast<PetscScalar
>(a_in);
348 const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
351 libmesh_assert_equal_to (this->size(), v->
size());
356 LIBMESH_CHKERR(
ierr);
362 ierr = VecGhostGetLocalForm (_vec,&loc_vec);
363 LIBMESH_CHKERR(
ierr);
364 ierr = VecGhostGetLocalForm (v->
_vec,&v_loc_vec);
365 LIBMESH_CHKERR(
ierr);
367 ierr = VecAXPY(loc_vec, a, v_loc_vec);
368 LIBMESH_CHKERR(
ierr);
370 ierr = VecGhostRestoreLocalForm (v->
_vec,&v_loc_vec);
371 LIBMESH_CHKERR(
ierr);
372 ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
373 LIBMESH_CHKERR(
ierr);
379 template <
typename T>
381 const std::vector<numeric_index_type> & dof_indices)
383 if (dof_indices.empty())
386 this->_restore_array();
388 PetscErrorCode
ierr=0;
390 ierr = VecSetValues (_vec, cast_int<PetscInt>(dof_indices.size()),
391 idx_values, v, INSERT_VALUES);
392 LIBMESH_CHKERR(
ierr);
394 this->_is_closed =
false;
399 template <
typename T>
402 this->_restore_array();
404 PetscErrorCode
ierr = 0;
405 PetscScalar factor =
static_cast<PetscScalar
>(factor_in);
409 ierr = VecScale(_vec, factor);
410 LIBMESH_CHKERR(
ierr);
415 ierr = VecGhostGetLocalForm (_vec,&loc_vec);
416 LIBMESH_CHKERR(
ierr);
418 ierr = VecScale(loc_vec, factor);
419 LIBMESH_CHKERR(
ierr);
421 ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
422 LIBMESH_CHKERR(
ierr);
426 template <
typename T>
429 PetscErrorCode
ierr = 0;
431 const PetscVector<T> * v_vec = cast_ptr<const PetscVector<T> *>(&v);
433 ierr = VecPointwiseDivide(_vec, _vec, v_vec->
_vec);
434 LIBMESH_CHKERR(
ierr);
439 template <
typename T>
442 this->_restore_array();
444 PetscErrorCode
ierr = 0;
449 LIBMESH_CHKERR(
ierr);
454 ierr = VecGhostGetLocalForm (_vec,&loc_vec);
455 LIBMESH_CHKERR(
ierr);
457 ierr = VecAbs(loc_vec);
458 LIBMESH_CHKERR(
ierr);
460 ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
461 LIBMESH_CHKERR(
ierr);
465 template <
typename T>
468 this->_restore_array();
471 PetscErrorCode
ierr = 0;
474 PetscScalar
value=0.;
477 const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
481 LIBMESH_CHKERR(
ierr);
483 return static_cast<T
>(
value);
486 template <
typename T>
489 this->_restore_array();
492 PetscErrorCode
ierr = 0;
495 PetscScalar
value=0.;
498 const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
502 LIBMESH_CHKERR(
ierr);
504 return static_cast<T
>(
value);
508 template <
typename T>
512 this->_restore_array();
513 libmesh_assert(this->
closed());
515 PetscErrorCode
ierr = 0;
516 PetscScalar s =
static_cast<PetscScalar
>(s_in);
518 if (this->size() != 0)
522 ierr = VecSet(_vec, s);
523 LIBMESH_CHKERR(
ierr);
528 ierr = VecGhostGetLocalForm (_vec,&loc_vec);
529 LIBMESH_CHKERR(
ierr);
531 ierr = VecSet(loc_vec, s);
532 LIBMESH_CHKERR(
ierr);
534 ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
535 LIBMESH_CHKERR(
ierr);
544 template <
typename T>
549 const PetscVector<T> * v = cast_ptr<const PetscVector<T> *>(&v_in);
558 template <
typename T>
562 this->_restore_array();
565 libmesh_assert_equal_to (this->size(), v.
size());
566 libmesh_assert_equal_to (this->local_size(), v.
local_size());
567 libmesh_assert (v.
closed());
569 PetscErrorCode
ierr = 0;
579 ierr = VecCopy (v.
_vec, this->_vec);
580 LIBMESH_CHKERR(
ierr);
586 libmesh_assert_equal_to (this->_type, v.
_type);
592 ierr = VecCopy (v.
_vec, this->_vec);
593 LIBMESH_CHKERR(
ierr);
599 ierr = VecGhostGetLocalForm (_vec,&loc_vec);
600 LIBMESH_CHKERR(
ierr);
601 ierr = VecGhostGetLocalForm (v.
_vec,&v_loc_vec);
602 LIBMESH_CHKERR(
ierr);
604 ierr = VecCopy (v_loc_vec, loc_vec);
605 LIBMESH_CHKERR(
ierr);
607 ierr = VecGhostRestoreLocalForm (v.
_vec,&v_loc_vec);
608 LIBMESH_CHKERR(
ierr);
609 ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
610 LIBMESH_CHKERR(
ierr);
622 template <
typename T>
626 this->_get_array(
false);
632 if (this->size() == v.size())
637 _values[i] = static_cast<PetscScalar>(v[first + i]);
647 _values[i] = static_cast<PetscScalar>(v[i]);
659 template <
typename T>
662 this->_restore_array();
665 PetscVector<T> * v_local = cast_ptr<PetscVector<T> *>(&v_local_in);
667 libmesh_assert(v_local);
668 libmesh_assert_equal_to (v_local->
size(), this->size());
670 PetscErrorCode
ierr = 0;
671 const PetscInt n = this->size();
677 std::vector<PetscInt>
idx(n);
682 LIBMESH_CHKERR(
ierr);
684 ierr = LibMeshVecScatterCreate(_vec, is,
687 LIBMESH_CHKERR(
ierr);
690 ierr = VecScatterBegin(scatter, _vec, v_local->
_vec,
691 INSERT_VALUES, SCATTER_FORWARD);
692 LIBMESH_CHKERR(
ierr);
694 ierr = VecScatterEnd (scatter, _vec, v_local->
_vec,
695 INSERT_VALUES, SCATTER_FORWARD);
696 LIBMESH_CHKERR(
ierr);
699 ierr = LibMeshISDestroy (&is);
700 LIBMESH_CHKERR(
ierr);
702 ierr = LibMeshVecScatterDestroy(&scatter);
703 LIBMESH_CHKERR(
ierr);
712 template <
typename T>
714 const std::vector<numeric_index_type> & send_list)
const 728 this->_restore_array();
731 PetscVector<T> * v_local = cast_ptr<PetscVector<T> *>(&v_local_in);
733 libmesh_assert(v_local);
734 libmesh_assert_equal_to (v_local->
size(), this->size());
735 libmesh_assert_less_equal (send_list.size(), v_local->
size());
737 PetscErrorCode
ierr=0;
739 cast_int<numeric_index_type>(send_list.size());
744 std::vector<PetscInt>
idx(n_sl + this->local_size());
747 idx[i] = static_cast<PetscInt>(send_list[i]);
749 idx[n_sl+i] = i + this->first_local_index();
752 PetscInt * idxptr =
idx.empty() ? nullptr :
idx.data();
753 ierr = ISCreateLibMesh(this->comm().
get(), n_sl+this->local_size(),
755 LIBMESH_CHKERR(
ierr);
757 ierr = LibMeshVecScatterCreate(_vec, is,
760 LIBMESH_CHKERR(
ierr);
764 ierr = VecScatterBegin(scatter, _vec, v_local->
_vec,
765 INSERT_VALUES, SCATTER_FORWARD);
766 LIBMESH_CHKERR(
ierr);
768 ierr = VecScatterEnd (scatter, _vec, v_local->
_vec,
769 INSERT_VALUES, SCATTER_FORWARD);
770 LIBMESH_CHKERR(
ierr);
773 ierr = LibMeshISDestroy (&is);
774 LIBMESH_CHKERR(
ierr);
776 ierr = LibMeshVecScatterDestroy(&scatter);
777 LIBMESH_CHKERR(
ierr);
786 template <
typename T>
788 const std::vector<numeric_index_type> & indices)
const 791 PetscErrorCode
ierr = 0;
795 ierr = VecCreateSeq(PETSC_COMM_SELF, cast_int<PetscInt>(indices.size()), &dest);
796 LIBMESH_CHKERR(
ierr);
804 ierr = ISCreateLibMesh(this->comm().
get(), cast_int<PetscInt>(indices.size()), idxptr,
806 LIBMESH_CHKERR(
ierr);
810 ierr = LibMeshVecScatterCreate(_vec,
815 LIBMESH_CHKERR(
ierr);
818 ierr = VecScatterBegin(scatter, _vec, dest, INSERT_VALUES, SCATTER_FORWARD);
819 LIBMESH_CHKERR(
ierr);
821 ierr = VecScatterEnd(scatter, _vec, dest, INSERT_VALUES, SCATTER_FORWARD);
822 LIBMESH_CHKERR(
ierr);
825 PetscScalar * values;
826 ierr = VecGetArray (dest, &values);
827 LIBMESH_CHKERR(
ierr);
832 v_local.reserve(indices.size());
834 v_local.insert(v_local.begin(), values, values+indices.size());
837 ierr = VecRestoreArray (dest, &values);
838 LIBMESH_CHKERR(
ierr);
841 ierr = LibMeshVecScatterDestroy(&scatter);
842 LIBMESH_CHKERR(
ierr);
844 ierr = LibMeshISDestroy (&is);
845 LIBMESH_CHKERR(
ierr);
847 ierr = LibMeshVecDestroy(&dest);
848 LIBMESH_CHKERR(
ierr);
853 template <
typename T>
856 const std::vector<numeric_index_type> & send_list)
858 this->_restore_array();
860 libmesh_assert_less_equal (send_list.size(), this->size());
861 libmesh_assert_less_equal (last_local_idx+1, this->size());
865 PetscErrorCode
ierr=0;
871 if (this->n_processors() == 1)
879 parallel_vec.
init (my_size, my_local_size,
true,
PARALLEL);
888 std::vector<PetscInt>
idx(my_local_size);
892 ierr = ISCreateLibMesh(this->comm().
get(), my_local_size,
894 LIBMESH_CHKERR(
ierr);
896 ierr = LibMeshVecScatterCreate(_vec, is,
897 parallel_vec.
_vec, is,
899 LIBMESH_CHKERR(
ierr);
901 ierr = VecScatterBegin(scatter, _vec, parallel_vec.
_vec,
902 INSERT_VALUES, SCATTER_FORWARD);
903 LIBMESH_CHKERR(
ierr);
905 ierr = VecScatterEnd (scatter, _vec, parallel_vec.
_vec,
906 INSERT_VALUES, SCATTER_FORWARD);
907 LIBMESH_CHKERR(
ierr);
910 ierr = LibMeshISDestroy (&is);
911 LIBMESH_CHKERR(
ierr);
913 ierr = LibMeshVecScatterDestroy(&scatter);
914 LIBMESH_CHKERR(
ierr);
918 parallel_vec.
close();
919 parallel_vec.
localize (*
this, send_list);
925 template <
typename T>
928 this->_restore_array();
931 parallel_object_only();
933 PetscErrorCode
ierr=0;
934 const PetscInt n = this->size();
935 const PetscInt nl = this->local_size();
936 PetscScalar * values;
939 v_local.resize(n, 0.);
941 ierr = VecGetArray (_vec, &values);
942 LIBMESH_CHKERR(
ierr);
946 for (PetscInt i=0; i<nl; i++)
947 v_local[i+ioff] = static_cast<T>(values[i]);
949 ierr = VecRestoreArray (_vec, &values);
950 LIBMESH_CHKERR(
ierr);
952 this->comm().sum(v_local);
958 #ifdef LIBMESH_USE_REAL_NUMBERS 964 this->_restore_array();
966 PetscErrorCode
ierr=0;
967 const PetscInt n = size();
968 const PetscInt nl = local_size();
969 PetscScalar * values;
973 if (n_processors() == 1)
977 ierr = VecGetArray (_vec, &values);
978 LIBMESH_CHKERR(
ierr);
980 for (PetscInt i=0; i<n; i++)
981 v_local[i] = static_cast<Real>(values[i]);
983 ierr = VecRestoreArray (_vec, &values);
984 LIBMESH_CHKERR(
ierr);
995 ierr = VecScatterCreateToZero(_vec, &ctx, &vout);
996 LIBMESH_CHKERR(
ierr);
998 ierr = VecScatterBegin(ctx, _vec, vout, INSERT_VALUES, SCATTER_FORWARD);
999 LIBMESH_CHKERR(
ierr);
1000 ierr = VecScatterEnd(ctx, _vec, vout, INSERT_VALUES, SCATTER_FORWARD);
1001 LIBMESH_CHKERR(
ierr);
1003 if (processor_id() == 0)
1007 ierr = VecGetArray (vout, &values);
1008 LIBMESH_CHKERR(
ierr);
1010 for (PetscInt i=0; i<n; i++)
1011 v_local[i] = static_cast<Real>(values[i]);
1013 ierr = VecRestoreArray (vout, &values);
1014 LIBMESH_CHKERR(
ierr);
1017 ierr = LibMeshVecScatterDestroy(&ctx);
1018 LIBMESH_CHKERR(
ierr);
1019 ierr = LibMeshVecDestroy(&vout);
1020 LIBMESH_CHKERR(
ierr);
1028 std::vector<Real> local_values (n, 0.);
1031 ierr = VecGetArray (_vec, &values);
1032 LIBMESH_CHKERR(
ierr);
1034 for (PetscInt i=0; i<nl; i++)
1035 local_values[i+ioff] = static_cast<Real>(values[i]);
1037 ierr = VecRestoreArray (_vec, &values);
1038 LIBMESH_CHKERR(
ierr);
1042 MPI_Reduce (local_values.data(), v_local.data(), n, MPI_REAL, MPI_SUM,
1043 pid, this->comm().get());
1052 #ifdef LIBMESH_USE_COMPLEX_NUMBERS 1058 this->_restore_array();
1060 PetscErrorCode
ierr=0;
1061 const PetscInt n = size();
1062 const PetscInt nl = local_size();
1063 PetscScalar * values;
1069 for (PetscInt i=0; i<n; i++)
1073 if (n_processors() == 1)
1075 ierr = VecGetArray (_vec, &values);
1076 LIBMESH_CHKERR(
ierr);
1078 for (PetscInt i=0; i<n; i++)
1079 v_local[i] = static_cast<Complex>(values[i]);
1081 ierr = VecRestoreArray (_vec, &values);
1082 LIBMESH_CHKERR(
ierr);
1093 std::vector<Real> real_local_values(n, 0.);
1094 std::vector<Real> imag_local_values(n, 0.);
1097 ierr = VecGetArray (_vec, &values);
1098 LIBMESH_CHKERR(
ierr);
1101 for (PetscInt i=0; i<nl; i++)
1103 real_local_values[i+ioff] =
static_cast<Complex>(values[i]).real();
1104 imag_local_values[i+ioff] =
static_cast<Complex>(values[i]).imag();
1107 ierr = VecRestoreArray (_vec, &values);
1108 LIBMESH_CHKERR(
ierr);
1116 std::vector<Real> real_v_local(n);
1117 std::vector<Real> imag_v_local(n);
1120 MPI_Reduce (real_local_values.data(),
1121 real_v_local.data(), n,
1123 pid, this->comm().get());
1125 MPI_Reduce (imag_local_values.data(),
1126 imag_v_local.data(), n,
1128 pid, this->comm().get());
1131 for (PetscInt i=0; i<n; i++)
1132 v_local[i] =
Complex(real_v_local[i], imag_v_local[i]);
1140 template <
typename T>
1144 this->_restore_array();
1146 PetscErrorCode
ierr = 0;
1149 const PetscVector<T> * vec1_petsc = cast_ptr<const PetscVector<T> *>(&vec1);
1150 const PetscVector<T> * vec2_petsc = cast_ptr<const PetscVector<T> *>(&vec2);
1156 ierr = VecPointwiseMult(this->vec(),
1159 LIBMESH_CHKERR(
ierr);
1166 ierr = VecGhostGetLocalForm (_vec,&loc_vec);
1167 LIBMESH_CHKERR(
ierr);
1168 ierr = VecGhostGetLocalForm (
const_cast<PetscVector<T> *
>(vec1_petsc)->vec(),&v1_loc_vec);
1169 LIBMESH_CHKERR(
ierr);
1170 ierr = VecGhostGetLocalForm (
const_cast<PetscVector<T> *
>(vec2_petsc)->vec(),&v2_loc_vec);
1171 LIBMESH_CHKERR(
ierr);
1173 ierr = VecPointwiseMult(loc_vec,v1_loc_vec,v2_loc_vec);
1174 LIBMESH_CHKERR(
ierr);
1176 ierr = VecGhostRestoreLocalForm (
const_cast<PetscVector<T> *
>(vec1_petsc)->vec(),&v1_loc_vec);
1177 LIBMESH_CHKERR(
ierr);
1178 ierr = VecGhostRestoreLocalForm (
const_cast<PetscVector<T> *
>(vec2_petsc)->vec(),&v2_loc_vec);
1179 LIBMESH_CHKERR(
ierr);
1180 ierr = VecGhostRestoreLocalForm (_vec,&loc_vec);
1181 LIBMESH_CHKERR(
ierr);
1187 template <
typename T>
1190 this->_restore_array();
1191 libmesh_assert (this->
closed());
1193 PetscErrorCode
ierr=0;
1194 PetscViewer petsc_viewer;
1197 ierr = PetscViewerCreate (this->comm().
get(),
1199 LIBMESH_CHKERR(
ierr);
1207 ierr = PetscViewerASCIIOpen( this->comm().
get(),
1210 LIBMESH_CHKERR(
ierr);
1212 #if PETSC_VERSION_LESS_THAN(3,7,0) 1213 ierr = PetscViewerSetFormat (petsc_viewer,
1214 PETSC_VIEWER_ASCII_MATLAB);
1216 ierr = PetscViewerPushFormat (petsc_viewer,
1217 PETSC_VIEWER_ASCII_MATLAB);
1220 LIBMESH_CHKERR(
ierr);
1222 ierr = VecView (_vec, petsc_viewer);
1223 LIBMESH_CHKERR(
ierr);
1232 #if PETSC_VERSION_LESS_THAN(3,7,0) 1233 ierr = PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
1234 PETSC_VIEWER_ASCII_MATLAB);
1236 ierr = PetscViewerPushFormat (PETSC_VIEWER_STDOUT_WORLD,
1237 PETSC_VIEWER_ASCII_MATLAB);
1240 LIBMESH_CHKERR(
ierr);
1242 ierr = VecView (_vec, PETSC_VIEWER_STDOUT_WORLD);
1243 LIBMESH_CHKERR(
ierr);
1250 ierr = LibMeshPetscViewerDestroy (&petsc_viewer);
1251 LIBMESH_CHKERR(
ierr);
1258 template <
typename T>
1260 const std::vector<numeric_index_type> & rows)
const 1262 this->_restore_array();
1265 IS parent_is, subvector_is;
1267 PetscErrorCode
ierr = 0;
1270 PetscVector<T> * petsc_subvector = cast_ptr<PetscVector<T> *>(&subvector);
1283 ierr = VecCreateMPI(this->comm().
get(),
1285 cast_int<PetscInt>(rows.size()),
1286 &(petsc_subvector->
_vec));
1287 LIBMESH_CHKERR(
ierr);
1289 ierr = VecSetFromOptions (petsc_subvector->
_vec);
1290 LIBMESH_CHKERR(
ierr);
1301 std::vector<PetscInt>
idx(rows.size());
1305 ierr = ISCreateLibMesh(this->comm().
get(),
1306 cast_int<PetscInt>(rows.size()),
1310 LIBMESH_CHKERR(
ierr);
1312 ierr = ISCreateLibMesh(this->comm().
get(),
1313 cast_int<PetscInt>(rows.size()),
1317 LIBMESH_CHKERR(
ierr);
1320 ierr = LibMeshVecScatterCreate(this->_vec,
1322 petsc_subvector->
_vec,
1324 &scatter); LIBMESH_CHKERR(
ierr);
1327 ierr = VecScatterBegin(scatter,
1329 petsc_subvector->
_vec,
1331 SCATTER_FORWARD); LIBMESH_CHKERR(
ierr);
1333 ierr = VecScatterEnd(scatter,
1335 petsc_subvector->
_vec,
1337 SCATTER_FORWARD); LIBMESH_CHKERR(
ierr);
1340 ierr = LibMeshISDestroy(&parent_is); LIBMESH_CHKERR(
ierr);
1341 ierr = LibMeshISDestroy(&subvector_is); LIBMESH_CHKERR(
ierr);
1342 ierr = LibMeshVecScatterDestroy(&scatter); LIBMESH_CHKERR(
ierr);
1347 template <
typename T>
1352 #ifdef LIBMESH_HAVE_CXX11_THREAD 1353 std::atomic_thread_fence(std::memory_order_acquire);
1355 Threads::spin_mutex::scoped_lock lock(_petsc_vector_mutex);
1362 if (_array_is_present && !read_only && _values_read_only)
1367 if (_array_is_present && read_only && !_values_read_only)
1368 _read_only_values = _values;
1370 if (!_array_is_present)
1372 #ifdef LIBMESH_HAVE_CXX11_THREAD 1373 std::lock_guard<std::mutex> lock(_petsc_vector_mutex);
1375 if (!_array_is_present)
1377 PetscErrorCode
ierr=0;
1380 #if PETSC_VERSION_LESS_THAN(3,2,0) 1384 ierr = VecGetArray(_vec, const_cast<PetscScalar **>(&_values));
1385 _values_read_only =
false;
1389 ierr = VecGetArrayRead(_vec, &_read_only_values);
1390 _values_read_only =
true;
1394 ierr = VecGetArray(_vec, &_values);
1395 _values_read_only =
false;
1398 LIBMESH_CHKERR(
ierr);
1399 _local_size = this->local_size();
1403 ierr = VecGhostGetLocalForm (_vec,&_local_form);
1404 LIBMESH_CHKERR(
ierr);
1406 #if PETSC_VERSION_LESS_THAN(3,2,0) 1410 ierr = VecGetArray(_local_form, const_cast<PetscScalar **>(&_values));
1411 _values_read_only =
false;
1415 ierr = VecGetArrayRead(_local_form, &_read_only_values);
1416 _values_read_only =
true;
1420 ierr = VecGetArray(_local_form, &_values);
1421 _values_read_only =
false;
1424 LIBMESH_CHKERR(
ierr);
1426 PetscInt my_local_size = 0;
1427 ierr = VecGetLocalSize(_local_form, &my_local_size);
1428 LIBMESH_CHKERR(
ierr);
1433 PetscInt petsc_first=0, petsc_last=0;
1434 ierr = VecGetOwnershipRange (_vec, &petsc_first, &petsc_last);
1435 LIBMESH_CHKERR(
ierr);
1439 #ifdef LIBMESH_HAVE_CXX11_THREAD 1440 std::atomic_thread_fence(std::memory_order_release);
1441 _array_is_present.store(
true, std::memory_order_relaxed);
1443 _array_is_present =
true;
1451 template <
typename T>
1454 if (_values_manually_retrieved)
1455 libmesh_error_msg(
"PetscVector values were manually retrieved but are being automatically restored!");
1457 #ifdef LIBMESH_HAVE_CXX11_THREAD 1458 std::atomic_thread_fence(std::memory_order_acquire);
1460 Threads::spin_mutex::scoped_lock lock(_petsc_vector_mutex);
1464 if (_array_is_present)
1466 #ifdef LIBMESH_HAVE_CXX11_THREAD 1467 std::lock_guard<std::mutex> lock(_petsc_vector_mutex);
1470 if (_array_is_present)
1472 PetscErrorCode
ierr=0;
1475 #if PETSC_VERSION_LESS_THAN(3,2,0) 1479 ierr = VecRestoreArray (_vec, const_cast<PetscScalar **>(&_values));
1481 if (_values_read_only)
1482 ierr = VecRestoreArrayRead (_vec, &_read_only_values);
1484 ierr = VecRestoreArray (_vec, &_values);
1487 LIBMESH_CHKERR(
ierr);
1492 #if PETSC_VERSION_LESS_THAN(3,2,0) 1496 ierr = VecRestoreArray (_local_form, const_cast<PetscScalar **>(&_values));
1498 if (_values_read_only)
1499 ierr = VecRestoreArrayRead (_local_form, &_read_only_values);
1501 ierr = VecRestoreArray (_local_form, &_values);
1503 LIBMESH_CHKERR(
ierr);
1505 ierr = VecGhostRestoreLocalForm (_vec,&_local_form);
1506 LIBMESH_CHKERR(
ierr);
1507 _local_form =
nullptr;
1510 #ifdef LIBMESH_HAVE_CXX11_THREAD 1511 std::atomic_thread_fence(std::memory_order_release);
1512 _array_is_present.store(
false, std::memory_order_relaxed);
1514 _array_is_present =
false;
1531 #endif // #ifdef LIBMESH_HAVE_PETSC std::string name(const ElemQuality q)
virtual Real l2_norm() const override
virtual void localize(std::vector< T > &v_local) const override
NumericVector interface to PETSc Vec.
void add_vector_conjugate_transpose(const NumericVector< T > &v, const SparseMatrix< T > &A)
virtual void add_vector_transpose(const NumericVector< T > &v, const SparseMatrix< T > &A) override
virtual numeric_index_type size() const override
virtual Real linfty_norm() const override
virtual bool initialized() const
void _get_array(bool read_only) const
virtual numeric_index_type local_size() const override
virtual void add(const numeric_index_type i, const T value) override
uint8_t processor_id_type
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
void iota(ForwardIter first, ForwardIter last, T value)
void _restore_array() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
virtual void insert(const T *v, const std::vector< numeric_index_type > &dof_indices) override
virtual void pointwise_mult(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
virtual Real l1_norm() const override
virtual void abs() override
dof_id_type numeric_index_type
virtual NumericVector< T > & operator+=(const NumericVector< T > &v) override
virtual void conjugate() override
virtual void localize_to_one(std::vector< T > &v_local, const processor_id_type proc_id=0) const override
virtual void create_subvector(NumericVector< T > &subvector, const std::vector< numeric_index_type > &rows) const override
virtual bool closed() const
virtual T sum() const override
std::complex< Real > Complex
ParallelType type() const
virtual void set(const numeric_index_type i, const T value) override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
T indefinite_dot(const NumericVector< T > &v) const
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType type=AUTOMATIC) override
static PetscErrorCode Mat * A
SparseMatrix interface to PETSc Mat.
PetscVector< T > & operator=(const PetscVector< T > &v)
virtual void reciprocal() override
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices) override
virtual void print_matlab(const std::string &name="") const override
virtual void scale(const T factor) override
virtual void close() override
virtual NumericVector< T > & operator/=(const NumericVector< T > &v) override
virtual NumericVector< T > & operator-=(const NumericVector< T > &v) override
virtual T dot(const NumericVector< T > &v) const override