LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   calling multiple files in FORTRAN90 (https://www.linuxquestions.org/questions/programming-9/calling-multiple-files-in-fortran90-841112/)

vjramana 10-29-2010 03:30 AM

calling multiple files in FORTRAN90
 
Kindly I request a help on my FORTRAN90 code. I have written FORTRAN code to calculate density profile. This code will open two files ( topology and x,y,z trajectory). The trajectory file contains XYZ data coordinates for multiple frames (about 20 or 100 frames for instance). One frame contains 20736 data.

Previously this code was written to open ONLY one input XYZ data file and do the calculation and would dump the results in separate file and it was running without problem. Recently my data file get bigger in size, so I decided to break the single XYZ data file into few small parts.

Now I have modified the code so that it calls the parts of the XYZ data files ( eg: maltoTHERMO_10Frames.traj, maltoTHERMO_20Frames.traj,... and so on) and do the calculation and dump the results in the same result output file. I have tested this code just with openning the XYZ coordinates and printing into another file to see wheather it collecting the data sequently and it was working fine.

This code supposedly read the x coordinate data and calculate distance and put a count accoding the criteria in the approperiate bins.
But when I let the code to do the calculation it stop and shows error. (I have indicated as !** in the code). If I enable this line, it shows error but if I disable this line, than the code do collect only the XYZ coordinates.
Could anyone help to correct this code? Many thanks in advance.

The code is as below:
Code:

 
module all_parameter
 integer,parameter :: MAXATOM=20736                        !CHANGE
 integer,parameter :: midwater=1500
 integer,parameter :: TOTALFRAMES=20
 integer,parameter :: NAT_HEAD = 44                        ! mass head =
 integer,parameter :: NAT_TAIL = 37                        ! mass tail =
 integer,parameter :: NAT_SOLUTE = 20736                ! mass solute =
 integer,parameter :: NAT_SOLVENT = 0                        !CHANGE
 integer,parameter :: MAXBIN=100               
 real,parameter :: MAX_BOX_length = 80.0       
!
 integer :: i, j,k, InputFrame_no, nhb, nlipid, total_atoms, jjj
 integer :: BIN, tot_lipid_atom, jat
 real :: del_length, fnid, GNRM, frameBoxLength, rlower, rupper
 real :: junk, xDistance, bx, by, bz
 real :: only_head, only_tail, only_water
 real :: mass_head, mass_tail, mass_water
 character*4 at(MAXATOM)
! Dimensions
 real,dimension(MAXATOM) :: x, y, z
 real,dimension(3) :: rcm
 real,dimension(MAXATOM) :: rx, ry, rz
 real,dimension(MAXATOM) :: ldp_LIPID_hist61, ldp_HEAD_hist62, ldp_TAIL_hist63, ldp_WATER_hist64
 real,dimension(MAXATOM) :: mass

end module all_parameter
!*************************************************************************
!        PROGRAM STARTS HERE
!*************************************************************************
PROGRAM local_density_profile
use all_parameter
implicit none
!=========================================================================
! Open files to be read and to write in
!=========================================================================
open(unit=40,status="old",file="trajFolder/malto-THERMO.top")        ! Topology file
open(unit=41,status="old",file="trajFolder/maltoTHERMO_10Frames.traj")
open(unit=42,status="old",file="trajFolder/maltoTHERMO_20Frames.traj")
open(unit=43,status="old",file="trajFolder/maltoTHERMO_30Frames.traj")
open(unit=44,status="old",file="trajFolder/maltoTHERMO_40Frames.traj")       
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
open(unit=50,status="unknown",file="XYZ-coordinates.dat")
open(unit=51,status="unknown",file="BOXinfo.dat ")
open(unit=52,status="unknown",file="lipid-counting-info.dat")
open(unit=61,status="unknown",file="only_LIPID.dat")
open(unit=62,status="unknown",file="only_HEAD.dat")
open(unit=63,status="unknown",file="only_TAIL.dat")
open(unit=64,status="unknown",file="only_WATER.dat")

! tot_lipid_atom - tracks total no. atoms (to distinguish solute/solvent)
! jat - tracks each lipid molecule and its head/tail

do i = 1, MAXBIN
  ldp_LIPID_hist61(i) = 0.0
  ldp_HEAD_hist62(i)  = 0.0
  ldp_TAIL_hist63(i)  = 0.0
  ldp_WATER_hist64(i) = 0.0
enddo
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! READ ATOMS LABLES FROM TOPOLOGY FILE (PARM) FILE 40
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! ignores titles and flags until the 6th line
read(40,'(a)') junk
read(40,'(a)') junk
read(40,'(a)') junk
read(40,'(a)') junk
read(40,'(a)') junk
read(40,'(a)') junk
! read the first number of line 7, format i8     
read(40,'(i8)') total_atoms
write(50,'(i8)') total_atoms
! ignores lines       
read(40,'(a)') junk
read(40,'(a)') junk
read(40,'(a)') junk
read(40,'(a)') junk
read(40,'(a)') junk
! starts reading all atom labels, format 20a4     
read(40,'(20a4)') (at(i),i = 1,total_atoms)

del_length = MAX_BOX_length / real(MAXBIN)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! READ COORDINATE FRAMES FROM TRAJ
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

fileLoop: do jjj = 41,42      ! I PUT THE LOOP HERE

        ! LOOP OVER FRAMES
        InputFrame_no = 1
        read(jjj,'(a)') junk
        whole: do while ( InputFrame_no <= TOTALFRAMES)   

        !  ignores blanks     
        write(50,*) 'frame ',InputFrame_no
        read(jjj,'(10F8.3)') (x(i),y(i),z(i),i = 1,total_atoms)  ! reading TRAJ file
        write(50,'(3F8.3)') (x(i),y(i),z(i),i = 1,total_atoms)  ! output data in x, y, z, coordinate format
        read(jjj,'(3F8.3)') bx,by,bz    ! box info dimension
        write(51,'(a,3F8.3)') 'BOXINFO', bx,by,bz

                nlipid = 0
                frameBoxLength = bx
                tot_lipid_atom = 0
                jat = 0

                loop1: do i = 1, total_atoms        !in one frame

                        rx(i) = x(i) ! temprary
                        xDistance = rx(i)
                       
                        !        tot_lipid_atom is total atoms in lipids !
                        !        jat is total head atoms and tail atoms  !

                        BIN = int(xDistance / del_length) + 1

                        OUT1:        IF ( tot_lipid_atom < NAT_SOLUTE ) then

                                        if (jat == ( NAT_HEAD + NAT_TAIL ) ) then
                                        nlipid = nlipid + 1
                                        write(52,*) "nlipid", nlipid   
                                        jat = 0
                                        end if
                       
                                        if ( jat < NAT_HEAD ) then
                                        IF (BIN .LE. MAXBIN) THEN
                                !**          ldp_HEAD_hist62(BIN) = ldp_HEAD_hist62(BIN) + 1
                                        ENDIF
                                        end if
                                ELSE 
                                        !IF (BIN .LE. MAXBIN) THEN
                                !**          ldp_WATER_hist64(BIN) = ldp_WATER_hist64(BIN) + 1
                                        !ENDIF

                                ENDIF OUT1
                                tot_lipid_atom = tot_lipid_atom + 1
                                jat = jat + 1


                !CLOSE LOOP ATOMS IN LIPIDS and WATER
                enddo loop1
                InputFrame_no = InputFrame_no + 1


        !CLOSE LOOP OVER FRAMES
        end do whole


!CLOSE LOOP OVER FILES
end do fileLoop

!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
!
do j = 1, MAXBIN
        write(61,*) ldp_LIPID_hist61(j)
        write(62,*) ldp_HEAD_hist62(j)
        write(63,*) ldp_TAIL_hist63(j)
        write(64,*) ldp_WATER_hist64(j)
end do


!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
!Normalize

end program local_density_profile
!########################################################################


colucix 10-31-2010 02:04 AM

Hi. It would be useful if you can post the exact error message. Furthermore, please use CODE tags to embed your code, so that it will result far more readable. To use code tags, either go in advanced mode, select the text and press the button marked with #, OR explicitly write the two tags [CODE] and [/CODE] before and after the text, respectively.

vjramana 10-31-2010 11:17 PM

Quote:

Originally Posted by colucix (Post 4144754)
Hi. It would be useful if you can post the exact error message. Furthermore, please use CODE tags to embed your code, so that it will result far more readable. To use code tags, either go in advanced mode, select the text and press the button marked with #, OR explicitly write the two tags [CODE] and [/CODE] before and after the text, respectively.

Thank you sir.
I have have taged in code mode.

Regards

colucix 11-01-2010 02:03 AM

Thank you. Nevertheless you did not show us the exact error message. What is the complete output of the program whenever it stops working?

vjramana 11-03-2010 12:42 AM

I use script to run this code. In side the code there I typed

gfortran ldp_malto_lowLyo1000_test2.f90 -o ldp_malto_lowLyo1000_test2.out
./ldp_malto_lowLyo1000_test2.out



And the error output is as below:
./ldp_run.sh: line 6: 11054 Segmentation fault ./ldp_malto_lowLyo1000_test2.out


regards

colucix 11-03-2010 03:45 AM

It's difficult to debug a segmentation fault error without having the possibility of running the code (we miss the input files). In general a segmentation fault happens when the program tries to access a memory location which is beyond that one allocated/requested by the program itself.

Regarding the multiple files reading, personally I would do something different: I would not open multiple I/O units, but I would stick with opening/closing only one unit by changing the name of the file in a loop (or by reading the file names passed as arguments to the program). I don't think this is the problem here, anyway.

I can't see any macroscopic error in the code, except the following (I'm not sure it is correct):
Code:

