FORTRAN read statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FORTRAN read statement
# 1  
Old 05-31-2013
FORTRAN read statement

Hi,
I've been having trouble figuring out what the following read statement is doing.
Code:
DO I = 1, natom
           k = 3 * (i - 1) + 1
           READ(8,*) AtNum(I), (X_2(J),J=k,k+2)
       END DO

The part I don't understand is:
Code:
 (X_2(J),J=k,k+2)

the file it is reading is simply a set of cartesian coordinates with an atom label in front. For example:
Code:
7   0.000000000000      0.000000000000     1.600000000000
 1     0.000000000000      0.000000000000   0.000000000000

Can someone please help? Let me know if you need more info.
# 2  
Old 05-31-2013
Quote:
Originally Posted by butson
Hi,
I've been having trouble figuring out what the following read statement is doing.
Code:
DO I = 1, natom
           k = 3 * (i - 1) + 1
           READ(8,*) AtNum(I), (X_2(J),J=k,k+2)
       END DO

The part I don't understand is:
Code:
 (X_2(J),J=k,k+2)

the file it is reading is simply a set of cartesian coordinates with an atom label in front. For example:
Code:
7   0.000000000000      0.000000000000     1.600000000000
 1     0.000000000000      0.000000000000   0.000000000000

Can someone please help? Let me know if you need more info.
It has been a long time since I've written any FORTRAN code, but the first time through this DO loop, I is set to 1, k is set to 1, and the READ reads data into the 1st element of the array AtNum (AtNum(1)) and the 1st three elements of the array X_2 (X_2(1[from J=k]), X_2(2[from J=k+1]), and X_2(3[from J=k+2]).
The second time through the loop, I will be 2 and k will be 4, so the READ will fill in AtNum(2) and X_2(4 through 6).
This will continue until incrementing I exceeds the value of natom which this code fragment doesn't set. This code fragment also does not show how file descriptor 8 was opened.

In FORTRAN, file descriptor 5 is standard input and file descriptor 6 is standard output. While in C, C++, and the standard shells, file descriptor 0 is standard input, FD 1 is standard output, and FD 2 is standard error.

Hope this helps...
# 3  
Old 05-31-2013
Are you reading a mol file, or some other format? The syntax for the input file doesn't look familiar with that leading atom number.

FORTRAN is a beast for io. All input has to be precisely formatted because you have to tell the FORTRAN code what to do with each individual character, including spaces, trailing zeros, etc. This doesn't look like f77, because the DO loop doesn't have a numbered CONTINUE statement. If you are using a more updated version of FORTRAN, it may be a little easier to use.

In my experience, the easiest thing to to with code like this is to create a c or cpp function to read the input file instead of reading it with the FORTRAN. In cpp, you can just create in input stream and say it's space delimited and dump your data into a vector. Then you can parse it out of the vector however you want. If you are using the gnu compilers, it is very easy to call a c or cpp function from your FORTRAN code. The FORTRAN is compiled as c under the hood anyway. You would just create the FORTRAN data structures you need, pass them to the c function in the call, and let the c populate the data structures from the input file. When the function returns, you will have what you want from the input file in your FORTRAN variables.

This is the easiest way to get FORTRAN code to play nicely with the rest of the more fashionable programming world.

LMHmedchem
# 4  
Old 05-31-2013
Thanks Don that helps a lot. So for the x_2 the format is (label(j), minimum j, max j) correct?

LMH that is also very helpful. I am using gfortran to compile. I will look into using this for future programs. This one was already made for me, but it isn't working and I am trying to figure out why. After reading the replies, I don't think this is the issue.

I was hoping maybe you all could help me with another thing I think might be the problem.
Code:
double precision stpmax, DelG(N), HDelG(N), ChgeX(N), DelX(N), w(N), fac, fad, fae, sumdg, sumdx, stpl, lgstst, STPMX

The yellow text is actually highlighted in the code. Could this be because it is too long? I think I read somewhere that there is a limit on how long a line can be in fortran.
# 5  
Old 05-31-2013
Back in the days of programs on punched cards, FORTRAN used columns 1-6 for statement numbers, column 7 being anything other than a space indicated it was a continuation of the previous card, columns 8-72 were program text, and columns 73-80 were ignored by the compiler (sometimes containing sequence numbers that could be used to sort the deck back into order using a card sorter in case you dropped your program deck). You'll have to read the man page for your compiler; there is probably an option to specify which columns in your input file contain program text.

PS Note that yellow text on a gray CODE tag background is almost unreadable.
# 6  
Old 05-31-2013
Quote:
Originally Posted by butson
. . .
Code:
. . . 
           READ(8,*) AtNum(I), (X_2(J),J=k,k+2)
. . .

I think that's what they called an "implied DO loop", a short form to use inside constructs like the one you show. J is assuming the values k, k+1, k+2, so it's reading those three coordinates into the X_2 array.

Jeepers, time passes!
# 7  
Old 05-31-2013
Quote:
Originally Posted by butson
Thanks Don that helps a lot. So for the x_2 the format is (label(j), minimum j, max j) correct?

