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