Skip to content

Commit c286653

Browse files
Dilawar Singhhrani
Dilawar Singh
andauthored
Typo and partial fixes to Issue 426 (#427)
* Fixed typo #426 * getFieldNames is available for element also Both are equivalent >>> a = moose.Stoich('a') >>> v1 = moose.getFieldNames(a, 'value') >>> v2 = a.getFieldNames('value') >>> assert v1 == v2 If not argument is passed to `getFieldNames`, it returns are field types. Co-authored-by: HarshaRani <ranishashi@gmail.com>
1 parent d861533 commit c286653

File tree

5 files changed

+65
-39
lines changed

5 files changed

+65
-39
lines changed

ksolve/Stoich.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ bool Stoich::getAllowNegative() const
278278

279279
void Stoich::setPath(const Eref& e, string v)
280280
{
281-
cout << "DeprecationWarning:: Use Soitch::readSystemPath instead. In "
281+
cout << "DeprecationWarning:: Use Soitch::reacSystemPath instead. In "
282282
"the future, it will be an error."
283283
<< endl;
284284
setReacSystemPath(e, v);
@@ -328,13 +328,15 @@ void Stoich::setElist(const Eref& e, const vector<ObjId>& elist)
328328
{
329329
if(compartment_ == Id()) {
330330
cerr << "Warning: Stoich::setElist/setReacSystemPath: Compartment not "
331-
"set. Aborting." << endl;
331+
"set. Aborting."
332+
<< endl;
332333
status_ = 4;
333334
return;
334335
}
335336
if(!(kinterface_ || dinterface_)) {
336337
cerr << "Warning: Stoich::setElist/setReacSystemPath: Neither solver "
337-
"has been set. Aborting." << endl;
338+
"has been set. Aborting."
339+
<< endl;
338340
status_ = 8;
339341
return;
340342
}
@@ -346,8 +348,10 @@ void Stoich::setElist(const Eref& e, const vector<ObjId>& elist)
346348
vector<Id> temp;
347349
filterWildcards(temp, elist);
348350
if(temp.size() == 0) {
349-
cerr << "Warning: Stoich::setElist/setReacSystemPath: No kinetics objects "
350-
"found on path. Aborting." << endl;
351+
cerr << "Warning: Stoich::setElist/setReacSystemPath: No kinetics "
352+
"objects "
353+
"found on path. Aborting."
354+
<< endl;
351355
status_ = 16;
352356
return;
353357
}

pybind11/helper.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -400,37 +400,41 @@ vector<string> mooseGetFieldNames(const string& className,
400400
return ret;
401401
}
402402

403-
if(finfoType == "valueFinfo" || finfoType == "value") {
403+
if(finfoType == "valueFinfo" || finfoType == "value" || finfoType == "*") {
404404
for(unsigned int ii = 0; ii < cinfo->getNumValueFinfo(); ++ii) {
405405
Finfo* finfo = cinfo->getValueFinfo(ii);
406406
ret.push_back(finfo->name());
407407
}
408408
}
409-
else if(finfoType == "srcFinfo" || finfoType == "src") {
409+
else if(finfoType == "srcFinfo" || finfoType == "src" || finfoType == "*") {
410410
for(unsigned int ii = 0; ii < cinfo->getNumSrcFinfo(); ++ii) {
411411
Finfo* finfo = cinfo->getSrcFinfo(ii);
412412
ret.push_back(finfo->name());
413413
}
414414
}
415-
else if(finfoType == "destFinfo" || finfoType == "dest") {
415+
else if(finfoType == "destFinfo" || finfoType == "dest" ||
416+
finfoType == "*") {
416417
for(unsigned int ii = 0; ii < cinfo->getNumDestFinfo(); ++ii) {
417418
Finfo* finfo = cinfo->getDestFinfo(ii);
418419
ret.push_back(finfo->name());
419420
}
420421
}
421-
else if(finfoType == "lookupFinfo" || finfoType == "lookup") {
422+
else if(finfoType == "lookupFinfo" || finfoType == "lookup" ||
423+
finfoType == "*") {
422424
for(unsigned int ii = 0; ii < cinfo->getNumLookupFinfo(); ++ii) {
423425
Finfo* finfo = cinfo->getLookupFinfo(ii);
424426
ret.push_back(finfo->name());
425427
}
426428
}
427-
else if(finfoType == "sharedFinfo" || finfoType == "shared") {
429+
else if(finfoType == "sharedFinfo" || finfoType == "shared" ||
430+
finfoType == "*") {
428431
for(unsigned int ii = 0; ii < cinfo->getNumSrcFinfo(); ++ii) {
429432
Finfo* finfo = cinfo->getSrcFinfo(ii);
430433
ret.push_back(finfo->name());
431434
}
432435
}
433-
else if(finfoType == "fieldElementFinfo" || finfoType == "fieldElement") {
436+
else if(finfoType == "fieldElementFinfo" || finfoType == "fieldElement" ||
437+
finfoType == "*") {
434438
for(unsigned int ii = 0; ii < cinfo->getNumFieldElementFinfo(); ++ii) {
435439
Finfo* finfo = cinfo->getFieldElementFinfo(ii);
436440
ret.push_back(finfo->name());
@@ -545,7 +549,6 @@ string mooseDoc(const string& query)
545549
throw runtime_error(__func__ + string(":: Not supported '" + query + "'"));
546550
}
547551

548-
549552
vector<string> mooseLe(const ObjId& obj)
550553
{
551554
vector<Id> children;
@@ -557,7 +560,7 @@ vector<string> mooseLe(const ObjId& obj)
557560
Neutral::children(obj.eref(), children);
558561
stringstream ss;
559562
ss << "Elements under " << obj.path() << endl;
560-
for(auto ch: children) {
563+
for(auto ch : children) {
561564
ss << " " + ch.path() << endl;
562565
chPaths.push_back(ch.path());
563566
}
@@ -571,7 +574,7 @@ vector<ObjId> mooseListMsg(const ObjId& obj)
571574
auto inmsgs = Field<vector<ObjId>>::get(obj, "msgIn");
572575
for(const auto inobj : inmsgs) {
573576
const Msg* msg = Msg::getMsg(inobj);
574-
if(! msg) {
577+
if(!msg) {
575578
cerr << "No Msg found on " << obj.path() << endl;
576579
continue;
577580
}
@@ -592,28 +595,28 @@ string mooseShowMsg(const ObjId& obj)
592595
auto inmsgs = Field<vector<ObjId>>::get(obj, "msgIn");
593596
for(const auto inobj : inmsgs) {
594597
const Msg* msg = Msg::getMsg(inobj);
595-
if(! msg) {
598+
if(!msg) {
596599
cerr << "No Msg found on " << obj.path() << endl;
597600
continue;
598601
}
599-
ss << fmt::format(" {0}, [{1}] <-- {2}, [{3}]\n", msg->getE2().path()
600-
, moose::vectorToCSV<string>(msg->getDestFieldsOnE2())
601-
, msg->getE1().path()
602-
, moose::vectorToCSV<string>(msg->getSrcFieldsOnE1()));
602+
ss << fmt::format(" {0}, [{1}] <-- {2}, [{3}]\n", msg->getE2().path(),
603+
moose::vectorToCSV<string>(msg->getDestFieldsOnE2()),
604+
msg->getE1().path(),
605+
moose::vectorToCSV<string>(msg->getSrcFieldsOnE1()));
603606
}
604607
ss << endl;
605608
auto outmsgs = Field<vector<ObjId>>::get(obj, "msgOut");
606609
ss << "OUTGOING:" << endl;
607610
for(const auto outobj : outmsgs) {
608611
const Msg* msg = Msg::getMsg(outobj);
609-
if(! msg) {
612+
if(!msg) {
610613
cerr << "No Msg found on " << obj.path() << endl;
611614
continue;
612615
}
613-
ss << fmt::format(" {0}, [{1}] <-- {2}, [{3}]\n", msg->getE1().path()
614-
, moose::vectorToCSV<string>(msg->getSrcFieldsOnE1())
615-
, msg->getE2().path()
616-
, moose::vectorToCSV<string>(msg->getDestFieldsOnE2()));
616+
ss << fmt::format(" {0}, [{1}] <-- {2}, [{3}]\n", msg->getE1().path(),
617+
moose::vectorToCSV<string>(msg->getSrcFieldsOnE1()),
618+
msg->getE2().path(),
619+
moose::vectorToCSV<string>(msg->getDestFieldsOnE2()));
617620
}
618621
return ss.str();
619622
}

pybind11/pymoose.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ PYBIND11_MODULE(_moose, m)
280280
[](ObjId &oid) { return oid.dataIndex; })
281281
.def("getDataIndex", [](const ObjId &oid) { return oid.dataIndex; })
282282

283+
.def(
284+
"getFieldNames",
285+
[](const ObjId &oid, const string &fieldtype) {
286+
return mooseGetFieldNames(oid.element()->cinfo()->name(),
287+
fieldtype);
288+
},
289+
"fieldtype"_a = "*")
290+
283291
.def_property_readonly("fieldIndex",
284292
[](ObjId &oid) { return oid.fieldIndex; })
285293

@@ -384,8 +392,9 @@ PYBIND11_MODULE(_moose, m)
384392
*/
385393

386394
m.def("seed", [](py::object &a) { moose::mtseed(a.cast<int>()); });
387-
m.def("rand", [](double a, double b) { return moose::mtrand(a, b); },
388-
"a"_a = 0, "b"_a = 1);
395+
m.def(
396+
"rand", [](double a, double b) { return moose::mtrand(a, b); },
397+
"a"_a = 0, "b"_a = 1);
389398
// This is a wrapper to Shell::wildcardFind. The python interface must
390399
// override it.
391400
m.def("wildcardFind", &wildcardFind2);

python/moose/__init__.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ def getFieldDict(classname, finfoType=""):
284284
Returns
285285
-------
286286
dict
287-
field names and their types.
287+
field names and their respective types as key-value pair.
288288
289289
Notes
290290
-----
@@ -301,27 +301,29 @@ def getFieldDict(classname, finfoType=""):
301301
return _moose.getFieldDict(classname, finfoType)
302302

303303

304-
def getFieldNames(classname, fieldtype="*"):
305-
"""Get a tuple containing the name of all the fields of `finfoType` kind.
304+
def getFieldNames(elem, fieldtype="*"):
305+
"""Get a tuple containing name of fields of a given fieldtype. If
306+
fieldtype is set to '*', all fields are returned.
306307
307308
Parameters
308309
----------
309-
className : string
310-
Name of the class to look up.
311-
finfoType : string
312-
The kind of field
313-
- valueFinfo
314-
- srcFinfo
315-
- destFinfo
316-
- lookupFinfo
317-
- fieldElementFinfo
310+
elem : string, obj
311+
Name of the class or a moose.element to look up.
312+
fieldtype : string
313+
The kind of field. Possible values are:
314+
- 'valueFinfo' or 'value'
315+
- 'srcFinfo' or 'src'
316+
- 'destFinfo' or 'dest'
317+
- 'lookupFinfo' or 'lookup'
318+
- 'fieldElementFinfo' or 'fieldElement'
318319
319320
Returns
320321
-------
321322
list
322323
Names of the fields of type `finfoType` in class `className`.
323324
"""
324-
return _moose.getFieldNames(classname, fieldtype)
325+
clsname = elem if isinstance(elem, str) else elem.className
326+
return _moose.getFieldNames(clsname, fieldtype)
325327

326328

327329
def isRunning():

tests/core/test_api.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def test_other():
4545
p.delay[1] = 0.99
4646
assert p.delay[1] == 0.99, p.delay[1]
4747

48+
c = moose.Stoich('/dadaa')
49+
v1 = moose.getFieldNames(c)
50+
v2 = c.getFieldNames()
51+
assert v1 == v2
52+
assert len(v1) > 10, v1
53+
54+
4855

4956
def test_vec():
5057
a = moose.Pool('/p111', 100)
@@ -126,6 +133,7 @@ def test_inheritance():
126133
assert isinstance(aa, moose.CubeMesh), (a.__class__, aa.__class__)
127134

128135
a = moose.CubeMesh('yapf')
136+
assert a.isA('CubeMesh') == a.isA['CubeMesh']
129137
assert a.isA['CubeMesh']
130138
assert a.isA['ChemCompt']
131139

0 commit comments

Comments
 (0)