definition of iphys in the .hpp file related to a contact law

Asked by Rioual on 2020-05-13

Hello,

I try to understand the way we can implement the definition of iPhys of a contact law.
I take example on CpmPhys (https://gitlab.com/yade-dev/trunk/-/blob/master/pkg/dem/ConcretePM.hpp#L138).
The goal is to store in iPhys the relevant parameters of a given interaction.

I don't understand why certain parameters are declared with simple brakets:
https://gitlab.com/yade-dev/trunk/-/blob/master/pkg/dem/ConcretePM.hpp#L163

why other parameters are declared with .def prefix as in
https://gitlab.com/yade-dev/trunk/-/blob/master/pkg/dem/ConcretePM.hpp#L202

and others with .def_readonly

thank you for the explanations,
Best

F.

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Jérôme Duriez
Solved:
2020-05-14
Last query:
2020-05-14
Last reply:
2020-05-14
Jan Stránský (honzik) said : #1

TLTR:
- put all attributes in "simple brackets"
- use .def and .staticmethod to use C++ functions in python and therefore to enable python testing

More details:
"simple brackets":
it is inside a macro, that makes the values public attributes of the class. You can then access them from the class and also from other parts of the C++ code. It has also other advantages:
- readable / assignable from python
- adds python docstrings
- adds it to automatically built documentation
- saved / loaded with O.save / O.load
- ... ?

.def, .staticmethod
is to expose methods (functions) to python (referenced are already existing/declared methods, e.g. funcG)

.def_readonly:
in python it is to return values of the class attributes, but you cannot set them from python.
("simple brackets" can be both read and assigned)
References existing objects, e.g. omega.
The point is to prevent accidental change of some "computed" values. E.g. omega is a function of kappaD, so changing omega alone is not meaningful.
It has disadvantage that it is not saved / loaded with O.save / O.load
Do not bother with .def_readonly for now.

cheers
Jan

Best Jérôme Duriez (jduriez) said : #2

Hi

- simple brackets refer to the ATTRS part of the YADE_CLASS_BASE_DOC_ATTRS_CTOR_PY macro [*].

- the .def() lines enter the PY part of the latter. I see this part as directly obeying the syntax of the boost::python C++ Library, eg [**]. Unlike real attributes, it is about methods (functions) here

[*] https://yade-dem.org/doc/prog.html#yade-class-base-doc-macro-family
[**] https://www.boost.org/doc/libs/1_73_0/libs/python/doc/html/reference/high_level_components/boost_python_def_hpp.html

Rioual (francois-rioual-v) said : #3

Thanks Jérôme Duriez, that solved my question.