Skip to content

Commit 77451f1

Browse files
author
Dilawar Singh
committed
All the learning experience is over. Pybind11 is great.
1 parent 6edda7a commit 77451f1

File tree

19 files changed

+383
-289
lines changed

19 files changed

+383
-289
lines changed

basecode/Cinfo.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -477,11 +477,6 @@ Finfo* Cinfo::getLookupFinfo(unsigned int i) const
477477
return lookupFinfos_[i];
478478
}
479479

480-
const FinfoWrapper Cinfo::getLookupFinfoWrapper(unsigned int i) const
481-
{
482-
return FinfoWrapper(getLookupFinfo(i));
483-
}
484-
485480
unsigned int Cinfo::getNumLookupFinfo() const
486481
{
487482
if (baseCinfo_)
@@ -505,11 +500,6 @@ Finfo* Cinfo::getSharedFinfo(unsigned int i)
505500
return sharedFinfos_[i];
506501
}
507502

508-
FinfoWrapper Cinfo::getSharedFinfoWrapper(unsigned int i)
509-
{
510-
return FinfoWrapper(getSharedFinfo(i));
511-
}
512-
513503
unsigned int Cinfo::getNumSharedFinfo() const
514504
{
515505
if (baseCinfo_)
@@ -534,11 +524,6 @@ Finfo* Cinfo::getFieldElementFinfo(unsigned int i) const
534524
return fieldElementFinfos_[i];
535525
}
536526

