Silverfrost Forums

Welcome to our forums

Grab parameters from external file

29 Feb 2008 11:58 #2853

Hi,

I'm new to Fortran and programming in general. I'm interested in running simulations in FORTRAN. One of the things is that I need to run the code multiple times using different starting parameters. Is there any way I can automate this? For example, grabbing parameters from an external file.

Thanks! All suggestions would be greatly appreciated.

1 Mar 2008 8:16 #2856

You can read data from a file using stanard Fortran READ statements. Then you can change the contents of the data file for each run.

As a refinement you could supply the name of the data file as a parameter on the command line for the executable and then read from the corresponding file. Then you could have separate files for each data set.

See the FTN95.chm under READ, GET_COMMAND and GET_COMMAND_ARGUMENT.

16 Mar 2008 7:00 #2927

Thanks Paul! That was really helpful. The next question I have is, how can I compile the project/program given that I have not specified the character buffer beforehand?

E.g., CALL getarg(1,buffer)

17 Mar 2008 7:47 #2930

You will need to define a CHARACTER variable for 'buffer' and give it a length that is sufficient for the maximum size (say 256).

17 Mar 2008 6:19 #2935

Hi, Paul, thanks. I did declare the buffer as a character variable beforehand. The issue is, when I compile the program, the error message appears:

C:\FORTRAN\CAT.F90(28) : error 573 - Missing expression

Although I have declared the variable, I did not input any arguments because the arguments are to be read in from the terminal/console. How can I specify this so it can compile?

Thanks!

17 Mar 2008 6:21 #2936

Can you post some code to look at?

21 Mar 2008 4:46 #2948

Hi Paul, Here is a sample code. So when I run the executable, I can append the parameters to the execute command and the program will use those parameters. Thanks for your help!

PROGRAM X
IMPLICIT NONE
INTEGER a1, a2
CHARACTER *20 buffer

CALL getarg(1,buffer)   
READ(buffer,*), a1
CALL getarg(2,buffer)
READ(buffer,*), a2

END PROGRAM X
22 Mar 2008 8:14 #2949

Louis

All you need to do is take out a couple of commas...

PROGRAM X 
IMPLICIT NONE 
INTEGER a1, a2 
CHARACTER *20 buffer 

CALL getarg(1,buffer) 
READ(buffer,*) a1 
CALL getarg(2,buffer) 
READ(buffer,*) a2 

END PROGRAM X
Please login to reply.