source: sasmodels/code_capcyl.py @ 2de9a5e

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 2de9a5e was 2de9a5e, checked in by HMP1 <helen.park@…>, 10 years ago

Update for Aaron

  • Property mode set to 100644
File size: 4.3 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import numpy as np
5from math import asin
6import pyopencl as cl
7from weights import GaussianDispersion
8from sasmodel import card
9from Capcyl_Gauss import Gauss76Wt, Gauss76Z
10
11def set_precision(src, qx, qy, dtype):
12    qx = np.ascontiguousarray(qx, dtype=dtype)
13    qy = np.ascontiguousarray(qy, dtype=dtype)
14    if np.dtype(dtype) == np.dtype('float32'):
15        header = """\
16#define real float
17"""
18    else:
19        header = """\
20#pragma OPENCL EXTENSION cl_khr_fp64: enable
21#define real double
22"""
23    return header+src, qx, qy
24
25class GpuCapCylinder(object):
26    PARS = {'scale':1, 'rad_cyl':1, 'rad_cap':1, 'length':1, 'sld_capcyl':1e-6, 'sld_solv':0, 'background':0,
27             'theta':0, 'phi':0}
28
29    PD_PARS = ['rad_cyl', 'length', 'rad_cap', 'theta', 'phi']
30
31    def __init__(self, qx, qy, dtype='float32'):
32
33        #create context, queue, and build program
34        ctx,_queue = card()
35        trala = open('NR_BessJ1.cpp').read()+"\n"+open('Capcyl_Kfun.cpp').read()+"\n"+open('Kernel-Cylinder.cpp').read()
36        src, qx, qy = set_precision(trala, qx, qy, dtype=dtype)
37        self.prg = cl.Program(ctx, src).build()
38        self.qx, self.qy = qx, qy
39
40
41        #buffers
42        mf = cl.mem_flags
43        self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx)
44        self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy)
45        G, Z = Gauss76Wt, Gauss76Z
46        self.Gauss76W_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=G)
47        self.Gauss76Z_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=Z)
48        self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes)
49        self.res = np.empty_like(self.qx)
50        self.vol_i = float(0.0)
51        self.vol_b = cl.Buffer(ctx, mf.WRITE_ONLY, self.vol_i.nbytes)
52
53    def eval(self, pars):
54
55        _ctx,queue = card()
56        rad_cyl,length,rad_cap,theta,phi = \
57            [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma'])
58             for base in GpuCapCylinder.PD_PARS]
59
60        rad_cyl.value, rad_cyl.weight = rad_cyl.get_weights(pars['rad_cyl'], 0, 1000, True)
61        rad_cap.value, rad_cap.weight = rad_cap.get_weights(pars['rad_cap'], 0, 1000, True)
62        length.value, length.weight = length.get_weights(pars['length'], 0, 1000, True)
63        theta.value, theta.weight = theta.get_weights(pars['theta'], -90, 180, False)
64        phi.value, phi.weight = phi.get_weights(pars['phi'], -90, 180, False)
65
66        sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0
67        size = len(theta.weight)
68        sub = pars['sld_capcyl']-np.float32(['sld_solv'])
69
70        for i in xrange(len(rad_cyl.weight)):
71            for m in xrange(len(rad_cap.weight)):
72                for j in xrange(len(length.weight)):
73                    for k in xrange(len(theta.weight)):
74                        for l in xrange(len(phi.weight)):
75
76                            self.prg.CapCylinderKernel(queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b,
77                                        self.vol_b, np.float32(rad_cyl.value[i]), np.float32(rad_cap.value[m]), np.float32(length.value[j]),
78                                        np.float32(theta.value[k]), np.float32(phi.value[l]), np.float32(sub), np.float32(pars['scale']),
79                                        np.float32(phi.weight[l]), np.float32(theta.weight[k]), np.float32(rad_cap.weight[m]),
80                                        np.float32(rad_cyl.weight[i]), np.float32(length.weight[j]), np.uint32(self.qx.size), np.uint32(size),
81                                        self.Gauss76W_b, self.Gauss76Z_b)
82
83                            cl.enqueue_copy(queue, self.res, self.res_b)
84                            cl.enqueue_copy(queue, self.vol_i, self.vol_b)
85
86                            sum += self.res
87                            vol += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]*self.vol_i
88                            norm_vol += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]
89                            norm += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]*theta.weight[k]*phi.weight[l]
90
91        if size > 1:
92            norm /= asin(1.0)
93
94        if vol != 0.0 and norm_vol != 0.0:
95            sum *= norm_vol/vol
96
97        return sum/norm + pars['background']
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Note: See TracBrowser for help on using the repository browser.