Skip to content

Commit 0f7929c

Browse files
author
Dilawar Singh
committed
Support vec. But first need a nap.
1 parent 93d6e83 commit 0f7929c

File tree

6 files changed

+83
-20
lines changed

6 files changed

+83
-20
lines changed

basecode/Id.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,7 @@ class Id
168168
friend istream& operator >>( istream& s, Id& i );
169169

170170
private:
171-
// static void setManager( Manager* m );
172171
unsigned int id_; // Unique identifier for Element*
173-
// unsigned int index_; // Index of array entry within element.
174172
static vector< Element* >& elements();
175173
};
176174

pybind11/Vec.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/***
2+
* Description: moose.vec class.
3+
*
4+
* Created: 2020-03-30
5+
6+
* Author: Dilawar Singh <[email protected]>
7+
* License: GPLv3
8+
*/
9+
10+
#ifndef VEC_H
11+
12+
#define VEC_H
13+
14+
#include <vector>
15+
using namespace std;
16+
17+
class MooseVec {
18+
19+
public:
20+
21+
MooseVec(const string& path, size_t n=1, const string& dtype="Neutral"): path_(path), n_(n), dtype_(dtype)
22+
{
23+
if(! mooseExists(path)) {
24+
objs_.clear();
25+
ObjId o = mooseCreate(dtype, path, n);
26+
for (size_t i = 0; i < n; i++)
27+
objs_.push_back(ObjId(o, i));
28+
}
29+
else {
30+
objs_.clear();
31+
auto o = Id(path);
32+
for (size_t i = 0; i < o.element()->numData(); i++)
33+
objs_.push_back(ObjId(o, i));
34+
}
35+
36+
}
37+
38+
size_t len()
39+
{
40+
return objs_.size();
41+
}
42+
43+
private:
44+
std::string path_;
45+
size_t n_;
46+
const std::string dtype_;
47+
std::vector<ObjId> objs_;
48+
};
49+
50+
51+
#endif /* end of include guard: VEC_H */

pybind11/helper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ Shell* getShellPtr(void)
141141
return reinterpret_cast<Shell*>(Id().eref().data());
142142
}
143143

144-
bool doesExist(const string& path)
144+
bool mooseExists(const string& path)
145145
{
146146
return Id(path) != Id() || path == "/" || path == "/root";
147147
}

pybind11/helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ObjId createIdFromPath(string path, string type, size_t numData = 1);
2626

2727
Shell* getShellPtr();
2828

29-
bool doesExist(const string& path);
29+
bool mooseExists(const string& path);
3030

3131
ObjId mooseElement(const string& path);
3232

pybind11/pymoose.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242

4343
#include "helper.h"
4444
#include "pymoose.h"
45+
4546
#include "Finfo.hpp"
47+
#include "Vec.hpp"
4648

4749
using namespace std;
4850
namespace py = pybind11;
@@ -61,11 +63,10 @@ py::object getValueFinfo(const ObjId& oid, const string& fname, const Finfo* f)
6163
r = pybind11::float_(getProp<double>(oid, fname));
6264
else if (rttType == "float")
6365
r = pybind11::float_(getProp<double>(oid, fname));
64-
else if (rttType == "vector<double>") {
66+
else if (rttType == "vector<double>") {
6567
// r = py::cast(getProp<vector<double>>(oid, fname));
6668
r = getFieldNumpy<double>(oid, fname);
67-
}
68-
else if (rttType == "string")
69+
} else if (rttType == "string")
6970
r = pybind11::str(getProp<string>(oid, fname));
7071
else if (rttType == "char")
7172
r = pybind11::str(getProp<string>(oid, fname));
@@ -115,12 +116,10 @@ py::function getDestFinfo(const ObjId& obj, const string& fname, const Finfo* f)
115116
{
116117
auto rttType = f->rttiType();
117118
if (rttType == "Id") {
118-
std::function<bool(const ObjId& tgt)> f = [obj, fname](const ObjId& tgt) {
119-
return SetGet1<ObjId>::set(obj, fname, tgt);
120-
};
119+
std::function<bool(const ObjId& tgt)> f = [obj, fname](
120+
const ObjId& tgt) { return SetGet1<ObjId>::set(obj, fname, tgt); };
121121
return py::cast(f);
122-
}
123-
else
122+
} else
124123
cout << "NotImplented: Setting " << fname << " with rttType '"
125124
<< rttType << "' on object " << obj.path() << endl;
126125
return py::function();
@@ -252,12 +251,16 @@ PYBIND11_MODULE(_cmoose, m)
252251
//---------------------------------------------------------------------
253252
.def("__repr__", [](const ObjId& oid) {
254253
return "<" + oid.element()->cinfo()->name() + " id=" +
255-
std::to_string(oid.id.value()) + " path=" + oid.path() +
254+
std::to_string(oid.id.value())
255+
+ " numData=" + to_string(oid.element()->numData())
256+
+ " path=" + oid.path() +
256257
">";
257258
});
258259

