I would like to be able to read/write to files from a User Fortan routine, how can I do this?



First, a few general rules:

- file writing should only be done from Junction Box routines. If you try to write a file from a User CEL Function you can run into problems with over-writing data (since the Solver may call a User CEL Function many times per iteration), file conflicts (especially in parallel) and other problems caused by partitions not being synchronized. File reading is generally OK from both types of routines.
- don't use file I/O as an excuse to avoid storing things in memory. If you find yourself reading something that you just wrote out last iteration, then storing the data in memory would be a much faster option.
- the current directory for I/O operations is the myrun_001.dir/ directory. Relative paths should be relative to this directory.
- on Linux the g77 compiler does not support file I/O.
- make sure you close files after you have opened them for reading/writing, otherwise you'll get an error next time you try to open it.
- Don't assign random unit numbers. Use the following routine to obtain a unit number that will not conflict with one that the solver is using:

INTEGER IUNIT
CHARACTER*4 CRESLT,CERACT
CALL GET_FORTRAN_UNIT( CERACT, CRESLT, IUNIT )

IUNIT and CRESLT are returned to the caller. CERACT is input and tells it what to do if there is an error. Usually this is just the string 'STOP'

Now you can just use the usual Fortran OPEN, READ, WRITE and CLOSE statements, e.g.:

OPEN(IUNIT,file='D:usermyfile.txt')
WRITE(IUNIT,*) 'hello'
CLOSE(IUNIT)

or

OPEN(IUNIT,file='D:usermyfile.txt')
READ(IUNIT,*) MyLocalVariable
CLOSE(IUNIT)





Show Form
No comments yet. Be the first to add a comment!