0

I have been trying to extract the atom number corresponding to atom name "OW" using if condition in fortran code from a file. But when I am using the 'if condition' the values are not written in a file. Could anybody help me regarding the same where I am doing wrong.

implicit none
character(len=100)::head,grofile
character(len=5):: res_nm,at_name
integer :: n,i,ierror,at_num
write(*,*) 'enter the name of gro file'
read(*,*) grofile
open(unit=10,file=grofile,status='old',action='read')
openif :     if (ierror == 0) then
!open was ok. Read values.
                read(10,*)head
                read(10,*)n
                do i=1,n
                read(10,200) at_name,at_num
                if (at_name == 'OW') then
                write(44,*)at_num
                200 format (10x,a5,i5)
        endif
        enddo
        endif openif
end program name

and the input file that I am using is

CNT in water
44316
    1LIG      C    1   2.814   2.448   2.231 -0.2002  0.0645 -0.2005
    1LIG      C    2   2.783   2.584   2.233  0.4146  0.2083 -0.1403
    1LIG      C    3   2.769   2.658   2.350 -0.4678 -0.0886 -0.0500
    1LIG      C    4   2.687   2.772   2.348 -0.7671 -0.3032 -0.0624
    1LIG      C    5   2.619   2.795   2.228 -0.2327 -0.2483 -0.3593
    1LIG      C    6   2.486   2.837   2.238 -0.0621  0.2349 -0.0781
................
    1LIG      H 1006   2.613   1.972  12.082 -1.2767  0.0570  0.2045
    1LIG      H 1007   2.804   2.173  12.099 -0.4228  1.8734  1.9762
    1LIG      H 1008   2.862   2.377  12.097 -0.7176 -2.2587  1.0804
    2water   OW 1009   2.221   1.281   6.853 -0.6831 -0.3395  0.1402
    2water  HW1 1010   2.191   1.215   6.789 -1.2195  0.6304 -0.6225
    2water  HW2 1011   2.143   1.333   6.871 -0.5687 -0.7024  1.7263
    2water   MW 1012   2.206   1.279   6.847 -0.7389 -0.2594  0.2489
    3water   OW 1013   2.826   4.482  12.736 -0.2852  0.1750  0.1277
    3water  HW1 1014   2.735   4.490  12.707 -0.3265 -0.3844  0.1046
    3water  HW2 1015   2.860   4.406  12.689  0.4937  0.9762 -0.6120
    3water   MW 1016   2.818   4.473  12.726 -0.1879  0.2065  0.0267
    4water   OW 1017   3.510   2.042  10.165  0.1154 -0.0258 -0.0813
    4water  HW1 1018   3.530   2.105  10.095  3.0124 -0.2562  0.4945
    4water  HW2 1019   3.434   1.993  10.132 -0.4188  1.8748 -1.7521
...............
4.90369   4.90369  14.25892 

Also, I am not getting any error for the code and without any output. The command that I am using

gfortran br_br_gofr_smooth_dlp4.f90 -o read -I /usr/local/include/ -lgmxfort -g -fcheck=all -fbounds-check

./read 
7
  • 2
    What does "does not seem to work" mean for you? It does not say anything useful. Were there any error messages? Which ones? Show them! Does the program behave incorrectly? How exactly? Commented Jun 14, 2021 at 6:40
  • 1
    Also, please think about the tags you ate using, the algorithm tag is probably wrong here and certainly do not use both fortran77 and fortran95 unless you are asking for the differences or similar. If the question is not version-specific, do not use any version tag, just tag fortran. Commented Jun 14, 2021 at 6:41
  • 1
    I think we would need to see a sample input file and expected output file in order to know what was going wrong. Commented Jun 14, 2021 at 7:03
  • 2
    You may also want to show where ierror is assigned a value. Commented Jun 14, 2021 at 7:12
  • 2
    Aren't you missing iostat=ierror in the open statement. If it shouldn't be there then you have a confusing comment two lines later. Commented Jun 14, 2021 at 7:28

1 Answer 1

3

You have two errors:

  1. You are using ierror uninitialised, as noted in the comments
  2. at_name is a length 5 character variable. You are comparing it with a 2 letter character variable. For this to be true the leftmost 2 characters of at_name have to be the same as those in the two character variable. Unfortunately as your code is written it reads the atom name into the rightmost 2 characters of at_name. Thus the test fails

The code below shows a way of fixing the above, and does what I think you want. Especially for point 2 there are other ways.

ijb@ijb-Latitude-5410:~/work/stack$ cat pdb.f90
  implicit none
  character(len=100)::head,grofile
  character(len=5):: res_nm,at_name
  integer :: n,i,ierror,at_num
  write(*,*) 'enter the name of gro file'
  read(*,*) grofile
  open(unit=10,file=grofile,status='old',action='read',iostat=ierror)
  openif :     if (ierror == 0) then
     !open was ok. Read values.
     read(10,*)head
     read(10,*)n
     do i=1,n
        read(10,200) at_name,at_num
        if (Adjustl(at_name) == 'OW') then
           write(44,*)at_num
