Skip to content

Commit 070f735

Browse files
author
Dilawar Singh
committed
Perfect. The base API works now. Not time to do a profiling.
1 parent 07a1348 commit 070f735

File tree

5 files changed

+122
-132
lines changed

5 files changed

+122
-132
lines changed

basecode/Finfo.cpp

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,32 @@ Finfo::Finfo(const string& name, const string& doc) : name_(name), doc_(doc)
2121

2222
const Cinfo* Finfo::initCinfo()
2323
{
24-
static ReadOnlyValueFinfo< FinfoWrapper, string > fieldName(
25-
"fieldName",
26-
"Name of field handled by Finfo",
27-
&FinfoWrapper::getName
28-
);
29-
30-
static ReadOnlyValueFinfo< FinfoWrapper, string > docs(
31-
"docs",
32-
"Documentation for Finfo",
33-
&FinfoWrapper::docs
34-
);
35-
36-
static ReadOnlyValueFinfo< FinfoWrapper, string > type(
37-
"type",
38-
"RTTI type info for this Finfo",
39-
&FinfoWrapper::type
40-
);
41-
42-
static ReadOnlyValueFinfo< FinfoWrapper, vector< string > > src(
43-
"src",
44-
"Subsidiary SrcFinfos. Useful for SharedFinfos",
45-
&FinfoWrapper::src
46-
);
47-
static ReadOnlyValueFinfo< FinfoWrapper, vector< string > > dest(
48-
"dest",
49-
"Subsidiary DestFinfos. Useful for SharedFinfos",
50-
&FinfoWrapper::dest
51-
);
52-
53-
54-
static Finfo* finfoFinfos[] =
55-
{
56-
&fieldName, // ReadOnlyValue
57-
&docs, // ReadOnlyValue
58-
&type, // ReadOnlyValue
59-
&src, // ReadOnlyValue
60-
&dest, // ReadOnlyValue
24+
static ReadOnlyValueFinfo<FinfoWrapper, string> fieldName(
25+
"fieldName", "Name of field handled by Finfo", &FinfoWrapper::getName);
26+
27+
static ReadOnlyValueFinfo<FinfoWrapper, string> docs(
28+
"docs", "Documentation for Finfo", &FinfoWrapper::docs);
29+
30+
static ReadOnlyValueFinfo<FinfoWrapper, string> type(
31+
"type", "RTTI type info for this Finfo", &FinfoWrapper::type);
32+
33+
static ReadOnlyValueFinfo<FinfoWrapper, vector<string>> src(
34+
"src", "Subsidiary SrcFinfos. Useful for SharedFinfos",
35+
&FinfoWrapper::src);
36+
static ReadOnlyValueFinfo<FinfoWrapper, vector<string>> dest(
37+
"dest", "Subsidiary DestFinfos. Useful for SharedFinfos",
38+
&FinfoWrapper::dest);
39+
40+
static Finfo* finfoFinfos[] = {&fieldName, // ReadOnlyValue
41+
&docs, // ReadOnlyValue
42+
&type, // ReadOnlyValue
43+
&src, // ReadOnlyValue
44+
&dest, // ReadOnlyValue
6145
};
6246

63-
static Dinfo< Finfo* > dinfo;
64-
static Cinfo finfoCinfo (
65-
"Finfo",
66-
Neutral::initCinfo(),
67-
finfoFinfos,
68-
sizeof( finfoFinfos ) / sizeof( Finfo* ),
69-
&dinfo
70-
);
47+
static Dinfo<Finfo*> dinfo;
48+
static Cinfo finfoCinfo("Finfo", Neutral::initCinfo(), finfoFinfos,
49+
sizeof(finfoFinfos) / sizeof(Finfo*), &dinfo);
7150

7251
return &finfoCinfo;
7352
}
@@ -76,59 +55,57 @@ static const Cinfo* finfoCinfo = Finfo::initCinfo();
7655

7756
////////////////////////////////////////////////////////////////
7857

79-
const string& Finfo::name( ) const
58+
const string& Finfo::name() const
8059
{
8160
return name_;
8261
}
8362

84-
const string& Finfo::docs( ) const
63+
const string& Finfo::docs() const
8564
{
8665
return doc_;
8766
}
8867

8968
////////////////////////////////////////////////////////////////
9069
// Needed to access as a MOOSE field. Note that the Finfo is stored as a ptr
91-
string FinfoWrapper::getName( ) const
70+
string FinfoWrapper::getName() const
9271
{
9372
return f_->name();
9473
}
9574

96-
string FinfoWrapper::docs( ) const
75+
string FinfoWrapper::docs() const
9776
{
9877
return f_->docs();
9978
}
10079

101-
102-
string FinfoWrapper::type( ) const
80+
string FinfoWrapper::type() const
10381
{
10482
return f_->rttiType();
10583
}
10684

107-
vector< string > FinfoWrapper::src( ) const
85+
vector<string> FinfoWrapper::src() const
10886
{
10987
return f_->innerSrc();
11088
}
11189

112-
vector< string > FinfoWrapper::dest( ) const
90+
vector<string> FinfoWrapper::dest() const
11391
{
11492
return f_->innerDest();
11593
}
11694

117-
11895
// Default virtual functions.
11996
string Finfo::rttiType() const
12097
{
121-
return typeid( *this ).name();
98+
return typeid(*this).name();
12299
}
123100

124-
vector< string > Finfo::innerSrc() const
101+
vector<string> Finfo::innerSrc() const
125102
{
126-
static vector< string > ret;
103+
static vector<string> ret;
127104
return ret;
128105
}
129106

130-
vector< string > Finfo::innerDest() const
107+
vector<string> Finfo::innerDest() const
131108
{
132-
static vector< string > ret;
109+
static vector<string> ret;
133110
return ret;
134111
}

basecode/Finfo.h

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
#ifndef _FINFO_H
1010
#define _FINFO_H
1111

12-
class Finfo
13-
{
12+
class Finfo {
1413
public:
15-
Finfo( const string& name, const string& doc );
14+
Finfo(const string& name, const string& doc);
1615
virtual ~Finfo()
1716
{
1817
;
@@ -35,23 +34,23 @@ class Finfo
3534
/**
3635
* Assign function Ids, bindIndex and so on.
3736
*/
38-
virtual void registerFinfo( Cinfo* c ) = 0;
37+
virtual void registerFinfo(Cinfo* c) = 0;
3938

4039
/**
4140
* Function to set this field using a string argument.
4241
* Returns true on success.
4342
* Normally called only from SetGet::strSet.
4443
*/
45-
virtual bool strSet( const Eref& tgt, const string& field,
46-
const string& arg ) const = 0;
44+
virtual bool strSet(const Eref& tgt, const string& field,
45+
const string& arg) const = 0;
4746

4847
/**
4948
* Function to return value of field into a string argument.
5049
* Returns true on success.
5150
* Normally called only from SetGet::strGet.
5251
*/
53-
virtual bool strGet( const Eref& tgt, const string& field,
54-
string& returnValue ) const = 0;
52+
virtual bool strGet(const Eref& tgt, const string& field,
53+
string& returnValue) const = 0;
5554

5655
/**
5756
* This function is called on each new Element after
@@ -60,7 +59,7 @@ class Finfo
6059
* as soon as the parent is made.
6160
* Used in FieldElementFinfo.h
6261
*/
63-
virtual void postCreationFunc( Id newId, Element* newElm ) const
62+
virtual void postCreationFunc(Id newId, Element* newElm) const
6463
{
6564
;
6665
}
@@ -78,7 +77,7 @@ class Finfo
7877
* Is called only from msg src, so most Finfos return 0.
7978
* SrcFinfo and SharedFinfo will need to implement this.
8079
*/
81-
virtual bool checkTarget( const Finfo* target) const
80+
virtual bool checkTarget(const Finfo* target) const
8281
{
8382
return 0;
8483
}
@@ -89,7 +88,7 @@ class Finfo
8988
* Returns 1 on success.
9089
* Defaults to 0 because many Finfo combinations won't work.
9190
*/
92-
virtual bool addMsg( const Finfo* target, ObjId mid, Element* src ) const
91+
virtual bool addMsg(const Finfo* target, ObjId mid, Element* src) const
9392
{
9493
return 0;
9594
}
@@ -130,12 +129,12 @@ class Finfo
130129
/**
131130
* Returns subsidiary SrcFinfos
132131
*/
133-
virtual vector< string > innerSrc() const;
132+
virtual vector<string> innerSrc() const;
134133

135134
/**
136135
* Returns subsidiary DestFinfos
137136
*/
138-
virtual vector< string > innerDest() const;
137+
virtual vector<string> innerDest() const;
139138

140139
/**
141140
* Returns the Class Info for this Finfo
@@ -148,32 +147,31 @@ class Finfo
148147
};
149148

150149
// Wrapper to handle the access functions to the Finfo objects.
151-
class FinfoWrapper
152-
{
150+
class FinfoWrapper {
153151
public:
154-
FinfoWrapper( const Finfo* other ):f_( other )
152+
FinfoWrapper(const Finfo* other) : f_(other)
155153
{
156154
;
157155
}
158156

159-
string getName( ) const;
160-
string docs( ) const;
161-
string type( ) const;
157+
string getName() const;
158+
string docs() const;
159+
string type() const;
162160
/**
163161
* Returns the list of subsidiary SrcFinfos.
164162
* Used in Shared Finfos, empty otherwise. Goes via
165163
* internal virtual function.
166164
*/
167-
vector< string > src( ) const;
165+
vector<string> src() const;
168166
/**
169167
* Returns the list of subsidiary destFinfos.
170168
* Used in SharedFinfos and ValueFinfos, empty otherwise. Goes via
171169
* internal virtual function.
172170
*/
173-
vector< string > dest( ) const;
171+
vector<string> dest() const;
174172

175173
private:
176174
const Finfo* f_;
177175
};
178176

179-
#endif // _FINFO_H
177+
#endif // _FINFO_H

pybind11/pymoose.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,37 @@ py::array_t<T> getFieldNumpy(const ObjId& id, const string& fname)
6969
return py::array_t<T>(v.size(), v.data());
7070
}
7171

72-
py::object getFieldElement(const ObjId& oid, const string& fname)
72+
inline ObjId getElementField(const ObjId objid, const string& fname)
7373
{
74-
auto cinfo = oid.element()->cinfo();
75-
auto finfo = cinfo->findFinfo(fname);
76-
if (! finfo) {
77-
py::object avl = py::cast(cinfo->getFinfoNameAndType());
78-
py::print("Field " + fname + " is not found on '" + oid.path()
79-
+ "'. Available: ", avl);
80-
return pybind11::none();
74+
return ObjId(objid.path() + '/' + fname);
75+
}
76+
77+
ObjId getElementFieldItem(const ObjId& objid, const string& fname,
78+
unsigned int index)
79+
{
80+
ObjId oid = getElementField(objid, fname);
81+
82+
auto len = Field<unsigned int>::get(oid, "numField");
83+
assert(len >= 0);
84+
85+
if (index >= len) {
86+
throw runtime_error(
87+
"ElementField.getItem: index out of bounds. "
88+
"Total elements=" +
89+
to_string(len) + ".");
90+
return ObjId();
91+
}
92+
93+
// Negative indexing. Thanks Subha for hint.
94+
if (index < 0) {
95+
index += len;
8196
}
82-
// FIXME: I am here.
83-
Variable res = LookupField<unsigned int, Variable>::get(oid, fname, 0);
84-
return py::cast(res);
97+
if (index < 0) {
98+
throw runtime_error("ElementField.getItem: invalid index: " +
99+
to_string(index) + ".");
100+
return ObjId();
101+
}
102+
return ObjId(oid.id, oid.dataIndex, index);
85103
}
86104

87105
py::object getFieldGeneric(const ObjId& oid, const string& fname)
@@ -129,6 +147,7 @@ ObjId connect(const ObjId& src, const string& srcField, const ObjId& tgt,
129147
return pShell->doAddMsg("Single", src, srcField, tgt, tgtField);
130148
}
131149

150+
132151
PYBIND11_MODULE(_cmoose, m)
133152
{
134153
m.doc() = R"moosedoc(moose module.)moosedoc";
@@ -186,7 +205,8 @@ PYBIND11_MODULE(_cmoose, m)
186205
// See discussion here: https://github.com/pybind/pybind11/issues/1667
187206
// Required c++14 compiler.
188207
.def("getField", &getFieldGeneric)
189-
.def("getFieldElement", &getFieldElement)
208+
.def("getElementField", &getElementField)
209+
.def("getElementFieldItem", &getElementFieldItem)
190210
.def("getNumpy", &getFieldNumpy<double>)
191211

192212
//---------------------------------------------------------------------
@@ -203,9 +223,7 @@ PYBIND11_MODULE(_cmoose, m)
203223
">";
204224
});
205225

206-
py::class_<Variable>(m, "_Variable")
207-
.def(py::init<>())
208-
;
226+
py::class_<Variable>(m, "_Variable").def(py::init<>());
209227

210228
py::class_<Cinfo>(m, "_Cinfo")
211229
.def(py::init<>())

python/moose/moose.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def __repr__(self):
4141
return self.cobj.__repr__()
4242

4343
def connect(self, srcField, dest, destField):
44-
return self.cobj.connect(srcField, dest.cobj, destField)
44+
if hasattr(dest, 'cobj'):
45+
dest = dest.cobj
46+
return self.cobj.connect(srcField, dest, destField)
4547

4648
def parent(self, path):
4749
p = '/'.join(path.split('/')[:-1])

0 commit comments

Comments
 (0)