Skip to content
This repository was archived by the owner on Nov 3, 2022. It is now read-only.

Commit 448b8b5

Browse files
author
Dilawar Singh
committed
more tests are passing...
1 parent d1b4990 commit 448b8b5

File tree

6 files changed

+83
-36
lines changed

6 files changed

+83
-36
lines changed

pybind11/Vec.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MooseVec {
1818

1919
public:
2020
MooseVec(const string& path, size_t n = 1, const string& dtype = "Neutral")
21-
: path_(path), n_(n), dtype_(dtype)
21+
: path_(path)
2222
{
2323
if (!mooseExists(path)) {
2424
objs_.clear();
@@ -32,6 +32,12 @@ class MooseVec {
3232
}
3333
}
3434

35+
MooseVec(const ObjId& oid) : path_(oid.path())
36+
{
37+
for (size_t i = 0; i < oid.element()->numData(); i++)
38+
objs_.push_back(ObjId(oid, i));
39+
}
40+
3541
size_t len()
3642
{
3743
return objs_.size();
@@ -64,15 +70,18 @@ class MooseVec {
6470
vector<py::object> getAttr(const string& name)
6571
{
6672
vector<py::object> res(objs_.size());
67-
for (size_t i = 0; i < objs_.size(); i++)
73+
for (size_t i = 0; i < objs_.size(); i++)
6874
res[i] = getProperty(objs_[i], name);
6975
return res;
7076
}
7177

78+
const vector<ObjId>& objs() const
79+
{
80+
return objs_;
81+
}
82+
7283
private:
7384
std::string path_;
74-
size_t n_;
75-
const std::string dtype_;
7685
std::vector<ObjId> objs_;
7786
};
7887

pybind11/helper.cpp

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "../external/pybind11/include/pybind11/pybind11.h"
2121
#include "../external/pybind11/include/pybind11/stl.h"
2222
#include "../external/pybind11/include/pybind11/numpy.h"
23+
#include "../external/pybind11/include/pybind11/functional.h"
2324

2425
// See
2526
// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#binding-stl-containers
@@ -147,16 +148,19 @@ bool mooseExists(const string& path)
147148
return Id(path) != Id() || path == "/" || path == "/root";
148149
}
149150

151+
ObjId mooseElement(const ObjId& oid)
152+
{
153+
return oid;
154+
}
155+
150156
ObjId mooseElement(const string& path)
151157
{
152158
ObjId oid(path);
153159
if (oid.bad()) {
154-
throw runtime_error(std::string("moose_element: '") +
155-
std::string(path) +
156-
std::string("' does not exist!"));
160+
cerr << "moose_element: " << path << " does not exist!" << endl;
157161
return ObjId(Id());
158162
}
159-
return oid;
163+
return mooseElement(oid);
160164
}
161165

162166
ObjId loadModelInternal(const string& fname, const string& modelpath,
@@ -216,9 +220,14 @@ ObjId mooseConnect(const ObjId& src, const string& srcField, const ObjId& tgt,
216220
return pShell->doAddMsg("Single", src, srcField, tgt, tgtField);
217221
}
218222

219-
void mooseDelete(const ObjId& oid)
223+
bool mooseDelete(const ObjId& oid)
220224
{
221-
getShellPtr()->doDelete(oid);
225+
return getShellPtr()->doDelete(oid);
226+
}
227+
228+
bool mooseDelete(const string& path)
229+
{
230+
return getShellPtr()->doDelete(ObjId(path));
222231
}
223232

224233
ObjId mooseCreate(const string type, const string& path, size_t numdata)
@@ -390,15 +399,20 @@ py::object handleDestFinfo(const ObjId& obj, const string& fname)
390399
// return py::none();
391400
}
392401

393-
py::object getPropertyDestFinfo(const ObjId& oid, const string& fname)
402+
py::cpp_function getPropertyDestFinfo(const ObjId& oid, const string& fname,
403+
const Finfo* finfo)
394404
{
405+
const auto rttType = finfo->rttiType();
395406

396407
// Return function.
397-
std::function<py::object(const string&)> func = [oid](const string& fname) {
398-
return handleDestFinfo(oid, fname);
399-
};
400-
return py::cast(func);
408+
cout << "ObjId " << oid << " -- " << fname << ": " << rttType << endl;
401409

410+
if (rttType == "void") {
411+
std::function<py::object()> func = [oid, fname]() {
412+
return handleDestFinfo(oid, fname);
413+
};
414+
return func;
415+
}
402416
}
403417

404418
py::object getProperty(const ObjId& oid, const string& fname)
@@ -423,7 +437,7 @@ py::object getProperty(const ObjId& oid, const string& fname)
423437
// Return function.
424438
return getLookupValueFinfo(oid, fname, finfo);
425439
} else if (finfoType == "DestFinfo") {
426-
return getPropertyDestFinfo(oid, fname);
440+
return getPropertyDestFinfo(oid, fname, finfo);
427441
}
428442

429443
cerr << "NotImplemented: getProperty for " << fname << " with rttType "
@@ -441,13 +455,22 @@ py::object getLookupValueFinfoItem(const ObjId& oid, const string& fname,
441455
string tgtType = srcDestType[1];
442456

443457
py::object r;
444-
if (tgtType == "bool")
445-
r = py::cast(LookupField<string, bool>::get(oid, fname, k));
446-
else if (tgtType == "vector<Id>")
447-
r = py::cast(LookupField<string, vector<Id>>::get(oid, fname, k));
448-
else
449-
cerr << "Unsupported types: " << rttType << endl;
450-
return r;
458+
if (srcType == "string") {
459+
if (tgtType == "bool")
460+
return py::cast(LookupField<string, bool>::get(oid, fname, k));
461+
else if (tgtType == "vector<Id>")
462+
return py::cast(
463+
LookupField<string, vector<Id>>::get(oid, fname, k));
464+
else if (tgtType == "vector<ObjId>")
465+
return py::cast(
466+
LookupField<string, vector<ObjId>>::get(oid, fname, k));
467+
else
468+
MOOSE_DEBUG("Unsupported types: " << rttType << " and " << tgtType);
469+
}
470+
471+
MOOSE_DEBUG("Unsupported types: " << rttType << " src: " << srcType
472+
<< " and tgt:" << tgtType);
473+
py::none();
451474
}
452475

453476
py::object getLookupValueFinfo(const ObjId& oid, const string& fname,

pybind11/helper.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Shell* getShellPtr();
2828

2929
bool mooseExists(const string& path);
3030

31+
ObjId mooseElement(const ObjId& oid);
3132
ObjId mooseElement(const string& path);
3233

3334
ObjId loadModelInternal(const string& fname, const string& modelpath,
@@ -43,7 +44,8 @@ py::object getFieldGeneric(const ObjId& oid, const string& fname);
4344
ObjId mooseConnect(const ObjId& src, const string& srcField, const ObjId& tgt,
4445
const string& tgtField);
4546

46-
void mooseDelete(const ObjId& oid);
47+
bool mooseDelete(const ObjId& oid);
48+
bool mooseDelete(const string& path);
4749

4850
ObjId mooseCreate(const string type, const string& path, size_t numdata = 1);
4951

@@ -71,7 +73,8 @@ py::object handleDestFinfo(const ObjId& obj, const string& fname);
7173

7274
py::object getValueFinfo(const ObjId& oid, const string& fname, const Finfo* f);
7375

74-
py::object getPropertyDestFinfo(const ObjId& oid);
76+
py::cpp_function getPropertyDestFinfo(const ObjId& oid, const string& fname,
77+
const Finfo* finfo);
7578

7679
py::object getProperty(const ObjId& oid, const string& fname);
7780

pybind11/pymoose.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
#include "../external/pybind11/include/pybind11/pybind11.h"
2626
#include "../external/pybind11/include/pybind11/stl.h"
2727

28-
// See
29-
// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#binding-stl-containers
30-
// #include "../external/pybind11/include/pybind11/stl_bind.h"
28+
// See https://pybind11.readthedocs.io/en/master/classes.html#overloaded-methods
29+
template <typename... Args>
30+
using overload_cast_ = pybind11::detail::overload_cast_impl<Args...>;
3131

3232
#include "../basecode/global.h"
3333
#include "../basecode/header.h"
@@ -55,7 +55,6 @@ Id initModule(py::module& m)
5555
return initShell();
5656
}
5757

58-
5958
PYBIND11_MODULE(_cmoose, m)
6059
{
6160
m.doc() = R"moosedoc(moose module.)moosedoc";
@@ -135,7 +134,6 @@ PYBIND11_MODULE(_cmoose, m)
135134
* Attributes.
136135
*/
137136
.def("__getattr__", &getProperty)
138-
.def("__getattr__", &getPropertyDestFinfo)
139137
.def("__setattr__", &setProperty<double>)
140138
.def("__setattr__", &setProperty<vector<double>>)
141139
.def("__setattr__", &setProperty<std::string>)
@@ -186,12 +184,17 @@ PYBIND11_MODULE(_cmoose, m)
186184
py::class_<MooseVec>(m, "vec", py::dynamic_attr())
187185
.def(py::init<const string&, size_t, const string&>(), "path"_a,
188186
"n"_a = 1, "dtype"_a = "Neutral") // Default
187+
.def(py::init<const ObjId&>())
189188
.def("__len__", &MooseVec::len)
189+
.def("__iter__",
190+
[](const MooseVec& v) {
191+
return py::make_iterator(v.objs().begin(), v.objs().end());
192+
},
193+
py::keep_alive<0, 1>())
190194
.def("__getitem__", &MooseVec::getElem)
191195
.def("__setattr__", &MooseVec::setAttrOneToOne<double>)
192196
.def("__setattr__", &MooseVec::setAttrOneToAll<double>)
193-
.def("__getattr__", &MooseVec::getAttr)
194-
;
197+
.def("__getattr__", &MooseVec::getAttr);
195198

196199
// Module functions.
197200
m.def("getShell",
@@ -202,11 +205,13 @@ PYBIND11_MODULE(_cmoose, m)
202205
m.def("rand", [](double a, double b) { return moose::mtrand(a, b); },
203206
"a"_a = 0, "b"_a = 1);
204207
m.def("wildcardFind", &wildcardFind2);
205-
m.def("delete", &mooseDelete);
208+
m.def("delete", overload_cast_<const ObjId&>()(&mooseDelete));
209+
m.def("delete", overload_cast_<const string&>()(&mooseDelete));
206210
m.def("create", &mooseCreate);
207211
m.def("reinit", &mooseReinit);
208212
m.def("start", &mooseStart, "runtime"_a, "notify"_a = false);
209-
m.def("element", &mooseElement);
213+
m.def("element", overload_cast_<const ObjId&>()(&mooseElement));
214+
m.def("element", overload_cast_<const string&>()(&mooseElement));
210215
m.def("exists", &mooseExists);
211216
m.def("connect", &mooseConnect);
212217
m.def("getCwe", &mooseGetCwe);

tests/pybind11/test_api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ def test_other():
2525
finfo = moose.getFieldDict(a1.className)
2626
print(finfo)
2727

28+
def test_vec():
29+
a = moose.Pool('/p111', 100)
30+
v = moose.vec(a)
31+
for i, x in enumerate(v):
32+
print(i, x)
33+
2834
def main():
2935
test_children()
3036
test_other()
37+
test_vec()
3138

3239
if __name__ == '__main__':
3340
main()

tests/pybind11/test_shell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def test_ksolve2():
331331
kin = makereac2()
332332
run_and_assert(kin, "ksolve1_test2.png")
333333

334-
def test_access():
334+
def test_other():
335335
moose.Neutral('x')
336336
a = moose.Neutral('x/x')
337337
print(a.isA)
@@ -351,7 +351,7 @@ def main():
351351
test_ksolve0()
352352
test_ksolve1()
353353
test_ksolve2()
354-
test_access()
354+
test_other()
355355

356356
if __name__ == '__main__':
357357
main()

0 commit comments

Comments
 (0)