200        format (10x,a5,i5)
        endif
     enddo
  endif openif
end program
ijb@ijb-Latitude-5410:~/work/stack$ gfortran -std=f2008 -Wall -Wextra -fcheck=all -g -O pdb.f90 
pdb.f90:3:27:

    3 |   character(len=5):: res_nm,at_name
      |                           1
Warning: Unused variable ‘res_nm’ declared at (1) [-Wunused-variable]
ijb@ijb-Latitude-5410:~/work/stack$ cat stuff
CNT in water
20
    1LIG      C    1   2.814   2.448   2.231 -0.2002  0.0645 -0.2005
    1LIG      C    2   2.783   2.584   2.233  0.4146  0.2083 -0.1403
    1LIG      C    3   2.769   2.658   2.350 -0.4678 -0.0886 -0.0500
    1LIG      C    4   2.687   2.772   2.348 -0.7671 -0.3032 -0.0624
    1LIG      C    5   2.619   2.795   2.228 -0.2327 -0.2483 -0.3593
    1LIG      C    6   2.486   2.837   2.238 -0.0621  0.2349 -0.0781
    1LIG      H 1006   2.613   1.972  12.082 -1.2767  0.0570  0.2045
    1LIG      H 1007   2.804   2.173  12.099 -0.4228  1.8734  1.9762
    1LIG      H 1008   2.862   2.377  12.097 -0.7176 -2.2587  1.0804
    2water   OW 1009   2.221   1.281   6.853 -0.6831 -0.3395  0.1402
    2water  HW1 1010   2.191   1.215   6.789 -1.2195  0.6304 -0.6225
    2water  HW2 1011   2.143   1.333   6.871 -0.5687 -0.7024  1.7263
    2water   MW 1012   2.206   1.279   6.847 -0.7389 -0.2594  0.2489
    3water   OW 1013   2.826   4.482  12.736 -0.2852  0.1750  0.1277
    3water  HW1 1014   2.735   4.490  12.707 -0.3265 -0.3844  0.1046
    3water  HW2 1015   2.860   4.406  12.689  0.4937  0.9762 -0.6120
    3water   MW 1016   2.818   4.473  12.726 -0.1879  0.2065  0.0267
    4water   OW 1017   3.510   2.042  10.165  0.1154 -0.0258 -0.0813
    4water  HW1 1018   3.530   2.105  10.095  3.0124 -0.2562  0.4945
    4water  HW2 1019   3.434   1.993  10.132 -0.4188  1.8748 -1.7521
4.90369   4.90369  14.25892 
ijb@ijb-Latitude-5410:~/work/stack$ ls -lrt | tail
-rw-rw-r-- 1 ijb ijb   1106 Jun 11 05:43 pi_orig.f90
-rw-rw-r-- 1 ijb ijb    958 Jun 11 05:56 pi_ijb.f90~
-rw-rw-r-- 1 ijb ijb   1805 Jun 11 06:07 pi_ijb.f90
-rw-rw-r-- 1 ijb ijb   1106 Jun 11 06:29 pi2.f90~
-rw-rw-r-- 1 ijb ijb   1305 Jun 11 06:34 pi2.f90
-rw-rw-r-- 1 ijb ijb    537 Jun 14 08:39 pdb.f90~
-rw-rw-r-- 1 ijb ijb   1462 Jun 14 08:40 stuff~
-rw-rw-r-- 1 ijb ijb   1425 Jun 14 08:41 stuff
-rw-rw-r-- 1 ijb ijb    560 Jun 14 08:43 pdb.f90
-rwxrwxr-x 1 ijb ijb  20520 Jun 14 08:49 a.out
ijb@ijb-Latitude-5410:~/work/stack$ ./a.out
 enter the name of gro file
stuff
ijb@ijb-Latitude-5410:~/work/stack$ ls -lrt | tail
-rw-rw-r-- 1 ijb ijb    958 Jun 11 05:56 pi_ijb.f90~
-rw-rw-r-- 1 ijb ijb   1805 Jun 11 06:07 pi_ijb.f90
-rw-rw-r-- 1 ijb ijb   1106 Jun 11 06:29 pi2.f90~
-rw-rw-r-- 1 ijb ijb   1305 Jun 11 06:34 pi2.f90
-rw-rw-r-- 1 ijb ijb    537 Jun 14 08:39 pdb.f90~
-rw-rw-r-- 1 ijb ijb   1462 Jun 14 08:40 stuff~
-rw-rw-r-- 1 ijb ijb   1425 Jun 14 08:41 stuff
-rw-rw-r-- 1 ijb ijb    560 Jun 14 08:43 pdb.f90
-rwxrwxr-x 1 ijb ijb  20520 Jun 14 08:49 a.out
-rw-rw-r-- 1 ijb ijb     39 Jun 14 08:49 fort.44
ijb@ijb-Latitude-5410:~/work/stack$ cat fort.44
        1009
        1013
        1017
ijb@ijb-Latitude-5410:~/work/stack$ 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Sir for your valuable reply and to all who helped me solving this issue. Now the code is working fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.