Skip to content

Commit dd65791

Browse files
author
Dilawar Singh
committed
Updated [skip ci]
1 parent 76ec2a1 commit dd65791

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

pybind11/helper.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,12 @@ bool doesExist(const string& path)
148148

149149
ObjId mooseElement(const string& path)
150150
{
151-
ObjId oid;
152-
unsigned nid = 0, did = 0, fidx = 0;
153-
Id id;
154-
unsigned int numData = 0;
155-
156-
oid = ObjId(path);
151+
ObjId oid(path);
157152
if (oid.bad()) {
158153
throw runtime_error(std::string("moose_element: '") +
159154
std::string(path) +
160155
std::string("' does not exist!"));
161-
return Id();
156+
return ObjId(Id());
162157
}
163158
return oid;
164159
}
@@ -312,3 +307,18 @@ void mooseStart(double runtime, bool notify=false)
312307
{
313308
getShellPtr()->doStart(runtime, notify);
314309
}
310+
311+
//ObjId mooseCopy(const ObjId& orig, ObjId newParent, string newName, unsigned int n=1
312+
// , bool toGlobal=false, bool copyExtMsgs=false)
313+
//{
314+
// auto id = getShellPtr()->doCopy(orig.id, newParent, newName, n, toGlobal, copyExtMsgs);
315+
// return ObjId(id);
316+
//}
317+
318+
319+
ObjId mooseCopy(const Id& orig, ObjId newParent, string newName, unsigned int n=1
320+
, bool toGlobal=false, bool copyExtMsgs=false)
321+
{
322+
auto id = getShellPtr()->doCopy(orig, newParent, newName, n, toGlobal, copyExtMsgs);
323+
return ObjId(id);
324+
}

pybind11/helper.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ void mooseDelete(const ObjId& oid);
4747

4848
ObjId mooseCreate(const string type, const string& path, size_t numdata = 1);
4949

50+
// ObjId mooseCopy(const ObjId& orig, ObjId newParent, string newName, unsigned int n,
51+
// bool toGlobal, bool copyExtMsgs);
52+
53+
ObjId mooseCopy(const Id& orig, ObjId newParent, string newName, unsigned int n
54+
, bool toGlobal, bool copyExtMsgs);
55+
5056
py::object mooseGetCwe();
5157

5258
void mooseSetClock(const size_t clockId, double dt);
@@ -57,5 +63,4 @@ map<string, string> mooseGetFieldDict(const string& className,
5763
void mooseReinit();
5864
void mooseStart(double runtime, bool notify);
5965

60-
6166
#endif /* end of include guard: HELPER_H */

pybind11/pymoose.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ py::object getLookupValueFinfoItem(const ObjId& oid, const string& fname,
131131
string tgtType = srcDestType[1];
132132

133133
py::object r;
134-
if(tgtType == "bool")
134+
if (tgtType == "bool")
135135
r = py::cast(LookupField<string, bool>::get(oid, fname, k));
136-
else if(tgtType == "vector<Id>")
136+
else if (tgtType == "vector<Id>")
137137
r = py::cast(LookupField<string, vector<Id>>::get(oid, fname, k));
138138
else
139139
cerr << "Unsupported types: " << rttType << endl;
@@ -197,6 +197,8 @@ PYBIND11_MODULE(_cmoose, m)
197197
// properties
198198
.def_property_readonly("numIds", &Id::numIds)
199199
.def_property_readonly("path", &Id::path)
200+
.def_property_readonly("name",
201+
[](const Id& id) { return ObjId(id).name(); })
200202
.def_property_readonly("id", &Id::value)
201203
.def_property_readonly(
202204
"cinfo", [](Id& id) { return id.element()->cinfo(); },
@@ -222,6 +224,8 @@ PYBIND11_MODULE(_cmoose, m)
222224
.def_property_readonly("value",
223225
[](const ObjId oid) { return oid.id.value(); })
224226
.def_property_readonly("path", &ObjId::path)
227+
.def_property_readonly(
228+
"parent", [](const ObjId& oid) { return Neutral::parent(oid); })
225229
.def_property_readonly("name", &ObjId::name)
226230
.def_property_readonly(
227231
"className",
@@ -284,8 +288,7 @@ PYBIND11_MODULE(_cmoose, m)
284288
.def("quit", &Shell::doQuit);
285289

286290
// Module functions.
287-
m.def(
288-
"getShell",
291+
m.def("getShell",
289292
[]() { return reinterpret_cast<Shell*>(Id().eref().data()); },
290293
py::return_value_policy::reference);
291294

@@ -301,7 +304,9 @@ PYBIND11_MODULE(_cmoose, m)
301304
m.def("loadModelInternal", &loadModelInternal);
302305
m.def("getFieldDict", &mooseGetFieldDict, py::arg("className"),
303306
py::arg("finfoType") = "");
304-
307+
m.def("copy", &mooseCopy, py::arg("orig"), py::arg("newParent"),
308+
py::arg("newName"), py::arg("num") = 1, py::arg("toGlobal") = false,
309+
py::arg("copyExtMsgs") = false);
305310
// Attributes.
306311
m.attr("NA") = NA;
307312
m.attr("PI") = PI;

pymoose/moosemodule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2556,7 +2556,7 @@ int moose_clear(PyObject *m)
25562556
Py_CLEAR(GETSTATE(m)->error);
25572557
// I did get a segmentation fault at exit (without running a reinit() or
25582558
// start()) after creating a compartment. After putting the finalize here it
2559-
// went away. But did not reoccur even after commenting it out. Will need
2559+
// went away. But :edid not reoccur even after commenting it out. Will need
25602560
// closer debugging.
25612561
// - Subha 2012-08-18, 00:36
25622562
finalize();

python/moose/moose.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __getattr__(self, attr):
5959

6060
for p in _cmoose.wildcardFind('/##[TYPE=Cinfo]'):
6161
# create a class.
62-
cls = type(p.name, (__Neutral__,), dict(__metaclass__=p.name, objid=p.id))
62+
cls = type(p.name, (__Neutral__,), dict(__metaclass__=p.name))
6363
setattr(moose, cls.__name__, cls)
6464

6565
logger_.info("Declarting classes took %f sec" % (time.time() - t0))
@@ -104,6 +104,12 @@ def element(pathOrObject):
104104
def exists(path):
105105
return _cmoose.exists(path)
106106

107+
def copy(elem, newParent, newName="", n=1):
108+
if not newName:
109+
newName = elem.name
110+
if isinstance(newName, str):
111+
newParent = __Neutral__(newParent)
112+
return _cmoose.copy(elem, newParent, newName, n, False, False)
107113

108114
def getCwe():
109115
return __Neutral__(_cmoose.getCwe())

tests/pybind11/test_shell.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,18 +331,19 @@ def test_ksolve2():
331331
kin = makereac2()
332332
run_and_assert(kin, "ksolve1_test2.png")
333333

334-
def test_ksolve3():
334+
def test_access():
335335
moose.Neutral('x')
336336
a = moose.Neutral('x/x')
337337
print(a.isA)
338338
print(a.isA('Compartment'))
339339
print(a.isA['Compartment'])
340+
print(a.parent)
340341

341342
def main():
342343
test_ksolve0()
343344
test_ksolve1()
344345
test_ksolve2()
345-
test_ksolve3()
346+
test_access()
346347

347348
if __name__ == '__main__':
348349
main()

0 commit comments

Comments
 (0)