[a7684e5] | 1 | """ |
---|
| 2 | SAS model constructor. |
---|
| 3 | |
---|
| 4 | Small angle scattering models are defined by a set of kernel functions: |
---|
| 5 | |
---|
| 6 | *Iq(q, p1, p2, ...)* returns the scattering at q for a form with |
---|
| 7 | particular dimensions averaged over all orientations. |
---|
| 8 | |
---|
| 9 | *Iqxy(qx, qy, p1, p2, ...)* returns the scattering at qx,qy for a form |
---|
| 10 | with particular dimensions for a single orientation. |
---|
| 11 | |
---|
| 12 | *Imagnetic(qx, qy, result[], p1, p2, ...)* returns the scattering for the |
---|
| 13 | polarized neutron spin states (up-up, up-down, down-up, down-down) for |
---|
| 14 | a form with particular dimensions for a single orientation. |
---|
| 15 | |
---|
| 16 | *form_volume(p1, p2, ...)* returns the volume of the form with particular |
---|
| 17 | dimension. |
---|
| 18 | |
---|
| 19 | *ER(p1, p2, ...)* returns the effective radius of the form with |
---|
| 20 | particular dimensions. |
---|
| 21 | |
---|
| 22 | *VR(p1, p2, ...)* returns the volume ratio for core-shell style forms. |
---|
| 23 | |
---|
| 24 | These functions are defined in a kernel module .py script and an associated |
---|
| 25 | set of .c files. The model constructor will use them to create models with |
---|
| 26 | polydispersity across volume and orientation parameters, and provide |
---|
| 27 | scale and background parameters for each model. |
---|
| 28 | |
---|
| 29 | *Iq*, *Iqxy*, *Imagnetic* and *form_volume* should be stylized C-99 |
---|
[5d4777d] | 30 | functions written for OpenCL. All functions need prototype declarations |
---|
| 31 | even if the are defined before they are used. OpenCL does not support |
---|
| 32 | *#include* preprocessor directives, so instead the list of includes needs |
---|
| 33 | to be given as part of the metadata in the kernel module definition. |
---|
| 34 | The included files should be listed using a path relative to the kernel |
---|
| 35 | module, or if using "lib/file.c" if it is one of the standard includes |
---|
| 36 | provided with the sasmodels source. The includes need to be listed in |
---|
| 37 | order so that functions are defined before they are used. |
---|
| 38 | |
---|
[994d77f] | 39 | Floating point values should be declared as *double*. For single precision |
---|
| 40 | calculations, *double* will be replaced by *float*. The single precision |
---|
| 41 | conversion will also tag floating point constants with "f" to make them |
---|
| 42 | single precision constants. When using integral values in floating point |
---|
| 43 | expressions, they should be expressed as floating point values by including |
---|
| 44 | a decimal point. This includes 0., 1. and 2. |
---|
[5d4777d] | 45 | |
---|
| 46 | OpenCL has a *sincos* function which can improve performance when both |
---|
| 47 | the *sin* and *cos* values are needed for a particular argument. Since |
---|
| 48 | this function does not exist in C99, all use of *sincos* should be |
---|
| 49 | replaced by the macro *SINCOS(value,sn,cn)* where *sn* and *cn* are |
---|
[994d77f] | 50 | previously declared *double* variables. When compiled for systems without |
---|
| 51 | OpenCL, *SINCOS* will be replaced by *sin* and *cos* calls. If *value* is |
---|
| 52 | an expression, it will appear twice in this case; whether or not it will be |
---|
| 53 | evaluated twice depends on the quality of the compiler. |
---|
[5d4777d] | 54 | |
---|
| 55 | If the input parameters are invalid, the scattering calculator should |
---|
| 56 | return a negative number. Particularly with polydispersity, there are |
---|
| 57 | some sets of shape parameters which lead to nonsensical forms, such |
---|
| 58 | as a capped cylinder where the cap radius is smaller than the |
---|
| 59 | cylinder radius. The polydispersity calculation will ignore these points, |
---|
| 60 | effectively chopping the parameter weight distributions at the boundary |
---|
| 61 | of the infeasible region. The resulting scattering will be set to |
---|
| 62 | background. This will work correctly even when polydispersity is off. |
---|
[a7684e5] | 63 | |
---|
| 64 | *ER* and *VR* are python functions which operate on parameter vectors. |
---|
| 65 | The constructor code will generate the necessary vectors for computing |
---|
| 66 | them with the desired polydispersity. |
---|
| 67 | |
---|
[ff7119b] | 68 | The available kernel parameters are defined as a list, with each parameter |
---|
| 69 | defined as a sublist with the following elements: |
---|
| 70 | |
---|
| 71 | *name* is the name that will be used in the call to the kernel |
---|
| 72 | function and the name that will be displayed to the user. Names |
---|
| 73 | should be lower case, with words separated by underscore. If |
---|
| 74 | acronyms are used, the whole acronym should be upper case. |
---|
| 75 | |
---|
| 76 | *units* should be one of *degrees* for angles, *Ang* for lengths, |
---|
| 77 | *1e-6/Ang^2* for SLDs. |
---|
| 78 | |
---|
| 79 | *default value* will be the initial value for the model when it |
---|
| 80 | is selected, or when an initial value is not otherwise specified. |
---|
| 81 | |
---|
| 82 | [*lb*, *ub*] are the hard limits on the parameter value, used to limit |
---|
| 83 | the polydispersity density function. In the fit, the parameter limits |
---|
| 84 | given to the fit are the limits on the central value of the parameter. |
---|
| 85 | If there is polydispersity, it will evaluate parameter values outside |
---|
| 86 | the fit limits, but not outside the hard limits specified in the model. |
---|
| 87 | If there are no limits, use +/-inf imported from numpy. |
---|
| 88 | |
---|
| 89 | *type* indicates how the parameter will be used. "volume" parameters |
---|
| 90 | will be used in all functions. "orientation" parameters will be used |
---|
| 91 | in *Iqxy* and *Imagnetic*. "magnetic* parameters will be used in |
---|
| 92 | *Imagnetic* only. If *type* is the empty string, the parameter will |
---|
| 93 | be used in all of *Iq*, *Iqxy* and *Imagnetic*. |
---|
| 94 | |
---|
| 95 | *description* is a short description of the parameter. This will |
---|
| 96 | be displayed in the parameter table and used as a tool tip for the |
---|
| 97 | parameter value in the user interface. |
---|
| 98 | |
---|
[a7684e5] | 99 | The kernel module must set variables defining the kernel meta data: |
---|
| 100 | |
---|
| 101 | *name* is the model name |
---|
| 102 | |
---|
| 103 | *title* is a short description of the model, suitable for a tool tip, |
---|
| 104 | or a one line model summary in a table of models. |
---|
| 105 | |
---|
| 106 | *description* is an extended description of the model to be displayed |
---|
| 107 | while the model parameters are being edited. |
---|
| 108 | |
---|
[ff7119b] | 109 | *parameters* is the list of parameters. Parameters in the kernel |
---|
| 110 | functions must appear in the same order as they appear in the |
---|
| 111 | parameters list. Two additional parameters, *scale* and *background* |
---|
| 112 | are added to the beginning of the parameter list. They will show up |
---|
| 113 | in the documentation as model parameters, but they are never sent to |
---|
| 114 | the kernel functions. |
---|
[a7684e5] | 115 | |
---|
| 116 | *source* is the list of C-99 source files that must be joined to |
---|
| 117 | create the OpenCL kernel functions. The files defining the functions |
---|
| 118 | need to be listed before the files which use the functions. |
---|
| 119 | |
---|
| 120 | *ER* is a python function defining the effective radius. If it is |
---|
| 121 | not present, the effective radius is 0. |
---|
| 122 | |
---|
| 123 | *VR* is a python function defining the volume ratio. If it is not |
---|
| 124 | present, the volume ratio is 1. |
---|
| 125 | |
---|
[5d4777d] | 126 | *form_volume*, *Iq*, *Iqxy*, *Imagnetic* are strings containing the |
---|
| 127 | C source code for the body of the volume, Iq, and Iqxy functions |
---|
| 128 | respectively. These can also be defined in the last source file. |
---|
| 129 | |
---|
[a503bfd] | 130 | *Iq* and *Iqxy* also be instead be python functions defining the |
---|
| 131 | kernel. If they are marked as *Iq.vectorized = True* then the |
---|
| 132 | kernel is passed the entire *q* vector at once, otherwise it is |
---|
| 133 | passed values one *q* at a time. The performance improvement of |
---|
| 134 | this step is significant. |
---|
| 135 | |
---|
[ff7119b] | 136 | An *info* dictionary is constructed from the kernel meta data and |
---|
[a503bfd] | 137 | returned to the caller. |
---|
| 138 | |
---|
| 139 | Additional fields can be defined in the kernel definition file that |
---|
| 140 | are not needed for sas modelling. |
---|
| 141 | |
---|
| 142 | *demo* is a dictionary of parameter=value defining a set of |
---|
| 143 | parameters to use by default when *compare* is called. |
---|
[ff7119b] | 144 | |
---|
[a503bfd] | 145 | *oldname* is the name of the model in sasview before sasmodels |
---|
| 146 | was split into its own package, and *oldpars* is a dictionary |
---|
| 147 | of *parameter: old_parameter* pairs defining the new names for |
---|
| 148 | the parameters. This is used by *compare* to check the values |
---|
| 149 | of the new model against the values of the old model before |
---|
| 150 | you are ready to add the new model to sasmodels. |
---|
[ff7119b] | 151 | |
---|
| 152 | The model evaluator, function call sequence consists of q inputs and the return vector, |
---|
| 153 | followed by the loop value/weight vector, followed by the values for |
---|
| 154 | the non-polydisperse parameters, followed by the lengths of the |
---|
| 155 | polydispersity loops. To construct the call for 1D models, the |
---|
| 156 | categories *fixed-1d* and *pd-1d* list the names of the parameters |
---|
| 157 | of the non-polydisperse and the polydisperse parameters respectively. |
---|
| 158 | Similarly, *fixed-2d* and *pd-2d* provide parameter names for 2D models. |
---|
| 159 | The *pd-rel* category is a set of those parameters which give |
---|
| 160 | polydispersitiy as a portion of the value (so a 10% length dispersity |
---|
| 161 | would use a polydispersity value of 0.1) rather than absolute |
---|
| 162 | dispersity such as an angle plus or minus 15 degrees. |
---|
| 163 | |
---|
| 164 | The *volume* category lists the volume parameters in order for calls |
---|
| 165 | to volume within the kernel (used for volume normalization) and for |
---|
| 166 | calls to ER and VR for effective radius and volume ratio respectively. |
---|
| 167 | |
---|
| 168 | The *orientation* and *magnetic* categories list the orientation and |
---|
| 169 | magnetic parameters. These are used by the sasview interface. The |
---|
| 170 | blank category is for parameters such as scale which don't have any |
---|
| 171 | other marking. |
---|
| 172 | |
---|
[a7684e5] | 173 | The doc string at the start of the kernel module will be used to |
---|
| 174 | construct the model documentation web pages. Embedded figures should |
---|
| 175 | appear in the subdirectory "img" beside the model definition, and tagged |
---|
| 176 | with the kernel module name to avoid collision with other models. Some |
---|
| 177 | file systems are case-sensitive, so only use lower case characters for |
---|
| 178 | file names and extensions. |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | The function :func:`make` loads the metadata from the module and returns |
---|
[ff7119b] | 182 | the kernel source. The function :func:`doc` extracts the doc string |
---|
| 183 | and adds the parameter table to the top. The function :func:`sources` |
---|
| 184 | returns a list of files required by the model. |
---|
[a7684e5] | 185 | """ |
---|
| 186 | |
---|
| 187 | # TODO: identify model files which have changed since loading and reload them. |
---|
| 188 | |
---|
[99e6860] | 189 | __all__ = ["make", "doc", "sources", "use_single"] |
---|
[14de349] | 190 | |
---|
[5d4777d] | 191 | import sys |
---|
[f734e7d] | 192 | from os.path import abspath, dirname, join as joinpath, exists |
---|
[994d77f] | 193 | import re |
---|
[14de349] | 194 | |
---|
| 195 | import numpy as np |
---|
[f734e7d] | 196 | C_KERNEL_TEMPLATE_PATH = joinpath(dirname(__file__), 'kernel_template.c') |
---|
[14de349] | 197 | |
---|
| 198 | F64 = np.dtype('float64') |
---|
| 199 | F32 = np.dtype('float32') |
---|
| 200 | |
---|
[ce27e21] | 201 | # Scale and background, which are parameters common to every form factor |
---|
| 202 | COMMON_PARAMETERS = [ |
---|
[33e91b1] | 203 | ["scale", "", 1, [0, np.inf], "", "Source intensity"], |
---|
| 204 | ["background", "1/cm", 0, [0, np.inf], "", "Source background"], |
---|
[ce27e21] | 205 | ] |
---|
| 206 | |
---|
| 207 | |
---|
[14de349] | 208 | # Conversion from units defined in the parameter table for each model |
---|
| 209 | # to units displayed in the sphinx documentation. |
---|
| 210 | RST_UNITS = { |
---|
| 211 | "Ang": "|Ang|", |
---|
[a5d0d00] | 212 | "1/Ang": "|Ang^-1|", |
---|
[14de349] | 213 | "1/Ang^2": "|Ang^-2|", |
---|
| 214 | "1e-6/Ang^2": "|1e-6Ang^-2|", |
---|
| 215 | "degrees": "degree", |
---|
| 216 | "1/cm": "|cm^-1|", |
---|
| 217 | "": "None", |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | # Headers for the parameters tables in th sphinx documentation |
---|
| 221 | PARTABLE_HEADERS = [ |
---|
[19dcb933] | 222 | "Parameter", |
---|
| 223 | "Description", |
---|
[14de349] | 224 | "Units", |
---|
| 225 | "Default value", |
---|
| 226 | ] |
---|
| 227 | |
---|
[ff7119b] | 228 | # Minimum width for a default value (this is shorter than the column header |
---|
| 229 | # width, so will be ignored). |
---|
[14de349] | 230 | PARTABLE_VALUE_WIDTH = 10 |
---|
| 231 | |
---|
[a7684e5] | 232 | # Documentation header for the module, giving the model name, its short |
---|
| 233 | # description and its parameter table. The remainder of the doc comes |
---|
| 234 | # from the module docstring. |
---|
[33e91b1] | 235 | DOC_HEADER = """.. _%(name)s: |
---|
[a7684e5] | 236 | |
---|
[19dcb933] | 237 | %(label)s |
---|
[a7684e5] | 238 | ======================================================= |
---|
| 239 | |
---|
| 240 | %(title)s |
---|
| 241 | |
---|
| 242 | %(parameters)s |
---|
| 243 | |
---|
| 244 | The returned value is scaled to units of |cm^-1|. |
---|
| 245 | |
---|
| 246 | %(docs)s |
---|
| 247 | """ |
---|
[ce27e21] | 248 | |
---|
[4eac427] | 249 | def format_units(par): |
---|
| 250 | return RST_UNITS.get(par, par) |
---|
| 251 | |
---|
[19dcb933] | 252 | def make_partable(pars): |
---|
[ff7119b] | 253 | """ |
---|
| 254 | Generate the parameter table to include in the sphinx documentation. |
---|
| 255 | """ |
---|
[19dcb933] | 256 | pars = COMMON_PARAMETERS + pars |
---|
[14de349] | 257 | column_widths = [ |
---|
| 258 | max(len(p[0]) for p in pars), |
---|
[19dcb933] | 259 | max(len(p[-1]) for p in pars), |
---|
[4eac427] | 260 | max(len(format_units(p[1])) for p in pars), |
---|
[14de349] | 261 | PARTABLE_VALUE_WIDTH, |
---|
| 262 | ] |
---|
| 263 | column_widths = [max(w, len(h)) |
---|
[33e91b1] | 264 | for w, h in zip(column_widths, PARTABLE_HEADERS)] |
---|
[14de349] | 265 | |
---|
| 266 | sep = " ".join("="*w for w in column_widths) |
---|
| 267 | lines = [ |
---|
| 268 | sep, |
---|
[33e91b1] | 269 | " ".join("%-*s" % (w, h) for w, h in zip(column_widths, PARTABLE_HEADERS)), |
---|
[14de349] | 270 | sep, |
---|
| 271 | ] |
---|
| 272 | for p in pars: |
---|
| 273 | lines.append(" ".join([ |
---|
[33e91b1] | 274 | "%-*s" % (column_widths[0], p[0]), |
---|
| 275 | "%-*s" % (column_widths[1], p[-1]), |
---|
[4eac427] | 276 | "%-*s" % (column_widths[2], format_units(p[1])), |
---|
[33e91b1] | 277 | "%*g" % (column_widths[3], p[2]), |
---|
[14de349] | 278 | ])) |
---|
| 279 | lines.append(sep) |
---|
| 280 | return "\n".join(lines) |
---|
| 281 | |
---|
[32c160a] | 282 | def _search(search_path, filename): |
---|
| 283 | """ |
---|
| 284 | Find *filename* in *search_path*. |
---|
| 285 | |
---|
| 286 | Raises ValueError if file does not exist. |
---|
| 287 | """ |
---|
| 288 | for path in search_path: |
---|
[f734e7d] | 289 | target = joinpath(path, filename) |
---|
| 290 | if exists(target): |
---|
[32c160a] | 291 | return target |
---|
[33e91b1] | 292 | raise ValueError("%r not found in %s" % (filename, search_path)) |
---|
[14de349] | 293 | |
---|
[ff7119b] | 294 | def sources(info): |
---|
| 295 | """ |
---|
| 296 | Return a list of the sources file paths for the module. |
---|
| 297 | """ |
---|
[33e91b1] | 298 | search_path = [dirname(info['filename']), |
---|
| 299 | abspath(joinpath(dirname(__file__), 'models'))] |
---|
[5d4777d] | 300 | return [_search(search_path, f) for f in info['source']] |
---|
[ff7119b] | 301 | |
---|
[f734e7d] | 302 | def use_single(source): |
---|
[ff7119b] | 303 | """ |
---|
[f734e7d] | 304 | Convert code from double precision to single precision. |
---|
[ff7119b] | 305 | """ |
---|
[f734e7d] | 306 | # Convert double keyword to float. Accept an 'n' parameter for vector |
---|
| 307 | # values, where n is 2, 4, 8 or 16. Assume complex numbers are represented |
---|
| 308 | # as cdouble which is typedef'd to double2. |
---|
| 309 | source = re.sub(r'(^|[^a-zA-Z0-9_]c?)double(([248]|16)?($|[^a-zA-Z0-9_]))', |
---|
| 310 | r'\1float\2', source) |
---|
| 311 | # Convert floating point constants to single by adding 'f' to the end. |
---|
| 312 | # OS/X driver complains if you don't do this. |
---|
| 313 | source = re.sub(r'[^a-zA-Z_](\d*[.]\d+|\d+[.]\d*)([eE][+-]?\d+)?', |
---|
| 314 | r'\g<0>f', source) |
---|
| 315 | return source |
---|
| 316 | |
---|
| 317 | |
---|
| 318 | def kernel_name(info, is_2D): |
---|
| 319 | """ |
---|
| 320 | Name of the exported kernel symbol. |
---|
| 321 | """ |
---|
| 322 | return info['name'] + "_" + ("Iqxy" if is_2D else "Iq") |
---|
| 323 | |
---|
[14de349] | 324 | |
---|
[ce27e21] | 325 | def categorize_parameters(pars): |
---|
[14de349] | 326 | """ |
---|
[ce27e21] | 327 | Build parameter categories out of the the parameter definitions. |
---|
| 328 | |
---|
| 329 | Returns a dictionary of categories. |
---|
[14de349] | 330 | """ |
---|
[ce27e21] | 331 | partype = { |
---|
| 332 | 'volume': [], 'orientation': [], 'magnetic': [], '': [], |
---|
| 333 | 'fixed-1d': [], 'fixed-2d': [], 'pd-1d': [], 'pd-2d': [], |
---|
| 334 | 'pd-rel': set(), |
---|
[14de349] | 335 | } |
---|
| 336 | |
---|
[ce27e21] | 337 | for p in pars: |
---|
[33e91b1] | 338 | name, ptype = p[0], p[4] |
---|
[ce27e21] | 339 | if ptype == 'volume': |
---|
| 340 | partype['pd-1d'].append(name) |
---|
| 341 | partype['pd-2d'].append(name) |
---|
| 342 | partype['pd-rel'].add(name) |
---|
| 343 | elif ptype == 'magnetic': |
---|
| 344 | partype['fixed-2d'].append(name) |
---|
| 345 | elif ptype == 'orientation': |
---|
| 346 | partype['pd-2d'].append(name) |
---|
| 347 | elif ptype == '': |
---|
| 348 | partype['fixed-1d'].append(name) |
---|
| 349 | partype['fixed-2d'].append(name) |
---|
| 350 | else: |
---|
[33e91b1] | 351 | raise ValueError("unknown parameter type %r" % ptype) |
---|
[ce27e21] | 352 | partype[ptype].append(name) |
---|
[14de349] | 353 | |
---|
[ce27e21] | 354 | return partype |
---|
[14de349] | 355 | |
---|
[f734e7d] | 356 | def indent(s, depth): |
---|
| 357 | """ |
---|
| 358 | Indent a string of text with *depth* additional spaces on each line. |
---|
| 359 | """ |
---|
| 360 | spaces = " "*depth |
---|
[33e91b1] | 361 | sep = "\n" + spaces |
---|
[f734e7d] | 362 | return spaces + sep.join(s.split("\n")) |
---|
| 363 | |
---|
| 364 | |
---|
| 365 | def build_polydispersity_loops(pd_pars): |
---|
| 366 | """ |
---|
| 367 | Build polydispersity loops |
---|
| 368 | |
---|
| 369 | Returns loop opening and loop closing |
---|
| 370 | """ |
---|
[33e91b1] | 371 | LOOP_OPEN = """\ |
---|
[f734e7d] | 372 | for (int %(name)s_i=0; %(name)s_i < N%(name)s; %(name)s_i++) { |
---|
| 373 | const double %(name)s = loops[2*(%(name)s_i%(offset)s)]; |
---|
| 374 | const double %(name)s_w = loops[2*(%(name)s_i%(offset)s)+1];\ |
---|
| 375 | """ |
---|
| 376 | depth = 4 |
---|
| 377 | offset = "" |
---|
| 378 | loop_head = [] |
---|
| 379 | loop_end = [] |
---|
| 380 | for name in pd_pars: |
---|
[33e91b1] | 381 | subst = {'name': name, 'offset': offset} |
---|
| 382 | loop_head.append(indent(LOOP_OPEN % subst, depth)) |
---|
[f734e7d] | 383 | loop_end.insert(0, (" "*depth) + "}") |
---|
[33e91b1] | 384 | offset += '+N' + name |
---|
[f734e7d] | 385 | depth += 2 |
---|
| 386 | return "\n".join(loop_head), "\n".join(loop_end) |
---|
| 387 | |
---|
[33e91b1] | 388 | C_KERNEL_TEMPLATE = None |
---|
[f734e7d] | 389 | def make_model(info): |
---|
| 390 | """ |
---|
| 391 | Generate the code for the kernel defined by info, using source files |
---|
| 392 | found in the given search path. |
---|
| 393 | """ |
---|
| 394 | # TODO: need something other than volume to indicate dispersion parameters |
---|
| 395 | # No volume normalization despite having a volume parameter. |
---|
| 396 | # Thickness is labelled a volume in order to trigger polydispersity. |
---|
| 397 | # May want a separate dispersion flag, or perhaps a separate category for |
---|
| 398 | # disperse, but not volume. Volume parameters also use relative values |
---|
| 399 | # for the distribution rather than the absolute values used by angular |
---|
| 400 | # dispersion. Need to be careful that necessary parameters are available |
---|
| 401 | # for computing volume even if we allow non-disperse volume parameters. |
---|
| 402 | |
---|
| 403 | # Load template |
---|
| 404 | global C_KERNEL_TEMPLATE |
---|
| 405 | if C_KERNEL_TEMPLATE is None: |
---|
| 406 | with open(C_KERNEL_TEMPLATE_PATH) as fid: |
---|
| 407 | C_KERNEL_TEMPLATE = fid.read() |
---|
| 408 | |
---|
| 409 | # Load additional sources |
---|
| 410 | source = [open(f).read() for f in sources(info)] |
---|
| 411 | |
---|
| 412 | # Prepare defines |
---|
| 413 | defines = [] |
---|
| 414 | partype = info['partype'] |
---|
| 415 | pd_1d = partype['pd-1d'] |
---|
| 416 | pd_2d = partype['pd-2d'] |
---|
| 417 | fixed_1d = partype['fixed-1d'] |
---|
| 418 | fixed_2d = partype['fixed-1d'] |
---|
| 419 | |
---|
| 420 | iq_parameters = [p[0] |
---|
[33e91b1] | 421 | for p in info['parameters'][2:] # skip scale, background |
---|
| 422 | if p[0] in set(fixed_1d + pd_1d)] |
---|
[f734e7d] | 423 | iqxy_parameters = [p[0] |
---|
[33e91b1] | 424 | for p in info['parameters'][2:] # skip scale, background |
---|
| 425 | if p[0] in set(fixed_2d + pd_2d)] |
---|
[f734e7d] | 426 | volume_parameters = [p[0] |
---|
[33e91b1] | 427 | for p in info['parameters'] |
---|
| 428 | if p[4] == 'volume'] |
---|
[f734e7d] | 429 | |
---|
| 430 | # Fill in defintions for volume parameters |
---|
| 431 | if volume_parameters: |
---|
| 432 | defines.append(('VOLUME_PARAMETERS', |
---|
| 433 | ','.join(volume_parameters))) |
---|
| 434 | defines.append(('VOLUME_WEIGHT_PRODUCT', |
---|
[33e91b1] | 435 | '*'.join(p + '_w' for p in volume_parameters))) |
---|
[f734e7d] | 436 | |
---|
| 437 | # Generate form_volume function from body only |
---|
| 438 | if info['form_volume'] is not None: |
---|
[6137124] | 439 | if volume_parameters: |
---|
[33e91b1] | 440 | vol_par_decl = ', '.join('double ' + p for p in volume_parameters) |
---|
[6137124] | 441 | else: |
---|
| 442 | vol_par_decl = 'void' |
---|
[f734e7d] | 443 | defines.append(('VOLUME_PARAMETER_DECLARATIONS', |
---|
[6137124] | 444 | vol_par_decl)) |
---|
[f734e7d] | 445 | fn = """\ |
---|
| 446 | double form_volume(VOLUME_PARAMETER_DECLARATIONS); |
---|
| 447 | double form_volume(VOLUME_PARAMETER_DECLARATIONS) { |
---|
| 448 | %(body)s |
---|
| 449 | } |
---|
[33e91b1] | 450 | """ % {'body':info['form_volume']} |
---|
[f734e7d] | 451 | source.append(fn) |
---|
| 452 | |
---|
| 453 | # Fill in definitions for Iq parameters |
---|
[33e91b1] | 454 | defines.append(('IQ_KERNEL_NAME', info['name'] + '_Iq')) |
---|
[f734e7d] | 455 | defines.append(('IQ_PARAMETERS', ', '.join(iq_parameters))) |
---|
| 456 | if fixed_1d: |
---|
| 457 | defines.append(('IQ_FIXED_PARAMETER_DECLARATIONS', |
---|
[33e91b1] | 458 | ', \\\n '.join('const double %s' % p for p in fixed_1d))) |
---|
[f734e7d] | 459 | if pd_1d: |
---|
| 460 | defines.append(('IQ_WEIGHT_PRODUCT', |
---|
[33e91b1] | 461 | '*'.join(p + '_w' for p in pd_1d))) |
---|
[f734e7d] | 462 | defines.append(('IQ_DISPERSION_LENGTH_DECLARATIONS', |
---|
[33e91b1] | 463 | ', \\\n '.join('const int N%s' % p for p in pd_1d))) |
---|
[f734e7d] | 464 | defines.append(('IQ_DISPERSION_LENGTH_SUM', |
---|
[33e91b1] | 465 | '+'.join('N' + p for p in pd_1d))) |
---|
[f734e7d] | 466 | open_loops, close_loops = build_polydispersity_loops(pd_1d) |
---|
| 467 | defines.append(('IQ_OPEN_LOOPS', |
---|
[33e91b1] | 468 | open_loops.replace('\n', ' \\\n'))) |
---|
[f734e7d] | 469 | defines.append(('IQ_CLOSE_LOOPS', |
---|
[33e91b1] | 470 | close_loops.replace('\n', ' \\\n'))) |
---|
[f734e7d] | 471 | if info['Iq'] is not None: |
---|
| 472 | defines.append(('IQ_PARAMETER_DECLARATIONS', |
---|
[33e91b1] | 473 | ', '.join('double ' + p for p in iq_parameters))) |
---|
[f734e7d] | 474 | fn = """\ |
---|
| 475 | double Iq(double q, IQ_PARAMETER_DECLARATIONS); |
---|
| 476 | double Iq(double q, IQ_PARAMETER_DECLARATIONS) { |
---|
| 477 | %(body)s |
---|
| 478 | } |
---|
[33e91b1] | 479 | """ % {'body':info['Iq']} |
---|
[f734e7d] | 480 | source.append(fn) |
---|
| 481 | |
---|
| 482 | # Fill in definitions for Iqxy parameters |
---|
[33e91b1] | 483 | defines.append(('IQXY_KERNEL_NAME', info['name'] + '_Iqxy')) |
---|
[f734e7d] | 484 | defines.append(('IQXY_PARAMETERS', ', '.join(iqxy_parameters))) |
---|
| 485 | if fixed_2d: |
---|
| 486 | defines.append(('IQXY_FIXED_PARAMETER_DECLARATIONS', |
---|
[33e91b1] | 487 | ', \\\n '.join('const double %s' % p for p in fixed_2d))) |
---|
[f734e7d] | 488 | if pd_2d: |
---|
| 489 | defines.append(('IQXY_WEIGHT_PRODUCT', |
---|
[33e91b1] | 490 | '*'.join(p + '_w' for p in pd_2d))) |
---|
[f734e7d] | 491 | defines.append(('IQXY_DISPERSION_LENGTH_DECLARATIONS', |
---|
[33e91b1] | 492 | ', \\\n '.join('const int N%s' % p for p in pd_2d))) |
---|
[f734e7d] | 493 | defines.append(('IQXY_DISPERSION_LENGTH_SUM', |
---|
[33e91b1] | 494 | '+'.join('N' + p for p in pd_2d))) |
---|
[f734e7d] | 495 | open_loops, close_loops = build_polydispersity_loops(pd_2d) |
---|
| 496 | defines.append(('IQXY_OPEN_LOOPS', |
---|
[33e91b1] | 497 | open_loops.replace('\n', ' \\\n'))) |
---|
[f734e7d] | 498 | defines.append(('IQXY_CLOSE_LOOPS', |
---|
[33e91b1] | 499 | close_loops.replace('\n', ' \\\n'))) |
---|
[f734e7d] | 500 | if info['Iqxy'] is not None: |
---|
| 501 | defines.append(('IQXY_PARAMETER_DECLARATIONS', |
---|
[33e91b1] | 502 | ', '.join('double ' + p for p in iqxy_parameters))) |
---|
[f734e7d] | 503 | fn = """\ |
---|
| 504 | double Iqxy(double qx, double qy, IQXY_PARAMETER_DECLARATIONS); |
---|
| 505 | double Iqxy(double qx, double qy, IQXY_PARAMETER_DECLARATIONS) { |
---|
| 506 | %(body)s |
---|
| 507 | } |
---|
[33e91b1] | 508 | """ % {'body':info['Iqxy']} |
---|
[f734e7d] | 509 | source.append(fn) |
---|
| 510 | |
---|
| 511 | # Need to know if we have a theta parameter for Iqxy; it is not there |
---|
| 512 | # for the magnetic sphere model, for example, which has a magnetic |
---|
| 513 | # orientation but no shape orientation. |
---|
| 514 | if 'theta' in pd_2d: |
---|
| 515 | defines.append(('IQXY_HAS_THETA', '1')) |
---|
| 516 | |
---|
| 517 | #for d in defines: print d |
---|
[33e91b1] | 518 | DEFINES = '\n'.join('#define %s %s' % (k, v) for k, v in defines) |
---|
| 519 | SOURCES = '\n\n'.join(source) |
---|
| 520 | return C_KERNEL_TEMPLATE % { |
---|
[f734e7d] | 521 | 'DEFINES':DEFINES, |
---|
| 522 | 'SOURCES':SOURCES, |
---|
| 523 | } |
---|
| 524 | |
---|
[32c160a] | 525 | def make(kernel_module): |
---|
[14de349] | 526 | """ |
---|
[a7684e5] | 527 | Build an OpenCL/ctypes function from the definition in *kernel_module*. |
---|
[14de349] | 528 | |
---|
[a7684e5] | 529 | The module can be loaded with a normal python import statement if you |
---|
| 530 | know which module you need, or with __import__('sasmodels.model.'+name) |
---|
| 531 | if the name is in a string. |
---|
[14de349] | 532 | """ |
---|
[32c160a] | 533 | # TODO: allow Iq and Iqxy to be defined in python |
---|
[ce27e21] | 534 | #print kernelfile |
---|
[32c160a] | 535 | info = dict( |
---|
[33e91b1] | 536 | filename=abspath(kernel_module.__file__), |
---|
| 537 | name=kernel_module.name, |
---|
| 538 | title=kernel_module.title, |
---|
| 539 | description=kernel_module.description, |
---|
| 540 | parameters=COMMON_PARAMETERS + kernel_module.parameters, |
---|
| 541 | source=getattr(kernel_module, 'source', []), |
---|
| 542 | oldname=kernel_module.oldname, |
---|
| 543 | oldpars=kernel_module.oldpars, |
---|
[32c160a] | 544 | ) |
---|
[5d4777d] | 545 | # Fill in attributes which default to None |
---|
[33e91b1] | 546 | info.update((k, getattr(kernel_module, k, None)) |
---|
[5d4777d] | 547 | for k in ('ER', 'VR', 'form_volume', 'Iq', 'Iqxy')) |
---|
| 548 | # Fill in the derived attributes |
---|
[33e91b1] | 549 | info['limits'] = dict((p[0], p[3]) for p in info['parameters']) |
---|
[32c160a] | 550 | info['partype'] = categorize_parameters(info['parameters']) |
---|
[33e91b1] | 551 | info['defaults'] = dict((p[0], p[2]) for p in info['parameters']) |
---|
[32c160a] | 552 | |
---|
[f734e7d] | 553 | # Assume if one part of the kernel is python then all parts are. |
---|
| 554 | source = make_model(info) if not callable(info['Iq']) else None |
---|
[32c160a] | 555 | return source, info |
---|
[14de349] | 556 | |
---|
[a7684e5] | 557 | def doc(kernel_module): |
---|
| 558 | """ |
---|
| 559 | Return the documentation for the model. |
---|
| 560 | """ |
---|
[33e91b1] | 561 | subst = dict(name=kernel_module.name.replace('_', '-'), |
---|
[19dcb933] | 562 | label=" ".join(kernel_module.name.split('_')).capitalize(), |
---|
[a7684e5] | 563 | title=kernel_module.title, |
---|
| 564 | parameters=make_partable(kernel_module.parameters), |
---|
[19dcb933] | 565 | docs=kernel_module.__doc__) |
---|
[33e91b1] | 566 | return DOC_HEADER % subst |
---|
[a7684e5] | 567 | |
---|
[ff7119b] | 568 | |
---|
[14de349] | 569 | |
---|
| 570 | def demo_time(): |
---|
[b3f6bc3] | 571 | from .models import cylinder |
---|
[3c56da87] | 572 | import datetime |
---|
[b3f6bc3] | 573 | tic = datetime.datetime.now() |
---|
[33e91b1] | 574 | make(cylinder) |
---|
[3c56da87] | 575 | toc = (datetime.datetime.now() - tic).total_seconds() |
---|
| 576 | print "time:", toc |
---|
[14de349] | 577 | |
---|
[ff58110] | 578 | def main(): |
---|
| 579 | if len(sys.argv) <= 1: |
---|
| 580 | print "usage: python -m sasmodels.generate modelname" |
---|
| 581 | else: |
---|
| 582 | name = sys.argv[1] |
---|
| 583 | import sasmodels.models |
---|
[33e91b1] | 584 | __import__('sasmodels.models.' + name) |
---|
[ff58110] | 585 | model = getattr(sasmodels.models, name) |
---|
[33e91b1] | 586 | source, _ = make(model) |
---|
| 587 | print source |
---|
[14de349] | 588 | |
---|
| 589 | if __name__ == "__main__": |
---|
[ff58110] | 590 | main() |
---|