Related
I use Fortran 90 via Force 2.0, with a 64bits pc under windows 10.
Everytime I compile and execute my code one of my column (the 't' one at the left) that I write in a .dat file changes even though I didn't change my code.
! module
module force_param
implicit none
integer,parameter :: dim=3
integer,parameter :: dim_l=dim-1
real*8,parameter :: rapmas=0.5, omega2=100, fsm2=1.E-4, x0_=0.1E0, x2_ =0.01E0
real*8,parameter :: omeg=10.d0, Aex=0.01E0
contains
subroutine force(x,v,f)
implicit none
real*8 :: x(dim), v(dim), f(dim_l)
f(1)=-omega2*(x(1)-x(3)) - omega2*rapmas*(x(1)-x(2))
f(2)=-omega2*(x(2)-x(1)) - fsm2*(v(1)-v(3))
end subroutine force
end module force_param
! algorithme de heun
subroutine heun(x,v,xn,vn,dt)
use force_param
IMPLICIT NONE
real*8::x( dim),v( dim),xn(dim),vn( dim),dt
real*8::f( dim_l), fn( dim_l)
call force(x,v,f)
xn(1: dim_l)=x( 1 : dim_l)+v( 1 : dim_l )*dt
vn(1:dim_l)=v(1: dim_l)+f( 1 : dim_l )*dt
call force(xn,vn,fn )
xn( 1 : dim_l)=x(1:dim_l)+((v(1:dim_l)+vn(1:dim_l))*.5d0*dt)
vn( 1 : dim_l)=v(1:dim_l)+((f(1:dim_l)+fn(1:dim_l))*.5d0*dt)
end subroutine heun
Thank you, I don't know how to access the debugging options though, I tried the button that says 'debugging' but it doesn't seem to do anything. I initialized all my variables too and nothing changed :
! PROGRAM PRINCIPAL
program vibrations
use force_param
implicit none
integer,parameter :: n=50
integer :: i
real*8 :: dt,pi
real*8:: x(dim),v(dim),xn(dim),vn(dim)
real*8 :: t,tn
pi=3.141592
dt=2*pi/(omeg*n)
x(1)=x0_
x(2)=x2_
x(3)=0
v(:)=0
xn(:)=0
vn(:)=0
t=0
tn=0
do i=0,n*1000
t=i*dt
x(3)=0
v(3)=0
tn=(i+n)*dt
xn(3)=0
vn(3)=0
call heun(x,v,xn,vn,dt)
open(11,file='oscill_libI.dat')
write(11,*)t,xn(1), xn(2)
x(:)=xn(:)
v(:)=vn(:)
enddo
end program vibrations
Here is the beginning of my dat file, sometimes the 't' column goes from 0 to 62, sometimes from 0 to 79 it's like randomized, but the 2 right columns stay the same :
0.0000000000000000 9.88551278186548116E-002 1.07106110228664085E-002
1.25663681030273432E-002 9.54368715187558225E-002 1.28277973235651557E-002
2.51327362060546865E-002 8.98431692414301264E-002 1.62639044499528901E-002
3.76991043090820280E-002 8.22344006522621740E-002 2.08756113538187266E-002
5.02654724121093729E-002 7.28284342737571672E-002 2.64687493992564297E-002
6.28318405151367110E-002 6.18938950672803800E-002 3.28048473194145346E-002
7.53982086181640559E-002 4.97418661274231927E-002 3.96091910096331989E-002
8.79645767211914009E-002 3.67162863528836467E-002 4.65801381808427575E-002
0.10053094482421875 2.31833471408859799E-002 5.33993871027770503E-002
0.11309731292724609 9.52022271687244688E-003 5.97428674724096373E-002
0.12566368103027342 -3.89651034653112948E-003 6.52919008503467374E-002
0.13823004913330078 -1.67023992139428389E-002 6.97442687804509659E-002
0.15079641723632811 -2.85562232108175044E-002 7.28248289506616686E-002
0.16336278533935547 -3.91503348001675106E-002 7.42953335021119959E-002
0.17592915344238280 -4.82199079113247164E-002 7.39631284187664967E-002
0.18849552154541016 -5.55508033296286119E-002 7.16884480472895630E-002
Here is two example of the end of my .dat file, generated from the same code :
62.643344993591306 -2.22234174408908088E-002 -6.79345709753481353E-002
62.655911361694336 7.81763163624332930E-004 -8.43500285728980836E-002
62.668477729797360 2.31009413326696972E-002 -9.94182790176534326E-002
62.681044097900390 4.40812444698733333E-002 -0.11254380214307025
62.693610466003413 6.31170757113199837E-002 -0.12318313084617104
62.706176834106444 7.96689081015940076E-002 -0.13086315388478151
62.718743202209467 9.32799461005491132E-002 -0.13519725211303918
62.731309570312497 0.10359012770409017 -0.13589874559045442
62.743875938415528 0.11034702522217735 -0.13279121399446445
62.756442306518551 0.11341330157177297 -0.12581535178107928
62.769008674621581 0.11277048866797904 -0.11503212942774470
62.781575042724604 0.10851897164177816 -0.10062214936426861
62.794141410827635 0.10087418344945331 -8.28812061224447061E-002
62.806707778930658 9.01591351206557229E-002 -6.22121809658964242E-002
76.566880851745609 -2.17677700529351897E-002 -1.05802168534466483E-002
76.579447219848632 -4.88389979588944381E-002 6.79397900452341150E-003
76.592013587951655 -7.46968241174833236E-002 2.32872848719374026E-002
76.604579956054678 -9.85925322542900023E-002 3.82250728574096491E-002
76.617146324157716 -0.11983644140594378 5.09885339005944488E-002
76.629712692260739 -0.13781930927178770 6.10355204755949760E-002
76.642279060363762 -0.15203121044591339 6.79188940734381658E-002
76.654845428466800 -0.16207729220063621 7.13017843304816629E-002
76.667411796569823 -0.16768990946606288 7.09692653728316025E-002
76.679978164672846 -0.16873675548102926 6.68360694761913066E-002
76.692544532775869 -0.16522473162941959 5.89500848494855997E-002
76.705110900878907 -0.15729943518448128 4.74915192035008926E-002
76.717677268981930 -0.14524028276312445 3.27677494663660557E-002
76.730243637084953 -0.12945142581913546 1.52040161357704148E-002
76.742810005187991 -0.11044874807218197 -4.66974609003714767E-003
76.755376373291014 -8.88433591287705327E-002 -2.62355258305883438E-002
! algorithme de heun
subroutine heun(x,v,xn,vn,dt)
use force_param
IMPLICIT NONE
real*8::x( dim),v( dim),xn(dim),vn( dim),dt
real*8::f( dim_l), fn( dim_l)
call force(x,v,f)
xn(1: dim_l)=x( 1 : dim_l)+v( 1 : dim_l )*dt
vn(1:dim_l)=v(1: dim_l)+f( 1 : dim_l )*dt
call force(xn,vn,fn )
xn( 1 : dim_l)=x(1:dim_l)+((v(1:dim_l)+vn(1:dim_l))*.5d0*dt)
vn( 1 : dim_l)=v(1:dim_l)+((f(1:dim_l)+fn(1:dim_l))*.5d0*dt)
end subroutine heun
! PROGRAM PRINCIPAL
program vibrations
use force_param
implicit none
integer,parameter :: n=50
integer :: i
real*8 :: dt,pi
real*8:: x(dim),v(dim),xn(dim),vn(dim),f( dim_l),fn( dim_l)
real*8 :: t,tn
pi=3.141592
dt=2*pi/(omeg*n)
x(1)=x0_
x(2)=x2_
x(3)=0
v(:)=0
xn(:)=0
vn(:)=0
t=0
tn=0
f(:) = 0
fn(:) = 0
do i=0,n*1000
t=i*dt
x(3)=0
v(3)=0
tn=(i+n)*dt
xn(3)=0
vn(3)=0
call heun(x,v,xn,vn,dt)
open(11,file='oscill_lib.dat')
write(11,'(f15.8,1x,f15.8,1x,f15.8)')t,xn(1), xn(2)
x(:)=xn(:)
v(:)=vn(:)
enddo
close(11)
end program vibrations
I misunderstood your issue: You have to close your file. Use close(11) before terminating your program. That way you make sure your output is saved fully.
And, as mentioned in the comments. You should open the file only once. Or close it and the open again for appending if necessary.
I am creating a linked list from the input file. Each node in the linked list includes the information in each block of the input.
When I try to get value from input file, I assigned multiple lines (4 lines each time) into a 1D array "tmp". I think the DO-loop I used in the "input:DO" loop is wrong. But I do not know how to solve.
I use gfortran to compile and no mistake come out for compiling.
I have tried WRITE for testing in the "input:DO" loop for testing. The result shows that I can open the input file successfully.
PROGRAM read
IMPLICIT NONE
INTEGER,PARAMETER :: nat=4
character(len=20) :: filename
!Derived types to store atom data
TYPE :: atom
CHARACTER(LEN=2) :: atom_name
REAL, DIMENSION(3) :: coord
END TYPE atom
!The array info stores info of all atom in one time step
type :: atom_seq
type(atom),dimension(nat):: info
type(atom_seq),pointer :: p
end type atom_seq
TYPE (atom_seq), POINTER :: head
TYPE (atom_seq), POINTER :: tail
type(atom), dimension(nat) :: temp
! Declare variable
INTEGER :: istat
INTEGER :: i=0, n=0
! Open input data file
WRITE(*,*) 'ENTER the file name with the data to be read: '
READ(*,'(A20)') filename
NULLIFY(head)
OPEN( UNIT=9, FILE=TRIM(filename), STATUS="OLD", ACTION="READ", IOSTAT=istat)
! Was the open successful
fileopen: IF (istat == 0) THEN
input: DO
!WRITE(*,*) "OPEN done " ! for testing
READ(9,*) ! <--when run, error is in this line
READ(9,*)
DO i = 1, nat
READ(9,*,IOSTAT=istat) temp(i)%atom_name, temp(i)%coord(1), temp(i)%coord(2), temp(i)%coord(3)
ENDDO
IF (istat /= 0) EXIT
n = n + 1 ! Bump count
IF (.NOT. ASSOCIATED(head) ) THEN ! No values in list
ALLOCATE(head, STAT=istat) ! Allocate new value
tail => head ! Tail points to new value
NULLIFY(tail%p) ! Nullify p in new value
DO i = 1, nat ! Store number
tail%info(i)%atom_name = temp(i)%atom_name
tail%info(i)%coord(1) = temp(i)%coord(1)
tail%info(i)%coord(2) = temp(i)%coord(2)
tail%info(i)%coord(3) = temp(i)%coord(3)
ENDDO
ELSE ! Values already in list
ALLOCATE(tail%p, STAT=istat) ! Allocate new value
tail => tail%p
NULLIFY(tail%p)
DO i = 1, nat ! Store number
tail%info(i)%atom_name = temp(i)%atom_name
tail%info(i)%coord(1) = temp(i)%coord(1)
tail%info(i)%coord(2) = temp(i)%coord(2)
tail%info(i)%coord(3) = temp(i)%coord(3)
ENDDO
END IF
END DO input
ELSE fileopen
WRITE(*,1030) istat
1030 FORMAT ('File open failed --status = ', I6)
END IF fileopen
END PROGRAM read
The input file: inp
4
Particles:1_0
O 0.8050005000 0.7000000000 3.2350000000
H 1.4750005000 1.2800000000 2.8650000000
H 0.8550005000 -0.0900000000 2.7150000000
O 0.4050005000 0.7500000000 -4.1350000000
4
Particles:1_5
O 0.8799478358 0.6383317306 3.1569568025
H 1.4046592860 1.2232485826 2.4978364665
H 1.1472783731 -0.2687458123 3.0229092093
O 0.5392992531 0.6047144782 -4.0811918365
4
Particles:1_10
O -3.8021765454 3.1600783692 -4.5455655916
H -4.5320715486 3.0937504111 4.9514896261
H -3.5088238380 4.0613340230 -4.5394597924
O -3.3469012765 -0.7064128847 1.2465212113
and the error is
hg#xi /home/hg/pole $ ./read
ENTER the file name with the data to be read:
inp
At line XXX of file read.f95 (unit = 9, file = 'inp')
Fortran runtime error: End of file
Error termination. Backtrace:
#0 0x7f1c1fdbb31a
#1 0x7f1c1fdbbec5
#2 0x7f1c1fdbc68d
#3 0x7f1c1ff32a33
#4 0x7f1c1ff364b7
#5 0x7f1c1ff365b8
#6 0x5566d3dc9daf
#7 0x5566d3dca9ed
#8 0x7f1c1f9d0b96
#9 0x5566d3dc9a79
#10 0xffffffffffffffff
I hope to fix the problem. If my idea is wrong, please give some suggestions to design a better data structure to save data in the input (The input file may have thousands of blocks, instead of 3. It is big and the number of blocks is unknown before running the code. )
There is a test on the IOSTAT missing with the first executed READ in the loop. When the result is not OK the loop can be terminated e.g. change:
fileopen: IF (istat == 0) THEN
input: DO
!WRITE(*,*) "OPEN done " ! for testing
READ(9,*) ! <--when run, error is in this line
READ(9,*)
in
fileopen: IF (istat == 0) THEN
input: DO
!WRITE(*,*) "OPEN done " ! for testing
READ(9,*,IOSTAT=istat)
IF (istat /=0) EXIT
READ(9,*)
The following 3D complex FFT Fortran MPI program runs when the number of processes P = 1, 2, 4, 8, and 16. But, it fails when P = 12 with the following error at the place where function fftw_mpi_plan_dft_3d is called.
"Program received signal SIGSEGV: Segmentation fault - invalid memory reference."
Why does this program fail with P = 12?
PROGRAM test
USE, INTRINSIC :: ISO_C_BINDING
IMPLICIT NONE
INCLUDE 'mpif.h'
INCLUDE 'fftw3-mpi.f03'
INTEGER :: ier
integer(C_INTPTR_T), parameter :: L = 256
integer(C_INTPTR_T), parameter :: N = 256
integer(C_INTPTR_T), parameter :: M = 48
type(C_PTR) :: plan, cdata
complex(C_DOUBLE_COMPLEX), pointer :: data(:,:,:)
integer(C_INTPTR_T) :: i, j, alloc_local, local_M, local_j_offset
CALL MPI_INIT(ier)
CALL fftw_mpi_init
alloc_local = fftw_mpi_local_size_3d(M, N, L, MPI_COMM_WORLD, local_M, local_j_offset)
cdata = fftw_alloc_complex(alloc_local)
CALL C_F_POINTER(cdata, data, [L, N, local_M])
plan = fftw_mpi_plan_dft_3d(M, N, L, data, data, MPI_COMM_WORLD, FFTW_FORWARD, FFTW_ESTIMATE)
CALL fftw_destroy_plan(plan)
CALL fftw_free(cdata)
CALL MPI_FINALIZE(ier)
STOP
END PROGRAM test
I see the same problem with fftw_mpi_plan_dft_c2r on a Cray XE6 using cray-mpich 7.1.3. It doesn't matter if fftw is compiled with gcc, Cray cc, or Intel icc. The segmentation fault only occurs for certain combinations of array dimensions and number of processors. One combination that results in a segfault is array size 1920,3840,3840 on 128 processes. As with the original post, I used FFTW_ESTIMATE and so no transform is done by the planner. Stack trace follows.
#0 0x00000000008f26f0 in ?? ()
#1 0x00000000004179bb in fftw_plan_destroy_internal (ego=0x8e2af0) at plan.c:49
#2 0x000000000040d755 in mkplan (ego_=0x8def60, p_=0x8f1440, plnr=0x8d6110)
at transpose-pairwise.c:467
#3 0x0000000000418a07 in invoke_solver (ego=0x8d6110, p=0x8f1440, s=0x8def60,
nflags=0x7fffffff4fc0) at planner.c:486
#4 0x0000000000418b9c in search0 (ego=0x8d6110, p=0x8f1440, slvndx=0x7fffffff4fcc,
flagsp=0x7fffffff4fc0) at planner.c:529
#5 0x0000000000418db8 in search (ego=0x8d6110, p=0x8f1440, slvndx=0x7fffffff4fcc,
flagsp=0x7fffffff4fc0) at planner.c:600
#6 0x00000000004191d8 in mkplan (ego=0x8d6110, p=0x8f1440) at planner.c:711
#7 0x0000000000419d35 in fftw_mkplan_d (ego=0x8d6110, p=0x8f1440) at planner.c:970
#8 0x0000000000411d47 in mkplan (ego_=0x8df140, p_=0x8df930, plnr=0x8d6110)
at dft-rank1-bigvec.c:165
#9 0x0000000000418a07 in invoke_solver (ego=0x8d6110, p=0x8df930, s=0x8df140,
nflags=0x7fffffff5290) at planner.c:486
#10 0x0000000000418b9c in search0 (ego=0x8d6110, p=0x8df930, slvndx=0x7fffffff529c,
flagsp=0x7fffffff5290) at planner.c:529
#11 0x0000000000418db8 in search (ego=0x8d6110, p=0x8df930, slvndx=0x7fffffff529c,
flagsp=0x7fffffff5290) at planner.c:600
#12 0x00000000004191d8 in mkplan (ego=0x8d6110, p=0x8df930) at planner.c:711
#13 0x0000000000419d35 in fftw_mkplan_d (ego=0x8d6110, p=0x8df930) at planner.c:970
#14 0x00000000004157b1 in mkplan (ego_=0x8df4a0, p_=0x8df650, plnr=0x8d6110)
at rdft2-rank-geq2.c:179
#15 0x0000000000418a07 in invoke_solver (ego=0x8d6110, p=0x8df650, s=0x8df4a0,
nflags=0x7fffffff5550) at planner.c:486
#16 0x0000000000418b9c in search0 (ego=0x8d6110, p=0x8df650, slvndx=0x7fffffff555c,
flagsp=0x7fffffff5550) at planner.c:529
#17 0x0000000000418db8 in search (ego=0x8d6110, p=0x8df650, slvndx=0x7fffffff555c,
flagsp=0x7fffffff5550) at planner.c:600
#18 0x00000000004191d8 in mkplan (ego=0x8d6110, p=0x8df650) at placner.c:711
#19 0x000000000041d1e2 in mkplan0 (plnr=0x8d6110, flags=64, prb=0x8df650, hash_info=0,
wisdom_state=WISDOM_NORMAL) at apiplan.c:34
#20 0x000000000041d229 in mkplan (plnr=0x8d6110, flags=64, prb=0x8df650, hash_info=0)
at apiplan.c:48
#21 0x000000000041d438 in fftw_mkapiplan (sign=0, flags=0, prb=0x8df650)
at apiplan.c:111
#22 0x000000000040a385 in plan_guru_rdft2 (rnk=3, dims0=0x8df540, howmany=1,
r=0x2aab74c21010, c=0x2aab73000010, comm=1140850688, kind=HC2R00, flags=64)
at api.c:784
#23 0x000000000040a542 in fftw_mpi_plan_many_dft_c2r (rnk=3, n=0x7fffffff5830,
howmany=1, iblock=0, oblock=0, in=0x2aab73000010, out=0x2aab74c21010,
comm=1140850688, flags=64) at api.c:831
I am using an old fortran program given to me to open a netcdf file, read its contents, perform some calculations and interpolation, and write the data to another file format. I have very little experience in fortran, so please any help would be deeply appreciated.
The program is compiled successfully:
ifort -c -CB -CU -ftrapuv -par_report0 -vec_report0 -heap-arrays -O0 -stand f90 -check all -traceback -fstack-protector -assume protect_parens -implicitnone -debug -gen-interfaces -check arg_temp_created -ftrapuv -g -convert big_endian -I/opt/cray/netcdf/4.3.0/INTEL/130/include/ CAM_netcdf_to_WRF_intermediate.f90 ; ifort CAM_netcdf_to_WRF_intermediate.o -L/opt/cray/netcdf/4.3.0/INTEL/130/lib -lnetcdf -lnetcdff
The program crashes, running out of bounds while trying to read in the netcdf file:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7657d33 in nf_open_ (A1=0x18 <Address 0x18 out of bounds>, A2=0x4e04bc <__NLITPACK_19>,
A3=0x7fffffff90ec, C1=128) at fort-control.c:27
27 fort-control.c: No such file or directory.
Running GDB, using 'bt full':
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7657d33 in nf_open_ (A1=0x18 <Address 0x18 out of bounds>, A2=0x4e04bc <__NLITPACK_19>,
A3=0x7fffffff90ec, C1=128) at fort-control.c:27
27 fort-control.c: No such file or directory.
(gdb) bt full
#0 0x00007ffff7657d33 in nf_open_ (A1=0x18 <Address 0x18 out of bounds>,
A2=0x4e04bc <__NLITPACK_19>, A3=0x7fffffff90ec, C1=128) at fort-control.c:27
B1 = 0x0
B3 = 5113020
#1 0x00007ffff76630ac in NETCDF::nf90_open (
path=<error reading variable: Cannot access memory at address 0x18>, mode=0, ncid=-858993460,
chunksize=<error reading variable: Cannot access memory at address 0x0>,
cache_size=<error reading variable: Cannot access memory at address 0x0>,
cache_nelems=<error reading variable: Cannot access memory at address 0x0>,
cache_preemption=<error reading variable: Cannot access memory at address 0x0>,
comm=<error reading variable: Cannot access memory at address 0x0>,
info=<error reading variable: Cannot access memory at address 0x0>, .tmp.PATH.len_V$ffc=128)
at netcdf4_file.f90:64
nf90_open = -144388088
ret = 0
preemption_out = 0
nelems_out = -1
size_out = 0
preemption_in = 32767
nelems_in = -134664192
size_in = 32767
The program is below:
program CAM_netcdf_to_WRF_intermediate
use netcdf
implicit none
! Declarations:
integer, parameter :: outfile_diagnostics = 16
integer, parameter :: infile_CAM_files_and_dates = 15
character(len=24) :: HDATE
! dimensions:
integer, parameter :: nx_CAM=288,ny_CAM=192,nz_CAM=26 &
,nfields=5,nfields2d=9,nfields2d_to_read=5 &
,nz_soil=4,nz_CLM=1,nfields_soil=2
integer, parameter :: nz_WRF=38
character(len=128) :: netcdf_cam_filename,netcdf_clm_filename,netcdf_pop_filename
character(len=128) :: netcdf_ice_filename
integer :: iEOF
logical :: EOF
! open outpuf log file:
open(outfile_diagnostics,form='formatted',file="Output/CCSM2WRF.log")
! read the first date and netcdf file name from the input file:
open(infile_CAM_files_and_dates,form='formatted',file="Input/CCSM2WRF.input")
read(infile_CAM_files_and_dates,*,iostat=iEOF) netcdf_cam_filename,netcdf_clm_filename,&
netcdf_pop_filename,netcdf_ice_filename,hdate
if (iEOF<0) then;
print *, "EOF True"
EOF=.true.;
else;
print *, "EOF False"
EOF=.false.;
end if
call dummy_read(nz_WRF,hdate,outfile_diagnostics,netcdf_cam_filename &
,netcdf_clm_filename,netcdf_pop_filename &
,netcdf_ice_filename,nx_CAM,ny_CAM,nz_CAM)
stop
end program CAM_netcdf_to_WRF_intermediate
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SUBROUTINE HANDLE_ERR(STATUS)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
use netcdf
implicit none
INTEGER STATUS
IF (STATUS .NE. NF90_NOERR) THEN
PRINT *, NF90_STRERROR(STATUS)
STOP 'Stopped'
ENDIF
END SUBROUTINE HANDLE_ERR
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Subroutine dummy_read &
(nz_WRF,outfile_diagnostics,netcdf_cam_filename &
,netcdf_clm_filename,netcdf_pop_filename,netcdf_ice_filename &
,nx_CAM,ny_CAM,nz_CAM)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
use netcdf
implicit none
integer :: nz_WRF
integer :: nx_CAM,ny_CAM,nz_CAM
character(len=128) :: filename
character(len=24) :: HDATE
integer :: outfile_diagnostics
integer :: STATUS, NCID, NCID_clm, NCID_pop, NCID_ice
character(len=128) :: netcdf_cam_filename, netcdf_clm_filename, netcdf_pop_filename
character(len=128) :: netcdf_ice_filename
! open output files for metgrid in WRF/WPS intermediate format:
write(filename,'("Output/FILE:",A13)') hdate(1:13)
write(outfile_diagnostics,*) "output intermediate file filename=",filename
open(10,form='unformatted',file=filename)
write(filename,'("Output/SST:",A13)') hdate(1:13)
write(outfile_diagnostics,*) "output intermediate SST file filename=",filename
open(11,form='unformatted',file=filename)
STATUS = NF90_OPEN(netcdf_cam_filename, 0, NCID)
! STATUS = NF90_OPEN(path = "Inputdata/ind/cam_CCSM4_historical_197909-197912-1979090100.nc", mode= 0, ncid = NCID)
IF (STATUS .NE. NF90_NOERR) CALL HANDLE_ERR(STATUS)
print *, "first status conditional statement"
STATUS = NF90_OPEN(netcdf_clm_filename, 0, NCID_clm)
IF (STATUS .NE. NF90_NOERR) CALL HANDLE_ERR(STATUS)
STATUS = NF90_OPEN(netcdf_pop_filename, 0, NCID_pop)
IF (STATUS .NE. NF90_NOERR) CALL HANDLE_ERR(STATUS)
STATUS = NF90_OPEN(netcdf_ice_filename, 0, NCID_ice)
IF (STATUS .NE. NF90_NOERR) CALL HANDLE_ERR(STATUS)
status=NF90_CLOSE(NCID)
status=NF90_CLOSE(NCID_clm)
status=NF90_CLOSE(NCID_pop)
status=NF90_CLOSE(NCID_ice)
print *, "Leaving dummy, going to MAIN"
return
end Subroutine dummy_read
The open statement works if I hard-code the path of the netcdf file (see the commented out line within the dummy_read subroutine). Printing out the netcdf_cam_filename within main returns a valid string, however printing out the string within the dummy_read subroutine returns an empty string. I am uncertain why the netcdf_cam_filename string is not making it into the subroutine correctly.
Please ask if you need additional information. I only posted pieces of the code that I think applies to the error. Thanks in advance.
Your subroutine call is mismatched to the actual definition.
Your call to dummy_read is:
call dummy_read(nz_WRF,hdate,outfile_diagnostics,netcdf_cam_filename &
,netcdf_clm_filename,netcdf_pop_filename &
,netcdf_ice_filename,nx_CAM,ny_CAM,nz_CAM)
While your declaration of dummy_read is:
Subroutine dummy_read &
(nz_WRF,outfile_diagnostics,netcdf_cam_filename &
,netcdf_clm_filename,netcdf_pop_filename,netcdf_ice_filename &
,nx_CAM,ny_CAM,nz_CAM)
Or shown a different way:
call dummy_read(nz_WRF,hdate, outfile_diagnostics,netcdf_cam_filename,netcdf_clm_filename,netcdf_pop_filename,netcdf_ice_filename,nx_CAM,ny_CAM,nz_CAM)
Subroutine dummy_read(nz_WRF,outfile_diagnostics,netcdf_cam_filename,netcdf_clm_filename,netcdf_pop_filename,netcdf_ice_filename,nx_CAM, ny_CAM,nz_CAM)
Which results in an argument mismatch. The dummy argument outfile_diagnostics is associated with the actual argument hdate and so on. You are passing 10 arguments to a subroutine declared to take 9.
You might wonder why the compiler produced an executable in such a case rather than producing an error. This is because you are calling the procedure with an implicit interface and Fortran trusts you to do the right thing. Fortran can detect argument mismatches but to do so you need to provide an explicit interface. Aside from explicitly declaring the interface, the easiest ways to do this are to either make the procedure a module procedure (by putting the subroutine into a module) or an internal procedure (by putting the procedure in the main program after a contains statement).
You can also ask the compiler to provide high levels of warnings to avoid this problem. Compiling with gfortran with -Wall produces this warning with your code:
call dummy_read(nz_WRF,hdate,outfile_diagnostics,netcdf_cam_filename &
1
Warning: Type mismatch in argument 'outfile_diagnostics' at (1); passed CHARACTER(1) to INTEGER(4)
Adiitionally, ifort provides the option -gen-interfaces flag that will automatically generate modules to contain external procedures. I would however consider this a tool to help port code to newer language standards than something to rely on.
I'm new to fortran and I'm trying to write and compile an easy example that I found in this address to read a simple 2 column text file:
Date Water-flow
717976 7.140
717977 6.570
717978 6.040
717979 5.780
717980 5.530
The program is pasted below, but when trying to compile and run I don't really understand the resulting error, could you please advice me?
PROGRAM READHPOP
IMPLICIT NONE
INTEGER, PARAMETER :: lun = 10
INTEGER :: res,i
CHARACTER(len=80) :: cbuffer
INTEGER :: flength
INTEGER,ALLOCATABLE,DIMENSION(:) :: dates
REAL,ALLOCATABLE,DIMENSION(:) :: water_flow
INTEGER :: c_position,string_length
OPEN(UNIT=lun,FILE="st.dat",FORM="FORMATTED",IOSTAT=res)
IF(res/=0) THEN
PRINT*,'error in opening file, status: ',res
STOP
END IF
READ(UNIT=lun,FMT='(A)',IOSTAT=res) cbuffer
IF(res /=0) THEN
PRINT *,'ERROR IN READING file, status: ',res
CLOSE(UNIT=lun)
STOP
END IF
string_length=LEN_TRIM(cbuffer)
c_position=INDEX(cbuffer,':')
READ(cbuffer(c_position+1:string_length),FMT='(A10)') flength
ALLOCATE(dates(flength),STAT=res)
IF (res/=0) THEN
PRINT*,'ERROR IN ALLOCATING MEMORY, status:',res
CLOSE(UNIT=lun)
STOP
END IF
READ(UNIT=lun,FMT='(A)',IOSTAT=res) cbuffer
DO i=1,cbuffer
READ(UNIT=lun,FMT='(I6,1X,F6.3)') dates(i),water_flow(i)
PRINT*,'DIAS ',dates(i)
END DO
end PROGRAM readhpop
EDIT: thanks to all for the inputs, just to close the issue, I paste below the working code, regards!
PROGRAM READHPOP
IMPLICIT NONE
INTEGER, PARAMETER :: lun = 10
INTEGER :: res,i
CHARACTER(len=80) :: cbuffer
INTEGER :: flength
INTEGER,ALLOCATABLE,DIMENSION(:) :: dates
REAL,ALLOCATABLE,DIMENSION(:) :: water_flow
INTEGER :: c_position,string_length
OPEN(UNIT=lun,FILE="st.dat",FORM="FORMATTED",IOSTAT=res)
IF(res/=0) THEN
PRINT*,'error in opening file, status: ',res
STOP
END IF
READ(UNIT=lun,FMT='(A)',IOSTAT=res) cbuffer
IF(res /=0) THEN
PRINT *,'ERROR IN READING file, status: ',res
CLOSE(UNIT=lun)
STOP
END IF
string_length=LEN_TRIM(cbuffer)
c_position=INDEX(cbuffer,':')
READ(cbuffer(c_position+1:string_length),FMT='(I10)') flength
ALLOCATE(dates(flength),water_flow(flength),STAT=res)
IF (res/=0) THEN
PRINT*,'ERROR IN ALLOCATING MEMORY, status:',res
CLOSE(UNIT=lun)
STOP
END IF
DO i=1,flength
READ(UNIT=lun,FMT='(I6,F6.3)') dates(i),water_flow(i)
PRINT*,'LINE OF FILE',i,' DAYS ',dates(i),' WATERFLOW ',water_flow(i)
END DO
PAUSE
END PROGRAM READHPOP
Okay, here is a working version:
PROGRAM READHPOP
IMPLICIT NONE
INTEGER, PARAMETER :: lun = 10
INTEGER :: res,i
CHARACTER(len=80) :: cbuffer
INTEGER :: flength
INTEGER,ALLOCATABLE,DIMENSION(:) :: dates
REAL,ALLOCATABLE,DIMENSION(:) :: water_flow
INTEGER :: c_position,string_length
OPEN(UNIT=lun,FILE="st.dat",FORM="FORMATTED",IOSTAT=res)
IF(res/=0) THEN
PRINT*,'error in opening file, status: ',res
STOP
END IF
READ(UNIT=lun,FMT='(A)',IOSTAT=res) cbuffer
IF(res /=0) THEN
PRINT *,'ERROR IN READING file, status: ',res
CLOSE(UNIT=lun)
STOP
END IF
string_length=LEN_TRIM(cbuffer)
c_position=INDEX(cbuffer,':')
READ(cbuffer(c_position+1:string_length),FMT='(I10)') flength
ALLOCATE(dates(flength),water_flow(flength),STAT=res)
IF (res/=0) THEN
PRINT*,'ERROR IN ALLOCATING MEMORY, status:',res
CLOSE(UNIT=lun)
STOP
END IF
DO i=1,flength
READ(UNIT=lun,FMT='(I6,F6.3)') dates(i),water_flow(i)
PRINT*,'DIAS ',dates(i)
END DO
end PROGRAM readhpop
Issues:
water_flow needs to be allocated
When determining flength: flength is an integer, so read it as an integer (here: '(I10)'). The number of dates is expected after a colon, so change st.dat to:
Date Water-flow: 5
717976 7.140
717977 6.570
717978 6.040
717979 5.780
717980 5.530
Loop over flength instead of cbuffer
One read statement too much - you are trying to read beyond the end of the file...
In this line
READ(cbuffer(c_position+1:string_length),FMT='(A10)') flength
you read an integer value under a character edit descriptor. This may well return a value in flength which exceeds any reasonable value for your allocatable array. For example, in a quick test I got a value of 538981169. Change that line to
READ(cbuffer(c_position+1:string_length),'(i)') flength