Skip to content

Commit d2993f8

Browse files
authored
Merge pull request #3443 from grmnptr/piecewise-divide
Add pointwise_divide to PetscVector
2 parents 09d0e4f + 46c636d commit d2993f8

10 files changed

Lines changed: 92 additions & 2 deletions

File tree

include/numerics/distributed_vector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ class DistributedVector final : public NumericVector<T>
226226
virtual void pointwise_mult (const NumericVector<T> & vec1,
227227
const NumericVector<T> & vec2) override;
228228

229+
virtual void pointwise_divide (const NumericVector<T> & vec1,
230+
const NumericVector<T> & vec2) override;
231+
229232
virtual void swap (NumericVector<T> & v) override;
230233

231234
virtual std::size_t max_allowed_id() const override;

include/numerics/eigen_sparse_vector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ class EigenSparseVector final : public NumericVector<T>
228228
virtual void pointwise_mult (const NumericVector<T> & vec1,
229229
const NumericVector<T> & vec2) override;
230230

231+
virtual void pointwise_divide (const NumericVector<T> & vec1,
232+
const NumericVector<T> & vec2) override;
233+
231234
virtual void swap (NumericVector<T> & v) override;
232235

233236
virtual std::size_t max_allowed_id() const override;

include/numerics/laspack_vector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ class LaspackVector final : public NumericVector<T>
226226
virtual void pointwise_mult (const NumericVector<T> & vec1,
227227
const NumericVector<T> & vec2) override;
228228

229+
virtual void pointwise_divide (const NumericVector<T> & vec1,
230+
const NumericVector<T> & vec2) override;
231+
229232
virtual void swap (NumericVector<T> & v) override;
230233

231234
virtual std::size_t max_allowed_id() const override;

include/numerics/numeric_vector.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,14 @@ class NumericVector : public ReferenceCountedObject<NumericVector<T>>,
671671
virtual void pointwise_mult (const NumericVector<T> & vec1,
672672
const NumericVector<T> & vec2) = 0;
673673

674+
/**
675+
* Computes \f$ u_i \leftarrow \frac{v_{1,i}}{v_{2,i}} \f$ (summation not implied)
676+
* i.e. the pointwise (component-wise) division of \p vec1 and
677+
* \p vec2, and stores the result in \p *this.
678+
*/
679+
virtual void pointwise_divide (const NumericVector<T> & vec1,
680+
const NumericVector<T> & vec2) = 0;
681+
674682
/**
675683
* Prints the local contents of the vector, by default to
676684
* libMesh::out

include/numerics/petsc_vector.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ class PetscVector final : public NumericVector<T>
321321
virtual void pointwise_mult (const NumericVector<T> & vec1,
322322
const NumericVector<T> & vec2) override;
323323

324+
virtual void pointwise_divide (const NumericVector<T> & vec1,
325+
const NumericVector<T> & vec2) override;
326+
324327
virtual void print_matlab(const std::string & name = "") const override;
325328

326329
virtual void create_subvector(NumericVector<T> & subvector,

src/numerics/distributed_vector.C

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,12 @@ void DistributedVector<T>::pointwise_mult (const NumericVector<T> &,
619619
libmesh_not_implemented();
620620
}
621621

622-
622+
template <typename T>
623+
void DistributedVector<T>::pointwise_divide (const NumericVector<T> &,
624+
const NumericVector<T> &)
625+
{
626+
libmesh_not_implemented();
627+
}
623628

624629
//--------------------------------------------------------------
625630
// Explicit instantiations

src/numerics/eigen_sparse_vector.C

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ void EigenSparseVector<T>::pointwise_mult (const NumericVector<T> & /*vec1*/,
418418
libmesh_not_implemented();
419419
}
420420

421+
template <typename T>
422+
void EigenSparseVector<T>::pointwise_divide (const NumericVector<T> & /*vec1*/,
423+
const NumericVector<T> & /*vec2*/)
424+
{
425+
libmesh_not_implemented();
426+
}
421427

