[SciPy-User] Using ctypes with scipy.integrate

Kevin Gullikson kevin.gullikson at gmail.com
Wed Nov 25 13:51:01 EST 2015


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 <alf.rodrigo at gmail.com>
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:
> [image: Inline image 3]
>
> Plane solution:
> [image: Inline image 1]
> where
> [image: 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
>> [1]:
> http://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html#general-multiple-integration-dblquad-tplquad-nquad
> [2]:
> http://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html#faster-integration-using-ctypes
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> https://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20151125/4c4612b7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Point.png
Type: image/png
Size: 5065 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20151125/4c4612b7/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2D_d_integral.png
Type: image/png
Size: 7489 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20151125/4c4612b7/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rd.png
Type: image/png
Size: 3605 bytes
Desc: not available
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20151125/4c4612b7/attachment-0002.png>


More information about the SciPy-User mailing list