我在寫一個用於將多種類型(球體,平面,NURBS曲面,stl文件...)的幾何圖形導入科學Fortran代碼的庫。這種問題似乎是由OOP製造的,因爲它很容易定義type :: geom
,然後定義爲type,extends(geom) :: analytic
等等。我遇到的部分是文件IO。在Fortran中使用多態數據類型的文件IO
我現在的解決方案是編寫定義形狀的參數,包括一些標誌,告訴我它是哪種形狀。在閱讀時,我實例化了一個class(geom) :: object
,(因爲我不知道提前知道哪個子類型),但我怎麼讀它?我不能訪問任何子類型的特定組件。我看到這個向下轉換是verboten,此外,新的allocate(subtype :: class)
似乎沒有工作。新的READ(FORMATTED)
似乎不被ifort或gfortran實施。即
module geom_mod
type :: geom
end type
type,extends(geom) :: sphere
integer :: type
real(8) :: center(3),radius
contains
generic :: READ(FORMATTED)=> read_sphere ! not implemented anywhere
end type
contains
subroutine read_geom(object)
class(geom),intent(out),pointer :: object
integer :: type
read(10,*) object%type ! can't access the subtype data yet
read(10,*) type
backspace(10)
if(type==1) then
allocate(sphere :: object)! downcast?
read(10,*) object ! doesn't work
end if
end read_geom
end module
我要對所有這一切錯了嗎?我可以使用除多態之外的其他東西來破解這個,但是在其他地方這看起來更清晰。援助將不勝感激。
編輯:使用IanH的模塊
program test
use geom_mod
implicit none
class(geom),allocatable :: object
open(10)
write(10,*) '1'
write(10,*) sphere(center=0,radius=1)
rewind(10)
call read(object) ! works !
end program test
輸入文件需要有(至少)我的示例代碼的兩條記錄 - 第一條記錄的類型爲整數,第二條記錄爲球體數據。你真的得到了段錯誤還是文件結束狀態? – IanH 2012-07-20 06:01:52
我在球體類型中添加了一個'integer :: type = 1'行,這只是混淆了這個問題。我不知道爲什麼這會導致seg錯誤,但是當我堅持使用模塊時,它會起作用。謝謝! – weymouth 2012-07-20 06:08:26
它看起來像seg故障鏈接到具有默認值的派生類型中的任何變量。所以如果我在類型定義中設置'radius = 1',我會得到seg故障。 – weymouth 2012-07-20 06:18:15