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

Commit bc95f4d

Browse files
author
Dilawar Singh
committed
Enabled helper functions.
1 parent aeecc41 commit bc95f4d

File tree

9 files changed

+170
-169
lines changed

9 files changed

+170
-169
lines changed

pybind11/helper.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,6 @@ ObjId shellConnect(const ObjId& src, const string& srcField, const ObjId& tgt,
205205
return getShellPtr()->doAddMsg(msgType, src, srcField, tgt, tgtField);
206206
}
207207

208-
//// Python API.
209-
// ObjId mooseConnect(const py::object& src, const string& srcField,
210-
// const py::object& tgt, const string& tgtField,
211-
// const string& msgType)
212-
//{
213-
// // py::print("src", src, src.attr("type"));
214-
// // return src.connect(srcField, tgt, tgtField, msgType);
215-
//}
216-
217208
void mooseMoveId(const Id& a, const ObjId& b)
218209
{
219210
getShellPtr()->doMove(a, b);
@@ -226,11 +217,22 @@ void mooseMoveObjId(const ObjId& a, const ObjId& b)
226217

227218
ObjId mooseCreate(const string type, const string& path, unsigned int numdata)
228219
{
229-
//auto newpath = moose::normalizePath(path);
220+
221+
#if 0
222+
// NOTE: This function is costly because of regex use. But it can be
223+
// enabled later.
224+
auto newpath = moose::normalizePath(path);
225+
#endif
226+
227+
// Split into dirname and basename component.
230228
auto p = moose::splitPath(path);
231-
//if (!mooseExists(p.first))
232-
// throw runtime_error("Parent path " + p.first + " does not exists.");
233-
return getShellPtr()->doCreate2(type, ObjId(p.first), p.second, numdata);
229+
230+
// Name must not end with [\d*] etc. normalizePath takes care of it if
231+
// enabled.
232+
string name(p.second);
233+
if(name.back() == ']')
234+
name = name.substr(0, name.find_last_of('['));
235+
return getShellPtr()->doCreate2(type, ObjId(p.first), name, numdata);
234236
}
235237

236238
void mooseSetClock(const unsigned int clockId, double dt)

pybind11/helper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ inline ObjId mooseObjIdObj(const ObjId& obj)
5252
return ObjId(obj.id, obj.dataIndex, obj.fieldIndex);
5353
}
5454

55+
inline ObjId mooseObjIdId(const Id& id)
56+
{
57+
return ObjId(id);
58+
}
59+
60+
5561
ObjId loadModelInternal(const string& fname, const string& modelpath,
5662
const string& solverclass);
5763

pybind11/pymoose.cpp

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ namespace py = pybind11;
3737
using namespace std;
3838
using namespace pybind11::literals;
3939

40-
Id initModule(py::module &m) { return initShell(); }
40+
Id initModule(py::module &m)
41+
{
42+
return initShell();
43+
}
4144

