[Fortran90] how to write several output into one file

In summary: So your 130 was the number of items to write. You were trying to write 390 items, 130 times, not 130 items, 3x each. So, you're going to get 130 lines of 3 items, not 3 lines of 130 items. He's saying that 130 is not what you wanted right there.I think you're misunderstanding the purpose of the "130" in the format statement. It is not the filename, it is a number that tells the WRITE statement how many times to repeat the format. So with your current code, you are trying to write 130 sets of 3 values on each line, which is not what you want.To write each value on
  • #1
s_hy
61
0
hi all,

Currently I am doing 2d fdtd (TMz mode) modeling. I expect to have output for Ez, Hx and Hy in one file, which is mean that the output will be in 3 column. But I don't have any idea how format it. Anyone can help me? Below is the code

Code:
!problem 3.9, Taflove chapter 3
!to model 2d TMz mode fdtd Simulation using Yee grid.
!time step deltat = (1/sqrt(2)*delta/c) is used

subroutine fd2d
implicit none

double precision                   :: f0               !frequency
double precision                   :: miu
double precision                   :: miur             !permittivity
double precision                   :: miu0
double precision                   :: E0
double precision                   :: delta 
double precision                   :: deltat
double precision                   :: deltax 
double precision                   :: deltay
double precision                   :: sigma
double precision                   :: sigmastar
double precision                   :: Eps
double precision                   :: Eps0
double precision                   :: Epsr
double precision                   :: lambda
double precision                   :: lambdax
double precision                   :: lambday
double precision                   :: nlambdax
double precision                   :: nlambday
double precision                   :: omega
double precision                   :: S
double precision                   :: k
double precision                   :: c
integer                            :: i,j
integer                            :: m,p
integer                            :: mlast,plast
integer                            :: n
integer                            :: iinit 
integer                            :: ilast 
integer                            :: jinit 
integer                            :: jlast 
double precision,dimension(202,202):: Ez
double precision,dimension(202,202):: Hy
double precision,dimension(202,202):: Hx
double precision                   :: EzAmp
double precision                   :: HyAmp
double precision                   :: HxAmp
double precision                   :: Ca
double precision                   :: Cb
double precision                   :: Da
double precision                   :: Db
double precision, parameter        :: pi = 3.14159265
character(len=20)                  :: filename

f0 = 100.0
miu0 = 1.0
miur = 1.0
Eps0 = 1.0
Epsr = 1.0
!delta = 1.e-3
delta = 0.1
!lambda = c/f0
lambda = 1
 c = 1.0
E0 = 1.0
deltat = 1/sqrt(2.0)*(delta/c)
k = 2*pi / lambda
omega = c*k
nlambdax = 5
nlambday = 5
lambdax = lambda
lambday = lambda

deltax = delta
deltay = delta

S = 1.0001
iinit = 1 
ilast = 100
jinit = 1
jlast = 100
mlast = 2*ilast-1
plast = 2*jlast-1

print*, 'see me?'

!initialize Ez, Hx,Hy to zero at t=0
do p = 1,2*jlast
 do m = 1,2*ilast
  if (Mod(m,2) == 0) then
   if (Mod(p,2) == 0) then
      Hx(m-1,p+2) = 0
      Hy(m,p+1) = 0
      else
      Ez(m-1,p+1) = 0
   end if
  end if
 end do
end do

print*, 'see me?2'
!medium's properties
 Ca = 1.0
 Cb = deltat/(1-0.5*deltax)
 Print *,'Ca=',Ca,'Cb=',Cb
 Da = 1.0
 Db = deltat/deltax
 Print *, 'Da=',Da, 'Db=',Db

!initiate sinusoidal wavepulse at center

do n = 1,200
   
  write (filename, "('ez',I3.3,'.dat')") n
  open (unit=130,file=filename)
 
