v0.14.0
petsc_smart_ptr_objects.cpp

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)
SETERRQ2(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
"use count should be %d but is %d", expected,
m_ptr.use_count());
};
// check copy constructor
{
Mat m;
CHKERR MatCreate(PETSC_COMM_SELF, &m);
}
// Check casting on PetscObject
{ std::ignore = 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
m_ptr.reset();
// counts should be zero now
CHKERR check(0);
{
}
}
return 0;
}
help
static char help[]
Definition: petsc_smart_ptr_objects.cpp:12
MoFEM.hpp
MoFEM::CoreTmp< 0 >::Finalize
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition: Core.cpp:112
main
int main(int argc, char *argv[])
Definition: petsc_smart_ptr_objects.cpp:14
CHKERR
#define CHKERR
Inline error check.
Definition: definitions.h:548
MoFEM::SmartPetscObj::use_count
int use_count() const
Definition: PetscSmartObj.hpp:109
MoFEM
implementation of Data Operators for Forces and Sources
Definition: Common.hpp:10
MoFEM::CoreTmp< 0 >::Initialize
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
CATCH_ERRORS
#define CATCH_ERRORS
Catch errors.
Definition: definitions.h:385
MOFEM_DATA_INCONSISTENCY
@ MOFEM_DATA_INCONSISTENCY
Definition: definitions.h:31
m
FTensor::Index< 'm', 3 > m
Definition: shallow_wave.cpp:80
MoFEM::SmartPetscObj< Mat >
MoFEMFunctionReturn
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
Definition: definitions.h:429
MoFEMFunctionBegin
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
Definition: definitions.h:359