4

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

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

where func and dfunc are two functions' names. Of course I use rtsafe with different function func and dfunc. I would like to print the name of the called functions func and dfunc when I am inside rtsafe, because when there is an error in rtsafe I would like to know which function I was using. Something like

write(,)"my func = ", func

(?)

Does anybody know how to do that?

simona
  • 2,009
  • 6
  • 29
  • 41

2 Answers2

1

You could add an optional argument in your functions that 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

In the first call to your function in rtsafe you get the name of the function for later printing in case of an error.

Did not test this but it should work more or less like this, and it the only way I can think of to do this in Fortran.

s-h
  • 11
  • 1
0

Maybe you can work up some manual solution (pass the name of the function, then print it with "OK" ... or something like that), but printing the names of the functions/subroutines (reflecting) is not possible.

Rook
  • 60,248
  • 49
  • 165
  • 242
  • Cannot it print the pointer to the function? I know that there are no pointers in fortran, but something similar? In this case I could write a table pointers-->'function name'. Given a pointer, I'd read the name from the table. – simona Jan 15 '12 at 19:19
  • @simona - There are pointers in fortran too, but they're not the same thing as in C. That being said, I really couldn't say ... I've never had the need to do something like that, so would be speaking off the top of my head. Leave the question, there are some more knowledgeable people here who might figure out a solution. But really, can't you just print out a control statement after the function has finished or something dead simple - if it's for the purpose you stated. – Rook Jan 15 '12 at 19:25
  • I would like to print only when the calling function rtsaf fails. Because the error is not critical and the execution goes on, if I print a control statement when the func finishes, I'll be submerged with output from func... – simona Jan 15 '12 at 19:58
  • OK. This rtsafe is a root finder function. I'm finding the roots of a function func1 with rtsafe, but func1 inside calls another instance of rtsafe applied on a function func2. – simona Jan 15 '12 at 22:02
  • rtsafe -> func1 -> rtsafe ->func2. Now the failure happens when func is not bracketed, then rtsafe states an error and returns 0. that should be OK (?) when it happens to func2, because when 0 is returned inside func1 that should result in a bad value in output of func1 (say y), that then goes in rtsafe. The idea is that when func1 returns y inside rtsafe, then that value is such that y!=0 and rtsafe discards it. – simona Jan 15 '12 at 22:20
  • The point here is that there is another function func3,that is defined by rtsafe->func1->rtsafe->func2 and depends on two parameters.Now for some values of these parameters func1 and func2 are not well-defined,they return nan or garbage,so I have to stay away from that values.But if I restrict the range of the parameters then these functions are not invertible(no bracketing),and I need to deal with this case somehow smoothly.Maybe this isn't the best approach,but I'm still thinking how to solve it.Meanwhile I would like to understand if I am correct when I think that bracketing fails in func2 – simona Jan 15 '12 at 22:28