!initiate sinusoidal wavepulse at center
  Ez(101,101) = sin((n+1)*omega*deltat)
  
  do m = 2,2*ilast-1
    do p = 2,2*jlast-1
    
      if (Mod(m,2)/=0) then
	if (Mod(p,2)/=0) then
	  Ez(m,p+2) = Ca*Ez(m,p+2)+Cb*(Hy(m+1,p+2)-Hy(m-1,p+2)+Hx(m,p+1)-Hx(m,p+3))
          write (130,1) Ez(m,p+2)
          !print *, Ez(m,p+2)
        end if
      end if
    end do
  end do

  do m = 2,2*ilast-1
    do p = 2,2*jlast-1

      if (Mod(m,2) == 0) then
	if (Mod(p,2) == 0) then
	  Hy(m,p+1) = Da*Hy(m,p+1)+Db*(Ez(m+1,p+1)-Ez(m-1,p+1))
          Hx(m-1,p+2) = Da*Hx(m-1,p+2)+Db*(Ez(m-1,p+1)-Ez(m-1,p+3))
          write (130,1) Hx(m-1,p+2)
          write (130,1) Hy(m,p+1)
        end if
      end if
     end do
  end do

1 format (130(3E15.6))
  close (unit=130)

end do !n

end SUBROUTINE fd2d

Anyway, format I used to write file is not working.

Thank you
 
Technology news on Phys.org
  • #2
Each WRITE statement in Fortran starts a new line. To write three values on the same line you would do something like

write (130,1) Ez(m,p+2), Hx(m-1,p+2), Hy(m,p+1)

If might be easier to have a separate DO loop (or nested loops) for the write statement, after you have done all the calculations.

Also, your format statement should probably be just

1 format (3E15.6)

I'm not sure what you thought the "130" was for, but it actually means you could write 130 sets of three values all on one line, if you had one WRITE statement that output all 390 values. From yuor OP, that's not what you wanted to do.
 
  • #3
Thank you, alephzero.

I did as what you said and it's work! Btw, 130 is the filename, which I think will not effect the output.
 
Last edited:
  • #4
Good.

By the way, Alephzero was talking about the 130 in your format statement:
Code:
1 format (130(3E15.6))
  close (unit=130)
that's a multiplier right there to write 130 times, 3 fields of 15.6.
 
  • #5
for sharing your code and asking for help with formatting the output. It looks like you are trying to write data for Ez, Hx, and Hy into one file with three columns. To do this, you can use the "write" statement with the "advance" option set to "no" and the "fmt" option set to "(3E15.6)". This will write the data in three columns with a field width of 15 and a decimal precision of 6. Your code might look something like this:

do n = 1,200
write (filename, "('output.dat')") n
open (unit=130,file=filename)

!write Ez, Hx, and Hy data into one file
do m = 2,2*ilast-1
do p = 2,2*jlast-1
if (Mod(m,2)/=0) then
if (Mod(p,2)/=0) then
write (130, "(3E15.6)", advance="no") Ez(m,p+2), Hx(m-1,p+2), Hy(m,p+1)
end if
end if
end do
end do

close (unit=130)
end do

I hope this helps. If you continue to have trouble with formatting, I recommend consulting the documentation for Fortran90 or reaching out to a colleague or mentor for assistance. Good luck with your modeling!
 

1. How do I write multiple outputs into one file in Fortran90?

To write multiple outputs into one file in Fortran90, you can use the WRITE statement with the UNIT and IOSTAT options. The UNIT option specifies the file to be written to, while the IOSTAT option checks for any errors during the writing process.

2. Can I append data to an existing file in Fortran90?

Yes, you can append data to an existing file in Fortran90 by using the WRITE statement with the POSITION='APPEND' option. This will add the data to the end of the file without overwriting any existing data.

3. How do I print a header or label for my output in Fortran90?

To add a header or label to your output in Fortran90, you can use the FORMAT statement to specify the format of your output, including any desired headers. Then, use the WRITE statement with the FORMAT option to write the header and data to the file.

4. Is it possible to write different types of data (integer, real, string) into one file in Fortran90?

Yes, it is possible to write different types of data into one file in Fortran90. You can use the FORMAT statement to specify the format for each type of data and then use the WRITE statement to write the data to the file using the appropriate format.

5. How can I ensure that my output file is properly closed after writing in Fortran90?

To ensure that your output file is properly closed after writing in Fortran90, you can use the CLOSE statement after all your WRITE statements to explicitly close the file. This will also free up any resources associated with the file. Alternatively, you can use the ENDFILE statement to end the writing process and automatically close the file.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
Replies
1
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
12K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Programming and Computer Science
Replies
8
Views
6K
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top