Skip to content

Commit 8f60899

Browse files
author
Dilawar Singh
committed
Perfecto. Create works.
1 parent aa95a0c commit 8f60899

File tree

4 files changed

+267
-2
lines changed

4 files changed

+267
-2
lines changed

pymoose/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/../CheckCXXCompiler.cmake)
44
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake_modules/")
55

66
pybind11_add_module(_moose pymoose.cpp)
7-
target_link_libraries(_moose moose)
7+
target_link_libraries(_moose PRIVATE moose)
88

99
enable_testing()
1010
add_test(NAME sanity

pymoose/pymoose.cpp

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
// =====================================================================================
2+
//
3+
// Filename: pymoose.cpp
4+
//
5+
// Description:
6+
//
7+
// Version: 1.0
8+
// Created: 03/15/2020 04:03:58 PM
9+
// Revision: none
10+
// Compiler: g++
11+
//
12+
// Author: Dilawar Singh (), [email protected]
13+
// Organization: NCBS Bangalore
14+
//
15+
// =====================================================================================
16+
17+
#include <vector>
18+
#include <typeinfo>
19+
#include <utility>
20+
21+
#include "../basecode/header.h"
22+
#include "../shell/Shell.h"
23+
#include "../scheduling/Clock.h"
24+
#include "../utility/print_function.hpp"
25+
#include "../utility/utility.h"
26+
#include "../utility/strutil.h"
27+
#include "../mpi/PostMaster.h"
28+
29+
#include "pymoose.h"
30+
31+
class Pool;
32+
class BufPool;
33+
34+
#include "../external/pybind11/include/pybind11/pybind11.h"
35+
36+
37+
using namespace std;
38+
namespace py = pybind11;
39+
40+
const vector<const char*> classMap { "AdExIF", "AdThreshIF", "Adaptor",
41+
"Annotator", "Arith", "BufPool", "CaConc", "Cell", "ChemCompt", "Cinfo",
42+
"Clock", "Compartment", "ConcChan", "CubeMesh", "CylMesh", "DestField",
43+
"DiagonalMsg", "DifBuffer", "DifShell", "DiffAmp", "Dsolve",
44+
"ElementField", "EndoMesh", "Enz", "ExIF", "Finfo", "Function",
45+
"GapJunction", "GraupnerBrunel2012CaPlasticitySynHandler", "Group",
46+
"Gsolve", "HHChannel", "HHChannel2D", "HHGate", "HHGate2D", "HSolve",
47+
"INFINITE", "IntFire", "Interpol", "Interpol2D", "IzhIF", "IzhikevichNrn",
48+
"Ksolve", "LIF", "Leakage", "LookupField", "MMPump", "MMenz",
49+
"MarkovChannel", "MarkovGslSolver", "MarkovRateTable", "MarkovSolver",
50+
"MeshEntry", "MgBlock", "Msg", "Mstring", "NMDAChan", "Nernst",
51+
"NeuroMesh", "Neuron", "Neutral", "OneToAllMsg", "OneToOneDataIndexMsg",
52+
"OneToOneMsg", "PIDController", "Pool", "PostMaster", "PsdMesh",
53+
"PulseGen", "PyRun", "QIF", "RC", "RandSpike", "Reac", "STDPSynHandler",
54+
"STDPSynapse", "SeqSynHandler", "Shell", "SimpleSynHandler", "SingleMsg",
55+
"SocketStreamer", "SparseMsg", "Species", "SpikeGen", "SpikeStats",
56+
"Spine", "SpineMesh", "Stats", "SteadyState", "StimulusTable", "Stoich",
57+
"Streamer", "SymCompartment", "SynChan", "Synapse", "Table", "Table2",
58+
"TimeTable", "VClamp" };
59+
60+
Id initShell(void)
61+
{
62+
Cinfo::rebuildOpIndex();
63+
Id shellId;
64+
Element* shelle = new GlobalDataElement( shellId, Shell::initCinfo(), "root", 1 );
65+
66+
Id clockId = Id::nextId();
67+
assert( clockId.value() == 1 );
68+
Id classMasterId = Id::nextId();
69+
Id postMasterId = Id::nextId();
70+
71+
Shell* s = reinterpret_cast< Shell* >( shellId.eref().data() );
72+
s->setHardware(1, 1, 0);
73+
s->setShellElement( shelle );
74+
75+
/// Sets up the Elements that represent each class of Msg.
76+
auto numMsg = Msg::initMsgManagers();
77+
78+
new GlobalDataElement( clockId, Clock::initCinfo(), "clock", 1 );
79+
new GlobalDataElement( classMasterId, Neutral::initCinfo(), "classes", 1);
80+
new GlobalDataElement( postMasterId, PostMaster::initCinfo(), "postmaster", 1 );
81+
82+
assert ( shellId == Id() );
83+
assert( clockId == Id( 1 ) );
84+
assert( classMasterId == Id( 2 ) );
85+
assert( postMasterId == Id( 3 ) );
86+
87+
Shell::adopt( shellId, clockId, numMsg++ );
88+
Shell::adopt( shellId, classMasterId, numMsg++ );
89+
Shell::adopt( shellId, postMasterId, numMsg++ );
90+
assert( numMsg == 10 ); // Must be the same on all nodes.
91+
92+
Cinfo::makeCinfoElements( classMasterId );
93+
return shellId;
94+
}
95+
96+
97+
/* --------------------------------------------------------------------------*/
98+
/**
99+
* @Synopsis getShell.
100+
*
101+
* @Param argc
102+
* @Param argv
103+
*
104+
* @Returns
105+
*/
106+
/* ----------------------------------------------------------------------------*/
107+
Id getShell(int argc, char ** argv)
108+
{
109+
static int inited = 0;
110+
if (inited)
111+
return Id(0);
112+
113+
Id shellId = initShell();
114+
inited = 1;
115+
116+
Shell * shellPtr = reinterpret_cast<Shell*>(shellId.eref().data());
117+
if ( shellPtr->myNode() == 0 )
118+
{
119+
if ( Shell::numNodes() > 1 )
120+
{
121+
// Use the last clock for the postmaster, so that it is called
122+
// after everything else has been processed and all messages
123+
// are ready to send out.
124+
shellPtr->doUseClock( "/postmaster", "process", 9 );
125+
shellPtr->doSetClock( 9, 1.0 ); // Use a sensible default.
126+
}
127+
}
128+
return shellId;
129+
}
130+
131+
/**
132+
Utility function to create objects from full path, dimensions
133+
and classname.
134+
*/
135+
Id createIdFromPath(string path, string type, size_t numData)
136+
{
137+
string parent_path;
138+
string name;
139+
140+
string trimmed_path = moose::trim( path );
141+
size_t pos = trimmed_path.rfind("/");
142+
if (pos != string::npos)
143+
{
144+
name = trimmed_path.substr(pos+1);
145+
parent_path = trimmed_path.substr(0, pos);
146+
}
147+
else
148+
{
149+
name = trimmed_path;
150+
}
151+
// handle relative path
152+
if (trimmed_path[0] != '/')
153+
{
154+
string current_path = SHELLPTR->getCwe().path();
155+
if (current_path != "/")
156+
{
157+
parent_path = current_path + "/" + parent_path;
158+
}
159+
else
160+
{
161+
parent_path = current_path + parent_path;
162+
}
163+
}
164+
else if (parent_path.empty())
165+
{
166+
parent_path = "/";
167+
}
168+
ObjId parent_id(parent_path);
169+
if (parent_id.bad() )
170+
{
171+
string message = "Parent element does not exist: ";
172+
message += parent_path;
173+
PyErr_SetString(PyExc_ValueError, message.c_str());
174+
return Id();
175+
}
176+
177+
Id nId = SHELLPTR->doCreate(type,
178+
parent_id,
179+
string(name),
180+
numData,
181+
MooseGlobal
182+
);
183+
184+
if (nId == Id() && trimmed_path != "/" && trimmed_path != "/root")
185+
{
186+
string message = "no such moose class : " + type;
187+
PyErr_SetString(PyExc_TypeError, message.c_str());
188+
}
189+
190+
return nId;
191+
}
192+
193+
PYBIND11_MODULE(_moose, m)
194+
{
195+
m.doc() = R"moosedoc(moose module.
196+
)moosedoc";
197+
198+
py::class_<ObjId>(m, "ObjId")
199+
.def(py::init<>())
200+
;
201+
202+
py::class_<Id>(m, "Id")
203+
.def(py::init<>())
204+
;
205+
206+
// Add Shell Class.
207+
py::class_<Shell>(m, "Shell")
208+
.def(py::init<>())
209+
.def("doCreate", &Shell::doCreate)
210+
.def("doDelete", &Shell::doDelete)
211+
.def("doAddMsg", &Shell::doAddMsg)
212+
.def("doQuit", &Shell::doQuit)
213+
.def("doStart", &Shell::doStart)
214+
.def("doReinit", &Shell::doReinit)
215+
.def("doStop", &Shell::doStop)
216+
.def("doMove", &Shell::doMove)
217+
.def("doCopy", &Shell::doCopy)
218+
.def("destroy", &Shell::destroy)
219+
.def("doFind", &Shell::doFind)
220+
.def("doLoadModel", &Shell::doLoadModel)
221+
.def("doSaveModel", &Shell::doSaveModel)
222+
.def("handleCreate", &Shell::handleCreate)
223+
.def("handleCopy", &Shell::handleCopy)
224+
.def("handleQuit", &Shell::handleQuit)
225+
;
226+
227+
m.def("create", &createIdFromPath);
228+
229+
m.attr("__version__") = MOOSE_VERSION;
230+
231+
}