4245
bool setFieldGeneric(const ObjId &oid, const string &fieldName,
4346
const py::object &val)
@@ -112,12 +115,10 @@ py::object getFieldGeneric(const ObjId &oid, const string &fieldName)
112115
return __Finfo__::getFieldValue(oid, finfo);
113116
else if (finfoType == "FieldElementFinfo") {
114117
return py::cast(__Finfo__(oid, finfo, "FieldElementFinfo"));
115-
}
116-
else if (finfoType == "LookupValueFinfo") {
118+
} else if (finfoType == "LookupValueFinfo") {
117119
// Return function.
118120
return py::cast(__Finfo__(oid, finfo, "LookupValueFinfo"));
119-
}
120-
else if (finfoType == "DestFinfo") {
121+
} else if (finfoType == "DestFinfo") {
121122
// Return a setter function. It can be used to set field on DestFinfo.
122123
return __Finfo__::getDestFinfoSetterFunc(oid, finfo);
123124
}
@@ -152,9 +153,9 @@ PYBIND11_MODULE(_moose, m)
152153
py::class_<__Finfo__>(m, "_Finfo", py::dynamic_attr())
153154
.def(py::init<const ObjId &, const Finfo *, const char *>())
154155
.def_property_readonly("type", &__Finfo__::type)
155-
.def_property_readonly(
156-
"vec",
157-
[](const __Finfo__ &finfo) { return MooseVec(finfo.getObjId()); })
156+
.def_property_readonly("vec", [](const __Finfo__ &finfo) {
157+
return MooseVec(finfo.getObjId());
158+
})
158159
.def_property("num", &__Finfo__::getNumField,
159160
&__Finfo__::setNumField) // Only for FieldElementFinfos
160161
.def("__call__", &__Finfo__::operator())
@@ -171,20 +172,18 @@ PYBIND11_MODULE(_moose, m)
171172
.def_property_readonly("numIds", &Id::numIds)
172173
.def_property_readonly("path", &Id::path)
173174
.def_property_readonly(
174-
"name", [](const Id &id) { return id.element()->getName(); })
175+
"name", [](const Id &id) { return id.element()->getName(); })
175176
.def_property_readonly("id", &Id::value)
176177
.def("__getitem__", [](const Id &id, size_t i) { return ObjId(id, i); })
178+
.def_property_readonly("cinfo",
179+
[](Id &id) { return id.element()->cinfo(); },
180+
py::return_value_policy::reference)
177181
.def_property_readonly(
178-
"cinfo", [](Id &id) { return id.element()->cinfo(); },
179-
py::return_value_policy::reference)
180-
.def_property_readonly(
181-
"type", [](Id &id) { return id.element()->cinfo()->name(); })
182-
.def("__repr__",
183-
[](const Id &id) {
184-
return "<Id id=" + std::to_string(id.value()) +
185-
" path=" + id.path() +
186-
" class=" + id.element()->cinfo()->name() + ">";
187-
})
182+
"type", [](Id &id) { return id.element()->cinfo()->name(); })
183+
.def("__repr__", [](const Id &id) {
184+
return "<Id id=" + std::to_string(id.value()) + " path=" +
185+
id.path() + " class=" + id.element()->cinfo()->name() + ">";
186+
})
188187
/**
189188
* Override __eq__ etc.
190189
*/
@@ -206,17 +205,17 @@ PYBIND11_MODULE(_moose, m)
206205
[](const ObjId &oid) { return MooseVec(oid); })
207206
.def_property_readonly("path", &ObjId::path)
208207
.def_property_readonly(
209-
"parent", [](const ObjId &oid) { return Neutral::parent(oid); })
208+
"parent", [](const ObjId &oid) { return Neutral::parent(oid); })
210209
.def_property_readonly(
211-
"name", [](const ObjId &oid) { return oid.element()->getName(); })
212-
.def_property_readonly(
213-
"className",
214-
[](const ObjId &oid) { return oid.element()->cinfo()->name(); })
210+
"name", [](const ObjId &oid) { return oid.element()->getName(); })
211+
.def_property_readonly("className", [](const ObjId &oid) {
212+
return oid.element()->cinfo()->name();
213+
})
215214
.def_property_readonly("id", [](ObjId &oid) { return oid.id; })
216215
.def_property_readonly(
217-
"dataIndex", [](ObjId &oid) { return oid.eref().dataIndex(); })
216+
"dataIndex", [](ObjId &oid) { return oid.eref().dataIndex(); })
218217
.def_property_readonly(
219-
"type", [](ObjId &oid) { return oid.element()->cinfo()->name(); })
218+
"type", [](ObjId &oid) { return oid.element()->cinfo()->name(); })
220219

221220
//--------------------------------------------------------------------
222221
// Set/Get
@@ -244,24 +243,22 @@ PYBIND11_MODULE(_moose, m)
244243
.def("connect",
245244
[](const ObjId &src, const string &srcfield, const ObjId &tgt,
246245
const string &tgtfield, const string &type) {
247-
return shellConnect(src, srcfield, tgt, tgtfield, type);
248-
})
249-
.def(
250-
"connect",
251-
[](const ObjId &src, const string &srcfield, const MooseVec &tgtvec,
252-
const string &tgtfield, const string &type) {
253-
return shellConnect(src, srcfield, tgtvec.obj(), tgtfield,
254-
type);
255-
})
246+
return shellConnect(src, srcfield, tgt, tgtfield, type);
247+
})
248+
.def("connect", [](const ObjId &src, const string &srcfield,
249+
const MooseVec &tgtvec, const string &tgtfield,
250+
const string &type) {
251+
return shellConnect(src, srcfield, tgtvec.obj(), tgtfield, type);
252+
})
256253
//---------------------------------------------------------------------
257254
// Extra
258255
//---------------------------------------------------------------------
259256
.def("__repr__", [](const ObjId &oid) {
260-
return "<moose." + oid.element()->cinfo()->name() +
261-
" id=" + std::to_string(oid.id.value()) +
262-
" dataIndex=" + to_string(oid.eref().dataIndex()) +
263-
" path=" + oid.path() + ">";
264-
});
257+
return "<moose." + oid.element()->cinfo()->name() + " id=" +
258+
std::to_string(oid.id.value()) + " dataIndex=" +
259+
to_string(oid.eref().dataIndex()) + " path=" + oid.path() +
260+
">";
261+
});
265262

