No Matches

Create and destroy PETSc objects with smart pointer.

Create and destroy PETSc objects with smart pointer

/** \file petsc_smart_ptr_objects.cpp
* \example petsc_smart_ptr_objects.cpp
* \brief Create and destroy PETSc objects with smart pointer
#include <MoFEM.hpp>
using namespace MoFEM;
static char help[] = "...\n\n";
int main(int argc, char *argv[]) {
MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
try {
// check is count is correct
auto check = [&](const int expected) {
if (m_ptr.use_count() != expected)
"use count should be %d but is %d", expected,
// check copy constructor
Mat m;
// Check casting on PetscObject
{ PetscObject obj = static_cast<PetscObject>(m_ptr); }
SmartPetscObj<Mat> n_ptr(m_ptr);
CHKERR check(2);
CHKERR check(1);
// check if casting works well
CHKERR MatSetSizes(m_ptr, 2, 2, 2, 2);
// nesting once
SmartPetscObj<Mat> n_ptr = m_ptr;
CHKERR check(2);
// again
SmartPetscObj<Mat> i_ptr = m_ptr;
CHKERR check(3);
CHKERR check(2);
// nesting again
SmartPetscObj<Mat> n_ptr = m_ptr;
CHKERR check(2);
// only one now
CHKERR check(1);
// reset, i.e. delete matrix
// counts should be zero now
CHKERR check(0);
return 0;
static char help[]
int main()
Definition: adol-c_atom.cpp:46
Catch errors.
Definition: definitions.h:372
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:346
Definition: definitions.h:31
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:416
#define CHKERR
Inline error check.
Definition: definitions.h:535
FTensor::Index< 'm', SPACE_DIM > m
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition: Core.cpp:72
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition: Core.cpp:112
intrusive_ptr for managing petsc objects