Skip to content

Commit 2a4aedf

Browse files
authored
Merge pull request #4047 from farscape-project/lX_norm
Refactor some code
2 parents 2657abb + 5dba51c commit 2a4aedf

6 files changed

Lines changed: 47 additions & 51 deletions

File tree

include/base/dof_map.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ class DofMap : public ReferenceCountedObject<DofMap>,
719719
* \returns The number of degrees of freedom on each partition for a
720720
* particular variable \p vn.
721721
*/
722-
std::vector<dof_id_type> n_dofs_on_each_processor(const unsigned int vn) const
722+
std::vector<dof_id_type> n_dofs_per_processor(const unsigned int vn) const
723723
{
724724
std::vector<dof_id_type> n_local_dofs(this->n_processors(), 0);
725725
this->comm().allgather(this->n_local_dofs(vn), n_local_dofs);

include/numerics/petsc_matrix.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ class PetscMatrix final : public PetscMatrixBase<T>
242242

243243
virtual Real l1_norm () const override;
244244

245+
virtual Real frobenius_norm () const;
246+
245247
virtual Real linfty_norm () const override;
246248

247249
/**
@@ -360,6 +362,14 @@ class PetscMatrix final : public PetscMatrixBase<T>
360362
mutable Threads::spin_mutex _petsc_matrix_mutex;
361363
#endif
362364

365+
/**
366+
* \returns A norm of the matrix, where the type of norm to compute is
367+
* determined by the template parameter N of the PETSc-defined type NormType.
368+
* The valid template arguments are NORM_1, NORM_FROBENIUS and NORM_INFINITY,
369+
* as used to define l1_norm(), frobenius_norm() and linfty_norm().
370+
*/
371+
template <NormType N> Real norm () const;
372+
363373
friend class ::PetscMatrixTest;
364374
};
365375

include/numerics/petsc_vector.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,14 @@ class PetscVector final : public NumericVector<T>
465465
* Whether or not the data array is for read only access
466466
*/
467467
mutable bool _values_read_only;
468+
469+
/**
470+
* \returns A norm of the vector, where the type of norm to compute is
471+
* determined by the template parameter N of the PETSc-defined type NormType.
472+
* The valid template arguments are NORM_1, NORM_2 and NORM_INFINITY, as used
473+
* to define l1_norm(), l2_norm() and linfty_norm().
474+
*/
475+
template <NormType N> Real norm () const;
468476
};
469477

470478

src/numerics/petsc_matrix.C

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ std::unique_ptr<SparseMatrix<T>> PetscMatrix<T>::clone () const
466466

467467

468468
template <typename T>
469-
Real PetscMatrix<T>::l1_norm () const
469+
template <NormType N>
470+
Real PetscMatrix<T>::norm () const
470471
{
471472
libmesh_assert (this->initialized());
472473

@@ -477,32 +478,26 @@ Real PetscMatrix<T>::l1_norm () const
477478

478479
libmesh_assert (this->closed());
479480

480-
LibmeshPetscCall(MatNorm(this->_mat, NORM_1, &petsc_value));
481+
LibmeshPetscCall(MatNorm(this->_mat, N, &petsc_value));
481482

482483
value = static_cast<Real>(petsc_value);
483484

484485
return value;
485486
}
486-
487-
488-
487+
template <typename T>
488+
Real PetscMatrix<T>::l1_norm () const
489+
{
490+
return PetscMatrix<T>::norm<NORM_1>();
491+
}
492+
template <typename T>
493+
Real PetscMatrix<T>::frobenius_norm () const
494+
{
495+
return PetscMatrix<T>::norm<NORM_FROBENIUS>();
496+
}
489497
template <typename T>
490498
Real PetscMatrix<T>::linfty_norm () const
491499
{
492-
libmesh_assert (this->initialized());
493-
494-
semiparallel_only();
495-
496-
PetscReal petsc_value;
497-
Real value;
498-
499-
libmesh_assert (this->closed());
500-
501-
LibmeshPetscCall(MatNorm(this->_mat, NORM_INFINITY, &petsc_value));
502-
503-
value = static_cast<Real>(petsc_value);
504-
505-
return value;
500+
return PetscMatrix<T>::norm<NORM_INFINITY>();
506501
}
507502

508503

src/numerics/petsc_vector.C

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,57 +54,40 @@ T PetscVector<T>::sum () const
5454
}
5555

5656

57+
5758
template <typename T>
58-
Real PetscVector<T>::l1_norm () const
59+
template <NormType N>
60+
Real PetscVector<T>::norm () const
5961
{
62+
parallel_object_only();
63+
6064
this->_restore_array();
6165
libmesh_assert(this->closed());
6266

6367
PetscReal value=0.;
6468

65-
LibmeshPetscCall(VecNorm (_vec, NORM_1, &value));
69+
LibmeshPetscCall(VecNorm (_vec, N, &value));
6670

6771
return static_cast<Real>(value);
6872
}
69-
70-
71-
73+
template <typename T>
74+
Real PetscVector<T>::l1_norm () const
75+
{
76+
return PetscVector<T>::norm<NORM_1>();
77+
}
7278
template <typename T>
7379
Real PetscVector<T>::l2_norm () const
7480
{
75-
parallel_object_only();
76-
77-
this->_restore_array();
78-
libmesh_assert(this->closed());
79-
80-
PetscReal value=0.;
81-
82-
LibmeshPetscCall(VecNorm (_vec, NORM_2, &value));
83-
84-
return static_cast<Real>(value);
81+
return PetscVector<T>::norm<NORM_2>();
8582
}
86-
87-
88-
89-
9083
template <typename T>
9184
Real PetscVector<T>::linfty_norm () const
9285
{
93-
parallel_object_only();
94-
95-
this->_restore_array();
96-
libmesh_assert(this->closed());
97-
98-
PetscReal value=0.;
99-
100-
LibmeshPetscCall(VecNorm (_vec, NORM_INFINITY, &value));
101-
102-
return static_cast<Real>(value);
86+
return PetscVector<T>::norm<NORM_INFINITY>();
10387
}
10488

10589

10690

107-
10891
template <typename T>
10992
NumericVector<T> &
11093
PetscVector<T>::operator += (const NumericVector<T> & v)

tests/systems/systems_test.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ public:
604604
for (const Node * node : mesh.node_ptr_range())
605605
CPPUNIT_ASSERT_EQUAL(dof_id_type(node->n_vars(0)), n_dofs);
606606

607-
std::vector<dof_id_type> each = sys.get_dof_map().n_dofs_on_each_processor(888);
607+
std::vector<dof_id_type> each = sys.get_dof_map().n_dofs_per_processor(888);
608608
CPPUNIT_ASSERT_EQUAL(std::accumulate(each.begin(), each.end(), dof_id_type(0)), dof_id_type(5));
609609
CPPUNIT_ASSERT_EQUAL(sys.get_dof_map().n_dofs(888), dof_id_type(5));
610610
}

0 commit comments

Comments
 (0)