source: sasmodels/capcylcope.py @ 8faffcd

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

Update for Aaron

  • Property mode set to 100644
File size: 4.0 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
9
10def set_precision(src, qx, qy, dtype):
11    qx = np.ascontiguousarray(qx, dtype=dtype)
12    qy = np.ascontiguousarray(qy, dtype=dtype)
13    if np.dtype(dtype) == np.dtype('float32'):
14        header = """\
15#define real float
16"""
17    else:
18        header = """\
19#pragma OPENCL EXTENSION cl_khr_fp64: enable
20#define real double
21"""
22    return header+src, qx, qy
23
24class GpuCapCylinder(object):
25    PARS = {'scale':1, 'rad_cyl':1, 'rad_cap':1, 'length':1, 'sld_capcyl':1e-6, 'sld_solv':0, 'background':0,
26             'theta':0, 'phi':0}
27
28    PD_PARS = ['rad_cyl', 'length', 'rad_cap', 'theta', 'phi']
29
30    def __init__(self, qx, qy, dtype='float32'):
31
32        #create context, queue, and build program
33        ctx,_queue = card()
34        trala = open('NR_BessJ1.cpp').read()+"\n"+open('Capcyl_Kfun.cpp').read()+"\n"+open('Kernel-Cylinder.cpp').read()
35        src, qx, qy = set_precision(trala, qx, qy, dtype=dtype)
36
37        self.prg = cl.Program(ctx, open('Kernel-CapCyl.cpp').read()).build()
38
39        #buffers
40        mf = cl.mem_flags
41        self.qx_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx)
42        self.qy_b = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy)
43        self.res_b = cl.Buffer(ctx, mf.WRITE_ONLY, qx.nbytes)
44        self.res = np.empty_like(self.qx)
45        self.vol_i = float(0.0)
46        self.vol_b = cl.Buffer(ctx, mf.WRITE_ONLY, self.vol_i.nbytes)
47
48    def eval(self, pars):
49
50        _ctx,queue = card()
51        rad_cyl,length,rad_cap,theta,phi = \
52            [GaussianDispersion(int(pars[base+'_pd_n']), pars[base+'_pd'], pars[base+'_pd_nsigma'])
53             for base in GpuCapCylinder.PD_PARS]
54
55        rad_cyl.value, rad_cyl.weight = rad_cyl.get_weights(pars['rad_cyl'], 0, 1000, True)
56        rad_cap.value, rad_cap.weight = rad_cap.get_weights(pars['rad_cap'], 0, 1000, True)
57        length.value, length.weight = length.get_weights(pars['length'], 0, 1000, True)
58        theta.value, theta.weight = theta.get_weights(pars['theta'], -90, 180, False)
59        phi.value, phi.weight = phi.get_weights(pars['phi'], -90, 180, False)
60
61        sum, norm, norm_vol, vol = 0.0, 0.0, 0.0, 0.0
62        size = len(theta.weight)
63        sub = pars['sld_capcyl']-np.float32(['sld_solv'])
64
65        for i in xrange(len(rad_cyl.weight)):
66            for m in xrange(len(rad_cap.weight)):
67                for j in xrange(len(length.weight)):
68                    for k in xrange(len(theta.weight)):
69                        for l in xrange(len(phi.weight)):
70
71                            self.prg.CapCylinderKernel(queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b,
72                                        self.vol_b, np.float32(rad_cyl.value[i]), np.float32(rad_cap.value[m]), np.float32(length.value[j]),
73                                        np.float32(theta.value[k]), np.float32(phi.value[l]), np.float32(sub), np.float32(pars['scale']),
74                                        np.float32(phi.weight[l]), np.float32(theta.weight[k]), np.float32(rad_cap.weight[m]),
75                                        np.float32(rad_cyl.weight[i]), np.float32(length.weight[j]), np.uint32(self.qx.size), np.uint32(size))
76
77                            cl.enqueue_copy(queue, self.res, self.res_b)
78                            cl.enqueue_copy(queue, self.vol_i, self.vol_b)
79
80                            sum += self.res
81                            vol += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]*self.vol_i
82                            norm_vol += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]
83                            norm += rad_cyl.weight[i]*length.weight[j]*rad_cap.weight[m]*theta.weight[k]*phi.weight[l]
84
85        if size > 1:
86            norm /= asin(1.0)
87
88        if vol != 0.0 and norm_vol != 0.0:
89            sum *= norm_vol/vol
90
91        return sum/norm + pars['background']
92
93
94
95
96
97
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
Note: See TracBrowser for help on using the repository browser.