View previous topic :: View next topic |
Author |
Message |
LouisT
Joined: 29 Feb 2008 Posts: 4
|
Posted: Sat Mar 01, 2008 12:58 am Post subject: Grab parameters from external file |
|
|
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. |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Sat Mar 01, 2008 9:16 am Post subject: |
|
|
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. |
|
Back to top |
|
|
LouisT
Joined: 29 Feb 2008 Posts: 4
|
Posted: Sun Mar 16, 2008 8:00 pm Post subject: |
|
|
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) |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Mon Mar 17, 2008 8:47 am Post subject: |
|
|
You will need to define a CHARACTER variable for "buffer" and give it a length that is sufficient for the maximum size (say 256). |
|
Back to top |
|
|
LouisT
Joined: 29 Feb 2008 Posts: 4
|
Posted: Mon Mar 17, 2008 7:19 pm Post subject: |
|
|
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! |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Mon Mar 17, 2008 7:21 pm Post subject: |
|
|
Can you post some code to look at? |
|
Back to top |
|
|
LouisT
Joined: 29 Feb 2008 Posts: 4
|
Posted: Fri Mar 21, 2008 5:46 pm Post subject: |
|
|
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 |
|
Back to top |
|
|
PaulLaidler Site Admin
Joined: 21 Feb 2005 Posts: 7928 Location: Salford, UK
|
Posted: Sat Mar 22, 2008 9:14 am Post subject: |
|
|
Louis
All you need to do is take out a couple of commas...
Code: | 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 |
|
|
Back to top |
|
|
|