MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PyRun Class Reference

#include <PyRun.h>

+ Collaboration diagram for PyRun:

Public Member Functions

PyObject * getGlobals () const
 
string getInitString () const
 
string getInputVar () const
 
PyObject * getLocals () const
 
int getMode () const
 
string getOutputVar () const
 
string getRunString () const
 
void process (const Eref &e, ProcPtr p)
 
 PyRun ()
 
void reinit (const Eref &e, ProcPtr p)
 
void run (const Eref &e, string statement)
 
void setGlobals (PyObject *globals)
 
void setInitString (string str)
 
void setInputVar (string name)
 
void setLocals (PyObject *locals)
 
void setMode (int flag)
 
void setOutputVar (string name)
 
void setRunString (string str)
 
void trigger (const Eref &e, double input)
 
 ~PyRun ()
 

Static Public Member Functions

static const CinfoinitCinfo ()
 

Static Public Attributes

static const int RUNBOTH = 0
 
static const int RUNPROC = 1
 
static const int RUNTRIG = 2
 

Protected Attributes

PyObject * globals_
 
PYCODEOBJECTinitcompiled_
 
string initstr_
 
string inputvar_
 
PyObject * locals_
 
int mode_
 
string outputvar_
 
PYCODEOBJECTruncompiled_
 
string runstr_
 

Detailed Description

PyRun allows caling Python functions from moose.

Definition at line 77 of file PyRun.h.

Constructor & Destructor Documentation

PyRun::PyRun ( )

Definition at line 182 of file PyRun.cpp.

References inputvar_, locals_, and value.

182  :mode_(0), initstr_(""), runstr_(""),
183  globals_(0), locals_(0),
185  inputvar_("input_"), outputvar_("output")
186 {
187  locals_ = PyDict_New();
188  if (!locals_){
189  cerr << "Could not initialize locals dict" << endl;
190  return;
191  }
192  PyObject * value = PyFloat_FromDouble(0.0);
193  if (!value && PyErr_Occurred()){
194  PyErr_Print();
195  return;
196  }
197  if (PyDict_SetItemString(locals_, inputvar_.c_str(), value)){
198  PyErr_Print();
199  }
200 }
uint32_t value
Definition: moosemodule.h:42
string initstr_
Definition: PyRun.h:119
PYCODEOBJECT * runcompiled_
Definition: PyRun.h:123
PyObject * globals_
Definition: PyRun.h:121
string runstr_
Definition: PyRun.h:120
PYCODEOBJECT * initcompiled_
Definition: PyRun.h:124
PyObject * locals_
Definition: PyRun.h:122
string inputvar_
Definition: PyRun.h:125
int mode_
Definition: PyRun.h:118
string outputvar_
Definition: PyRun.h:126
PyRun::~PyRun ( )

Definition at line 202 of file PyRun.cpp.

References globals_, and locals_.

203 {
204  Py_XDECREF(globals_);
205  Py_XDECREF(locals_);
206 }
PyObject * globals_
Definition: PyRun.h:121
PyObject * locals_
Definition: PyRun.h:122

Member Function Documentation

PyObject* PyRun::getGlobals ( ) const
string PyRun::getInitString ( ) const

Definition at line 223 of file PyRun.cpp.

References initstr_.

Referenced by initCinfo().

224 {
225  return initstr_;
226 }
string initstr_
Definition: PyRun.h:119

+ Here is the caller graph for this function:

string PyRun::getInputVar ( ) const

Definition at line 234 of file PyRun.cpp.

References inputvar_.

Referenced by initCinfo().

235 {
236  return inputvar_;
237 }
string inputvar_
Definition: PyRun.h:125

+ Here is the caller graph for this function:

PyObject* PyRun::getLocals ( ) const
int PyRun::getMode ( ) const

Definition at line 255 of file PyRun.cpp.

References mode_.

Referenced by initCinfo().

