Skip to content

Commit 22aeb43

Browse files
author
Dilawar Singh
committed
moose.element return single ObjId and not vec as I mistakenly assumed.
1 parent ea81f1c commit 22aeb43

File tree

9 files changed

+173
-132
lines changed

9 files changed

+173
-132
lines changed

.clang-format

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ AccessModifierOffset: -4
44
ConstructorInitializerIndentWidth: 4
55
AlignEscapedNewlinesLeft: true
66
AlignTrailingComments: true
7-
AllowAllParametersOfDeclarationOnNextLine: true
8-
AllowShortIfStatementsOnASingleLine: true
9-
AllowShortLoopsOnASingleLine: true
7+
AllowAllParametersOfDeclarationOnNextLine: false
8+
AllowShortIfStatementsOnASingleLine: false
9+
AllowShortLoopsOnASingleLine: false
1010
AlwaysBreakTemplateDeclarations: true
1111
AlwaysBreakBeforeMultilineStrings: true
1212
BreakBeforeBinaryOperators: false

pybind11/MooseVec.cpp

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ ObjId MooseVec::parent() const
6262
return Neutral::parent(oid_);
6363
}
6464

65+
vector<MooseVec> MooseVec::children() const
66+
{
67+
vector<Id> children;
68+
Neutral::children(oid_.eref(), children);
69+
vector<MooseVec> res;
70+
std::transform(children.begin(), children.end(), res.begin(), [](const Id& id){ return MooseVec(id); });
71+
return res;
72+
}
73+
74+
6575
unsigned int MooseVec::len()
6676
{
6777
return (unsigned int)size();
@@ -92,35 +102,36 @@ vector<py::object> MooseVec::getAttribute(const string& name)
92102
return res;
93103
}
94104

95-
// FIXME: Only double is supported here. Not sure if this is enough. This
96-
// should be the API function.
97-
py::array_t<double> MooseVec::getAttributeNumpy(const string &name)
98-
{
99-
auto cinfo = oid_.element()->cinfo();
100-
auto finfo = cinfo->findFinfo(name);
101105

102-
if (!finfo) {
103-
throw py::key_error(name + " is not found on '" + oid_.path() + "'.");
104-
}
105-
106-
string finfoType = cinfo->getFinfoType(finfo);
107-
108-
// Either return a simple value (ValueFinfo), list, dict or DestFinfo
109-
// setter.
110-
// The DestFinfo setter is a function.
111-
112-
vector<double> res(size());
113-
if (finfoType == "ValueFinfo") {
114-
for (unsigned int i = 0; i < size(); i++)
115-
res[i] = getField<double>(getItem(i), name);
116-
return py::array_t<double>(res.size(), res.data());
117-
}
118-
119-
throw runtime_error("MooseVec::getAttributeNumpy::NotImplemented : " + name +
120-
" with rttType " + finfo->rttiType() + " and type: '" +
121-
finfoType + "'");
122-
return py::array_t<double>();
123-
}
106+
// // FIXME: Only double is supported here. Not sure if this is enough. This
107+
// // should be the API function.
108+
// py::array_t<double> MooseVec::getAttributeNumpy(const string &name)
109+
// {
110+
// auto cinfo = oid_.element()->cinfo();
111+
// auto finfo = cinfo->findFinfo(name);
112+
//
113+
// if (!finfo) {
114+
// throw py::key_error(name + " is not found on '" + oid_.path() + "'.");
115+
// }
116+
//
117+
// string finfoType = cinfo->getFinfoType(finfo);
118+
//
119+
// // Either return a simple value (ValueFinfo), list, dict or DestFinfo
120+
// // setter.
121+
// // The DestFinfo setter is a function.
122+
//
123+
// vector<double> res(size());
124+
// if (finfoType == "ValueFinfo") {
125+
// for (unsigned int i = 0; i < size(); i++)
126+
// res[i] = getField<double>(getItem(i), name);
127+
// return py::array_t<double>(res.size(), res.data());
128+
// }
129+
//
130+
// throw runtime_error("MooseVec::getAttributeNumpy::NotImplemented : " + name +
131+
// " with rttType " + finfo->rttiType() + " and type: '" +
132+
// finfoType + "'");
133+
// return py::array_t<double>();
134+
// }
124135

125136

126137
ObjId MooseVec::connectToSingle(const string& srcfield, const ObjId& tgt,

pybind11/MooseVec.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class MooseVec
2626

2727
const size_t size() const;
2828

29+
vector<MooseVec> children() const;
30+
2931
const string path() const;
3032

3133
const string name() const;
@@ -75,7 +77,16 @@ class MooseVec
7577

7678
// Get attributes.
7779
vector<py::object> getAttribute(const string& name);
78-
py::array_t<double> getAttributeNumpy(const string& name);
80+
81+
// TODO: May be buffer https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html#buffer-protocol
82+
template<typename T=double>
83+
py::array_t<T> getAttributeNumpy(const string& name)
84+
{
85+
vector<T> res(size());
86+
for (unsigned int i = 0; i < size(); i++)
87+
res[i] = Field<T>::get(getItem(i), name);
88+
return py::array_t<T>(res.size(), res.data());
89+
}
7990

8091
vector<ObjId> objs() const;
8192

pybind11/helper.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,17 @@ void mooseStop()
326326
}
327327

328328
// Id is synonym with Id in previous binding.
329-
MooseVec mooseCopy(const py::object& elem, ObjId newParent, string newName,
329+
MooseVec mooseCopy(const py::object& elem, const py::object& newParent, string newName,
330330
unsigned int n = 1, bool toGlobal = false,
331331
bool copyExtMsgs = false)
332332
{
333333
Id orig = py::cast<Id>(elem);
334-
return MooseVec(getShellPtr()->doCopy(orig, newParent, newName, n, toGlobal,
334+
ObjId newp;
335+
if(py::isinstance<MooseVec>(newParent))
336+
newp = newParent.cast<MooseVec>().obj();
337+
else
338+
newp = newParent.cast<ObjId>();
339+
return MooseVec(getShellPtr()->doCopy(orig, newp, newName, n, toGlobal,
335340
copyExtMsgs));
336341
}
337342

pybind11/helper.h

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,37 +44,44 @@ void mooseMoveId(const Id& a, const ObjId& b);
4444
void mooseMoveObjId(const ObjId& a, const ObjId& b);
4545
#endif
4646

47-
template<typename P=ObjId, typename Q=ObjId>
47+
template <typename P = ObjId, typename Q = ObjId>
4848
inline void mooseMove(const P& src, const Q& tgt)
4949
{
5050
getShellPtr()->doMove(Id(src), ObjId(tgt));
5151
}
5252

53-
54-
inline MooseVec mooseObjIdPath(const string& p)
53+
inline ObjId mooseObjIdPath(const string& p)
5554
{
5655
// handle relative path.
5756
string path(p);
58-
if(p[0] != '/')
59-
path = mooseGetCweId().path() + '/' + path;
57+
58+
// If path is a relative path.
59+
if (p[0] != '/')
60+
{
61+
string cwepath(mooseGetCweId().path());
62+
if(cwepath.back() != '/')
63+
cwepath.push_back('/');
64+
path = cwepath + p;
65+
}
6066
ObjId oid(path);
6167
if (oid.bad()) {
62-
cerr << "moose_element: " << path << " does not exist!" << endl;
68+
cerr << "moose.element: " << path << " does not exist!" << endl;
6369
}
64-
return MooseVec(oid);
70+
return oid;
6571
}
6672

67-
inline MooseVec mooseObjIdObj(const ObjId& obj)
73+
inline ObjId mooseObjIdObj(const ObjId& obj)
6874
{
69-
return MooseVec(obj);
75+
return obj;
7076
}
7177

72-
inline MooseVec mooseObjIdId(const Id& id)
78+
inline ObjId mooseObjIdId(const Id& id)
7379
{
74-
return MooseVec(id);
80+
return ObjId(id);
7581
}
7682

77-
inline ObjId mooseCreateFromPath(const string type, const string& p, unsigned int numdata)
83+
inline ObjId mooseCreateFromPath(const string type, const string& p,
84+
unsigned int numdata)
7885
{
7986

8087
#if 0
@@ -85,39 +92,40 @@ inline ObjId mooseCreateFromPath(const string type, const string& p, unsigned in
8592

8693
// relative path.
8794
string path(p);
88-
if(path[0] != '/')
89-
path = mooseGetCweId().path()+'/'+path;
95+
if (path[0] != '/')
96+
path = mooseGetCweId().path() + '/' + path;
9097

9198
// If path exists and user is asking for the same type then return the
9299
// object else raise exception.
93-
if(mooseExists(path)) {
100+
if (mooseExists(path)) {
94101
auto oid = ObjId(path);
95-
if(oid.element()->cinfo()->name() == type)
102+
if (oid.element()->cinfo()->name() == type)
96103
return oid;
97104
}
98105

99106
// Split into dirname and basename component.
100107
auto pp = moose::splitPath(path);
101108

102109
// Name must not end with [\d*] etc. normalizePath takes care of it if
103-
// enabled.
110+
// enabled.
104111
string name(pp.second);
105-
if(name.back() == ']')
112+
if (name.back() == ']')
106113
name = name.substr(0, name.find_last_of('['));
107114
return getShellPtr()->doCreate2(type, ObjId(pp.first), name, numdata);
108115
}
109116

110-
inline ObjId mooseCreateFromObjId(const string& type, const ObjId& oid, unsigned int numData)
117+
inline ObjId mooseCreateFromObjId(const string& type, const ObjId& oid,
118+
unsigned int numData)
111119
{
112120
return oid;
113121
}
114122

115-
inline ObjId mooseCreateFromId(const string& type, const Id& id, unsigned int numData)
123+
inline ObjId mooseCreateFromId(const string& type, const Id& id,
124+
unsigned int numData)
116125
{
117126
return ObjId(id);
118127
}
119128

120-
121129
ObjId loadModelInternal(const string& fname, const string& modelpath,
122130
const string& solverclass);
123131

@@ -130,8 +138,9 @@ ObjId getElementFieldItem(const ObjId& objid, const string& fname,
130138
ObjId shellConnect(const ObjId& src, const string& srcField, const ObjId& tgt,
131139
const string& tgtField, const string& msgType);
132140

133-
ObjId shellConnectToVec(const ObjId& src, const string& srcField, const MooseVec& tgt,
134-
const string& tgtField, const string& msgType);
141+
ObjId shellConnectToVec(const ObjId& src, const string& srcField,
142+
const MooseVec& tgt, const string& tgtField,
143+
const string& msgType);
135144

136145
inline bool mooseDeleteObj(const ObjId& oid)
137146
{
@@ -143,14 +152,13 @@ inline bool mooseDeleteStr(const string& path)
143152
return getShellPtr()->doDelete(ObjId(path));
144153
}
145154

155+
MooseVec mooseCopy(const py::object& orig, const py::object& newParent,
156+
string newName, unsigned int n, bool toGlobal,
157+
bool copyExtMsgs);
146158

147159
ObjId mooseCreate(const string type, const string& path,
148160
unsigned int numdata = 1);
149161

150-
MooseVec mooseCopy(const py::object& orig, ObjId newParent, string newName,
151-
unsigned int n, bool toGlobal, bool copyExtMsgs);
152-
153-
154162
void mooseSetClock(const unsigned int clockId, double dt);
155163

156164
void mooseUseClock(size_t tick, const string& path, const string& field);
@@ -166,6 +174,7 @@ void mooseStop();
166174

167175
py::cpp_function getPropertyDestFinfo(const ObjId& oid, const Finfo* finfo);
168176

169-
vector<string> mooseGetFieldNames(const string& className, const string& finfoType);
177+
vector<string> mooseGetFieldNames(const string& className,
178+
const string& finfoType);
170179

171180
#endif /* end of include guard: HELPER_H */

pybind11/pymoose.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ PYBIND11_MODULE(_moose, m)
309309
.def("__setattr__", &MooseVec::setAttrOneToAll<double>)
310310
.def("__setattr__", &MooseVec::setAttrOneToAll<string>)
311311
.def("__setattr__", &MooseVec::setAttrOneToAll<bool>)
312+
.def("__getattr__", &MooseVec::getAttributeNumpy<double>)
313+
.def("__getattr__", &MooseVec::getAttributeNumpy<float>)
314+
// These three are probably never needed.
315+
.def("__getattr__", &MooseVec::getAttributeNumpy<unsigned int>)
316+
.def("__getattr__", &MooseVec::getAttributeNumpy<unsigned long>)
317+
// For the rest non-POD types.
312318
.def("__getattr__", &MooseVec::getAttribute)
313319
.def("__repr__", [](const MooseVec & v)->string {
314320
return "<moose.vec class=" + v.dtype() + " path=" + v.path() +
@@ -326,8 +332,11 @@ PYBIND11_MODULE(_moose, m)
326332

327333
// Thi properties are not vectorised.
328334
.def_property_readonly("parent", &MooseVec::parent)
335+
.def_property_readonly("children", &MooseVec::children)
329336
.def_property_readonly("name", &MooseVec::name)
330337
.def_property_readonly("path", &MooseVec::path)
338+
// Wrapped object.
339+
.def_property_readonly("objid", &MooseVec::obj)
331340
;
332341

333342
// Module functions.

pybind11/pymoose.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#include "MooseVec.h"
2222

2323
template <typename T>
24-
inline void setField(const ObjId& id, const string& fname, T val)
24+
inline bool setField(const ObjId& id, const string& fname, T val)
2525
{
2626
// cout << "Setting " << fname << " to value " << val << typeid(T).name() <<
2727
// endl;
28-
Field<T>::set(id, fname, val);
28+
return Field<T>::set(id, fname, val);
2929
}
3030

3131
template <typename T>

python/moose/moose.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,6 @@ def copy(elem, newParent, newName="", n=1):
129129
return _moose.copy(elem.id, newParent, newName, n, False, False)
130130

131131

132-
def getCwe():
133-
return PyObjId(_moose.getCwe())
134-
135-
136132
def pwe():
137133
"""Print present working element. Convenience function for GENESIS
138134
users. If you want to retrieve the element in stead of printing
@@ -141,7 +137,7 @@ def pwe():
141137
>>> pwe()
142138
>>> '/'
143139
"""
144-
pwe_ = getCwe()
140+
pwe_ = _moose.getCwe()
145141
print(pwe_.path)
146142
return pwe_
147143

0 commit comments

Comments
 (0)