0

I try in a small code to write output results with numerical values between various text.

For the moment, I do :

   ! Print results
   write(*,*)
   write(*,*) '  Time step = ',dt
   write(*,*)
   write(*,1001) epsilon,step
   write(*,*)
   write(*,*) '  Problem size = ',size_x*size_y
   write(*,*)
   write(*,1002) elapsed_time                                                            
   write(*,*)
   write(*,*) '  Computed solution in seq.dat file '                               
   write(*,*)

   ! Formats available to display the computed values on the grid                        
1001 format('   Convergence = ',f11.9,' after ',i9,' steps ')                            
1002 format('   Wall Clock = ',f15.6)                                   

which produces at the execution :

  Time step = 0.000003755783907217

  Convergence = 0.100000000 after 8882 steps

  Problem size = 24576

  Wall Clock =        5.213814

  Computed solution in Seq.dat

My issue is about the line "Wall Clock = 5.213814", I would like to get only one space juste after "Wall Clock =" before the value "5.213814". Currently, I think these multiple spaces that I get come from the "f15.6" with 1002 format(' Wall Clock = ',f15.6).

Here's what I want to get (with another value for steps) :

  Time step = 0.000003755783907217

  Convergence = 0.100000000 after 20910988821 steps

  Problem size = 24576

  Wall Clock = 5.213814

  Computed solution in Seq.dat

I have set "f15.6" since I can get high number for "Wall Clock", same thing for espilon and step variables.

I don't know in all cases how to set just one space between words and values to write between them, as when I printf, in C language, different values and words on the same line.

I know there's a simple solution but can't find it.

UPDATE 1 :

I tried the solution indicated in the first answer.

Here's what I have done :

write(*,1001) epsilon,step
write(*,1002) elapsed_time


1001 format('  Convergence = ',f0.9,' after ',i9,' steps ')
1002 format('  Wall Clock = ',f0.6)

and I get :

  Convergence = .100000000 after      8882 steps 

   Problem size =        24576

  Wall Clock = 2.492813

As you can see, "Convergence" value is .100000000 instead of 0.100000000 (leading zero has disappeared).

And what about the integers values, can I write "i0" to have as few as possible ?

Thanks

2 Answers2

0

Modern Fortran compilers understand a 'length' of 0 to mean: As few as possible:

program write_format
    use iso_fortran_env, only: real64

    implicit none
    print 1001, 5.213814
    print 1001, 12345678.901234_real64
1001 format("Wall Clock = ", f0.6)

end program write_format

Output:

Wall Clock = 5.213814
Wall Clock = 12345678.901234

Cheers

Usually it's not liked to update the question after the answer to ask additional questions, but since they're quite similar, I think it's okay.

Firstly, yes, format I0 means as few digits as necessary, and probably is what you want.

The second part is trickier, it seems to boil down to 'at least that many digits, but more if needed' -- and I don't think there's a format specifier for that (but I might be wrong).

I'd probably cheat and use something like this:

if (epsilon < 10.) then
    write(*, 1002) epsilon
else
    write(*, 1003) epsilon
end if

1002 format("Convergenge = ", f11.9)
1003 format("Convergence = ", f0.9)

But then again, I also found this answer quite intuitive: How to pad FORTRAN floating point output with leading zeros?

Adapted for you, it would mean splitting the floating point number into an integer and the rest, and putting it back together again:

write(*, 1002) int(epsilon), epsilon-int(epsilon)
1002 format("Convergence = ", I0, F0.9)
chw21
  • 7,970
  • 1
  • 16
  • 31
  • -@chw21 thanks for your answser. Nethertheless, could you take a look on my **UPDATE 1** after my first post. –  Sep 20 '17 at 23:25
0

this is a bit cumbersome, but one way to get minimum width and preserve the lead zero is to use an internal write like this:

 character*30 val
 write(val,'(f11.9)')0.1d0
 write(*,'(3a,i0,a)')'converge = ',trim(adjustl(val)),' after ',32432,' steps'

converge = 0.100000000 after 32432 steps

agentp
  • 6,849
  • 2
  • 19
  • 37