256 {
257  return mode_;
258 }
int mode_
Definition: PyRun.h:118

+ Here is the caller graph for this function:

string PyRun::getOutputVar ( ) const

Definition at line 245 of file PyRun.cpp.

References outputvar_.

Referenced by initCinfo().

246 {
247  return outputvar_;
248 }
string outputvar_
Definition: PyRun.h:126

+ Here is the caller graph for this function:

string PyRun::getRunString ( ) const

Definition at line 213 of file PyRun.cpp.

References runstr_.

Referenced by initCinfo().

214 {
215  return runstr_;
216 }
string runstr_
Definition: PyRun.h:120

+ Here is the caller graph for this function:

const Cinfo * PyRun::initCinfo ( )
static

Definition at line 67 of file PyRun.cpp.

References getInitString(), getInputVar(), getMode(), getOutputVar(), getRunString(), Neutral::initCinfo(), outputOut(), process(), pyRunCinfo, reinit(), run(), setInitString(), setInputVar(), setMode(), setOutputVar(), setRunString(), and trigger().

68 {
69  static ValueFinfo< PyRun, string > runstring(
70  "runString",
71  "String to be executed at each time step.",
74 
75  static ValueFinfo< PyRun, string > initstring(
76  "initString",
77  "String to be executed at initialization (reinit).",
80 
81  static ValueFinfo< PyRun, string > inputvar(
82  "inputVar",
83  "Name of local variable in which input balue is to be stored. Default"
84  " is `input_` (to avoid conflict with Python's builtin function"
85  " `input`).",
88 
89  static ValueFinfo< PyRun, string > outputvar(
90  "outputVar",
91  "Name of local variable for storing output. Default is `output`.",
94 
95  static ValueFinfo< PyRun, int > mode(
96  "mode",
97  "Flag to indicate whether runString should be executed for both trigger and process, or one of them.",
100 
101  // static ValueFinfo< PyRun, PyObject* > globals(
102  // "globals",
103  // "Global environment dict",
104  // &PyRun::setGlobals,
105  // &PyRun::getGlobals);
106 
107  // static ValueFinfo< PyRun, PyObject* > locals(
108  // "locals",
109  // "Local environment dict",
110  // &PyRun::setLocals,
111  // &PyRun::getLocals);
112 
113  static DestFinfo trigger(
114  "trigger",
115  "Executes the current runString whenever a message arrives. It stores"
116  " the incoming value in local variable named"
117  " `input_`, which can be used in the"
118  " `runString` (the underscore is added to avoid conflict with Python's"
119  " builtin function `input`). If debug is True, it prints the input"
120  " value.",
122 
123  static DestFinfo run(
124  "run",
125  "Runs a specified string. Does not modify existing run or init strings.",
127 
128  static DestFinfo process(
129  "process",
130  "Handles process call. Runs the current runString.",
132 
133  static DestFinfo reinit(
134  "reinit",
135  "Handles reinit call. Runs the current initString.",
137 
138  static Finfo * processShared[] = { &process, &reinit };
139  static SharedFinfo proc(
140  "proc",
141  "This is a shared message to receive Process messages "
142  "from the scheduler objects."
143  "The first entry in the shared msg is a MsgDest "
144  "for the Process operation. It has a single argument, "
145  "ProcInfo, which holds lots of information about current "
146  "time, thread, dt and so on. The second entry is a MsgDest "
147  "for the Reinit operation. It also uses ProcInfo. ",
148  processShared, sizeof( processShared ) / sizeof( Finfo* ));
149 
150  static Finfo * pyRunFinfos[] = {
151  &runstring,
152  &initstring,
153  &mode,
154  &inputvar,
155  &outputvar,
156  &trigger,
157  outputOut(),
158  // &locals,
159  // &globals,
160  &run,
161  &proc,
162  };
163 
164  static string doc[] = {
165  "Name", "PyRun",
166  "Author", "Subhasis Ray",
167  "Description", "Runs Python statements from inside MOOSE."};
168  static Dinfo< PyRun > dinfo;
169  static Cinfo pyRunCinfo(
170  "PyRun",
172  pyRunFinfos,
173  sizeof(pyRunFinfos) / sizeof(Finfo*),
174  &dinfo,
175  doc,
176  sizeof(doc) / sizeof(string));
177  return &pyRunCinfo;
178 }
string getOutputVar() const
Definition: PyRun.cpp:245
void run(const Eref &e, string statement)
Definition: PyRun.cpp:295
Definition: Dinfo.h:60
Definition: EpFunc.h:64
void process(const Eref &e, ProcPtr p)
Definition: PyRun.cpp:309
void setInitString(string str)
Definition: PyRun.cpp:218
static const Cinfo * pyRunCinfo
Definition: PyRun.cpp:180
void setMode(int flag)
Definition: PyRun.cpp:250
static SrcFinfo1< double > * outputOut()
Definition: PyRun.cpp:56
int getMode() const
Definition: PyRun.cpp:255
void setOutputVar(string name)
Definition: PyRun.cpp:239
void reinit(const Eref &e, ProcPtr p)
Definition: PyRun.cpp:357
string getRunString() const
Definition: PyRun.cpp:213
string getInputVar() const
Definition: PyRun.cpp:234
void setRunString(string str)
Definition: PyRun.cpp:208
void trigger(const Eref &e, double input)
Definition: PyRun.cpp:260
static const Cinfo * initCinfo()
Definition: Neutral.cpp:16
void setInputVar(string name)
Definition: PyRun.cpp:228
string getInitString() const
Definition: PyRun.cpp:223
Definition: Cinfo.h:18
Definition: Finfo.h:12

+ Here is the call graph for this function:

void PyRun::process ( const Eref e,
ProcPtr  p 
)

Definition at line 309 of file PyRun.cpp.

References globals_, locals_, mode_, output(), outputOut(), outputvar_, runcompiled_, and value.

Referenced by initCinfo().

310 {
311  // PyRun_String(runstr_.c_str(), 0, globals_, locals_);
312  // PyRun_SimpleString(runstr_.c_str());
313  if (!runcompiled_ || mode_ == 2){
314  return;
315  }
316  PyEval_EvalCode(runcompiled_, globals_, locals_);
317  if (PyErr_Occurred()){
318  PyErr_Print ();
319  }
320  PyObject * value = PyDict_GetItemString(locals_, outputvar_.c_str());
321  if (value){
322  double output = PyFloat_AsDouble(value);
323  if (PyErr_Occurred()){
324  PyErr_Print ();
325  } else {
326  outputOut()->send(e, output);
327  }
328  }
329 }
uint32_t value
Definition: moosemodule.h:42
PYCODEOBJECT * runcompiled_
Definition: PyRun.h:123
PyObject * globals_
Definition: PyRun.h:121
static SrcFinfo1< double > * output()
Definition: Arith.cpp:14
static SrcFinfo1< double > * outputOut()
Definition: PyRun.cpp:56
PyObject * locals_
Definition: PyRun.h:122
int mode_
Definition: PyRun.h:118
string outputvar_
Definition: PyRun.h:126

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void PyRun::reinit ( const Eref e,
ProcPtr  p 
)

Definition at line 357 of file PyRun.cpp.

References get_program_name(), globals_, handleError(), initcompiled_, initstr_, locals_, PYCODEOBJECT, runcompiled_, and runstr_.

Referenced by initCinfo().

358 {
359  PyObject * main_module;
360  if (globals_ == NULL){
361  main_module = PyImport_AddModule("__main__");
362  globals_ = PyModule_GetDict(main_module);
363  Py_XINCREF(globals_);
364  }
365  if (locals_ == NULL){
366  locals_ = PyDict_New();
367  if (!locals_){
368  cerr << "Could not initialize locals dict" << endl;
369  }
370  }
371  initcompiled_ = (PYCODEOBJECT*)Py_CompileString(
372  initstr_.c_str(),
373  get_program_name().c_str(),
374  Py_file_input);
375  if (!initcompiled_){
376  cerr << "Error compiling initString" << endl;
377  handleError(true);
378  } else {
379  PyEval_EvalCode(initcompiled_, globals_, locals_);
380  if (PyErr_Occurred()){
381  PyErr_Print ();
382  }
383  }
384  runcompiled_ = (PYCODEOBJECT*)Py_CompileString(
385  runstr_.c_str(),
386  get_program_name().c_str(),
387  Py_file_input);
388  if (!runcompiled_){
389  cerr << "Error compiling runString" << endl;
390  handleError(true);
391  } else {
392  PyEval_EvalCode(runcompiled_, globals_, locals_);
393  if (PyErr_Occurred()){
394  PyErr_Print ();
395  }
396  }
397 }
string initstr_
Definition: PyRun.h:119
PYCODEOBJECT * runcompiled_
Definition: PyRun.h:123
PyObject * globals_
Definition: PyRun.h:121
string runstr_
Definition: PyRun.h:120
string get_program_name()
Definition: PyRun.h:67
PYCODEOBJECT * initcompiled_
Definition: PyRun.h:124
PyObject * locals_
Definition: PyRun.h:122
void handleError(bool syntax)
Definition: PyRun.cpp:335
#define PYCODEOBJECT
Definition: PyRun.h:65

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void PyRun::run ( const Eref e,
string  statement 
)

Definition at line 295 of file PyRun.cpp.

References locals_, output(), outputOut(), outputvar_, and value.

Referenced by initCinfo().

296 {
297  PyRun_SimpleString(statement.c_str());
298  PyObject * value = PyDict_GetItemString(locals_, outputvar_.c_str());
299  if (value){
300  double output = PyFloat_AsDouble(value);
301  if (PyErr_Occurred()){
302  PyErr_Print ();
303  } else {
304  outputOut()->send(e, output);
305  }
306  }
307 }
uint32_t value
Definition: moosemodule.h:42
static SrcFinfo1< double > * output()
Definition: Arith.cpp:14
static SrcFinfo1< double > * outputOut()
Definition: PyRun.cpp:56
PyObject * locals_
Definition: PyRun.h:122
string outputvar_
Definition: PyRun.h:126

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void PyRun::setGlobals ( PyObject *  globals)
void PyRun::setInitString ( string  str)

Definition at line 218 of file PyRun.cpp.

References initstr_.

Referenced by initCinfo().

219 {
220  initstr_ = statement;
221 }
string initstr_
Definition: PyRun.h:119

+ Here is the caller graph for this function:

void PyRun::setInputVar ( string  name)

Definition at line 228 of file PyRun.cpp.

References inputvar_, locals_, and name.

Referenced by initCinfo().

229 {
230  PyDict_DelItemString(locals_, inputvar_.c_str());
231  inputvar_ = name;
232 }
PyObject * locals_
Definition: PyRun.h:122
static char name[]
Definition: mfield.cpp:401
string inputvar_
Definition: PyRun.h:125

+ Here is the caller graph for this function:

void PyRun::setLocals ( PyObject *  locals)
void PyRun::setMode ( int  flag)

Definition at line 250 of file PyRun.cpp.

References mode_.

Referenced by initCinfo().

251 {
252  mode_ = flag;
253 }
int mode_
Definition: PyRun.h:118

+ Here is the caller graph for this function:

void PyRun::setOutputVar ( string  name)

Definition at line 239 of file PyRun.cpp.

References locals_, name, and outputvar_.

Referenced by initCinfo().

240 {
241  PyDict_DelItemString(locals_, outputvar_.c_str());
242  outputvar_ = name;
243 }
PyObject * locals_
Definition: PyRun.h:122
static char name[]
Definition: mfield.cpp:401
string outputvar_
Definition: PyRun.h:126

+ Here is the caller graph for this function:

void PyRun::setRunString ( string  str)

Definition at line 208 of file PyRun.cpp.

References runstr_.

Referenced by initCinfo().

209 {
210  runstr_ = statement;
211 }
string runstr_
Definition: PyRun.h:120

+ Here is the caller graph for this function:

void PyRun::trigger ( const Eref e,
double  input 
)

Definition at line 260 of file PyRun.cpp.

References globals_, inputvar_, locals_, mode_, output(), outputOut(), outputvar_, runcompiled_, and value.

Referenced by initCinfo().

261 {
262  if (!runcompiled_){
263  return;
264  }
265  if (mode_ == 1){
266  return;
267  }
268 
269  PyObject * value = PyDict_GetItemString(locals_, inputvar_.c_str());
270  if (value){
271  Py_DECREF(value);
272  }
273  value = PyFloat_FromDouble(input);
274  if (!value && PyErr_Occurred()){
275  PyErr_Print();
276  }
277  if (PyDict_SetItemString(locals_, inputvar_.c_str(), value)){
278  PyErr_Print();
279  }
280  PyEval_EvalCode(runcompiled_, globals_, locals_);
281  if (PyErr_Occurred()){
282  PyErr_Print ();
283  }
284  value = PyDict_GetItemString(locals_, outputvar_.c_str());
285  if (value){
286  double output = PyFloat_AsDouble(value);
287  if (PyErr_Occurred()){
288  PyErr_Print ();
289  } else {
290  outputOut()->send(e, output);
291  }
292  }
293 }
uint32_t value
Definition: moosemodule.h:42
PYCODEOBJECT * runcompiled_
Definition: PyRun.h:123
PyObject * globals_
Definition: PyRun.h:121
static SrcFinfo1< double > * output()
Definition: Arith.cpp:14
static SrcFinfo1< double > * outputOut()
Definition: PyRun.cpp:56
PyObject * locals_
Definition: PyRun.h:122
string inputvar_
Definition: PyRun.h:125
int mode_
Definition: PyRun.h:118
string outputvar_
Definition: PyRun.h:126

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

PyObject* PyRun::globals_
protected

Definition at line 121 of file PyRun.h.

Referenced by process(), reinit(), trigger(), and ~PyRun().

PYCODEOBJECT* PyRun::initcompiled_
protected

Definition at line 124 of file PyRun.h.

Referenced by reinit().

string PyRun::initstr_
protected

Definition at line 119 of file PyRun.h.

Referenced by getInitString(), reinit(), and setInitString().

string PyRun::inputvar_
protected

Definition at line 125 of file PyRun.h.

Referenced by getInputVar(), PyRun(), setInputVar(), and trigger().

PyObject* PyRun::locals_
protected

Definition at line 122 of file PyRun.h.

Referenced by process(), PyRun(), reinit(), run(), setInputVar(), setOutputVar(), trigger(), and ~PyRun().

int PyRun::mode_
protected

Definition at line 118 of file PyRun.h.

Referenced by getMode(), process(), setMode(), and trigger().

string PyRun::outputvar_
protected

Definition at line 126 of file PyRun.h.

Referenced by getOutputVar(), process(), run(), setOutputVar(), and trigger().

const int PyRun::RUNBOTH = 0
static

Definition at line 82 of file PyRun.h.

PYCODEOBJECT* PyRun::runcompiled_
protected

Definition at line 123 of file PyRun.h.

Referenced by process(), reinit(), and trigger().

const int PyRun::RUNPROC = 1
static

Definition at line 80 of file PyRun.h.

string PyRun::runstr_
protected

Definition at line 120 of file PyRun.h.

Referenced by getRunString(), reinit(), and setRunString().

const int PyRun::RUNTRIG = 2
static

Definition at line 81 of file PyRun.h.


The documentation for this class was generated from the following files: