source: sasmodels/sasmodels/kernel_iq.c @ 98ba1fc

core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change on this file since 98ba1fc was ae2b6b5, checked in by Paul Kienzle <pkienzle@…>, 8 years ago

increase code correspondance between iq.c and iq.cl

  • Property mode set to 100644
File size: 8.2 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 par_offset[NPARS];  // offset of par value blocks in the value & weight vector
25    int32_t par_coord[NPARS];   // ids of the coordination parameters
26    int32_t pd_coord[NPARS];    // polydispersity coordination bitvector
27    int32_t num_active;         // number of non-trivial pd loops
28    int32_t total_pd;           // total number of voxels in hypercube
29    int32_t num_coord;          // number of coordinated parameters
30    int32_t theta_par;          // id of spherical correction variable
31} ProblemDetails;
32
33typedef struct {
34    PARAMETER_TABLE;
35} ParameterBlock;
36#endif
37
38
39kernel
40void KERNEL_NAME(
41    int32_t nq,                 // number of q values
42    const int32_t pd_start,     // where we are in the polydispersity loop
43    const int32_t pd_stop,      // where we are stopping in the polydispersity loop
44    global const ProblemDetails *details,
45    global const double *weights,
46    global const double *values,
47    global const double *q, // nq q values, with padding to boundary
48    global double *result,  // nq+3 return values, again with padding
49    const double cutoff     // cutoff in the polydispersity weight product
50    )
51{
52  // Storage for the current parameter values.  These will be updated as we
53  // walk the polydispersity cube.
54  ParameterBlock local_values;  // current parameter values
55  double *pvec = (double *)(&local_values);  // Alias named parameters with a vector
56  double norm;
57
58  // number of active loops
59  const int num_active = details->num_active;
60
61  // Fill in the initial variables
62  #ifdef USE_OPENMP
63  #pragma omp parallel for
64  #endif
65  for (int k=0; k < NPARS; k++) {
66    pvec[k] = values[details->par_offset[k]];
67  }
68
69  // Monodisperse computation
70  if (num_active == 0) {
71    #ifdef INVALID
72    if (INVALID(local_values)) { return; }
73    #endif
74    norm = CALL_VOLUME(local_values);
75
76    double scale, background;
77    scale = values[0];
78    background = values[1];
79
80    #ifdef USE_OPENMP
81    #pragma omp parallel for
82    #endif
83    for (int q_index=0; q_index < nq; q_index++) {
84      double scattering = CALL_IQ(q, q_index, local_values);
85      result[q_index] = (norm>0. ? scale*scattering/norm + background : background);
86    }
87    return;
88  }
89
90#if MAX_PD > 0
91
92  // need product of weights at every Iq calc, so keep product of
93  // weights from the outer loops so that weight = partial_weight * fast_weight
94  double partial_weight; // product of weight w4*w3*w2 but not w1
95  double spherical_correction; // cosine correction for latitude variation
96  double weight; // product of partial_weight*w1*spherical_correction
97
98  // Location in the polydispersity hypercube, one index per dimension.
99  int pd_index[MAX_PD];
100
101  // Location of the coordinated parameters in their own sub-cubes.
102  int offset[NPARS];
103
104  // Number of coordinated indices
105  const int num_coord = details->num_coord;
106
107  // Number of elements in the longest polydispersity loop
108  const int fast_length = details->pd_length[0];
109
110  // Trigger the reset behaviour that happens at the end the fast loop
111  // by setting the initial index >= weight vector length.
112  pd_index[0] = fast_length;
113
114  // Default the spherical correction to 1.0 in case it is not otherwise set
115  spherical_correction = 1.0;
116
117  // Since we are no longer looping over the entire polydispersity hypercube
118  // for each q, we need to track the result and normalization values between
119  // calls.  This means initializing them to 0 at the start and accumulating
120  // them between calls.
121  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++) {
127      result[q_index] = 0.0;
128    }
129  }
130
131  // Loop over the weights then loop over q, accumulating values
132  for (int loop_index=pd_start; loop_index < pd_stop; loop_index++) {
133    // check if fast loop needs to be reset
134    if (pd_index[0] == fast_length) {
135      //printf("should be here with %d active\n", num_active);
136
137      // Compute position in polydispersity hypercube
138      for (int k=0; k < num_active; k++) {
139        pd_index[k] = (loop_index/details->pd_stride[k])%details->pd_length[k];
140        //printf("pd_index[%d] = %d\n",k,pd_index[k]);
141      }
142
143      // Compute partial weights
144      partial_weight = 1.0;
145      //printf("partial weight %d: ", loop_index);
146      for (int k=1; k < num_active; k++) {
147        double wi = weights[details->pd_offset[k] + pd_index[k]];
148        //printf("pd[%d]=par[%d]=%g ", k, details->pd_par[k], wi);
149        partial_weight *= wi;
150      }
151      //printf("\n");
152
153      // Update parameter offsets in weight vector
154      //printf("slow %d: ", loop_index);
155      for (int k=0; k < num_coord; k++) {
156        int par = details->par_coord[k];
157        int coord = details->pd_coord[k];
158        int this_offset = details->par_offset[par];
159        int block_size = 1;
160        for (int bit=0; coord != 0; bit++) {
161          if (coord&1) {
162              this_offset += block_size * pd_index[bit];
163              block_size *= details->pd_length[bit];
164          }
165          coord >>= 1;
166        }
167        offset[par] = this_offset;
168        pvec[par] = values[this_offset];
169        //printf("par[%d]=v[%d]=%g \n", k, offset[k], pvec[k]);
170        // if theta is not coordinated with fast index, precompute spherical correction
171        if (par == details->theta_par && !(details->par_coord[k]&1)) {
172          spherical_correction = fmax(fabs(cos(M_PI_180*pvec[details->theta_par])), 1.e-6);
173        }
174      }
175      //printf("\n");
176    }
177
178    // Update fast parameters
179    //printf("fast %d: ", loop_index);
180    for (int k=0; k < num_coord; k++) {
181      if (details->pd_coord[k]&1) {
182        const int par = details->par_coord[k];
183        pvec[par] = values[offset[par]++];
184        //printf("p[%d]=v[%d]=%g ", par, offset[par]-1, pvec[par]);
185        // if theta is coordinated with fast index, compute spherical correction each time
186        if (par == details->theta_par) {
187          spherical_correction = fmax(fabs(cos(M_PI_180*pvec[details->theta_par])), 1.e-6);
188        }
189      }
190    }
191    //printf("\n");
192
193    // Increment fast index
194    const double wi = weights[details->pd_offset[0] + pd_index[0]];
195    weight = partial_weight*wi;
196    pd_index[0]++;
197
198    #ifdef INVALID
199    if (INVALID(local_values)) continue;
200    #endif
201
202    // Accumulate I(q)
203    // Note: weight==0 must always be excluded
204    if (weight > cutoff) {
205      // spherical correction has some nasty effects when theta is +90 or -90
206      // where it becomes zero.  If the entirety of the correction
207      weight *= spherical_correction;
208      norm += weight * CALL_VOLUME(local_values);
209
210      #ifdef USE_OPENMP
211      #pragma omp parallel for
212      #endif
213      for (int q_index=0; q_index < nq; q_index++) {
214        const double scattering = CALL_IQ(q, q_index, local_values);
215        result[q_index] += weight*scattering;
216      }
217    }
218  }
219
220  if (pd_stop >= details->total_pd) {
221    // End of the PD loop we can normalize
222    double scale, background;
223    scale = values[0];
224    background = values[1];
225    #ifdef USE_OPENMP
226    #pragma omp parallel for
227    #endif
228    for (int q_index=0; q_index < nq; q_index++) {
229      result[q_index] = (norm>0. ? scale*result[q_index]/norm + background : background);
230    }
231  }
232
233  // Remember the updated norm.
234  result[nq] = norm;
235#endif // MAX_PD > 0
236}
Note: See TracBrowser for help on using the repository browser.