How to get object from SmartPetscObj?

15 views
Skip to first unread message
Assigned to lik...@wp.pl by me

Karol Lewandowski

unread,
Nov 10, 2021, 3:04:26 PM11/10/21
to MoFEM Q&A
I need a reference to DM from SmartPetscObj<DM> dM,
how to do that?

Karol Lewandowski

unread,
Nov 10, 2021, 3:08:20 PM11/10/21
to MoFEM Q&A
dM = simple->getDM();


CHKERR PetscObjectReference(getPetscObject(dM.get()));
DM *dm = dM.get();


Hmm, is that the correct answer?

Lukasz Kaczmraczyk

unread,
Nov 10, 2021, 3:32:03 PM11/10/21
to MoFEM Q&A

Karol,

So I expect, that you rather like to do,

 // That get a smart pointer, if you go out of the scope, dM is dereferenced, if the reference count is zero object is destroyed.
SmartPetscObj<DM> dM = simple->getDM();
// No bump reference, and cas SmartPetscObj<DM> to PetscObject,
PetscObjectReference(getPetscObject(dM.get()));

DM dm  = dM;

// do somthing
// Now you have to remember to destroy DM.
DMDestroy(dm);

However, I do not know exactly what you like to do, why you need to do that? Why SmartPestcObj is not good for you, and you need to work with raw DM object?

One comment, that

DM *dm = dM.get();

makes no big sense, since PetscObject, in this case, DM is a pointer to structure. That would make only sense, in the case like that,

auto get_dm  = [simple](DM *dm) {
  MoFEMFunctionBegin;
  dM = simple->getDM();
  CHKERR PetscObjectReference(getPetscObject(dM.get()));
  DM *dm = dM.get();
  MoFEMFunctionReturn(0);
};

DM dm;
CHKERR get_dm(&dm);
// Do something
CHKERR DMDestroy(&dm);

Kind regards,
Lukasz

Karol Lewandowski

unread,
Nov 10, 2021, 3:38:26 PM11/10/21
to MoFEM Q&A
I want to have a MortarContactInterface that stores internally a reference to DM, but I don't want to constrain the user on how is he/she/they passing this DM.
It should be either as a smart object or dm itself. How would you do it? How should I store a pointer to DM?

Karol Lewandowski

unread,
Nov 10, 2021, 3:42:10 PM11/10/21
to MoFEM Q&A
The problem is that many functions in MoFEM are still not accepting SmartObjs

Karol Lewandowski

unread,
Nov 10, 2021, 3:47:19 PM11/10/21
to MoFEM Q&A
MoFEMErrorCode MortarInterface::addContactElements(DM dm) {
  MoFEMFunctionBeginHot;
  dmPtr = &dm;

  CHKERR DMMoFEMAddElement(*dmPtr, "CONTACT_ELEM");
  CHKERR DMMoFEMAddElement(*dmPtr, "CONTACT_POST_PROC");

  auto simple = mField.getInterface<Simple>();
  simple->getOtherFiniteElements().push_back("CONTACT_ELEM");
  simple->getOtherFiniteElements().push_back("CONTACT_POST_PROC");

  MoFEMFunctionReturnHot(0);
}

MoFEMErrorCode MortarInterface::addContactElements(SmartPetscObj<DM> dm) {
  MoFEMFunctionBeginHot;

  CHKERR PetscObjectReference(getPetscObject(dm.get()));
  *dmPtr = dm.get();
  CHKERR addContactElements(*dmPtr);

  MoFEMFunctionReturnHot(0);
}

template <typename T>
MoFEMErrorCode MortarInterface::setupSolver(bool is_quasi_static) {
  MoFEMFunctionBegin;

  if (!dmPtr)
    SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
            "DM not defined, addContactElements first");
...


Karol Lewandowski

unread,
Nov 10, 2021, 3:50:50 PM11/10/21
to MoFEM Q&A
It is probably not safe to Destroy DM in the destructor, in case the users want to still do something with it when they are done with contact.

Lukasz Kaczmraczyk

unread,
Nov 10, 2021, 3:54:57 PM11/10/21
to MoFEM Q&A
Hello, 

1. SmartPetscObj is designed to work as shared_ptr<Obj>, to not forget to destroy objects. When you go out of the scope object is destroyed. 
2. All functions which take PetscObject by value work with SmartPetscObj. 

This code  can you help to understand how SmartPetscObj is working,
L.
Reply all
Reply to author
Forward
0 new messages