简体   繁体   English

Fortran 代码仅在运行 python 时才会出错

[英]Fortran code gives error only when run through python

I have a python code (let's call it 'pycode.py') that creates a.txt file ('fortc.txt').我有一个 python 代码(我们称之为'pycode.py'),它创建一个.txt 文件('fortc.txt')。 This file needs to be read by a.exe ('f90code.exe') generated through a fortran90 code.该文件需要通过fortran90代码生成的a.exe('f90code.exe')读取。 The fortran code then do some operation and write the result in another.txt file ('res_tc.txt'), previously created through pycode.py, that pycode.py will finally read and print. fortran 代码然后执行一些操作并将结果写入另一个.txt 文件('res_tc.txt'),之前通过 pycode.py 创建,pycode.py 最终将读取和打印。 Here is the python code:这是 python 代码:

import csv
import sys, os
import numpy as np

with open('fortc.txt', 'w') as g:
    writer = csv.writer(g, delimiter=" ", quoting=csv.QUOTE_NONE, escapechar=' ')
    writer.writerow(['1', '2', '3', '4'])

    f = open('res_tc.txt','w+')
    os.system('./f90code.exe')
    readres = np.loadtxt('res_tc.txt')
    tc = float(readres)
    print(tc)
    f.close()

If I create the file 'fortc.txt' manually and then just run the f90 code, without passing though python, everything works fine and I get the expected result.如果我手动创建文件“fortc.txt”,然后只运行 f90 代码,而不通过 python,一切正常,我得到了预期的结果。 However, if I run everything through python, 'fortc.txt' is created correctly, but apparently fortran is not able to read it properly.但是,如果我通过 python 运行所有内容,则会正确创建“fortc.txt”,但显然 fortran 无法正确读取它。 In fact, everything in 'fortc.txt' is read as '0.0000000000000', producing the following error when performing the operation:事实上,“fortc.txt”中的所有内容都被读取为“0.0000000000000”,执行操作时会产生以下错误:

Note: The following floating-point exceptions are signalling: IEEE_INVALID_FLAG IEEE_DENORMAL

And obviously I get a 'NaN' as result.显然我得到了一个“NaN”。 Furthermore, it looks like fortran is not running the do loops correctly, since if I try to print something while inside a do loop, I don't get anything printed out.此外,看起来 fortran 没有正确运行 do 循环,因为如果我尝试在 do 循环内打印某些内容,我不会打印出任何内容。 Here is the part of the f90 code that reads the file:以下是读取文件的 f90 代码部分:

program tc

      real*8, allocatable:: ne(:), kt(:), abund(:), z(:)
      integer:: i, j, ndati, n
      data ab/0.,0.1,0.31623,1./

      open(96,file='fortc.txt',status='old')

      n=0
      do
        read(96,*,end=320)
        n=n+1
      end do

      320 continue
      ndat=n

      allocate(ne(ndat), kt(ndat), abund(ndat), z(ndat))

      rewind(96)

      do j=1, ndat
         read(96,*) kt(j), abund(j), ne(j), z(j)
         print*, kt(j)
      end do
end program

There is no outcome for the print call at the end of the f90, just the error shown above and the 'NaN' (derived from the print within the python code). f90 结束时的打印调用没有结果,只有上面显示的错误和“NaN”(源自 python 代码中的打印)。 On the other hand, if the print statement is brought outside of the do loop, I get something printed ('0.00000000', as mentioned above).另一方面,如果将 print 语句带到 do 循环之外,我会打印一些东西('0.00000000',如上所述)。 Again, this happens only when running the compiled fortran code through python, while if I just run the.exe from my terminal, it works as intended.同样,只有在通过 python 运行编译的 fortran 代码时才会发生这种情况,而如果我只是从终端运行.exe,它会按预期工作。

Is there something wrong about how the do loops are dealt with? do循环的处理方式有什么问题吗? The fact that the numbers within the.txt file are not read correctly, and that the print is not performed, suggested me that the issue could be there. .txt 文件中的数字没有正确读取,并且没有执行打印,这表明我可能存在问题。 However, I am not being able to fix it.但是,我无法修复它。

Your input file is empty when you call the fortran code:当您调用 fortran 代码时,您的输入文件为空:

with open('fortc.txt', 'w') as g:

By default, Python will flush data written to this file and close it when you exit the with body.默认情况下,Python 将刷新写入此文件的数据,并在退出with正文时将其关闭。 But you include your call to the fortran code in it.但是您在其中包含了对 fortran 代码的调用。

If you want to use with for file manipulation, rearrange your code in this way:如果您想使用with进行文件操作,请以这种方式重新排列您的代码:

import csv
import sys, os
import numpy as np

with open('fortc.txt', 'w') as g:
    writer = csv.writer(g, delimiter=" ", quoting=csv.QUOTE_NONE, escapechar=' ')
    writer.writerow(['1', '2', '3', '4'])

f = open('res_tc.txt','w+')   # not sure why the frotran program would output to "f" just because it is open.  If it does, ok, it will wrk.
os.system('./f90code.exe')
readres = np.loadtxt('res_tc.txt')
tc = float(readres)
print(tc)
f.close()  # you can use explicit open and close calls for fortc.txt as well

暂无
暂无

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

相关问题 在 Python 中使用 statsmodels 时出现 Fortran 运行时错误 - Fortran run-time error when using statsmodels in Python 通过anaconda提示符执行python代码时,导入numpy起作用,但通过cmd运行时产生错误 - Import numpy works when python code executed through anaconda prompt but produces an error when run through cmd 在TextWrangler上编写的看似完美的代码在Python 3上运行时会产生SyntaxError - Seemingly perfect code written on TextWrangler gives SyntaxError when run on Python 3 当我尝试运行代码时,它会出错 - When I am trying to run the code it gives error 执行数组旋转 Python 代码时,会出现错误 TypeError - When exectuting a array rotation Python code it gives the error TypeError Powershell 执行策略受到限制,但仅当通过 python 运行时 - Powershell Execution Policy is restricted, but only when run through python Python try except 块在我不希望出现问题时运行异常代码,并且在我希望它运行异常代码时只给出错误 - Python try except block runs the except code when I don't expect a problem and just gives an error when I expect it to run the except code 如果我从 cmd 运行此代码,Python 会出错,但是如果我从 Jupyter 运行它,则运行良好 - Python gives error if I run this code from cmd, however runs fine if I run it from Jupyter 当我在 pycharm 上运行此代码时,它在创建表的行上给出了错误“输入错误”。 有语法错误吗? - When i run this code on pycharm it gives an error “Input Error” on the line create table . Is there a syntax error? Python 代码运行错误 需要帮助。 它给出了“无法导入名称'键'”的错误 - Python code run error Need help. It gives error that "cannot import name 'keys'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM