MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PyRun.h
Go to the documentation of this file.
1 // PyRun.h ---
2 //
3 // Filename: PyRun.h
4 // Description:
5 // Author: subha
6 // Maintainer:
7 // Created: Sat Oct 11 14:40:45 2014 (+0530)
8 // Version:
9 // Last-Updated: Fri Jun 19 18:54:49 2015 (-0400)
10 // By: Subhasis Ray
11 // Update #: 31
12 // URL:
13 // Keywords:
14 // Compatibility:
15 //
16 //
17 
18 // Commentary:
19 //
20 // Class to call Python functions from MOOSE
21 //
22 //
23 
24 // Change log:
25 //
26 //
27 //
28 //
29 // This program is free software; you can redistribute it and/or
30 // modify it under the terms of the GNU General Public License as
31 // published by the Free Software Foundation; either version 3, or
32 // (at your option) any later version.
33 //
34 // This program is distributed in the hope that it will be useful,
35 // but WITHOUT ANY WARRANTY; without even the implied warranty of
36 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 // General Public License for more details.
38 //
39 // You should have received a copy of the GNU General Public License
40 // along with this program; see the file COPYING. If not, write to
41 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth
42 // Floor, Boston, MA 02110-1301, USA.
43 //
44 //
45 
46 // Code:
47 
48 #ifndef _PYCALL_H
49 #define _PYCALL_H
50 
51 #include <climits>
52 
53 #if PY_MAJOR_VERSION >= 3
54 #define PYCODEOBJECT PyObject
55 
56 string get_program_name()
57 {
58  wchar_t * progname = Py_GetProgramName();
59  char buffer[PATH_MAX+1];
60  size_t ret = wcstombs(buffer, progname, PATH_MAX);
61  buffer[ret] = '\0';
62  return string(buffer);
63 }
64 #else
65 #define PYCODEOBJECT PyCodeObject
66 
68 {
69  char * progname = Py_GetProgramName();
70  return string(progname);
71 }
72 #endif
73 
77 class PyRun
78 {
79 public:
80  static const int RUNPROC; // only process call
81  static const int RUNTRIG; // only trigger call
82  static const int RUNBOTH; // both
83 
84  PyRun();
85  ~PyRun();
86 
87  void setInitString(string str);
88  string getInitString() const;
89 
90  void setRunString(string str);
91  string getRunString() const;
92 
93  void setGlobals(PyObject *globals);
94  PyObject * getGlobals() const;
95 
96  void setLocals(PyObject *locals);
97  PyObject * getLocals() const;
98 
99  void setMode(int flag);
100  int getMode() const;
101 
102  void setInputVar(string name);
103  string getInputVar() const;
104 
105  void setOutputVar(string name);
106  string getOutputVar() const;
107 
108  void run(const Eref& e, string statement);
109 
110  void trigger(const Eref& e, double input); // this is a way to trigger execution via incoming message - can be useful for debugging
111 
112  void process(const Eref& e, ProcPtr p);
113  void reinit(const Eref& e, ProcPtr p);
114 
115  static const Cinfo * initCinfo();
116 
117 protected:
118  int mode_; // flag to decide when to run the Python string
119  string initstr_; // statement str for running at reinit
120  string runstr_; // statement str for running in each process call
121  PyObject * globals_; // global env dict
122  PyObject * locals_; // local env dict
123  PYCODEOBJECT * runcompiled_; // compiled form of procstr_
124  PYCODEOBJECT * initcompiled_; // coimpiled form of initstr_
125  string inputvar_; // identifier for input variable.
126  string outputvar_; // identifier for output variable
127 };
128 
129 #endif
130 
131 
132 
133 //
134 // PyRun.h ends here
PyObject * getLocals() const
string initstr_
Definition: PyRun.h:119
string getOutputVar() const
Definition: PyRun.cpp:245
PYCODEOBJECT * runcompiled_
Definition: PyRun.h:123
void run(const Eref &e, string statement)
Definition: PyRun.cpp:295
PyObject * globals_
Definition: PyRun.h:121
void process(const Eref &e, ProcPtr p)
Definition: PyRun.cpp:309
void setInitString(string str)
Definition: PyRun.cpp:218
Definition: PyRun.h:77
static const int RUNPROC
Definition: PyRun.h:80
string runstr_
Definition: PyRun.h:120
void setMode(int flag)
Definition: PyRun.cpp:250
PyRun()
Definition: PyRun.cpp:182
string get_program_name()
Definition: PyRun.h:67
PYCODEOBJECT * initcompiled_
Definition: PyRun.h:124
int getMode() const
Definition: PyRun.cpp:255
Definition: Eref.h:26
void setOutputVar(string name)
Definition: PyRun.cpp:239
void setGlobals(PyObject *globals)
~PyRun()
Definition: PyRun.cpp:202
void reinit(const Eref &e, ProcPtr p)
Definition: PyRun.cpp:357
PyObject * locals_
Definition: PyRun.h:122
string getRunString() const
Definition: PyRun.cpp:213
static const Cinfo * initCinfo()
Definition: PyRun.cpp:67
static char name[]
Definition: mfield.cpp:401
string getInputVar() const
Definition: PyRun.cpp:234
void setRunString(string str)
Definition: PyRun.cpp:208
static const int RUNTRIG
Definition: PyRun.h:81
void trigger(const Eref &e, double input)
Definition: PyRun.cpp:260
string inputvar_
Definition: PyRun.h:125
PyObject * getGlobals() const
#define PYCODEOBJECT
Definition: PyRun.h:65
void setInputVar(string name)
Definition: PyRun.cpp:228
int mode_
Definition: PyRun.h:118
string getInitString() const
Definition: PyRun.cpp:223
Definition: Cinfo.h:18
string outputvar_
Definition: PyRun.h:126
void setLocals(PyObject *locals)
static const int RUNBOTH
Definition: PyRun.h:82