fortran + iso_c_binding(通过ctypes或cython)和matplotlib在读取名单时的冲突[仅限python Anaconda !!]

时间:2015-03-07 14:11:43

标签: matplotlib fortran ctypes cython anaconda

[编辑:问题仅适用于python anaconda,而不适用于标准/usr/bin/python2.7]

[仅供参考:[{3}}对于尝试使用带有ctypes或cython的fortran的人来说仍然有用,可归功于the gist referred to in this post]

当使用python中的fortran代码(使用iso_c_bindings)时,无论是通过ctypes还是通过cython,我遇到了与matplotlib相关的奇怪的不兼容问题。基本上,如果matplotlib被“激活”(通过%pylab或使用pyplot.plot命令),读取名称列表将省略任何数字!!即值9.81读为9.00。没有matplotlib,没问题。

我做了http://www.fortran90.org/src/best-practices.html

基本上,fortran模块只允许从名单中读取双精度参数g,并将其存储为全局模块变量。它还可以将其值打印到字符串,并允许从外部直接设置其值。这有三个功能:

  • READ_PAR
  • print_par
  • set_par

您可以下载要点示例,然后运行:

make ctypes
python test_ctypes.py

test_ctypes.py包含:

from ctypes import CDLL, c_double
import matplotlib.pyplot as plt

f = CDLL('./lib.so')

print "Read param and print to screen"
f.read_par()
f.print_par()

# calling matplotlib's plot command seem to prevent
# subsequent namelist reading
print "Call matplotlib.pyplot's plot"
plt.plot([1,2],[3,4])

print "Print param just to be sure: everything is fine"
f.print_par()

print "But any new read will lose decimals on the way!"
f.read_par()
f.print_par()

print "Directly set parameter works fine"
f.set_par(c_double(9.81))
f.print_par()

print "But reading from namelist really does not work anymore"
f.read_par()
f.print_par()

输出:

Read param and print to screen
g   9.8100000000000005     
Call matplotlib.pyplot's plot
Print param just to be sure: everything is fine
g   9.8100000000000005     
But any new read will lose decimals on the way!
g   9.0000000000000000     
Directly set parameter works fine
g   9.8100000000000005     
But reading from namelist really does not work anymore
g   9.0000000000000000    

cython示例也是如此(make clean; make cython; python test_cython.py)。

有谁知道发生了什么,或者是否有任何解决方法?我为我的fortran代码编写包装器的主要原因是能够使用模型,设置参数(通过名单),运行它,绘制结果,设置其他参数等等。所以对于我的用例,这种bug违背了交互的目的......

非常感谢任何提示。

PS:我很高兴在某个地方提交一个bug,但不知道在哪里(gfortran?matplotlib?)

0 个答案:

没有答案