LMH that is also very helpful. I am using gfortran to compile. I will look into using this for future programs. This one was already made for me, but it isn't working and I am trying to figure out why. After reading the replies, I don't think this is the issue.

I was hoping maybe you all could help me with another thing I think might be the problem.
Code:
double precision stpmax, DelG(N), HDelG(N), ChgeX(N), DelX(N), w(N), fac, fad, fae, sumdg, sumdx, stpl, lgstst, STPMX

The yellow text is actually highlighted in the code. Could this be because it is too long? I think I read somewhere that there is a limit on how long a line can be in fortran.
Does all of your code have 6 blank spaces before there is any code other than statement numbers? If you add 6 spaces to the front of the code you posted here, it happens that the first highlighted character is character 73, which would be beyond where a fortran compiler would normally read.

I would reformat the text as follows,
Code:
      double precision stpmax, DelG(N), HDelG(N), ChgeX(N), DelX(N),
     - w(N), fac, fad, fae, sumdg, sumdx, stpl, lgstst, STPMX

Note that the first line begins with 6 spaces and the d in double is at the 7th character. Each line is no longer than 72 characters, including the initial 6 spaces. The second line has a - as the 6th character. This indicates that the line beginning with w is a continuation of the previous line. Note that DelX(N) has a trailing coma because the list of variables has not been terminated. You could also break this up into several lines.

Code:
      double precision stpmax, DelG(N), HDelG(N), ChgeX(N), DelX(N)
      double precision w(N), fac, fad, fae, sumdg, sumdx
      double precision stpl, lgstst, STPMX

I usually have comment lines like,

C----------------------------------------------------------------------

the last dash is character 72 and I know not to code past that.

I don't like long declaration lists because I like to have allot of comments about what things are. Since FORTRAN variables have to be declared before any executable code, you can't conveniently declare var near where they are used in the code like you can in cpp. This makes good documentation even more important.

LMHmedchem
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] While read line and if statement not working

I'm looking for some help in figuring why my little bit of code will not process any entries other then the first one in my list. while read line ;do hostname=${line//\"} a=`ssh user@$hostname uptime;echo $?` if ];then dt=`date` touch... (6 Replies)
Discussion started by: whegra
6 Replies

2. Shell Programming and Scripting

Read statement not working

hello guys, i am having the below piece of code error () { echo"Press y /n" read ans case $ans in y) main;; n) exit esac } In the abve code, read statement is not working i.e not waiting for user to enter input. ,i tested exit status its 1. could anyone help me to do this ... (11 Replies)
Discussion started by: mohanalakshmi
11 Replies

3. Shell Programming and Scripting

awk doesn't understand 'read' statement!!!

While working on awk programming, i found that it doesn't understand 'read' statement. Then what's the use of 'continue' and 'break' statement in awk. For ex: awk '{k=1; while (k<10) {print $0; k++}}' emp.lst Now, please say if I want to put the logic that after priting 1 line, it will ask for... (13 Replies)
Discussion started by: ravisingh
13 Replies

4. UNIX for Dummies Questions & Answers

Read statement within while read loop

hi, this is my script #!/bin/ksh cat temp_file.dat | while read line do read test if ]; then break else echo "ERROR" fi done when i execute this code , the script does wait for the user input . it directly prints "ERROR" and terminates after the no. of times as there... (3 Replies)
Discussion started by: siva1612
3 Replies

5. Shell Programming and Scripting

Read SQL statement in Script

Hi Guys.. need some urgent help... I am stuck in something badly I need to write a script which would read a sql statement (which might be a join/inner join/select/sub select etc. ) I need to read that sql statement ... and in the output I want all the table names and columns (doesn't... (4 Replies)
Discussion started by: freakygs
4 Replies

6. Shell Programming and Scripting

Read from user inside a while statement

Hi there I want to ask to a user something about each line of a file. This is my code: #cat test.txt first line second line third line #cat test.sh #!/bin/ksh read_write () { echo "Do you want to print the line (YES/NO)?\n" read TEST case ${TEST} in YES) return 0;; ... (3 Replies)
Discussion started by: tirkha
3 Replies

7. Shell Programming and Scripting

generating the variable list for WHILE READ statement at runtime

Hi, I am reading the contents of a file in variables as - cat ${var_file_name} | while read COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11 The problem is ... my file can have any number of columns - 5, 10, 11 .... So i want a dynamic variable list as - cat ${var_file_name} |... (8 Replies)
Discussion started by: gopalss
8 Replies

8. Shell Programming and Scripting

Read statement not working in a script

I have a script consisting of certain functions whose input is a file at same location. In that file i have written the name of anothe file at same location. The third file contains a word which act as a function in the first script.Let me give an example i have a scrip file say 1.sh in which i am... (7 Replies)
Discussion started by: sumitdua
7 Replies

9. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies

10. Shell Programming and Scripting

how to read a file to an echo statement

I was searching for an option where i can echo some strings together with the contents of a file. Eg. I was to echo the below string to a file "alter application PMS_ add variable CurrYear Y2009;" >> ${ESS_MAXL_DIR}/PMS_SUB.txt The value 2009 is coming from a file called date.txt without the... (3 Replies)
Discussion started by: Celvin VK
3 Replies
Login or Register to Ask a Question