【发布时间】:2023-03-24 17:31:01
【问题描述】:
我的问题是垃圾收集如何(并且确实)为 Fortran 模块(通过 f2py)编译为外部模块并由 Python 脚本使用?
背景
我目前正在开发一个有限元程序,主要是用 Python 编写的(为方便起见),它使用 Fortran 子例程的几个模块来完成无法避免某些循环的任务(即组装/更新切线刚度矩阵)。这是使用 f2py (the quick way) 成功编译的,生成的 .dll 文件在 python 脚本中作为 python 模块导入。然后可以由 Python 成功调用函数/子例程。
Fortran 模块的结构如下,不同的子程序使用模块中定义的一些全局变量和它们自己的虚拟变量(根据需要)。
module mod
integer, parameter :: iwp = SELECTED_REAL_KIND(15)
real(iwp), allocatable, dimension(:,:) :: points, der, matrix, jac, deriv, bee, g_coord
real(iwp), allocatable, dimension(:) :: fun, disps
real(iwp), allocatable, dimension(:,:,:) :: tensor
integer, allocatable, dimension(:,:) :: g_num, g_g
integer, allocatable, dimension(:) :: etype, weights
integer :: ndof,nodf,ndim,nels,nip,nod
contains
...subroutines,functions...
全局变量直接从 Python 脚本分配和分配为具有正确类型的 F 连续 numpy 数组(可能风格很差,但它适用于我)。 这些可能是相对较大的数组,我想确保它们在 python 脚本成功完成后被释放...
欢迎提出其他建议!非常感谢
【问题讨论】: