Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python.announce > #2967

forcallpy: embed Python, call Python scripts within Fortran programs

From Laurent Pointal <laurent.pointal@free.fr>
Newsgroups comp.lang.python.announce
Subject forcallpy: embed Python, call Python scripts within Fortran programs
Date 2018-09-01 14:43 +0000
Organization Guest of ProXad - France
Message-ID <mailman.47.1536132467.2753.python-announce-list@python.org> (permalink)
References <5b8aa582$0$14316$426a74cc@news.free.fr>

Show all headers | View raw


Hello,

Here is an announce for people with main Fortran programs who need to 
call Python scripts and do not want to start another process each time. 
Forcallpy embeds the Python interpreter in your Fortran program and makes 
scalars or one dimensional arrays of numbers easy to transmit between the 
two languages. It automatically install numpy in the Python side (used by 
this library), and support Python multithreading.

Project git repository and bug tracking:

    https://sourcesup.renater.fr/projects/forcallpy/

Documentation:

    https://forcallpy.readthedocs.io/en/latest/

For an example, here is the Fortran demo program, without comments (more 
details in documentation about values transmission between Python and 
Fortran):

PROGRAM forcallpy_demo
  USE forcallpy
  USE, INTRINSIC :: iso_c_binding, ONLY: c_ptr, c_null_ptr, c_loc
 
  IMPLICIT NONE
  INTEGER :: coderr
  INTEGER :: intres
  INTEGER,DIMENSION(10),TARGET :: tabint;
  DOUBLE PRECISION,DIMENSION(5),TARGET :: tabdbl;
  TYPE(c_ptr),DIMENSION(3) :: tabptr;
  DOUBLE PRECISION,DIMENSION(4),TARGET :: tabres;
 
  tabint(1:10) = (/ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12  /)
  tabdbl(1:5) = (/ 10.1, 10.3, 10.5, 10.7, 10.9 /)
  tabptr(1:3) = (/ c_null_ptr, c_null_ptr , c_null_ptr  /)
  tabres(1:4) = (/ 0.0, 0.0, 0.0, 0.0 /)
  tabptr(2) = c_loc(tabdbl)
 
  CALL pyinit(register_sighandlers=1, print_errors=1, verbosity=0, &
	 coderr=coderr)

  CALL pyrun_i4r8('print("result =",3 + 4 *  x)', x=3.6D0)
  CALL pyrun_i4r8('for i in range(3): print("This is a Python loop", i, 
"directly in fortran source.")')
  CALL pyrun_i4r8('import math')
  CALL pyrun_i4r8('print("Pi value is", math.pi)')
  CALL pyrun_i4r8('import sys'//NEW_LINE('a')//'sys.path.insert(0, ".")')
  CALL pyrun_i4r8('import forcallpy_demomodule')
  CALL pyrun_i4r8('print("Doc forcallpy:", forcallpy.__doc__)')
  CALL pyrun_i4r8('print("Namespace forcallpy:", dir(forcallpy)')
  intres = pyfct_i4r8_i4(&
	"int(forcallpy_demomodule.a_function(a,b,c,x,av,zv,yw))", &
	a=2,b=-4,c=7,x=3.5D+0,av=tabint,zv=tabdbl,yw=tabres)
  WRITE(*,*) "Result =", intres
  WRITE(*,*) "Inout array after:", tabres
  CALL pysub_i4r8("forcallpy_demomodule.a_subroutine(av,zv,p)", &
                                    av=tabint,zv=tabdbl,p=tabptr)     
  WRITE(*,*) "Computing a zero division in Python"
  CALL pysub_i4r8("print(1/0)")
  CALL pyrun_i4r8('forcallpy_demomodule.test_threading()')
  call sleep(10)
  CALL pyterm() 
END PROGRAM forcallpy_demo

--

L.Pointal <laurent.pointal@limsi.fr>
CNRS / LIMSI

Back to comp.lang.python.announce | Previous | Next | Find similar | Unroll thread


Thread

forcallpy: embed Python, call Python scripts within Fortran programs Laurent Pointal <laurent.pointal@free.fr> - 2018-09-01 14:43 +0000

csiph-web