source: sasmodels/sasmodels/kernel_iq.c @ 01c8d9e

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 01c8d9e was 01c8d9e, checked in by Suczewski <ges3@…>, 6 years ago

beta approximation, first pass

  • Property mode set to 100644
File size: 27.5 KB
RevLine 
[2e44ac7]1/*
2    ##########################################################
3    #                                                        #
4    #   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   #
5    #   !!                                              !!   #
6    #   !!  KEEP THIS CODE CONSISTENT WITH KERNELPY.PY  !!   #
7    #   !!                                              !!   #
8    #   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   #
9    #                                                        #
10    ##########################################################
11*/
12
[9ee2756]13// NOTE: the following macros are defined in generate.py:
14//
[6aee3ab]15//  MAX_PD : the maximum number of dispersity loops allowed for this model,
16//      which will be at most modelinfo.MAX_PD.
[9ee2756]17//  NUM_PARS : the number of parameters in the parameter table
18//  NUM_VALUES : the number of values to skip at the start of the
19//      values array before you get to the dispersity values.
20//  PARAMETER_TABLE : list of parameter declarations used to create the
21//      ParameterTable type.
22//  KERNEL_NAME : model_Iq, model_Iqxy or model_Imagnetic.  This code is
23//      included three times, once for each kernel type.
[2c108a3]24//  MAGNETIC : defined when the magnetic kernel is being instantiated
25//  NUM_MAGNETIC : the number of magnetic parameters
[9ee2756]26//  MAGNETIC_PARS : a comma-separated list of indices to the sld
27//      parameters in the parameter table.
28//  CALL_VOLUME(table) : call the form volume function
29//  CALL_IQ(q, table) : call the Iq function for 1D calcs.
30//  CALL_IQ_A(q, table) : call the Iq function with |q| for 2D data.
31//  CALL_IQ_AC(qa, qc, table) : call the Iqxy function for symmetric shapes
32//  CALL_IQ_ABC(qa, qc, table) : call the Iqxy function for asymmetric shapes
[108e70e]33//  CALL_IQ_XY(qx, qy, table) : call the Iqxy function for arbitrary models
[9ee2756]34//  INVALID(table) : test if the current point is feesible to calculate.  This
35//      will be defined in the kernel definition file.
[ff10479]36//  PROJECTION : equirectangular=1, sinusoidal=2
37//      see explore/jitter.py for definitions.
[767dca8]38
[01c8d9e]39
[03cac08]40#ifndef _PAR_BLOCK_ // protected block so we can include this code twice.
41#define _PAR_BLOCK_
[2e44ac7]42
43typedef struct {
[60eab2a]44#if MAX_PD > 0
[2c108a3]45    int32_t pd_par[MAX_PD];     // id of the nth dispersity variable
46    int32_t pd_length[MAX_PD];  // length of the nth dispersity weight vector
[0a7e5eb4]47    int32_t pd_offset[MAX_PD];  // offset of pd weights in the value & weight vector
[5cf3c33]48    int32_t pd_stride[MAX_PD];  // stride to move to the next index at this level
[60eab2a]49#endif // MAX_PD > 0
[bde38b5]50    int32_t num_eval;           // total number of voxels in hypercube
51    int32_t num_weights;        // total length of the weights vector
[5ff1b03]52    int32_t num_active;         // number of non-trivial pd loops
[8698a0d]53    int32_t theta_par;          // id of first orientation variable
[2e44ac7]54} ProblemDetails;
55
[bde38b5]56// Intel HD 4000 needs private arrays to be a multiple of 4 long
[2e44ac7]57typedef struct {
[56547a8]58    PARAMETER_TABLE
[bde38b5]59} ParameterTable;
60typedef union {
61    ParameterTable table;
62    double vector[4*((NUM_PARS+3)/4)];
[2e44ac7]63} ParameterBlock;
[9eb3632]64#endif // _PAR_BLOCK_
[03cac08]65
[9ee2756]66#if defined(MAGNETIC) && NUM_MAGNETIC > 0
67// ===== Helper functions for magnetism =====
[a4280bd]68
[32e3c9b]69// Return value restricted between low and high
70static double clip(double value, double low, double high)
71{
[b966a96]72  return (value < low ? low : (value > high ? high : value));
[32e3c9b]73}
74
75// Compute spin cross sections given in_spin and out_spin
76// To convert spin cross sections to sld b:
77//     uu * (sld - m_sigma_x);
78//     dd * (sld + m_sigma_x);
[2c108a3]79//     ud * (m_sigma_y - 1j*m_sigma_z);
80//     du * (m_sigma_y + 1j*m_sigma_z);
81// weights for spin crosssections: dd du real, ud real, uu, du imag, ud imag
[885753a]82static void set_spin_weights(double in_spin, double out_spin, double weight[6])
[32e3c9b]83{
[b966a96]84  in_spin = clip(in_spin, 0.0, 1.0);
85  out_spin = clip(out_spin, 0.0, 1.0);
[7c35fda]86  // Previous version of this function took the square root of the weights,
87  // under the assumption that
88  //
[885753a]89  //     w*I(q, rho1, rho2, ...) = I(q, sqrt(w)*rho1, sqrt(w)*rho2, ...)
[7c35fda]90  //
91  // However, since the weights are applied to the final intensity and
92  // are not interned inside the I(q) function, we want the full
93  // weight and not the square root.  Any function using
94  // set_spin_weights as part of calculating an amplitude will need to
95  // manually take that square root, but there is currently no such
96  // function.
[84e89cd]97  weight[0] = (1.0-in_spin) * (1.0-out_spin); // dd
98  weight[1] = (1.0-in_spin) * out_spin;       // du
99  weight[2] = in_spin * (1.0-out_spin);       // ud
100  weight[3] = in_spin * out_spin;             // uu
[885753a]101  weight[4] = weight[1]; // du.imag
102  weight[5] = weight[2]; // ud.imag
[a4280bd]103}
104
[9ee2756]105// Compute the magnetic sld
[2c108a3]106static double mag_sld(
[885753a]107  const unsigned int xs, // 0=dd, 1=du.real, 2=ud.real, 3=uu, 4=du.imag, 5=ud.imag
[2c108a3]108  const double qx, const double qy,
109  const double px, const double py,
110  const double sld,
111  const double mx, const double my, const double mz
112)
[a4280bd]113{
[2c108a3]114  if (xs < 4) {
[a4280bd]115    const double perp = qy*mx - qx*my;
[2c108a3]116    switch (xs) {
[aadec17]117      default: // keep compiler happy; condition ensures xs in [0,1,2,3]
[2c108a3]118      case 0: // uu => sld - D M_perpx
119          return sld - px*perp;
[885753a]120      case 1: // ud.real => -D M_perpy
[2c108a3]121          return py*perp;
[885753a]122      case 2: // du.real => -D M_perpy
[2c108a3]123          return py*perp;
[885753a]124      case 3: // dd => sld + D M_perpx
[2c108a3]125          return sld + px*perp;
126    }
127  } else {
128    if (xs== 4) {
[885753a]129      return -mz;  // du.imag => +D M_perpz
[2c108a3]130    } else { // index == 5
[885753a]131      return +mz;  // ud.imag => -D M_perpz
[2c108a3]132    }
133  }
[32e3c9b]134}
[9ee2756]135
[2c108a3]136
[9ee2756]137#endif
138
139// ===== Helper functions for orientation and jitter =====
140
[8698a0d]141// To change the definition of the angles, run explore/angles.py, which
142// uses sympy to generate the equations.
143
[9ee2756]144#if !defined(_QAC_SECTION) && defined(CALL_IQ_AC)
145#define _QAC_SECTION
146
147typedef struct {
148    double R31, R32;
149} QACRotation;
150
151// Fill in the rotation matrix R from the view angles (theta, phi) and the
152// jitter angles (dtheta, dphi).  This matrix can be applied to all of the
153// (qx, qy) points in the image to produce R*[qx,qy]' = [qa,qc]'
154static void
155qac_rotation(
156    QACRotation *rotation,
157    double theta, double phi,
158    double dtheta, double dphi)
[8698a0d]159{
160    double sin_theta, cos_theta;
161    double sin_phi, cos_phi;
162
[9ee2756]163    // reverse view matrix
[8698a0d]164    SINCOS(theta*M_PI_180, sin_theta, cos_theta);
165    SINCOS(phi*M_PI_180, sin_phi, cos_phi);
[9ee2756]166    const double V11 = cos_phi*cos_theta;
167    const double V12 = sin_phi*cos_theta;
168    const double V21 = -sin_phi;
169    const double V22 = cos_phi;
170    const double V31 = sin_theta*cos_phi;
171    const double V32 = sin_phi*sin_theta;
[8698a0d]172
[9ee2756]173    // reverse jitter matrix
174    SINCOS(dtheta*M_PI_180, sin_theta, cos_theta);
175    SINCOS(dphi*M_PI_180, sin_phi, cos_phi);
176    const double J31 = sin_theta;
177    const double J32 = -sin_phi*cos_theta;
178    const double J33 = cos_phi*cos_theta;
[8698a0d]179
[9ee2756]180    // reverse matrix
181    rotation->R31 = J31*V11 + J32*V21 + J33*V31;
182    rotation->R32 = J31*V12 + J32*V22 + J33*V32;
[8698a0d]183}
184
[9ee2756]185// Apply the rotation matrix returned from qac_rotation to the point (qx,qy),
186// returning R*[qx,qy]' = [qa,qc]'
[d86f0fc]187static void
[9ee2756]188qac_apply(
[ec8d4ac]189    QACRotation *rotation,
[9ee2756]190    double qx, double qy,
191    double *qa_out, double *qc_out)
[8698a0d]192{
[ec8d4ac]193    const double dqc = rotation->R31*qx + rotation->R32*qy;
[9ee2756]194    // Indirect calculation of qab, from qab^2 = |q|^2 - qc^2
195    const double dqa = sqrt(-dqc*dqc + qx*qx + qy*qy);
[8698a0d]196
[9ee2756]197    *qa_out = dqa;
198    *qc_out = dqc;
[8698a0d]199}
[9ee2756]200#endif // _QAC_SECTION
201
202#if !defined(_QABC_SECTION) && defined(CALL_IQ_ABC)
203#define _QABC_SECTION
204
205typedef struct {
206    double R11, R12;
207    double R21, R22;
208    double R31, R32;
209} QABCRotation;
210
211// Fill in the rotation matrix R from the view angles (theta, phi, psi) and the
212// jitter angles (dtheta, dphi, dpsi).  This matrix can be applied to all of the
213// (qx, qy) points in the image to produce R*[qx,qy]' = [qa,qb,qc]'
[8698a0d]214static void
[9ee2756]215qabc_rotation(
216    QABCRotation *rotation,
[8698a0d]217    double theta, double phi, double psi,
[9ee2756]218    double dtheta, double dphi, double dpsi)
[8698a0d]219{
220    double sin_theta, cos_theta;
221    double sin_phi, cos_phi;
222    double sin_psi, cos_psi;
223
224    // reverse view matrix
225    SINCOS(theta*M_PI_180, sin_theta, cos_theta);
226    SINCOS(phi*M_PI_180, sin_phi, cos_phi);
227    SINCOS(psi*M_PI_180, sin_psi, cos_psi);
[9ee2756]228    const double V11 = -sin_phi*sin_psi + cos_phi*cos_psi*cos_theta;
229    const double V12 = sin_phi*cos_psi*cos_theta + sin_psi*cos_phi;
230    const double V21 = -sin_phi*cos_psi - sin_psi*cos_phi*cos_theta;
231    const double V22 = -sin_phi*sin_psi*cos_theta + cos_phi*cos_psi;
[8698a0d]232    const double V31 = sin_theta*cos_phi;
233    const double V32 = sin_phi*sin_theta;
234
235    // reverse jitter matrix
[9ee2756]236    SINCOS(dtheta*M_PI_180, sin_theta, cos_theta);
237    SINCOS(dphi*M_PI_180, sin_phi, cos_phi);
238    SINCOS(dpsi*M_PI_180, sin_psi, cos_psi);
[8698a0d]239    const double J11 = cos_psi*cos_theta;
[9ee2756]240    const double J12 = sin_phi*sin_theta*cos_psi + sin_psi*cos_phi;
241    const double J13 = sin_phi*sin_psi - sin_theta*cos_phi*cos_psi;
242    const double J21 = -sin_psi*cos_theta;
243    const double J22 = -sin_phi*sin_psi*sin_theta + cos_phi*cos_psi;
244    const double J23 = sin_phi*cos_psi + sin_psi*sin_theta*cos_phi;
[8698a0d]245    const double J31 = sin_theta;
246    const double J32 = -sin_phi*cos_theta;
247    const double J33 = cos_phi*cos_theta;
248
249    // reverse matrix
[9ee2756]250    rotation->R11 = J11*V11 + J12*V21 + J13*V31;
251    rotation->R12 = J11*V12 + J12*V22 + J13*V32;
252    rotation->R21 = J21*V11 + J22*V21 + J23*V31;
253    rotation->R22 = J21*V12 + J22*V22 + J23*V32;
254    rotation->R31 = J31*V11 + J32*V21 + J33*V31;
255    rotation->R32 = J31*V12 + J32*V22 + J33*V32;
[8698a0d]256}
257
[9ee2756]258// Apply the rotation matrix returned from qabc_rotation to the point (qx,qy),
259// returning R*[qx,qy]' = [qa,qb,qc]'
[d86f0fc]260static void
[9ee2756]261qabc_apply(
[ec8d4ac]262    QABCRotation *rotation,
[9ee2756]263    double qx, double qy,
264    double *qa_out, double *qb_out, double *qc_out)
[8698a0d]265{
[ec8d4ac]266    *qa_out = rotation->R11*qx + rotation->R12*qy;
267    *qb_out = rotation->R21*qx + rotation->R22*qy;
268    *qc_out = rotation->R31*qx + rotation->R32*qy;
[8698a0d]269}
270
[9ee2756]271#endif // _QABC_SECTION
272
273// ==================== KERNEL CODE ========================
[03cac08]274kernel
275void KERNEL_NAME(
[5cf3c33]276    int32_t nq,                 // number of q values
[2c108a3]277    const int32_t pd_start,     // where we are in the dispersity loop
278    const int32_t pd_stop,      // where we are stopping in the dispersity loop
[6e7ff6d]279    global const ProblemDetails *details,
[9eb3632]280    global const double *values,
[2e44ac7]281    global const double *q, // nq q values, with padding to boundary
[9eb3632]282    global double *result,  // nq+1 return values, again with padding
[2c108a3]283    const double cutoff     // cutoff in the dispersity weight product
[2e44ac7]284    )
285{
[9ee2756]286#ifdef USE_OPENCL
287  // who we are and what element we are working with
288  const int q_index = get_global_id(0);
289  if (q_index >= nq) return;
290#else
291  // Define q_index here so that debugging statements can be written to work
292  // for both OpenCL and DLL using:
293  //    if (q_index == 0) {printf(...);}
294  int q_index = 0;
295#endif
296
[6aee3ab]297  // ** Fill in the local values table **
298  // Storage for the current parameter values.
299  // These will be updated as we walk the dispersity mesh.
[9eb3632]300  ParameterBlock local_values;
[6aee3ab]301  //   values[0] is scale
302  //   values[1] is background
303  #ifdef USE_OPENMP
304  #pragma omp parallel for
305  #endif
306  for (int i=0; i < NUM_PARS; i++) {
307    local_values.vector[i] = values[2+i];
308    //if (q_index==0) printf("p%d = %g\n",i, local_values.vector[i]);
309  }
310  //if (q_index==0) printf("NUM_VALUES:%d  NUM_PARS:%d  MAX_PD:%d\n", NUM_VALUES, NUM_PARS, MAX_PD);
311  //if (q_index==0) printf("start:%d  stop:%d\n", pd_start, pd_stop);
[2e44ac7]312
[6aee3ab]313  // ** Precompute magnatism values **
[a4280bd]314#if defined(MAGNETIC) && NUM_MAGNETIC>0
[bde38b5]315  // Location of the sld parameters in the parameter vector.
[9eb3632]316  // These parameters are updated with the effective sld due to magnetism.
317  const int32_t slds[] = { MAGNETIC_PARS };
[32e3c9b]318
319  // Interpret polarization cross section.
[a4280bd]320  //     up_frac_i = values[NUM_PARS+2];
321  //     up_frac_f = values[NUM_PARS+3];
322  //     up_angle = values[NUM_PARS+4];
[9ee2756]323  // TODO: could precompute more magnetism parameters before calling the kernel.
[885753a]324  double xs_weights[8];  // uu, ud real, du real, dd, ud imag, du imag, fill, fill
[32e3c9b]325  double cos_mspin, sin_mspin;
[885753a]326  set_spin_weights(values[NUM_PARS+2], values[NUM_PARS+3], xs_weights);
[a4280bd]327  SINCOS(-values[NUM_PARS+4]*M_PI_180, sin_mspin, cos_mspin);
[9eb3632]328#endif // MAGNETIC
[3044216]329
[6aee3ab]330  // ** Fill in the initial results **
[9ee2756]331  // If pd_start is zero that means that we are starting a new calculation,
332  // and must initialize the result to zero.  Otherwise, we are restarting
333  // the calculation from somewhere in the middle of the dispersity mesh,
334  // and we update the value rather than reset it. Similarly for the
335  // normalization factor, which is stored as the final value in the
336  // results vector (one past the number of q values).
337  //
338  // The code differs slightly between opencl and dll since opencl is only
339  // seeing one q value (stored in the variable "this_result") while the dll
[01c8d9e]340  // version must loop over all q
341  #if BETA
342  double *beta_result = &(result[nq+1]); // = result + nq + 1
343  double weight_norm = (pd_start == 0 ? 0.0 : result[2*nq+1]);
344  #endif
[9ee2756]345  #ifdef USE_OPENCL
346    double pd_norm = (pd_start == 0 ? 0.0 : result[nq]);
347    double this_result = (pd_start == 0 ? 0.0 : result[q_index]);
[01c8d9e]348    #if BETA
349      double this_beta_result = (pd_start == 0 ? 0.0 : result[nq + q_index];
[9ee2756]350  #else // !USE_OPENCL
[01c8d9e]351    double pd_norm = (pd_start == 0 ? 0.0 : result[nq]); 
[9ee2756]352    if (pd_start == 0) {
353      #ifdef USE_OPENMP
354      #pragma omp parallel for
355      #endif
356      for (int q_index=0; q_index < nq; q_index++) result[q_index] = 0.0;
[01c8d9e]357      #if BETA
358      for (int q_index=0; q_index < nq; q_index++) beta_result[q_index] = 0.0;
359      #endif
[9ee2756]360    }
[6aee3ab]361    //if (q_index==0) printf("start %d %g %g\n", pd_start, pd_norm, result[0]);
[9ee2756]362#endif // !USE_OPENCL
363
364
[01c8d9e]365
366
367
[9ee2756]368// ====== macros to set up the parts of the loop =======
369/*
370Based on the level of the loop, uses C preprocessor magic to construct
371level-specific looping variables, including these from loop level 3:
372
373  int n3 : length of loop for mesh level 3
374  int i3 : current position in the loop for level 3, which is calculated
375       from a combination of pd_start, pd_stride[3] and pd_length[3].
376  int p3 : is the index into the parameter table for mesh level 3
377  double v3[] : pointer into dispersity array to values for loop 3
378  double w3[] : pointer into dispersity array to weights for loop 3
379  double weight3 : the product of weights from levels 3 and up, computed
380       as weight5*weight4*w3[i3].  Note that we need an outermost
381       value weight5 set to 1.0 for this to work properly.
382
383After expansion, the loop struction will look like the following:
384
385  // --- PD_INIT(4) ---
386  const int n4 = pd_length[4];
387  const int p4 = pd_par[4];
388  global const double *v4 = pd_value + pd_offset[4];
389  global const double *w4 = pd_weight + pd_offset[4];
390  int i4 = (pd_start/pd_stride[4])%n4;  // position in level 4 at pd_start
391
392  // --- PD_INIT(3) ---
393  const int n3 = pd_length[3];
394  ...
395  int i3 = (pd_start/pd_stride[3])%n3;  // position in level 3 at pd_start
396
397  PD_INIT(2)
398  PD_INIT(1)
399  PD_INIT(0)
400
401  // --- PD_OUTERMOST_WEIGHT(5) ---
402  const double weight5 = 1.0;
403
404  // --- PD_OPEN(4,5) ---
405  while (i4 < n4) {
406    parameter[p4] = v4[i4];  // set the value for pd parameter 4 at this mesh point
407    const double weight4 = w4[i4] * weight5;
408
409    // from PD_OPEN(3,4)
410    while (i3 < n3) {
411      parameter[p3] = v3[i3];  // set the value for pd parameter 3 at this mesh point
412      const double weight3 = w3[i3] * weight4;
413
414      PD_OPEN(3,2)
415      PD_OPEN(2,1)
416      PD_OPEN(0,1)
417
[6aee3ab]418      // ... main loop body ...
419      APPLY_PROJECTION    // convert jitter values to spherical coords
420      BUILD_ROTATION      // construct the rotation matrix qxy => qabc
421      for each q
422          FETCH_Q         // set qx,qy from the q input vector
423          APPLY_ROTATION  // convert qx,qy to qa,qb,qc
424          CALL_KERNEL     // scattering = Iqxy(qa, qb, qc, p1, p2, ...)
[9ee2756]425
426      ++step;  // increment counter representing position in dispersity mesh
427
428      PD_CLOSE(0)
429      PD_CLOSE(1)
430      PD_CLOSE(2)
431
432      // --- PD_CLOSE(3) ---
433      if (step >= pd_stop) break;
434      ++i3;
435    }
436    i3 = 0; // reset loop counter for next round through the loop
437
438    // --- PD_CLOSE(4) ---
439    if (step >= pd_stop) break;
440    ++i4;
[2e44ac7]441  }
[9ee2756]442  i4 = 0; // reset loop counter even though no more rounds through the loop
443
444*/
445
446
[6aee3ab]447// ** prepare inner loops **
[9ee2756]448
449// Depending on the shape type (radial, axial, triaxial), the variables
[6aee3ab]450// and calling parameters in the loop body will be slightly different.
451// Macros capture the differences in one spot so the rest of the code
452// is easier to read. The code below both declares variables for the
453// inner loop and defines the macros that use them.
[ff10479]454
[01c8d9e]455
[9ee2756]456#if defined(CALL_IQ)
457  // unoriented 1D
458  double qk;
[01c8d9e]459  #if BETA == 0
460    #define FETCH_Q() do { qk = q[q_index]; } while (0)
461    #define BUILD_ROTATION() do {} while(0)
462    #define APPLY_ROTATION() do {} while(0)
463    #define CALL_KERNEL() CALL_IQ(qk,local_values.table)
464
465  // unoriented 1D Beta
466  #elif BETA == 1
467    double F1, F2;
468    #define FETCH_Q() do { qk = q[q_index]; } while (0)
469    #define BUILD_ROTATION() do {} while(0)
470    #define APPLY_ROTATION() do {} while(0)
471    #define CALL_KERNEL() CALL_IQ(qk,F1,F2,local_values.table)
472  #endif
[9ee2756]473
474#elif defined(CALL_IQ_A)
475  // unoriented 2D
476  double qx, qy;
[2c108a3]477  #define FETCH_Q() do { qx = q[2*q_index]; qy = q[2*q_index+1]; } while (0)
478  #define BUILD_ROTATION() do {} while(0)
479  #define APPLY_ROTATION() do {} while(0)
480  #define CALL_KERNEL() CALL_IQ_A(sqrt(qx*qx+qy*qy), local_values.table)
[9ee2756]481
482#elif defined(CALL_IQ_AC)
483  // oriented symmetric 2D
484  double qx, qy;
[2c108a3]485  #define FETCH_Q() do { qx = q[2*q_index]; qy = q[2*q_index+1]; } while (0)
[9ee2756]486  double qa, qc;
487  QACRotation rotation;
[ff10479]488  // theta, phi, dtheta, dphi are defined below in projection to avoid repeated code.
[767dca8]489  #define BUILD_ROTATION() qac_rotation(&rotation, theta, phi, dtheta, dphi);
[ec8d4ac]490  #define APPLY_ROTATION() qac_apply(&rotation, qx, qy, &qa, &qc)
[2c108a3]491  #define CALL_KERNEL() CALL_IQ_AC(qa, qc, local_values.table)
[9ee2756]492
493#elif defined(CALL_IQ_ABC)
494  // oriented asymmetric 2D
495  double qx, qy;
[2c108a3]496  #define FETCH_Q() do { qx = q[2*q_index]; qy = q[2*q_index+1]; } while (0)
[9ee2756]497  double qa, qb, qc;
498  QABCRotation rotation;
[ff10479]499  // theta, phi, dtheta, dphi are defined below in projection to avoid repeated code.
500  // psi and dpsi are only for IQ_ABC, so they are processed here.
501  const double psi = values[details->theta_par+4];
[6aee3ab]502  local_values.table.psi = 0.;
[ff10479]503  #define BUILD_ROTATION() qabc_rotation(&rotation, theta, phi, psi, dtheta, dphi, local_values.table.psi)
[ec8d4ac]504  #define APPLY_ROTATION() qabc_apply(&rotation, qx, qy, &qa, &qb, &qc)
[ff10479]505  #define CALL_KERNEL() CALL_IQ_ABC(qa, qb, qc, local_values.table)
[108e70e]506#elif defined(CALL_IQ_XY)
507  // direct call to qx,qy calculator
508  double qx, qy;
509  #define FETCH_Q() do { qx = q[2*q_index]; qy = q[2*q_index+1]; } while (0)
510  #define BUILD_ROTATION() do {} while(0)
511  #define APPLY_ROTATION() do {} while(0)
512  #define CALL_KERNEL() CALL_IQ_XY(qx, qy, local_values.table)
[ff10479]513#endif
514
[2a7e20e]515// Define APPLY_PROJECTION depending on model symmetries. We do this outside
516// the previous if block so that we don't need to repeat the identical
517// logic in the IQ_AC and IQ_ABC branches.  This will become more important
518// if we implement more projections, or more complicated projections.
519#if defined(CALL_IQ) || defined(CALL_IQ_A)  // no orientation
[ff10479]520  #define APPLY_PROJECTION() const double weight=weight0
[2a7e20e]521#elif defined(CALL_IQ_XY) // pass orientation to the model
[108e70e]522  // CRUFT: support oriented model which define Iqxy rather than Iqac or Iqabc
523  // Need to plug the values for the orientation angles back into parameter
524  // table in case they were overridden by the orientation offset.  This
525  // means that orientation dispersity will not work for these models, but
526  // it was broken anyway, so no matter.  Still want to provide Iqxy in case
527  // the user model wants full control of orientation/magnetism.
528  #if defined(HAVE_PSI)
529    const double theta = values[details->theta_par+2];
530    const double phi = values[details->theta_par+3];
531    const double psi = values[details->theta_par+4];
532    double weight;
533    #define APPLY_PROJECTION() do { \
534      local_values.table.theta = theta; \
535      local_values.table.phi = phi; \
536      local_values.table.psi = psi; \
537      weight=weight0; \
538    } while (0)
539  #elif defined(HAVE_THETA)
540    const double theta = values[details->theta_par+2];
541    const double phi = values[details->theta_par+3];
542    double weight;
543    #define APPLY_PROJECTION() do { \
544      local_values.table.theta = theta; \
545      local_values.table.phi = phi; \
546      weight=weight0; \
547    } while (0)
548  #else
549    #define APPLY_PROJECTION() const double weight=weight0
550  #endif
[2a7e20e]551#else // apply jitter and view before calling the model
[9ee2756]552  // Grab the "view" angles (theta, phi, psi) from the initial parameter table.
553  const double theta = values[details->theta_par+2];
554  const double phi = values[details->theta_par+3];
[6aee3ab]555  // Make sure jitter angle defaults to zero if there is no jitter distribution
556  local_values.table.theta = 0.;
557  local_values.table.phi = 0.;
[ff10479]558  // The "jitter" angles (dtheta, dphi, dpsi) are stored with the
559  // dispersity values and copied to the local parameter table as
560  // we go through the mesh.
561  double dtheta, dphi, weight;
[2a7e20e]562  #if PROJECTION == 1 // equirectangular
[ff10479]563    #define APPLY_PROJECTION() do { \
[767dca8]564      dtheta = local_values.table.theta; \
565      dphi = local_values.table.phi; \
[ff10479]566      weight = fabs(cos(dtheta*M_PI_180)) * weight0; \
[767dca8]567    } while (0)
[2a7e20e]568  #elif PROJECTION == 2 // sinusoidal
[ff10479]569    #define APPLY_PROJECTION() do { \
[767dca8]570      dtheta = local_values.table.theta; \
571      dphi = local_values.table.phi; \
[ff10479]572      weight = weight0; \
573      if (dtheta != 90.0) dphi /= cos(dtheta*M_PI_180); \
574      else if (dphi != 0.0) weight = 0.; \
575      if (fabs(dphi) >= 180.) weight = 0.; \
[767dca8]576    } while (0)
577  #endif
[2a7e20e]578#endif // done defining APPLY_PROJECTION
[9ee2756]579
[6aee3ab]580// ** define looping macros **
581
582// Define looping variables
583#define PD_INIT(_LOOP) \
584  const int n##_LOOP = details->pd_length[_LOOP]; \
585  const int p##_LOOP = details->pd_par[_LOOP]; \
586  global const double *v##_LOOP = pd_value + details->pd_offset[_LOOP]; \
587  global const double *w##_LOOP = pd_weight + details->pd_offset[_LOOP]; \
588  int i##_LOOP = (pd_start/details->pd_stride[_LOOP])%n##_LOOP;
589
590// Jump into the middle of the dispersity loop
591#define PD_OPEN(_LOOP,_OUTER) \
592  while (i##_LOOP < n##_LOOP) { \
593    local_values.vector[p##_LOOP] = v##_LOOP[i##_LOOP]; \
594    const double weight##_LOOP = w##_LOOP[i##_LOOP] * weight##_OUTER;
595
596// create the variable "weight#=1.0" where # is the outermost level+1 (=MAX_PD).
597#define _PD_OUTERMOST_WEIGHT(_n) const double weight##_n = 1.0;
598#define PD_OUTERMOST_WEIGHT(_n) _PD_OUTERMOST_WEIGHT(_n)
599
600// Close out the loop
601#define PD_CLOSE(_LOOP) \
602    if (step >= pd_stop) break; \
603    ++i##_LOOP; \
604  } \
605  i##_LOOP = 0;
[9eb3632]606
[9ee2756]607// ====== construct the loops =======
608
609// Pointers to the start of the dispersity and weight vectors, if needed.
[7b7da6b]610#if MAX_PD>0
[bde38b5]611  global const double *pd_value = values + NUM_VALUES;
612  global const double *pd_weight = pd_value + details->num_weights;
[7b7da6b]613#endif
[9eb3632]614
[9ee2756]615// The variable "step" is the current position in the dispersity loop.
616// It will be incremented each time a new point in the mesh is accumulated,
617// and used to test whether we have reached pd_stop.
618int step = pd_start;
619
[6aee3ab]620// *** define loops for each of 0, 1, 2, ..., modelinfo.MAX_PD-1 ***
621
[9ee2756]622// define looping variables
[9eb3632]623#if MAX_PD>4
[9ee2756]624  PD_INIT(4)
[9eb3632]625#endif
626#if MAX_PD>3
[9ee2756]627  PD_INIT(3)
[9eb3632]628#endif
629#if MAX_PD>2
[9ee2756]630  PD_INIT(2)
[9eb3632]631#endif
632#if MAX_PD>1
[9ee2756]633  PD_INIT(1)
[9eb3632]634#endif
635#if MAX_PD>0
[9ee2756]636  PD_INIT(0)
[9eb3632]637#endif
[2e44ac7]638
[9ee2756]639// open nested loops
640PD_OUTERMOST_WEIGHT(MAX_PD)
[9eb3632]641#if MAX_PD>4
[9ee2756]642  PD_OPEN(4,5)
[9eb3632]643#endif
644#if MAX_PD>3
[9ee2756]645  PD_OPEN(3,4)
[9eb3632]646#endif
647#if MAX_PD>2
[9ee2756]648  PD_OPEN(2,3)
[9eb3632]649#endif
650#if MAX_PD>1
[9ee2756]651  PD_OPEN(1,2)
[9eb3632]652#endif
653#if MAX_PD>0
[9ee2756]654  PD_OPEN(0,1)
[9eb3632]655#endif
[5ff1b03]656
[9ee2756]657//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");}
658
659  // ====== loop body =======
660  #ifdef INVALID
661  if (!INVALID(local_values.table))
662  #endif
663  {
[ff10479]664     APPLY_PROJECTION();
665
[9ee2756]666    // Accumulate I(q)
667    // Note: weight==0 must always be excluded
[767dca8]668    if (weight > cutoff) {
669      pd_norm += weight * CALL_VOLUME(local_values.table);
[2c108a3]670      BUILD_ROTATION();
[01c8d9e]671#if BETA
672    if (weight > cutoff) {
673      weight_norm += weight;}
674#endif
[9ee2756]675#ifndef USE_OPENCL
676      // DLL needs to explicitly loop over the q values.
677      #ifdef USE_OPENMP
678      #pragma omp parallel for
679      #endif
680      for (q_index=0; q_index<nq; q_index++)
681#endif // !USE_OPENCL
682      {
683
[2c108a3]684        FETCH_Q();
685        APPLY_ROTATION();
[9ee2756]686
687        // ======= COMPUTE SCATTERING ==========
688        #if defined(MAGNETIC) && NUM_MAGNETIC > 0
[2c108a3]689          // Compute the scattering from the magnetic cross sections.
690          double scattering = 0.0;
691          const double qsq = qx*qx + qy*qy;
692          if (qsq > 1.e-16) {
693            // TODO: what is the magnetic scattering at q=0
694            const double px = (qy*cos_mspin + qx*sin_mspin)/qsq;
695            const double py = (qy*sin_mspin - qx*cos_mspin)/qsq;
696
697            // loop over uu, ud real, du real, dd, ud imag, du imag
[aadec17]698            for (unsigned int xs=0; xs<6; xs++) {
[885753a]699              const double xs_weight = xs_weights[xs];
[2c108a3]700              if (xs_weight > 1.e-8) {
701                // Since the cross section weight is significant, set the slds
702                // to the effective slds for this cross section, call the
703                // kernel, and add according to weight.
704                for (int sk=0; sk<NUM_MAGNETIC; sk++) {
705                  const int32_t mag_index = NUM_PARS+5 + 3*sk;
706                  const int32_t sld_index = slds[sk];
707                  const double mx = values[mag_index];
708                  const double my = values[mag_index+1];
709                  const double mz = values[mag_index+2];
710                  local_values.vector[sld_index] =
711                    mag_sld(xs, qx, qy, px, py, values[sld_index+2], mx, my, mz);
[885753a]712//if (q_index==0) printf("%d: (qx,qy)=(%g,%g) xs=%d sld%d=%g p=(%g,%g) m=(%g,%g,%g)\n",
[dc6f601]713//  q_index, qx, qy, xs, sk, local_values.vector[sld_index], px, py, mx, my, mz);
[2c108a3]714                }
715                scattering += xs_weight * CALL_KERNEL();
[a4280bd]716              }
[9eb3632]717            }
[32e3c9b]718          }
[9ee2756]719        #else  // !MAGNETIC
[01c8d9e]720          #if defined(CALL_IQ) && BETA == 1
721            CALL_KERNEL();
722            const double scatteringF1 = F1;
723            const double scatteringF2 = F2;
724          #else
725            const double scattering = CALL_KERNEL();
726          #endif
[9ee2756]727        #endif // !MAGNETIC
[767dca8]728//printf("q_index:%d %g %g %g %g\n", q_index, scattering, weight0);
[9ee2756]729
730        #ifdef USE_OPENCL
[01c8d9e]731          #if defined(CALL_IQ)&& BETA == 1
732             this_result += weight * scatteringF2;
733             this_beta_result += weight * scatteringF1;
734            #else
735              this_result += weight * scattering;
736          #endif
[9ee2756]737        #else // !USE_OPENCL
[01c8d9e]738          #if defined(CALL_IQ)&& BETA == 1
739            result[q_index] += weight * scatteringF2;
740            beta_result[q_index] += weight * scatteringF1;
741            #endif
742            #else
743            result[q_index] += weight * scattering;
744          #endif
[9ee2756]745        #endif // !USE_OPENCL
[3044216]746      }
[03cac08]747    }
[2e44ac7]748  }
[9ee2756]749// close nested loops
750++step;
751#if MAX_PD>0
752  PD_CLOSE(0)
[9eb3632]753#endif
754#if MAX_PD>1
[9ee2756]755  PD_CLOSE(1)
[9eb3632]756#endif
757#if MAX_PD>2
[9ee2756]758  PD_CLOSE(2)
[9eb3632]759#endif
760#if MAX_PD>3
[9ee2756]761  PD_CLOSE(3)
[9eb3632]762#endif
763#if MAX_PD>4
[9ee2756]764  PD_CLOSE(4)
[9eb3632]765#endif
[f2f67a6]766
[9ee2756]767// Remember the current result and the updated norm.
768#ifdef USE_OPENCL
769  result[q_index] = this_result;
770  if (q_index == 0) result[nq] = pd_norm;
[01c8d9e]771  #if BETA
772  beta_result[q_index] = this_beta_result;
773  #endif
774  if (q_index == 0) result[nq] = pd_norm;
775
[9ee2756]776//if (q_index == 0) printf("res: %g/%g\n", result[0], pd_norm);
777#else // !USE_OPENCL
[a738209]778  result[nq] = pd_norm;
[01c8d9e]779  #if BETA
780  result[2*nq+1] = weight_norm;
781  #endif
[9ee2756]782//printf("res: %g/%g\n", result[0], pd_norm);
783#endif // !USE_OPENCL
[2c108a3]784
[6aee3ab]785// ** clear the macros in preparation for the next kernel **
[2c108a3]786#undef PD_INIT
787#undef PD_OPEN
788#undef PD_CLOSE
789#undef FETCH_Q
[ff10479]790#undef APPLY_PROJECTION
[2c108a3]791#undef BUILD_ROTATION
792#undef APPLY_ROTATION
793#undef CALL_KERNEL
[2e44ac7]794}
Note: See TracBrowser for help on using the repository browser.