7 #include <structmember.h>
10 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
11 #include <numpy/arrayobject.h>
24 #include <boost/format.hpp>
31 #include "../utility/print_function.hpp"
32 #include "../basecode/header.h"
33 #include "../basecode/global.h"
34 #include "../basecode/Id.h"
35 #include "../basecode/ObjId.h"
36 #include "../utility/utility.h"
37 #include "../shell/Shell.h"
38 #include "../shell/Wildcard.h"
47 extern void testSync();
50 extern void testSyncArray(
52 unsigned int numThreads,
86 unsigned int size,
unsigned int runsteps
118 unsigned int * iptr = ⅈ
120 if (
id.element()->hasFields())
127 length =
id.element()->numData();
129 for (*iptr = 0; *iptr < length; ++(*iptr))
131 ret.push_back(
ObjId(
id, ii, jj));
144 LOG(
moose::info,
"Interrupt signal (" << signum <<
") received.");
154 extern PyTypeObject
IdType;
180 vector <int> vec_dims;
181 Py_ssize_t num_dims = 1;
186 if (PyTuple_Check(dims))
188 num_dims = PyTuple_Size(dims);
189 for (Py_ssize_t ii = 0; ii < num_dims; ++ ii)
191 PyObject* dim = PyTuple_GetItem(dims, ii);
192 dim_value = PyInt_AsLong(dim);
193 if ((dim_value == -1) && PyErr_Occurred())
197 vec_dims.push_back((
unsigned int)dim_value);
200 else if (PyInt_Check(dims))
202 dim_value = PyInt_AsLong(dims);
207 vec_dims.push_back(dim_value);
212 vec_dims.push_back(dim_value);
222 void *
to_cpp(PyObject *
object,
char typecode)
228 int * ret =
new int();
229 * ret = PyInt_AsLong(
object);
234 long v = PyInt_AsLong(
object);
235 long * ret =
new long();
241 short v = PyInt_AsLong(
object);
242 short * ret =
new short();
248 float v = (float)PyFloat_AsDouble(
object);
249 if ( v == -1.0 && PyErr_Occurred())
251 PyErr_SetString(PyExc_TypeError,
"Expected a sequence of floating point numbers.");
255 float * ret =
new float();
262 double v = PyFloat_AsDouble(
object);
263 if ( v == -1.0 && PyErr_Occurred())
265 PyErr_SetString(PyExc_TypeError,
"Expected a sequence of floating point numbers.");
269 double * ret =
new double();
276 char* tmp = PyString_AsString(
object);
281 string * ret =
new string(tmp);
286 unsigned int v = PyInt_AsUnsignedLongMask(
object);
287 unsigned int * ret =
new unsigned int();
293 unsigned long v = PyInt_AsUnsignedLongMask(
object);
294 unsigned long * ret =
new unsigned long();
319 return PySequenceToVector< int >(object,
'i');
321 return PySequenceToVector < unsigned int > (object,
'I');
323 return PySequenceToVector < short > (object,
'h');
325 return PySequenceToVector < long > (object,
'l');
327 return PySequenceToVector < unsigned long > (object,
'k');
329 return PySequenceToVector < float > (object,
'f');
331 return PySequenceToVector < double > (object,
'd');
333 return PySequenceToVector < string > (object,
's');
335 return PySequenceToVector < ObjId > (object,
'y');
337 return PySequenceToVector < Id > (object,
'x');
339 return PySequenceToVectorOfVectors< double >(object,
'd');
341 return PySequenceToVectorOfVectors< int >(object,
'i');
343 return PySequenceToVectorOfVectors< unsigned int > (object,
'I');
352 PyObject *
to_py(
void * obj,
char typecode)
358 double * ptr =
static_cast<double *
>(obj);
360 return PyFloat_FromDouble(*ptr);
364 string * ptr =
static_cast< string *
>(obj);
365 assert (ptr != NULL);
366 return PyString_FromString(ptr->c_str());
370 Id * ptr =
static_cast< Id *
>(obj);
374 return (PyObject *)(
id);
382 return (PyObject*)oid;
386 long * ptr =
static_cast< long *
>(obj);
388 return PyLong_FromLong(*ptr);
392 unsigned long * ptr =
static_cast< unsigned long *
>(obj);
394 return PyLong_FromUnsignedLong(*ptr);
398 int * ptr =
static_cast< int *
>(obj);
400 return PyInt_FromLong(*ptr);
404 unsigned int * ptr =
static_cast< unsigned int *
>(obj);
406 return PyLong_FromUnsignedLong(*ptr);
410 bool * ptr =
static_cast< bool *
>(obj);
421 #ifdef HAVE_LONG_LONG
424 long long * ptr =
static_cast< long long *
> (obj);
426 return PyLong_FromLongLong(*ptr);
430 unsigned long long * ptr =
static_cast< unsigned long long *
>(obj);
432 return PyLong_FromUnsignedLongLong(*ptr);
434 #endif // HAVE_LONG_LONG
437 float * ptr =
static_cast< float*
>(obj);
439 return PyFloat_FromDouble(*ptr);
443 char * ptr =
static_cast< char *
>(obj);
445 return Py_BuildValue(
"c", *ptr);
449 short * ptr =
static_cast< short*
>(obj);
451 return Py_BuildValue(
"h", *ptr);
455 unsigned short * ptr =
static_cast< unsigned short *
>(obj);
457 return Py_BuildValue(
"H", *ptr);
478 PyErr_SetString(PyExc_TypeError,
"unhandled data type");
493 PyObject * item =
to_py(vptr, typecode);
498 if (PyTuple_SetItem(tuple, (Py_ssize_t)index, item) != 0)
500 PyErr_SetString(PyExc_RuntimeError,
"convert_and_set_tuple_entry: could not set tuple entry.");
506 cout <<
"Set tuple " << tuple <<
" entry " << index <<
" with " << item << endl;
522 vector< double > * vec =
static_cast< vector < double >*
>(obj);
525 ret = PyTuple_New((Py_ssize_t)vec->size());
526 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
528 if (0 != PyTuple_SetItem(ret, ii, PyFloat_FromDouble(vec->at(ii))))
535 npy_intp size = (npy_intp)(vec->size());
536 ret = PyArray_SimpleNew(1, &size, NPY_DOUBLE);
538 char * ptr = PyArray_BYTES((PyArrayObject*)ret);
539 memcpy(ptr, &(*vec)[0], vec->size() *
sizeof(double));
545 vector< int > * vec =
static_cast< vector < int >*
>(obj);
548 ret = PyTuple_New((Py_ssize_t)vec->size());
549 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
551 if (0 != PyTuple_SetItem(ret, ii, PyInt_FromLong(vec->at(ii))))
558 npy_intp size = (npy_intp)(vec->size());
559 ret = PyArray_SimpleNew(1, &size, NPY_INT);
561 char * ptr = PyArray_BYTES((PyArrayObject*)ret);
562 memcpy(ptr, &(*vec)[0], size *
sizeof(
int));
568 vector< int > * vec =
static_cast< vector < int >*
>(obj);
571 ret = PyTuple_New((Py_ssize_t)vec->size());
572 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
574 if (0 != PyTuple_SetItem(ret, ii, PyLong_FromLong(vec->at(ii))))
581 npy_intp size = (npy_intp)(vec->size());
582 ret = PyArray_SimpleNew(1, &size, NPY_UINT);
584 char * ptr = PyArray_BYTES((PyArrayObject*)ret);
585 memcpy(ptr, &(*vec)[0], size *
sizeof(
unsigned int));
591 vector< long > * vec =
static_cast< vector < long >*
>(obj);
594 ret = PyTuple_New((Py_ssize_t)vec->size());
595 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
597 if (0 != PyTuple_SetItem(ret, ii, PyLong_FromLong(vec->at(ii))))
604 npy_intp size = (npy_intp)(vec->size());
605 ret = PyArray_SimpleNew(1, &size, NPY_INT);
607 char * ptr = PyArray_BYTES((PyArrayObject*)ret);
608 memcpy(ptr, &(*vec)[0], size *
sizeof(
long));
614 vector< Id > * vec =
static_cast< vector < Id >*
>(obj);
616 ret = PyTuple_New((Py_ssize_t)vec->size());
618 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
629 vector< ObjId > * vec =
static_cast< vector < ObjId >*
>(obj);
631 ret = PyTuple_New((Py_ssize_t)vec->size());
633 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
644 vector< char > * vec =
static_cast< vector < char >*
>(obj);
646 ret = PyTuple_New((Py_ssize_t)vec->size());
648 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
659 vector< short > * vec =
static_cast< vector < short >*
>(obj);
661 ret = PyTuple_New((Py_ssize_t)vec->size());
663 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
674 vector< unsigned int > * vec =
static_cast< vector < unsigned int >*
>(obj);
677 ret = PyTuple_New((Py_ssize_t)vec->size());
678 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
680 if (0 != PyTuple_SetItem(ret, ii, PyLong_FromUnsignedLong(vec->at(ii))))
687 npy_intp size = (npy_intp)(vec->size());
688 ret = PyArray_SimpleNew(1, &size, NPY_UINT);
690 char * ptr = PyArray_BYTES((PyArrayObject*)ret);
691 memcpy(ptr, &(*vec)[0], size *
sizeof(
unsigned int));
697 vector< long long> * vec =
static_cast< vector < long long>*
>(obj);
699 ret = PyTuple_New((Py_ssize_t)vec->size());
701 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
703 if (0 != PyTuple_SetItem(ret, ii, PyLong_FromLongLong(vec->at(ii))))
710 npy_intp size = (npy_intp)(vec->size());
711 ret = PyArray_SimpleNew(1, &size, NPY_LONGLONG);
713 char * ptr = PyArray_BYTES((PyArrayObject*)ret);
714 memcpy(ptr, &(*vec)[0], size *
sizeof(
long long));
720 vector< unsigned long long> * vec =
static_cast< vector < unsigned long long>*
>(obj);
723 ret = PyTuple_New((Py_ssize_t)vec->size());
724 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
726 if (0 != PyTuple_SetItem(ret, ii, PyLong_FromUnsignedLongLong(vec->at(ii))))
733 npy_intp size = (npy_intp)(vec->size());
734 ret = PyArray_SimpleNew(1, &size, NPY_ULONGLONG);
736 char * ptr = PyArray_BYTES((PyArrayObject*)ret);
737 memcpy(ptr, &(*vec)[0], size *
sizeof(
unsigned long long));
743 vector< float > * vec =
static_cast< vector < float >*
>(obj);
746 ret = PyTuple_New((Py_ssize_t)vec->size());
747 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
749 if (0 != PyTuple_SetItem(ret, ii, PyFloat_FromDouble(vec->at(ii))))
756 npy_intp size = (npy_intp)(vec->size());
757 ret = PyArray_SimpleNew(1, &size, NPY_FLOAT);
759 char * ptr = PyArray_BYTES((PyArrayObject*)ret);
760 memcpy(ptr, &(*vec)[0], size *
sizeof(
float));
766 vector< string > * vec =
static_cast< vector < string >*
>(obj);
768 ret = PyTuple_New((Py_ssize_t)vec->size());
770 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
772 string v = vec->at(ii);
782 vector< vector< unsigned int > > * vec =
static_cast< vector < vector< unsigned int >
>* >(obj);
784 ret = PyTuple_New((Py_ssize_t)vec->size());
786 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
797 vector< vector< int > > * vec =
static_cast< vector < vector< int >
>* >(obj);
799 ret = PyTuple_New((Py_ssize_t)vec->size());
801 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
812 vector< vector< double > > * vec =
static_cast< vector < vector< double >
>* >(obj);
814 ret = PyTuple_New((Py_ssize_t)vec->size());
816 for (
unsigned int ii = 0; ii < vec->size(); ++ii)
826 PyErr_SetString(PyExc_TypeError,
"unhandled type");
835 static map<string, PyTypeObject *> defined_classes;
836 return defined_classes;
842 static map<string, vector <PyGetSetDef> > getset_defs;
852 static map<string, string> alias;
856 alias[
"lambda_"] =
"lambda";
884 args.push_back(
"moose");
885 map<string, string>::const_iterator it;
893 it = argmap.find(
"INFINITE");
894 if (it != argmap.end())
899 args.push_back(
"-i");
902 it = argmap.find(
"NUMNODES");
903 if (it != argmap.end())
905 istringstream(it->second) >>
numNodes;
906 args.push_back(
"-n");
907 args.push_back(it->second);
909 it = argmap.find(
"NUMCORES");
910 if (it != argmap.end())
912 istringstream(it->second) >>
numCores;
920 it = argmap.find(
"QUIT");
921 if (it != argmap.end())
923 istringstream(it->second) >>
quitFlag;
926 args.push_back(
"-q");
929 it = argmap.find(
"VERBOSITY");
930 if (it != argmap.end())
934 it = argmap.find(
"DOUNITTESTS");
935 if (it != argmap.end())
939 it = argmap.find(
"DOREGRESSIONTESTS");
940 if (it != argmap.end())
947 cout <<
"ENVIRONMENT: " << endl
948 <<
"----------------------------------------" << endl
951 <<
" NUMNODES = " <<
numNodes << endl
956 <<
"========================================" << endl;
969 static int inited = 0;
976 unsigned int doBenchmark = 0;
978 Id shellId =
init(argc, argv, dounit, doregress, doBenchmark );
985 if ( shellPtr->
myNode() == 0 )
992 shellPtr->
doUseClock(
"/postmaster",
"process", 9 );
1006 if ( doBenchmark != 0 )
1019 static bool finalized =
false;
1029 for (map<
string, vector<PyGetSetDef> >::iterator it =
1034 vector <PyGetSetDef> &getsets = it->second;
1066 static const char * finfoTypes[] = {
"valueFinfo",
1071 "fieldElementFinfo",
1092 string fieldType =
"";
1098 cerr <<
"Unknown class " << className << endl;
1107 cerr <<
"Unknown field " << fieldName << endl;
1122 int parseFinfoType(
string className,
string finfoType,
string fieldName, vector<string> & typeVec)
1124 string typestring =
getFieldType(className, fieldName);
1125 if (typestring.empty())
1130 if ((
int)typeVec.size() >
maxArgs)
1134 for (
unsigned int ii = 0; ii < typeVec.size() ; ++ii)
1136 char type_code =
shortType(typeVec[ii]);
1150 vector <string> ret;
1154 cerr <<
"Invalid class name." << endl;
1158 if (finfoType ==
"valueFinfo" || finfoType ==
"value")
1163 ret.push_back(finfo->
name());
1166 else if (finfoType ==
"srcFinfo" || finfoType ==
"src")
1171 ret.push_back(finfo->
name());
1174 else if (finfoType ==
"destFinfo" || finfoType ==
"dest")
1179 ret.push_back(finfo->
name());
1182 else if (finfoType ==
"lookupFinfo" || finfoType ==
"lookup")
1187 ret.push_back(finfo->
name());
1190 else if (finfoType ==
"sharedFinfo" || finfoType ==
"shared")
1195 ret.push_back(finfo->
name());
1198 else if (finfoType ==
"fieldElementFinfo" || finfoType ==
"fieldElement")
1203 ret.push_back(finfo->
name());
1217 vector<string>& fieldNames, vector<string>&fieldTypes)
1222 cerr <<
"Invalid class." << endl;
1225 if (finfoType ==
"valueFinfo" || finfoType ==
"value")
1230 fieldNames.push_back(finfo->
name());
1231 fieldTypes.push_back(finfo->
rttiType());
1234 else if (finfoType ==
"srcFinfo" || finfoType ==
"src")
1239 fieldNames.push_back(finfo->
name());
1240 fieldTypes.push_back(finfo->
rttiType());
1243 else if (finfoType ==
"destFinfo" || finfoType ==
"dest")
1248 fieldNames.push_back(finfo->
name());
1249 fieldTypes.push_back(finfo->
rttiType());
1252 else if (finfoType ==
"lookupFinfo" || finfoType ==
"lookup")
1257 fieldNames.push_back(finfo->
name());
1258 fieldTypes.push_back(finfo->
rttiType());
1261 else if (finfoType ==
"sharedFinfo" || finfoType ==
"shared")
1266 fieldNames.push_back(finfo->
name());
1267 fieldTypes.push_back(finfo->
rttiType());
1270 else if (finfoType ==
"fieldElementFinfo" || finfoType ==
"field" || finfoType ==
"fieldElement")
1275 fieldNames.push_back(finfo->
name());
1276 fieldTypes.push_back(finfo->
rttiType());
1297 string basetype_str =
"";
1298 PyTypeObject * base = NULL;
1299 for (base = Py_TYPE(
self);
1300 base != &
ObjIdType; base = base->tp_base)
1302 basetype_str = base->tp_name;
1303 size_t dot = basetype_str.find(
'.');
1304 basetype_str = basetype_str.substr(dot+1);
1318 "getFieldNames(className, finfoType='valueFinfo') -> tuple\n"
1320 "Get a tuple containing the name of all the fields of `finfoType`\n"
1325 "className : string\n"
1326 " Name of the class to look up.\n"
1327 "finfoType : string\n"
1328 " The kind of field "
1333 " `fieldElementFinfo` - \n"
1338 " Names of the fields of type `finfoType` in class `className`.\n"
1343 char * className = NULL;
1344 char _finfoType[] =
"valueFinfo";
1345 char * finfoType = _finfoType;
1346 if (!PyArg_ParseTuple(args,
"s|s", &className, &finfoType))
1350 vector <string> fieldNames =
getFieldNames(className, finfoType);
1351 PyObject * ret = PyTuple_New(fieldNames.size());
1353 for (
unsigned int ii = 0; ii < fieldNames.size(); ++ii)
1355 if (PyTuple_SetItem(ret, ii, PyString_FromString(fieldNames[ii].c_str())) == -1)
1365 "copy(src, dest, name, n, toGlobal, copyExtMsg) -> bool\n"
1367 "Make copies of a moose object.\n"
1371 "src : vec, element or str\n"
1373 "dest : vec, element or str\n"
1374 " Destination object to copy into.\n"
1376 " Name of the new object. If omitted, name of the original will be used.\n"
1378 " Number of copies to make.\n"
1380 " Relevant for parallel environments only. If false, the copies will\n"
1381 " reside on local node, otherwise all nodes get the copies.\n"
1382 "copyExtMsg : int\n"
1383 " If true, messages to/from external objects are also copied.\n"
1388 " newly copied vec\n"
1393 PyObject * src = NULL, * dest = NULL;
1394 char * newName = NULL;
1395 static const char * kwlist[] = {
"src",
"dest",
"name",
"n",
"toGlobal",
"copyExtMsg", NULL};
1396 unsigned int num=1, toGlobal=0, copyExtMsgs=0;
1397 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"OO|sIII", const_cast<char**>(kwlist), &src, &dest, &newName, &num, &toGlobal, ©ExtMsgs))
1403 if (PyObject_IsInstance(src, (PyObject*)&
IdType))
1407 else if (PyObject_IsInstance(src, (PyObject*)&
ObjIdType))
1409 _src = ((
_ObjId*)src)->oid_.id;
1411 else if (PyString_Check(src))
1413 _src =
Id(PyString_AsString(src));
1417 PyErr_SetString(PyExc_TypeError,
"Source must be instance of vec, element or string.");
1422 PyErr_SetString(PyExc_ValueError,
"Cannot make copy of moose shell.");
1425 if (PyObject_IsInstance(dest, (PyObject*)&
IdType))
1429 else if (PyObject_IsInstance(dest, (PyObject*)&
ObjIdType))
1431 _dest = ((
_ObjId*)dest)->oid_;
1433 else if (PyString_Check(dest))
1435 _dest =
ObjId(PyString_AsString(dest));
1439 PyErr_SetString(PyExc_TypeError,
"destination must be instance of vec, element or string.");
1446 else if (_dest.
bad())
1451 if (newName == NULL)
1458 name = string(newName);
1461 tgt->
id_ =
SHELLPTR->doCopy(_src, _dest, name, num, toGlobal, copyExtMsgs);
1462 PyObject * ret = (PyObject*)tgt;
1470 PyObject * src, * dest;
1471 if (!PyArg_ParseTuple(args,
"OO:moose_move", &src, &dest))
1477 if (PyObject_IsInstance(src, (PyObject*)&
IdType))
1481 else if (PyObject_IsInstance(src, (PyObject*)&
ObjIdType))
1483 _src = ((
_ObjId*)src)->oid_.id;
1485 else if (PyString_Check(src))
1487 _src =
Id(PyString_AsString(src));
1491 PyErr_SetString(PyExc_TypeError,
"Source must be instance of vec, element or string.");
1496 PyErr_SetString(PyExc_ValueError,
"Cannot make move moose shell.");
1499 if (PyObject_IsInstance(dest, (PyObject*)&
IdType))
1503 else if (PyObject_IsInstance(dest, (PyObject*)&
ObjIdType))
1505 _dest = ((
_ObjId*)dest)->oid_;
1507 else if (PyString_Check(dest))
1509 _dest =
ObjId(PyString_AsString(dest));
1513 PyErr_SetString(PyExc_TypeError,
"destination must be instance of vec, element or string.");
1520 else if (_dest.
bad())
1529 "delete(obj)->None\n"
1531 "Delete the underlying moose object. This does not delete any of the\n"
1532 "Python objects referring to this vec but does invalidate them. Any\n"
1533 "attempt to access them will raise a ValueError.\n"
1538 " vec of the object to be deleted.\n"
1548 bool isObjId_ =
false;
1549 if (!PyArg_ParseTuple(args,
"O:moose.delete", &obj))
1558 if (PyObject_IsInstance(obj, (PyObject*)&
IdType))
1560 oid_ = ((
_Id*)obj)->id_;
1563 else if (PyObject_IsInstance(obj, (PyObject*)&
ObjIdType))
1565 oid_ = ((
_ObjId*)obj)->oid_;
1568 else if (PyString_Check(obj))
1570 oid_ =
ObjId(PyString_AsString(obj));
1574 PyErr_SetString(PyExc_ValueError,
"cannot delete moose shell.");
1577 if (oid_ ==
ObjId())
1579 PyErr_SetString(PyExc_ValueError,
"cannot delete moose shell.");
1589 ((
_Id*)obj)->id_ =
Id();
1600 "useClock(tick, path, fn)\n\n"
1601 "schedule `fn` function of every object that matches `path` on tick no. `tick`.\n\n "
1602 "Most commonly the function is 'process'. NOTE: unlike earlier versions, now autoschedule is not available. You have to call useClock for every element that should be updated during the simulation.\n\n "
1603 "The sequence of clockticks with the same dt is according to their number. This is utilized for controlling the order of updates in various objects where it matters. The following convention should be observed when assigning clockticks to various components of a model:\n\n "
1604 "Clock ticks 0-3 are for electrical (biophysical) components, 4 and 5 are for chemical kinetics, 6 and 7 are for lookup tables and stimulus, 8 and 9 are for recording tables.\n\n "
1605 "Generally, `process` is the method to be assigned a clock tick. Notable exception is `init` method of Compartment class which is assigned tick 0.\n\n"
1606 " - 0 : Compartment: `init`\n"
1607 " - 1 : Compartment: `process`\n"
1608 " - 2 : HHChannel and other channels: `process`\n"
1609 " - 3 : CaConc : `process`\n"
1610 " - 4,5 : Elements for chemical kinetics : `process`\n"
1611 " - 6,7 : Lookup (tables), stimulus : `process`\n"
1612 " - 8,9 : Tables for plotting : `process`\n"
1617 " tick number on which the targets should be scheduled.\n"
1619 " path of the target element(s). This can be a wildcard also.\n"
1621 " name of the function to be called on each tick. Commonly `process`.\n"
1625 "In multi-compartmental neuron model a compartment's membrane potential (Vm) is dependent on its neighbours' membrane potential. Thus it must get the neighbour's present Vm before computing its own Vm in next time step. This ordering is achieved by scheduling the `init` function, which communicates membrane potential, on tick 0 and `process` function on tick 1.\n\n"
1626 " >>> moose.useClock(0, '/model/compartment_1', 'init')\n"
1627 " >>> moose.useClock(1, '/model/compartment_1', 'process')\n");
1631 char *
path, * field;
1633 if(!PyArg_ParseTuple(args,
"Iss:moose_useClock", &tick, &path, &field))
1637 SHELLPTR->doUseClock(
string(path),
string(field), tick);
1643 "setClock(tick, dt)\n"
1645 "set the ticking interval of `tick` to `dt`.\n"
1647 "A tick with interval `dt` will call the functions scheduled on that tick every `dt` timestep.\n"
1654 " ticking interval\n");
1660 if(!PyArg_ParseTuple(args,
"Id:moose_setClock", &tick, &dt))
1666 PyErr_SetString(PyExc_ValueError,
"dt must be positive.");
1674 "start(time, notify = False) -> None\n"
1676 "Run simulation for `t` time. Advances the simulator clock by `t`\n"
1677 "time. If 'notify = True', a message is written to terminal whenever \n"
1678 "10\% of simulation time is over. \n"
1680 "After setting up a simulation, YOU MUST CALL MOOSE.REINIT() before\n"
1681 "CALLING MOOSE.START() TO EXECUTE THE SIMULATION. Otherwise, the\n"
1682 "simulator behaviour will be undefined. Once moose.reinit() has been\n"
1683 "called, you can call moose.start(t) as many time as you like. This\n"
1684 "will continue the simulation from the last state for `t` time.\n"
1689 " duration of simulation.\n"
1691 " default False. If True, notify user whenever 10\% of simultion \n"
1700 "moose.reinit : (Re)initialize simulation\n"
1705 double runtime = 0.0;
1706 bool notify =
false;
1708 PyArg_ParseTuple(args,
"d|I:moose_start", &runtime, ¬ify);
1712 PyErr_SetString(PyExc_ValueError,
"simulation runtime must be positive.");
1717 struct sigaction sigHandler;
1719 sigemptyset(&sigHandler.sa_mask);
1720 sigHandler.sa_flags = 0;
1721 sigaction(SIGINT, &sigHandler, NULL);
1731 SHELLPTR->doStart( runtime, notify );
1737 "reinit() -> None\n"
1739 "Reinitialize simulation.\n"
1741 "This function (re)initializes moose simulation. It must be called\n"
1742 "before you start the simulation (see moose.start). If you want to\n"
1743 "continue simulation after you have called moose.reinit() and\n"
1744 "moose.start(), you must NOT call moose.reinit() again. Calling\n"
1745 "moose.reinit() again will take the system back to initial setting\n"
1746 "(like clear out all data recording tables, set state variables to\n"
1747 "their initial values, etc.\n"
1761 return Py_BuildValue(
"i",
SHELLPTR->isRunning());
1767 if (!PyArg_ParseTuple(args,
"s", &path))
1771 return Py_BuildValue(
"i",
Id(path) !=
Id() ||
string(path) ==
"/" ||
string(path) ==
"/root");
1775 "loadModel(filename, modelpath, solverclass) -> vec\n"
1777 "Load model from a file to a specified path.\n"
1782 " model description file.\n"
1784 " moose path for the top level element of the model to be created.\n"
1785 "solverclass : str, optional\n"
1786 " solver type to be used for simulating the model.\n"
1791 " loaded model container vec.\n"
1796 char * fname = NULL, * modelpath = NULL, * solverclass = NULL;
1798 if(!PyArg_ParseTuple(args,
"ss|s:moose_loadModel", &fname, &modelpath, &solverclass))
1800 cout <<
"here in moose load";
1806 model->
id_ =
SHELLPTR->doLoadModel(
string(fname),
string(modelpath));
1810 model->
id_ =
SHELLPTR->doLoadModel(
string(fname),
string(modelpath),
string(solverclass));
1812 if (model->
id_ ==
Id())
1815 PyErr_SetString(PyExc_IOError,
"could not load model");
1818 PyObject * ret =
reinterpret_cast<PyObject*
>(model);
1881 PyObject * element = NULL;
1884 if (PyTuple_Size(args) == 0)
1888 else if(PyArg_ParseTuple(args,
"s:moose_setCwe", &path))
1890 oid =
ObjId(
string(path));
1892 else if (PyArg_ParseTuple(args,
"O:moose_setCwe", &element))
1895 if (PyObject_IsInstance(element, (PyObject*)&
IdType))
1897 oid = (
reinterpret_cast<_Id*
>(element))->id_;
1899 else if (PyObject_IsInstance(element, (PyObject*)&
ObjIdType))
1901 oid = (
reinterpret_cast<_ObjId*
>(element))->oid_;
1905 PyErr_SetString(PyExc_NameError,
"setCwe: Argument must be an vec or element");
1923 if (!PyArg_ParseTuple(args,
":moose_getCwe"))
1935 "connect(src, srcfield, destobj, destfield[,msgtype]) -> bool\n"
1937 "Create a message between `src_field` on `src` object to `dest_field` on `dest` object.\n"
1938 "This function is used mainly, to say, connect two entities, and to denote what kind of give-and-take relationship they share."
1939 "It enables the 'destfield' (of the 'destobj') to acquire the data, from 'srcfield'(of the 'src')."
1943 "src : element/vec/string\n"
1944 " the source object (or its path) \n"
1945 " (the one that provides information)\n"
1947 " source field on self.(type of the information)\n"
1948 "destobj : element\n"
1949 " Destination object to connect to.\n"
1950 " (The one that need to get information)\n"
1952 " field to connect to on `destobj`.\n"
1954 " type of the message. Can be \n"
1961 " Default: `Single`.\n"
1965 "msgmanager: melement\n"
1966 " message-manager for the newly created message.\n"
1970 "Connect the output of a pulse generator to the input of a spike\n"
1973 " >>> pulsegen = moose.PulseGen('pulsegen')\n"
1974 " >>> spikegen = moose.SpikeGen('spikegen')\n"
1975 " >>> pulsegen.connect('output', spikegen, 'Vm')\n"
1984 PyObject * srcPtr = NULL, * destPtr = NULL;
1985 char * srcField = NULL, * destField = NULL, * msgType = NULL;
1986 static char default_msg_type[] =
"Single";
1987 if(!PyArg_ParseTuple(args,
"OsOs|s:moose_connect", &srcPtr, &srcField, &destPtr, &destField, &msgType))
1991 if (msgType == NULL)
1993 msgType = default_msg_type;
2003 _Id * _src =
reinterpret_cast<_Id*
>(srcPtr);
2006 else if (PyString_Check(srcPtr))
2008 char * _src = PyString_AsString(srcPtr);
2009 src =
ObjId(
string(_src));
2013 PyErr_SetString(PyExc_TypeError,
"source does not resolve to an element.");
2023 _Id * _dest =
reinterpret_cast<_Id*
>(destPtr);
2026 else if (PyString_Check(destPtr))
2028 char * _dest = PyString_AsString(destPtr);
2029 dest =
ObjId(
string(_dest));
2033 PyErr_SetString(PyExc_TypeError,
"target does not resolve to an element.");
2040 ObjId mid =
SHELLPTR->doAddMsg(msgType, src,
string(srcField), dest,
string(destField));
2043 PyErr_SetString(PyExc_NameError,
"check field names and type compatibility.");
2051 "getFieldDict(className, finfoType) -> dict\n"
2053 "Get dictionary of field names and types for specified class.\n"
2058 " MOOSE class to find the fields of.\n"
2059 "finfoType : str (optional)\n"
2060 " Finfo type of the fields to find. If empty or not specified, all\n"
2061 " fields will be retrieved.\n"
2066 " field names and their types.\n"
2070 " This behaviour is different from `getFieldNames` where only\n"
2071 " `valueFinfo`s are returned when `finfoType` remains unspecified.\n"
2075 "List all the source fields on class Neutral::\n"
2077 ">>> moose.getFieldDict('Neutral', 'srcFinfo')\n"
2078 " {'childMsg': 'int'}\n"
2083 char * className = NULL;
2084 char * fieldType = NULL;
2085 if (!PyArg_ParseTuple(args,
"s|s:moose_getFieldDict", &className, &fieldType))
2089 if (!className || (strlen(className) <= 0))
2091 PyErr_SetString(PyExc_ValueError,
"Expected non-empty class name.");
2095 Id classId =
Id(
"/classes/" +
string(className));
2096 if (classId ==
Id())
2098 string msg = string(className);
2099 msg +=
" not a valid MOOSE class.";
2100 PyErr_SetString(PyExc_NameError, msg.c_str());
2103 static const char * finfoTypes [] = {
"valueFinfo",
"lookupFinfo",
"srcFinfo",
"destFinfo",
"sharedFinfo", NULL};
2104 vector <string> fields, types;
2105 if (fieldType && strlen(fieldType) > 0)
2107 if (
getFieldDict(className,
string(fieldType), fields, types) == 0)
2109 PyErr_SetString(PyExc_ValueError,
"Invalid finfo type.");
2115 for (
const char ** ptr = finfoTypes; *ptr != NULL; ++ptr)
2117 if (
getFieldDict(className,
string(*ptr), fields, types) == 0)
2119 string message =
"No such finfo type: ";
2120 message += string(*ptr);
2121 PyErr_SetString(PyExc_ValueError, message.c_str());
2126 PyObject * ret = PyDict_New();
2129 PyErr_SetString(PyExc_SystemError,
"Could not allocate dictionary object.");
2132 for (
unsigned int ii = 0; ii < fields.size(); ++ ii)
2134 PyObject *
value = Py_BuildValue(
"s", types[ii].c_str());
2135 if (value == NULL || PyDict_SetItemString(ret, fields[ii].c_str(), value) == -1)
2150 if (!PyArg_ParseTuple(args,
"Oss:moose_getfield", &pyobj, &field, &type))
2154 if (!PyObject_IsInstance(pyobj, (PyObject*)&
ObjIdType))
2156 PyErr_SetString(PyExc_TypeError,
"moose.getField(element, fieldname, fieldtype): First argument must be an instance of element or its subclass");
2159 string fname(field), ftype(type);
2166 if (ftype ==
"char")
2169 return PyInt_FromLong(value);
2171 else if (ftype ==
"double")
2174 return PyFloat_FromDouble(value);
2176 else if (ftype ==
"float")
2179 return PyFloat_FromDouble(value);
2181 else if (ftype ==
"int")
2184 return PyInt_FromLong(value);
2186 else if (ftype ==
"string")
2189 return PyString_FromString(value.c_str());
2191 else if (ftype ==
"unsigned int" || ftype ==
"unsigned" || ftype ==
"uint")
2194 return PyInt_FromLong(value);
2196 else if (ftype ==
"Id")
2200 return (PyObject*)
value;
2202 else if (ftype ==
"ObjId")
2206 return (PyObject*)
value;
2208 else if (ftype ==
"vector<int>")
2211 PyObject * ret = PyTuple_New((Py_ssize_t)value.size());
2213 for (
unsigned int ii = 0; ii < value.size(); ++ ii )
2215 PyObject * entry = Py_BuildValue(
"i", value[ii]);
2216 if (!entry || PyTuple_SetItem(ret, (Py_ssize_t)ii, entry))
2225 else if (ftype ==
"vector<double>")
2228 PyObject * ret = PyTuple_New((Py_ssize_t)value.size());
2230 for (
unsigned int ii = 0; ii < value.size(); ++ ii )
2232 PyObject * entry = Py_BuildValue(
"f", value[ii]);
2233 if (!entry || PyTuple_SetItem(ret, (Py_ssize_t)ii, entry))
2242 else if (ftype ==
"vector<float>")
2245 PyObject * ret = PyTuple_New((Py_ssize_t)value.size());
2247 for (
unsigned int ii = 0; ii < value.size(); ++ ii )
2249 PyObject * entry = Py_BuildValue(
"f", value[ii]);
2250 if (!entry || PyTuple_SetItem(ret, (Py_ssize_t)ii, entry))
2259 else if (ftype ==
"vector<string>")
2262 PyObject * ret = PyTuple_New((Py_ssize_t)value.size());
2264 for (
unsigned int ii = 0; ii < value.size(); ++ ii )
2266 PyObject * entry = Py_BuildValue(
"s", value[ii].c_str());
2267 if (!entry || PyTuple_SetItem(ret, (Py_ssize_t)ii, entry))
2275 else if (ftype ==
"vector<Id>")
2278 PyObject * ret = PyTuple_New((Py_ssize_t)value.size());
2280 for (
unsigned int ii = 0; ii < value.size(); ++ ii )
2283 entry->
id_ = value[ii];
2284 if (PyTuple_SetItem(ret, (Py_ssize_t)ii, (PyObject*)entry))
2292 else if (ftype ==
"vector<ObjId>")
2295 PyObject * ret = PyTuple_New((Py_ssize_t)value.size());
2297 for (
unsigned int ii = 0; ii < value.size(); ++ ii )
2300 entry->
oid_ = value[ii];
2301 if (PyTuple_SetItem(ret, (Py_ssize_t)ii, (PyObject*)entry))
2309 PyErr_SetString(PyExc_TypeError,
"Field type not handled.");
2314 "moose.seed(seedvalue) -> seed \n"
2316 "Reseed MOOSE random number generator.\n"
2321 " Value to use for seeding. \n"
2322 " All RNGs in moose except rand functions in moose.Function \n"
2323 " expression use this seed.\n"
2324 " By default (when this function is not called) seed is initializecd \n"
2325 " to some random value using system random device (if available). \n"
2328 " default: random number generated using system random device\n"
2336 "moose.rand() : get a pseudorandom number in the [0,1) interval.\n"
2342 if (!PyArg_ParseTuple(args,
"|l", &seed))
2351 "moose.rand() -> [0,1)\n"
2355 "float in [0, 1) real interval generated by MT19937.\n"
2359 "moose.seed() : reseed the random number generator.\n"
2363 "MOOSE does not automatically seed the random number generator. You\n"
2364 "must explicitly call moose.seed() to create a new sequence of random\n"
2365 "numbers each time.\n"
2374 "moose.wildcardFind(expression) -> tuple of melements.\n"
2376 "Find an object by wildcard.\n"
2380 "expression : str\n"
2381 " MOOSE allows wildcard expressions of the form::\n"
2383 " {PATH}/{WILDCARD}[{CONDITION}]\n"
2385 " where {PATH} is valid path in the element tree.\n"
2386 " `{WILDCARD}` can be `#` or `##`.\n"
2388 " `#` causes the search to be restricted to the children of the\n"
2389 " element specified by {PATH}.\n"
2391 " `##` makes the search to recursively go through all the descendants\n"
2392 " of the {PATH} element.\n"
2393 " {CONDITION} can be::\n"
2395 " TYPE={CLASSNAME} : an element satisfies this condition if it is of\n"
2396 " class {CLASSNAME}.\n"
2397 " ISA={CLASSNAME} : alias for TYPE={CLASSNAME}\n"
2398 " CLASS={CLASSNAME} : alias for TYPE={CLASSNAME}\n"
2399 " FIELD({FIELDNAME}){OPERATOR}{VALUE} : compare field {FIELDNAME} with\n"
2400 " {VALUE} by {OPERATOR} where {OPERATOR} is a comparison operator (=,\n"
2401 " !=, >, <, >=, <=).\n"
2403 " For example, /mymodel/##[FIELD(Vm)>=-65] will return a list of all\n"
2404 " the objects under /mymodel whose Vm field is >= -65.\n"
2409 " all elements that match the wildcard.\n"
2414 vector <ObjId> objects;
2415 char * wildcard_path = NULL;
2416 if (!PyArg_ParseTuple(args,
"s:moose.wildcardFind", &wildcard_path))
2421 PyObject * ret = PyTuple_New(objects.size());
2424 PyErr_SetString(PyExc_RuntimeError,
"moose.wildcardFind: failed to allocate new tuple.");
2428 for (
unsigned int ii = 0; ii < objects.size(); ++ii)
2434 PyErr_SetString(PyExc_RuntimeError,
"moose.wildcardFind: failed to allocate new vec.");
2437 if (PyTuple_SetItem(ret, (Py_ssize_t)ii, entry))
2453 cout <<
"Quitting MOOSE." << endl;
2463 static vector <Id> classes(
Field< vector<Id> >::
get(
ObjId(
"/classes"),
2465 for (
unsigned ii = 0; ii < classes.size(); ++ii)
2467 const string& className = classes[ii].element()->getName();
2470 cout <<
"\nCreating " << className << endl;
2475 cerr <<
"Error: no cinfo found with name " << className << endl;
2494 "*-----------------------------------------------------------------*\n"
2495 "* This is Python generated documentation. *\n"
2496 "* Use moose.doc('classname') to display builtin documentation for *\n"
2497 "* class `classname`. *\n"
2498 "* Use moose.doc('classname.fieldname') to display builtin *\n"
2499 "* documentation for `field` in class `classname`. *\n"
2500 "*-----------------------------------------------------------------*\n"
2505 const string & className = cinfo->
name();
2509 cout <<
"\tDefining lookupFields for " << className << endl;
2514 for (
unsigned int ii = 0; ii < num; ++ii)
2516 const string&
name =
const_cast<Cinfo*
>(cinfo)->getLookupFinfo(ii)->
name();
2519 get_getsetdefs()[className][currIndex].name = (
char*)calloc(name.size() + 1,
sizeof(char));
2520 strncpy(const_cast<char*>(
get_getsetdefs()[className][currIndex].name)
2521 , const_cast<char*>(name.c_str()), name.size());
2522 get_getsetdefs()[className][currIndex].doc = (
char*)
"Lookup field";
2524 PyObject * args = PyTuple_New(1);
2525 PyTuple_SetItem(args, 0, PyString_FromString(name.c_str()));
2530 cout <<
"\tDefined lookupField " <<
get_getsetdefs()[className][currIndex].name << endl;
2542 const string& className = cinfo->
name();
2543 map <string, PyTypeObject * >::iterator existing =
2553 cout <<
"Defining class " << className << endl;
2561 string str =
"moose." + className;
2563 PyTypeObject * new_class =
2564 (PyTypeObject*)PyType_Type.tp_alloc(&PyType_Type, 0);
2569 new_class->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE;
2570 PyHeapTypeObject* et = (PyHeapTypeObject*)new_class;
2571 et->ht_name = PyUnicode_FromString(className.c_str());
2572 #if PY_MINOR_VERSION >= 3
2573 et->ht_qualname = PyUnicode_FromString(str.c_str());
2576 new_class->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
2605 new_class->tp_name = strdup(str.c_str());
2606 new_class->tp_doc = moose_Class_documentation;
2609 map<string, PyTypeObject *>::iterator base_iter =
2617 new_class->tp_base = base_iter->second;
2619 Py_INCREF(new_class->tp_base);
2659 if (PyType_Ready(new_class) < 0)
2661 cerr <<
"Fatal error: Could not initialize class '" << className
2666 Py_INCREF(new_class);
2669 PyDict_SetItemString(new_class->tp_dict,
"__module__", PyUnicode_InternFromString(
"moose"));
2679 if (!PyObject_IsInstance(
self, (PyObject*)&
ObjIdType))
2681 PyErr_SetString(PyExc_TypeError,
"First argument must be an instance of element");
2690 if (!PyArg_ParseTuple((PyObject *)closure,
2691 "s:_get_destField: "
2692 "expected a string in getter closure.",
2697 PyObject * args = PyTuple_New(2);
2699 PyTuple_SetItem(args, 0,
self);
2701 PyTuple_SetItem(args, 1, PyString_FromString(name));
2705 Py_XDECREF((PyObject*)ret);
2707 PyErr_SetString(PyExc_RuntimeError,
"moose_ObjId_get_destField_attr: failed to init DestField object");
2710 return (PyObject*)ret;
2716 const string& className = cinfo->
name();
2720 cout <<
"\tCreating destField attributes for " << className << endl;
2729 size_t currIndex = vec.size();
2732 Finfo * destFinfo =
const_cast<Cinfo*
>(cinfo)->getDestFinfo(ii);
2733 const string&
name = destFinfo->
name();
2748 PyGetSetDef destFieldGetSet;
2749 vec.push_back(destFieldGetSet);
2751 vec[currIndex].name = strdup(name.c_str());
2752 vec[currIndex].doc = (
char*)
"Destination field";
2754 PyObject *args = PyTuple_New(1);
2755 if (!args || !vec[currIndex].name) {
2756 cerr <<
"moosemodule.cpp: defineDestFinfos: allocation failed\n";
2759 PyTuple_SetItem(args, 0, PyString_FromString(name.c_str()));
2760 vec[currIndex].closure = (
void*)args;
2784 if (!PyObject_IsInstance(
self, (PyObject*)&
ObjIdType))
2786 PyErr_SetString(PyExc_TypeError,
2787 "First argument must be an instance of element");
2796 if (!PyArg_ParseTuple((PyObject *)closure,
2797 "s:moose_ObjId_get_lookupField_attr: expected a string in getter closure.",
2806 PyObject * args = PyTuple_New(2);
2807 PyTuple_SetItem(args, 0,
self);
2809 PyTuple_SetItem(args, 1, PyString_FromString(name));
2813 Py_XDECREF((PyObject*)ret);
2815 PyErr_SetString(PyExc_RuntimeError,
2816 "moose_ObjId_get_lookupField_attr: failed to init LookupField object");
2819 return (PyObject*)ret;
2836 if (!PyArg_ParseTuple((PyObject *)closure,
2837 "s:moose_ObjId_get_elementField_attr: expected a string in getter closure.",
2846 PyObject * args = PyTuple_New(2);
2847 PyTuple_SetItem(args, 0,
self);
2849 PyTuple_SetItem(args, 1, PyString_FromString(name));
2855 Py_DECREF((PyObject*)ret);
2857 PyErr_SetString(PyExc_RuntimeError,
"moose_ObjId_get_elementField_attr: failed to init ElementField object");
2860 return (PyObject*)ret;
2865 const string & className = cinfo->
name();
2869 cout <<
"\tDefining elementFields for " << className << endl;
2874 for (
unsigned int ii = 0; ii < num; ++ii)
2876 const string&
name =
const_cast<Cinfo*
>(cinfo)->getFieldElementFinfo(ii)->
name();
2879 get_getsetdefs()[className][currIndex].name = (
char*)calloc(name.size() + 1,
sizeof(char));
2880 strncpy(const_cast<char*>(
get_getsetdefs()[className][currIndex].name)
2881 , const_cast<char*>(name.c_str()), name.size());
2882 get_getsetdefs()[className][currIndex].doc = (
char*)
"Element field";
2884 PyObject * args = PyTuple_New(1);
2885 PyTuple_SetItem(args, 0, PyString_FromString(name.c_str()));
2890 cout <<
"\tDefined elementField " <<
get_getsetdefs()[className][currIndex].name << endl;
2907 PyTypeObject * pyclass = it->second;
2909 new_obj->
oid_ = oid;
2911 return (PyObject*)new_obj;
2915 "moose.element(arg) -> moose object\n"
2917 "Convert a path or an object to the appropriate builtin moose class\n"
2922 "arg : str/vec/moose object\n"
2923 " path of the moose element to be converted or another element (possibly\n"
2924 " available as a superclass instance).\n"
2929 " MOOSE element (object) corresponding to the `arg` converted to write subclass.\n"
2934 PyObject * obj = NULL;
2936 unsigned nid = 0, did = 0, fidx = 0;
2938 unsigned int numData = 0;
2939 if (PyArg_ParseTuple(args,
"s", &path))
2945 PyErr_SetString(PyExc_ValueError, ( std::string(
"moose_element: '")
2947 + std::string(
"' does not exist!")
2957 PyErr_SetString(PyExc_TypeError,
"moose_element: unknown class");
2961 if (PyArg_ParseTuple(args,
"I|II", &nid, &did, &fidx))
2963 oid =
ObjId(
id, did, fidx);
2968 PyErr_SetString(PyExc_RuntimeError,
"moose_element: not a moose class.");
2973 if (!PyArg_ParseTuple(args,
"O|II", &obj, &did, &fidx))
2975 PyErr_SetString(PyExc_TypeError,
"moose_element: argument must be a path or an existing element or an vec");
2979 if (PyObject_IsInstance(obj, (PyObject*)&
ObjIdType))
2981 oid = ((
_ObjId*)obj)->oid_;
2983 else if (PyObject_IsInstance(obj, (PyObject*)&
IdType))
2985 oid =
ObjId(((
_Id*)obj)->id_, did, fidx);
2993 PyErr_SetString(PyExc_TypeError,
"moose_element: cannot convert to moose element.");
2999 PyErr_SetString(PyExc_RuntimeError,
"moose_element: not a moose class.");
3010 {
"element", (PyCFunction)
moose_element, METH_VARARGS, moose_element_documentation},
3011 {
"getFieldNames", (PyCFunction)
moose_getFieldNames, METH_VARARGS, moose_getFieldNames_documentation},
3012 {
"copy", (PyCFunction)
moose_copy, METH_VARARGS|METH_KEYWORDS, moose_copy_documentation},
3013 {
"move", (PyCFunction)
moose_move, METH_VARARGS,
"Move a vec object to a destination."},
3014 {
"delete", (PyCFunction)
moose_delete, METH_VARARGS, moose_delete_documentation},
3015 {
"useClock", (PyCFunction)
moose_useClock, METH_VARARGS, moose_useClock_documentation},
3016 {
"setClock", (PyCFunction)
moose_setClock, METH_VARARGS, moose_setClock_documentation},
3017 {
"start", (PyCFunction)
moose_start, METH_VARARGS, moose_start_documentation},
3018 {
"reinit", (PyCFunction)
moose_reinit, METH_VARARGS, moose_reinit_documentation},
3019 {
"stop", (PyCFunction)
moose_stop, METH_VARARGS,
"Stop simulation"},
3020 {
"isRunning", (PyCFunction)
moose_isRunning, METH_VARARGS,
"True if the simulation is currently running."},
3021 {
"exists", (PyCFunction)
moose_exists, METH_VARARGS,
"True if there is an object with specified path."},
3022 {
"loadModel", (PyCFunction)
moose_loadModel, METH_VARARGS, moose_loadModel_documentation},
3024 {
"connect", (PyCFunction)
moose_connect, METH_VARARGS, moose_connect_documentation},
3025 {
"getCwe", (PyCFunction)
moose_getCwe, METH_VARARGS,
"Get the current working element. 'pwe' is an alias of this function."},
3026 {
"setCwe", (PyCFunction)
moose_setCwe, METH_VARARGS,
"Set the current working element. 'ce' is an alias of this function"},
3027 {
"getFieldDict", (PyCFunction)
moose_getFieldDict, METH_VARARGS, moose_getFieldDict_documentation},
3030 "getField(element, field, fieldtype) -- Get specified field of specified type from object vec."
3032 {
"seed", (PyCFunction)
moose_seed, METH_VARARGS, moose_seed_documentation},
3033 {
"rand", (PyCFunction)
moose_rand, METH_NOARGS, moose_rand_documentation},
3034 {
"wildcardFind", (PyCFunction)
moose_wildcardFind, METH_VARARGS, moose_wildcardFind_documentation},
3036 "quit", (PyCFunction)
moose_quit, METH_NOARGS,
"Finalize MOOSE threads and quit MOOSE. This is made available for"
3037 " debugging purpose only. It will automatically get called when moose"
3038 " module is unloaded. End user should not use this function."
3041 {NULL, NULL, 0, NULL}
3050 "MOOSE = Multiscale Object-Oriented Simulation Environment.\n"
3052 "Moose is the core of a modern software platform for the simulation\n"
3053 "of neural systems ranging from subcellular components and\n"
3054 "biochemical reactions to complex models of single neurons, large\n"
3055 "networks, and systems-level processes.");
3059 int moose_traverse(PyObject *m, visitproc visit,
void *arg)
3065 int moose_clear(PyObject *m)
3075 static struct PyModuleDef MooseModuleDef =
3077 PyModuleDef_HEAD_INIT,
3079 moose_module_documentation,
3088 #define INITERROR return NULL
3089 #define MODINIT(name) PyInit_##name()
3091 #define INITERROR return
3092 #define MODINIT(name) init##name()
3097 clock_t modinit_start = clock();
3105 int argc = args.size();
3106 char ** argv = (
char**)calloc(args.size(),
sizeof(
char*));
3107 for (
int ii = 0; ii < argc; ++ii)
3109 argv[ii] = (
char*)(calloc(args[ii].length()+1,
sizeof(char)));
3110 strncpy(argv[ii], args[ii].c_str(), args[ii].length()+1);
3115 for (
int ii = 1; ii < argc; ++ii)
3121 PyObject * moose_module = PyModule_Create(&MooseModuleDef);
3123 PyObject *moose_module = Py_InitModule3(
"_moose",
3125 moose_module_documentation);
3127 if (moose_module == NULL)
3132 char error[] =
"moose.Error";
3133 st->
error = PyErr_NewException(error, NULL, NULL);
3134 if (st->
error == NULL)
3136 Py_XDECREF(moose_module);
3139 int registered = Py_AtExit(&
finalize);
3140 if (registered != 0)
3142 cerr <<
"Failed to register finalize() to be called at exit. " << endl;
3149 IdType.tp_new = PyType_GenericNew;
3150 if (PyType_Ready(&
IdType) < 0)
3156 PyModule_AddObject(moose_module,
"vec", (PyObject*)&
IdType);
3167 PyModule_AddObject(moose_module,
"melement", (PyObject*)&
ObjIdType);
3199 PyModule_AddObject(moose_module,
"DestField", (PyObject*)&
moose_DestField);
3202 PyModule_AddIntConstant(moose_module,
"NUMCORES",
numCores);
3203 PyModule_AddIntConstant(moose_module,
"NUMNODES",
numNodes);
3205 PyModule_AddIntConstant(moose_module,
"MYNODE",
myNode);
3206 PyModule_AddIntConstant(moose_module,
"INFINITE",
isInfinite);
3207 PyModule_AddStringConstant(moose_module,
"__version__",
SHELLPTR->doVersion().c_str());
3208 PyModule_AddStringConstant(moose_module,
"VERSION",
SHELLPTR->doVersion().c_str());
3209 PyObject * module_dict = PyModule_GetDict(moose_module);
3210 clock_t defclasses_start = clock();
3219 PyModule_AddObject(moose_module, ii->first.c_str(), (PyObject*)(ii->second));
3222 clock_t defclasses_end = clock();
3225 << (defclasses_end - defclasses_start) * 1.0 /CLOCKS_PER_SEC
3229 clock_t modinit_end = clock();
3232 << (modinit_end - modinit_start) * 1.0 /CLOCKS_PER_SEC
3240 return moose_module;
static unsigned int numCores
int wildcardFind(const string &path, vector< ObjId > &ret)
PyObject * deleteObjId(ObjId obj)
const Cinfo * baseCinfo() const
PyObject * moose_exists(PyObject *dummy, PyObject *args)
void mtseed(unsigned int x)
Set the global seed or all rngs.
Finfo * getFieldElementFinfo(unsigned int i) const
PyObject * moose_start(PyObject *dummy, PyObject *args)
#define ObjId_SubtypeCheck(v)
int defineElementFinfos(const Cinfo *cinfo)
static void clearAllElements()
const char ** getFinfoTypes()
finalize()
Finfo * getDestFinfo(unsigned int i) const
Finfo * getValueFinfo(unsigned int i) const
unsigned int getNumSrcFinfo() const
PyObject * moose_reinit(PyObject *dummy, PyObject *args)
PyObject * moose_delete(PyObject *dummy, PyObject *args)
PyTypeObject moose_ElementField
PyObject * moose_getFieldNames(PyObject *dummy, PyObject *args)
void doSetClock(unsigned int tickNum, double dt)
unsigned int getNumDestFinfo() const
PyObject * convert_and_set_tuple_entry(PyObject *tuple, unsigned int index, void *vptr, char typecode)
static void cleanSimulation()
Clean-up MOOSE before shutting down. This function is called whenever keyboard interrupt terminates t...
PyObject * moose_getCwe(PyObject *dummy, PyObject *args)
void finalize()
create_shell()
static DestFinfo dummy("dummy","This Finfo is a dummy. If you are reading this you have used an invalid index", 0)
map< string, PyTypeObject * > & get_moose_classes()
PyObject * moose_getFieldDict(PyObject *dummy, PyObject *args)
unsigned int getNumValueFinfo() const
PyObject * moose_ObjId_get_destField_attr(PyObject *self, void *closure)
virtual string rttiType() const
static const Cinfo * find(const std::string &name)
PyObject * moose_element(PyObject *dummy, PyObject *args)
vector< ObjId > all_elements(Id id)
PyObject * moose_setClock(PyObject *dummy, PyObject *args)
const map< string, string > & get_field_alias()
PyObject * moose_ObjId_get_elementField_attr(PyObject *self, void *closure)
PyDoc_STRVAR(moose_getFieldNames_documentation,"getFieldNames(className, finfoType='valueFinfo') -> tuple\n""\n""Get a tuple containing the name of all the fields of `finfoType`\n""kind.\n""\n""Parameters\n""----------\n""className : string\n"" Name of the class to look up.\n""finfoType : string\n"" The kind of field "" `valueFinfo` - "" `srcFinfo` - "" `destFinfo` - "" `lookupFinfo`- "" `fieldElementFinfo` - \n""\n""Returns\n""-------\n""tuple\n"" Names of the fields of type `finfoType` in class `className`.\n")
const string & name() const
void testBuiltinsProcess()
#define ElementField_SubtypeCheck(v)
#define RAISE_INVALID_ID(ret, msg)
PyObject * moose_ObjId_get_lookupField_attr(PyObject *self, void *closure)
int defineClass(PyObject *module_dict, const Cinfo *cinfo)
PyObject * moose_quit(PyObject *dummy)
PyObject * moose_setCwe(PyObject *dummy, PyObject *args)
void mooseBenchmarks(unsigned int option)
map< string, vector< PyGetSetDef > > & get_getsetdefs()
int defineDestFinfos(const Cinfo *cinfo)
double pymoose_mtrand_(void)
void * to_cpp(PyObject *object, char typecode)
static int doRegressionTests
const std::string & name() const
static PyMethodDef MooseMethods[]
PyObject * moose_stop(PyObject *dummy, PyObject *args)
int defineLookupFinfos(const Cinfo *cinfo)
static unsigned int myNode
PyTypeObject moose_DestField
void testSchedulingProcess()
#define Id_SubtypeCheck(v)
PyObject * moose_connect(PyObject *dummy, PyObject *args)
PyObject * moose_getField(PyObject *dummy, PyObject *args)
Finfo * getLookupFinfo(unsigned int i) const
PyObject * moose_useClock(PyObject *dummy, PyObject *args)
void processTests(Shell *)
PyObject * to_pytuple(void *obj, char typecode)
unsigned int getNumFieldElementFinfo() const
vector< int > pysequence_to_dimvec(PyObject *dims)
Finfo * getSrcFinfo(unsigned int i) const
string getFieldType(string className, string fieldName)
const map< string, string > & getArgMap()
static bool isValid(Id id)
void nonMpiTests(Shell *)
string getBaseClass() const
int parseFinfoType(string className, string finfoType, string fieldName, vector< string > &typeVec)
vector< string > setup_runtime_env()
PyObject * moose_seed(PyObject *dummy, PyObject *args)
PyObject * moose_copy(PyObject *dummy, PyObject *args, PyObject *kwargs)
PyTypeObject moose_LookupField
static unsigned int numNodes
#define LOG(t, a)
This macro only expands when not compiling for release.
Id init(int argc, char **argv, bool &doUnitTests, bool &doRegressionTests, unsigned int &benchmark)
PyObject * moose_wildcardFind(PyObject *dummy, PyObject *args)
void tokenize(const string &str, const string &delimiters, vector< string > &tokens)
void handle_keyboard_interrupts(int signum)
Handle signal raised by user during simulation.
double mtrand(void)
Generate a random double between 0 and 1.
Id getShell(int argc, char **argv)
setup_runtime_env()
PyObject * to_py(void *obj, char typecode)
void destroyMsgManagers()
static unsigned int myNode()
void doUseClock(string path, string field, unsigned int tick)
PyTypeObject * getBaseClass(PyObject *self)
static A get(const ObjId &dest, const string &field)
void speedTestMultiNodeIntFireNetwork(unsigned int size, unsigned int runsteps)
PyObject * moose_loadModel(PyObject *dummy, PyObject *args)
PyObject * moose_move(PyObject *dummy, PyObject *args)
PyObject * moose_ElementField_getId(_Field *self, void *closure)
static unsigned int numNodes()
unsigned int getNumLookupFinfo() const
PyObject * oid_to_element(ObjId oid)
int getFieldDict(string className, string finfoType, vector< string > &fieldNames, vector< string > &fieldTypes)
int defineAllClasses(PyObject *module_dict)
vector< string > getFieldNames(string className, string finfoType)
const unsigned int BADINDEX
Used by ObjId and Eref.
void pymoose_mtseed_(long int seed)
PyObject * moose_isRunning(PyObject *dummy, PyObject *args)
PyObject * moose_rand(PyObject *dummy)
static void clearAllMsgs()
const Finfo * findFinfo(const string &name) const