[5ceb7d0] | 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 | |
---|
[108e70e] | 9 | *Iqac(qab, qc, p1, p2, ...)* returns the scattering at qab, qc |
---|
| 10 | for a rotationally symmetric form with particular dimensions. |
---|
| 11 | qab, qc are determined from shape orientation and scattering angles. |
---|
| 12 | This call is used if the shape has orientation parameters theta and phi. |
---|
[5ceb7d0] | 13 | |
---|
[108e70e] | 14 | *Iqabc(qa, qb, qc, p1, p2, ...)* returns the scattering at qa, qb, qc |
---|
| 15 | for a form with particular dimensions. qa, qb, qc are determined from |
---|
| 16 | shape orientation and scattering angles. This call is used if the shape |
---|
| 17 | has orientation parameters theta, phi and psi. |
---|
| 18 | |
---|
| 19 | *Iqxy(qx, qy, p1, p2, ...)* returns the scattering at qx, qy. Use this |
---|
| 20 | to create an arbitrary 2D theory function, needed for q-dependent |
---|
| 21 | background functions and for models with non-uniform magnetism. |
---|
[5ceb7d0] | 22 | |
---|
| 23 | *form_volume(p1, p2, ...)* returns the volume of the form with particular |
---|
[e6408d0] | 24 | dimension, or 1.0 if no volume normalization is required. |
---|
[5ceb7d0] | 25 | |
---|
[39a06c9] | 26 | *shell_volume(p1, p2, ...)* returns the volume of the shell for forms |
---|
| 27 | which are hollow. |
---|
[5ceb7d0] | 28 | |
---|
[39a06c9] | 29 | *effective_radius(mode, p1, p2, ...)* returns the effective radius of |
---|
| 30 | the form with particular dimensions. Mode determines the type of |
---|
| 31 | effective radius returned, with mode=1 for equivalent volume. |
---|
[5ceb7d0] | 32 | |
---|
[d5ac45f] | 33 | #define INVALID(v) (expr) returns False if v.parameter is invalid |
---|
| 34 | for some parameter or other (e.g., v.bell_radius < v.radius). If |
---|
| 35 | necessary, the expression can call a function. |
---|
| 36 | |
---|
[5ceb7d0] | 37 | These functions are defined in a kernel module .py script and an associated |
---|
| 38 | set of .c files. The model constructor will use them to create models with |
---|
| 39 | polydispersity across volume and orientation parameters, and provide |
---|
| 40 | scale and background parameters for each model. |
---|
| 41 | |
---|
[108e70e] | 42 | C code should be stylized C-99 functions written for OpenCL. All functions |
---|
| 43 | need prototype declarations even if the are defined before they are used. |
---|
| 44 | Although OpenCL supports *#include* preprocessor directives, the list of |
---|
| 45 | includes should be given as part of the metadata in the kernel module |
---|
| 46 | definition. The included files should be listed using a path relative to the |
---|
| 47 | kernel module, or if using "lib/file.c" if it is one of the standard includes |
---|
| 48 | provided with the sasmodels source. The includes need to be listed in order |
---|
| 49 | so that functions are defined before they are used. |
---|
[5ceb7d0] | 50 | |
---|
| 51 | Floating point values should be declared as *double*. For single precision |
---|
| 52 | calculations, *double* will be replaced by *float*. The single precision |
---|
| 53 | conversion will also tag floating point constants with "f" to make them |
---|
| 54 | single precision constants. When using integral values in floating point |
---|
| 55 | expressions, they should be expressed as floating point values by including |
---|
| 56 | a decimal point. This includes 0., 1. and 2. |
---|
| 57 | |
---|
| 58 | OpenCL has a *sincos* function which can improve performance when both |
---|
| 59 | the *sin* and *cos* values are needed for a particular argument. Since |
---|
| 60 | this function does not exist in C99, all use of *sincos* should be |
---|
| 61 | replaced by the macro *SINCOS(value, sn, cn)* where *sn* and *cn* are |
---|
| 62 | previously declared *double* variables. When compiled for systems without |
---|
| 63 | OpenCL, *SINCOS* will be replaced by *sin* and *cos* calls. If *value* is |
---|
| 64 | an expression, it will appear twice in this case; whether or not it will be |
---|
| 65 | evaluated twice depends on the quality of the compiler. |
---|
| 66 | |
---|
| 67 | If the input parameters are invalid, the scattering calculator should |
---|
| 68 | return a negative number. Particularly with polydispersity, there are |
---|
| 69 | some sets of shape parameters which lead to nonsensical forms, such |
---|
| 70 | as a capped cylinder where the cap radius is smaller than the |
---|
| 71 | cylinder radius. The polydispersity calculation will ignore these points, |
---|
| 72 | effectively chopping the parameter weight distributions at the boundary |
---|
| 73 | of the infeasible region. The resulting scattering will be set to |
---|
| 74 | background. This will work correctly even when polydispersity is off. |
---|
| 75 | |
---|
| 76 | The kernel module must set variables defining the kernel meta data: |
---|
| 77 | |
---|
| 78 | *id* is an implicit variable formed from the filename. It will be |
---|
| 79 | a valid python identifier, and will be used as the reference into |
---|
| 80 | the html documentation, with '_' replaced by '-'. |
---|
| 81 | |
---|
| 82 | *name* is the model name as displayed to the user. If it is missing, |
---|
| 83 | it will be constructed from the id. |
---|
| 84 | |
---|
| 85 | *title* is a short description of the model, suitable for a tool tip, |
---|
| 86 | or a one line model summary in a table of models. |
---|
| 87 | |
---|
| 88 | *description* is an extended description of the model to be displayed |
---|
| 89 | while the model parameters are being edited. |
---|
| 90 | |
---|
| 91 | *parameters* is the list of parameters. Parameters in the kernel |
---|
| 92 | functions must appear in the same order as they appear in the |
---|
| 93 | parameters list. Two additional parameters, *scale* and *background* |
---|
| 94 | are added to the beginning of the parameter list. They will show up |
---|
| 95 | in the documentation as model parameters, but they are never sent to |
---|
[17bbadd] | 96 | the kernel functions. Note that *effect_radius* and *volfraction* |
---|
| 97 | must occur first in structure factor calculations. |
---|
[5ceb7d0] | 98 | |
---|
[17bbadd] | 99 | *category* is the default category for the model. The category is |
---|
| 100 | two level structure, with the form "group:section", indicating where |
---|
| 101 | in the manual the model will be located. Models are alphabetical |
---|
| 102 | within their section. |
---|
[5ceb7d0] | 103 | |
---|
| 104 | *source* is the list of C-99 source files that must be joined to |
---|
| 105 | create the OpenCL kernel functions. The files defining the functions |
---|
| 106 | need to be listed before the files which use the functions. |
---|
| 107 | |
---|
[108e70e] | 108 | *form_volume*, *Iq*, *Iqac*, *Iqabc* are strings containing |
---|
| 109 | the C source code for the body of the volume, Iq, and Iqac functions |
---|
[5ceb7d0] | 110 | respectively. These can also be defined in the last source file. |
---|
| 111 | |
---|
[108e70e] | 112 | *Iq*, *Iqac*, *Iqabc* also be instead be python functions defining the |
---|
[5ceb7d0] | 113 | kernel. If they are marked as *Iq.vectorized = True* then the |
---|
| 114 | kernel is passed the entire *q* vector at once, otherwise it is |
---|
| 115 | passed values one *q* at a time. The performance improvement of |
---|
| 116 | this step is significant. |
---|
| 117 | |
---|
| 118 | *demo* is a dictionary of parameter=value defining a set of |
---|
| 119 | parameters to use by default when *compare* is called. Any |
---|
| 120 | parameter not set in *demo* gets the initial value from the |
---|
| 121 | parameter list. *demo* is mostly needed to set the default |
---|
| 122 | polydispersity values for tests. |
---|
| 123 | |
---|
[a5b8477] | 124 | A :class:`modelinfo.ModelInfo` structure is constructed from the kernel meta |
---|
| 125 | data and returned to the caller. |
---|
[5ceb7d0] | 126 | |
---|
| 127 | The doc string at the start of the kernel module will be used to |
---|
| 128 | construct the model documentation web pages. Embedded figures should |
---|
| 129 | appear in the subdirectory "img" beside the model definition, and tagged |
---|
| 130 | with the kernel module name to avoid collision with other models. Some |
---|
| 131 | file systems are case-sensitive, so only use lower case characters for |
---|
| 132 | file names and extensions. |
---|
| 133 | |
---|
| 134 | Code follows the C99 standard with the following extensions and conditions:: |
---|
| 135 | |
---|
| 136 | M_PI_180 = pi/180 |
---|
| 137 | M_4PI_3 = 4pi/3 |
---|
| 138 | square(x) = x*x |
---|
| 139 | cube(x) = x*x*x |
---|
[1e7b0db0] | 140 | sas_sinx_x(x) = sin(x)/x, with sin(0)/0 -> 1 |
---|
[5ceb7d0] | 141 | all double precision constants must include the decimal point |
---|
| 142 | all double declarations may be converted to half, float, or long double |
---|
| 143 | FLOAT_SIZE is the number of bytes in the converted variables |
---|
[a5b8477] | 144 | |
---|
| 145 | :func:`load_kernel_module` loads the model definition file and |
---|
[785cbec] | 146 | :func:`modelinfo.make_model_info` parses it. :func:`make_source` |
---|
[a5b8477] | 147 | converts C-based model definitions to C source code, including the |
---|
| 148 | polydispersity integral. :func:`model_sources` returns the list of |
---|
| 149 | source files the model depends on, and :func:`timestamp` returns |
---|
| 150 | the latest time stamp amongst the source files (so you can check if |
---|
| 151 | the model needs to be rebuilt). |
---|
| 152 | |
---|
| 153 | The function :func:`make_doc` extracts the doc string and adds the |
---|
| 154 | parameter table to the top. *make_figure* in *sasmodels/doc/genmodel* |
---|
| 155 | creates the default figure for the model. [These two sets of code |
---|
| 156 | should mignrate into docs.py so docs can be updated in one place]. |
---|
[5ceb7d0] | 157 | """ |
---|
| 158 | from __future__ import print_function |
---|
| 159 | |
---|
[7891a2a] | 160 | # TODO: determine which functions are useful outside of generate |
---|
[69aa451] | 161 | #__all__ = ["model_info", "make_doc", "make_source", "convert_type"] |
---|
[5ceb7d0] | 162 | |
---|
| 163 | import sys |
---|
[a22d104] | 164 | from os import environ |
---|
| 165 | from os.path import abspath, dirname, join as joinpath, exists, getmtime, sep |
---|
[5ceb7d0] | 166 | import re |
---|
| 167 | import string |
---|
[7fcdc9f] | 168 | from zlib import crc32 |
---|
[9ee2756] | 169 | from inspect import currentframe, getframeinfo |
---|
[108e70e] | 170 | import logging |
---|
[5ceb7d0] | 171 | |
---|
[7ae2b7f] | 172 | import numpy as np # type: ignore |
---|
[5ceb7d0] | 173 | |
---|
[6d6508e] | 174 | from .modelinfo import Parameter |
---|
[68e7f9d] | 175 | from .custom import load_custom_kernel_module |
---|
[fcd7bbd] | 176 | |
---|
[2d81cfe] | 177 | # pylint: disable=unused-import |
---|
[f619de7] | 178 | try: |
---|
[f2f67a6] | 179 | from typing import Tuple, Sequence, Iterator, Dict |
---|
[f619de7] | 180 | from .modelinfo import ModelInfo |
---|
| 181 | except ImportError: |
---|
| 182 | pass |
---|
[2d81cfe] | 183 | # pylint: enable=unused-import |
---|
[f619de7] | 184 | |
---|
[108e70e] | 185 | logger = logging.getLogger(__name__) |
---|
| 186 | |
---|
[4991048] | 187 | # jitter projection to use in the kernel code. See explore/jitter.py |
---|
| 188 | # for details. To change it from a program, set generate.PROJECTION. |
---|
| 189 | PROJECTION = 1 |
---|
| 190 | |
---|
[a5da1f2] | 191 | def get_data_path(external_dir, target_file): |
---|
| 192 | path = abspath(dirname(__file__)) |
---|
| 193 | if exists(joinpath(path, target_file)): |
---|
| 194 | return path |
---|
| 195 | |
---|
| 196 | # check next to exe/zip file |
---|
| 197 | exepath = dirname(sys.executable) |
---|
| 198 | path = joinpath(exepath, external_dir) |
---|
| 199 | if exists(joinpath(path, target_file)): |
---|
| 200 | return path |
---|
| 201 | |
---|
| 202 | # check in py2app Contents/Resources |
---|
| 203 | path = joinpath(exepath, '..', 'Resources', external_dir) |
---|
| 204 | if exists(joinpath(path, target_file)): |
---|
| 205 | return abspath(path) |
---|
| 206 | |
---|
| 207 | raise RuntimeError('Could not find '+joinpath(external_dir, target_file)) |
---|
| 208 | |
---|
| 209 | EXTERNAL_DIR = 'sasmodels-data' |
---|
[6cbdcd4] | 210 | DATA_PATH = get_data_path(EXTERNAL_DIR, 'kernel_iq.c') |
---|
[c2c51a2] | 211 | MODEL_PATH = joinpath(DATA_PATH, 'models') |
---|
[5ceb7d0] | 212 | |
---|
| 213 | F16 = np.dtype('float16') |
---|
| 214 | F32 = np.dtype('float32') |
---|
| 215 | F64 = np.dtype('float64') |
---|
| 216 | try: # CRUFT: older numpy does not support float128 |
---|
| 217 | F128 = np.dtype('float128') |
---|
| 218 | except TypeError: |
---|
| 219 | F128 = None |
---|
| 220 | |
---|
| 221 | # Conversion from units defined in the parameter table for each model |
---|
[30b60d2] | 222 | # to units displayed in the sphinx documentation. |
---|
[573ffab] | 223 | # This section associates the unit with the macro to use to produce the LaTex |
---|
| 224 | # code. The macro itself needs to be defined in sasmodels/doc/rst_prolog. |
---|
| 225 | # |
---|
| 226 | # NOTE: there is an RST_PROLOG at the end of this file which is NOT |
---|
| 227 | # used for the bundled documentation. Still as long as we are defining the macros |
---|
[30b60d2] | 228 | # in two places any new addition should define the macro in both places. |
---|
[5ceb7d0] | 229 | RST_UNITS = { |
---|
| 230 | "Ang": "|Ang|", |
---|
| 231 | "1/Ang": "|Ang^-1|", |
---|
| 232 | "1/Ang^2": "|Ang^-2|", |
---|
[462a115] | 233 | "Ang^3": "|Ang^3|", |
---|
[573ffab] | 234 | "Ang^2": "|Ang^2|", |
---|
[462a115] | 235 | "1e15/cm^3": "|1e15cm^3|", |
---|
| 236 | "Ang^3/mol": "|Ang^3|/mol", |
---|
[5ceb7d0] | 237 | "1e-6/Ang^2": "|1e-6Ang^-2|", |
---|
| 238 | "degrees": "degree", |
---|
| 239 | "1/cm": "|cm^-1|", |
---|
| 240 | "Ang/cm": "|Ang*cm^-1|", |
---|
[52ec91e] | 241 | "g/cm^3": "|g/cm^3|", |
---|
| 242 | "mg/m^2": "|mg/m^2|", |
---|
[5ceb7d0] | 243 | "": "None", |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | # Headers for the parameters tables in th sphinx documentation |
---|
| 247 | PARTABLE_HEADERS = [ |
---|
| 248 | "Parameter", |
---|
| 249 | "Description", |
---|
| 250 | "Units", |
---|
| 251 | "Default value", |
---|
| 252 | ] |
---|
| 253 | |
---|
| 254 | # Minimum width for a default value (this is shorter than the column header |
---|
| 255 | # width, so will be ignored). |
---|
| 256 | PARTABLE_VALUE_WIDTH = 10 |
---|
| 257 | |
---|
| 258 | # Documentation header for the module, giving the model name, its short |
---|
| 259 | # description and its parameter table. The remainder of the doc comes |
---|
| 260 | # from the module docstring. |
---|
| 261 | DOC_HEADER = """.. _%(id)s: |
---|
| 262 | |
---|
| 263 | %(name)s |
---|
| 264 | ======================================================= |
---|
| 265 | |
---|
| 266 | %(title)s |
---|
| 267 | |
---|
| 268 | %(parameters)s |
---|
| 269 | |
---|
| 270 | %(returns)s |
---|
| 271 | |
---|
| 272 | %(docs)s |
---|
| 273 | """ |
---|
| 274 | |
---|
[7891a2a] | 275 | |
---|
[ff31782] | 276 | def set_integration_size(info, n): |
---|
| 277 | # type: (ModelInfo, int) -> None |
---|
| 278 | """ |
---|
| 279 | Update the model definition, replacing the gaussian integration with |
---|
| 280 | a gaussian integration of a different size. |
---|
| 281 | |
---|
| 282 | Note: this really ought to be a method in modelinfo, but that leads to |
---|
| 283 | import loops. |
---|
| 284 | """ |
---|
[d86f0fc] | 285 | if info.source and any(lib.startswith('lib/gauss') for lib in info.source): |
---|
[ff31782] | 286 | from .gengauss import gengauss |
---|
[d86f0fc] | 287 | path = joinpath(MODEL_PATH, "lib", "gauss%d.c"%n) |
---|
| 288 | if not exists(path): |
---|
[ff31782] | 289 | gengauss(n, path) |
---|
| 290 | info.source = ["lib/gauss%d.c"%n if lib.startswith('lib/gauss') |
---|
[d86f0fc] | 291 | else lib for lib in info.source] |
---|
[ff31782] | 292 | |
---|
[5ceb7d0] | 293 | def format_units(units): |
---|
[f619de7] | 294 | # type: (str) -> str |
---|
[5ceb7d0] | 295 | """ |
---|
| 296 | Convert units into ReStructured Text format. |
---|
| 297 | """ |
---|
| 298 | return "string" if isinstance(units, list) else RST_UNITS.get(units, units) |
---|
| 299 | |
---|
[7891a2a] | 300 | |
---|
[5ceb7d0] | 301 | def make_partable(pars): |
---|
[f619de7] | 302 | # type: (List[Parameter]) -> str |
---|
[5ceb7d0] | 303 | """ |
---|
| 304 | Generate the parameter table to include in the sphinx documentation. |
---|
| 305 | """ |
---|
| 306 | column_widths = [ |
---|
[fcd7bbd] | 307 | max(len(p.name) for p in pars), |
---|
| 308 | max(len(p.description) for p in pars), |
---|
| 309 | max(len(format_units(p.units)) for p in pars), |
---|
[5ceb7d0] | 310 | PARTABLE_VALUE_WIDTH, |
---|
| 311 | ] |
---|
| 312 | column_widths = [max(w, len(h)) |
---|
| 313 | for w, h in zip(column_widths, PARTABLE_HEADERS)] |
---|
| 314 | |
---|
[d86f0fc] | 315 | underbar = " ".join("="*w for w in column_widths) |
---|
[5ceb7d0] | 316 | lines = [ |
---|
[d86f0fc] | 317 | underbar, |
---|
[5ceb7d0] | 318 | " ".join("%-*s" % (w, h) |
---|
| 319 | for w, h in zip(column_widths, PARTABLE_HEADERS)), |
---|
[d86f0fc] | 320 | underbar, |
---|
[5ceb7d0] | 321 | ] |
---|
| 322 | for p in pars: |
---|
| 323 | lines.append(" ".join([ |
---|
[fcd7bbd] | 324 | "%-*s" % (column_widths[0], p.name), |
---|
| 325 | "%-*s" % (column_widths[1], p.description), |
---|
| 326 | "%-*s" % (column_widths[2], format_units(p.units)), |
---|
| 327 | "%*g" % (column_widths[3], p.default), |
---|
[5ceb7d0] | 328 | ])) |
---|
[d86f0fc] | 329 | lines.append(underbar) |
---|
[5ceb7d0] | 330 | return "\n".join(lines) |
---|
| 331 | |
---|
[7891a2a] | 332 | |
---|
[5ceb7d0] | 333 | def _search(search_path, filename): |
---|
[f619de7] | 334 | # type: (List[str], str) -> str |
---|
[5ceb7d0] | 335 | """ |
---|
| 336 | Find *filename* in *search_path*. |
---|
| 337 | |
---|
| 338 | Raises ValueError if file does not exist. |
---|
| 339 | """ |
---|
| 340 | for path in search_path: |
---|
| 341 | target = joinpath(path, filename) |
---|
| 342 | if exists(target): |
---|
| 343 | return target |
---|
| 344 | raise ValueError("%r not found in %s" % (filename, search_path)) |
---|
| 345 | |
---|
[d5ac45f] | 346 | |
---|
[17bbadd] | 347 | def model_sources(model_info): |
---|
[f619de7] | 348 | # type: (ModelInfo) -> List[str] |
---|
[5ceb7d0] | 349 | """ |
---|
| 350 | Return a list of the sources file paths for the module. |
---|
| 351 | """ |
---|
[7891a2a] | 352 | search_path = [dirname(model_info.filename), MODEL_PATH] |
---|
[6d6508e] | 353 | return [_search(search_path, f) for f in model_info.source] |
---|
[5ceb7d0] | 354 | |
---|
| 355 | |
---|
[0dc34c3] | 356 | def dll_timestamp(model_info): |
---|
[f619de7] | 357 | # type: (ModelInfo) -> int |
---|
[69aa451] | 358 | """ |
---|
| 359 | Return a timestamp for the model corresponding to the most recently |
---|
| 360 | changed file or dependency. |
---|
| 361 | """ |
---|
[0dc34c3] | 362 | # TODO: fails DRY; templates appear two places. |
---|
| 363 | model_templates = [joinpath(DATA_PATH, filename) |
---|
| 364 | for filename in ('kernel_header.c', 'kernel_iq.c')] |
---|
[69aa451] | 365 | source_files = (model_sources(model_info) |
---|
[0dc34c3] | 366 | + model_templates |
---|
[6d6508e] | 367 | + [model_info.filename]) |
---|
[0dc34c3] | 368 | # Note: file may not exist when it is a standard model from library.zip |
---|
| 369 | times = [getmtime(f) for f in source_files if exists(f)] |
---|
| 370 | newest = max(times) if times else 0 |
---|
[69aa451] | 371 | return newest |
---|
| 372 | |
---|
[0dc34c3] | 373 | def ocl_timestamp(model_info): |
---|
| 374 | # type: (ModelInfo) -> int |
---|
| 375 | """ |
---|
| 376 | Return a timestamp for the model corresponding to the most recently |
---|
| 377 | changed file or dependency. |
---|
[7891a2a] | 378 | |
---|
[0dc34c3] | 379 | Note that this does not look at the time stamps for the OpenCL header |
---|
| 380 | information since that need not trigger a recompile of the DLL. |
---|
| 381 | """ |
---|
[f2f67a6] | 382 | # TODO: fails DRY; templates appear two places. |
---|
[0dc34c3] | 383 | model_templates = [joinpath(DATA_PATH, filename) |
---|
[6cbdcd4] | 384 | for filename in ('kernel_header.c', 'kernel_iq.c')] |
---|
[0dc34c3] | 385 | source_files = (model_sources(model_info) |
---|
| 386 | + model_templates |
---|
| 387 | + [model_info.filename]) |
---|
| 388 | # Note: file may not exist when it is a standard model from library.zip |
---|
| 389 | times = [getmtime(f) for f in source_files if exists(f)] |
---|
| 390 | newest = max(times) if times else 0 |
---|
| 391 | return newest |
---|
[f2f67a6] | 392 | |
---|
[7fcdc9f] | 393 | def tag_source(source): |
---|
| 394 | # type: (str) -> str |
---|
| 395 | """ |
---|
| 396 | Return a unique tag for the source code. |
---|
| 397 | """ |
---|
| 398 | # Note: need 0xffffffff&val to force an unsigned 32-bit number |
---|
[6773b02] | 399 | try: |
---|
| 400 | source = source.encode('utf8') |
---|
| 401 | except AttributeError: # bytes has no encode attribute in python 3 |
---|
| 402 | pass |
---|
[7fcdc9f] | 403 | return "%08X"%(0xffffffff&crc32(source)) |
---|
[5ceb7d0] | 404 | |
---|
| 405 | def convert_type(source, dtype): |
---|
[f619de7] | 406 | # type: (str, np.dtype) -> str |
---|
[5ceb7d0] | 407 | """ |
---|
| 408 | Convert code from double precision to the desired type. |
---|
| 409 | |
---|
| 410 | Floating point constants are tagged with 'f' for single precision or 'L' |
---|
| 411 | for long double precision. |
---|
| 412 | """ |
---|
[2e49f9e] | 413 | source = _fix_tgmath_int(source) |
---|
[5ceb7d0] | 414 | if dtype == F16: |
---|
| 415 | fbytes = 2 |
---|
[1e2a1ba] | 416 | source = _convert_type(source, "half", "f") |
---|
[5ceb7d0] | 417 | elif dtype == F32: |
---|
| 418 | fbytes = 4 |
---|
| 419 | source = _convert_type(source, "float", "f") |
---|
| 420 | elif dtype == F64: |
---|
| 421 | fbytes = 8 |
---|
[d5ac45f] | 422 | # no need to convert if it is already double |
---|
[5ceb7d0] | 423 | elif dtype == F128: |
---|
| 424 | fbytes = 16 |
---|
| 425 | source = _convert_type(source, "long double", "L") |
---|
| 426 | else: |
---|
[7891a2a] | 427 | raise ValueError("Unexpected dtype in source conversion: %s" % dtype) |
---|
| 428 | return ("#define FLOAT_SIZE %d\n" % fbytes)+source |
---|
[5ceb7d0] | 429 | |
---|
| 430 | |
---|
| 431 | def _convert_type(source, type_name, constant_flag): |
---|
[f619de7] | 432 | # type: (str, str, str) -> str |
---|
[5ceb7d0] | 433 | """ |
---|
| 434 | Replace 'double' with *type_name* in *source*, tagging floating point |
---|
| 435 | constants with *constant_flag*. |
---|
| 436 | """ |
---|
| 437 | # Convert double keyword to float/long double/half. |
---|
| 438 | # Accept an 'n' # parameter for vector # values, where n is 2, 4, 8 or 16. |
---|
| 439 | # Assume complex numbers are represented as cdouble which is typedef'd |
---|
| 440 | # to double2. |
---|
| 441 | source = re.sub(r'(^|[^a-zA-Z0-9_]c?)double(([248]|16)?($|[^a-zA-Z0-9_]))', |
---|
| 442 | r'\1%s\2'%type_name, source) |
---|
[2e49f9e] | 443 | source = _tag_float(source, constant_flag) |
---|
| 444 | return source |
---|
| 445 | |
---|
| 446 | TGMATH_INT_RE = re.compile(r""" |
---|
| 447 | (?: # Non-capturing match; not lookbehind since pattern length is variable |
---|
| 448 | \b # word boundary |
---|
| 449 | # various math functions |
---|
| 450 | (a?(sin|cos|tan)h? | atan2 |
---|
| 451 | | erfc? | tgamma |
---|
| 452 | | exp(2|10|m1)? | log(2|10|1p)? | pow[nr]? | sqrt | rsqrt | rootn |
---|
| 453 | | fabs | fmax | fmin |
---|
| 454 | ) |
---|
| 455 | \s*[(]\s* # open parenthesis |
---|
| 456 | ) |
---|
| 457 | [+-]?(0|[1-9]\d*) # integer |
---|
| 458 | (?= # lookahead match: don't want to move from end of int |
---|
| 459 | \s*[,)] # comma or close parenthesis for end of argument |
---|
| 460 | ) # end lookahead |
---|
| 461 | """, re.VERBOSE) |
---|
| 462 | def _fix_tgmath_int(source): |
---|
| 463 | # type: (str) -> str |
---|
| 464 | """ |
---|
| 465 | Replace f(integer) with f(integer.) for sin, cos, pow, etc. |
---|
| 466 | |
---|
| 467 | OS X OpenCL complains that it can't resolve the type generic calls to |
---|
| 468 | the standard math functions when they are called with integer constants, |
---|
| 469 | but this does not happen with the Windows Intel driver for example. |
---|
| 470 | To avoid confusion on the matrix marketplace, automatically promote |
---|
| 471 | integers to floats if we recognize them in the source. |
---|
| 472 | |
---|
| 473 | The specific functions we look for are: |
---|
| 474 | |
---|
| 475 | trigonometric: sin, asin, sinh, asinh, etc., and atan2 |
---|
| 476 | exponential: exp, exp2, exp10, expm1, log, log2, log10, logp1 |
---|
| 477 | power: pow, pown, powr, sqrt, rsqrt, rootn |
---|
| 478 | special: erf, erfc, tgamma |
---|
| 479 | float: fabs, fmin, fmax |
---|
| 480 | |
---|
| 481 | Note that we don't convert the second argument of dual argument |
---|
| 482 | functions: atan2, fmax, fmin, pow, powr. This could potentially |
---|
| 483 | be a problem for pow(x, 2), but that case seems to work without change. |
---|
| 484 | """ |
---|
| 485 | out = TGMATH_INT_RE.sub(r'\g<0>.', source) |
---|
| 486 | return out |
---|
| 487 | |
---|
| 488 | |
---|
| 489 | # Floating point regular expression |
---|
| 490 | # |
---|
| 491 | # Define parts: |
---|
| 492 | # |
---|
| 493 | # E = [eE][+-]?\d+ : Exponent |
---|
| 494 | # P = [.] : Decimal separator |
---|
| 495 | # N = [1-9]\d* : Natural number, no leading zeros |
---|
| 496 | # Z = 0 : Zero |
---|
| 497 | # F = \d+ : Fractional number, maybe leading zeros |
---|
| 498 | # F? = \d* : Optional fractional number |
---|
| 499 | # |
---|
| 500 | # We want to reject bare natural numbers and bare decimal points, so we |
---|
| 501 | # need to tediously outline the cases where we have either a fraction or |
---|
| 502 | # an exponent: |
---|
| 503 | # |
---|
| 504 | # ( ZP | ZPF | ZE | ZPE | ZPFE | NP | NPF | NE | NPE | NPFE | PF | PFE ) |
---|
| 505 | # |
---|
| 506 | # |
---|
| 507 | # We can then join cases by making parts optional. The following are |
---|
| 508 | # some ways to do this: |
---|
| 509 | # |
---|
| 510 | # ( (Z|N)(P|PF|E|PE|PFE) | PFE? ) # Split on lead |
---|
| 511 | # => ( (Z|N)(PF?|(PF?)?E) | PFE? ) |
---|
| 512 | # ( ((Z|N)PF?|PF)E? | (Z|N)E) # Split on point |
---|
| 513 | # ( (ZP|ZPF|NP|NPF|PF) | (Z|ZP|ZPF|N|NP|NPF|PF)E ) # Split on E |
---|
| 514 | # => ( ((Z|N)PF?|PF) | ((Z|N)(PF?)? | PF) E ) |
---|
| 515 | FLOAT_RE = re.compile(r""" |
---|
| 516 | (?<!\w) # use negative lookbehind since '.' confuses \b test |
---|
| 517 | # use split on lead to match float ( (Z|N)(PF?|(PF?)?E) | PFE? ) |
---|
| 518 | ( ( 0 | [1-9]\d* ) # ( ( Z | N ) |
---|
| 519 | ([.]\d* | ([.]\d*)? [eE][+-]?\d+ ) # (PF? | (PF?)? E ) |
---|
| 520 | | [.]\d+ ([eE][+-]?\d+)? # | PF (E)? |
---|
| 521 | ) # ) |
---|
| 522 | (?!\w) # use negative lookahead since '.' confuses \b test |
---|
| 523 | """, re.VERBOSE) |
---|
| 524 | def _tag_float(source, constant_flag): |
---|
[5ceb7d0] | 525 | # Convert floating point constants to single by adding 'f' to the end, |
---|
| 526 | # or long double with an 'L' suffix. OS/X complains if you don't do this. |
---|
[2e49f9e] | 527 | out = FLOAT_RE.sub(r'\g<0>%s'%constant_flag, source) |
---|
| 528 | #print("in",repr(source),"out",repr(out), constant_flag) |
---|
| 529 | return out |
---|
| 530 | |
---|
| 531 | def test_tag_float(): |
---|
[bb4b509] | 532 | """check that floating point constants are properly identified and tagged with 'f'""" |
---|
[2e49f9e] | 533 | |
---|
[bb4b509] | 534 | cases = """ |
---|
[2e49f9e] | 535 | ZP : 0. |
---|
| 536 | ZPF : 0.0,0.01,0.1 |
---|
| 537 | Z E: 0e+001 |
---|
| 538 | ZP E: 0.E0 |
---|
| 539 | ZPFE: 0.13e-031 |
---|
| 540 | NP : 1., 12. |
---|
| 541 | NPF : 1.0001, 1.1, 1.0 |
---|
| 542 | N E: 1e0, 37E-080 |
---|
| 543 | NP E: 1.e0, 37.E-080 |
---|
| 544 | NPFE: 845.017e+22 |
---|
| 545 | PF : .1, .0, .0100 |
---|
| 546 | PFE: .6e+9, .82E-004 |
---|
| 547 | # isolated cases |
---|
| 548 | 0. |
---|
| 549 | 1e0 |
---|
| 550 | 0.13e-013 |
---|
| 551 | # untouched |
---|
| 552 | struct3.e3, 03.05.67, 37 |
---|
| 553 | # expressions |
---|
| 554 | 3.75+-1.6e-7-27+13.2 |
---|
| 555 | a3.e2 - 0. |
---|
| 556 | 4*atan(1) |
---|
| 557 | 4.*atan(1.) |
---|
| 558 | """ |
---|
| 559 | |
---|
[bb4b509] | 560 | output = """ |
---|
[2e49f9e] | 561 | ZP : 0.f |
---|
| 562 | ZPF : 0.0f,0.01f,0.1f |
---|
| 563 | Z E: 0e+001f |
---|
| 564 | ZP E: 0.E0f |
---|
| 565 | ZPFE: 0.13e-031f |
---|
| 566 | NP : 1.f, 12.f |
---|
| 567 | NPF : 1.0001f, 1.1f, 1.0f |
---|
| 568 | N E: 1e0f, 37E-080f |
---|
| 569 | NP E: 1.e0f, 37.E-080f |
---|
| 570 | NPFE: 845.017e+22f |
---|
| 571 | PF : .1f, .0f, .0100f |
---|
| 572 | PFE: .6e+9f, .82E-004f |
---|
| 573 | # isolated cases |
---|
| 574 | 0.f |
---|
| 575 | 1e0f |
---|
| 576 | 0.13e-013f |
---|
| 577 | # untouched |
---|
| 578 | struct3.e3, 03.05.67, 37 |
---|
| 579 | # expressions |
---|
| 580 | 3.75f+-1.6e-7f-27+13.2f |
---|
| 581 | a3.e2 - 0.f |
---|
| 582 | 4*atan(1) |
---|
| 583 | 4.f*atan(1.f) |
---|
| 584 | """ |
---|
| 585 | |
---|
| 586 | for case_in, case_out in zip(cases.split('\n'), output.split('\n')): |
---|
| 587 | out = _tag_float(case_in, 'f') |
---|
| 588 | assert case_out == out, "%r => %r"%(case_in, out) |
---|
[5ceb7d0] | 589 | |
---|
| 590 | |
---|
[32e3c9b] | 591 | def kernel_name(model_info, variant): |
---|
| 592 | # type: (ModelInfo, str) -> str |
---|
[5ceb7d0] | 593 | """ |
---|
| 594 | Name of the exported kernel symbol. |
---|
[32e3c9b] | 595 | |
---|
| 596 | *variant* is "Iq", "Iqxy" or "Imagnetic". |
---|
[5ceb7d0] | 597 | """ |
---|
[32e3c9b] | 598 | return model_info.name + "_" + variant |
---|
[5ceb7d0] | 599 | |
---|
| 600 | |
---|
| 601 | def indent(s, depth): |
---|
[f619de7] | 602 | # type: (str, int) -> str |
---|
[5ceb7d0] | 603 | """ |
---|
| 604 | Indent a string of text with *depth* additional spaces on each line. |
---|
| 605 | """ |
---|
| 606 | spaces = " "*depth |
---|
[d86f0fc] | 607 | interline_separator = "\n" + spaces |
---|
| 608 | return spaces + interline_separator.join(s.split("\n")) |
---|
[5ceb7d0] | 609 | |
---|
| 610 | |
---|
[f619de7] | 611 | _template_cache = {} # type: Dict[str, Tuple[int, str, str]] |
---|
[d5ac45f] | 612 | def load_template(filename): |
---|
[f619de7] | 613 | # type: (str) -> str |
---|
[d86f0fc] | 614 | """ |
---|
| 615 | Load template file from sasmodels resource directory. |
---|
| 616 | """ |
---|
[7891a2a] | 617 | path = joinpath(DATA_PATH, filename) |
---|
[d5ac45f] | 618 | mtime = getmtime(path) |
---|
| 619 | if filename not in _template_cache or mtime > _template_cache[filename][0]: |
---|
| 620 | with open(path) as fid: |
---|
[303d8d6] | 621 | _template_cache[filename] = (mtime, fid.read(), path) |
---|
[da63656] | 622 | return _template_cache[filename][1], path |
---|
[d5ac45f] | 623 | |
---|
[303d8d6] | 624 | |
---|
[03cac08] | 625 | _FN_TEMPLATE = """\ |
---|
| 626 | double %(name)s(%(pars)s); |
---|
| 627 | double %(name)s(%(pars)s) { |
---|
[da63656] | 628 | #line %(line)d "%(filename)s" |
---|
[03cac08] | 629 | %(body)s |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | """ |
---|
[108e70e] | 633 | def _gen_fn(model_info, name, pars): |
---|
| 634 | # type: (ModelInfo, str, List[Parameter]) -> str |
---|
[5ceb7d0] | 635 | """ |
---|
[d5ac45f] | 636 | Generate a function given pars and body. |
---|
| 637 | |
---|
| 638 | Returns the following string:: |
---|
[5ceb7d0] | 639 | |
---|
[d5ac45f] | 640 | double fn(double a, double b, ...); |
---|
| 641 | double fn(double a, double b, ...) { |
---|
| 642 | .... |
---|
| 643 | } |
---|
[5ceb7d0] | 644 | """ |
---|
[60eab2a] | 645 | par_decl = ', '.join(p.as_function_argument() for p in pars) if pars else 'void' |
---|
[108e70e] | 646 | body = getattr(model_info, name) |
---|
| 647 | filename = model_info.filename |
---|
| 648 | # Note: if symbol is defined strangely in the module then default it to 1 |
---|
| 649 | lineno = model_info.lineno.get(name, 1) |
---|
[da63656] | 650 | return _FN_TEMPLATE % { |
---|
| 651 | 'name': name, 'pars': par_decl, 'body': body, |
---|
[108e70e] | 652 | 'filename': filename.replace('\\', '\\\\'), 'line': lineno, |
---|
[da63656] | 653 | } |
---|
[d5ac45f] | 654 | |
---|
[17bbadd] | 655 | |
---|
[03cac08] | 656 | def _call_pars(prefix, pars): |
---|
[f619de7] | 657 | # type: (str, List[Parameter]) -> List[str] |
---|
[03cac08] | 658 | """ |
---|
[bb4b509] | 659 | Return a list of *prefix+parameter* from parameter items. |
---|
| 660 | |
---|
| 661 | *prefix* should be "v." if v is a struct. |
---|
[03cac08] | 662 | """ |
---|
[69aa451] | 663 | return [p.as_call_reference(prefix) for p in pars] |
---|
[d5ac45f] | 664 | |
---|
[17bbadd] | 665 | |
---|
[01c8d9e] | 666 | _IQXY_PATTERN = re.compile(r"(^|\s)double\s+I(?P<mode>q(ac|abc|xy))\s*[(]", |
---|
[03cac08] | 667 | flags=re.MULTILINE) |
---|
[108e70e] | 668 | def find_xy_mode(source): |
---|
[f619de7] | 669 | # type: (List[str]) -> bool |
---|
[03cac08] | 670 | """ |
---|
[108e70e] | 671 | Return the xy mode as qa, qac, qabc or qxy. |
---|
[d5ac45f] | 672 | |
---|
[03cac08] | 673 | Note this is not a C parser, and so can be easily confused by |
---|
| 674 | non-standard syntax. Also, it will incorrectly identify the following |
---|
[108e70e] | 675 | as having 2D models:: |
---|
[03cac08] | 676 | |
---|
| 677 | /* |
---|
[108e70e] | 678 | double Iqac(qab, qc, ...) { ... fill this in later ... } |
---|
| 679 | */ |
---|
| 680 | |
---|
| 681 | If you want to comment out the function, use // on the front of the |
---|
| 682 | line:: |
---|
| 683 | |
---|
| 684 | /* |
---|
| 685 | // double Iqac(qab, qc, ...) { ... fill this in later ... } |
---|
[03cac08] | 686 | */ |
---|
| 687 | |
---|
| 688 | """ |
---|
[108e70e] | 689 | for code in source: |
---|
| 690 | m = _IQXY_PATTERN.search(code) |
---|
| 691 | if m is not None: |
---|
| 692 | return m.group('mode') |
---|
| 693 | return 'qa' |
---|
[d5ac45f] | 694 | |
---|
[c036ddb] | 695 | # Note: not presently used. Need to know whether Fq is available before |
---|
| 696 | # trying to compile the source. Leave the code here in case we decide that |
---|
| 697 | # define have_Fq for each form factor is too tedious and error prone. |
---|
[01c8d9e] | 698 | _FQ_PATTERN = re.compile(r"(^|\s)void\s+Fq[(]", flags=re.MULTILINE) |
---|
[c036ddb] | 699 | def contains_Fq(source): |
---|
| 700 | # type: (List[str]) -> bool |
---|
| 701 | """ |
---|
| 702 | Return True if C source defines "void Fq(". |
---|
| 703 | """ |
---|
[01c8d9e] | 704 | for code in source: |
---|
[cd28947] | 705 | if _FQ_PATTERN.search(code) is not None: |
---|
[01c8d9e] | 706 | return True |
---|
| 707 | return False |
---|
[17bbadd] | 708 | |
---|
[e44432d] | 709 | _SHELL_VOLUME_PATTERN = re.compile(r"(^|\s)double\s+shell_volume[(]", flags=re.MULTILINE) |
---|
| 710 | def contains_shell_volume(source): |
---|
| 711 | # type: (List[str]) -> bool |
---|
| 712 | """ |
---|
[cd28947] | 713 | Return True if C source defines "double shell_volume(". |
---|
[e44432d] | 714 | """ |
---|
| 715 | for code in source: |
---|
[cd28947] | 716 | if _SHELL_VOLUME_PATTERN.search(code) is not None: |
---|
[e44432d] | 717 | return True |
---|
| 718 | return False |
---|
| 719 | |
---|
[108e70e] | 720 | def _add_source(source, code, path, lineno=1): |
---|
[da63656] | 721 | """ |
---|
| 722 | Add a file to the list of source code chunks, tagged with path and line. |
---|
| 723 | """ |
---|
[7891a2a] | 724 | path = path.replace('\\', '\\\\') |
---|
[108e70e] | 725 | source.append('#line %d "%s"' % (lineno, path)) |
---|
[da63656] | 726 | source.append(code) |
---|
[5ceb7d0] | 727 | |
---|
[17bbadd] | 728 | def make_source(model_info): |
---|
[a4280bd] | 729 | # type: (ModelInfo) -> Dict[str, str] |
---|
[5ceb7d0] | 730 | """ |
---|
[17bbadd] | 731 | Generate the OpenCL/ctypes kernel from the module info. |
---|
| 732 | |
---|
[f619de7] | 733 | Uses source files found in the given search path. Returns None if this |
---|
| 734 | is a pure python model, with no C source components. |
---|
[5ceb7d0] | 735 | """ |
---|
[6d6508e] | 736 | if callable(model_info.Iq): |
---|
[739aad4] | 737 | raise ValueError("can't compile python model") |
---|
| 738 | #return None |
---|
[17bbadd] | 739 | |
---|
[5ceb7d0] | 740 | # TODO: need something other than volume to indicate dispersion parameters |
---|
| 741 | # No volume normalization despite having a volume parameter. |
---|
| 742 | # Thickness is labelled a volume in order to trigger polydispersity. |
---|
| 743 | # May want a separate dispersion flag, or perhaps a separate category for |
---|
| 744 | # disperse, but not volume. Volume parameters also use relative values |
---|
| 745 | # for the distribution rather than the absolute values used by angular |
---|
| 746 | # dispersion. Need to be careful that necessary parameters are available |
---|
| 747 | # for computing volume even if we allow non-disperse volume parameters. |
---|
[6d6508e] | 748 | partable = model_info.parameters |
---|
[c036ddb] | 749 | |
---|
[03cac08] | 750 | # Load templates and user code |
---|
| 751 | kernel_header = load_template('kernel_header.c') |
---|
[9ee2756] | 752 | kernel_code = load_template('kernel_iq.c') |
---|
[da63656] | 753 | user_code = [(f, open(f).read()) for f in model_sources(model_info)] |
---|
[c036ddb] | 754 | |
---|
[03cac08] | 755 | # Build initial sources |
---|
[da63656] | 756 | source = [] |
---|
| 757 | _add_source(source, *kernel_header) |
---|
| 758 | for path, code in user_code: |
---|
| 759 | _add_source(source, code, path) |
---|
[108e70e] | 760 | if model_info.c_code: |
---|
| 761 | _add_source(source, model_info.c_code, model_info.filename, |
---|
| 762 | lineno=model_info.lineno.get('c_code', 1)) |
---|
| 763 | |
---|
[69aa451] | 764 | # Make parameters for q, qx, qy so that we can use them in declarations |
---|
[108e70e] | 765 | q, qx, qy, qab, qa, qb, qc \ |
---|
| 766 | = [Parameter(name=v) for v in 'q qx qy qab qa qb qc'.split()] |
---|
[c036ddb] | 767 | |
---|
[d5ac45f] | 768 | # Generate form_volume function, etc. from body only |
---|
[f619de7] | 769 | if isinstance(model_info.form_volume, str): |
---|
[d19962c] | 770 | pars = partable.form_volume_parameters |
---|
[108e70e] | 771 | source.append(_gen_fn(model_info, 'form_volume', pars)) |
---|
[e44432d] | 772 | if isinstance(model_info.shell_volume, str): |
---|
| 773 | pars = partable.form_volume_parameters |
---|
| 774 | source.append(_gen_fn(model_info, 'shell_volume', pars)) |
---|
[f619de7] | 775 | if isinstance(model_info.Iq, str): |
---|
[d19962c] | 776 | pars = [q] + partable.iq_parameters |
---|
[108e70e] | 777 | source.append(_gen_fn(model_info, 'Iq', pars)) |
---|
[f619de7] | 778 | if isinstance(model_info.Iqxy, str): |
---|
[108e70e] | 779 | pars = [qx, qy] + partable.iq_parameters + partable.orientation_parameters |
---|
| 780 | source.append(_gen_fn(model_info, 'Iqxy', pars)) |
---|
| 781 | if isinstance(model_info.Iqac, str): |
---|
| 782 | pars = [qab, qc] + partable.iq_parameters |
---|
| 783 | source.append(_gen_fn(model_info, 'Iqac', pars)) |
---|
| 784 | if isinstance(model_info.Iqabc, str): |
---|
| 785 | pars = [qa, qb, qc] + partable.iq_parameters |
---|
| 786 | source.append(_gen_fn(model_info, 'Iqabc', pars)) |
---|
| 787 | |
---|
[e44432d] | 788 | # Check for shell_volume in source |
---|
| 789 | is_hollow = contains_shell_volume(source) |
---|
| 790 | |
---|
[108e70e] | 791 | # What kind of 2D model do we need? Is it consistent with the parameters? |
---|
| 792 | xy_mode = find_xy_mode(source) |
---|
| 793 | if xy_mode == 'qabc' and not partable.is_asymmetric: |
---|
| 794 | raise ValueError("asymmetric oriented models need to define Iqabc") |
---|
| 795 | elif xy_mode == 'qac' and partable.is_asymmetric: |
---|
| 796 | raise ValueError("symmetric oriented models need to define Iqac") |
---|
| 797 | elif not partable.orientation_parameters and xy_mode in ('qac', 'qabc'): |
---|
| 798 | raise ValueError("Unexpected function I%s for unoriented shape"%xy_mode) |
---|
| 799 | elif partable.orientation_parameters and xy_mode not in ('qac', 'qabc'): |
---|
| 800 | if xy_mode == 'qxy': |
---|
| 801 | logger.warn("oriented shapes should define Iqac or Iqabc") |
---|
| 802 | else: |
---|
| 803 | raise ValueError("Expected function Iqac or Iqabc for oriented shape") |
---|
[d5ac45f] | 804 | |
---|
[03cac08] | 805 | # Define the parameter table |
---|
[9ee2756] | 806 | lineno = getframeinfo(currentframe()).lineno + 2 |
---|
| 807 | source.append('#line %d "sasmodels/generate.py"'%lineno) |
---|
| 808 | #source.append('introduce breakage in generate to test lineno reporting') |
---|
[03cac08] | 809 | source.append("#define PARAMETER_TABLE \\") |
---|
[69aa451] | 810 | source.append("\\\n".join(p.as_definition() |
---|
[d19962c] | 811 | for p in partable.kernel_parameters)) |
---|
[03cac08] | 812 | # Define the function calls |
---|
[e44432d] | 813 | call_effective_radius = "#define CALL_EFFECTIVE_RADIUS(_mode, _v) 0.0" |
---|
[d19962c] | 814 | if partable.form_volume_parameters: |
---|
[d2bb604] | 815 | refs = _call_pars("_v.", partable.form_volume_parameters) |
---|
[e44432d] | 816 | if is_hollow: |
---|
| 817 | call_volume = "#define CALL_VOLUME(_form, _shell, _v) do { _form = form_volume(%s); _shell = shell_volume(%s); } while (0)"%((",".join(refs),)*2) |
---|
| 818 | else: |
---|
| 819 | call_volume = "#define CALL_VOLUME(_form, _shell, _v) do { _form = _shell = form_volume(%s); } while (0)"%(",".join(refs)) |
---|
[5399809] | 820 | if model_info.effective_radius_type: |
---|
[e44432d] | 821 | call_effective_radius = "#define CALL_EFFECTIVE_RADIUS(_mode, _v) effective_radius(_mode, %s)"%(",".join(refs)) |
---|
[d5ac45f] | 822 | else: |
---|
| 823 | # Model doesn't have volume. We could make the kernel run a little |
---|
| 824 | # faster by not using/transferring the volume normalizations, but |
---|
| 825 | # the ifdef's reduce readability more than is worthwhile. |
---|
[e44432d] | 826 | call_volume = "#define CALL_VOLUME(_form, _shell, _v) do { _form = _shell = 1.0; } while (0)" |
---|
[03cac08] | 827 | source.append(call_volume) |
---|
[6e7ba14] | 828 | source.append(call_effective_radius) |
---|
[8698a0d] | 829 | model_refs = _call_pars("_v.", partable.iq_parameters) |
---|
[c036ddb] | 830 | |
---|
| 831 | if model_info.have_Fq: |
---|
| 832 | pars = ",".join(["_q", "&_F1", "&_F2",] + model_refs) |
---|
| 833 | call_iq = "#define CALL_FQ(_q, _F1, _F2, _v) Fq(%s)" % pars |
---|
| 834 | clear_iq = "#undef CALL_FQ" |
---|
| 835 | else: |
---|
[01c8d9e] | 836 | pars = ",".join(["_q"] + model_refs) |
---|
| 837 | call_iq = "#define CALL_IQ(_q, _v) Iq(%s)" % pars |
---|
[c036ddb] | 838 | clear_iq = "#undef CALL_IQ" |
---|
[9ee2756] | 839 | if xy_mode == 'qabc': |
---|
| 840 | pars = ",".join(["_qa", "_qb", "_qc"] + model_refs) |
---|
[108e70e] | 841 | call_iqxy = "#define CALL_IQ_ABC(_qa,_qb,_qc,_v) Iqabc(%s)" % pars |
---|
[9ee2756] | 842 | clear_iqxy = "#undef CALL_IQ_ABC" |
---|
| 843 | elif xy_mode == 'qac': |
---|
| 844 | pars = ",".join(["_qa", "_qc"] + model_refs) |
---|
[108e70e] | 845 | call_iqxy = "#define CALL_IQ_AC(_qa,_qc,_v) Iqac(%s)" % pars |
---|
[9ee2756] | 846 | clear_iqxy = "#undef CALL_IQ_AC" |
---|
[c036ddb] | 847 | elif xy_mode == 'qa' and not model_info.have_Fq: |
---|
[8698a0d] | 848 | pars = ",".join(["_qa"] + model_refs) |
---|
| 849 | call_iqxy = "#define CALL_IQ_A(_qa,_v) Iq(%s)" % pars |
---|
| 850 | clear_iqxy = "#undef CALL_IQ_A" |
---|
[c036ddb] | 851 | elif xy_mode == 'qa' and model_info.have_Fq: |
---|
| 852 | pars = ",".join(["_qa", "&_F1", "&_F2",] + model_refs) |
---|
| 853 | # Note: uses rare C construction (expr1, expr2) which computes |
---|
| 854 | # expr1 then expr2 and evaluates to expr2. This allows us to |
---|
| 855 | # leave it looking like a function even though it is returning |
---|
| 856 | # its values by reference. |
---|
| 857 | call_iqxy = "#define CALL_FQ_A(_qa,_F1,_F2,_v) (Fq(%s),_F2)" % pars |
---|
| 858 | clear_iqxy = "#undef CALL_FQ_A" |
---|
[108e70e] | 859 | elif xy_mode == 'qxy': |
---|
| 860 | orientation_refs = _call_pars("_v.", partable.orientation_parameters) |
---|
| 861 | pars = ",".join(["_qx", "_qy"] + model_refs + orientation_refs) |
---|
| 862 | call_iqxy = "#define CALL_IQ_XY(_qx,_qy,_v) Iqxy(%s)" % pars |
---|
| 863 | clear_iqxy = "#undef CALL_IQ_XY" |
---|
| 864 | if partable.orientation_parameters: |
---|
| 865 | call_iqxy += "\n#define HAVE_THETA" |
---|
| 866 | clear_iqxy += "\n#undef HAVE_THETA" |
---|
| 867 | if partable.is_asymmetric: |
---|
| 868 | call_iqxy += "\n#define HAVE_PSI" |
---|
| 869 | clear_iqxy += "\n#undef HAVE_PSI" |
---|
| 870 | |
---|
[03cac08] | 871 | |
---|
[bb4b509] | 872 | magpars = [k-2 for k, p in enumerate(partable.call_parameters) |
---|
[32e3c9b] | 873 | if p.type == 'sld'] |
---|
[03cac08] | 874 | # Fill in definitions for numbers of parameters |
---|
[d19962c] | 875 | source.append("#define MAX_PD %s"%partable.max_pd) |
---|
[a4280bd] | 876 | source.append("#define NUM_PARS %d"%partable.npars) |
---|
[9eb3632] | 877 | source.append("#define NUM_VALUES %d" % partable.nvalues) |
---|
[a4280bd] | 878 | source.append("#define NUM_MAGNETIC %d" % partable.nmagnetic) |
---|
[32e3c9b] | 879 | source.append("#define MAGNETIC_PARS %s"%",".join(str(k) for k in magpars)) |
---|
[4991048] | 880 | source.append("#define PROJECTION %d"%PROJECTION) |
---|
[03cac08] | 881 | # TODO: allow mixed python/opencl kernels? |
---|
[c036ddb] | 882 | ocl = _kernels(kernel_code, call_iq, clear_iq, call_iqxy, clear_iqxy, model_info.name) |
---|
| 883 | dll = _kernels(kernel_code, call_iq, clear_iq, call_iqxy, clear_iqxy, model_info.name) |
---|
[01c8d9e] | 884 | |
---|
[a4280bd] | 885 | result = { |
---|
| 886 | 'dll': '\n'.join(source+dll[0]+dll[1]+dll[2]), |
---|
| 887 | 'opencl': '\n'.join(source+ocl[0]+ocl[1]+ocl[2]), |
---|
| 888 | } |
---|
| 889 | return result |
---|
[5ceb7d0] | 890 | |
---|
[7891a2a] | 891 | |
---|
[c036ddb] | 892 | def _kernels(kernel, call_iq, clear_iq, call_iqxy, clear_iqxy, name): |
---|
[b966a96] | 893 | # type: ([str,str], str, str, str) -> List[str] |
---|
| 894 | code = kernel[0] |
---|
| 895 | path = kernel[1].replace('\\', '\\\\') |
---|
[9eb3632] | 896 | iq = [ |
---|
[f2f67a6] | 897 | # define the Iq kernel |
---|
[32e3c9b] | 898 | "#define KERNEL_NAME %s_Iq" % name, |
---|
[f2f67a6] | 899 | call_iq, |
---|
[a4280bd] | 900 | '#line 1 "%s Iq"' % path, |
---|
[b966a96] | 901 | code, |
---|
[c036ddb] | 902 | clear_iq, |
---|
[f2f67a6] | 903 | "#undef KERNEL_NAME", |
---|
[9eb3632] | 904 | ] |
---|
[f2f67a6] | 905 | |
---|
[9eb3632] | 906 | iqxy = [ |
---|
[f2f67a6] | 907 | # define the Iqxy kernel from the same source with different #defines |
---|
[32e3c9b] | 908 | "#define KERNEL_NAME %s_Iqxy" % name, |
---|
[f2f67a6] | 909 | call_iqxy, |
---|
[a4280bd] | 910 | '#line 1 "%s Iqxy"' % path, |
---|
[b966a96] | 911 | code, |
---|
[8698a0d] | 912 | clear_iqxy, |
---|
[32e3c9b] | 913 | "#undef KERNEL_NAME", |
---|
[bb4b509] | 914 | ] |
---|
[32e3c9b] | 915 | |
---|
[9eb3632] | 916 | imagnetic = [ |
---|
[32e3c9b] | 917 | # define the Imagnetic kernel |
---|
| 918 | "#define KERNEL_NAME %s_Imagnetic" % name, |
---|
| 919 | "#define MAGNETIC 1", |
---|
[f2f67a6] | 920 | call_iqxy, |
---|
[a4280bd] | 921 | '#line 1 "%s Imagnetic"' % path, |
---|
[b966a96] | 922 | code, |
---|
[8698a0d] | 923 | clear_iqxy, |
---|
[32e3c9b] | 924 | "#undef MAGNETIC", |
---|
[f2f67a6] | 925 | "#undef KERNEL_NAME", |
---|
| 926 | ] |
---|
[a4280bd] | 927 | |
---|
| 928 | return iq, iqxy, imagnetic |
---|
[f2f67a6] | 929 | |
---|
[4d76711] | 930 | |
---|
[68e7f9d] | 931 | def load_kernel_module(model_name): |
---|
[f619de7] | 932 | # type: (str) -> module |
---|
[f2f67a6] | 933 | """ |
---|
| 934 | Return the kernel module named in *model_name*. |
---|
| 935 | |
---|
| 936 | If the name ends in *.py* then load it as a custom model using |
---|
| 937 | :func:`sasmodels.custom.load_custom_kernel_module`, otherwise |
---|
| 938 | load it from :mod:`sasmodels.models`. |
---|
| 939 | """ |
---|
[68e7f9d] | 940 | if model_name.endswith('.py'): |
---|
| 941 | kernel_module = load_custom_kernel_module(model_name) |
---|
| 942 | else: |
---|
[a22d104] | 943 | try: |
---|
| 944 | from sasmodels import models |
---|
| 945 | __import__('sasmodels.models.'+model_name) |
---|
| 946 | kernel_module = getattr(models, model_name, None) |
---|
[2f733b1] | 947 | except ImportError: |
---|
| 948 | # If the model isn't a built in model, try the plugin directory |
---|
[f9c8474] | 949 | plugin_path = environ.get('SAS_MODELPATH', None) |
---|
[a22d104] | 950 | if plugin_path is not None: |
---|
| 951 | file_name = model_name.split(sep)[-1] |
---|
| 952 | model_name = plugin_path + sep + file_name + ".py" |
---|
[2f733b1] | 953 | kernel_module = load_custom_kernel_module(model_name) |
---|
| 954 | else: |
---|
| 955 | raise |
---|
[68e7f9d] | 956 | return kernel_module |
---|
| 957 | |
---|
[5ceb7d0] | 958 | |
---|
| 959 | section_marker = re.compile(r'\A(?P<first>[%s])(?P=first)*\Z' |
---|
[7891a2a] | 960 | % re.escape(string.punctuation)) |
---|
[5ceb7d0] | 961 | def _convert_section_titles_to_boldface(lines): |
---|
[f619de7] | 962 | # type: (Sequence[str]) -> Iterator[str] |
---|
[5ceb7d0] | 963 | """ |
---|
| 964 | Do the actual work of identifying and converting section headings. |
---|
| 965 | """ |
---|
| 966 | prior = None |
---|
| 967 | for line in lines: |
---|
| 968 | if prior is None: |
---|
| 969 | prior = line |
---|
| 970 | elif section_marker.match(line): |
---|
| 971 | if len(line) >= len(prior): |
---|
| 972 | yield "".join(("**", prior, "**")) |
---|
| 973 | prior = None |
---|
| 974 | else: |
---|
| 975 | yield prior |
---|
| 976 | prior = line |
---|
| 977 | else: |
---|
| 978 | yield prior |
---|
| 979 | prior = line |
---|
| 980 | if prior is not None: |
---|
| 981 | yield prior |
---|
| 982 | |
---|
[7891a2a] | 983 | |
---|
[5ceb7d0] | 984 | def convert_section_titles_to_boldface(s): |
---|
[f619de7] | 985 | # type: (str) -> str |
---|
[5ceb7d0] | 986 | """ |
---|
| 987 | Use explicit bold-face rather than section headings so that the table of |
---|
| 988 | contents is not polluted with section names from the model documentation. |
---|
| 989 | |
---|
| 990 | Sections are identified as the title line followed by a line of punctuation |
---|
| 991 | at least as long as the title line. |
---|
| 992 | """ |
---|
| 993 | return "\n".join(_convert_section_titles_to_boldface(s.split('\n'))) |
---|
| 994 | |
---|
[7891a2a] | 995 | |
---|
[17bbadd] | 996 | def make_doc(model_info): |
---|
[f619de7] | 997 | # type: (ModelInfo) -> str |
---|
[5ceb7d0] | 998 | """ |
---|
| 999 | Return the documentation for the model. |
---|
| 1000 | """ |
---|
| 1001 | Iq_units = "The returned value is scaled to units of |cm^-1| |sr^-1|, absolute scale." |
---|
| 1002 | Sq_units = "The returned value is a dimensionless structure factor, $S(q)$." |
---|
[6592f56] | 1003 | docs = model_info.docs if model_info.docs is not None else "" |
---|
| 1004 | docs = convert_section_titles_to_boldface(docs) |
---|
[6e45516] | 1005 | if model_info.structure_factor: |
---|
| 1006 | pars = model_info.parameters.kernel_parameters |
---|
| 1007 | else: |
---|
[b6dab59] | 1008 | pars = (model_info.parameters.common_parameters |
---|
| 1009 | + model_info.parameters.kernel_parameters) |
---|
[6e45516] | 1010 | partable = make_partable(pars) |
---|
[6d6508e] | 1011 | subst = dict(id=model_info.id.replace('_', '-'), |
---|
| 1012 | name=model_info.name, |
---|
| 1013 | title=model_info.title, |
---|
[6e45516] | 1014 | parameters=partable, |
---|
[6d6508e] | 1015 | returns=Sq_units if model_info.structure_factor else Iq_units, |
---|
[5ceb7d0] | 1016 | docs=docs) |
---|
| 1017 | return DOC_HEADER % subst |
---|
| 1018 | |
---|
| 1019 | |
---|
[6592f56] | 1020 | # TODO: need a single source for rst_prolog; it is also in doc/rst_prolog |
---|
[bb4b509] | 1021 | RST_PROLOG = r"""\ |
---|
[6592f56] | 1022 | .. |Ang| unicode:: U+212B |
---|
| 1023 | .. |Ang^-1| replace:: |Ang|\ :sup:`-1` |
---|
| 1024 | .. |Ang^2| replace:: |Ang|\ :sup:`2` |
---|
| 1025 | .. |Ang^-2| replace:: |Ang|\ :sup:`-2` |
---|
| 1026 | .. |1e-6Ang^-2| replace:: 10\ :sup:`-6`\ |Ang|\ :sup:`-2` |
---|
| 1027 | .. |Ang^3| replace:: |Ang|\ :sup:`3` |
---|
| 1028 | .. |Ang^-3| replace:: |Ang|\ :sup:`-3` |
---|
| 1029 | .. |Ang^-4| replace:: |Ang|\ :sup:`-4` |
---|
| 1030 | .. |cm^-1| replace:: cm\ :sup:`-1` |
---|
| 1031 | .. |cm^2| replace:: cm\ :sup:`2` |
---|
| 1032 | .. |cm^-2| replace:: cm\ :sup:`-2` |
---|
| 1033 | .. |cm^3| replace:: cm\ :sup:`3` |
---|
| 1034 | .. |1e15cm^3| replace:: 10\ :sup:`15`\ cm\ :sup:`3` |
---|
| 1035 | .. |cm^-3| replace:: cm\ :sup:`-3` |
---|
| 1036 | .. |sr^-1| replace:: sr\ :sup:`-1` |
---|
| 1037 | |
---|
| 1038 | .. |cdot| unicode:: U+00B7 |
---|
| 1039 | .. |deg| unicode:: U+00B0 |
---|
| 1040 | .. |g/cm^3| replace:: g\ |cdot|\ cm\ :sup:`-3` |
---|
| 1041 | .. |mg/m^2| replace:: mg\ |cdot|\ m\ :sup:`-2` |
---|
| 1042 | .. |fm^2| replace:: fm\ :sup:`2` |
---|
| 1043 | .. |Ang*cm^-1| replace:: |Ang|\ |cdot|\ cm\ :sup:`-1` |
---|
| 1044 | """ |
---|
| 1045 | |
---|
| 1046 | # TODO: make a better fake reference role |
---|
| 1047 | RST_ROLES = """\ |
---|
| 1048 | .. role:: ref |
---|
| 1049 | |
---|
| 1050 | .. role:: numref |
---|
| 1051 | |
---|
| 1052 | """ |
---|
| 1053 | |
---|
[001d9f5] | 1054 | def make_html(model_info): |
---|
[e65c3ba] | 1055 | # type: (ModelInfo) -> str |
---|
[001d9f5] | 1056 | """ |
---|
| 1057 | Convert model docs directly to html. |
---|
| 1058 | """ |
---|
| 1059 | from . import rst2html |
---|
[6592f56] | 1060 | |
---|
| 1061 | rst = make_doc(model_info) |
---|
| 1062 | return rst2html.rst2html("".join((RST_ROLES, RST_PROLOG, rst))) |
---|
| 1063 | |
---|
| 1064 | def view_html(model_name): |
---|
[e65c3ba] | 1065 | # type: (str) -> None |
---|
| 1066 | """ |
---|
| 1067 | Load the model definition and view its help. |
---|
| 1068 | """ |
---|
[6592f56] | 1069 | from . import modelinfo |
---|
| 1070 | kernel_module = load_kernel_module(model_name) |
---|
| 1071 | info = modelinfo.make_model_info(kernel_module) |
---|
[234c532] | 1072 | view_html_from_info(info) |
---|
| 1073 | |
---|
| 1074 | def view_html_from_info(info): |
---|
[e65c3ba] | 1075 | # type: (ModelInfo) -> None |
---|
| 1076 | """ |
---|
| 1077 | View the help for a loaded model definition. |
---|
| 1078 | """ |
---|
[234c532] | 1079 | from . import rst2html |
---|
[6592f56] | 1080 | url = "file://"+dirname(info.filename)+"/" |
---|
[c4e3215] | 1081 | rst2html.view_html(make_html(info), url=url) |
---|
[001d9f5] | 1082 | |
---|
[5ceb7d0] | 1083 | def demo_time(): |
---|
[f619de7] | 1084 | # type: () -> None |
---|
[5ceb7d0] | 1085 | """ |
---|
| 1086 | Show how long it takes to process a model. |
---|
| 1087 | """ |
---|
| 1088 | import datetime |
---|
[6d6508e] | 1089 | from .modelinfo import make_model_info |
---|
| 1090 | from .models import cylinder |
---|
| 1091 | |
---|
[5ceb7d0] | 1092 | tic = datetime.datetime.now() |
---|
[17bbadd] | 1093 | make_source(make_model_info(cylinder)) |
---|
[5ceb7d0] | 1094 | toc = (datetime.datetime.now() - tic).total_seconds() |
---|
| 1095 | print("time: %g"%toc) |
---|
| 1096 | |
---|
[7891a2a] | 1097 | |
---|
[5ceb7d0] | 1098 | def main(): |
---|
[f619de7] | 1099 | # type: () -> None |
---|
[5ceb7d0] | 1100 | """ |
---|
| 1101 | Program which prints the source produced by the model. |
---|
| 1102 | """ |
---|
[6d6508e] | 1103 | from .modelinfo import make_model_info |
---|
| 1104 | |
---|
[5ceb7d0] | 1105 | if len(sys.argv) <= 1: |
---|
| 1106 | print("usage: python -m sasmodels.generate modelname") |
---|
| 1107 | else: |
---|
| 1108 | name = sys.argv[1] |
---|
[68e7f9d] | 1109 | kernel_module = load_kernel_module(name) |
---|
| 1110 | model_info = make_model_info(kernel_module) |
---|
[17bbadd] | 1111 | source = make_source(model_info) |
---|
[c036ddb] | 1112 | print(source['dll']) |
---|
[5ceb7d0] | 1113 | |
---|
[7891a2a] | 1114 | |
---|
[5ceb7d0] | 1115 | if __name__ == "__main__": |
---|
| 1116 | main() |
---|