266263
// Variable.
267264
py::class_<Variable>(m, "_Variable").def(py::init<>());
@@ -276,51 +273,46 @@ PYBIND11_MODULE(_moose, m)
276273
.def("baseCinfo", &Cinfo::baseCinfo, py::return_value_policy::reference)
277274
.def("isA", &Cinfo::isA);
278275

279-
// Vec
276+
// Vec class.
280277
py::class_<MooseVec>(m, "vec", py::dynamic_attr())
281278
.def(py::init<const string &, unsigned int, const string &>(), "path"_a,
282279
"n"_a = 1, "dtype"_a = "Neutral") // Default
283280
.def(py::init<const ObjId &>())
284281
.def("connect", &MooseVec::connectToSingle)
285282
.def("connect", &MooseVec::connectToVec)
286283
.def("__len__", &MooseVec::len)
287-
.def(
288-
"__iter__",
289-
[](MooseVec &v) {
290-
// Generate an iterator which is a vector<ObjId>. And then
291-
// pass the reference to the objects.
292-
v.generateIterator();
293-
return py::make_iterator(v.objref().begin(), v.objref().end());
294-
},
295-
py::keep_alive<0, 1>())
284+
.def("__iter__",
285+
[](MooseVec &v) {
286+
// Generate an iterator which is a vector<ObjId>. And then
287+
// pass the reference to the objects.
288+
v.generateIterator();
289+
return py::make_iterator(v.objref().begin(), v.objref().end());
290+
},
291+
py::keep_alive<0, 1>())
296292
.def("__getitem__", &MooseVec::getItem)
297293
.def("__setattr__", &MooseVec::setAttrOneToOne)
298294
.def("__setattr__", &MooseVec::setAttrOneToAll)
299295
.def("__getattr__", &MooseVec::getAttr)
300-
.def("__repr__",
301-
[](const MooseVec &v) -> string {
302-
return "<moose.vec class=" + v.dtype() + " path=" + v.path() +
303-
" id=" + std::to_string(v.id()) +
304-
" size=" + std::to_string(v.size()) + ">";
305-
})
296+
.def("__repr__", [](const MooseVec & v)->string {
297+
return "<moose.vec class=" + v.dtype() + " path=" + v.path() +
298+
" id=" + std::to_string(v.id()) + " size=" +
299+
std::to_string(v.size()) + ">";
300+
})
306301
// This is to provide old API support. Some scripts use .vec even on a
307302
// vec to get a vec. So silly or so Zen?!
308-
.def_property_readonly(
309-
"vec", [](const MooseVec &vec) { return &vec; },
310-
py::return_value_policy::reference_internal)
303+
.def_property_readonly("vec", [](const MooseVec &vec) { return &vec; },
304+
py::return_value_policy::reference_internal)
311305
.def_property_readonly("type",
312306
[](const MooseVec &v) { return "moose.vec"; });
313307

314308
// Module functions.
315-
m.def(
316-
"getShell",
317-
[]() { return reinterpret_cast<Shell *>(Id().eref().data()); },
318-
py::return_value_policy::reference);
309+
m.def("getShell",
310+
[]() { return reinterpret_cast<Shell *>(Id().eref().data()); },
311+
py::return_value_policy::reference);
319312

