Skip to content
Snippets Groups Projects
Select Git revision
  • 9d8ae61a5a1004414cc87fa06a62a6adb80fe9a8
  • master default protected
  • ujwal_21_08_2024
  • dev_mm_pf
  • cyrielle
  • vinayak
  • dev_mm_torchSCRU
  • debug_mm_pf
  • newStructureNonLocal
  • Mohamed_stochasticDMN
  • dev_mm_bench
  • stochdmn
  • revert-351ff7aa
  • ujwal_29April2024
  • dev_mm_ann
  • mohamed_vevp
  • ujwal_debug
  • ujwal_2ndApril2024
  • ujwal_October_2023
  • gabriel
  • SFEM
  • v4.0
  • v3.2.3_multiplePhase
  • v3.5
  • v3.3.2
  • v3.4
  • v3.3
  • ver3.2
  • verJulienWork
  • ver3.1
  • ver2
  • ver1.1.2
  • ver1.1.1
  • ver1.1
34 results

mlawNonLocalPorousCoupled.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