read(jjj,'(10F8.3)') (x(i),y(i),z(i),i = 1,total_atoms)  ! reading TRAJ file
Shouldn't it be 3F8.3?

vjramana 11-06-2010 04:23 AM

Yea,
That was the error. When I correct it as 3, it runs fine.
Thank you so much for your kind help. It helps me to understand the meaning of segmentation fault.

Regards



Quote:

Originally Posted by colucix (Post 4147768)
It's difficult to debug a segmentation fault error without having the possibility of running the code (we miss the input files). In general a segmentation fault happens when the program tries to access a memory location which is beyond that one allocated/requested by the program itself.

Regarding the multiple files reading, personally I would do something different: I would not open multiple I/O units, but I would stick with opening/closing only one unit by changing the name of the file in a loop (or by reading the file names passed as arguments to the program). I don't think this is the problem here, anyway.

I can't see any macroscopic error in the code, except the following (I'm not sure it is correct):
Code:

read(jjj,'(10F8.3)') (x(i),y(i),z(i),i = 1,total_atoms)  ! reading TRAJ file
Shouldn't it be 3F8.3?


colucix 11-06-2010 05:39 AM

So that was the problem! Most likely it stored some weird value in x, y and z and the subsequent calculation of xDistance and BIN brought to an invalid index for the ldp_HEAD_hist62 array. Glad to see it works now! Kind regards.


All times are GMT -5. The time now is 01:01 AM.