Skip to content

Commit bb1cae5

Browse files
author
Dilawar Singh
committed
Getting there. [skip ci]
1 parent 5cccf8b commit bb1cae5

File tree

8 files changed

+120
-47
lines changed

8 files changed

+120
-47
lines changed

basecode/Cinfo.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,14 @@ const FinfoWrapper Cinfo::findFinfoWrapper(const string& name) const
229229
return FinfoWrapper(findFinfo(name));
230230
}
231231

232-
std::vector<pair<string, string>> Cinfo::getFinfoNameAndType() const
233-
{
234-
vector<pair<string, string>> res;
235-
for (auto& f : srcFinfos_) res.push_back({"srcFinfos", f->name()});
236-
237-
for (auto& f : destFinfos_) res.push_back({"destFinfo", f->name()});
238-
239-
for (auto& f : valueFinfos_) res.push_back({"valueFinfo", f->name()});
240-
241-
for (auto& f : lookupFinfos_) res.push_back({"lookupFinfo", f->name()});
242-
243-
for (auto& f : sharedFinfos_) res.push_back({"sharedFinfo", f->name()});
244-
245-
for (auto& f : fieldElementFinfos_)
246-
res.push_back({"fieldElement", f->name()});
247-
248-
return res;
232+
void Cinfo::getFinfoWithType(std::vector<pair<string, Finfo*>>& res) const
233+
{
234+
for (auto f : srcFinfos_) res.push_back({"srcFinfos", f});
235+
for (auto f : destFinfos_) res.push_back({"destFinfo", f});
236+
for (auto f : valueFinfos_) res.push_back({"valueFinfo", f});
237+
for (auto f : lookupFinfos_) res.push_back({"lookupFinfo", f});
238+
for (auto f : sharedFinfos_) res.push_back({"sharedFinfo", f});
239+
for (auto f : fieldElementFinfos_) res.push_back({"fieldElement", f});
249240
}
250241

251242
bool Cinfo::banCreation() const

basecode/Cinfo.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class Cinfo {
2020
/**
2121
* The Cinfo intializer is used for static initialization
2222
* of all the MOOSE Cinfos. Each MOOSE class must set up
23-
* a function to build its Cinfo. This function must be
24-
* called statically in the MOOSE class .cpp file.
23+
* a function to build its Cinfo. This function must be * called statically in the MOOSE class .cpp file.
2524
* Note how it takes the base *Cinfo as an argument. This
2625
* lets us call the base Cinfo initializer when making
2726
* each Cinfo class, thus ensuring the correct static
@@ -143,8 +142,8 @@ class Cinfo {
143142
*/
144143
const map<string, Finfo*>& finfoMap() const;
145144

146-
// Used in python bindings. Also return type of finfo.
147-
std::vector<std::pair<string,string>> getFinfoNameAndType() const;
145+
// Used in python bindings.
146+
void getFinfoWithType(std::vector<pair<string, Finfo*>>& res) const;
148147

149148
/**
150149
* Returns the Dinfo, which manages creation and destruction
@@ -321,12 +320,8 @@ class Cinfo {
321320
// Useful to know in case we have transient OpFuncs made and
322321
// destroyed.
323322
static unsigned int numCoreOpFunc_;
324-
// map< string, FuncId > opFuncNames_;
325323

326324
static map<string, Cinfo*>& cinfoMap();
327-
328-
// Many opfuncs share same FuncId
329-
// static map< OpFunc*, FuncId >& funcMap();
330325
};
331326

332327
#endif // _CINFO_H

basecode/Conv.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ template< class T > class Conv
102102
return "ObjId";
103103
if ( typeid( T ) == typeid(vector<double>))
104104
return "vector<double>";
105+
if ( typeid( T ) == typeid(vector<Id>))
106+
return "vector<Id>";
107+
if ( typeid( T ) == typeid(vector<ObjId>))
108+
return "vector<ObjId>";
105109
if ( typeid( T ) == typeid(Variable))
106110
return "Variable";
107-
return typeid( T ).name(); // this is not portable but may be more useful than "bad"
111+
return typeid(T).name(); // this is not portable but may be more useful than "bad"
108112
}
109113

110114
private:

pybind11/helper.cpp

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include "../basecode/global.h"
3030
#include "../basecode/Cinfo.h"
3131

32-
3332
#include "../shell/Shell.h"
3433
#include "../shell/Wildcard.h"
3534
#include "../shell/Neutral.h"
@@ -165,7 +164,7 @@ ObjId mooseElement(const string& path)
165164
}
166165

167166
ObjId loadModelInternal(const string& fname, const string& modelpath,
168-
const string& solverclass = "")
167+
const string& solverclass = "")
169168
{
170169
Id model;
171170
if (solverclass.empty()) {
@@ -186,7 +185,8 @@ ObjId getElementField(const ObjId objid, const string& fname)
186185
return ObjId(objid.path() + '/' + fname);
187186
}
188187

189-
ObjId getElementFieldItem(const ObjId& objid, const string& fname, unsigned int index)
188+
ObjId getElementFieldItem(const ObjId& objid, const string& fname,
189+
unsigned int index)
190190
{
191191
ObjId oid = getElementField(objid, fname);
192192

@@ -213,8 +213,8 @@ ObjId getElementFieldItem(const ObjId& objid, const string& fname, unsigned int
213213
return ObjId(oid.id, oid.dataIndex, index);
214214
}
215215

216-
217-
ObjId connect(const ObjId& src, const string& srcField, const ObjId& tgt, const string& tgtField)
216+
ObjId connect(const ObjId& src, const string& srcField, const ObjId& tgt,
217+
const string& tgtField)
218218
{
219219
auto pShell = getShellPtr();
220220
return pShell->doAddMsg("Single", src, srcField, tgt, tgtField);
@@ -248,3 +248,67 @@ py::object mooseGetCwe()
248248
return py::cast(getShellPtr()->getCwe());
249249
}
250250

251+
map<string, string> mooseGetFieldDict(const string& className,
252+
const string& finfoType = "")
253+
{
254+
const Cinfo* cinfo = Cinfo::find(className);
255+
if (! cinfo) {
256+
cout << "Warning: Invalid class " << className << endl;
257+
return {};
258+
}
259+
260+
map<string, string> fieldDict;
261+
if(finfoType == "")
262+
{
263+
auto finfos = cinfo->finfoMap();
264+
for (auto& v : finfos)
265+
fieldDict[v.first] = v.second->rttiType();
266+
return fieldDict;
267+
}
268+
269+
// Now the specific one.
270+
// FIXME: Fix the typeids or remove the 'get' and 'set'
271+
if (finfoType == "valueFinfo" || finfoType == "value") {
272+
for (unsigned int ii = 0; ii < cinfo->getNumValueFinfo(); ++ii) {
273+
auto *finfo = cinfo->getValueFinfo(ii);
274+
fieldDict[finfo->name()] = finfo->rttiType();
275+
}
276+
} else if (finfoType == "srcFinfo" || finfoType == "src") {
277+
for (unsigned int ii = 0; ii < cinfo->getNumSrcFinfo(); ++ii) {
278+
auto *finfo = cinfo->getSrcFinfo(ii);
279+
fieldDict[finfo->name()] = finfo->rttiType();
280+
}
281+
} else if (finfoType == "destFinfo" || finfoType == "dest") {
282+
for (unsigned int ii = 0; ii < cinfo->getNumDestFinfo(); ++ii) {
283+
auto *finfo = cinfo->getDestFinfo(ii);
284+
fieldDict[finfo->name()] = finfo->rttiType();
285+
}
286+
} else if (finfoType == "lookupFinfo" || finfoType == "lookup") {
287+
for (unsigned int ii = 0; ii < cinfo->getNumLookupFinfo(); ++ii) {
288+
auto *finfo = cinfo->getLookupFinfo(ii);
289+
fieldDict[finfo->name()] = finfo->rttiType();
290+
}
291+
} else if (finfoType == "sharedFinfo" || finfoType == "shared") {
292+
for (unsigned int ii = 0; ii < cinfo->getNumSrcFinfo(); ++ii) {
293+
auto *finfo = cinfo->getSrcFinfo(ii);
294+
fieldDict[finfo->name()] = finfo->rttiType();
295+
}
296+
} else if (finfoType == "fieldElementFinfo" || finfoType == "field" ||
297+
finfoType == "fieldElement") {
298+
for (unsigned int ii = 0; ii < cinfo->getNumFieldElementFinfo(); ++ii) {
299+
auto *finfo = cinfo->getFieldElementFinfo(ii);
300+
fieldDict[finfo->name()] = finfo->rttiType();
301+
}
302+
}
303+
return fieldDict;
304+
}
305+
306+
void mooseReinit()
307+
{
308+
getShellPtr()->doReinit();
309+
}
310+
311+
void mooseStart(double runtime, bool notify=false)
312+
{
313+
getShellPtr()->doStart(runtime, notify);
314+
}

pybind11/helper.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// Filename: helper.h
44
//
5-
// Description:
5+
// Description:
66
//
77
// Version: 1.0
88
// Created: 03/22/2020 09:06:16 PM
@@ -22,30 +22,40 @@ using namespace std;
2222

2323
Id initShell();
2424

25-
ObjId createIdFromPath(string path, string type, size_t numData=1);
25+
ObjId createIdFromPath(string path, string type, size_t numData = 1);
2626

2727
Shell* getShellPtr();
2828

2929
bool doesExist(const string& path);
3030

3131
ObjId mooseElement(const string& path);
3232

33-
ObjId loadModelInternal(const string& fname, const string& modelpath, const string& solverclass);
33+
ObjId loadModelInternal(const string& fname, const string& modelpath,
34+
const string& solverclass);
3435

3536
ObjId getElementField(const ObjId objid, const string& fname);
3637

37-
ObjId getElementFieldItem(const ObjId& objid, const string& fname, unsigned int index);
38+
ObjId getElementFieldItem(const ObjId& objid, const string& fname,
39+
unsigned int index);
3840

3941
py::object getFieldGeneric(const ObjId& oid, const string& fname);
4042

41-
ObjId connect(const ObjId& src, const string& srcField, const ObjId& tgt, const string& tgtField);
43+
ObjId connect(const ObjId& src, const string& srcField, const ObjId& tgt,
44+
const string& tgtField);
4245

4346
void mooseDelete(const ObjId& oid);
4447

45-
ObjId mooseCreate(const string type, const string& path, size_t numdata=1);
48+
ObjId mooseCreate(const string type, const string& path, size_t numdata = 1);
4649

4750
py::object mooseGetCwe();
4851

4952
void mooseSetClock(const size_t clockId, double dt);
50-
53+
54+
map<string, string> mooseGetFieldDict(const string& className,
55+
const string& finfoType);
56+
57+
void mooseReinit();
58+
void mooseStart(double runtime, bool notify);
59+
60+
5161
#endif /* end of include guard: HELPER_H */

pybind11/pymoose.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ PYBIND11_MODULE(_cmoose, m)
152152
[](const ObjId oid) { return oid.id.value(); })
153153
.def_property_readonly("path", &ObjId::path)
154154
.def_property_readonly("name", &ObjId::name)
155-
.def_property_readonly("name", &ObjId::name)
155+
.def_property_readonly("className", [](const ObjId& oid){ return oid.element()->cinfo()->name(); })
156156
.def_property_readonly("id", [](ObjId& oid) { return oid.id; })
157157
.def_property_readonly(
158158
"type", [](ObjId& oid) { return oid.element()->cinfo()->name(); })
@@ -208,8 +208,8 @@ PYBIND11_MODULE(_cmoose, m)
208208
.def("setClock", &Shell::doSetClock)
209209
.def("reinit", &Shell::doReinit)
210210
.def("delete", &Shell::doDelete)
211-
.def("start", &Shell::doStart, py::arg("runtime"),
212-
py::arg("notify") = false)
211+
.def("start", &Shell::doStart
212+
, py::arg("runtime"), py::arg("notify") = false)
213213
.def("quit", &Shell::doQuit);
214214

215215
// Module functions.
@@ -220,10 +220,14 @@ PYBIND11_MODULE(_cmoose, m)
220220
m.def("wildcardFind", &wildcardFind2);
221221
m.def("delete", &mooseDelete);
222222
m.def("create", &mooseCreate);
223+
m.def("reinit", &mooseReinit);
224+
m.def("start", &mooseStart, py::arg("runtime"), py::arg("notify") = false);
223225
m.def("element", &mooseElement);
224226
m.def("exists", &doesExist);
225227
m.def("getCwe", &mooseGetCwe);
226228
m.def("setClock", &mooseSetClock);
229+
m.def("loadModelInternal", &loadModelInternal);
230+
m.def("getFieldDict", &mooseGetFieldDict, py::arg("className"), py::arg("finfoType")="");
227231

228232
// Attributes.
229233
m.attr("NA") = NA;

python/moose/moose.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def wildcardFind(pattern):
7171
"""
7272
paths = []
7373
for p in _cmoose.wildcardFind(pattern):
74-
paths.append(p)
74+
paths.append(__Neutral__.fromObjId(p))
7575
return paths
7676

7777
def delete(a):
@@ -82,7 +82,7 @@ def element(pathOrObject):
8282
obj = _cmoose.element(pathOrObject)
8383
else:
8484
obj = _cmoose.element(pathOrObject.path)
85-
return obj
85+
return __Neutral__.fromObjId(obj)
8686

8787
def exists(path):
8888
return _cmoose.exists(path)
@@ -311,8 +311,8 @@ def getFieldDoc(tokens, indent=''):
311311
fieldname = tokens[1]
312312
while True:
313313
try:
314-
classelement = _cmoose.element('/classes/' + classname)
315-
for finfo in classelement.children:
314+
classelement = element('/classes/' + classname)
315+
for fieldelement in classelement.children:
316316
for fieldelement in finfo:
317317
baseinfo = ''
318318
if classname != tokens[0]:

tests/pybind11/test_api.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ def test_children():
1616
moose.le(a1)
1717
moose.le(a2)
1818
moose.le(a3)
19-
2019
s = moose.getCwe()
2120
print(type(s), s)
2221

22+
def test_other():
23+
a1 = moose.Pool('/ada')
24+
print('classname', a1.className)
25+
finfo = moose.getFieldDict(a1.className)
26+
print(finfo)
27+
2328
def main():
2429
test_children()
30+
test_other()
2531

2632
if __name__ == '__main__':
2733
main()
28-

0 commit comments

Comments
 (0)