Re: Any suggested workarounds for writing wrappers for functions that are to be passed as arguments?
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: Any suggested workarounds for writing wrappers for functions that are to be passed as arguments?         

Group: comp.lang.fortran · Group Profile
Author: Will W.
Date: Sep 4, 2008 10:47

On Sep 2, 11:45 pm, Ron Ford wrote:
> On Mon, 1 Sep 2008 21:02:59 -0700 (PDT), Will W. posted:
>
>> So what do I want to do?
>> ------------------------------------
>> Say I want to create a module of integration routines in xy Cartesian
>> geometry for integrating a user-specified function, f(x,y) over a
>> polygon.  Easy enough.
>
> I've never used fortran to integrate.
>
> What would f(x,y) look like in fortran, with, say, a parabola?
> --
> War will never cease until babies begin to come into the world with larger
> cerebrums and smaller adrenal glands. 2
> H. L. Mencken

You could do something like this.

FUNCTION Specific_Paraboloid(x,y) RESULT(f)
REAL,INTENT(IN) :: x,y
REAL :: f
!!--begin--A=1.0
B=2.0
C=3.0
D=4.0
E=5.0
F=6.0
f = A*x**2 + B*x + C*x*y + D*y**2 + E*y + F
!!--end--
END FUNCTION

A more general possible usage could be this, where I have "imported"
the coefficients from a module called VAR_General_Paraboloid (VAR is
for variables), so that one could set the coefficients A-F before
doing the integration and get different paraboloids.

FUNCTION General_Paraboloid(x,y) RESULT(f)
USE VAR_General_Paraboloid,ONLY: A,B,C,D,E,F
REAL,INTENT(IN) :: x,y
REAL :: f
!!--begin--
f = A*x**2 + B*x + C*x*y + D*y**2 + E*y + F
!!--end--
END FUNCTION

You can get even more general if you use a function parser, like
Stuart Midgley's, which lets you represent functions as text with
variables, then pass values of those variables to an evaluator, which
evaluates the function. This is very useful for reading functions
from input, for example, to read in general boundary conditions or
sources for a PDE solver. Then using the example of a 2D source,
which you would like to integrate over cells to get the cell-average
sources to stick in the RHS of a 2D finite volume method, the f(x,y)
might look like:

FUNCTION MySourceFunction(x,y) RESULT(f)
USE VAR_MySourceFunction,ONLY: FunctionID
REAL,INTENT(IN) :: x,y
REAL :: f
REAL :: error
!!--begin--

call s_evaluatefn(FunctionID, (/x,y/), f )

!!--end--
END FUNCTION

My source function is specified by the FunctionID handle, which is
returned when the function is created through the parser, with a call
like

call s_createfn( "1-tanh( (x-0.5)**2 + (y-0.4)**2 )" ,
"x y" , FunctionID )

Like I alluded to, I use the parser to read in boundary conditions as
functions, then use an integrator to evaluate the face-average values
on the boundary. Stuart Midgley's parser is quite nice. I highly
recommend it. It has quite a few functions, heaviside, hat, an if
statement if( logical-expression , true-val , false-val ), really more
of a substitution and the option to specify uncertainties with the
function's coefficients and propagate them
to output.
+W
no comments
diggit! del.icio.us! reddit!

RELATED THREADS
SubjectArticles qty Group
Hypergeometric functions and beta functionssci.math ·