Skip to content

Commit dd9b50e

Browse files
author
Dilawar Singh
committed
Perfect. One more test passes.
1 parent fec019b commit dd9b50e

File tree

8 files changed

+131
-104
lines changed

8 files changed

+131
-104
lines changed

pybind11/MooseVec.cpp

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ namespace py = pybind11;
1919
#include "pymoose.h"
2020
#include "MooseVec.h"
2121

22-
MooseVec::MooseVec(const string& path, unsigned int n = 1, const string& dtype = "Neutral")
23-
: path_(path)
22+
MooseVec::MooseVec(const string& path, unsigned int n = 1,
23+
const string& dtype = "Neutral")
24+
: path_(path)
2425
{
2526
if (!mooseExists(path)) {
2627
objs_.clear();
@@ -60,9 +61,14 @@ unsigned int MooseVec::len()
6061
return (unsigned int)size();
6162
}
6263

63-
const ObjId& MooseVec::getItem(const size_t i) const
64+
const ObjId& MooseVec::getItemRef(const size_t i) const
6465
{
65-
return objs_.at(i);
66+
return objs_[i];
67+
}
68+
69+
ObjId MooseVec::getItem(const size_t i) const
70+
{
71+
return objs_[i];
6672
}
6773

6874
void MooseVec::setAttrOneToAll(const string& name, const py::object& val)
@@ -72,12 +78,12 @@ void MooseVec::setAttrOneToAll(const string& name, const py::object& val)
7278

7379
void MooseVec::setAttrOneToOne(const string& name, const py::sequence& val)
7480
{
75-
if ( py::len(val) != objs_.size())
81+
if (py::len(val) != objs_.size())
7682
throw runtime_error(
77-
"Length of sequence on the right hand side "
78-
"does not match size of vector. "
79-
"Expected " +
80-
to_string(objs_.size()) + ", got " + to_string(py::len(val)));
83+
"Length of sequence on the right hand side "
84+
"does not match size of vector. "
85+
"Expected " +
86+
to_string(objs_.size()) + ", got " + to_string(py::len(val)));
8187
for (size_t i = 0; i < objs_.size(); i++)
8288
setFieldGeneric(objs_[i], name, val[i]);
8389
}
@@ -94,3 +100,26 @@ const vector<ObjId>& MooseVec::objs() const
94100
{
95101
return objs_;
96102
}
103+
104+
ObjId MooseVec::connectToSingle(const string& srcfield, const ObjId& tgt,
105+
const string& tgtfield, const string& msgtype)
106+
{
107+
ObjId res;
108+
for (const auto& obj : objs_)
109+
res = mooseConnect(obj, srcfield, tgt, tgtfield, msgtype);
110+
return res;
111+
}
112+
113+
ObjId MooseVec::connectToVec(const string& srcfield, const MooseVec& tgt,
114+
const string& tgtfield, const string& msgtype)
115+
{
116+
if (objs_.size() != tgt.size())
117+
throw runtime_error(
118+
"Length mismatch. Source vector size is " + to_string(size()) +
119+
" but the target vector size is " + to_string(tgt.size()));
120+
121+
ObjId res;
122+
for (size_t i = 0; i < size(); i++)
123+
res = mooseConnect(objs_[i], srcfield, tgt.getItem(i), tgtfield, msgtype);
124+
return res;
125+
}

pybind11/MooseVec.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class MooseVec {
2525

2626
unsigned int len();
2727

28-
const ObjId& getItem(const size_t i) const;
28+
const ObjId& getItemRef(const size_t i) const;
29+
ObjId getItem(const size_t i) const;
2930

3031
void setAttrOneToAll(const string& name, const py::object& val);
3132

@@ -35,6 +36,10 @@ class MooseVec {
3536

3637
const vector<ObjId>& objs() const;
3738

39+
ObjId connectToSingle(const string& srcfield, const ObjId& tgt, const string& tgtfield, const string& msgtype);
40+
41+
ObjId connectToVec(const string& srcfield, const MooseVec& tgt, const string& tgtfield, const string& msgtype);
42+
3843
private:
3944
std::string path_;
4045
std::vector<ObjId> objs_;

pybind11/helper.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ ObjId getElementFieldItem(const ObjId& objid, const string& fname,
214214
}
215215

216216
ObjId mooseConnect(const ObjId& src, const string& srcField, const ObjId& tgt,
217-
const string& tgtField)
217+
const string& tgtField, const string& msgType)
218218
{
219219
auto pShell = getShellPtr();
220-
return pShell->doAddMsg("Single", src, srcField, tgt, tgtField);
220+
return pShell->doAddMsg(msgType, src, srcField, tgt, tgtField);
221221
}
222222

223223
bool mooseDelete(const ObjId& oid)
@@ -241,6 +241,11 @@ void mooseSetClock(const unsigned int clockId, double dt)
241241
getShellPtr()->doSetClock(clockId, dt);
242242
}
243243

