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