[b3f6bc3] | 1 | import numpy as np |
---|
[c85db69] | 2 | from numpy import pi, cos |
---|
[b3f6bc3] | 3 | |
---|
[c85db69] | 4 | from .generate import F64 |
---|
[b3f6bc3] | 5 | |
---|
[f734e7d] | 6 | class PyModel(object): |
---|
| 7 | def __init__(self, info): |
---|
| 8 | self.info = info |
---|
[3c56da87] | 9 | |
---|
| 10 | def __call__(self, q_input): |
---|
| 11 | kernel = self.info['Iqxy'] if q_input.is_2D else self.info['Iq'] |
---|
| 12 | return PyKernel(kernel, self.info, q_input) |
---|
| 13 | |
---|
| 14 | # pylint: disable=no-self-use |
---|
[f734e7d] | 15 | def make_input(self, q_vectors): |
---|
| 16 | return PyInput(q_vectors, dtype=F64) |
---|
[3c56da87] | 17 | |
---|
[f734e7d] | 18 | def release(self): |
---|
| 19 | pass |
---|
| 20 | |
---|
[b3f6bc3] | 21 | class PyInput(object): |
---|
| 22 | """ |
---|
| 23 | Make q data available to the gpu. |
---|
| 24 | |
---|
| 25 | *q_vectors* is a list of q vectors, which will be *[q]* for 1-D data, |
---|
| 26 | and *[qx, qy]* for 2-D data. Internally, the vectors will be reallocated |
---|
| 27 | to get the best performance on OpenCL, which may involve shifting and |
---|
| 28 | stretching the array to better match the memory architecture. Additional |
---|
| 29 | points will be evaluated with *q=1e-3*. |
---|
| 30 | |
---|
| 31 | *dtype* is the data type for the q vectors. The data type should be |
---|
| 32 | set to match that of the kernel, which is an attribute of |
---|
| 33 | :class:`GpuProgram`. Note that not all kernels support double |
---|
| 34 | precision, so even if the program was created for double precision, |
---|
| 35 | the *GpuProgram.dtype* may be single precision. |
---|
| 36 | |
---|
| 37 | Call :meth:`release` when complete. Even if not called directly, the |
---|
| 38 | buffer will be released when the data object is freed. |
---|
| 39 | """ |
---|
| 40 | def __init__(self, q_vectors, dtype): |
---|
| 41 | self.nq = q_vectors[0].size |
---|
| 42 | self.dtype = dtype |
---|
| 43 | self.is_2D = (len(q_vectors) == 2) |
---|
[c85db69] | 44 | self.q_vectors = [np.ascontiguousarray(q, self.dtype) for q in q_vectors] |
---|
[750ffa5] | 45 | self.q_pointers = [q.ctypes.data for q in self.q_vectors] |
---|
[b3f6bc3] | 46 | |
---|
| 47 | def release(self): |
---|
| 48 | self.q_vectors = [] |
---|
| 49 | |
---|
| 50 | class PyKernel(object): |
---|
| 51 | """ |
---|
| 52 | Callable SAS kernel. |
---|
| 53 | |
---|
| 54 | *kernel* is the DllKernel object to call. |
---|
| 55 | |
---|
| 56 | *info* is the module information |
---|
| 57 | |
---|
[3c56da87] | 58 | *q_input* is the DllInput q vectors at which the kernel should be |
---|
[b3f6bc3] | 59 | evaluated. |
---|
| 60 | |
---|
| 61 | The resulting call method takes the *pars*, a list of values for |
---|
| 62 | the fixed parameters to the kernel, and *pd_pars*, a list of (value,weight) |
---|
| 63 | vectors for the polydisperse parameters. *cutoff* determines the |
---|
| 64 | integration limits: any points with combined weight less than *cutoff* |
---|
| 65 | will not be calculated. |
---|
| 66 | |
---|
| 67 | Call :meth:`release` when done with the kernel instance. |
---|
| 68 | """ |
---|
[3c56da87] | 69 | def __init__(self, kernel, info, q_input): |
---|
[b3f6bc3] | 70 | self.info = info |
---|
[3c56da87] | 71 | self.q_input = q_input |
---|
| 72 | self.res = np.empty(q_input.nq, q_input.dtype) |
---|
| 73 | dim = '2d' if q_input.is_2D else '1d' |
---|
[b3f6bc3] | 74 | # Loop over q unless user promises that the kernel is vectorized by |
---|
| 75 | # taggining it with vectorized=True |
---|
| 76 | if not getattr(kernel, 'vectorized', False): |
---|
| 77 | if dim == '2d': |
---|
| 78 | def vector_kernel(qx, qy, *args): |
---|
[3c56da87] | 79 | return np.array([kernel(qxi, qyi, *args) |
---|
| 80 | for qxi, qyi in zip(qx, qy)]) |
---|
[b3f6bc3] | 81 | else: |
---|
| 82 | def vector_kernel(q, *args): |
---|
[3c56da87] | 83 | return np.array([kernel(qi, *args) |
---|
| 84 | for qi in q]) |
---|
[b3f6bc3] | 85 | self.kernel = vector_kernel |
---|
| 86 | else: |
---|
| 87 | self.kernel = kernel |
---|
[c85db69] | 88 | fixed_pars = info['partype']['fixed-' + dim] |
---|
| 89 | pd_pars = info['partype']['pd-' + dim] |
---|
[b3f6bc3] | 90 | vol_pars = info['partype']['volume'] |
---|
| 91 | |
---|
| 92 | # First two fixed pars are scale and background |
---|
| 93 | pars = [p[0] for p in info['parameters'][2:]] |
---|
[3c56da87] | 94 | offset = len(self.q_input.q_vectors) |
---|
| 95 | self.args = self.q_input.q_vectors + [None] * len(pars) |
---|
| 96 | self.fixed_index = np.array([pars.index(p) + offset |
---|
| 97 | for p in fixed_pars[2:]]) |
---|
| 98 | self.pd_index = np.array([pars.index(p) + offset |
---|
| 99 | for p in pd_pars]) |
---|
| 100 | self.vol_index = np.array([pars.index(p) + offset |
---|
| 101 | for p in vol_pars]) |
---|
[c85db69] | 102 | try: self.theta_index = pars.index('theta') + offset |
---|
[b3f6bc3] | 103 | except ValueError: self.theta_index = -1 |
---|
| 104 | |
---|
| 105 | # Caller needs fixed_pars and pd_pars |
---|
| 106 | self.fixed_pars = fixed_pars |
---|
| 107 | self.pd_pars = pd_pars |
---|
| 108 | |
---|
[6edb74a] | 109 | def __call__(self, fixed, pd, cutoff=1e-5): |
---|
[9404dd3] | 110 | #print("fixed",fixed) |
---|
| 111 | #print("pd", pd) |
---|
[b3f6bc3] | 112 | args = self.args[:] # grab a copy of the args |
---|
| 113 | form, form_volume = self.kernel, self.info['form_volume'] |
---|
| 114 | # First two fixed |
---|
| 115 | scale, background = fixed[:2] |
---|
[c85db69] | 116 | for index, value in zip(self.fixed_index, fixed[2:]): |
---|
[b3f6bc3] | 117 | args[index] = float(value) |
---|
[c85db69] | 118 | res = _loops(form, form_volume, cutoff, scale, background, args, |
---|
[b3f6bc3] | 119 | pd, self.pd_index, self.vol_index, self.theta_index) |
---|
| 120 | |
---|
| 121 | return res |
---|
| 122 | |
---|
| 123 | def release(self): |
---|
[3c56da87] | 124 | self.q_input = None |
---|
[b3f6bc3] | 125 | |
---|
| 126 | def _loops(form, form_volume, cutoff, scale, background, |
---|
| 127 | args, pd, pd_index, vol_index, theta_index): |
---|
| 128 | """ |
---|
| 129 | *form* is the name of the form function, which should be vectorized over |
---|
| 130 | q, but otherwise have an interface like the opencl kernels, with the |
---|
| 131 | q parameters first followed by the individual parameters in the order |
---|
| 132 | given in model.parameters (see :mod:`sasmodels.generate`). |
---|
| 133 | |
---|
| 134 | *form_volume* calculates the volume of the shape. *vol_index* gives |
---|
| 135 | the list of volume parameters |
---|
| 136 | |
---|
| 137 | *cutoff* ignores the corners of the dispersion hypercube |
---|
| 138 | |
---|
| 139 | *scale*, *background* multiplies the resulting form and adds an offset |
---|
| 140 | |
---|
| 141 | *args* is the prepopulated set of arguments to the form function, starting |
---|
| 142 | with the q vectors, and including slots for all the parameters. The |
---|
| 143 | values for the parameters will be substituted with values from the |
---|
| 144 | dispersion functions. |
---|
| 145 | |
---|
| 146 | *pd* is the list of dispersion parameters |
---|
| 147 | |
---|
| 148 | *pd_index* are the indices of the dispersion parameters in *args* |
---|
| 149 | |
---|
| 150 | *vol_index* are the indices of the volume parameters in *args* |
---|
| 151 | |
---|
| 152 | *theta_index* is the index of the theta parameter for the sasview |
---|
| 153 | spherical correction, or -1 if there is no angular dispersion |
---|
| 154 | """ |
---|
| 155 | |
---|
[f734e7d] | 156 | ################################################################ |
---|
| 157 | # # |
---|
| 158 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # |
---|
| 159 | # !! !! # |
---|
| 160 | # !! KEEP THIS CODE CONSISTENT WITH KERNEL_TEMPLATE.C !! # |
---|
| 161 | # !! !! # |
---|
| 162 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # |
---|
| 163 | # # |
---|
| 164 | ################################################################ |
---|
[b3f6bc3] | 165 | |
---|
| 166 | weight = np.empty(len(pd), 'd') |
---|
[6edb74a] | 167 | if weight.size > 0: |
---|
| 168 | # weight vector, to be populated by polydispersity loops |
---|
| 169 | |
---|
| 170 | # identify which pd parameters are volume parameters |
---|
| 171 | vol_weight_index = np.array([(index in vol_index) for index in pd_index]) |
---|
| 172 | |
---|
| 173 | # Sort parameters in decreasing order of pd length |
---|
| 174 | Npd = np.array([len(pdi[0]) for pdi in pd], 'i') |
---|
| 175 | order = np.argsort(Npd)[::-1] |
---|
| 176 | stride = np.cumprod(Npd[order]) |
---|
| 177 | pd = [pd[index] for index in order] |
---|
| 178 | pd_index = pd_index[order] |
---|
| 179 | vol_weight_index = vol_weight_index[order] |
---|
| 180 | |
---|
| 181 | fast_value = pd[0][0] |
---|
| 182 | fast_weight = pd[0][1] |
---|
| 183 | else: |
---|
| 184 | stride = np.array([1]) |
---|
| 185 | vol_weight_index = slice(None, None) |
---|
[f734e7d] | 186 | # keep lint happy |
---|
| 187 | fast_value = [None] |
---|
| 188 | fast_weight = [None] |
---|
[b3f6bc3] | 189 | |
---|
| 190 | ret = np.zeros_like(args[0]) |
---|
| 191 | norm = np.zeros_like(ret) |
---|
| 192 | vol = np.zeros_like(ret) |
---|
| 193 | vol_norm = np.zeros_like(ret) |
---|
| 194 | for k in range(stride[-1]): |
---|
| 195 | # update polydispersity parameter values |
---|
[c85db69] | 196 | fast_index = k % stride[0] |
---|
[b3f6bc3] | 197 | if fast_index == 0: # bottom loop complete ... check all other loops |
---|
[6edb74a] | 198 | if weight.size > 0: |
---|
[c85db69] | 199 | for i, index, in enumerate(k % stride): |
---|
[6edb74a] | 200 | args[pd_index[i]] = pd[i][0][index] |
---|
| 201 | weight[i] = pd[i][1][index] |
---|
[b3f6bc3] | 202 | else: |
---|
| 203 | args[pd_index[0]] = fast_value[fast_index] |
---|
| 204 | weight[0] = fast_weight[fast_index] |
---|
[4c2c535] | 205 | |
---|
| 206 | # Computes the weight, and if it is not sufficient then ignore this |
---|
| 207 | # parameter set. |
---|
| 208 | # Note: could precompute w1*...*wn so we only need to multiply by w0 |
---|
[b3f6bc3] | 209 | w = np.prod(weight) |
---|
| 210 | if w > cutoff: |
---|
[3c56da87] | 211 | # Note: can precompute spherical correction if theta_index is not |
---|
| 212 | # the fast index. Correction factor for spherical integration |
---|
| 213 | #spherical_correction = abs(cos(pi*args[phi_index])) if phi_index>=0 else 1.0 |
---|
| 214 | spherical_correction = (abs(cos(pi * args[theta_index])) * pi / 2 |
---|
| 215 | if theta_index >= 0 else 1.0) |
---|
[f734e7d] | 216 | #spherical_correction = 1.0 |
---|
[4c2c535] | 217 | |
---|
| 218 | # Call the scattering function and adds it to the total. |
---|
| 219 | I = form(*args) |
---|
| 220 | if np.isnan(I).any(): continue |
---|
| 221 | ret += w * I * spherical_correction |
---|
| 222 | norm += w |
---|
[b3f6bc3] | 223 | |
---|
| 224 | # Volume normalization. |
---|
[3c56da87] | 225 | # If there are "volume" polydispersity parameters, then these |
---|
| 226 | # will be used to call the form_volume function from the user |
---|
| 227 | # supplied kernel, and accumulate a normalized weight. |
---|
| 228 | # Note: can precompute volume norm if fast index is not a volume |
---|
[b3f6bc3] | 229 | if form_volume: |
---|
| 230 | vol_args = [args[index] for index in vol_index] |
---|
| 231 | vol_weight = np.prod(weight[vol_weight_index]) |
---|
[4c2c535] | 232 | vol += vol_weight * form_volume(*vol_args) |
---|
| 233 | vol_norm += vol_weight |
---|
[b3f6bc3] | 234 | |
---|
[c85db69] | 235 | positive = (vol * vol_norm != 0.0) |
---|
[b3f6bc3] | 236 | ret[positive] *= vol_norm[positive] / vol[positive] |
---|
[c85db69] | 237 | result = scale * ret / norm + background |
---|
[b3f6bc3] | 238 | return result |
---|