I am having an issue with a Fortran program where the program is written to evaluate local density of sample data. In my original program, n_grid is actually calculated using L and ds values. I have tried changing the values of the 'ds' and 'n_grid', and at the end concluded that when the value of 'n_grid' is 20 or greater, the program runs perfectly but if 'n_grid' is 19 or less, the program runs upto line 67 but if i try to write the data to an output file at line 69, it crashes and gives a "malloc(): corrupted top size" error. How can I fix this issue and what could be causing it?
program localdensity
implicit none
integer :: i , j , k , a , b , n_grid , n , nc , p , q , r , ncin1
double precision :: L , ds , ignore
double precision, allocatable :: dn(:,:,:)
double precision, allocatable :: rco(:,:)
! INPUT PARAMETERS !
ncin1 = 14 ! total configurations in each block file
nc = ncin1*1 ! total block files
ds = 3d0 ! dx = dy = dz = 2 A
! INPUT PARAMETERS !
print*, 'started reading data file'
open (1, file = 'Silica_13824_negative_press-11_100.txt', status = 'old')
read(1,*) n , ignore , L
close(1)
print* , n , L
L = 59.99999d0
!L = 60.0001d0
allocate(rco(3,nc*n))
n_grid = 19
allocate(dn(n_grid , n_grid , n_grid))
open (1, file = 'Silica_13824_negative_press-11_100.txt', status = 'old')
do j = 1 , nc
read(1,*)
do i=1,n
k=i+(j-1)*n
read(1,*) a , b , rco(1,k), rco(2,k), rco(3,k) ! reading data (x,y,z)
end do
end do
print*, 'ended reading data file n :' , n , L
close(1)
rco(:,:) = rco(:,:) - L*floor(rco(:,:)/L) ! Appying Periodic Boundary condition
dn = 0
print*
do i = 1,nc
do j = 1,n
p = floor( rco(1 , (i-1)*n + j) / ds) + 1
q = floor( rco(2 , (i-1)*n + j) / ds) + 1
r = floor( rco(3 , (i-1)*n + j) / ds) + 1
dn(p,q,r) = dn(p,q,r) + 1
end do
if (mod(i,ncin1)==0) print*, 'block', i/ncin1, 'done'
end do
print*, 'total particles counted :', sum(dn) ,'; n*nc = ', n*nc
print*, 'nc is ' , nc
dn = dn/nc
print*, sum(dn)
print*,'n_grid' ,n_grid
open (unit = 2, file = "parameters_neg.txt" , action="write" , status="replace")
write(2,*) n , nc , L , n_grid
close(2)
open (unit = 3, file = "data_from_program_neg.txt" , action="write" , status="replace")
do i = 1 , n_grid
do j = 1 , n_grid
do k = 1 , n_grid
write(3,*) i , j , k , dn(i,j,k)
end do
end do
end do
close(3)
print*, 'Program has computed everything, data has been saved to data_from_program.txt. Now run file named l ocal_density.py '
endprogram localdensity
When i run it :
started reading data file
13824 57.988311117244002
ended reading data file n : 13824 59.999989999999997
block 1 done
total particles counted : 183355.00000000000 ; n*nc = 193536
nc is 14
13096.785714285699
n_grid 19
malloc(): corrupted top size
Program received signal SIGABRT: Process abort signal.
Backtrace for this error:
#0 0x7f7360b16d21 in ???
#1 0x7f7360b15ef5 in ???
#2 0x7f736094708f in ???
at /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
#3 0x7f736094700b in __GI_raise
at ../sysdeps/unix/sysv/linux/raise.c:51
#4 0x7f7360926858 in __GI_abort
at /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:79
#5 0x7f736099126d in __libc_message
at ../sysdeps/posix/libc_fatal.c:155
#6 0x7f73609992fb in malloc_printerr
at /build/glibc-SzIz7B/glibc-2.31/malloc/malloc.c:5347
#7 0x7f736099c6b9 in _int_malloc
at /build/glibc-SzIz7B/glibc-2.31/malloc/malloc.c:4107
#8 0x7f736099e153 in __GI___libc_malloc
at /build/glibc-SzIz7B/glibc-2.31/malloc/malloc.c:3058
#9 0x7f7360b161d8 in ???
#10 0x7f7360d61df4 in ???
#11 0x7f7360d57300 in ???
#12 0x7f7360d57ccc in ???
#13 0x561eddba1a54 in ???
#14 0x561eddba1e89 in ???
#15 0x7f7360928082 in __libc_start_main
at ../csu/libc-start.c:308
#16 0x561eddba017d in ???
#17 0xffffffffffffffff in ???
Aborted (core dumped)
Integer :: i , k
Integer , parameter :: Br_sn_cvo = 10
Integer , parameter