简体   繁体   中英

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. After wrapping the modules with f2py, f2py -c -m photdyn_model.f90 -m photdyn_model and importing into python, I get an error message

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

Why is f2py having trouble with this publicly defined constant? 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. 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.

I found the error had been caused by a previous module photdynh which had been compiled by gfortran and was accidentally still on disk. gfortran is also the compiler I used with f2py. Although the problem is solved in practice, still there are issues with f2py which deserve attention. 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. This mistake seems hard to solve. Is this a bug in f2py?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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