source: sasmodels/lamellarcode.py @ 8a20be5

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

Added a fit2 (fits two different models at different angles)
(preliminary) Added CoreshellCyl? and CapCyl? Kernels
(preliminary) Updated kernels to include functions

  • Property mode set to 100644
File size: 3.0 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import numpy as np
5import pyopencl as cl
6from weights import GaussianDispersion
7
8def set_precision(src, qx, qy, dtype):
9    qx = np.ascontiguousarray(qx, dtype=dtype)
10    qy = np.ascontiguousarray(qy, dtype=dtype)
11    if dtype == 'double':
12        header = """\
13#pragma OPENCL EXTENSION cl_khr_fp64: enable
14#define real double
15"""
16        return header+src,qx,qy
17    else:
18        return src,qx,qy
19
20
21class GpuLamellar(object):
22    PARS = {
23        'scale':1, 'bi_thick':1, 'sld_bi':1e-6, 'sld_sol':0, 'background':0,
24    }
25
26    def __init__(self, qx, qy, dtype='float'):
27
28        #create context, queue, and build program
29        self.ctx = cl.create_some_context()
30        self.queue = cl.CommandQueue(self.ctx)
31        src,qx,qy = set_precision(open('Kernel-Lamellar.cpp').read(), qx, qy, dtype=dtype)
32        self.prg = cl.Program(self.ctx, src).build()
33        self.qx, self.qy = qx, qy
34
35        #buffers
36        mf = cl.mem_flags
37        self.qx_b = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qx)
38        self.qy_b = cl.Buffer(self.ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=self.qy)
39        self.res_b = cl.Buffer(self.ctx, mf.WRITE_ONLY, qx.nbytes)
40        self.res = np.empty_like(self.qx)
41
42    def eval(self, pars):
43
44        bi_thick = GaussianDispersion(int(pars['bi_thick_pd_n']), pars['bi_thick_pd'], pars['bi_thick_pd_nsigma'])
45        bi_thick.value, bi_thick.weight = bi_thick.get_weights(pars['bi_thick'], 0, 1000, True)
46
47        sum, norm = 0.0, 0.0
48        sub = pars['sld_bi'] - pars['sld_sol']
49
50        real = np.float32 if self.qx.dtype == np.dtype('float32') else np.float64
51        for i in xrange(len(bi_thick.weight)):
52            self.prg.LamellarKernel(self.queue, self.qx.shape, None, self.qx_b, self.qy_b, self.res_b, real(bi_thick.value[i]),
53                                    real(pars['scale']), real(sub), real(pars['background']), np.uint32(self.qx.size))
54            cl.enqueue_copy(self.queue, self.res, self.res_b)
55
56            sum += bi_thick.weight[i]*self.res
57            norm += bi_thick.weight[i]
58
59        return sum/norm + pars['background']
60
61def demo():
62
63    from time import time
64    import matplotlib.pyplot as plt
65
66    #create qx and qy evenly spaces
67    qx = np.linspace(-.01, .01, 128)
68    qy = np.linspace(-.01, .01, 128)
69    qx, qy = np.meshgrid(qx, qy)
70
71    #saved shape of qx
72    r_shape = qx.shape
73
74    #reshape for calculation; resize as float32
75    qx = qx.flatten()
76    qy = qy.flatten()
77
78    pars = LamellarParameters(scale=1, bi_thick=100, sld_bi=.291e-6, sld_sol=5.77e-6, background=0)
79    t = time()
80    result = GpuLamellar(qx, qy)
81    result.x = result.lamellar_fit(pars, b_n=35, b_w=.1, sigma=3)
82    result.x = np.reshape(result.x, r_shape)
83    tt = time()
84
85    print("Time taken: %f" % (tt - t))
86
87    f = open("r.txt", "w")
88    for x in xrange(len(r_shape)):
89        f.write(str(result.x[x]))
90        f.write("\n")
91
92    plt.pcolormesh(np.log10(result.x))
93    plt.show()
94
95
96if __name__ == "__main__":
97    demo()
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
Note: See TracBrowser for help on using the repository browser.