Skip to content

Commit 8f33a3a

Browse files
committed
add missing files
1 parent c276684 commit 8f33a3a

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module cop_python_interface
2+
3+
!-----------------------------------------------------------------------------
4+
! Void phase for Python interaction
5+
!-----------------------------------------------------------------------------
6+
7+
use ESMF, only: ESMF_LogWrite, ESMF_LOGMSG_INFO
8+
use, intrinsic :: iso_c_binding, only : C_PTR, C_CHAR
9+
10+
implicit none
11+
12+
!-----------------------------------------------------------------------------
13+
! Public module interface
14+
!-----------------------------------------------------------------------------
15+
16+
interface
17+
subroutine conduit_fort_to_py(cnode, py_script) bind(C, name="conduit_fort_to_py")
18+
use iso_c_binding
19+
implicit none
20+
type(C_PTR), value, intent(in) :: cnode
21+
character(kind=C_CHAR), intent(in) :: py_script(*)
22+
end subroutine conduit_fort_to_py
23+
24+
function c_conduit_fort_from_py(name) result(res) bind(C, name="conduit_fort_from_py")
25+
use iso_c_binding
26+
implicit none
27+
character(kind=C_CHAR), intent(in) :: name(*)
28+
type(C_PTR) :: res
29+
end function c_conduit_fort_from_py
30+
end interface
31+
32+
!-----------------------------------------------------------------------------
33+
! Private module data
34+
!-----------------------------------------------------------------------------
35+
36+
character(len=*), parameter :: modName = "(cop_python_interface)"
37+
character(len=*), parameter :: u_FILE_u = __FILE__
38+
39+
!===============================================================================
40+
contains
41+
!===============================================================================
42+
43+
function conduit_fort_from_py(name) result(res)
44+
use iso_c_binding
45+
implicit none
46+
47+
! input/output variables
48+
character(*), intent(in) :: name
49+
type(C_PTR) :: res
50+
51+
! local variables
52+
character(len=*), parameter :: subname = trim(modName)//':(conduit_fort_from_py) '
53+
!---------------------------------------------------------------------------
54+
55+
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
56+
57+
res = c_conduit_fort_from_py(trim(name) // C_NULL_CHAR)
58+
59+
call ESMF_LogWrite(subname//' done', ESMF_LOGMSG_INFO)
60+
61+
end function conduit_fort_from_py
62+
63+
end module cop_python_interface
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <conduit.hpp>
2+
#include <conduit_cpp_to_c.hpp>
3+
// conduit python module capi header
4+
#include "conduit_python.hpp"
5+
// embedded interp
6+
#include "python_interpreter.hpp"
7+
8+
// single python interp instance for our example.
9+
PythonInterpreter *interp = NULL;
10+
11+
extern "C" {
12+
13+
//----------------------------------------------------------------------------
14+
// returns our static instance of our python interpreter
15+
// if not already inited initializes it
16+
//----------------------------------------------------------------------------
17+
18+
PythonInterpreter *init_python_interpreter()
19+
{
20+
if( interp == NULL)
21+
{
22+
interp = new PythonInterpreter();
23+
if( !interp->initialize() )
24+
{
25+
std::cout << "ERROR: interp->initialize() failed " << std::endl;
26+
return NULL;
27+
}
28+
// setup for conduit python c api
29+
if(!interp->run_script("import conduit"))
30+
{
31+
std::cout << "ERROR: `import conduit` failed" << std::endl;
32+
return NULL;
33+
}
34+
35+
if(import_conduit() < 0)
36+
{
37+
std::cout << "failed to import Conduit Python C-API";
38+
return NULL;
39+
}
40+
41+
// Turn this on if you want to see every line
42+
// the python interpreter executes
43+
//interp->set_echo(true);
44+
}
45+
return interp;
46+
}
47+
48+
//----------------------------------------------------------------------------
49+
// access node passed from fortran to python
50+
//----------------------------------------------------------------------------
51+
52+
void conduit_fort_to_py(conduit_node *data, const char *py_script) {
53+
// create python interpreter
54+
PythonInterpreter *pyintp = init_python_interpreter();
55+
56+
// add extra system paths
57+
// pyintp->add_system_path("/usr/local/lib/python3.9/site-packages");
58+
59+
// show code
60+
// pyintp->set_echo(true);
61+
62+
// get global dict and insert wrapped conduit node
63+
PyObject *py_mod_dict = pyintp->global_dict();
64+
65+
// get cpp ref to passed node
66+
conduit::Node &n = conduit::cpp_node_ref(data);
67+
68+
// create py object to wrap the conduit node
69+
PyObject *py_node = PyConduit_Node_Python_Wrap(&n, 0); // python owns => false
70+
71+
// my_node is set in here statically, it will be used to access node under python
72+
pyintp->set_dict_object(py_mod_dict, py_node, "my_node");
73+
74+
// trigger script
75+
bool err = pyintp->run_script_file(py_script, py_mod_dict);
76+
77+
// check error
78+
if (err) {
79+
pyintp->check_error();
80+
}
81+
82+
}
83+
84+
}

0 commit comments

Comments
 (0)