244+
void mooseUseClock(size_t tick, const string& path, const string& field)
245+
{
246+
getShellPtr()->doUseClock(path, field, tick);
247+
}
248+
244249
/* --------------------------------------------------------------------------*/
245250
/**
246251
* @Synopsis Current Working Element.

pybind11/helper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ObjId getElementFieldItem(const ObjId& objid, const string& fname,
4242
unsigned int index);
4343

4444
ObjId mooseConnect(const ObjId& src, const string& srcField, const ObjId& tgt,
45-
const string& tgtField);
45+
const string& tgtField, const string& msgType);
4646

4747
bool mooseDelete(const ObjId& oid);
4848
bool mooseDelete(const string& path);
@@ -56,6 +56,8 @@ py::object mooseGetCwe();
5656

5757
void mooseSetClock(const unsigned int clockId, double dt);
5858

59+
void mooseUseClock(size_t tick, const string& path, const string& field);
60+
5961
map<string, string> mooseGetFieldDict(const string& className,
6062
const string& finfoType);
6163

pybind11/pymoose.cpp

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
#include <utility>
2121
#include <vector>
2222

23-
#include "../external/pybind11/include/pybind11/functional.h"
24-
#include "../external/pybind11/include/pybind11/numpy.h"
2523
#include "../external/pybind11/include/pybind11/pybind11.h"
2624
#include "../external/pybind11/include/pybind11/stl.h"
25+
#include "../external/pybind11/include/pybind11/numpy.h"
26+
// #include "../external/pybind11/include/pybind11/functional.h"
27+
namespace py = pybind11;
2728

2829
// See https://pybind11.readthedocs.io/en/master/classes.html#overloaded-methods
2930
template <typename... Args>
3031
using overload_cast_ = pybind11::detail::overload_cast_impl<Args...>;
3132

32-
#include "../basecode/global.h"
3333
#include "../basecode/header.h"
34+
#include "../basecode/global.h"
3435
#include "../basecode/Cinfo.h"
3536
#include "../builtins/Variable.h"
3637
#include "../shell/Neutral.h"
@@ -46,17 +47,16 @@ using overload_cast_ = pybind11::detail::overload_cast_impl<Args...>;
4647

4748
#include "pymoose.h"
4849

49-
5050
using namespace std;
51-
namespace py = pybind11;
5251
using namespace pybind11::literals;
5352

5453
Id initModule(py::module& m)
5554
{
5655
return initShell();
5756
}
5857

59-
bool setFieldGeneric(const ObjId& oid, const string& fieldName, const py::object& val)
58+
bool setFieldGeneric(const ObjId& oid, const string& fieldName,
59+
const py::object& val)
6060
{
6161
auto cinfo = oid.element()->cinfo();
6262
auto finfo = cinfo->findFinfo(fieldName);
@@ -66,30 +66,33 @@ bool setFieldGeneric(const ObjId& oid, const string& fieldName, const py::object
6666
}
6767
auto fieldType = finfo->rttiType();
6868

69-
if(fieldType == "double")
69+
if (fieldType == "double")
7070
return Field<double>::set(oid, fieldName, val.cast<double>());
71-
if(fieldType == "float")
71+
if (fieldType == "float")
7272
return Field<float>::set(oid, fieldName, val.cast<float>());
73-
if(fieldType == "unsigned int")
74-
return Field<unsigned int>::set(oid, fieldName, val.cast<unsigned int>());
75-
if(fieldType == "unsigned long")
76-
return Field<unsigned long>::set(oid, fieldName, val.cast<unsigned long>());
77-
if(fieldType == "int")
73+
if (fieldType == "unsigned int")
74+
return Field<unsigned int>::set(oid, fieldName,
75+
val.cast<unsigned int>());
76+
if (fieldType == "unsigned long")
77+
return Field<unsigned long>::set(oid, fieldName,
78+
val.cast<unsigned long>());
79+
if (fieldType == "int")
7880
return Field<int>::set(oid, fieldName, val.cast<int>());
79-
if(fieldType == "bool")
81+
if (fieldType == "bool")
8082
return Field<bool>::set(oid, fieldName, val.cast<bool>());
81-
if(fieldType == "string")
83+
if (fieldType == "string")
8284
return Field<string>::set(oid, fieldName, val.cast<string>());
83-
if(fieldType == "char")
85+
if (fieldType == "char")
8486
return Field<char>::set(oid, fieldName, val.cast<char>());
85-
if(fieldType == "ObjId")
87+
if (fieldType == "ObjId")
8688
return Field<ObjId>::set(oid, fieldName, val.cast<ObjId>());
87-
if(fieldType == "Id")
89+
if (fieldType == "Id")
8890
return Field<Id>::set(oid, fieldName, val.cast<Id>());
89-
if(fieldType == "Variable")
91+
if (fieldType == "Variable")
9092
return Field<Variable>::set(oid, fieldName, val.cast<Variable>());
9193

92-
throw runtime_error("NotImplemented: setField for " + fieldName + " with value type " + fieldType);
94+
throw runtime_error("NotImplemented: setField for " + fieldName +
95+
" with value type " + fieldType);
9396
return false;
9497
}
9598

@@ -99,7 +102,8 @@ py::object getFieldGeneric(const ObjId& oid, const string& fieldName)
99102
auto finfo = cinfo->findFinfo(fieldName);
100103

101104
if (!finfo) {
102-
cout << "Error: " << fieldName << " is not found on " << oid.path() << endl;
105+
cout << "Error: " << fieldName << " is not found on " << oid.path()
106+
<< endl;
103107
return pybind11::none();
104108
}
105109

@@ -123,38 +127,12 @@ py::object getFieldGeneric(const ObjId& oid, const string& fieldName)
123127
return pybind11::none();
124128
}
125129

126-
template<>
127-
ObjId connectVec(const MooseVec& src, const string& srcField, const ObjId& tgt, const string& tgtField)
128-
{
129-
ObjId result;
130-
for (const auto& obj : src.objs())
131-
result = connect<ObjId, ObjId>(obj, srcField, tgt, tgtField);
132-
return result;
133-
}
134-
135-
template<>
136-
ObjId connectVec(const ObjId& src, const string& srcField, const MooseVec& tgt, const string& tgtField)
137-
{
138-
ObjId result;
139-
for (const auto& obj : tgt.objs())
140-
result = connect<ObjId, ObjId>(src, srcField, obj, tgtField);
141-
return result;
142-
}
143-
144-
template<>
145-
ObjId connectVec(const MooseVec& src, const string& srcField, const MooseVec& tgt, const string& tgtField)
146-
{
147-
ObjId result;
148-
if (src.size() != tgt.size())
149-
throw runtime_error(
150-
"Size mismatch for source and target vectors. Source size " +
151-
std::to_string(src.size()) + ", target size " +
152-
std::to_string(tgt.size()) + ".");
153-
for (size_t i = 0; i < src.size(); i++)
154-
result = connect<ObjId, ObjId>(src.objs()[i], srcField, tgt.objs()[i], tgtField);
155-
return result;
156-
}
157-
130+
// ObjId connectGeneric(const py::object& src, const string srcfield,
131+
// const py::object& tgt, const string& tgtfield,
132+
// const string& type)
133+
// {
134+
// return src.connect(srcfield, tgt, tgtfield, type);
135+
// }
158136

159137
PYBIND11_MODULE(_cmoose, m)
160138
{
@@ -233,12 +211,12 @@ PYBIND11_MODULE(_cmoose, m)
233211
// .def("getElementField", &getElementField)
234212
// .def("getElementFieldItem", &getElementFieldItem)
235213
// .def("getNumpy", &getFieldNumpy<double>)
236-
214+
237215
/**
238216
* Override __eq__ etc.
239217
*/
240-
.def("__eq__", [](const ObjId& a, const ObjId& b){ return a==b; })
241-
.def("__ne__", [](const ObjId& a, const ObjId& b){ return a!=b; })
218+
.def("__eq__", [](const ObjId& a, const ObjId& b) { return a == b; })
219+
.def("__ne__", [](const ObjId& a, const ObjId& b) { return a != b; })
242220