320313
m.def("seed", [](unsigned int a) { moose::mtseed(a); });
321-
m.def(
322-
"rand", [](double a, double b) { return moose::mtrand(a, b); },
323-
"a"_a = 0, "b"_a = 1);
314+
m.def("rand", [](double a, double b) { return moose::mtrand(a, b); },
315+
"a"_a = 0, "b"_a = 1);
324316
// This is a wrapper to Shell::wildcardFind. The python interface must
325317
// override it.
326318
m.def("wildcardFind", &wildcardFind2);
@@ -334,20 +326,20 @@ PYBIND11_MODULE(_moose, m)
334326
m.def("stop", &mooseStop);
335327
m.def("element", &mooseObjIdPath);
336328
m.def("element", &mooseObjIdObj);
329+
m.def("element", &mooseObjIdId);
337330
m.def("exists", &mooseExists);
338331
m.def("getCwe", &mooseGetCwe);
339332
m.def("setCwe", &mooseSetCwe);
340333
m.def("setClock", &mooseSetClock);
341334
m.def("useClock", &mooseUseClock);
342335
m.def("loadModelInternal", &loadModelInternal);
343336
m.def("getFieldNames", &mooseGetFieldNames);
344-
m.def(
345-
"getField",
346-
[](const ObjId &oid, const string &fieldName, const string &ftype) {
347-
// ftype is not needed anymore.
348-
return getFieldGeneric(oid, fieldName);
349-
},
350-
"el"_a, "fieldname"_a, "ftype"_a = "");
337+
m.def("getField",
338+
[](const ObjId &oid, const string &fieldName, const string &ftype) {
339+
// ftype is not needed anymore.
340+
return getFieldGeneric(oid, fieldName);
341+
},
342+
"el"_a, "fieldname"_a, "ftype"_a = "");
351343
m.def("getFieldDict", &mooseGetFieldDict, "className"_a,
352344
"finfoType"_a = "");
353345
m.def("copy", &mooseCopy, "orig"_a, "newParent"_a, "newName"_a, "num"_a = 1,

python/moose/SBML/readSBML.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,6 @@ def mooseReadSBML(filepath, loadpath, solver="ee",validate="on"):
132132
else:
133133
loadpath ='/'+loadpath
134134
baseId = moose.Neutral(loadpath)
135-
basePath = baseId
136-
137-
# All the model will be created under model as
138-
# a thumbrule
139-
basePath = moose.Neutral(baseId.path)
140135
# Map Compartment's SBML id as key and value is
141136
# list of[ Moose ID and SpatialDimensions ]
142137
global comptSbmlidMooseIdMap
@@ -157,14 +152,14 @@ def mooseReadSBML(filepath, loadpath, solver="ee",validate="on"):
157152
mapParameter(model, globparameterIdValue)
158153
msgCmpt = ""
159154
errorFlag,msgCmpt = createCompartment(
160-
basePath, model, comptSbmlidMooseIdMap)
155+
baseId, model, comptSbmlidMooseIdMap)
161156

162-
groupInfo = checkGroup(basePath,model,comptSbmlidMooseIdMap)
157+
groupInfo = checkGroup(baseId,model,comptSbmlidMooseIdMap)
163158
funcDef = checkFuncDef(model)
164159
if errorFlag:
165160
specInfoMap = {}
166161
errorFlag,warning = createSpecies(
167-
basePath, model, comptSbmlidMooseIdMap, specInfoMap, modelAnnotaInfo,groupInfo)
162+
baseId, model, comptSbmlidMooseIdMap, specInfoMap, modelAnnotaInfo,groupInfo)
168163
if errorFlag:
169164
msgRule = createRules(
170165
model, specInfoMap, globparameterIdValue)
@@ -174,8 +169,7 @@ def mooseReadSBML(filepath, loadpath, solver="ee",validate="on"):
174169
if len(moose.wildcardFind(moose.element(loadpath).path+"/##[ISA=ReacBase],/##[ISA=EnzBase]")) == 0:
175170
errorFlag = False
176171
noRE = ("Atleast one reaction should be present to display in the widget ")
177-
getModelAnnotation(
178-
model, baseId, basePath)
172+
getModelAnnotation(model, baseId)
179173
if not errorFlag:
180174
# Any time in the middle if SBML does not read then I
181175
# delete everything from model level This is important
@@ -383,7 +377,7 @@ def populatedict(annoDict, label, value):
383377
annoDict[label] = {value}
384378

385379

386-
def getModelAnnotation(obj, baseId, basepath):
380+
def getModelAnnotation(obj, baseId):
387381
annotationNode = obj.getAnnotation()
388382
if annotationNode is not None:
389383
numchild = annotationNode.getNumChildren()
@@ -408,7 +402,6 @@ def getModelAnnotation(obj, baseId, basepath):
408402
if(nodeName == "plots"):
409403
plotValue = (
410404
grandChildNode.getChild(0).toXMLString())
411-
p = moose.element(baseId)
412405
datapath = moose.element(baseId).path + "/data"
413406
if not moose.exists(datapath):
414407
datapath = moose.Neutral(baseId.path + "/data")
@@ -419,9 +412,10 @@ def getModelAnnotation(obj, baseId, basepath):
419412
for plots in plotlist:
420413
plots = plots.replace(" ", "")
421414
plotorg = plots
422-
if( moose.exists(basepath.path + plotorg) and isinstance(moose.element(basepath.path+plotorg),moose.PoolBase)) :
415+
if( moose.exists(baseId.path + plotorg) and
416+
isinstance(moose.element(baseId.path+plotorg),moose.PoolBase)) :
423417
plotSId = moose.element(
424-
basepath.path + plotorg)
418+
baseId.path + plotorg)
425419
# plotorg = convertSpecialChar(plotorg)
426420
plot2 = plots.replace('/', '_')
427421
plot3 = plot2.replace('[', '_')

0 commit comments

Comments
 (0)