[SciPy-User] Using ctypes with scipy.integrate

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[SciPy-User] Using ctypes with scipy.integrate

Rodrigo Araújo
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:
Inline image 3

Plane solution:
Inline image 1
where
Inline image 2

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
Reply | Threaded
Open this post in threaded view
|

Re: Using ctypes with scipy.integrate

kgulliks
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 <[hidden email]> 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:
Inline image 3

Plane solution:
Inline image 1
where
Inline image 2

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
Reply | Threaded
Open this post in threaded view
|

Re: Using ctypes with scipy.integrate

Rodrigo Araújo
Hi Kevin,

Thanks a lot, problem solved.

Best regards,

Rodrigo Araújo

On Wed, Nov 25, 2015 at 3:51 PM, Kevin Gullikson <[hidden email]> 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 <[hidden email]> 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:
Inline image 3

Plane solution:
Inline image 1
where
Inline image 2

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