Skip to content

Commit c7a7b24

Browse files
author
Dilawar Singh
committed
Lets handle LookupField::set and get.
1 parent a06e478 commit c7a7b24

File tree

5 files changed

+121
-59
lines changed

5 files changed

+121
-59
lines changed

basecode/Cinfo.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ void Cinfo::registerFinfo(Finfo* f)
137137
}
138138
}
139139

140+
/* --------------------------------------------------------------------------*/
141+
/**
142+
* @Synopsis The runtime type of Finfo. Use dynamic_cast to figure it out. Use
143+
* RTTI to check if downcast is correct. This is not so costly when used in Python interface.
144+
*
145+
* @Param f const Finfo*
146+
*
147+
* @Returns String representing the polymorphic type of Finfo.
148+
*/
149+
/* ----------------------------------------------------------------------------*/
150+
string Cinfo::getFinfoType(const Finfo* f) const
151+
{
152+
if (dynamic_cast<const DestFinfo*>(f)) {
153+
return "DestInfo";
154+
} else if (dynamic_cast<const SrcFinfo*>(f)) {
155+
return "SrcFinfo";
156+
} else if (dynamic_cast<const ValueFinfoBase*>(f)) {
157+
return "ValueFinfo";
158+
} else if (dynamic_cast<const LookupValueFinfoBase*>(f)) {
159+
return "LookupValueFinfo";
160+
} else if (dynamic_cast<const SharedFinfo*>(f)) {
161+
return "SharedFinfo";
162+
} else if (dynamic_cast<const FieldElementFinfoBase*>(f)) {
163+
return "FieldElementFinfo";
164+
}
165+
return "";
166+
}
167+
140168
void Cinfo::registerPostCreationFinfo(const Finfo* f)
141169
{
142170
postCreationFinfos_.push_back(f);
@@ -229,16 +257,6 @@ const FinfoWrapper Cinfo::findFinfoWrapper(const string& name) const
229257
return FinfoWrapper(findFinfo(name));
230258
}
231259

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});
240-
}
241-
242260
bool Cinfo::banCreation() const
243261
{
244262
return banCreation_;
@@ -291,14 +309,14 @@ const DinfoBase* Cinfo::dinfo() const
291309
bool Cinfo::isA(const string& ancestor) const
292310
{
293311
if (ancestor == "Neutral")
294-
return 1;
312+
return true;
295313
const Cinfo* base = this;
296314
while (base && base != Neutral::initCinfo()) {
297315
if (ancestor == base->name_)
298-
return 1;
316+
return true;
299317
base = base->baseCinfo_;
300318
}
301-
return 0;
319+
return false;
302320
}
303321

304322
void Cinfo::reportFids() const

basecode/Cinfo.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ class Cinfo {
106106
*/
107107
const std::string& name() const;
108108

109+
/**
110+
* Returns the type of Finfo.
111+
*/
112+
string getFinfoType(const Finfo* finfo) const;
113+
109114
/**
110115
* Finds the Cinfo with the specified name.
111116
*/
@@ -142,9 +147,6 @@ class Cinfo {
142147
*/
143148
const map<string, Finfo*>& finfoMap() const;
144149

145-
// Used in python bindings.
146-
void getFinfoWithType(std::vector<pair<string, Finfo*>>& res) const;
147-
148150
/**
149151
* Returns the Dinfo, which manages creation and destruction
150152
* of the data, and knows about its size.

basecode/Conv.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,6 @@ template< class T > class Conv
100100
return "Id";
101101
if ( typeid( T ) == typeid(ObjId))
102102
return "ObjId";
103-
if ( typeid( T ) == typeid(vector<double>))
104-
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>";
109-
if ( typeid( T ) == typeid(Variable))
110-
return "Variable";
111103
return typeid(T).name(); // this is not portable but may be more useful than "bad"
112104
}
113105

pybind11/pymoose.cpp

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../external/pybind11/include/pybind11/pybind11.h"
2424
#include "../external/pybind11/include/pybind11/stl.h"
2525
#include "../external/pybind11/include/pybind11/numpy.h"
26+
#include "../external/pybind11/include/pybind11/functional.h"
2627

2728
// See
2829
// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#binding-stl-containers
@@ -51,7 +52,7 @@ Id initModule(py::module& m)
5152
}
5253

5354
template <typename T = double>
54-
void setProp(const ObjId& id, const string& fname, T val)
55+
void setProperty(const ObjId& id, const string& fname, T val)
5556
{
5657
Field<T>::set(id, fname, val);
5758
}
@@ -71,50 +72,96 @@ py::array_t<T> getFieldNumpy(const ObjId& id, const string& fname)
7172
return py::array_t<T>(v.size(), v.data());
7273
}
7374

74-
py::object getFieldGeneric(const ObjId& oid, const string& fname)
75+
py::object getPropertyValueFinfo(const ObjId& oid, const string& fname, const string& rttType)
7576
{
76-
auto cinfo = oid.element()->cinfo();
77-
auto finfo = cinfo->findFinfo(fname);
78-
79-
if (!finfo) {
80-
py::print("Field " + fname + " is not found on " + oid.path());
81-
return pybind11::none();
82-
}
83-
84-
string ftype = finfo->rttiType();
85-
if (ftype == "double")
77+
if (rttType == "double")
8678
return pybind11::float_(getProp<double>(oid, fname));
87-
else if (ftype == "float")
79+
else if (rttType == "float")
8880
return pybind11::float_(getProp<double>(oid, fname));
89-
else if (ftype == "vector<double>")
81+
else if (rttType == "vector<double>")
9082
return py::cast(getProp<vector<double>>(oid, fname));
91-
else if (ftype == "string")
83+
else if (rttType == "string")
9284
return pybind11::str(getProp<string>(oid, fname));
93-
else if (ftype == "char")
85+
else if (rttType == "char")
9486
return pybind11::str(getProp<string>(oid, fname));
95-
else if (ftype == "int")
87+
else if (rttType == "int")
9688
return pybind11::int_(getProp<int>(oid, fname));
97-
else if (ftype == "unsigned long")
89+
else if (rttType == "unsigned long")
9890
return pybind11::int_(getProp<unsigned long>(oid, fname));
99-
else if (ftype == "unsigned int")
91+
else if (rttType == "unsigned int")
10092
return pybind11::int_(getProp<unsigned int>(oid, fname));
101-
else if (ftype == "bool")
93+
else if (rttType == "bool")
10294
return pybind11::bool_(getProp<bool>(oid, fname));
103-
else if (ftype == "Id")
95+
else if (rttType == "Id")
10496
return py::cast(getProp<Id>(oid, fname));
105-
else if (ftype == "ObjId")
97+
else if (rttType == "ObjId")
10698
return py::cast(getProp<ObjId>(oid, fname));
107-
else if (ftype == "Variable")
99+
else if (rttType == "Variable")
108100
return py::cast(getProp<Variable>(oid, fname));
109-
else if (ftype == "vector<Id>")
101+
else if (rttType == "vector<Id>")
110102
return py::cast(getProp<vector<Id>>(oid, fname));
111-
else if (ftype == "vector<ObjId>")
103+
else if (rttType == "vector<ObjId>")
112104
return py::cast(getProp<vector<ObjId>>(oid, fname));
105+
py::print("Warning: pymoose::getProperty::Warning: Unsupported type " + rttType);
106+
return py::none();
107+
}
108+
109+
py::list getPropertyElementFinfo(const ObjId& objid, const string& fname)
110+
{
111+
auto oid = ObjId(objid.path() + '/' + fname);
112+
auto len = Field<unsigned int>::get(oid, "numField");
113+
assert(len >= 0);
114+
vector<ObjId> res(len);
115+
for (size_t i = 0; i < len; i++)
116+
res[i] = ObjId(oid.path(), oid.dataIndex, i);
117+
return py::cast(res);
118+
}
119+
120+
template<typename A, typename L>
121+
py::object getLookValueFinfo(const ObjId& oid, const string& fname, const string& key)
122+
{
123+
return py::cast(LookupField<A, L>::get(oid, fname, key));
124+
}
125+
126+
py::dict getPropertyLookValueFinfo(const ObjId& oid, const string& fname)
127+
{
128+
py::dict res;
129+
auto v = [oid, fname](const string& key){
130+
return LookupField<string, bool>::get(oid, fname, key);
131+
};
132+
return res;
133+
}
113134

114-
py::print("Warning: pymoose::getFieldGeneric::Warning: Unsupported type " + ftype);
135+
py::object getProperty(const ObjId& oid, const string& fname)
136+
{
137+
auto cinfo = oid.element()->cinfo();
138+
auto finfo = cinfo->findFinfo(fname);
139+
140+
if (!finfo) {
141+
py::print("Field " + fname + " is not found on " + oid.path());
142+
return pybind11::none();
143+
}
144+
145+
string rttType = finfo->rttiType();
146+
string finfoType = cinfo->getFinfoType(finfo);
147+
148+
if(finfoType == "ValueFinfo")
149+
return getPropertyValueFinfo(oid, fname, rttType);
150+
else if(finfoType == "FieldElementFinfo")
151+
return getPropertyElementFinfo(oid, fname);
152+
else if(finfoType == "LookupValueFinfo")
153+
return getPropertyLookValueFinfo(oid, fname);
154+
155+
cout << "Searching for " << fname << " with rttType "
156+
<< rttType << " and type: " << finfoType << endl;
157+
158+
159+
py::print("Warning: pymoose::getProperty::Warning: Unsupported type " + rttType);
115160
return pybind11::none();
116161
}
117162

163+
164+
118165
PYBIND11_MODULE(_cmoose, m)
119166
{
120167
m.doc() = R"moosedoc(moose module.)moosedoc";
@@ -161,18 +208,20 @@ PYBIND11_MODULE(_cmoose, m)
161208
// Set/Get
162209
//--------------------------------------------------------------------
163210
// Overload of Field::set
164-
.def("setField", &setProp<double>)
165-
.def("setField", &setProp<double>)
166-
.def("setField", &setProp<vector<double>>)
167-
.def("setField", &setProp<std::string>)
168-
.def("setField", &setProp<bool>)
211+
.def("setField", &setProperty<double>)
212+
.def("setField", &setProperty<double>)
213+
.def("setField", &setProperty<vector<double>>)
214+
.def("setField", &setProperty<std::string>)
215+
.def("setField", &setProperty<bool>)
169216

170217
// Overload for Field::get
171-
.def("getField", &getFieldGeneric)
218+
.def("getField", &getProperty)
219+
172220
.def("getElementField", &getElementField)
173221
.def("getElementFieldItem", &getElementFieldItem)
174222
.def("getNumpy", &getFieldNumpy<double>)
175223

224+
176225
//---------------------------------------------------------------------
177226
// Connect
178227
//---------------------------------------------------------------------

tests/pybind11/test_shell.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ def makereac2():
227227
r1.connect("sub", A, "reac")
228228
r1.connect("prd", B, "reac")
229229

230-
A.connect("nOut", sum.getElementFieldItem("x", 0), "input")
230+
# A.connect("nOut", sum.getElementFieldItem("x", 0), "input")
231+
print("xxx", sum.x[0])
232+
A.connect("nOut", sum.x[0], "input")
231233
B.connect("nOut", sum.getElementFieldItem("x", 1), "input")
232234
sum.connect("valueOut", tot1, "setN");
233235

@@ -332,9 +334,8 @@ def test_ksolve2():
332334
def test_ksolve3():
333335
a = moose.Neutral('x')
334336
a = moose.Neutral('x/x')
335-
isA = a.getElementField('isA')
336-
print(a, isA)
337337
print(a.isA)
338+
print(a.isA['Compartment'])
338339

339340
def main():
340341
test_ksolve0()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy