@@ -135,19 +135,20 @@ py::object getFieldGeneric(const ObjId &oid, const string &fieldName)
135135
136136 string finfoType = cinfo->getFinfoType (finfo);
137137
138- // Either return a simple value (ValueFinfo), list, dict or DestFinfo
139- // setter.
140- // The DestFinfo setter is a function.
141-
138+ // Things are very compilcated here. There return object from this function
139+ // can be of different types: a simple value (ValueFinfo), list, dict or
140+ // DestFinfo setter which is a function.
142141 if (finfoType == " ValueFinfo" )
143142 return __Finfo__::getFieldValue (oid, finfo);
144143 else if (finfoType == " FieldElementFinfo" ) {
144+ // This is a Finfo
145145 return py::cast (__Finfo__ (oid, finfo, " FieldElementFinfo" ));
146146 } else if (finfoType == " LookupValueFinfo" ) {
147- // Return function.
147+ // This is a function.
148148 return py::cast (__Finfo__ (oid, finfo, " LookupValueFinfo" ));
149149 } else if (finfoType == " DestFinfo" ) {
150- // Return a setter function. It can be used to set field on DestFinfo.
150+ // Return a setter function.
151+ // It can be used to set field on DestFinfo.
151152 return __Finfo__::getDestFinfoSetterFunc (oid, finfo);
152153 }
153154
@@ -157,7 +158,6 @@ py::object getFieldGeneric(const ObjId &oid, const string &fieldName)
157158 return pybind11::none ();
158159}
159160
160-
161161/* --------------------------------------------------------------------------*/
162162/* *
163163 * @Synopsis MOOSE extension module _moose.so.
@@ -174,20 +174,14 @@ PYBIND11_MODULE(_moose, m)
174174
175175 initModule (m);
176176
177- py::object melement;
178-
179- // A thin wrapper around Id from ../basecode/Id.h . Usually this is shows
180- // at moose.vec.
181- py::class_<Id>(m, " mid" )
177+ // A thin wrapper around Id from ../basecode/Id.h .
178+ py::class_<Id>(m, " __id__" )
182179 .def (py::init<>())
183180 .def (py::init<unsigned int >())
184181 .def (py::init<const string &>())
185182 .def (py::init<const ObjId &>())
186183 // properties
187184 .def_property_readonly (" numIds" , &Id::numIds)
188- // FIXME/NB: Don't expose path. Note that Stoich as `path` attribute
189- // which
190- // will not work if this is exposed.
191185 .def_property_readonly (" path" , &Id::path)
192186 .def_property_readonly (
193187 " name" , [](const Id &id) { return id.element ()->getName (); })
@@ -208,6 +202,8 @@ PYBIND11_MODULE(_moose, m)
208202 .def (" __eq__" , [](const Id &a, const Id &b) { return a == b; })
209203 .def (" __ne__" , [](const Id &a, const Id &b) { return a != b; })
210204 .def (" __hash__" , &Id::value)
205+
206+ // Id attributes are same as ObjItem attributes.
211207 .def (" __getattr__" , [](const Id &id, const string &key) {
212208 return getFieldGeneric (ObjId (id), key);
213209 })
@@ -216,32 +212,32 @@ PYBIND11_MODULE(_moose, m)
216212 return setFieldGeneric (ObjId (id), key, val);
217213 });
218214
219-
220215 // This is a wrapper around Field::get and LookupField::get which may
221216 // return simple values or vector. Python scripts expect LookupField to
222217 // return either list of dict which can be queried by key and index. This
223218 // class bind both __getitem__ to the getter function call.
224219 // Note that both a.isA["Compartment"] and a.isA("Compartment") are valid
225220 // now.
226- py::class_<__Finfo__>(m, " Field " , py::dynamic_attr ())
221+ py::class_<__Finfo__>(m, " __Field__ " , py::dynamic_attr ())
227222 .def (py::init<const ObjId &, const Finfo *, const char *>())
228223 .def_property_readonly (" type" , &__Finfo__::type)
229- .def_property_readonly (" vec" , [](const __Finfo__ &finfo) {
230- return MooseVec (finfo.getObjId ());
231- })
232- .def_property (" num" , &__Finfo__::getNumField,
233- &__Finfo__::setNumField) // Only for FieldElementFinfos
234- .def (" __call__" , &__Finfo__::operator ())
224+ .def_property (" num" , &__Finfo__::getNumField, &__Finfo__::setNumField)
225+ .def_property_readonly (
226+ " vec" , [](__Finfo__ &finfo) { return finfo.getMooseVecPtr (); },
227+ py::return_value_policy::reference_internal)
228+
229+ // Only for FieldElementFinfos
230+ .def (" __len__" , &__Finfo__::getNumField)
235231 .def (" __call__" , &__Finfo__::operator ())
236232 .def (" __getitem__" , &__Finfo__::getItem)
237233 .def (" __setitem__" , &__Finfo__::setItem)
238- . def ( " __len__ " , &__Finfo__::getNumField) ;
234+ ;
239235
240236 /* *
241237 * @name ObjId. It is a base of all other moose objects.
242238 * @{ */
243239 /* * @} */
244- py::class_<ObjId>(m, " ObjId " , py::metaclass ( melement) )
240+ py::class_<ObjId>(m, " melement" )
245241 .def (py::init<>())
246242 .def (py::init<Id>())
247243 .def (py::init<Id, unsigned int >())
@@ -311,14 +307,15 @@ PYBIND11_MODULE(_moose, m)
311307 py::class_<Variable>(m, " _Variable" ).def (py::init<>());
312308
313309 // Cinfo.
314- py::class_<Cinfo>(m, " _Cinfo " )
310+ py::class_<Cinfo>(m, " __Cinfo__ " )
315311 .def (py::init<>())
316312 .def_property_readonly (" name" , &Cinfo::name)
317313 .def_property_readonly (" finfoMap" , &Cinfo::finfoMap,
318- py::return_value_policy::reference )
314+ py::return_value_policy::reference_internal )
319315 // .def("findFinfo", &Cinfo::findFinfoWrapper)
320- .def (" baseCinfo" , &Cinfo::baseCinfo, py::return_value_policy::reference)
321- .def (" isA" , &Cinfo::isA);
316+ .def (" baseCinfo" , &Cinfo::baseCinfo,
317+ py::return_value_policy::reference_internal)
318+ .def (" isA" , &Cinfo::isA, py::return_value_policy::reference_internal);
322319
323320 // Vec class.
324321 py::class_<MooseVec>(m, " vec" )
@@ -338,9 +335,10 @@ PYBIND11_MODULE(_moose, m)
338335 return py::make_iterator (v.objref ().begin (), v.objref ().end ());
339336 },
340337 py::keep_alive<0 , 1 >())
338+
341339 .def (" __getitem__" , &MooseVec::getItem)
342- // Beware of pybind11 overload resolution order:
343- // https://pybind11.readthedocs.io/en/stable/advanced/functions.html#overload-resolution-order
340+ . def ( " __getitem__ " , &MooseVec::getItemRange)
341+
344342 // Templated function won't work here. The first one is always called.
345343 .def (" __getattr__" , &MooseVec::getAttribute)
346344 .def (" __setattr__" , &MooseVec::setAttrOneToOne<double >)
0 commit comments