243221
/**
244222
* Attributes.
@@ -261,14 +239,30 @@ PYBIND11_MODULE(_cmoose, m)
261239
//---------------------------------------------------------------------
262240
// Connect
263241
//---------------------------------------------------------------------
264-
.def("connect", &connect<ObjId, ObjId>)
265-
.def("connect", &connect<ObjId, Id>)
266-
.def("connect", &connect<Id, ObjId>)
267-
.def("connect", &connect<Id, Id>)
268242
// This would be so much easier with c++17.
269-
.def("connect", &connectVec<MooseVec, ObjId>)
270-
.def("connect", &connectVec<ObjId, MooseVec>)
271-
.def("connect", &connectVec<MooseVec, MooseVec>)
243+
.def("connect",
244+
[](const ObjId& src, const string& srcfield, const ObjId& tgt,
245+
const string& tgtfield, const string& type) {
246+
return mooseConnect(src, srcfield, tgt, tgtfield, type);
247+
})
248+
// // , "src"_a, "srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a)
249+
// //, "src"_a, "srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a =
250+
// //"OneToAll")
251+
// .def("connect", &connectVec<MooseVec, MooseVec>)
252+
// // , "src"_a, //"srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a =
253+
// // "OneToOne")
254+
// .def("connect", &connect<ObjId, ObjId>)
255+
// // , "src"_a, "srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a =
256+
// // "Single")
257+
// .def("connect", &connect<ObjId, Id>)
258+
// // , "src"_a, "srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a =
259+
// // "Single")
260+
// .def("connect", &connect<Id, ObjId>)
261+
// // , "src"_a, "srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a =
262+
// // "Single")
263+
// .def("connect", &connect<Id, Id>)
264+
// // , "src"_a, "srcfield"_a, "dest"_a, "destfield"_a, "msgtype"_a =
265+
// // "Single")
272266

273267
//---------------------------------------------------------------------
274268
// Extra
@@ -310,6 +304,8 @@ PYBIND11_MODULE(_cmoose, m)
310304
.def(py::init<const string&, unsigned int, const string&>(), "path"_a,
311305
"n"_a = 1, "dtype"_a = "Neutral") // Default
312306
.def(py::init<const ObjId&>())
307+
.def("connect", &MooseVec::connectToSingle)
308+
.def("connect", &MooseVec::connectToVec)
313309
.def("__len__", &MooseVec::len)
314310
.def("__iter__",
315311
[](const MooseVec& v) {
@@ -327,7 +323,9 @@ PYBIND11_MODULE(_cmoose, m)
327323
// This is to provide old API support. Some scripts use .vec even on a
328324
// vec to get a vec. So silly or so Zen?!
329325
.def_property_readonly("vec", [](const MooseVec& vec) { return &vec; },
330-
py::return_value_policy::reference_internal);
326+
py::return_value_policy::reference_internal)
327+
.def_property_readonly("type",
328+
[](const MooseVec& v) { return "moose.vec"; });
331329

332330
// Module functions.
333331
m.def("getShell",
@@ -346,9 +344,9 @@ PYBIND11_MODULE(_cmoose, m)
346344
m.def("element", overload_cast_<const ObjId&>()(&mooseElement));
347345
m.def("element", overload_cast_<const string&>()(&mooseElement));
348346
m.def("exists", &mooseExists);
349-
m.def("connect", &mooseConnect);
350347
m.def("getCwe", &mooseGetCwe);
351348
m.def("setClock", &mooseSetClock);
349+
m.def("useClock", &mooseUseClock);
352350
m.def("loadModelInternal", &loadModelInternal);
353351
m.def("getFieldDict", &mooseGetFieldDict, "className"_a,
354352
"finfoType"_a = "");

pybind11/pymoose.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,8 @@ py::array_t<T> getFieldNumpy(const ObjId& id, const string& fname)
4343
return py::array_t<T>(v.size(), v.data());
4444
}
4545

46-
template <typename P = ObjId, typename Q = ObjId>
47-
ObjId connect(const P& src, const string& srcField, const Q& tgt,
48-
const string& tgtField)
49-
{
50-
return mooseConnect(ObjId(src), srcField, ObjId(tgt), tgtField);
51-
}
52-
53-
template<typename P, typename Q>
54-
ObjId connectVec(const P& src, const string& srcField, const Q& tgt, const string& tgtField);
55-
56-
57-
bool setFieldGeneric(const ObjId& id, const string& frname, const py::object& val);
46+
bool setFieldGeneric(const ObjId& id, const string& frname,
47+
const py::object& val);
5848

5949
py::object getFieldGeneric(const ObjId& oid, const string& fname);
6050

0 commit comments

Comments
 (0)