source: sasmodels/sasmodels/kernel_iq.c @ d4c33d6

core_shell_microgelsmagnetic_modelticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since d4c33d6 was d4c33d6, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

send cos-weighted theta values to kernel rather than computing them inside

  • Property mode set to 100644
File size: 10.9 KB
Line 
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
17typedef 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
24    int32_t num_eval;           // total number of voxels in hypercube
25    int32_t num_weights;        // total length of the weights vector
26    int32_t num_active;         // number of non-trivial pd loops
27    int32_t theta_par;          // id of spherical correction variable (not used)
28} ProblemDetails;
29
30// Intel HD 4000 needs private arrays to be a multiple of 4 long
31typedef struct {
32    PARAMETER_TABLE
33} ParameterTable;
34typedef union {
35    ParameterTable table;
36    double vector[4*((NUM_PARS+3)/4)];
37} ParameterBlock;
38#endif // _PAR_BLOCK_
39
40
41#if defined(MAGNETIC) && NUM_MAGNETIC>0
42
43// Return value restricted between low and high
44static 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);
55static void set_spins(double in_spin, double out_spin, double spins[4])
56{
57  in_spin = clip(in_spin, 0.0, 1.0);
58  out_spin = clip(out_spin, 0.0, 1.0);
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
65static 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;
70}
71
72#endif // MAGNETIC
73
74kernel
75void 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
82    global double *result,  // nq+1 return values, again with padding
83    const double cutoff     // cutoff in the polydispersity weight product
84    )
85{
86  // Storage for the current parameter values.  These will be updated as we
87  // walk the polydispersity cube.
88  ParameterBlock local_values;
89
90#if defined(MAGNETIC) && NUM_MAGNETIC>0
91  // Location of the sld parameters in the parameter vector.
92  // These parameters are updated with the effective sld due to magnetism.
93  #if NUM_MAGNETIC > 3
94  const int32_t slds[] = { MAGNETIC_PARS };
95  #endif
96
97  // TODO: could precompute these outside of the kernel.
98  // Interpret polarization cross section.
99  //     up_frac_i = values[NUM_PARS+2];
100  //     up_frac_f = values[NUM_PARS+3];
101  //     up_angle = values[NUM_PARS+4];
102  double spins[4];
103  double cos_mspin, sin_mspin;
104  set_spins(values[NUM_PARS+2], values[NUM_PARS+3], spins);
105  SINCOS(-values[NUM_PARS+4]*M_PI_180, sin_mspin, cos_mspin);
106#endif // MAGNETIC
107
108  // Fill in the initial variables
109  //   values[0] is scale
110  //   values[1] is background
111  #ifdef USE_OPENMP
112  #pragma omp parallel for
113  #endif
114  for (int i=0; i < NUM_PARS; i++) {
115    local_values.vector[i] = values[2+i];
116//printf("p%d = %g\n",i, local_values.vector[i]);
117  }
118//printf("NUM_VALUES:%d  NUM_PARS:%d  MAX_PD:%d\n", NUM_VALUES, NUM_PARS, MAX_PD);
119//printf("start:%d  stop:%d\n", pd_start, pd_stop);
120
121  double pd_norm = (pd_start == 0 ? 0.0 : result[nq]);
122  if (pd_start == 0) {
123    #ifdef USE_OPENMP
124    #pragma omp parallel for
125    #endif
126    for (int q_index=0; q_index < nq; q_index++) result[q_index] = 0.0;
127  }
128//printf("start %d %g %g\n", pd_start, pd_norm, result[0]);
129
130#if MAX_PD>0
131  global const double *pd_value = values + NUM_VALUES;
132  global const double *pd_weight = pd_value + details->num_weights;
133#endif
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:%ld, %d\n",w0,values,(w0-values), NUM_VALUES);
172#endif
173
174
175  int step = pd_start;
176
177#if MAX_PD>4
178  const double weight5 = 1.0;
179  while (i4 < n4) {
180    local_values.vector[p4] = v4[i4];
181    double weight4 = w4[i4] * weight5;
182//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);
183#elif MAX_PD>3
184    const double weight4 = 1.0;
185#endif
186#if MAX_PD>3
187  while (i3 < n3) {
188    local_values.vector[p3] = v3[i3];
189    double weight3 = w3[i3] * weight4;
190//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);
191#elif MAX_PD>2
192    const double weight3 = 1.0;
193#endif
194#if MAX_PD>2
195  while (i2 < n2) {
196    local_values.vector[p2] = v2[i2];
197    double weight2 = w2[i2] * weight3;
198//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);
199#elif MAX_PD>1
200    const double weight2 = 1.0;
201#endif
202#if MAX_PD>1
203  while (i1 < n1) {
204    local_values.vector[p1] = v1[i1];
205    double weight1 = w1[i1] * weight2;
206//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);
207#elif MAX_PD>0
208    const double weight1 = 1.0;
209#endif
210#if MAX_PD>0
211  while(i0 < n0) {
212    local_values.vector[p0] = v0[i0];
213    double weight0 = w0[i0] * weight1;
214//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);
215#else
216    const double weight0 = 1.0;
217#endif
218
219//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");
220//printf("sphcor: %g\n", spherical_correction);
221
222    #ifdef INVALID
223    if (!INVALID(local_values.table))
224    #endif
225    {
226      // Accumulate I(q)
227      // Note: weight==0 must always be excluded
228      if (weight0 > cutoff) {
229        pd_norm += weight0 * CALL_VOLUME(local_values.table);
230
231        #ifdef USE_OPENMP
232        #pragma omp parallel for
233        #endif
234        for (int q_index=0; q_index<nq; q_index++) {
235#if defined(MAGNETIC) && NUM_MAGNETIC > 0
236          const double qx = q[2*q_index];
237          const double qy = q[2*q_index+1];
238          const double qsq = qx*qx + qy*qy;
239
240          // Constant across orientation, polydispersity for given qx, qy
241          double scattering = 0.0;
242          // TODO: what is the magnetic scattering at q=0
243          if (qsq > 1.e-16) {
244            double p[4];  // dd, du, ud, uu
245            p[0] = (qy*cos_mspin + qx*sin_mspin)/qsq;
246            p[3] = -p[0];
247            p[1] = p[2] = (qy*sin_mspin - qx*cos_mspin)/qsq;
248
249            for (int index=0; index<4; index++) {
250              const double xs = spins[index];
251              if (xs > 1.e-8) {
252                const int spin_flip = (index==1) || (index==2);
253                const double pk = p[index];
254                for (int axis=0; axis<=spin_flip; axis++) {
255                  #define M1 NUM_PARS+5
256                  #define M2 NUM_PARS+8
257                  #define M3 NUM_PARS+13
258                  #define SLD(_M_offset, _sld_offset) \
259                      local_values.vector[_sld_offset] = xs * (axis \
260                      ? (index==1 ? -values[_M_offset+2] : values[_M_offset+2]) \
261                      : mag_sld(qx, qy, pk, values[_M_offset], values[_M_offset+1], \
262                                (spin_flip ? 0.0 : values[_sld_offset+2])))
263                  #if NUM_MAGNETIC==1
264                      SLD(M1, MAGNETIC_PAR1);
265                  #elif NUM_MAGNETIC==2
266                      SLD(M1, MAGNETIC_PAR1);
267                      SLD(M2, MAGNETIC_PAR2);
268                  #elif NUM_MAGNETIC==3
269                      SLD(M1, MAGNETIC_PAR1);
270                      SLD(M2, MAGNETIC_PAR2);
271                      SLD(M3, MAGNETIC_PAR3);
272                  #else
273                  for (int sk=0; sk<NUM_MAGNETIC; sk++) {
274                      SLD(M1+3*sk, slds[sk]);
275                  }
276                  #endif
277                  scattering += CALL_IQ(q, q_index, local_values.table);
278                }
279              }
280            }
281          }
282#else  // !MAGNETIC
283          const double scattering = CALL_IQ(q, q_index, local_values.table);
284#endif // !MAGNETIC
285//printf("q_index:%d %g %g %g %g\n",q_index, scattering, weight, spherical_correction, weight0);
286          result[q_index] += weight0 * scattering;
287        }
288      }
289    }
290    ++step;
291#if MAX_PD>0
292    if (step >= pd_stop) break;
293    ++i0;
294  }
295  i0 = 0;
296#endif
297#if MAX_PD>1
298    if (step >= pd_stop) break;
299    ++i1;
300  }
301  i1 = 0;
302#endif
303#if MAX_PD>2
304    if (step >= pd_stop) break;
305    ++i2;
306  }
307  i2 = 0;
308#endif
309#if MAX_PD>3
310    if (step >= pd_stop) break;
311    ++i3;
312  }
313  i3 = 0;
314#endif
315#if MAX_PD>4
316    if (step >= pd_stop) break;
317    ++i4;
318  }
319  i4 = 0;
320#endif
321
322//printf("res: %g/%g\n", result[0], pd_norm);
323  // Remember the updated norm.
324  result[nq] = pd_norm;
325}
Note: See TracBrowser for help on using the repository browser.