理工系研究者のための Mac OS X, Windows, Linux メモ
 

FortranのTips

  1. Absoft Fortran 7 -- 実行時に segmentation Fault が出る(2002.9.13)
  2. 一度CLOSEしたファイルに書き足す方法(2002.7.31)
  3. 書き出しファイル名を番号付きにする(2002.7.10)
  4. Absoft Fortran 7.0 -- rerun ranlib(1) (can't load from it) (2002.2.14)
  5. Absoft Fortran・・・WARNING (2001.7.7)
    File "loss.f" ; Line 83 ## WARNING: variable rnau misaligned
  6. NAMELISTを読み込んでくれない・・順番が重要らしい (2001.3.9)

Absoft Fortran 7 -- 実行時に segmentation Fault が出る

Hulinks のサポートページより。

コンパイルは問題なくできるが、実行時に segmentation fault が起こって停止する場合、原因はスタックオーバーフローである。対処法は -s オプションを付けてコンパイルするか、ターミナルウインドウでプログラム実行中に、

limit stacksize unlimited

と打つと良い。

一度CLOSEしたファイルに書き足す方法

ファイルを始めに作っておく。

open(10,file='filename',status='REPLACE',)
write(10,*) "# Temperature vs Intensity"
close(10)

追記したいときに、

open(10,file="filename',position='APPEND')
write(10,*) temperature, intensity
close(10)

とすれば良い。
position='APPEND'
は、開いたファイルの最後から追記するという事。

書き出しファイル名を番号付きにする

c.. sample
character*5 label5
character*2 label2
ii=1234
jj=8
call label_gen5(ii,label5)
call label_gen2(jj,label2)
write(*,*) label5
write(*,*) label2
open(43,file='.'//'/label2.'//label2)
write(43,*) label2
close(43)
open(44,file='.'//'/label5.'//label5)
write(44,*) label5
close(44)
stop
end
c=======================================================================
subroutine label_gen5(nstart,label)
implicit none

integer nstart
integer ind(5)
integer ind1,ind2,count
character*1 cha(5)
character*5 label

do count=1,5
ind(count)=0
cha(count)='0'
enddo

count=1
ind1=nstart
1111 ind(count)=mod(ind1,10)
cha(count)=char(48+ind(count))
ind2=int(ind1/10)
ind1=ind2
count=count+1
if (ind1.gt.0) goto 1111

label=cha(5)//cha(4)//cha(3)//cha(2)//cha(1)

return
end
c=======================================================================
subroutine label_length(nch,dir,iln)
implicit none

integer nch,iln,i
character dir(nch)

iln=0
do i=1,nch
if(dir(i).eq.' ') goto 100
iln=iln+1
enddo
100 continue
if(dir(iln).eq.'/') iln=iln-1
c
return
end
subroutine label_gen2(nstart,label)
implicit none

integer nstart
integer ind(2)
integer ind1,ind2,count
character*1 cha(2)
character*2 label

do count=1,2
ind(count)=0
cha(count)='0'
enddo

count=1
ind1=nstart
1111 ind(count)=mod(ind1,10)
cha(count)=char(48+ind(count))
ind2=int(ind1/10)
ind1=ind2
count=count+1
if (ind1.gt.0) goto 1111

label=cha(2)//cha(1)

return
end

rerun ranlib(1) (can't load from it)

Absoft Fortranのエラー

f77 main.o init.o besselk.o elc_advection.o recording.o pho_advection.o -o positron2D
/usr/bin/ld: table of contents for archive: /Applications/Absoft/lib/libfio.a is out of date; rerun ranlib(1) (can't load from it)
/usr/bin/ld: table of contents for archive: /Applications/Absoft/lib/libf77math.a is out of date; rerun ranlib(1) (can't load from it)

make: *** [positron2D] Error 1

解決法

% cd /Applications/Absoft/lib/
% ranlib libfio.a
% ranlib libf77math.a

Absoft Fortran・・・WARNING (2001.7.7)

原因・・common 文の中に、変数型の違うもの、もしくは配列の次元の違うものが入っている。
common /dist_func/f1,rnau
たとえば、f1 は2次元、rnauは1次元など。

NAMELISTを読み込んでくれない・・順番が重要らしい (2001.3.9)

次のようなプログラムを書きました。

-------------------------------------------------------
program test

real alpha, beta

NAMELIST /NM1/ alpha
NAMELIST /NM2/ beta

open(10,file='NAMELISTS')
READ ( 5, NML=NM1 )
READ ( 5, NML=NM2 )
close(10)

end
------------------------------------------------------
------------------------------------------------------
&NM3 gamma=3.0 /
&NM1 alpha=1.0 /
&NM2 beta =2.0 /
-------------------------------------------------------

ネームリストの読み込む順番と、データファイルの並びの順番を
そろえておかないと、コンパイラによってはエラーになります。
サブルーチンの順番を変えたときなどに読み込む順番が変わることが
あるので注意。

どうしても逆順に読むときは、一旦 close して再度 open すればよい。

open(10,file='NAMELISTS')
READ ( 5, NML=NM2 )
close(10)
open(10,file='NAMELISTS')
READ ( 5, NML=NM1 )
close(10)

また、データファイルの最後の/の後で改行しておかないと、エラーが出た。