[f2f67a6] | 1 | |
---|
| 2 | /* |
---|
| 3 | ########################################################## |
---|
| 4 | # # |
---|
| 5 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # |
---|
| 6 | # !! !! # |
---|
| 7 | # !! KEEP THIS CODE CONSISTENT WITH KERNELPY.PY !! # |
---|
| 8 | # !! !! # |
---|
| 9 | # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! # |
---|
| 10 | # # |
---|
| 11 | ########################################################## |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | #ifndef _PAR_BLOCK_ // protected block so we can include this code twice. |
---|
| 15 | #define _PAR_BLOCK_ |
---|
| 16 | |
---|
| 17 | typedef struct { |
---|
| 18 | #if MAX_PD > 0 |
---|
| 19 | int32_t pd_par[MAX_PD]; // id of the nth polydispersity variable |
---|
| 20 | int32_t pd_length[MAX_PD]; // length of the nth polydispersity weight vector |
---|
| 21 | int32_t pd_offset[MAX_PD]; // offset of pd weights in the value & weight vector |
---|
| 22 | int32_t pd_stride[MAX_PD]; // stride to move to the next index at this level |
---|
| 23 | #endif // MAX_PD > 0 |
---|
[bde38b5] | 24 | int32_t num_eval; // total number of voxels in hypercube |
---|
| 25 | int32_t num_weights; // total length of the weights vector |
---|
[f2f67a6] | 26 | int32_t num_active; // number of non-trivial pd loops |
---|
| 27 | int32_t theta_par; // id of spherical correction variable |
---|
| 28 | } ProblemDetails; |
---|
| 29 | |
---|
[4f1f876] | 30 | // Intel HD 4000 needs private arrays to be a multiple of 4 long |
---|
[f2f67a6] | 31 | typedef struct { |
---|
[56547a8] | 32 | PARAMETER_TABLE |
---|
[4f1f876] | 33 | } ParameterTable; |
---|
| 34 | typedef union { |
---|
| 35 | ParameterTable table; |
---|
| 36 | double vector[4*((NUM_PARS+3)/4)]; |
---|
[f2f67a6] | 37 | } ParameterBlock; |
---|
[9eb3632] | 38 | #endif // _PAR_BLOCK_ |
---|
| 39 | |
---|
| 40 | |
---|
[a4280bd] | 41 | #if defined(MAGNETIC) && NUM_MAGNETIC>0 |
---|
[9eb3632] | 42 | |
---|
| 43 | // Return value restricted between low and high |
---|
| 44 | static double clip(double value, double low, double high) |
---|
| 45 | { |
---|
| 46 | return (value < low ? low : (value > high ? high : value)); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | // Compute spin cross sections given in_spin and out_spin |
---|
| 50 | // To convert spin cross sections to sld b: |
---|
| 51 | // uu * (sld - m_sigma_x); |
---|
| 52 | // dd * (sld + m_sigma_x); |
---|
| 53 | // ud * (m_sigma_y + 1j*m_sigma_z); |
---|
| 54 | // du * (m_sigma_y - 1j*m_sigma_z); |
---|
[a4280bd] | 55 | static void set_spins(double in_spin, double out_spin, double spins[4]) |
---|
[9eb3632] | 56 | { |
---|
| 57 | in_spin = clip(in_spin, 0.0, 1.0); |
---|
| 58 | out_spin = clip(out_spin, 0.0, 1.0); |
---|
[a4280bd] | 59 | spins[0] = sqrt(sqrt((1.0-in_spin) * (1.0-out_spin))); // dd |
---|
| 60 | spins[1] = sqrt(sqrt((1.0-in_spin) * out_spin)); // du |
---|
| 61 | spins[2] = sqrt(sqrt(in_spin * (1.0-out_spin))); // ud |
---|
| 62 | spins[3] = sqrt(sqrt(in_spin * out_spin)); // uu |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | static double mag_sld(double qx, double qy, double p, |
---|
| 66 | double mx, double my, double sld) |
---|
| 67 | { |
---|
| 68 | const double perp = qy*mx - qx*my; |
---|
| 69 | return sld + perp*p; |
---|
[9eb3632] | 70 | } |
---|
[f2f67a6] | 71 | |
---|
[9eb3632] | 72 | #endif // MAGNETIC |
---|
[f2f67a6] | 73 | |
---|
| 74 | kernel |
---|
| 75 | void KERNEL_NAME( |
---|
| 76 | int32_t nq, // number of q values |
---|
| 77 | const int32_t pd_start, // where we are in the polydispersity loop |
---|
| 78 | const int32_t pd_stop, // where we are stopping in the polydispersity loop |
---|
| 79 | global const ProblemDetails *details, |
---|
| 80 | global const double *values, |
---|
| 81 | global const double *q, // nq q values, with padding to boundary |
---|
[a738209] | 82 | global double *result, // nq+1 return values, again with padding |
---|
[f2f67a6] | 83 | const double cutoff // cutoff in the polydispersity weight product |
---|
| 84 | ) |
---|
| 85 | { |
---|
[9eb3632] | 86 | |
---|
| 87 | // who we are and what element we are working with |
---|
| 88 | const int q_index = get_global_id(0); |
---|
| 89 | if (q_index >= nq) return; |
---|
| 90 | |
---|
[ae2b6b5] | 91 | // Storage for the current parameter values. These will be updated as we |
---|
[bde38b5] | 92 | // walk the polydispersity cube. |
---|
[b9c12fe5] | 93 | ParameterBlock local_values; |
---|
[f2f67a6] | 94 | |
---|
[a4280bd] | 95 | #if defined(MAGNETIC) && NUM_MAGNETIC>0 |
---|
[4f1f876] | 96 | // Location of the sld parameters in the parameter vector. |
---|
[9eb3632] | 97 | // These parameters are updated with the effective sld due to magnetism. |
---|
[a4280bd] | 98 | #if NUM_MAGNETIC > 3 |
---|
[9eb3632] | 99 | const int32_t slds[] = { MAGNETIC_PARS }; |
---|
[a4280bd] | 100 | #endif |
---|
[f2f67a6] | 101 | |
---|
[9eb3632] | 102 | // TODO: could precompute these outside of the kernel. |
---|
| 103 | // Interpret polarization cross section. |
---|
[a4280bd] | 104 | // up_frac_i = values[NUM_PARS+2]; |
---|
| 105 | // up_frac_f = values[NUM_PARS+3]; |
---|
| 106 | // up_angle = values[NUM_PARS+4]; |
---|
| 107 | double spins[4]; |
---|
[9eb3632] | 108 | double cos_mspin, sin_mspin; |
---|
[a4280bd] | 109 | set_spins(values[NUM_PARS+2], values[NUM_PARS+3], spins); |
---|
| 110 | SINCOS(-values[NUM_PARS+4]*M_PI_180, sin_mspin, cos_mspin); |
---|
[9eb3632] | 111 | #endif // MAGNETIC |
---|
| 112 | |
---|
[bde38b5] | 113 | // Fill in the initial variables |
---|
| 114 | // values[0] is scale |
---|
| 115 | // values[1] is background |
---|
| 116 | for (int i=0; i < NUM_PARS; i++) { |
---|
| 117 | local_values.vector[i] = values[2+i]; |
---|
| 118 | //if (q_index==0) printf("p%d = %g\n",i, local_values.vector[i]); |
---|
[f2f67a6] | 119 | } |
---|
[bde38b5] | 120 | //if (q_index==0) printf("NUM_VALUES:%d NUM_PARS:%d MAX_PD:%d\n", NUM_VALUES, NUM_PARS, MAX_PD); |
---|
| 121 | //if (q_index==0) printf("start:%d stop:%d\n", pd_start, pd_stop); |
---|
| 122 | |
---|
| 123 | double pd_norm = (pd_start == 0 ? 0.0 : result[nq]); |
---|
| 124 | double this_result = (pd_start == 0 ? 0.0 : result[q_index]); |
---|
[9eb3632] | 125 | //if (q_index==0) printf("start %d %g %g\n", pd_start, pd_norm, this_result); |
---|
[f2f67a6] | 126 | |
---|
[7b7da6b] | 127 | #if MAX_PD>0 |
---|
[bde38b5] | 128 | global const double *pd_value = values + NUM_VALUES; |
---|
| 129 | global const double *pd_weight = pd_value + details->num_weights; |
---|
[7b7da6b] | 130 | #endif |
---|
[f2f67a6] | 131 | |
---|
[9eb3632] | 132 | // Jump into the middle of the polydispersity loop |
---|
| 133 | #if MAX_PD>4 |
---|
| 134 | int n4=details->pd_length[4]; |
---|
| 135 | int i4=(pd_start/details->pd_stride[4])%n4; |
---|
| 136 | const int p4=details->pd_par[4]; |
---|
| 137 | global const double *v4 = pd_value + details->pd_offset[4]; |
---|
| 138 | global const double *w4 = pd_weight + details->pd_offset[4]; |
---|
| 139 | #endif |
---|
| 140 | #if MAX_PD>3 |
---|
| 141 | int n3=details->pd_length[3]; |
---|
| 142 | int i3=(pd_start/details->pd_stride[3])%n3; |
---|
| 143 | const int p3=details->pd_par[3]; |
---|
| 144 | global const double *v3 = pd_value + details->pd_offset[3]; |
---|
| 145 | global const double *w3 = pd_weight + details->pd_offset[3]; |
---|
| 146 | //if (q_index==0) printf("offset %d: %d %d\n", 3, details->pd_offset[3], NUM_VALUES); |
---|
| 147 | #endif |
---|
| 148 | #if MAX_PD>2 |
---|
| 149 | int n2=details->pd_length[2]; |
---|
| 150 | int i2=(pd_start/details->pd_stride[2])%n2; |
---|
| 151 | const int p2=details->pd_par[2]; |
---|
| 152 | global const double *v2 = pd_value + details->pd_offset[2]; |
---|
| 153 | global const double *w2 = pd_weight + details->pd_offset[2]; |
---|
| 154 | #endif |
---|
| 155 | #if MAX_PD>1 |
---|
| 156 | int n1=details->pd_length[1]; |
---|
| 157 | int i1=(pd_start/details->pd_stride[1])%n1; |
---|
| 158 | const int p1=details->pd_par[1]; |
---|
| 159 | global const double *v1 = pd_value + details->pd_offset[1]; |
---|
| 160 | global const double *w1 = pd_weight + details->pd_offset[1]; |
---|
| 161 | #endif |
---|
| 162 | #if MAX_PD>0 |
---|
| 163 | int n0=details->pd_length[0]; |
---|
| 164 | int i0=(pd_start/details->pd_stride[0])%n0; |
---|
| 165 | const int p0=details->pd_par[0]; |
---|
| 166 | global const double *v0 = pd_value + details->pd_offset[0]; |
---|
| 167 | global const double *w0 = pd_weight + details->pd_offset[0]; |
---|
| 168 | #endif |
---|
[f2f67a6] | 169 | |
---|
| 170 | |
---|
[9eb3632] | 171 | #if MAX_PD>0 |
---|
[0f00d95] | 172 | const int theta_par = details->theta_par; |
---|
| 173 | const bool fast_theta = (theta_par == p0); |
---|
| 174 | const bool slow_theta = (theta_par >= 0 && !fast_theta); |
---|
| 175 | double spherical_correction = 1.0; |
---|
[9eb3632] | 176 | #else |
---|
[0f00d95] | 177 | // Note: if not polydisperse the weights cancel and we don't need the |
---|
| 178 | // spherical correction. |
---|
| 179 | const double spherical_correction = 1.0; |
---|
[9eb3632] | 180 | #endif |
---|
| 181 | |
---|
| 182 | int step = pd_start; |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | #if MAX_PD>4 |
---|
| 186 | const double weight5 = 1.0; |
---|
| 187 | while (i4 < n4) { |
---|
[4f1f876] | 188 | local_values.vector[p4] = v4[i4]; |
---|
[9eb3632] | 189 | double weight4 = w4[i4] * weight5; |
---|
[4f1f876] | 190 | //if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 4, p4, i4, n4, local_values.vector[p4], weight4); |
---|
[9eb3632] | 191 | #elif MAX_PD>3 |
---|
| 192 | const double weight4 = 1.0; |
---|
| 193 | #endif |
---|
| 194 | #if MAX_PD>3 |
---|
| 195 | while (i3 < n3) { |
---|
[4f1f876] | 196 | local_values.vector[p3] = v3[i3]; |
---|
[9eb3632] | 197 | double weight3 = w3[i3] * weight4; |
---|
[4f1f876] | 198 | //if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 3, p3, i3, n3, local_values.vector[p3], weight3); |
---|
[9eb3632] | 199 | #elif MAX_PD>2 |
---|
| 200 | const double weight3 = 1.0; |
---|
| 201 | #endif |
---|
| 202 | #if MAX_PD>2 |
---|
| 203 | while (i2 < n2) { |
---|
[4f1f876] | 204 | local_values.vector[p2] = v2[i2]; |
---|
[9eb3632] | 205 | double weight2 = w2[i2] * weight3; |
---|
[4f1f876] | 206 | //if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 2, p2, i2, n2, local_values.vector[p2], weight2); |
---|
[9eb3632] | 207 | #elif MAX_PD>1 |
---|
| 208 | const double weight2 = 1.0; |
---|
| 209 | #endif |
---|
| 210 | #if MAX_PD>1 |
---|
| 211 | while (i1 < n1) { |
---|
[4f1f876] | 212 | local_values.vector[p1] = v1[i1]; |
---|
[9eb3632] | 213 | double weight1 = w1[i1] * weight2; |
---|
[4f1f876] | 214 | //if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 1, p1, i1, n1, local_values.vector[p1], weight1); |
---|
[9eb3632] | 215 | #elif MAX_PD>0 |
---|
| 216 | const double weight1 = 1.0; |
---|
| 217 | #endif |
---|
| 218 | #if MAX_PD>0 |
---|
[0f00d95] | 219 | if (slow_theta) { // Theta is not in inner loop |
---|
[4f1f876] | 220 | spherical_correction = fmax(fabs(cos(M_PI_180*local_values.vector[theta_par])), 1.e-6); |
---|
[0f00d95] | 221 | } |
---|
[9eb3632] | 222 | while(i0 < n0) { |
---|
[4f1f876] | 223 | local_values.vector[p0] = v0[i0]; |
---|
[9eb3632] | 224 | double weight0 = w0[i0] * weight1; |
---|
[4f1f876] | 225 | //if (q_index == 0) printf("step:%d level %d: p:%d i:%d n:%d value:%g weight:%g\n", step, 0, p0, i0, n0, local_values.vector[p0], weight0); |
---|
[9eb3632] | 226 | if (fast_theta) { // Theta is in inner loop |
---|
[4f1f876] | 227 | spherical_correction = fmax(fabs(cos(M_PI_180*local_values.vector[p0])), 1.e-6); |
---|
[f2f67a6] | 228 | } |
---|
[9eb3632] | 229 | #else |
---|
| 230 | const double weight0 = 1.0; |
---|
| 231 | #endif |
---|
[f2f67a6] | 232 | |
---|
[4f1f876] | 233 | //if (q_index == 0) {printf("step:%d of %d, pars:",step,pd_stop); for (int i=0; i < NUM_PARS; i++) printf("p%d=%g ",i, local_values.vector[i]); printf("\n"); } |
---|
[9eb3632] | 234 | //if (q_index == 0) printf("sphcor: %g\n", spherical_correction); |
---|
[ae2b6b5] | 235 | |
---|
[f2f67a6] | 236 | #ifdef INVALID |
---|
[4f1f876] | 237 | if (!INVALID(local_values.table)) |
---|
[f2f67a6] | 238 | #endif |
---|
[9eb3632] | 239 | { |
---|
| 240 | // Accumulate I(q) |
---|
| 241 | // Note: weight==0 must always be excluded |
---|
| 242 | if (weight0 > cutoff) { |
---|
[a3a0c5c] | 243 | // spherical correction is set at a minimum of 1e-6, otherwise there |
---|
| 244 | // would be problems looking at models with theta=90. |
---|
[9eb3632] | 245 | const double weight = weight0 * spherical_correction; |
---|
[4f1f876] | 246 | pd_norm += weight * CALL_VOLUME(local_values.table); |
---|
[f2f67a6] | 247 | |
---|
[a4280bd] | 248 | #if defined(MAGNETIC) && NUM_MAGNETIC > 0 |
---|
| 249 | const double qx = q[2*q_index]; |
---|
| 250 | const double qy = q[2*q_index+1]; |
---|
| 251 | const double qsq = qx*qx + qy*qy; |
---|
[f2f67a6] | 252 | |
---|
[a4280bd] | 253 | // Constant across orientation, polydispersity for given qx, qy |
---|
| 254 | double scattering = 0.0; |
---|
| 255 | // TODO: what is the magnetic scattering at q=0 |
---|
| 256 | if (qsq > 1.e-16) { |
---|
[bde38b5] | 257 | double p[4]; // dd, du, ud, uu |
---|
[a4280bd] | 258 | p[0] = (qy*cos_mspin + qx*sin_mspin)/qsq; |
---|
| 259 | p[3] = -p[0]; |
---|
| 260 | p[1] = p[2] = (qy*sin_mspin - qx*cos_mspin)/qsq; |
---|
[ae2b6b5] | 261 | |
---|
[a4280bd] | 262 | for (int index=0; index<4; index++) { |
---|
| 263 | const double xs = spins[index]; |
---|
| 264 | if (xs > 1.e-8) { |
---|
| 265 | const int spin_flip = (index==1) || (index==2); |
---|
| 266 | const double pk = p[index]; |
---|
| 267 | for (int axis=0; axis<=spin_flip; axis++) { |
---|
| 268 | #define M1 NUM_PARS+5 |
---|
| 269 | #define M2 NUM_PARS+8 |
---|
| 270 | #define M3 NUM_PARS+13 |
---|
| 271 | #define SLD(_M_offset, _sld_offset) \ |
---|
[4f1f876] | 272 | local_values.vector[_sld_offset] = xs * (axis \ |
---|
[a4280bd] | 273 | ? (index==1 ? -values[_M_offset+2] : values[_M_offset+2]) \ |
---|
| 274 | : mag_sld(qx, qy, pk, values[_M_offset], values[_M_offset+1], \ |
---|
| 275 | (spin_flip ? 0.0 : values[_sld_offset+2]))) |
---|
| 276 | #if NUM_MAGNETIC==1 |
---|
| 277 | SLD(M1, MAGNETIC_PAR1); |
---|
| 278 | #elif NUM_MAGNETIC==2 |
---|
| 279 | SLD(M1, MAGNETIC_PAR1); |
---|
| 280 | SLD(M2, MAGNETIC_PAR2); |
---|
| 281 | #elif NUM_MAGNETIC==3 |
---|
| 282 | SLD(M1, MAGNETIC_PAR1); |
---|
| 283 | SLD(M2, MAGNETIC_PAR2); |
---|
| 284 | SLD(M3, MAGNETIC_PAR3); |
---|
| 285 | #else |
---|
| 286 | for (int sk=0; sk<NUM_MAGNETIC; sk++) { |
---|
| 287 | SLD(M1+3*sk, slds[sk]); |
---|
| 288 | } |
---|
| 289 | #endif |
---|
[4f1f876] | 290 | scattering += CALL_IQ(q, q_index, local_values.table); |
---|
[a4280bd] | 291 | } |
---|
[9eb3632] | 292 | } |
---|
| 293 | } |
---|
[a4280bd] | 294 | } |
---|
[9eb3632] | 295 | #else // !MAGNETIC |
---|
[4f1f876] | 296 | const double scattering = CALL_IQ(q, q_index, local_values.table); |
---|
[9eb3632] | 297 | #endif // !MAGNETIC |
---|
| 298 | this_result += weight * scattering; |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | ++step; |
---|
| 302 | #if MAX_PD>0 |
---|
| 303 | if (step >= pd_stop) break; |
---|
| 304 | ++i0; |
---|
| 305 | } |
---|
| 306 | i0 = 0; |
---|
| 307 | #endif |
---|
| 308 | #if MAX_PD>1 |
---|
| 309 | if (step >= pd_stop) break; |
---|
| 310 | ++i1; |
---|
| 311 | } |
---|
| 312 | i1 = 0; |
---|
| 313 | #endif |
---|
| 314 | #if MAX_PD>2 |
---|
| 315 | if (step >= pd_stop) break; |
---|
| 316 | ++i2; |
---|
[f2f67a6] | 317 | } |
---|
[9eb3632] | 318 | i2 = 0; |
---|
| 319 | #endif |
---|
| 320 | #if MAX_PD>3 |
---|
| 321 | if (step >= pd_stop) break; |
---|
| 322 | ++i3; |
---|
| 323 | } |
---|
| 324 | i3 = 0; |
---|
| 325 | #endif |
---|
| 326 | #if MAX_PD>4 |
---|
| 327 | if (step >= pd_stop) break; |
---|
| 328 | ++i4; |
---|
| 329 | } |
---|
| 330 | i4 = 0; |
---|
| 331 | #endif |
---|
[f2f67a6] | 332 | |
---|
[9eb3632] | 333 | //if (q_index==0) printf("res: %g/%g\n", this_result, pd_norm); |
---|
| 334 | // Remember the current result and the updated norm. |
---|
| 335 | result[q_index] = this_result; |
---|
| 336 | if (q_index == 0) result[nq] = pd_norm; |
---|
[f2f67a6] | 337 | } |
---|