chombo-discharge
Loading...
Searching...
No Matches
PetscStencil Class Reference

A linear stencil over global PETSc rows, i.e. a list of (row, weight) pairs. More...

#include <CD_PetscStencil.H>

Public Member Functions

 PetscStencil ()=default
 Default constructor. Creates an empty stencil.
 ~PetscStencil ()=default
 Destructor.
void clear ()
 Remove all entries from the stencil.
int size () const noexcept
 Get the number of entries in the stencil.
PetscInt row (const int a_entry) const noexcept
 Get the PETSc row of a stencil entry.
PetscScalar weight (const int a_entry) const noexcept
 Get the weight of a stencil entry.
void add (const PetscInt &a_row, const PetscScalar &a_weight) noexcept
 Append an entry to this stencil.
PetscStencil & operator+= (const PetscStencil &a_otherStencil) noexcept
 Append all entries of another stencil to this stencil.
void compress () noexcept
 Sort the entries by row and merge entries with the same row by summing their weights.
PetscStencil & operator*= (const PetscScalar &a_scalingFactor) noexcept
 Scale all weights in this stencil.

Protected Attributes

std::vector< std::pair< PetscInt, PetscScalar > > m_entries
 Stencil entries as (global PETSc row, weight). Rows are unique and ascending only after compress().

Detailed Description

A linear stencil over global PETSc rows, i.e. a list of (row, weight) pairs.

This plays the role that VoFStencil plays for Chombo data, but each entry is a global PETSc row rather than a VolIndex. A VoFStencil entry is only meaningful together with the level and grid patch it was built on, whereas a PetscStencil entry can be passed directly to MatSetValues.

Entries are appended by add() and operator+= without looking for an existing entry with the same row, so a row can appear more than once until compress() is called. compress() sorts the entries by row and merges duplicates, which makes the rows unique and ascending. Duplicates are summed in the order they were added, so the result is the same as merging on every add, bit for bit. Call compress() before passing the entries to MatSetValues: duplicate rows would be summed with ADD_VALUES but would overwrite each other with INSERT_VALUES. Building a stencil of N entries this way costs O(N log N), compared with O(N^2) for merging on every add; at a hundred entries the difference is roughly a factor of three to five.

Member Function Documentation

◆ add()

void PetscStencil::add ( const PetscInt & a_row,
const PetscScalar & a_weight )
noexcept

Append an entry to this stencil.

The entry is not merged with an existing entry for the same row; call compress() for that.

Parameters
[in]a_rowGlobal PETSc row
[in]a_weightWeight of the entry

◆ compress()

void PetscStencil::compress ( )
noexcept

Sort the entries by row and merge entries with the same row by summing their weights.

After this the rows are unique and in ascending order, which is the cheapest order for MatSetValues. The sort is stable, so the weights of a row are summed in the order they were added.

◆ operator*=()

PetscStencil & PetscStencil::operator*= ( const PetscScalar & a_scalingFactor)
noexcept

Scale all weights in this stencil.

Parameters
[in]a_scalingFactorScaling factor
Returns
This stencil.

◆ operator+=()

PetscStencil & PetscStencil::operator+= ( const PetscStencil & a_otherStencil)
noexcept

Append all entries of another stencil to this stencil.

Entries are not merged with existing entries for the same row; call compress() for that.

Parameters
[in]a_otherStencilOther PETSc stencil
Returns
This stencil.

◆ row()

PetscInt PetscStencil::row ( const int a_entry) const
noexcept

Get the PETSc row of a stencil entry.

Parameters
[in]a_entryEntry index, in the range [0, size()).
Returns
Global PETSc row of the entry.

◆ size()

int PetscStencil::size ( ) const
noexcept

Get the number of entries in the stencil.

Returns
Number of (row, weight) entries.

◆ weight()

PetscScalar PetscStencil::weight ( const int a_entry) const
noexcept

Get the weight of a stencil entry.

Parameters
[in]a_entryEntry index, in the range [0, size()).
Returns
Weight of the entry.

The documentation for this class was generated from the following files: