Skip to content
Snippets Groups Projects
Select Git revision
  • 82ef2ab118fb009c015bd2404bcb003f25d6d6d1
  • master default protected
  • overlaps_tags_and_distributed_export
  • overlaps_tags_and_distributed_export_rebased
  • relaying
  • alphashapes
  • patches-4.14
  • steplayer
  • bl
  • pluginMeshQuality
  • fixBugsAmaury
  • hierarchical-basis
  • new_export_boris
  • oras_vs_osm
  • reassign_partitions
  • distributed_fwi
  • rename-classes
  • fix/fortran-api-example-t4
  • robust_partitions
  • reducing_files
  • fix_overlaps
  • gmsh_4_14_0
  • gmsh_4_13_1
  • gmsh_4_13_0
  • gmsh_4_12_2
  • gmsh_4_12_1
  • gmsh_4_12_0
  • gmsh_4_11_1
  • gmsh_4_11_0
  • gmsh_4_10_5
  • gmsh_4_10_4
  • gmsh_4_10_3
  • gmsh_4_10_2
  • gmsh_4_10_1
  • gmsh_4_10_0
  • gmsh_4_9_5
  • gmsh_4_9_4
  • gmsh_4_9_3
  • gmsh_4_9_2
  • gmsh_4_9_1
  • gmsh_4_9_0
41 results

viewlist.cpp

Blame
  • FieldPython.h 1.46 KiB
    // Gmsh - Copyright (C) 1997-2019 C. Geuzaine, J.-F. Remacle
    //
    // See the LICENSE.txt file for license information. Please report all
    // issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
    
    #ifndef FIELD_PYTHON_H
    #define FIELD_PYTHON_H
    
    #include "Field.h"
    #include "Python.h"
    
    class FieldPython : public Field {
      PyObject *_callback;
    
    public:
      const char *getName() { return "Python"; }
    
      std::string getDescription() { return "simple call to a python function"; }
    
      FieldPython(PyObject *cb, PyObject *arg = NULL)
      {
        _callback = cb;
        Py_INCREF(_callback);
      }
    
      ~FieldPython() { Py_DECREF(_callback); }
    
      double operator()(double x, double y, double z, GEntity *ge = 0)
      {
        PyObject *pyge = SWIG_NewPointerObj((void *)ge, SWIGTYPE_p_GEntity, 0);
        PyObject *args = Py_BuildValue("(dddO)", x, y, z, pyge);
        PyObject *result = PyEval_CallObject(_callback, args);
        Py_DECREF(args);
        if(result) {
          double r = PyFloat_AsDouble(result);
          if(PyErr_Occurred()) {
            PyErr_Print();
            PyErr_Clear();
            Msg::Error("Result of python function of field %i cannot be "
                       "interpreted as a float.",
                       id);
            r = MAX_LC;
          }
          Py_DECREF(result);
          return r;
        }
        else {
          if(PyErr_Occurred()) {
            PyErr_Print();
            PyErr_Clear();
          }
          Msg::Error(
            "An error occurs while evaluating python function of field %i.", id);
          return MAX_LC;
        }
      }
    };
    
    #endif