260+
// Variable.
259261
py::class_<Variable>(m, "_Variable").def(py::init<>());
260262

263+
// Cinfo.
261264
py::class_<Cinfo>(m, "_Cinfo")
262265
.def(py::init<>())
263266
.def_property_readonly("name", &Cinfo::name)
@@ -267,6 +270,7 @@ PYBIND11_MODULE(_cmoose, m)
267270
.def("baseCinfo", &Cinfo::baseCinfo, py::return_value_policy::reference)
268271
.def("isA", &Cinfo::isA);
269272

273+
// Shell
270274
py::class_<Shell>(m, "_Shell")
271275
.def(py::init<>())
272276
.def("create", &Shell::doCreate2)
@@ -278,27 +282,37 @@ PYBIND11_MODULE(_cmoose, m)
278282
.def("start", &Shell::doStart, "runtime"_a, "notify"_a = false)
279283
.def("quit", &Shell::doQuit);
280284

285+
// Vec
286+
py::class_<MooseVec>(m, "vec")
287+
.def(py::init<const string&, size_t, const string&>(),
288+
"path"_a, "n"_a = 1, "dtype"_a = "Neutral") // Default
289+
.def("__len__", &MooseVec::len)
290+
;
291+
281292
// Module functions.
282293
m.def("getShell",
283294
[]() { return reinterpret_cast<Shell*>(Id().eref().data()); },
284295
py::return_value_policy::reference);
285296

286-
m.def("seed", [](size_t a){ moose::mtseed(a);});
287-
m.def("rand", [](double a, double b){ return moose::mtrand(a, b);}, "a"_a=0, "b"_a=1);
297+
m.def("seed", [](size_t a) { moose::mtseed(a); });
298+
m.def("rand", [](double a, double b) { return moose::mtrand(a, b); },
299+
"a"_a = 0, "b"_a = 1);
288300
m.def("wildcardFind", &wildcardFind2);
289301
m.def("delete", &mooseDelete);
290302
m.def("create", &mooseCreate);
291303
m.def("reinit", &mooseReinit);
292304
m.def("start", &mooseStart, "runtime"_a, "notify"_a = false);
293305
m.def("element", &mooseElement);
294-
m.def("exists", &doesExist);
306+
m.def("exists", &mooseExists);
295307
m.def("connect", &mooseConnect);
296308
m.def("getCwe", &mooseGetCwe);
297309
m.def("setClock", &mooseSetClock);
298310
m.def("loadModelInternal", &loadModelInternal);
299-
m.def("getFieldDict", &mooseGetFieldDict, "className"_a, "finfoType"_a = "");
300-
m.def("copy", &mooseCopy, "orig"_a, "newParent"_a, "newName"_a
301-
, "num"_a = 1, "toGlobal"_a = false, "copyExtMsgs"_a = false);
311+
m.def("getFieldDict", &mooseGetFieldDict, "className"_a,
312+
"finfoType"_a = "");
313+
m.def("copy", &mooseCopy, "orig"_a, "newParent"_a, "newName"_a, "num"_a = 1,
314+
"toGlobal"_a = false, "copyExtMsgs"_a = false);
315+
302316
// Attributes.
303317
m.attr("NA") = NA;
304318
m.attr("PI") = PI;

tests/py_moose/test_vec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
def test_vec():
55
foo = moose.Pool('/foo1', 500)
66
bar = moose.vec('/foo1')
7-
assert len(bar) == 500
7+
assert len(bar) == 500, len(bar)
88

99
def test_vec2():
1010
iaf = moose.vec('/iaf', n=10, dtype='IntFire')

0 commit comments

Comments
 (0)