Dynamic output format setting

2019-01-29 03:57发布

问题:

I tried to make the output format dynamically in the sense that the number of variables to be printed out could be varied dynamically. I had done some experiment with the following two methods (see the context below), but both of them led to a error message like this:

forrtl: error (63): output conversion error, unit 1016, file /panfs/roc/Node_ 16.txt

The first method uses a string to specify the output format, for example,

real a(4) = [1 2 3 4]
int size = 4
write(string,'(a,i3,a)') '(a,',size,'(f9.4))'
write(*, string) a(:)

The second method is what I just learnt from the Internet, which occupies only one line, but it didn't work either:

write(*,'(a,<size>f9.4)') a(:)

Please help me with this format setting. Thanks.

EDIT: I found the culprit of the problem. In my real project, some element of the array "a" is pretty huge such that f9.4 is not suitable to display the full array. To fix the problem, I replace f9.4 with something like e11.3.

回答1:

Fortran recently added * as an unlimited format repeater. e.g., '( *(2X, F3.1) )' This is easier to use than a dynamic format.



回答2:

You don't need to supply exact number of values, indicating more is ok. The normal way is to use a large enough value, like

        '(a,999f9.4)'

in Fortran 2008 u can use the feature M.S.B. shows *999(f9.4).

If you need dynamic string for some other purpose, use the concatenation operator //.