537-
const FinfoWrapper Cinfo::getFieldElementFinfoWrapper(unsigned int i) const
538-
{
539-
return FinfoWrapper(getFieldElementFinfo(i));
540-
}
541-
542527
unsigned int Cinfo::getNumFieldElementFinfo() const
543528
{
544529
if (baseCinfo_)

basecode/Cinfo.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class Cinfo {
123123
* Returns 0 on failure.
124124
*/
125125
const Finfo* findFinfo(const string& name) const;
126+
127+
// This if for python binding.
126128
const FinfoWrapper findFinfoWrapper(const string& name) const;
127129

128130
/**
@@ -178,7 +180,6 @@ class Cinfo {
178180
* Return the specified SrcFinfo
179181
*/
180182
Finfo* getSrcFinfo(unsigned int i) const;
181-
FinfoWrapper getSrcFinfoWrapper(unsigned int i) const;
182183

183184
/**
184185
* Return number of SrcFinfos
@@ -189,7 +190,6 @@ class Cinfo {
189190
* Return the specified DestFinfo
190191
*/
191192
Finfo* getDestFinfo(unsigned int i) const;
192-
FinfoWrapper getDestFinfoWrapper(unsigned int i) const;
193193

194194
/**
195195
* Return number of DestFinfo
@@ -200,7 +200,6 @@ class Cinfo {
200200
* Return the specified ValueFinfo
201201
*/
202202
Finfo* getValueFinfo(unsigned int i) const;
203-
FinfoWrapper getValueFinfoWrapper(unsigned int i) const;
204203

205204
/**
206205
* Return number of ValueFinfo
@@ -211,7 +210,6 @@ class Cinfo {
211210
* Return the specified LookupFinfo
212211
*/
213212
Finfo* getLookupFinfo(unsigned int i) const;
214-
const FinfoWrapper getLookupFinfoWrapper(unsigned int i) const;
215213

216214
/**
217215
* Return number of LookupFinfo
@@ -222,7 +220,6 @@ class Cinfo {
222220
* Return the specified SharedFinfo
223221
*/
224222
Finfo* getSharedFinfo(unsigned int i);
225-
FinfoWrapper getSharedFinfoWrapper(unsigned int i);
226223

227224
/**
228225
* Return number of SharedFinfos
@@ -233,7 +230,6 @@ class Cinfo {
233230
* Return the specified FieldElementFinfo
234231
*/
235232
Finfo* getFieldElementFinfo(unsigned int i) const;
236-
const FinfoWrapper getFieldElementFinfoWrapper(unsigned int i) const;
237233

238234
/**
239235
* Return number of FieldElementFinfos

pybind11/helper.cpp

Lines changed: 71 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Filename: helper.cpp
44
//
5-
// Description: Helper functions.
5+
// Description: Helper functions.
66
//
77
// Version: 1.0
88
// Created: 03/22/2020 09:05:13 PM
@@ -34,39 +34,40 @@ Id initShell(void)
3434

3535
Id shellId;
3636

37-
Element* shelle = new GlobalDataElement(shellId, Shell::initCinfo(), "/", 1 );
37+
Element* shelle =
38+
new GlobalDataElement(shellId, Shell::initCinfo(), "/", 1);
3839

3940
Id clockId = Id::nextId();
40-
assert( clockId.value() == 1 );
41+
assert(clockId.value() == 1);
4142
Id classMasterId = Id::nextId();
4243
Id postMasterId = Id::nextId();
4344

44-
Shell* s = reinterpret_cast< Shell* >( shellId.eref().data() );
45+
Shell* s = reinterpret_cast<Shell*>(shellId.eref().data());
4546
s->setHardware(1, 1, 0);
46-
s->setShellElement( shelle );
47+
s->setShellElement(shelle);
4748

4849
/// Sets up the Elements that represent each class of Msg.
4950
auto numMsg = Msg::initMsgManagers();
5051

5152
new GlobalDataElement(clockId, Clock::initCinfo(), "clock", 1);
5253
new GlobalDataElement(classMasterId, Neutral::initCinfo(), "classes", 1);
53-
new GlobalDataElement(postMasterId, PostMaster::initCinfo(), "postmaster", 1);
54+
new GlobalDataElement(postMasterId, PostMaster::initCinfo(), "postmaster",
55+
1);
5456

55-
assert ( shellId == Id() );
56-
assert( clockId == Id( 1 ) );
57-
assert( classMasterId == Id( 2 ) );
58-
assert( postMasterId == Id( 3 ) );
57+
assert(shellId == Id());
58+
assert(clockId == Id(1));
59+
assert(classMasterId == Id(2));
60+
assert(postMasterId == Id(3));
5961

60-
Shell::adopt( shellId, clockId, numMsg++ );
61-
Shell::adopt( shellId, classMasterId, numMsg++ );
62-
Shell::adopt( shellId, postMasterId, numMsg++ );
63-
assert( numMsg == 10 ); // Must be the same on all nodes.
62+
Shell::adopt(shellId, clockId, numMsg++);
63+
Shell::adopt(shellId, classMasterId, numMsg++);
64+
Shell::adopt(shellId, postMasterId, numMsg++);
65+
assert(numMsg == 10); // Must be the same on all nodes.
6466

65-
Cinfo::makeCinfoElements( classMasterId );
67+
Cinfo::makeCinfoElements(classMasterId);
6668
return shellId;
6769
}
6870

69-
7071
/**
7172
Utility function to create objects from full path, dimensions
7273
and classname.
@@ -77,56 +78,88 @@ Id createIdFromPath(string path, string type, size_t numData)
7778
string parent_path;
7879
string name;
7980

80-
string trimmed_path = moose::trim( path );
81+
string trimmed_path = moose::trim(path);
8182
size_t pos = trimmed_path.rfind("/");
82-
if (pos != string::npos)
83-
{
84-
name = trimmed_path.substr(pos+1);
83+
if (pos != string::npos) {
84+
name = trimmed_path.substr(pos + 1);
8585
parent_path = trimmed_path.substr(0, pos);
86-
}
87-
else
88-
{
86+
} else {
8987
name = trimmed_path;
9088
}
9189
// handle relative path
92-
if (trimmed_path[0] != '/')
93-
{
90+
if (trimmed_path[0] != '/') {
9491
string current_path = pShell->getCwe().path();
9592
if (current_path != "/")
9693
parent_path = current_path + "/" + parent_path;
9794
else
9895
parent_path = current_path + parent_path;
99-
}
100-
else if (parent_path.empty())
96+
} else if (parent_path.empty())
10197
parent_path = "/";
10298

10399
ObjId parent_id(parent_path);
104-
if (parent_id.bad() )
105-
{
100+
if (parent_id.bad()) {
106101
string message = "Parent element does not exist: ";
107102
message += parent_path;
108103
throw std::runtime_error(message);
109104
return Id();
110105
}
111106

112-
Id nId = pShell->doCreate(type,
113-
parent_id,
114-
string(name),
115-
numData,
116-
MooseGlobal
117-
);
107+
Id nId =
108+
pShell->doCreate(type, parent_id, string(name), numData, MooseGlobal);
118109

119-
if (nId == Id() && trimmed_path != "/" && trimmed_path != "/root")
120-
{
110+
if (nId == Id() && trimmed_path != "/" && trimmed_path != "/root") {
121111
string message = "no such moose class : " + type;
122112
throw std::runtime_error(message);
123113
}
124114

125115
return nId;
126116
}
127117

128-
129118
Shell* const getShellPtr(void)
130119
{
131120
return reinterpret_cast<Shell*>(Id().eref().data());
132121
}
122+
123+
bool exists(const string& path)
124+
{
125+
return Id(path) != Id() || path == "/" || path == "/root";
126+
}
127+
128+
bool exists(const Id& id)
129+
{
130+
return exists(id.path());
131+
}
132+
133+
Id element(const string& path)
134+
{
135+
ObjId oid;
136+
unsigned nid = 0, did = 0, fidx = 0;
137+
Id id;
138+
unsigned int numData = 0;
139+
140+
oid = ObjId(path);
141+
if (oid.bad()) {
142+
throw runtime_error(std::string("moose_element: '") +
143+
std::string(path) +
144+
std::string("' does not exist!"));
145+
return Id();
146+
}
147+
return oid;
148+
}
149+
150+
Id loadModelInternal(const string& fname, const string& modelpath,
151+
const string& solverclass = "")
152+
{
153+
Id model;
154+
if (solverclass.empty()) {
155+
model = getShellPtr()->doLoadModel(fname, modelpath);
156+
} else {
157+
model = getShellPtr()->doLoadModel(fname, modelpath, solverclass);
158+
}
159+
160+
if (model == Id()) {
161+
throw runtime_error("could not load model");
162+
return Id();
163+
}
164+
return ObjId(model);
165+
}

pybind11/helper.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,11 @@ Id createIdFromPath(string path, string type, size_t numData=1);
2525

2626
Shell* const getShellPtr();
2727

28+
bool exists(const string& path);
29+
bool exists(const Id& id);
30+
31+
Id element(const string& id);
32+
33+
Id loadModelInternal(const string& fname, const string& modelpath, const string& solverclass);
34+
2835
#endif /* end of include guard: HELPER_H */

pybind11/pymoose.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@
3737
using namespace std;
3838
namespace py = pybind11;
3939

40+
void defineFinfos(py::object& cls, const string& cname)
41+
{
42+
auto pCinfo = Cinfo::find(cname);
43+
assert(pCinfo);
44+
45+
cerr << "\n====================================================== " << endl;
46+
cerr << "Adding Finfos to class " << cls << " with name " << cname << endl;
47+
48+
cerr << "ValueFinfo: ";
49+
for(size_t i = 0; i < pCinfo->getNumValueFinfo(); i++)
50+
{
51+
auto pFinfo = pCinfo->getValueFinfo(i);
52+
cerr << pFinfo->name() << " ";
53+
}
54+
cerr << endl;
55+
56+
cerr << "FieldElementFinfo: ";
57+
for(size_t i = 0; i < pCinfo->getNumFieldElementFinfo(); i++)
58+
{
59+
auto pFinfo = pCinfo->getFieldElementFinfo(i);
60+
cout << pFinfo->name() << " ";
61+
}
62+
cerr << endl;
63+
cerr << "DestFinfos: ";
64+
for(size_t i = 0; i < pCinfo->getNumDestFinfo(); i++)
65+
{
66+
auto pFinfo = pCinfo->getDestFinfo(i);
67+
cout << pFinfo->name() << " ";
68+
}
69+
cerr << endl;
70+
}
71+
4072
void initModule(py::module& m)
4173
{
4274
initShell();
@@ -61,8 +93,15 @@ PYBIND11_MODULE(_cmoose, m)
6193
m.doc() = R"moosedoc(moose module.
6294
)moosedoc";
6395

64-
py::class_<Id>(m, "_Id").def(py::init<>()).def_property_readonly(
65-
"value", &Id::value);
96+
py::class_<Id>(m, "_Id").def(py::init<>())
97+
.def_property_readonly("value", &Id::value)
98+
.def_property_readonly("path", [](Id id){return id.path("/"); })
99+
// Return self.
100+
.def_property_readonly("id", [](Id& id){return id;})
101+
.def_property_readonly("type"
102+
, [](Id& id){ return id.element()->cinfo()->name(); }
103+
)
104+
;
66105

67106
py::class_<ObjId>(m, "_ObjId")
68107
.def(py::init<>())
@@ -91,6 +130,10 @@ PYBIND11_MODULE(_cmoose, m)
91130

92131
m.def("create", &createIdFromPath);
93132

133+
m.def("exists", &createIdFromPath);
134+
135+
m.def("element", &element);
136+
94137
m.def("move", [](Id o, ObjId oid){ getShellPtr()->doMove(o, oid); });
95138
m.def("copy", [](Id o, ObjId newP, string newName="", size_t n=1, bool toGlobal=false, bool copyExtMsg=false){
96139
if(newName.empty())
@@ -115,6 +158,9 @@ PYBIND11_MODULE(_cmoose, m)
115158

116159
m.def("_wildcardFind", &wildcardFindPybind);
117160

161+
m.def("loadModelInternal", &loadModelInternal);
162+
163+
118164
m.def("getProperty", &getProperty<double>);
119165
m.def("getProperty", &getProperty<vector<double>>);
120166
m.def("getProperty", &getProperty<string>);
@@ -129,5 +175,8 @@ PYBIND11_MODULE(_cmoose, m)
129175
m.def("setProperty", &setProperty<unsigned int>);
130176
m.def("setProperty", &setProperty<bool>);
131177

178+
// TODO:
179+
m.def("__defineFinfos", &defineFinfos);
180+
132181
m.attr("__version__") = MOOSE_VERSION;
133182
}

0 commit comments

Comments
 (0)