Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
If a Shell function retuns Id, wrap it in a MooseVec. The current API
Browse files Browse the repository at this point in the history
has this behaviour.
  • Loading branch information
Dilawar Singh committed Apr 7, 2020
1 parent 8ca430c commit 8092a8e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions pybind11/MooseVec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ MooseVec::MooseVec(const ObjId& oid) : oid_(oid), path_(oid.path())
{
}

MooseVec::MooseVec(const Id& id) : oid_(ObjId(id)), path_(id.path())
{
}

const string MooseVec::dtype() const
{
return oid_.element()->cinfo()->name();
Expand Down
5 changes: 4 additions & 1 deletion pybind11/MooseVec.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
#ifndef MOOSE_VEC_H
#define MOOSE_VEC_H

class MooseVec {
class MooseVec
{

public:
MooseVec(const string& path, unsigned int n, const string& dtype);

MooseVec(const Id& id);

MooseVec(const ObjId& oid);

const ObjId& obj() const;
Expand Down
21 changes: 9 additions & 12 deletions pybind11/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "../external/pybind11/include/pybind11/pybind11.h"
#include "../external/pybind11/include/pybind11/stl.h"

namespace py = pybind11;

// See
// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#binding-stl-containers
// #include "../external/pybind11/include/pybind11/stl_bind.h"
Expand All @@ -44,8 +46,6 @@

using namespace std;

namespace py = pybind11;

using namespace std;

Id initShell(void)
Expand Down Expand Up @@ -138,14 +138,12 @@ ObjId createIdFromPath(string path, string type, unsigned int numData)
return nId;
}


bool mooseExists(const string& p)
{
string path = moose::normalizePath(p);
return Id(path) != Id() || path == "/" || path == "/root";
}


ObjId loadModelInternal(const string& fname, const string& modelpath,
const string& solverclass = "")
{
Expand Down Expand Up @@ -215,7 +213,6 @@ void mooseMoveObjId(const ObjId& a, const ObjId& b)
getShellPtr()->doMove(a.id, b);
}


void mooseSetClock(const unsigned int clockId, double dt)
{
getShellPtr()->doSetClock(clockId, dt);
Expand Down Expand Up @@ -243,10 +240,9 @@ py::object mooseGetCwe()
return py::cast(mooseGetCweId());
}


void mooseSetCwe(const py::object& arg)
{
if(py::isinstance<py::str>(arg))
if (py::isinstance<py::str>(arg))
return getShellPtr()->setCwe(ObjId(arg.cast<string>()));
return getShellPtr()->setCwe(arg.cast<ObjId>());
}
Expand Down Expand Up @@ -319,13 +315,14 @@ void mooseStop()
getShellPtr()->doStop();
}

ObjId mooseCopy(const py::object& elem, ObjId newParent, string newName,
unsigned int n = 1, bool toGlobal = false,
bool copyExtMsgs = false)
// Id is synonym with Id in previous binding.
MooseVec mooseCopy(const py::object& elem, ObjId newParent, string newName,
unsigned int n = 1, bool toGlobal = false,
bool copyExtMsgs = false)
{
Id orig = py::cast<Id>(elem);
return ObjId(getShellPtr()->doCopy(orig, newParent, newName, n, toGlobal,
copyExtMsgs));
return MooseVec(getShellPtr()->doCopy(orig, newParent, newName, n, toGlobal,
copyExtMsgs));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion pybind11/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include "../shell/Shell.h"

#include "MooseVec.h"

namespace py = pybind11;
using namespace std;

Expand Down Expand Up @@ -134,7 +136,7 @@ inline bool mooseDeleteStr(const string& path)
ObjId mooseCreate(const string type, const string& path,
unsigned int numdata = 1);

ObjId mooseCopy(const py::object& orig, ObjId newParent, string newName,
MooseVec mooseCopy(const py::object& orig, ObjId newParent, string newName,
unsigned int n, bool toGlobal, bool copyExtMsgs);


Expand Down
2 changes: 2 additions & 0 deletions pybind11/pymoose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ PYBIND11_MODULE(_moose, m)
.def("__getitem__", &__Finfo__::getItem)
.def("__setitem__", &__Finfo__::setItem);

// A thin wrapper around Id from ../basecode/Id.h . Usually this is shows
// at moose.vec.
py::class_<Id>(m, "_Id")
.def(py::init<>())
.def(py::init<unsigned int>())
Expand Down

0 comments on commit 8092a8e

Please sign in to comment.