From: deltaquattro on
Hi,

I have a numerical integration subroutine quad and I want to use it to
perform an complex integral in my code. quad has an interface for a
function which depends only on x:

quad(func,a,b)
...
interface
real function func(x)
real :: x
end func
end interface
...
end quad

However, the function to be integrated depends on many parameters, not
only on x. How can I pass it to quad? Is it better to modify the
interface in quad (and all the subroutines called by quad), or to
create a module containing func and all the modules needed by it? Have
you ever met a similar problem? Thanks

best regards

deltaquattro
From: Arjen Markus on
On 17 apr, 12:00, deltaquattro <deltaquat...(a)gmail.com> wrote:
> Hi,
>
> I have a numerical integration subroutine quad and I want to use it to
> perform an complex integral in my code. quad has an interface for a
> function which depends only on x:
>
> quad(func,a,b)
> ..
> interface
>   real function func(x)
>   real :: x
>   end func
> end interface
> ..
> end quad
>
> However, the function to be integrated depends on many parameters, not
> only on x. How can I pass it to quad? Is it better to modify the
> interface in quad (and all the subroutines called by quad), or to
> create a module containing func and all the modules needed by it? Have
> you ever met a similar problem? Thanks
>
> best regards
>
> deltaquattro

Yes, it is a classic one. Fortran 2003 offers a couple
of solutions, but in Fortran 90/95 the easiest way is
to put the function and the data it needs in a module.
The data are then part of the module and the function
can be passed to the numerical routine(s).

For instance:

module my_set_of_functions ! More than one possible

type func_parameters
...
end type

type(func_parameters) :: data
contains

subroutine set_parameters(params)
type(func_parameters) :: params

data = params
end subroutine

real function func(x)
... use x and data to compute the value
end function


real function func2(x,y)
... use x, y, and data to compute the value
end function

etc.

Regards,

Arjen
From: deltaquattro on
On 17 Apr, 13:26, Arjen Markus <arjen.mar...(a)wldelft.nl> wrote:
> On 17 apr, 12:00, deltaquattro <deltaquat...(a)gmail.com> wrote:
>
>
>
> > Hi,
>
> > I have a numerical integration subroutine quad and I want to use it to
> > perform an complex integral in my code. quad has an interface for a
> > function which depends only on x:
>
> > quad(func,a,b)
> > ..
> > interface
> > real function func(x)
> > real :: x
> > end func
> > end interface
> > ..
> > end quad
>
> > However, the function to be integrated depends on many parameters, not
> > only on x. How can I pass it to quad? Is it better to modify the
> > interface in quad (and all the subroutines called by quad), or to
> > create a module containing func and all the modules needed by it? Have
> > you ever met a similar problem? Thanks
>
> > best regards
>
> > deltaquattro
>
> Yes, it is a classic one. Fortran 2003 offers a couple
> of solutions, but in Fortran 90/95 the easiest way is
> to put the function and the data it needs in a module.
> The data are then part of the module and the function
> can be passed to the numerical routine(s).
>
> For instance:
>
> module my_set_of_functions ! More than one possible
>
> type func_parameters
> ...
> end type
>
> type(func_parameters) :: data
> contains
>
> subroutine set_parameters(params)
> type(func_parameters) :: params
>
> data = params
> end subroutine
>
> real function func(x)
> ... use x and data to compute the value
> end function
>
> real function func2(x,y)
> ... use x, y, and data to compute the value
> end function
>
> etc.
>
> Regards,
>
> Arjen

Hi, Arjen,

does quad (and all of its subroutines!!!) need to use
my_set_of_functions? Or is this necessary only for the sub calling
quad? thanks,

best,

deltaquattro

From: Arjen Markus on
On 17 apr, 13:55, deltaquattro <deltaquat...(a)gmail.com> wrote:
> On 17 Apr, 13:26, Arjen Markus <arjen.mar...(a)wldelft.nl> wrote:
>
>
>
>
>
> > On 17 apr, 12:00, deltaquattro <deltaquat...(a)gmail.com> wrote:
>
> > > Hi,
>
> > > I have a numerical integration subroutine quad and I want to use it to
> > > perform an complex integral in my code. quad has an interface for a
> > > function which depends only on x:
>
> > > quad(func,a,b)
> > > ..
> > > interface
> > >   real function func(x)
> > >   real :: x
> > >   end func
> > > end interface
> > > ..
> > > end quad
>
> > > However, the function to be integrated depends on many parameters, not
> > > only on x. How can I pass it to quad? Is it better to modify the
> > > interface in quad (and all the subroutines called by quad), or to
> > > create a module containing func and all the modules needed by it? Have
> > > you ever met a similar problem? Thanks
>
> > > best regards
>
> > > deltaquattro
>
> > Yes, it is a classic one. Fortran 2003 offers a couple
> > of solutions, but in Fortran 90/95 the easiest way is
> > to put the function and the data it needs in a module.
> > The data are then part of the module and the function
> > can be passed to the numerical routine(s).
>
> > For instance:
>
> > module my_set_of_functions ! More than one possible
>
> >    type func_parameters
> >        ...
> >    end type
>
> >    type(func_parameters) :: data
> > contains
>
> > subroutine set_parameters(params)
> >     type(func_parameters) :: params
>
> >     data = params
> > end subroutine
>
> > real function func(x)
> >     ... use x and data to compute the value
> > end function
>
> > real function func2(x,y)
> >     ... use x, y, and data to compute the value
> > end function
>
> > etc.
>
> > Regards,
>
> > Arjen
>
> Hi, Arjen,
>
> does quad (and all of its subroutines!!!) need to use
> my_set_of_functions? Or is this necessary only for the sub calling
> quad? thanks,
>
> best,
>
> deltaquattro- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

Only the subroutine that calls quad needs to know of the
specific function - quad is independent of that module.

Regards,

Arjen
From: Ron Shepard on
In article
<92398d99-ea4c-414f-8ef4-9dee6492fff7(a)8g2000hsu.googlegroups.com>,
deltaquattro <deltaquattro(a)gmail.com> wrote:

> However, the function to be integrated depends on many parameters, not
> only on x. How can I pass it to quad? Is it better to modify the
> interface in quad (and all the subroutines called by quad), or to
> create a module containing func and all the modules needed by it? Have
> you ever met a similar problem? Thanks

This is the standard interface problem. Do not modify your quad()
subroutine. If you do that, then you might as well not even use the
dummy subroutine, you could just hardwire the call to your specific
code, eliminating all advantages of having a modular reusable quad()
routine for numerical integration.

With f77, the usual approach was to put all of the other parameters
(other than x) into a common block. The actual function (associated
with the dummy func()) could than access and/or modify those common
block variables as appropriate.

With f90 and later, a better approach is to put all of the extra
parameters into a module, along with the actual function and any
auxiliary subroutines it might need. This gives you the ability to
use derived data types and all of the power of f90, such as explicit
interfaces, even though the dummy routine only has one variable "x"
in its argument list. If you need to integrate a dozen functions,
then you simply write a dozen modules, each supporting a specific
function, and your quad() routine works correctly with all of them.

$.02 -Ron Shepard