简体   繁体   English

在使用 f2py 包装到 Python 的 Fortran 模块中定义常量的问题

[英]problem with defining constants in a Fortran module which is wrapped into Python using f2py

    module constants
    
    implicit none
    
    DOUBLE PRECISION,parameter,public::MJ=9.552538964304e-4
    DOUBLE PRECISION,parameter,public::pi=3.1415926536
    DOUBLE PRECISION,parameter,public::MS=0.44
    DOUBLE PRECISION,parameter,public::RS=0.002045615583096031
    
    end module constants
    
    
    module photdynh
    
    use constants

    INTEGER,parameter,public::NBOD=2,NDGL=6*NBOD, &
    n=7+8*(nbod-1),nmax=77970,nparam=3*nbod
    INTEGER,public::NOBS,ntransit(nbod),phottype
    DOUBLE PRECISION,public::mc(nbod),mp(nbod),xstart, &
    ystart(ndgl),per(nbod)
    DOUBLE PRECISION,public::a(NBOD),e(NBOD),inc(NBOD)
    DOUBLE PRECISION,public::g(NBOD),node(NBOD),l(NBOD),p(NBOD), &
    ex(NBOD),ey(NBOD)
    double precision,public::lb(n),ub(n),flux_obs(nmax), &
    jd(nmax),normerr(nmax),tmids(nbod),u1,u2,rearth
    logical::ipar(n),iorbel

    end module photdynh 

The code above defines fortran modules that are used by another module photdyn_model with use statements like use photdynh.上面的代码定义了 fortran 个模块,这些模块被另一个模块 photdyn_model 使用,使用语句如 use photdynh。 After wrapping the modules with f2py, f2py -c -m photdyn_model.f90 -m photdyn_model and importing into python, I get an error message用 f2py、f2py -c -m photdyn_model.f90 -m photdyn_model 包装模块并导入到 python 后,我收到一条错误消息

File "photdynmodel.py", line 4, in import photdyn_model ImportError: /mnt/d/TESS/TOI2095/photdyn_model.cpython-38-x86_64-linux-gnu.so: undefined symbol: __photdynh_MOD_ms文件“photdynmodel.py”,第 4 行,导入 photdyn_model ImportError: /mnt/d/TESS/TOI2095/photdyn_model.cpython-38-x86_64-linux-gnu.so: undefined symbol: __photdynh_MOD_ms

Why is f2py having trouble with this publicly defined constant?为什么 f2py 在处理这个公开定义的常量时遇到问题? Is there something wrong with this code/approach?此代码/方法有问题吗?

You don't say which Fortran compiler you're using, but at least with GFortran a named constant (a variable declared with the parameter attribute) doesn't necessarily result in a corresponding symbol in the object file.您没有说您使用的是哪个 Fortran 编译器,但至少对于 GFortran,命名常量(使用parameter属性声明的变量)不一定会在 object 文件中产生相应的符号。 It's been a very long time since I've used f2py, but if f2py expects that each Fortran variable has a corresponding symbol in the object file (as suggested by the error message you're getting), then that sounds like a bug in f2py.自从我使用 f2py 以来已经有很长时间了,但是如果 f2py 期望每个 Fortran 变量在 object 文件中都有一个相应的符号(如您收到的错误消息所建议的那样),那么这听起来像是 f2py 中的错误.

I found the error had been caused by a previous module photdynh which had been compiled by gfortran and was accidentally still on disk.我发现错误是由之前的模块 photdynh 引起的,该模块由 gfortran 编译并且不小心仍在磁盘上。 gfortran is also the compiler I used with f2py. gfortran 也是我在 f2py 中使用的编译器。 Although the problem is solved in practice, still there are issues with f2py which deserve attention.虽然在实践中解决了这个问题,但是f2py仍然存在一些值得关注的问题。 For example, if I declare MS and RS as variables in the module photdynh, and then set their values in the main python program as photdynh.MS=0.44 and photdynh.RS=0.44, for example, f2py sets these values to zeros in the wrapped FORTRAN code upon execution.例如,如果我在 photdynh 模块中将 MS 和 RS 声明为变量,然后在主程序 python 中将它们的值设置为 photdynh.MS=0.44 和 photdynh.RS=0.44,例如,f2py 将这些值设置为零执行时包装 FORTRAN 代码。 This mistake seems hard to solve.这个错误似乎很难解决。 Is this a bug in f2py?这是 f2py 中的错误吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM