如何将我的(7,3,3,3)数组传递给fortran子例程? [英] How do I pass my (7, 3, 3, 3) array to a fortran subroutine?

查看:225
本文介绍了如何将我的(7,3,3,3)数组传递给fortran子例程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



子例程需要一个numpy的形状(7,3,3,3) )。该数组是7个立方体的数组,大小为3x3x3。我还将整数7和3传递给子程序。

以下是代码

 子程序fit(n,m,a)

c ============================ =======================================
c ArrayTest.f
c ================================================== =================
cn - 传递的立方体数量
c
cm - 立方体尺寸的边缘
c
ca(n ,m,m,m) - 形状数组(n,m,m,m)
c
c ====================== =============================================

隐式无
整数m
整数n
双精度a(n,m,m,m)

结束子程序拟合

这只是为了看看我是否可以传递数组。当我从python编译并调用它时,出现以下错误。

 导入ArrayTest作为AT 
import numpy as np

n = 7
m = 3
a = np.ones((n,m,m,m))

AT.fit(n, m,a)

抛出

  ArrayTest.error:(shape(a,0)== n)第一个关键字失败n:fit:n = 3 

我不知道发生了什么事。在fortran中将数组定义为(m,m,m,m)不会产生任何问题,只是当我试图从两个整数中定义它时,它会导致问题,即使我设置m = n = 3。我将(7,3,3,3)数组传递给fortran子例程吗?

解决方案

查看文档字符串由f2py创建的Python函数:

  fit(a,[n,m])

适合的包装。

参数
----------
a:输入具有界限(n,m,m,m)的rank-4数组('d')

其他参数
----------------
n:输入int,可选
默认值:shape(a,0)
m:input int,optional
默认值:shape(a,1)

f2py认识到 n m 描述了 a ,因此不需要Python函数的参数,因为可以通过检查numpy数组的形状来找到它们。因此,它们是Python函数 fit 的可选第二和第三个参数:

  
In [9]:n = 7

在[10]中:m = 3

在[11]中:a = np.zeros((n,m,m,m))

在[12]中:AT.fit(a,n,m)

In [13]:AT.fit(a)


I have written a fortran subroutine to by used in python via the f2py command.

The subroutine takes a numpy ndarray of shape (7, 3, 3, 3). The array is an array of 7 cubes, of size 3x3x3. I also pass the integers 7 and 3 to the subroutine.

Here is the code

        subroutine fit(n, m, a)

c ===================================================================
c                            ArrayTest.f
c ===================================================================
c       n            - number of cubes being passed
c
c       m            - edge of cube size
c
c       a(n,m,m,m)   - array of shape (n,m,m,m)
c
c ===================================================================

        implicit none
        integer m
        integer n
        double precision a(n,m,m,m)

        end subroutine fit

This is just to see if I can pass the array. When I compile and call it from python, I get the following error.

import ArrayTest as AT 
import numpy as np

n = 7
m = 3
a = np.ones((n,m,m,m))

AT.fit(n, m, a)

throws

ArrayTest.error: (shape(a,0)==n) failed for 1st keyword n: fit:n=3

I have no idea what is going on. Defining the array in fortran as a(m,m,m,m) throws up no problems, it is only when I try to define it from two integers that it causes problems, even if I set both m = n = 3. How do I pass my (7, 3, 3, 3) array to a fortran subroutine?

解决方案

Take a look at the docstring of the Python function created by f2py:

fit(a,[n,m])

Wrapper for ``fit``.

Parameters
----------
a : input rank-4 array('d') with bounds (n,m,m,m)

Other Parameters
----------------
n : input int, optional
    Default: shape(a,0)
m : input int, optional
    Default: shape(a,1)

f2py recognized that n and m describe the shape of a, and so are not required arguments for the Python function, since they can be found by inspecting the shape of the numpy array. So they are optional second and third arguments of the Python function fit:

In [8]: import ArrayTest as AT

In [9]: n = 7

In [10]: m = 3

In [11]: a = np.zeros((n, m, m, m))

In [12]: AT.fit(a, n, m)

In [13]: AT.fit(a)

这篇关于如何将我的(7,3,3,3)数组传递给fortran子例程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