バグの例

コンパイルエラー (Compile error)

実行時エラー (Runtime error)

割付け配列(その1)

プログラム: sonde_vpt.f90

program sonde_vpt
! Description:
!
! Host: aofd165.fish.nagasaki-u.ac.jp
! Directory: /work2/kunoki/to_manda_sensei/Tools
!
! Revision history:
!  This file is created by /usr/local/mybin/nff.sh at 14:41 on 10-13-2014.
  character(len=1000)::strm
  character(len=500)::infle,ofle
  real,allocatable,dimension(:)::z,t,p,rh
  real,allocatable,dimension(:)::vpt, dvptdz
  real,parameter::rmiss=-999.9
  namelist /para/infle,ofle
  read(*,nml=para)
  open(11,file=infle,action="read")
  read(11,*)
  read(11,'(A)'),strm
  is=index(strm,"=")+1
  read(strm(is:),*)nd
  print *,'nd=',nd
  allocate(z(nd),t(nd),p(nd),rh(nd),vpt(nd))
  do i=1,nd
    read(11,*)z(i),t(i),p(i),rh(i)
  enddo !i
  close(11)
  vpt(:)=rmiss
  do i=1,nd
    tk=t(i)+273.15
    call virtual_potential_temperature(vpt(i), tk, p, rh)
  enddo !i
  dvptdz(:)=rmiss
  do i=2,nd-1
    dvptdz(i)=(vpt(i+1)-vpt(i-1))/(z(i+1)-z(i-1))
  enddo !i
  open(21,file=ofle)
  write(21,'(A,A)')'# Input: ',trim(infle)
  write(21,'(A)')'# z[m], T[degC], p[hPa], RH[%], VPT[K], dVPTdz[K/m]'
  do i=1,nd
    write(21,'(f8.1,2f10.2,f7.1,f9.2)')&
&   z(i),t(i),p(i),RH(i),VPT(i),dvptdz(i)
  enddo !i
  write(*,'(A,A)')'Input : ',trim(infle)
  write(*,'(A,A)')'Output: ',trim(ofle)
end program sonde_vpt
subroutine virtual_potential_temperature(vpt, tk, p, rh)
!
  implicit none
  real,intent(out)::vpt
! pt: virtual potential temperature (kelvin)
   real,intent(in)::tk,p,rh
! tk: absolute temperature (kelvin)
! p : pressure (hPa)
! rh: Relative Humidity (%)
  real,parameter::  kappa=287.0/1004.0
! Rd=287 J K-1 kg-1
! Cp=1004 J K-1 kg-1
  real,parameter::p0=1000.0
! p0: pressue (hPa)
  real pt !Potential Temp.
  real tc !air temp. in Celsius
  real es !saturated water vapor pressure
  real e  !water vapor pressure
  real w  !Mixing Ratio
  pt=tk*(p0/p)**kappa
! saturated water vapor pressure by Tetens (1930)'s formulae
  tc=tk-273.15
  es=611.0/100.*10**((7.5*tc)/(237.3+tc)) ![hPa] 
  e=rh/100*es
! water vapor mixing ratio in kg/kg
  w=0.622*e/(p-e)
  vpt=pt*(1.0 + 0.61*w)
end subroutine virtual_potential_temperature

実行用シェルスクリプト:sonde_vpt.run.sh

#!/bin/bash
# Description:
#
# Host: aofd165.fish.nagasaki-u.ac.jp
# Directory: /work2/kunoki/to_manda_sensei/Tools/Sonde.Prof.Thermo
#
# Revision history:
#  This file is created by /usr/local/mybin/nbscr.sh at 15:18 on 10-13-2014.
echo "Shell script, $(basename $0) starts."
echo
exe=sonde_vpt
namelist=$exe.namelist.txt
indir=proc1
odir=output
mkdir -vp $indir
mkdir -vp $odir
i=1
n=19
while [ $i -le $n ]; do
no=$(printf %02d $i)
cat <<EOF >$namelist
&para
infle="${indir}/stn${no}.raw.T.P.RH.txt"
ofle="${odir}/stn${no}.vpt.txt"
&end
EOF
${exe} < $namelist
i=$(expr $i + 1)
done #i
echo "Done $0"
echo

コンパイル時に、-tracebackオプションをつける(ifortの場合)

$ sonde_vpt.run.sh

forrtl: 致命的なエラー (174): SIGSEGV、segmentation fault occurred

Image PC Routine Line Source

sonde_vpt 0000000000403B2C MAIN__ 48 sonde_vpt.f90

sonde_vpt 0000000000402E7C Unknown Unknown Unknown

libc.so.6 0000003E8241D9F4 Unknown Unknown Unknown

sonde_vpt 0000000000402D89 Unknown Unknown Unknown

割付け配列(その2)

alloc_test: エラーとなる例

alloc_test2: 正常に動作する例

program alloc_test
! Description:
!
! Author: aym
!
! Host: aofd30
! Directory: /work1/aym/11.Work10/15.HeatBudjet/51.jcope2/32.NCEP_FLUX/11.test_read_flux/src
!
! Revision history:
!  2011-02-24 16:13
!    Initial Version
!  use
!  implicit none
  real,allocatable :: var(:,:)
  write(*,'(a)')'Program alloc_test starts.'
!  write(*,*)''
  call sub(var)
  write(*,'(a)')'Done program alloc_test.'
  write(*,*)
end program alloc_test
subroutine sub(var)
  real,allocatable :: var(:,:)
  allocate(var(2,2))
end subroutine sub

$ ifort -CB -traceback -o alloc_test alloc_test.f90

$ alloc_test

Program alloc_test starts.

forrtl: severe (174): SIGSEGV, segmentation fault occurred

Image PC Routine Line Source

alloc_test 0000000000402B75 MAIN__ 20 alloc_test.f90

alloc_test 0000000000402ACC Unknown Unknown Unknown

libc.so.6 0000003F6241D994 Unknown Unknown Unknown

alloc_test 00000000004029D9 Unknown Unknown Unknown

program alloc_test2
! Description:
!
! Author: aym
!
! Host: aofd30
! Directory: /work1/aym/11.Work10/15.HeatBudjet/51.jcope2/32.NCEP_FLUX/11.test_read_flux/src
!
! Revision history:
!  2011-02-24 16:13
!    Initial Version
!  use
!  implicit none
  real,allocatable :: var(:,:)
  write(*,'(a)')'Program alloc_test starts.'
!  write(*,*)''
  allocate(var(2,2))
  call sub(var)
  write(*,'(a)')'Done program alloc_test.'
  write(*,*)
end program alloc_test2
subroutine sub(var)
  real var(:,:)
end subroutine sub

$ ifort -CB -traceback -o alloc_test2 alloc_test2.f90

$ alloc_test2

Program alloc_test starts.

Done program alloc_test.

論理エラー (Logical Error)