2015-08-29 72 views
0

我需要在兩個不同的fortran程序之間交換數據,不幸的是需要分開(兩個不同的.exe),一個在f77中寫入另一個。
目前這是我的解決方案:
- f90(.exe)在f77(master)的開始處啓動,並使用do while('true')和inquire 等待來自f77的數據 - 當數據寫入某個文件(file_in)時,f90讀取,詳細說明並將結果寫入一個新文件(file_out) - f77使用do while('true')和詢問 並等待f90中的數據上 這是在f77中的程序的例子的代碼:Fortran:在兩個不同的程序之間交換數據

C Write data for f90 
    open(210, file=file_in, action='write', status='replace', form='unformatted') 
    write(210) data 
    close(210) 
    do while (.true.) 
     inquire(file=KDTree_out, exist=file_exist) 
     if (file_exist) then 
      call sleepqq(2) 
      exit 
     endif 
    enddo 
    open(220, file=KDTree_out, action='read', status='old', form='unformatted') 
    read(220) value 
    close(220, status='delete') 

這F90爲程序的例子的代碼:

do while(.true.) 
    inquire(file=KDTree_in, exist=file_exist) 
    if (file_exist) then 
      call sleepqq(2) 
      ! Read data 
      open(100, file=file_in, action='read', status='old', form='unformatted') 
      read(100) data 
      close(100, status='delete') 

      ! Elaborate data 

      ! Write data  
      open(150, file=file_out, action='write', status='replace', form='unformatted') 
      write(150) value 
      close(150) 

     endif 
enddo 

此解決方案似乎工作得很好,但正如您可以看到有一些sleepqq(2)(微秒)以避免文件錯誤結束,但如果您有和ssd或硬盤,則此等待時間可能會變化不是完美的解決方案。 你有什麼想法嗎?

感謝

+0

您的「F77」程序不是F77,但它(顯示的片段)是F90。那不是你需要單獨程序的唯一原因嗎? – francescalus

+0

F77程序是商業軟件的用戶子程序。雖然我需要F90的https://github.com/jmhodges/kdtree2/tree/master/src-f90。所以他們需要分開 – yellowhat

+0

我對你的問題沒有什麼可以直接說的,我不知道你的案例的全部細節。但是,如果這是我的項目,我會考慮如何在沒有多個程序與臨時文件共享數據的情況下執行此操作。 「F77」和「F90」之間的區別通常不是必需的:如果您正在編譯這兩個代碼塊,那麼就編譯器而言它們都是Fortran。也就是說,絕大多數F77代碼不能與F90共存在一個程序中。 [需要注意] – francescalus

回答

0

由於agentp,這解決了我的問題:

考慮使用第二個文件爲標誌,你寫關閉/沖洗數據文件後,應與一些計時的幫助問題

[編輯] 我遇到了一些問題,用於標記數據文件已準備好的文件。我並不總是能夠刪除這個文件。

這是我的解決方案後,數據文件已經準備好我寫一個程序:

open(202, file=chk_file2) 
close(202) 

在其他程序中,我有這樣的代碼來獲得該標誌並刪除文件,因爲我需要交流很多情況下數據:

do while (.true.) 
    inquire(file=chk_file2, exist=file_exist) 
    if (file_exist) then 
102 continue 
     istat = 1 
     do while (istat.eq.0) 
     open(401, file=chk_file2, iostat=istat, err=102) 
     close(401, status='delete', err=102) 
     enddo 
     exit 
    endif 
enddo 

或者:

do while (.true.) 
    inquire(file=chk_file2, exist=file_exist) 
    if (file_exist) then 
102 continue 
     do while (.true.) 
      open(400, file=chk_file2, iostat=istat, err=102) 
      if (istat.eq.0) then 
       close(400, status='delete', err=102) 
       exit 
      endif 
     enddo 
     exit 
    endif 
enddo 

感謝

相關問題