Skip to content

Commit bf118f4

Browse files
author
Dilawar Singh
committed
We can now assign moose.vec to a field which takes Id.
refactoring. int -> double, bool -> (int, double) coercing is allowed. Fixed script /home1/dilawars/Work.HG/GITLAB/moose-examples/snippets/interpol2d.py in https://gitlab.com/dilawar/moose-examples/-/issues/1
1 parent fb57aaf commit bf118f4

19 files changed

+336
-205
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Standard: Auto
3434
IndentWidth: 4
3535
TabWidth: 8
3636
UseTab: Never
37-
BreakBeforeBraces: Stroustrup
37+
BreakBeforeBraces: Allman
3838
IndentFunctionDeclarationAfterType: true
3939
SpacesInParentheses: false
4040
SpacesInAngles: false

pybind11/Finfo.cpp

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,23 @@ __Finfo__::__Finfo__(const ObjId& oid, const Finfo* f, const string& finfoType)
2828
: oid_(oid), f_(f), finfoType_(finfoType)
2929
{
3030
if (finfoType == "DestFinfo")
31-
func_ = [oid, f](const py::object& key) {
32-
return getLookupValueFinfoItem(oid, f, key);
33-
};
31+
func_ = [oid, f](const py::object& key)
32+
{ return getLookupValueFinfoItem(oid, f, key); };
3433
else if (finfoType == "FieldElementFinfo")
35-
func_ = [oid, f](const py::object& index) {
34+
func_ = [oid, f](const py::object& index)
35+
{
3636
// this is essential of make this function static.
3737
return getElementFinfoItem(oid, f, py::cast<unsigned int>(index));
3838
};
3939
else if (finfoType == "LookupValueFinfo")
40-
func_ = [oid, f, this](const py::object& key) {
40+
func_ = [oid, f, this](const py::object& key)
41+
{
4142
// Assigning is essential or make these functions static.
4243
return this->getLookupValueFinfoItem(oid, f, key);
4344
};
4445
else
45-
func_ = [this](const py::object& key) {
46+
func_ = [this](const py::object& key)
47+
{
4648
throw runtime_error("Not supported for Finfo type '" + finfoType_ +
4749
"'");
4850
return py::none();
@@ -69,7 +71,8 @@ bool __Finfo__::setLookupValueFinfoItem(const ObjId& oid, const py::object& key,
6971
auto srcType = srcDestType[0];
7072
auto destType = srcDestType[1];
7173

72-
if (srcType == "unsigned int") {
74+
if (srcType == "unsigned int")
75+
{
7376
if (destType == "double")
7477
return LookupField<unsigned int, double>::set(
7578
oid, fieldName, py::cast<unsigned int>(key),
@@ -95,24 +98,24 @@ py::object __Finfo__::getLookupValueFinfoItem(const ObjId& oid, const Finfo* f,
9598

9699
py::object r = py::none();
97100

98-
if (srcType == "string") {
99-
auto k = py::cast<string>(key);
100-
return getLookupValueFinfoItemInner<string>(oid, f, k, tgtType);
101-
} else if (srcType == "unsigned int") {
102-
auto k = py::cast<unsigned int>(key);
103-
return getLookupValueFinfoItemInner<unsigned int>(oid, f, k, tgtType);
104-
} else if (srcType == "ObjId") {
105-
auto k = py::cast<ObjId>(key);
106-
return getLookupValueFinfoItemInner<ObjId>(oid, f, k, tgtType);
107-
} else if (srcType == "Id") {
108-
auto k = py::cast<Id>(key);
109-
return getLookupValueFinfoItemInner<Id>(oid, f, k, tgtType);
110-
} else {
111-
py::print("getLookupValueFinfoItem::NotImplemented for key:", key,
112-
"srcType:", srcType, "and tgtType:", tgtType, "path: ",
113-
oid.path());
114-
throw runtime_error("getLookupValueFinfoItem::NotImplemented error");
115-
}
101+
if (srcType == "string")
102+
return getLookupValueFinfoItemInner<string>(oid, f, key.cast<string>(), tgtType);
103+
if (srcType == "unsigned int")
104+
return getLookupValueFinfoItemInner<unsigned int>(
105+
oid, f, key.cast<unsigned int>(), tgtType);
106+
if (srcType == "ObjId")
107+
return getLookupValueFinfoItemInner<ObjId>(oid, f, key.cast<ObjId>(),
108+
tgtType);
109+
if (srcType == "vector<double>")
110+
return getLookupValueFinfoItemInner<vector<double>>(
111+
oid, f, key.cast<vector<double>>(), tgtType);
112+
if (srcType == "Id")
113+
return getLookupValueFinfoItemInner<Id>(oid, f, key.cast<Id>(), tgtType);
114+
115+
py::print("getLookupValueFinfoItem::NotImplemented for key:", key,
116+
"srcType:", srcType, "and tgtType:", tgtType, "path: ",
117+
oid.path());
118+
throw runtime_error("getLookupValueFinfoItem::NotImplemented error");
116119
return r;
117120
}
118121

@@ -133,7 +136,8 @@ py::cpp_function __Finfo__::getDestFinfoSetterFunc(const ObjId& oid,
133136
vector<string> types;
134137
moose::tokenize(rttType, ",", types);
135138

136-
if (types.size() == 1) return getDestFinfoSetterFunc1(oid, finfo, types[0]);
139+
if (types.size() == 1)
140+
return getDestFinfoSetterFunc1(oid, finfo, types[0]);
137141

138142
assert(types.size() == 2);
139143
return getDestFinfoSetterFunc2(oid, finfo, types[0], types[1]);
@@ -146,19 +150,20 @@ py::cpp_function __Finfo__::getDestFinfoSetterFunc2(const ObjId& oid,
146150
const string& ftype2)
147151
{
148152
const auto fname = finfo->name();
149-
if (ftype1 == "double") {
150-
if (ftype2 == "unsigned int") {
153+
if (ftype1 == "double")
154+
{
155+
if (ftype2 == "unsigned int")
156+
{
151157
std::function<bool(double, unsigned int)> func = [oid, fname](
152-
const double a, const unsigned int b) {
153-
return SetGet2<double, unsigned int>::set(oid, fname, a, b);
154-
};
158+
const double a, const unsigned int b)
159+
{ return SetGet2<double, unsigned int>::set(oid, fname, a, b); };
155160
return func;
156161
}
157-
if (ftype2 == "long") {
162+
if (ftype2 == "long")
163+
{
158164
std::function<bool(double, long)> func = [oid, fname](
159-
const double a, const long b) {
160-
return SetGet2<double, long>::set(oid, fname, a, b);
161-
};
165+
const double a, const long b)
166+
{ return SetGet2<double, long>::set(oid, fname, a, b); };
162167
return func;
163168
}
164169
}
@@ -173,17 +178,23 @@ py::cpp_function __Finfo__::getDestFinfoSetterFunc1(const ObjId& oid,
173178
const string& ftype)
174179
{
175180
const auto fname = finfo->name();
176-
if (ftype == "void") {
177-
std::function<bool()> func = [oid, fname]() {
178-
return SetGet0::set(oid, fname);
179-
};
181+
if (ftype == "void")
182+
{
183+
std::function<bool()> func = [oid, fname]()
184+
{ return SetGet0::set(oid, fname); };
180185
return func;
181186
}
182187

183-
if (ftype == "double") return getSetGetFunc1<double>(oid, fname);
184-
if (ftype == "ObjId") return getSetGetFunc1<ObjId>(oid, fname);
185-
if (ftype == "Id") return getSetGetFunc1<Id>(oid, fname);
186-
if (ftype == "vector<Id>") return getSetGetFunc1<vector<Id>>(oid, fname);
188+
if (ftype == "double")
189+
return getSetGetFunc1<double>(oid, fname);
190+
if (ftype == "ObjId")
191+
return getSetGetFunc1<ObjId>(oid, fname);
192+
if (ftype == "Id")
193+
return getSetGetFunc1<Id>(oid, fname);
194+
if (ftype == "string")
195+
return getSetGetFunc1<string>(oid, fname);
196+
if (ftype == "vector<Id>")
197+
return getSetGetFunc1<vector<Id>>(oid, fname);
187198
if (ftype == "vector<ObjId>")
188199
return getSetGetFunc1<vector<ObjId>>(oid, fname);
189200
if (ftype == "vector<double>")
@@ -201,13 +212,17 @@ py::object __Finfo__::getFieldValue(const ObjId& oid, const Finfo* f)
201212

202213
if (rttType == "double" or rttType == "float")
203214
r = pybind11::float_(getField<double>(oid, fname));
204-
else if (rttType == "vector<double>") {
215+
else if (rttType == "vector<double>")
216+
{
205217
// r = py::cast(getField<vector<double>>(oid, fname));
206218
r = getFieldNumpy<double>(oid, fname);
207-
} else if (rttType == "vector<unsigned int>") {
219+
}
220+
else if (rttType == "vector<unsigned int>")
221+
{
208222
// r = pybind11::cast(getField<vector<unsigned int>>(oid, fname));
209223
r = getFieldNumpy<unsigned int>(oid, fname);
210-
} else if (rttType == "string")
224+
}
225+
else if (rttType == "string")
211226
r = pybind11::str(getField<string>(oid, fname));
212227
else if (rttType == "char")
213228
r = pybind11::int_(getField<char>(oid, fname));
@@ -231,7 +246,8 @@ py::object __Finfo__::getFieldValue(const ObjId& oid, const Finfo* f)
231246
r = py::cast(getField<vector<ObjId>>(oid, fname));
232247
else if (rttType == "vector<string>")
233248
r = py::cast(getField<vector<string>>(oid, fname));
234-
else {
249+
else
250+
{
235251
MOOSE_WARN("Warning: getValueFinfo:: Unsupported type '" + rttType +
236252
"'");
237253
r = py::none();
@@ -253,7 +269,8 @@ py::list __Finfo__::getElementFinfo(const ObjId& objid, const Finfo* f)
253269
py::object __Finfo__::getElementFinfoItem(const ObjId& oid, const Finfo* f,
254270
unsigned int index)
255271
{
256-
if (index >= getNumFieldStatic(oid, f)) {
272+
if (index >= getNumFieldStatic(oid, f))
273+
{
257274
throw py::index_error("Index " + to_string(index) + " out of range.");
258275
}
259276
auto o = ObjId(oid.path() + '/' + f->name());

pybind11/MooseVec.cpp

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
using namespace std;
1313

14-
#include "../external/pybind11/include/pybind11/pybind11.h"
15-
#include "../external/pybind11/include/pybind11/numpy.h"
16-
namespace py = pybind11;
17-
1814
#include "../utility/strutil.h"
1915
#include "helper.h"
2016
#include "pymoose.h"
@@ -24,7 +20,11 @@ MooseVec::MooseVec(const string& path, unsigned int n = 0,
2420
const string& dtype = "Neutral")
2521
: path_(path)
2622
{
23+
// If path is given and it does not exists, then create one. The old api
24+
// support it.
2725
oid_ = ObjId(path);
26+
if(oid_.bad())
27+
oid_ = mooseCreateFromPath(dtype, path, n);
2828
}
2929

3030
MooseVec::MooseVec(const ObjId& oid) : oid_(oid), path_(oid.path())
@@ -109,50 +109,12 @@ py::object MooseVec::getAttribute(const string& name)
109109
if(rttType == "int")
110110
return getAttributeNumpy<unsigned int>(name);
111111

112-
// FIXME: bool type is not working. Need to raise the ticket on pybind11
113-
// after creating and MWE.
114-
//if(rttType == "bool")
115-
// return getAttributeNumpy<bool>(name);
116-
117112
vector<py::object> res(size());
118113
for (unsigned int i = 0; i < size(); i++)
119114
res[i] = getFieldGeneric(getItem(i), name);
120115
return py::cast(res);
121116
}
122117

123-
124-
125-
// // FIXME: Only double is supported here. Not sure if this is enough. This
126-
// // should be the API function.
127-
// py::array_t<double> MooseVec::getAttributeNumpy(const string &name)
128-
// {
129-
// auto cinfo = oid_.element()->cinfo();
130-
// auto finfo = cinfo->findFinfo(name);
131-
//
132-
// if (!finfo) {
133-
// throw py::key_error(name + " is not found on '" + oid_.path() + "'.");
134-
// }
135-
//
136-
// string finfoType = cinfo->getFinfoType(finfo);
137-
//
138-
// // Either return a simple value (ValueFinfo), list, dict or DestFinfo
139-
// // setter.
140-
// // The DestFinfo setter is a function.
141-
//
142-
// vector<double> res(size());
143-
// if (finfoType == "ValueFinfo") {
144-
// for (unsigned int i = 0; i < size(); i++)
145-
// res[i] = getField<double>(getItem(i), name);
146-
// return py::array_t<double>(res.size(), res.data());
147-
// }
148-
//
149-
// throw runtime_error("MooseVec::getAttributeNumpy::NotImplemented : " + name +
150-
// " with rttType " + finfo->rttiType() + " and type: '" +
151-
// finfoType + "'");
152-
// return py::array_t<double>();
153-
// }
154-
155-
156118
ObjId MooseVec::connectToSingle(const string& srcfield, const ObjId& tgt,
157119
const string& tgtfield, const string& msgtype)
158120
{

0 commit comments

Comments
 (0)