Fortran:forpy 嵌入Python

2024-05-26 20:20
文章标签 python 嵌入 fortran forpy

本文主要是介绍Fortran:forpy 嵌入Python,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Fortran嵌入Python

利用forpy库,可以在Fortran程序内嵌入Python.

program test_forpyuse forpy_modimplicit noneinteger::ierrierr=forpy_initialize()!!---------------!!list!!---------------blocktype(list)::my_listierr=list_create(my_list)ierr=my_list%append(19)ierr=my_list%append("Hello world")ierr=my_list%append(3.14d0)ierr=print_py(my_list)call my_list%destroyend block!!----------------!!dict!!----------------blocktype(dict)::direal::a_valueierr=dict_create(di)ierr=di%setitem("temperature",273.0)ierr=di%setitem("pressure",1013.0)ierr=di%getitem(a_value,"pressure")write(*,*) 'pressure= ',a_valueierr=di%getitem(a_value,'does not exist')if(ierr/=0) thenif(exception_matches(KeyError)) thenwrite(*,*) 'Key not found....'call err_clearelsewrite(*,*) 'Unknown error.....'stopendifendif!default valueierr=di%get(a_value,"volumne",1.0)write(*,*) 'volume= ',a_valuecall di%destroy()endblock!!----------------!!tuple!!----------------blocktype(tuple)::tutype(object)::iteminteger::tu_leninteger::iinteger::int_valuecharacter(len=:),allocatable::str_valueierr=tuple_create(tu,4)ierr=tu%setitem(0,17)ierr=tu%setitem(1,"hello")ierr=tu%setitem(2,23)ierr=tu%setitem(3,"world")ierr=tu%len(tu_len)do i=0,tu_len-1 !!python 0-basedierr=tu%getitem(item,i)if(is_int(item))thenierr=cast(int_value,item)write(*,*) int_valueelse if(is_str(item)) thenierr=cast(str_value,item)write(*,*) str_valueendifend docall tu%destroy()endblock!!----------------------!!import python module !!----------------------blocktype(module_py)::datetimetype(object)::date,today,today_strcharacter(len=:),allocatable::today_fortranierr=import_py(datetime,"datetime")ierr=datetime%getattribute(date,'date')ierr=call_py(today,date,"today")ierr=call_py(today_str,today,'isoformat')ierr=cast(today_fortran,today_str)write(*,*) 'Today is ',today_fortran call datetime%destroy()call date%destroycall today%destroycall today_str%destroyendblock!!---------------------------------!!import my python module !!---------------------------------blocktype(list)::pathstype(tuple)::argstype(dict)::kwargs,xxxtype(module_py)::mymoduletype(object)::return_valuetype(ndarray)::cellsreal,dimension(2,3)::matrixcharacter(len=:),allocatable::return_stringierr=get_sys_path(paths)ierr=paths%append(".")ierr=import_py(mymodule,'mymodule')ierr=tuple_create(args,3)ierr=args%setitem(0,12)ierr=args%setitem(1,'Hi')ierr=args%setitem(2,.true.)ierr=dict_create(kwargs)ierr=kwargs%setitem("message","Hello world")matrix=1111ierr=ndarray_create_nocopy(cells,matrix) !!ndarray(shallow copy)!ierr=kwargs%setitem("cells",cells) !!成员是ndarrayierr=dict_create(xxx)ierr=xxx%setitem("cells",cells)ierr=kwargs%setitem("xxx",xxx) !!成员是dictierr=call_py(return_value,mymodule,"print_args",args,kwargs)ierr=cast(return_string,return_value)write(*,*) return_string matrix(1,1)=99999 !!python端也被改变ierr=call_py(return_value,mymodule,"print_args",args,kwargs)ierr=cast(return_string,return_value)write(*,*) return_string call args%destroycall kwargs%destroycall mymodule%destroycall return_value%destroycall cells%destroycall xxx%destroycall paths%destroyendblock!!-----------------!!array!!-----------------!!deep-copy : creating a numpy array from a Fortran array blockreal,dimension(:,:),allocatable::matrixtype(ndarray)::arrinteger::i,jallocate(matrix(2,3))do i=1,size(matrix,1)do j=1,size(matrix,2)matrix(i,j)=real(i)*jend doenddoierr=ndarray_create(arr,matrix)ierr=print_py(arr)call arr%destroyend block!!creating a numpy wrapper for a Fortran arrayblock!Fortran: asynchronousinteger::i,jreal,asynchronous::matrix(2,3)type(ndarray)::arrforall(i=1:2,j=1:3) matrix(i,j)=real(i)*jierr=ndarray_create_nocopy(arr,matrix)ierr=print_py(arr)matrix(1,1)=1234.0ierr=print_py(arr)call arr%destroyend block!how to return a ndarray from a subroutineblockuse iso_fortran_env,only:real64type(ndarray)::arrinteger::i,jreal(kind=real64),dimension(:,:),pointer::matrixierr=ndarray_create_empty(arr,[2,3],dtype='float64')ierr=arr%get_data(matrix)forall(i=1:2,j=1:3) matrix(i,j)=real(i,kind=real64)*jierr=print_py(arr)call arr%destroyend block!!----------------------------------call forpy_finalizeend program test_forpy!!====================================
!!编译:
!!   gfortran -c forpy_mod.F90
!!   gfortran test_forpy.F90 forpy_mod.o `python3-config --ldflags --embed`
!!
!!

mymodule.py

def print_args(*args,**kwargs):print("Arguments: ",args)print("keyword arguments: ",kwargs)return "Return from mymodule.print_args"

资料

文档,API

这篇关于Fortran:forpy 嵌入Python的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1005493

相关文章

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

使用Python实现矢量路径的压缩、解压与可视化

《使用Python实现矢量路径的压缩、解压与可视化》在图形设计和Web开发中,矢量路径数据的高效存储与传输至关重要,本文将通过一个Python示例,展示如何将复杂的矢量路径命令序列压缩为JSON格式,... 目录引言核心功能概述1. 路径命令解析2. 路径数据压缩3. 路径数据解压4. 可视化代码实现详解1

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

Python装饰器之类装饰器详解

《Python装饰器之类装饰器详解》本文将详细介绍Python中类装饰器的概念、使用方法以及应用场景,并通过一个综合详细的例子展示如何使用类装饰器,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. 引言2. 装饰器的基本概念2.1. 函数装饰器复习2.2 类装饰器的定义和使用3. 类装饰

Python 交互式可视化的利器Bokeh的使用

《Python交互式可视化的利器Bokeh的使用》Bokeh是一个专注于Web端交互式数据可视化的Python库,本文主要介绍了Python交互式可视化的利器Bokeh的使用,具有一定的参考价值,感... 目录1. Bokeh 简介1.1 为什么选择 Bokeh1.2 安装与环境配置2. Bokeh 基础2

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指