v0.13.2
Loading...
Searching...
No Matches
petsc_smart_ptr_objects.cpp
Go to the documentation of this file.
1/** \file petsc_smart_ptr_objects.cpp
2 * \example petsc_smart_ptr_objects.cpp
3 * \brief Create and destroy PETSc objects with smart pointer
4 */
5
6
7
8#include <MoFEM.hpp>
9
10using namespace MoFEM;
11
12static char help[] = "...\n\n";
13
14int main(int argc, char *argv[]) {
15
16 MoFEM::Core::Initialize(&argc, &argv, (char *)0, help);
17
18 try {
19
21
22 // check is count is correct
23 auto check = [&](const int expected) {
25 if (m_ptr.use_count() != expected)
26 SETERRQ2(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
27 "use count should be %d but is %d", expected,
28 m_ptr.use_count());
30 };
31
32 // check copy constructor
33 {
34 Mat m;
35 CHKERR MatCreate(PETSC_COMM_SELF, &m);
36 m_ptr = SmartPetscObj<Mat>(m);
37 }
38
39 // Check casting on PetscObject
40 { PetscObject obj = static_cast<PetscObject>(m_ptr); }
41
42 {
43 SmartPetscObj<Mat> n_ptr(m_ptr);
44 CHKERR check(2);
45 }
46
47 CHKERR check(1);
48
49 // check if casting works well
50 CHKERR MatSetSizes(m_ptr, 2, 2, 2, 2);
51
52 {
53 // nesting once
54 SmartPetscObj<Mat> n_ptr = m_ptr;
55 CHKERR check(2);
56 {
57 // again
58 SmartPetscObj<Mat> i_ptr = m_ptr;
59 CHKERR check(3);
60 }
61 CHKERR check(2);
62 }
63
64
65 {
66 // nesting again
67 SmartPetscObj<Mat> n_ptr = m_ptr;
68 CHKERR check(2);
69 }
70
71 // only one now
72 CHKERR check(1);
73
74 // reset, i.e. delete matrix
75 m_ptr.reset();
76
77 // counts should be zero now
78 CHKERR check(0);
79
80 {
82 }
83
84 }
86
88 return 0;
89}
int main()
Definition: adol-c_atom.cpp:46
#define CATCH_ERRORS
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
@ MOFEM_DATA_INCONSISTENCY
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 char help[]
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