0

I need to output a matrix with FORTRAN. I have a working code that calculates the values, but instead of a matrix, I get single a column. The matrix is huge, ixj = ~2000x2000.

Here is my sample code:

  open(19, file="results1.txt", status="old", position="rewind", 
 & action="write") 

  do j=0,p
  do i=0,o
  write(19,*) mat_user_yield_surface(d, eps(i), deps(j), 200.0d0)
  end do
  end do

  close(19)
Sampsa
  • 1
  • 3
  • Your first sequence of open() write() close() creates the the first empty line. In addition, you do not need to open and close the file inside the loop. To avoid the first empty line : open() do do write() enddo enddo close(). To write 2d array, google it : do, i=1,m write(*,"100g15.5") ( matrix(i,j), j=1,n ) enddo – user1824346 Feb 23 '15 at 13:32
  • possible duplicate of [write in array format in fortran](http://stackoverflow.com/questions/16654249/write-in-array-format-in-fortran) – francescalus Feb 23 '15 at 13:48
  • Thanks! That helped a lot, at least instead of constantly opening and closing the file the code runs much faster. – Sampsa Feb 23 '15 at 13:52
  • "replace" doest work due: error #7589: Not a valid value for the char-expr in this connect-spec. ['replace'] – Sampsa Feb 23 '15 at 14:00
  • fyi simply using the default `open(unit,file=)` with no other options will overwrite the file from the beginning. – agentp Feb 23 '15 at 14:14
  • A variable named "o" is asking for trouble -- it is too easy to confuse with a zero. – Fortranner Feb 23 '15 at 15:03

2 Answers2

2

Use an implied do loop:

do j=0,p
   write(19,'(2000g22.14)') (mat_user_yield_surface(d, eps(i), deps(j),200.0d0),i=0,o)
end do

I suggest not using "o" as a variable name, since it is easily confused with zero.

Fortranner
  • 2,525
  • 2
  • 22
  • 25
  • you should use a format for this eg: `write(19,'(2000g22.14)')`. The count `2000` here can be any sufficiently large number, or "*" if your compiler supports it. – agentp Feb 23 '15 at 16:59
  • Added a format string to address the comments of francescalus and agentp. – Fortranner Feb 23 '15 at 17:22
0

This "write(19,'(2000g22.14)')" worked perfectly! Thanks. So the final code is:

  open(19, file="results1.txt", status="old", position="rewind",
 & action="write") 

  do j=0,p
  write(19,'(2000g22.14)') (mat_user_yield_surface(d, eps(i), 
 & deps(j), 200.0d0), i=0,o)
  end do

  close(19)
Sampsa
  • 1
  • 3