[dbb0048] | 1 | #!/usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | import numpy as np |
---|
| 5 | import pyopencl as cl |
---|
[473183c] | 6 | |
---|
[dbb0048] | 7 | from weights import GaussianDispersion |
---|
[a42fec0] | 8 | from sasmodel import card, set_precision |
---|
[dbb0048] | 9 | |
---|
| 10 | class GpuEllipse(object): |
---|
| 11 | PARS = { |
---|
| 12 | 'scale':1, 'radius_a':1, 'radius_b':1, 'sldEll':1e-6, 'sldSolv':0, 'background':0, 'axis_theta':0, 'axis_phi':0, |
---|
| 13 | } |
---|
| 14 | PD_PARS = ['radius_a', 'radius_b', 'axis_theta', 'axis_phi'] |
---|
| 15 | |
---|
| 16 | def __init__(self, qx, qy, dtype='float32'): |
---|
| 17 | |
---|
| 18 | ctx,_queue = card() |
---|
[ca6c007] | 19 | src, qx, qy = set_precision(open('Kernel/Kernel-Ellipse.cpp').read(), qx, qy, dtype=dtype) |
---|
[dbb0048] | 20 | self.prg = cl.Program(ctx, src).build() |
---|
| 21 | self.qx, self.qy = qx, qy |
---|
| 22 | |
---|
| 23 | #buffers |
---|
| 24 | mf = cl.mem_flags |
---|
| 25 | self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx) |
---|
| 26 | self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy) |
---|
| 27 | self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes) |
---|
| 28 | self.res = np.empty_like(self.qx) |
---|
| 29 | |
---|
| 30 | def eval(self, pars): |
---|
| 31 | #b_n = radius_b # want, a_n = radius_a # want, etc |
---|
| 32 | _ctx,queue = card() |
---|
[a42fec0] | 33 | self.res[:] = 0 |
---|
| 34 | cl.enqueue_copy(queue, self.res_b, self.res) |
---|
[dbb0048] | 35 | radius_a, radius_b, axis_theta, axis_phi = \ |
---|
| 36 | [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma']) |
---|
| 37 | for base in GpuEllipse.PD_PARS] |
---|
| 38 | |
---|
| 39 | radius_a.value, radius_a.weight = radius_a.get_weights(pars['radius_a'], 0, 10000, True) |
---|
| 40 | radius_b.value, radius_b.weight = radius_b.get_weights(pars['radius_b'], 0, 10000, True) |
---|
| 41 | axis_theta.value, axis_theta.weight = axis_theta.get_weights(pars['axis_theta'], -90, 180, False) |
---|
| 42 | axis_phi.value, axis_phi.weight = axis_phi.get_weights(pars['axis_phi'], -90, 180, False) |
---|
| 43 | |
---|
| 44 | #Perform the computation, with all weight points |
---|
| 45 | sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0 |
---|
| 46 | size = len(axis_theta.weight) |
---|
| 47 | sub = pars['sldEll'] - pars['sldSolv'] |
---|
| 48 | real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64 |
---|
| 49 | |
---|
| 50 | #Loop over radius weight points |
---|
| 51 | for i in xrange(len(radius_a.weight)): |
---|
| 52 | #Loop over length weight points |
---|
| 53 | for j in xrange(len(radius_b.weight)): |
---|
| 54 | #Average over theta distribution |
---|
[1726b21] | 55 | vol += radius_a.weight[i]*radius_b.weight[j]*pow(radius_b.value[j], 2)*radius_a.value[i] |
---|
| 56 | norm_vol += radius_a.weight[i]*radius_b.weight[j] |
---|
| 57 | |
---|
[dbb0048] | 58 | for k in xrange(len(axis_theta.weight)): |
---|
| 59 | #Average over phi distribution |
---|
| 60 | for l in xrange(len(axis_phi.weight)): |
---|
| 61 | #call the kernel |
---|
| 62 | self.prg.EllipsoidKernel(queue, self.qx.shape, None, real(radius_a.weight[i]), |
---|
| 63 | real(radius_b.weight[j]), real(axis_theta.weight[k]), |
---|
| 64 | real(axis_phi.weight[l]), real(pars['scale']), real(radius_a.value[i]), |
---|
| 65 | real(radius_b.value[j]), real(sub), real(axis_theta.value[k]), |
---|
| 66 | real(axis_phi.value[l]), self.qx_b, self.qy_b, self.res_b, |
---|
| 67 | np.uint32(self.qx.size), np.uint32(len(axis_theta.weight))) |
---|
[a42fec0] | 68 | |
---|
[dbb0048] | 69 | norm += radius_a.weight[i]*radius_b.weight[j]*axis_theta.weight[k]*axis_phi.weight[l] |
---|
[1726b21] | 70 | |
---|
[dbb0048] | 71 | # Averaging in theta needs an extra normalization |
---|
| 72 | # factor to account for the sin(theta) term in the |
---|
| 73 | # integration (see documentation). |
---|
| 74 | |
---|
| 75 | # if size > 1: |
---|
| 76 | # norm /= math.asin(1.0) |
---|
[a42fec0] | 77 | cl.enqueue_copy(queue, self.res, self.res_b) |
---|
| 78 | sum += self.res |
---|
[dbb0048] | 79 | if vol != 0.0 and norm_vol != 0.0: |
---|
| 80 | sum *= norm_vol/vol |
---|
| 81 | |
---|
| 82 | return sum/norm+pars['background'] |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | def demo(): |
---|
| 86 | from time import time |
---|
| 87 | import matplotlib.pyplot as plt |
---|
| 88 | |
---|
| 89 | #create qx and qy evenly spaces |
---|
| 90 | qx = np.linspace(-.02, .02, 128) |
---|
| 91 | qy = np.linspace(-.02, .02, 128) |
---|
| 92 | qx, qy = np.meshgrid(qx, qy) |
---|
| 93 | |
---|
| 94 | #saved shape of qx |
---|
| 95 | r_shape = qx.shape |
---|
| 96 | #reshape for calculation; resize as float32 |
---|
| 97 | qx = qx.flatten() |
---|
| 98 | qy = qy.flatten() |
---|
| 99 | |
---|
| 100 | #int main |
---|
| 101 | pars = EllipsoidParameters(.027, 60, 180, .297e-6, 5.773e-06, 4.9, 0, 90) |
---|
| 102 | |
---|
| 103 | t = time() |
---|
| 104 | result = GpuEllipse(qx, qy) |
---|
| 105 | result.x = result.ellipsoid_fit(qx, qy, pars, b_n=35, t_n=35, a_n=1, p_n=1, sigma=3, b_w=.1, t_w=.1, a_w=.1, p_w=.1) |
---|
| 106 | result.x = np.reshape(result.x, r_shape) |
---|
| 107 | tt = time() |
---|
| 108 | print("Time taken: %f" % (tt - t)) |
---|
| 109 | |
---|
| 110 | plt.pcolormesh(result.x) |
---|
| 111 | plt.show() |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | if __name__ == "__main__": |
---|
| 115 | demo() |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|