[英]Fortran77 write(0,*) DLL command not flushing to R GUI console
[英]Write in a new line on every write(33,*) comand in Fortran77
我創建了一個fortran代碼來計算cfd模型的溫度。 在穩態仿真的每次迭代中都將調用此代碼,並計算溫度。 在每次調用代碼/迭代時,我都希望我的fortran代碼也將溫度字段保存在txt文件中並保存。 計算溫度場並將值保存在TFIELD(100; 1:6)中后,保存在txt文件中的部分如下所示:
OPEN(UNIT=35,FILE='W:\temperaturField.txt',
&FORM ='FORMATTED',STATUS='UNKNOWN',
&ACTION='READWRITE')
WRITE (35,FMT='5(F8.3,2X))') TFIELD(100,1:6)
使用此代碼,每次迭代它只會覆蓋我的txt文件的第一行。 但是我想將每個TFIELD(100,1:6)數組粘貼到新行上。 我怎樣才能做到這一點?
將POSTITION ='APPEND'添加到OPEN:
OPEN(UNIT=35,FILE='W:\temperaturField.txt',
&FORM ='FORMATTED',STATUS='UNKNOWN',POSITION='APPEND',
&ACTION='READWRITE')
似乎您正在為每次迭代打開和關閉文件。 如果需要調試,這是一種快速而骯臟的方法,但是它很慢。
如果要執行此操作,則可能需要執行@Jack所說的操作:在OPEN
語句中包含POSITION='APPEND'
以設置將數據寫入文件末尾的位置。 另外,您需要確保每次都將其關閉。
更好(更有效)的方法是使文件始終保持打開狀態。 我會用一個模塊來做到這一點:
module temp_writer_module
implicit none
integer :: temp_writer_unit
logical :: is_opened = .FALSE.
private :: temp_writer_unit, is_opened
contains
subroutine temp_writer_open()
integer :: ios
character(len=100) :: iomsg
if (is_opened) then
print*, "Warning: Temperature file already openend"
return
end if
open(newunit=temp_writer_unit, file='W:\temperatureField', &
form='FORMATTED', status='UNKNOWN', action='WRITE', &
iostat=ios, iomsg=iomsg)
if (ios /= 0) then
print*, "Error opening temperature file:"
print*, trim(iomsg)
STOP
end if
is_opened = .TRUE.
end subroutine temp_writer_open
subroutine temp_writer_close()
if (.not. is_opened) return
close(temp_writer_unit)
is_opened = .FALSE.
end subroutine temp_writer_close
subroutine temp_writer(temps)
real, intent(in) :: temps(6)
integer :: ios
character(len=100) :: iomsg
if (.not. is_opened) call temp_writer_open()
write(temp_writer_unit, *, iostat=ios, iomsg=iomsg) temps
if (ios /= 0) then
print*, "Error writing to temperature file:"
print*, trim(iomsg)
end if
end subroutine temp_writer
end module temp_writer_module
然后,您可以像下面這樣在程序中使用它:
subroutine calc_temps(...)
use temp_writer_module
<variable declarations>
<calculations>
call temp_writer(tfield(100, 1:6))
end subroutine calc_temps
只是不要忘記在程序結束之前調用temp_writer_close
例程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.