Skip to content

Commit 4624651

Browse files
author
Dilawar Singh
committed
name, path, and parent of moose.vec are not vectorised.
1 parent 6126a64 commit 4624651

File tree

4 files changed

+63
-40
lines changed

4 files changed

+63
-40
lines changed

pybind11/MooseVec.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ const string MooseVec::path() const
5757
return path_;
5858
}
5959

60+
ObjId MooseVec::parent() const
61+
{
62+
return Neutral::parent(oid_);
63+
}
64+
6065
unsigned int MooseVec::len()
6166
{
6267
return (unsigned int)size();
@@ -79,24 +84,6 @@ ObjId MooseVec::getFieldItem(const size_t i) const
7984
return ObjId(oid_.path(), oid_.dataIndex, i);
8085
}
8186

82-
void MooseVec::setAttrOneToAll(const string& name, const py::object& val)
83-
{
84-
for (size_t i = 0; i < size(); i++) setFieldGeneric(getItem(i), name, val);
85-
}
86-
87-
void MooseVec::setAttrOneToOne(const string& name, const py::sequence& val)
88-
{
89-
if (py::len(val) != size())
90-
throw runtime_error(
91-
"Length of sequence on the right hand side "
92-
"does not match size of vector. "
93-
"Expected " +
94-
to_string(size()) + ", got " + to_string(py::len(val)));
95-
96-
for (size_t i = 0; i < size(); i++)
97-
setFieldGeneric(getItem(i), name, val[i]);
98-
}
99-
10087
vector<py::object> MooseVec::getAttribute(const string& name)
10188
{
10289
vector<py::object> res(size());

pybind11/MooseVec.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class MooseVec
3030

3131
const string name() const;
3232

33+
ObjId parent() const;
34+
3335
unsigned int len();
3436

3537
const ObjId& getItemRef(const size_t i) const;
@@ -40,16 +42,44 @@ class MooseVec
4042
ObjId getFieldItem(const size_t i) const;
4143

4244
// Set attribute to vector.
43-
void setAttrOneToAll(const string& name, const py::object& val);
45+
// void setAttrOneToAll(const string& name, const py::object& val);
46+
// void setAttrOneToOne(const string& name, const py::sequence& val);
47+
48+
template<typename T>
49+
bool setAttrOneToAll(const string& name, const T& val)
50+
{
51+
bool res = true;
52+
for (size_t i = 0; i < size(); i++)
53+
res &= Field<T>::set(getItem(i), name, val);
54+
return res;
55+
}
56+
57+
58+
template<typename T=double>
59+
bool setAttrOneToOne(const string& name, const vector<T>& val)
60+
{
61+
if (val.size() != size())
62+
throw runtime_error(
63+
"Length of sequence on the right hand side "
64+
"does not match size of vector. "
65+
"Expected " +
66+
to_string(size()) + ", got " + to_string(val.size()));
67+
68+
bool res = true;
69+
for (size_t i = 0; i < size(); i++)
70+
res &= Field<T>::set(getItem(i), name, val[i]);
71+
72+
return res;
73+
}
4474

45-
void setAttrOneToOne(const string& name, const py::sequence& val);
4675

4776
// Get attributes.
4877
vector<py::object> getAttribute(const string& name);
4978
py::array_t<double> getAttributeNumpy(const string& name);
5079

5180
vector<ObjId> objs() const;
5281

82+
5383
ObjId connectToSingle(const string& srcfield, const ObjId& tgt, const string& tgtfield, const string& msgtype);
5484

5585
ObjId connectToVec(const string& srcfield, const MooseVec& tgt, const string& tgtfield, const string& msgtype);

pybind11/pymoose.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,19 +260,8 @@ PYBIND11_MODULE(_moose, m)
260260
//---------------------------------------------------------------------
261261
// Connect
262262
//---------------------------------------------------------------------
263-
// This would be so much easier with c++17.
264263
.def("connect", &shellConnect, "srcfield"_a, "dest"_a
265264
, "destfield"_a, "msgtype"_a="Single")
266-
// .def("connect",
267-
// [](const ObjId &src, const string &srcfield, const ObjId &tgt,
268-
// const string &tgtfield, const string &type) {
269-
// return shellConnect(src, srcfield, tgt, tgtfield, type);
270-
// }, "src"_a, "srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a = "Single")
271-
// .def("connect", [](const ObjId &src, const string &srcfield,
272-
// const MooseVec &tgtvec, const string &tgtfield,
273-
// const string &type) {
274-
// return shellConnect(src, srcfield, tgtvec.obj(), tgtfield, type);
275-
// }, "src"_a, "srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a="Single")
276265
//---------------------------------------------------------------------
277266
// Extra
278267
//---------------------------------------------------------------------
@@ -301,10 +290,8 @@ PYBIND11_MODULE(_moose, m)
301290
.def(py::init<const string &, unsigned int, const string &>(), "path"_a,
302291
"n"_a = 1, "dtype"_a = "Neutral") // Default
303292
.def(py::init<const ObjId &>())
304-
.def("connect", &MooseVec::connectToSingle)
305-
.def("connect", &MooseVec::connectToVec)
306-
.def_property_readonly("name", &MooseVec::name)
307-
.def_property_readonly("path", &MooseVec::path)
293+
.def("__eq__", [](const MooseVec &a, const MooseVec &b) { return a.obj() == b.obj(); })
294+
.def("__ne__", [](const MooseVec &a, const MooseVec &b) { return a.obj() != b.obj(); })
308295
.def("__len__", &MooseVec::len)
309296
.def("__iter__",
310297
[](MooseVec &v) {
@@ -315,8 +302,12 @@ PYBIND11_MODULE(_moose, m)
315302
},
316303
py::keep_alive<0, 1>())
317304
.def("__getitem__", &MooseVec::getItem)
318-
.def("__setattr__", &MooseVec::setAttrOneToAll)
319-
.def("__setattr__", &MooseVec::setAttrOneToOne)
305+
.def("__setattr__", &MooseVec::setAttrOneToOne<double>)
306+
.def("__setattr__", &MooseVec::setAttrOneToOne<string>)
307+
.def("__setattr__", &MooseVec::setAttrOneToOne<bool>)
308+
.def("__setattr__", &MooseVec::setAttrOneToAll<double>)
309+
.def("__setattr__", &MooseVec::setAttrOneToAll<string>)
310+
.def("__setattr__", &MooseVec::setAttrOneToAll<bool>)
320311
.def("__getattr__", &MooseVec::getAttribute)
321312
.def("__repr__", [](const MooseVec & v)->string {
322313
return "<moose.vec class=" + v.dtype() + " path=" + v.path() +
@@ -328,7 +319,15 @@ PYBIND11_MODULE(_moose, m)
328319
.def_property_readonly("vec", [](const MooseVec &vec) { return &vec; },
329320
py::return_value_policy::reference_internal)
330321
.def_property_readonly("type",
331-
[](const MooseVec &v) { return "moose.vec"; });
322+
[](const MooseVec &v) { return "moose.vec"; })
323+
.def("connect", &MooseVec::connectToSingle)
324+
.def("connect", &MooseVec::connectToVec)
325+
326+
// Thi properties are not vectorised.
327+
.def_property_readonly("parent", &MooseVec::parent)
328+
.def_property_readonly("name", &MooseVec::name)
329+
.def_property_readonly("path", &MooseVec::path)
330+
;
332331

333332
// Module functions.
334333
m.def("getShell",

tests/py_moose/test_api.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def test_vec():
4343
assert v[0] == v.vec[0], (v[0], v.vec[0])
4444
x = [random.random() for i in range(100)]
4545
v.conc = x
46-
assert np.equal(v.conc, x).all()
46+
assert sum(v.conc) == sum(x)
47+
assert np.allclose(v.conc, x), (v.conc, x)
4748

4849
def test_finfos():
4950
s = moose.SimpleSynHandler('synh')
@@ -132,7 +133,12 @@ def test_access():
132133
raise RuntimeError("Should have failed.")
133134
a2 = moose.element(a1)
134135
a3 = moose.element(a1.path)
135-
assert a2 == a3
136+
assert a2 == a3, (a2, a3)
137+
138+
def test_element():
139+
a = moose.Pool('xxxx', 2)
140+
ae = moose.element(a)
141+
assert ae.parent == a.parent, (ae.parent, a.parent)
136142

137143

138144
def main():
@@ -144,6 +150,7 @@ def main():
144150
test_inheritance()
145151
test_access()
146152
test_vec()
153+
test_element()
147154

148155
if __name__ == '__main__':
149156
main()

0 commit comments

Comments
 (0)