gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
FieldPython.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef FIELD_PYTHON_H
7 #define FIELD_PYTHON_H
8 
9 #include "Field.h"
10 #include "Python.h"
11 
12 class FieldPython : public Field {
13  PyObject *_callback;
14 
15 public:
16  const char *getName() { return "Python"; }
17 
18  std::string getDescription() { return "simple call to a python function"; }
19 
20  FieldPython(PyObject *cb, PyObject *arg = NULL)
21  {
22  _callback = cb;
23  Py_INCREF(_callback);
24  }
25 
26  ~FieldPython() { Py_DECREF(_callback); }
27 
28  double operator()(double x, double y, double z, GEntity *ge = 0)
29  {
30  PyObject *pyge = SWIG_NewPointerObj((void *)ge, SWIGTYPE_p_GEntity, 0);
31  PyObject *args = Py_BuildValue("(dddO)", x, y, z, pyge);
32  PyObject *result = PyObject_CallObject(_callback, args);
33  Py_DECREF(args);
34  if(result) {
35  double r = PyFloat_AsDouble(result);
36  if(PyErr_Occurred()) {
37  PyErr_Print();
38  PyErr_Clear();
39  Msg::Error("Result of python function of field %i cannot be "
40  "interpreted as a float.",
41  id);
42  r = MAX_LC;
43  }
44  Py_DECREF(result);
45  return r;
46  }
47  else {
48  if(PyErr_Occurred()) {
49  PyErr_Print();
50  PyErr_Clear();
51  }
52  Msg::Error(
53  "An error occurs while evaluating python function of field %i.", id);
54  return MAX_LC;
55  }
56  }
57 };
58 
59 #endif
Field.h
FieldPython::~FieldPython
~FieldPython()
Definition: FieldPython.h:26
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
FieldPython::getDescription
std::string getDescription()
Definition: FieldPython.h:18
MAX_LC
#define MAX_LC
Definition: GEntity.h:19
GEntity
Definition: GEntity.h:31
FieldPython::getName
const char * getName()
Definition: FieldPython.h:16
FieldPython::_callback
PyObject * _callback
Definition: FieldPython.h:13
Field
Definition: Field.h:103
FieldPython
Definition: FieldPython.h:12
FieldPython::FieldPython
FieldPython(PyObject *cb, PyObject *arg=NULL)
Definition: FieldPython.h:20
z
const double z
Definition: GaussQuadratureQuad.cpp:56
FieldPython::operator()
double operator()(double x, double y, double z, GEntity *ge=0)
Definition: FieldPython.h:28