Printing function name in fortran 90

I wrote code that finds the root of the function whose name is provided among the arguments, I think I took it from Numerical Recipes. Sort of

rtsafe double-precision function (x_init, x1, x2, xacc, func, dfunc)

where func and dfunc are the names of two functions. Of course I use rtsafe with various func and dfunc functions. I would like to print the name of the func and dfunc functions being called when I'm inside rtsafe, because when there is an error in rtsafe, I would like to know which function I used. Sort of

write (,) "my func =", func

(?)

Does anyone know how to do this?

+4
source share
2 answers

You can add an optional argument to your functions, which returns the name of the function:

FUNCTION f(x, fname) RESULT (fx) IMPLICIT NONE REAL :: x, fx CHARACTER(LEN=*), OPTIONAL :: fname CHARACTER(LEN=*), PARAMETER :: myfname='somename' IF (present(fname)) fname=myfname fx = x ! or whatever else END FUNCTION f 

The first time you call your function in rtsafe, you will get the function name for later printing in case of an error.

Not tested this, but it should work more or less like that, and this is the only way I can do this in Fortran.

+1
source

Perhaps you can solve some kind of manual solution (pass the name of the function, then print it with “OK” ... or something like that), but print the names of the functions / routines ( reflects ) is impossible.

0
source

Source: https://habr.com/ru/post/1391117/


All Articles