apps130106_iotorus_performance.ioncrot_getdensityΒΆ

import time
import numpy as np
from pyana.pep import iotorus

def get_densities_via_function_multi(xyzcoords):
    ''' Get the denisty via the get_density method.
    '''

    torus = iotorus.TwoPeakModel()
    torus = iotorus.RotatingNeutralCloud2(torus)

    n = xyzcoords.shape[0]
    iophase = np.random.random(n)

    t0 = time.time()
    x = xyzcoords[:, 0]
    y = xyzcoords[:, 1]
    z = xyzcoords[:, 2]
    n = torus.get_density(x, y, z, iophase=iophase)

    t1 = time.time()
    return t1- t0, None

def get_densities_via_function(xyzcoords):
    ''' Get the denisty via the get_density method.

    :param xyzcoords: (N, 3) numpy array.
    :returns: stat, vals
    '''

    torus = iotorus.TwoPeakModel()
    torus = iotorus.RotatingNeutralCloud(torus)
    torus.set_iophase(np.deg2rad(90.))

    val = []

    t0 = time.time()

    x = xyzcoords[:, 0]
    y = xyzcoords[:, 1]
    z = xyzcoords[:, 2]
    n = torus.get_density(x, y, z)

    t1 = time.time()
    
    return t1 - t0, None


def get_density_via_function(xyzcoords):
    ''' Get the denisty via the get_density method.

    :param xyzcoords: (N, 3) numpy array.
    :returns: stat, vals
    '''

    torus = iotorus.TwoPeakModel()
    torus = iotorus.RotatingNeutralCloud(torus)
    torus.set_iophase(np.deg2rad(90.))

    val = []

    t0 = time.time()

    for xyz in xyzcoords:
        x, y, z = xyz
#        print x, y, z,
        try:
            n = torus.get_density(x, y, z)
        except ValueError as e:
            n = 0.
#        print n
        val.append(n)

    t1 = time.time()
    
    return t1 - t0, None


def main():
    '''Main script'''

    nsample = 10000   # Call times.

    xyzcoords = np.random.random([nsample, 3]) * 30. - 15.
    # Domains are -15 to 15.

    stat0, vals0 = get_density_via_function(xyzcoords)
    print('get_density method took %f sec / %d call' % (stat0, nsample))

    stat1, vals1 = get_densities_via_function(xyzcoords)
    print('get_density method (multiple) took %f sec / %d call' % (stat1, nsample))

    stat2, vals2 = get_densities_via_function_multi(xyzcoords)
    print('get_density method (multiple, 2) took %f sec / %d call' % (stat2, nsample))


#    stat1, vals1 = get_density_via_interpolation(xyzcoords)

    

if __name__ == "__main__":
    main()