Hello,

In addition to the above method, in which i have used the Python/C API 
<https://docs.python.org/3/c-api/index.html> for calling Python function 
from C++, I have also tried to use pybind11 
<https://pybind11.readthedocs.io/en/stable/index.html>. I have followed the 
procedure given here 
<https://pybind11.readthedocs.io/en/stable/advanced/embedding.html#importing-modules>.
 
Attached herewith is the minimum reproducible example for embedding Python 
in c++ using pybind11.

Again, as in the trailing post, if the dealii object is commented out (line 
26 in the main.cpp file attached herewith) then I am able to call the 
Python function from C++. Otherwise, it gives the segmentation fault error. 
I am not sure what is creating the issue here.

Can someone help me with a solution or possible way forward?

Thanks
Vinayak
On Wednesday, November 22, 2023 at 7:29:38 PM UTC+1 Vinayak Vijay wrote:

> Hello,
>
> I am trying to build an application with dealii in which i need to 
> interface with a Python file. However, i am facing a segmentation fault 
> when the following condition is true: 
>
> *Condition*: the Python file imports numpy *AND* a dealii object (say a 
> Tensor<1,3>) is called. 
>
> I don't face such an issue if either of the conditions is altered. 
>
> I am also not facing such an issue when I have another library's (say 
> Eigen) object called.
>
> The segmentation fault appears only when "Condition" is true. I have tried 
> to create a reproducible example which has the following files:
>
> 1. cpp_to_python.h - Declares the function python_interface_function() in 
> class cpp_to_python
> 2. cpp_to_python.cpp - Defines the function python_interface_function() in 
> class cpp_to_python
> 3. main.cpp - creates an object of type  cpp_to_python and calls the 
> function python_interface_function()
> 4. python_test.py - a simple python file that is called inside 
> python_interface_function() to test the interface between cpp and python
> 5. CMakeLists.txt - contains all the packages that are needed for this 
> example
>
> *Note regarding the reproducible example:* 
>
> 1. When one runs the executable, one will get the segmentation fault 
> error. However, there will be no error if one comments out the line in 
> which dealii object is called i.e. line 19 in main.cpp file -* this shows 
> that the issue is somehow with dealii and python coupling*
>
> 2. Place the python_test.py in the build folder for it to be recognized.
>
> Python version - 3.8
> dealii version - 9.4
>
> Can someone please help me with this issue? Let me know if there's any 
> issue with running the reproducible example. 
>
>

-- 
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see 
https://groups.google.com/d/forum/dealii?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/dealii/76d6315b-9102-4e74-a14a-9641fc776100n%40googlegroups.com.
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

#include <stdio.h>
#include <iostream>
#include <stdlib.h>

#include <deal.II/base/tensor.h>

#include <Eigen/Core>
#include <Python.h>
#include "numpy/arrayobject.h"


#include <pybind11/pybind11.h>
#include <pybind11/embed.h>  // python interpreter
#include <pybind11/stl.h>  // type conversion

namespace py = pybind11;
using namespace dealii;
using namespace Eigen;

int main()
{
    // Try to print object from dealii - this results in segmentation fault
    Tensor<1, 3, double> v1;
    std::cout << v1 << std::endl;

    std::cout << "Starting pybind" << std::endl;
    py::scoped_interpreter guard{}; // start interpreter, dies when out of scope
    py::print("Hello, World!"); // use the Python API
    py::module_ calc = py::module_::import("python_test");
    py::object result = calc.attr("hello_python")(0);
    int n = result.cast<int>();
    std::cout << n << std::endl;

    
}  
# Set the name of the project and target:
# Local header files here ONLY
# Declare all source files the target consists of. Here, this is only
# the one step-X.cc file, but as you expand your project you may wish
# to add other source files as well. If your project becomes much larger,
# you may want to either replace the following statement by something like
  #  FILE(GLOB_RECURSE TARGET_SRC  "../src/*.cpp")
  #  FILE(GLOB_RECURSE TARGET_INC  "../include/*.h")
  #  SET(TARGET_SRC ${TARGET_SRC}  ${TARGET_INC})
# or switch altogether to the large project CMakeLists.txt file discussed
# in the "CMake in user projects" page accessible from the "User info"
# page of the documentation.


# Set the name of the project and target:
set(TARGET "main")

# Declare all source files the target consists of. Here, this is only
# the one step-X.cc file, but as you expand your project you may wish
# to add other source files as well. If your project becomes much larger,
# you may want to either replace the following statement by something like
#    file(GLOB_RECURSE TARGET_SRC  "source/*.cc")
#    file(GLOB_RECURSE TARGET_INC  "include/*.h")
#    set(TARGET_SRC ${TARGET_SRC}  ${TARGET_INC})
# or switch altogether to the large project CMakeLists.txt file discussed
# in the "CMake in user projects" page accessible from the "User info"
# page of the documentation.
set(TARGET_SRC
  ${TARGET}.cpp
  )


# Usually, you will not need to modify anything beyond this point...

CMAKE_MINIMUM_REQUIRED(VERSION 3.3.0)

FIND_PACKAGE(deal.II 9.4.0
  HINTS ${deal.II_DIR} ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR}
  )
IF(NOT ${deal.II_FOUND})
  MESSAGE(FATAL_ERROR "\n"
    "*** Could not locate a (sufficiently recent) version of deal.II. ***\n\n"
    "You may want to either pass a flag -DDEAL_II_DIR=/path/to/deal.II to 
cmake\n"
    "or set an environment variable \"DEAL_II_DIR\" that contains this path."
    )
ENDIF()

DEAL_II_INITIALIZE_CACHED_VARIABLES()
PROJECT(${TARGET} CXX)

find_package(Python COMPONENTS Interpreter Development NumPy REQUIRED)
find_package (Eigen3 3.3 REQUIRED)
find_package(pybind11 CONFIG REQUIRED)

# include_directories ( ${PYTHON_INCLUDE_DIRS} )
include_directories (${PYTHON_INCLUDE_DIRS})
include_directories(${EIGEN3_INCLUDE_DIR})
include_directories(${pybind11_INCLUDE_DIR})

# target_link_libraries(hello_ext PRIVATE Python::Python)

deal_ii_initialize_cached_variables()
project(${TARGET})
deal_ii_invoke_autopilot()
target_link_libraries(${TARGET} Python::Python)
target_link_libraries(${TARGET} Python::NumPy)
target_link_libraries(${TARGET} pybind11::headers)
import math
import numpy as np
from scipy.optimize import minimize, rosen, rosen_der


def hello_python(x):
    print("hello x in python  ", x)
    return 0


def min_rosen(x0):
    res = minimize(rosen, x0)
    return res

Reply via email to