# [SciPy-User] Using ctypes with scipy.integrate

3 messages
Open this post in threaded view
|

## [SciPy-User] Using ctypes with scipy.integrate

 Hello everyone,I'm using Python and SciPy to develop a reservoir flow simulator for my master thesis and part of the solution includes solving some integrals in order to build a coefficient matrix.This solution is based on using the solution for a source point acting at an observation point and integrating its value considering that the source is a plane instead of a point, as shown below:Point solution:Plane solution:whereI'm using scipy.integrate.nquad to integrate based on [1] using the following python function, which receives multiple arguments.def deriv_green_2d(self, x, y, sx, sy, sz, ox, oy, oz, ori, sqrt_u, dn):    """Calculates Green's function derivative value on two dimensions.    Keyword arguments:    x,y -- integration variables    sx, sy, sz -- source point coordinates    ox, oy, oz -- observation point coordinates    ori -- source orientation (wether it's a xy, xz or yz plane)    sqrt_u -- square root of Laplace's variable value    dn -- normal distance between the points    """    # linear distance    if ori == 0:        r = sqrt((sx-ox)**2 + (sy-oy+x)**2 + (sz-oz+y)**2)    elif ori == 1:        r = sqrt((sx-ox+x)**2 + (sy-oy)**2 + (sz-oz+y)**2)    else:        r = sqrt((sx-ox+x)**2 + (sy-oy+y)**2 + (sz-oz)**2)    return dn/r**2 * (sqrt_u + 1/r) * exp(-r*sqrt_u)As this integral gets solved multiple times for each timestep, it slows down the simulator performance.In order to improve the speed, I'm trying to use ctypes as shown in [2], but in the example provided, the only parameters for the c function are the integration variables and, as show above, I need multiple different arguments besides the integration variables (sx, sy, sz, ox, oy, oz, ori, sqrt_u and dn). Does anyone know if this possible on SciPy v0.16.1?Best regards and thanks in advance,Rodrigo Araújo _______________________________________________ SciPy-User mailing list [hidden email] https://mail.scipy.org/mailman/listinfo/scipy-user
Open this post in threaded view
|

## Re: Using ctypes with scipy.integrate

Rodrigo,

Yeah, that is definitely possible. You just have to pass the additional arguments using the 'args' keyword in your call to integrate.nquad. Here is a snippet of the C code that I use with integrate.quad for one of my problems.:

 double q_integrand_logisticQ_malmquist(int n, double args[n]) { //unpack arguments double q = args[0]; double gamma = args[1]; double alpha = args[2]; double beta = args[3];

Kevin Gullikson
PhD Candidate
University of Texas Astronomy
RLM 15.310E

On Wed, Nov 25, 2015 at 12:43 PM, Rodrigo Araújo wrote:
Hello everyone,

I'm using Python and SciPy to develop a reservoir flow simulator for my master thesis and part of the solution includes solving some integrals in order to build a coefficient matrix.

This solution is based on using the solution for a source point acting at an observation point and integrating its value considering that the source is a plane instead of a point, as shown below:

Point solution:

Plane solution:
where

I'm using scipy.integrate.nquad to integrate based on [1] using the following python function, which receives multiple arguments.

def deriv_green_2d(self, x, y, sx, sy, sz, ox, oy, oz, ori, sqrt_u, dn):
"""Calculates Green's function derivative value on two dimensions.

Keyword arguments:
x,y -- integration variables
sx, sy, sz -- source point coordinates
ox, oy, oz -- observation point coordinates
ori -- source orientation (wether it's a xy, xz or yz plane)
sqrt_u -- square root of Laplace's variable value
dn -- normal distance between the points
"""
# linear distance
if ori == 0:
r = sqrt((sx-ox)**2 + (sy-oy+x)**2 + (sz-oz+y)**2)
elif ori == 1:
r = sqrt((sx-ox+x)**2 + (sy-oy)**2 + (sz-oz+y)**2)
else:
r = sqrt((sx-ox+x)**2 + (sy-oy+y)**2 + (sz-oz)**2)

return dn/r**2 * (sqrt_u + 1/r) * exp(-r*sqrt_u)

As this integral gets solved multiple times for each timestep, it slows down the simulator performance.

In order to improve the speed, I'm trying to use ctypes as shown in [2], but in the example provided, the only parameters for the c function are the integration variables and, as show above, I need multiple different arguments besides the integration variables (sx, sy, sz, ox, oy, oz, ori, sqrt_u and dn).

Does anyone know if this possible on SciPy v0.16.1?

Best regards and thanks in advance,

Rodrigo Araújo

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user
Open this post in threaded view
|

## Re: Using ctypes with scipy.integrate

Hi Kevin,

Thanks a lot, problem solved.

Best regards,

Rodrigo Araújo

On Wed, Nov 25, 2015 at 3:51 PM, Kevin Gullikson wrote:
Rodrigo,

Yeah, that is definitely possible. You just have to pass the additional arguments using the 'args' keyword in your call to integrate.nquad. Here is a snippet of the C code that I use with integrate.quad for one of my problems.:

 double q_integrand_logisticQ_malmquist(int n, double args[n]) { //unpack arguments double q = args[0]; double gamma = args[1]; double alpha = args[2]; double beta = args[3];

Kevin Gullikson
PhD Candidate
University of Texas Astronomy
RLM 15.310E

On Wed, Nov 25, 2015 at 12:43 PM, Rodrigo Araújo wrote:
Hello everyone,

I'm using Python and SciPy to develop a reservoir flow simulator for my master thesis and part of the solution includes solving some integrals in order to build a coefficient matrix.

This solution is based on using the solution for a source point acting at an observation point and integrating its value considering that the source is a plane instead of a point, as shown below:

Point solution:

Plane solution:
where

I'm using scipy.integrate.nquad to integrate based on [1] using the following python function, which receives multiple arguments.

def deriv_green_2d(self, x, y, sx, sy, sz, ox, oy, oz, ori, sqrt_u, dn):
"""Calculates Green's function derivative value on two dimensions.

Keyword arguments:
x,y -- integration variables
sx, sy, sz -- source point coordinates
ox, oy, oz -- observation point coordinates
ori -- source orientation (wether it's a xy, xz or yz plane)
sqrt_u -- square root of Laplace's variable value
dn -- normal distance between the points
"""
# linear distance
if ori == 0:
r = sqrt((sx-ox)**2 + (sy-oy+x)**2 + (sz-oz+y)**2)
elif ori == 1:
r = sqrt((sx-ox+x)**2 + (sy-oy)**2 + (sz-oz+y)**2)
else:
r = sqrt((sx-ox+x)**2 + (sy-oy+y)**2 + (sz-oz)**2)

return dn/r**2 * (sqrt_u + 1/r) * exp(-r*sqrt_u)

As this integral gets solved multiple times for each timestep, it slows down the simulator performance.

In order to improve the speed, I'm trying to use ctypes as shown in [2], but in the example provided, the only parameters for the c function are the integration variables and, as show above, I need multiple different arguments besides the integration variables (sx, sy, sz, ox, oy, oz, ori, sqrt_u and dn).

Does anyone know if this possible on SciPy v0.16.1?

Best regards and thanks in advance,

Rodrigo Araújo

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user

_______________________________________________
SciPy-User mailing list
[hidden email]
https://mail.scipy.org/mailman/listinfo/scipy-user