Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing multiple arrays to output file

Status
Not open for further replies.

raberry22

Programmer
Dec 30, 2015
3
US
I've written a 3D finite difference diffusion code in Microsoft Visual studio Intel Fortran Compiler and I need to output the concentrations at certain time step values. I need an output file that includes the concentration array in the domain for specific time steps throughout the program. The format I have right now is listed below. m is the global indexing scheme.

open(71,file='Conc.out',status='unknown')

if(NumTS.eq.TS)then
do k=1,NZ
do j=1,NY
do i=1,NX
m=i+(j-1)*NX+(k-1)*NX*NY
write (71,*)TS
write (71,*)Cold(m)
end do
end do
end do
end if

close(unit=71)

NumTS is the time step value I wish to output i.e NumTS=100,150,200 etc. TS is the updated time step value that has an increase in time step size added to the previous time step value that is included in the main loop i.e 50 is added each time. How can I output the concentrations for each time step in one file that is column listed. I need the output file to be as seen below. Any help would be awesome! Thanks.

Note: This output command is within a subroutine and Cold(m) is the concentration array

100 150 200 250 etc..

1495.222 12453 16463 13564
1494.811 134546 13658 13565
1494.206 136563 13564 13567
1493.420 114543 16743 13569
******** ******** ******* *******
 
At this point, I would simply recommend that you transpose the manner you want to output your data and write out one row at a time...at the end, you should have your "column" (row really) headings on the first column, followed by the values of interest.
 
OK I'm fine with having multiple output files that have just one array in them but how would I format that in the code to output multiple files for each desired time step.eg. Conc100.out, Conc150.out, Conc200.out etc?
 
raberry22 said:
but how would I format that in the code to output multiple files for each desired time step.eg. Conc100.out, Conc150.out, Conc200.out etc?

For each timestep you have to cŕeate the file name, then open that file, write data in it and close the file.
For example something like this:
Code:
[COLOR=#a020f0]program[/color] raberry22
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: ts
  [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]3[/color] :: ts_char
  [COLOR=#2e8b57][b]character[/b][/color][COLOR=#804040][b]*[/b][/color][COLOR=#ff00ff]12[/color] :: fname

  [COLOR=#804040][b]do[/b][/color]  ts [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]100[/color], [COLOR=#ff00ff]200[/color], [COLOR=#ff00ff]50[/color]
    [COLOR=#0000ff]! create file name[/color]
    [COLOR=#804040][b]write[/b][/color](ts_char,[COLOR=#ff00ff]'(i3)'[/color]) ts
    fname [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'conc'[/color][COLOR=#804040][b]//[/b][/color]ts_char[COLOR=#804040][b]//[/b][/color][COLOR=#ff00ff]'.out'[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Creating file: '[/color], fname
    
    [COLOR=#0000ff]! open file[/color]
    [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]71[/color], [COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname, [COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'unknown'[/color])
    
    [COLOR=#0000ff]! write data[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#ff00ff]71[/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'writing data for TimeStep ='[/color], ts 
    
    [COLOR=#0000ff]! close file[/color]
    [COLOR=#804040][b]close[/b][/color]([COLOR=#804040][b]unit[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]71[/color])
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'... done.'[/color]
  [COLOR=#804040][b]end do[/b][/color]

[COLOR=#a020f0]end program[/color] raberry22

The program above creates in your current directory 3 files:
conc100.out
conc150.out
conc200.out
 
Code:
$ gfortran raberry22.f95 -o raberry22

$ raberry22
 Creating file: conc100.out
 ... done.
 Creating file: conc150.out
 ... done.
 Creating file: conc200.out
 ... done.
 
I have included below a set of commands that might work as well? NumTS is my main loop counter and I add 50 to each updated TS value. I have approximately 1500 time steps. Tstep=50 and the initial TS=Tstep

if(NumTS.eq.TS)then
! create file name
write(ts_char,'(i4)') TS
fname = 'conc'//ts_char//'.out'

! open file
open(71, file=fname, status='unknown')

! write data
write(71,*) (((Cold(i+(j-1)*NX+(k-1)*NX*NY),i=1,NX),j=1,NY),k=1,NZ)

close(unit=71)

end if

TSS=TS+Tstep
TS=TSS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top