source: sasmodels/sasmodels/kernel_iq.c @ 60eab2a

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

support autogenerated Iqxy in C models

  • Property mode set to 100644
File size: 7.6 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    int32_t pd_isvol[MAX_PD];   // True if parameter is a volume weighting parameter
24#endif // MAX_PD > 0
25    int32_t par_offset[NPARS];  // offset of par values in the value & weight vector
26    int32_t par_coord[NPARS];   // polydispersity coordination bitvector
27    int32_t fast_coord_pars[NPARS]; // ids of the fast coordination parameters
28    int32_t fast_coord_count;   // number of parameters coordinated with pd 1
29    int32_t theta_par;          // id of spherical correction variable
30} ProblemDetails;
31
32typedef struct {
33    PARAMETER_TABLE;
34} ParameterBlock;
35#endif
36
37
38kernel
39void KERNEL_NAME(
40    int32_t nq,                 // number of q values
41    const int32_t pd_start,     // where we are in the polydispersity loop
42    const int32_t pd_stop,      // where we are stopping in the polydispersity loop
43    global const ProblemDetails *problem,
44    global const double *weights,
45    global const double *values,
46    global const double *q, // nq q values, with padding to boundary
47    global double *result,  // nq+3 return values, again with padding
48    const double cutoff     // cutoff in the polydispersity weight product
49    )
50{
51  // Storage for the current parameter values.  These will be updated as we
52  // walk the polydispersity cube.
53  local ParameterBlock local_values;  // current parameter values
54  double *pvec = (double *)(&local_values);  // Alias named parameters with a vector
55
56#if MAX_PD > 0
57  if (problem->pd_length[0] == 1) {
58#endif // MAX_PD > 0
59    // Shouldn't need to copy!!
60    for (int k=0; k < NPARS; k++) {
61      pvec[k] = values[k+2];  // skip scale and background
62    }
63
64    const double volume = CALL_VOLUME(local_values);
65    #ifdef USE_OPENMP
66    #pragma omp parallel for
67    #endif
68    for (int i=0; i < nq; i++) {
69      double scattering = CALL_IQ(q, i, local_values);
70      if (volume != 0.0) scattering /= volume;
71      result[i] = values[0]*scattering + values[1];
72    }
73    return;
74#if MAX_PD > 0
75  }
76
77  // polydispersity loop index positions
78  local int offset[NPARS];  // NPARS excludes scale/background
79
80  printf("Entering polydispersity\n");
81  // Since we are no longer looping over the entire polydispersity hypercube
82  // for each q, we need to track the normalization values for each q in a
83  // separate work vector.
84  double norm;   // contains sum over weights
85  double vol; // contains sum over volume
86  double norm_vol; // contains weights over volume
87
88  // Initialize the results to zero
89  if (pd_start == 0) {
90    norm_vol = 0.0;
91    norm = 0.0;
92    vol = 0.0;
93
94    #ifdef USE_OPENMP
95    #pragma omp parallel for
96    #endif
97    for (int i=0; i < nq; i++) {
98      result[i] = 0.0;
99    }
100  } else {
101    //Pulling values from previous segment
102    norm = result[nq];
103    vol = result[nq+1];
104    norm_vol = result[nq+2];
105  }
106
107  // Location in the polydispersity hypercube, one index per dimension.
108  local int pd_index[MAX_PD];
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] = problem->pd_length[0];
113
114
115  // need product of weights at every Iq calc, so keep product of
116  // weights from the outer loops so that weight = partial_weight * fast_weight
117  double partial_weight = NAN; // product of weight w4*w3*w2 but not w1
118  double partial_volweight = NAN;
119  double weight = 1.0;        // set to 1 in case there are no weights
120  double vol_weight = 1.0;    // set to 1 in case there are no vol weights
121  double spherical_correction = 1.0;  // correction for latitude variation
122
123  // Loop over the weights then loop over q, accumulating values
124  for (int loop_index=pd_start; loop_index < pd_stop; loop_index++) {
125    // check if indices need to be updated
126    if (pd_index[0] >= problem->pd_length[0]) {
127
128      // RESET INDICES
129      pd_index[0] = loop_index%problem->pd_length[0];
130      partial_weight = 1.0;
131      partial_volweight = 1.0;
132      for (int k=1; k < MAX_PD; k++) {
133        pd_index[k] = (loop_index%problem->pd_length[k])/problem->pd_stride[k];
134        const double wi = weights[problem->pd_offset[0]+pd_index[0]];
135        partial_weight *= wi;
136        if (problem->pd_isvol[k]) partial_volweight *= wi;
137      }
138      for (int k=0; k < NPARS; k++) {
139        int coord = problem->par_coord[k];
140        int this_offset = problem->par_offset[k];
141        int block_size = 1;
142        for (int bit=0; bit < MAX_PD && coord != 0; bit++) {
143          if (coord&1) {
144              this_offset += block_size * pd_index[bit];
145              block_size *= problem->pd_length[bit];
146          }
147          coord /= 2;
148        }
149        offset[k] = this_offset;
150        pvec[k] = values[this_offset];
151      }
152      weight = partial_weight * weights[problem->pd_offset[0]+pd_index[0]];
153      if (problem->theta_par >= 0) {
154        spherical_correction = fabs(cos(M_PI_180*pvec[problem->theta_par]));
155      }
156      if (problem->theta_par == problem->pd_par[0]) {
157        weight *= spherical_correction;
158      }
159
160    } else {
161
162      // INCREMENT INDICES
163      pd_index[0] += 1;
164      const double wi = weights[problem->pd_offset[0]+pd_index[0]];
165      weight = partial_weight*wi;
166      if (problem->pd_isvol[0]) vol_weight *= wi;
167      for (int k=0; k < problem->fast_coord_count; k++) {
168         printf("fast loop %d coord %d idx %d ?%d offset %d %g\n",
169         k,problem->fast_coord_pars[k],pd_index[0],
170        problem->fast_coord_pars[k],
171            offset[problem->fast_coord_pars[k]],
172            values[offset[problem->fast_coord_pars[k]]]
173          );
174        pvec[problem->fast_coord_pars[k]]
175            = values[offset[problem->fast_coord_pars[k]]++];
176
177      }
178      if (problem->theta_par ==problem->pd_par[0]) {
179        weight *= fabs(cos(M_PI_180*pvec[problem->theta_par]));
180      }
181    }
182    #ifdef INVALID
183    if (INVALID(local_values)) continue;
184    #endif
185
186    // Accumulate I(q)
187    // Note: weight==0 must always be excluded
188    if (weight > cutoff) {
189      norm += weight;
190      vol += vol_weight * CALL_VOLUME(local_values);
191      norm_vol += vol_weight;
192
193      #ifdef USE_OPENMP
194      #pragma omp parallel for
195      #endif
196      for (int i=0; i < nq; i++) {
197        const double scattering = CALL_IQ(q, i, local_values);
198        result[i] += weight*scattering;
199      }
200    }
201  }
202
203  //Makes a normalization available for the next round
204  result[nq] = norm;
205  result[nq+1] = vol;
206  result[nq+2] = norm_vol;
207
208  //End of the PD loop we can normalize
209  if (pd_stop >= problem->pd_stride[MAX_PD-1]) {
210    #ifdef USE_OPENMP
211    #pragma omp parallel for
212    #endif
213    for (int i=0; i < nq; i++) {
214      if (vol*norm_vol != 0.0) {
215        result[i] *= norm_vol/vol;
216      }
217      result[i] = values[0]*result[i]/norm + values[1];
218    }
219  }
220#endif // MAX_PD > 0
221}
Note: See TracBrowser for help on using the repository browser.