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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Laurent Pointal <laurent.pointal@free.fr>
Newsgroups comp.lang.python.announce
Subject forcallpy: embed Python, call Python scripts within Fortran programs
Date 01 Sep 2018 14:43:14 GMT
Organization Guest of ProXad - France
Lines 69
Approved python-announce-list@python.org
Message-ID <mailman.47.1536132467.2753.python-announce-list@python.org> (permalink)
References <5b8aa582$0$14316$426a74cc@news.free.fr>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de ROwEuKqRJr4g0qaYF95wJQ3LKju6TFuCDLPugJSMGwlQ==
Return-Path <root@news.free.fr>
X-Original-To python-announce@python.org
Delivered-To python-announce-list@mail.python.org
X-Spam-Status OK 0.026
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'git': 0.05; 'repository': 0.05; 'scripts': 0.08; 'url-ip:137/8': 0.09; 'interpreter': 0.12; '(more': 0.16; '0.0,': 0.16; 'intrinsic': 0.16; 'is",': 0.16; 'library),': 0.16; 'nntp-host:78/8': 0.16; 'numpy': 0.16; 'received:212.27': 0.16; 'received:212.27.42': 0.16; 'received:free.fr': 0.16; 'subject:programs': 0.16; 'python': 0.16; 'bug': 0.16; 'subject:Python': 0.17; 'header:X-Complaints- To:1': 0.18; 'install': 0.20; 'languages.': 0.21; 'python"': 0.21; 'values': 0.24; 'header:User-Agent:1': 0.24; 'arrays': 0.25; 'division': 0.25; 'implicit': 0.25; 'announce': 0.27; 'program,': 0.29; 'skip:a 50': 0.31; 'url:io': 0.31; 'skip:" 50': 0.32; '(used': 0.33; 'url:readthedocs': 0.33; 'array': 0.34; 'another': 0.36; 'url:en': 0.37; '11,': 0.37; 'integer': 0.38; 'received:fr': 0.39; 'comments': 0.39; 'two': 0.39; 'end': 0.40; 'your': 0.60; 'received:edu': 0.61; 'subject:, ': 0.62; 'received:mit.edu': 0.62; 'received:mod-relay.xvm.mit.edu': 0.62; 'received:xvm.mit.edu': 0.62; 'url:projects': 0.62; 'easy': 0.62; 'programs': 0.63; 'here': 0.64; 'details': 0.64; 'call': 0.64; 'to:addr:comp-lang-python-announce': 0.70; 'to:addr:moderators.isc.org': 0.78; 'dimensional': 0.84; 'only:': 0.84; 'transmit': 0.84; 'subject:call': 0.93; 'url:fr': 0.95
Delivered-To comp-lang-python-announce@moderators.isc.org
User-Agent Pan/0.144 (Time is the enemy; 28ab3ba git.gnome.org/pan2)
X-Mailman-Approved-At Wed, 05 Sep 2018 03:27:45 -0400
X-BeenThere python-announce-list@python.org
X-Mailman-Version 2.1.29
Precedence list
List-Id Announcement-only list for the Python programming language <python-announce-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-announce-list>, <mailto:python-announce-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-announce-list/>
List-Post <mailto:python-announce-list@python.org>
List-Help <mailto:python-announce-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-announce-list>, <mailto:python-announce-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <5b8aa582$0$14316$426a74cc@news.free.fr>
Xref csiph.com comp.lang.python.announce:2967

Show key headers only | 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