pymoose/pymoose.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// =====================================================================================
2+
//
3+
// Filename: pymoose.h
4+
//
5+
// Description: pymoose module.
6+
//
7+
// Version: 1.0
8+
// Created: 03/17/2020 05:32:37 PM
9+
// Revision: none
10+
// Compiler: g++
11+
//
12+
// Author: Dilawar Singh (), [email protected]
13+
// Organization: NCBS Bangalore
14+
//
15+
// =====================================================================================
16+
17+
#ifndef PYMOOSE_H
18+
#define PYMOOSE_H
19+
20+
Id initShell(void);
21+
22+
/**
23+
Return the Id of the Shell object.
24+
*/
25+
Id getShell(int argc, char ** argv);
26+
27+
// Macro to create the Shell * out of shellId
28+
#define SHELLPTR (reinterpret_cast<Shell*>(getShell(0, NULL).eref().data()))
29+
30+
Id createIdFromPath(string path, string type, size_t numData=1);
31+
32+
33+
#endif /* end of include guard: PYMOOSE_H */

pymoose/test_pybind_moose.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import _moose as M
22
print("Using _moose from %s" % M.__file__)
33
print(dir(M))
4-
print(dir(M.Shell))
4+
a = M.create('a', 'Neutral', 1)
5+
print(a)

0 commit comments

Comments
 (0)