422428

423429
template <typename T>

src/numerics/laspack_vector.C

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,12 @@ void LaspackVector<T>::pointwise_mult (const NumericVector<T> & /*vec1*/,
482482
libmesh_not_implemented();
483483
}
484484

485-
485+
template <typename T>
486+
void LaspackVector<T>::pointwise_divide (const NumericVector<T> & /*vec1*/,
487+
const NumericVector<T> & /*vec2*/)
488+
{
489+
libmesh_not_implemented();
490+
}
486491

487492
template <typename T>
488493
Real LaspackVector<T>::max() const

src/numerics/petsc_vector.C

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,24 @@ void PetscVector<T>::pointwise_mult (const NumericVector<T> & vec1,
10221022
this->_is_closed = true;
10231023
}
10241024

1025+
template <typename T>
1026+
void PetscVector<T>::pointwise_divide (const NumericVector<T> & vec1,
1027+
const NumericVector<T> & vec2)
1028+
{
1029+
this->_restore_array();
1030+
1031+
// Convert arguments to PetscVector*.
1032+
const PetscVector<T> * const vec1_petsc = cast_ptr<const PetscVector<T> *>(&vec1);
1033+
const PetscVector<T> * const vec2_petsc = cast_ptr<const PetscVector<T> *>(&vec2);
1034+
1035+
// Call PETSc function.
1036+
PetscErrorCode ierr = VecPointwiseDivide(_vec, vec1_petsc->vec(), vec2_petsc->vec());
1037+
LIBMESH_CHKERR(ierr);
1038+
if (this->type() == GHOSTED)
1039+
VecGhostUpdateBeginEnd(this->comm(), _vec, INSERT_VALUES, SCATTER_FORWARD);
10251040

1041+
this->_is_closed = true;
1042+
}
10261043

10271044
template <typename T>
10281045
void PetscVector<T>::print_matlab (const std::string & name) const

tests/numerics/petsc_vector_test.C

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public:
2323

2424
CPPUNIT_TEST( testGetArray );
2525

26+
CPPUNIT_TEST( testPetscOperations );
27+
2628
CPPUNIT_TEST_SUITE_END();
2729

2830
void testGetArray()
@@ -77,6 +79,41 @@ public:
7779
v.restore_array();
7880
}
7981

82+
void testPetscOperations()
83+
{
84+
PetscVector<Number> v1(*my_comm, global_size, local_size);
85+
auto v2 = v1.clone();
86+
87+
const libMesh::dof_id_type
88+
first = v1.first_local_index(),
89+
last = v1.last_local_index();
90+
91+
for (libMesh::dof_id_type n=first; n != last; n++)
92+
{
93+
v1.set (n, static_cast<libMesh::Number>(n+1));
94+
v2->set (n, static_cast<libMesh::Number>(2*(n+1)));
95+
}
96+
v1.close();
97+
v2->close();
98+
99+
auto v_working_ptr = v1.clone();
100+
auto & v_working = *v_working_ptr;
101+
102+
v_working.pointwise_mult(v1, *v2);
103+
104+
for (libMesh::dof_id_type n=first; n != last; n++)
105+
LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v_working(n)),
106+
libMesh::Real((n+1)*2*(n+1)),
107+
libMesh::TOLERANCE*libMesh::TOLERANCE);
108+
109+
v_working.pointwise_divide(v1, *v2);
110+
111+
for (libMesh::dof_id_type n=first; n != last; n++)
112+
LIBMESH_ASSERT_FP_EQUAL(libMesh::libmesh_real(v_working(n)),
113+
libMesh::Real(0.5),
114+
libMesh::TOLERANCE*libMesh::TOLERANCE);
115+
}
116+
80117
};
81118

82119
CPPUNIT_TEST_SUITE_REGISTRATION( PetscVectorTest );

0 commit comments

Comments
 (0)