source: sasmodels/sasmodels/generate.py @ ae2b6b5

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

increase code correspondance between iq.c and iq.cl

  • Property mode set to 100644
File size: 23.9 KB
Line 
1"""
2SAS model constructor.
3
4Small angle scattering models are defined by a set of kernel functions:
5
6    *Iq(q, p1, p2, ...)* returns the scattering at q for a form with
7    particular dimensions averaged over all orientations.
8
9    *Iqxy(qx, qy, p1, p2, ...)* returns the scattering at qx, qy for a form
10    with particular dimensions for a single orientation.
11
12    *Imagnetic(qx, qy, result[], p1, p2, ...)* returns the scattering for the
13    polarized neutron spin states (up-up, up-down, down-up, down-down) for
14    a form with particular dimensions for a single orientation.
15
16    *form_volume(p1, p2, ...)* returns the volume of the form with particular
17    dimension, or 1.0 if no volume normalization is required.
18
19    *ER(p1, p2, ...)* returns the effective radius of the form with
20    particular dimensions.
21
22    *VR(p1, p2, ...)* returns the volume ratio for core-shell style forms.
23
24    #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
28These functions are defined in a kernel module .py script and an associated
29set of .c files.  The model constructor will use them to create models with
30polydispersity across volume and orientation parameters, and provide
31scale and background parameters for each model.
32
33*Iq*, *Iqxy*, *Imagnetic* and *form_volume* should be stylized C-99
34functions written for OpenCL.  All functions need prototype declarations
35even if the are defined before they are used.  OpenCL does not support
36*#include* preprocessor directives, so instead the list of includes needs
37to be given as part of the metadata in the kernel module definition.
38The included files should be listed using a path relative to the kernel
39module, or if using "lib/file.c" if it is one of the standard includes
40provided with the sasmodels source.  The includes need to be listed in
41order so that functions are defined before they are used.
42
43Floating point values should be declared as *double*.  For single precision
44calculations, *double* will be replaced by *float*.  The single precision
45conversion will also tag floating point constants with "f" to make them
46single precision constants.  When using integral values in floating point
47expressions, they should be expressed as floating point values by including
48a decimal point.  This includes 0., 1. and 2.
49
50OpenCL has a *sincos* function which can improve performance when both
51the *sin* and *cos* values are needed for a particular argument.  Since
52this function does not exist in C99, all use of *sincos* should be
53replaced by the macro *SINCOS(value, sn, cn)* where *sn* and *cn* are
54previously declared *double* variables.  When compiled for systems without
55OpenCL, *SINCOS* will be replaced by *sin* and *cos* calls.   If *value* is
56an expression, it will appear twice in this case; whether or not it will be
57evaluated twice depends on the quality of the compiler.
58
59If the input parameters are invalid, the scattering calculator should
60return a negative number. Particularly with polydispersity, there are
61some sets of shape parameters which lead to nonsensical forms, such
62as a capped cylinder where the cap radius is smaller than the
63cylinder radius.  The polydispersity calculation will ignore these points,
64effectively chopping the parameter weight distributions at the boundary
65of the infeasible region.  The resulting scattering will be set to
66background.  This will work correctly even when polydispersity is off.
67
68*ER* and *VR* are python functions which operate on parameter vectors.
69The constructor code will generate the necessary vectors for computing
70them with the desired polydispersity.
71The 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
91    the kernel functions.  Note that *effect_radius* and *volfraction*
92    must occur first in structure factor calculations.
93
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.
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
125A :class:`modelinfo.ModelInfo` structure is constructed from the kernel meta
126data and returned to the caller.
127
128The doc string at the start of the kernel module will be used to
129construct the model documentation web pages.  Embedded figures should
130appear in the subdirectory "img" beside the model definition, and tagged
131with the kernel module name to avoid collision with other models.  Some
132file systems are case-sensitive, so only use lower case characters for
133file names and extensions.
134
135Code 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
141    sinc(x) = sin(x)/x, with sin(0)/0 -> 1
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
145
146:func:`load_kernel_module` loads the model definition file and
147:modelinfo:`make_model_info` parses it. :func:`make_source`
148converts C-based model definitions to C source code, including the
149polydispersity integral.  :func:`model_sources` returns the list of
150source files the model depends on, and :func:`timestamp` returns
151the latest time stamp amongst the source files (so you can check if
152the model needs to be rebuilt).
153
154The function :func:`make_doc` extracts the doc string and adds the
155parameter table to the top.  *make_figure* in *sasmodels/doc/genmodel*
156creates the default figure for the model.  [These two sets of code
157should mignrate into docs.py so docs can be updated in one place].
158"""
159from __future__ import print_function
160
161#TODO: determine which functions are useful outside of generate
162#__all__ = ["model_info", "make_doc", "make_source", "convert_type"]
163
164from os.path import abspath, dirname, join as joinpath, exists, getmtime
165import re
166import string
167import warnings
168
169import numpy as np  # type: ignore
170
171from .modelinfo import Parameter
172from .custom import load_custom_kernel_module
173
174try:
175    from typing import Tuple, Sequence, Iterator, Dict
176    from .modelinfo import ModelInfo
177except ImportError:
178    pass
179
180TEMPLATE_ROOT = dirname(__file__)
181
182F16 = np.dtype('float16')
183F32 = np.dtype('float32')
184F64 = np.dtype('float64')
185try:  # CRUFT: older numpy does not support float128
186    F128 = np.dtype('float128')
187except TypeError:
188    F128 = None
189
190# Conversion from units defined in the parameter table for each model
191# to units displayed in the sphinx documentation.
192RST_UNITS = {
193    "Ang": "|Ang|",
194    "1/Ang": "|Ang^-1|",
195    "1/Ang^2": "|Ang^-2|",
196    "1e-6/Ang^2": "|1e-6Ang^-2|",
197    "degrees": "degree",
198    "1/cm": "|cm^-1|",
199    "Ang/cm": "|Ang*cm^-1|",
200    "g/cm3": "|g/cm^3|",
201    "mg/m2": "|mg/m^2|",
202    "": "None",
203    }
204
205# Headers for the parameters tables in th sphinx documentation
206PARTABLE_HEADERS = [
207    "Parameter",
208    "Description",
209    "Units",
210    "Default value",
211    ]
212
213# Minimum width for a default value (this is shorter than the column header
214# width, so will be ignored).
215PARTABLE_VALUE_WIDTH = 10
216
217# Documentation header for the module, giving the model name, its short
218# description and its parameter table.  The remainder of the doc comes
219# from the module docstring.
220DOC_HEADER = """.. _%(id)s:
221
222%(name)s
223=======================================================
224
225%(title)s
226
227%(parameters)s
228
229%(returns)s
230
231%(docs)s
232"""
233
234def format_units(units):
235    # type: (str) -> str
236    """
237    Convert units into ReStructured Text format.
238    """
239    return "string" if isinstance(units, list) else RST_UNITS.get(units, units)
240
241def make_partable(pars):
242    # type: (List[Parameter]) -> str
243    """
244    Generate the parameter table to include in the sphinx documentation.
245    """
246    column_widths = [
247        max(len(p.name) for p in pars),
248        max(len(p.description) for p in pars),
249        max(len(format_units(p.units)) for p in pars),
250        PARTABLE_VALUE_WIDTH,
251        ]
252    column_widths = [max(w, len(h))
253                     for w, h in zip(column_widths, PARTABLE_HEADERS)]
254
255    sep = " ".join("="*w for w in column_widths)
256    lines = [
257        sep,
258        " ".join("%-*s" % (w, h)
259                 for w, h in zip(column_widths, PARTABLE_HEADERS)),
260        sep,
261        ]
262    for p in pars:
263        lines.append(" ".join([
264            "%-*s" % (column_widths[0], p.name),
265            "%-*s" % (column_widths[1], p.description),
266            "%-*s" % (column_widths[2], format_units(p.units)),
267            "%*g" % (column_widths[3], p.default),
268            ]))
269    lines.append(sep)
270    return "\n".join(lines)
271
272def _search(search_path, filename):
273    # type: (List[str], str) -> str
274    """
275    Find *filename* in *search_path*.
276
277    Raises ValueError if file does not exist.
278    """
279    for path in search_path:
280        target = joinpath(path, filename)
281        if exists(target):
282            return target
283    raise ValueError("%r not found in %s" % (filename, search_path))
284
285
286def model_sources(model_info):
287    # type: (ModelInfo) -> List[str]
288    """
289    Return a list of the sources file paths for the module.
290    """
291    search_path = [dirname(model_info.filename),
292                   abspath(joinpath(dirname(__file__), 'models'))]
293    return [_search(search_path, f) for f in model_info.source]
294
295def timestamp(model_info):
296    # type: (ModelInfo) -> int
297    """
298    Return a timestamp for the model corresponding to the most recently
299    changed file or dependency.
300
301    Note that this does not look at the time stamps for the OpenCL header
302    information since that need not trigger a recompile of the DLL.
303    """
304    source_files = (model_sources(model_info)
305                    + model_templates()
306                    + [model_info.filename])
307    newest = max(getmtime(f) for f in source_files)
308    return newest
309
310def model_templates():
311    # type: () -> List[str]
312    # TODO: fails DRY; templates appear two places.
313    # should instead have model_info contain a list of paths
314    # Note: kernel_iq.cl is not on this list because changing it need not
315    # trigger a recompile of the dll.
316    return [joinpath(TEMPLATE_ROOT, filename)
317            for filename in ('kernel_header.c', 'kernel_iq.c')]
318
319def convert_type(source, dtype):
320    # type: (str, np.dtype) -> str
321    """
322    Convert code from double precision to the desired type.
323
324    Floating point constants are tagged with 'f' for single precision or 'L'
325    for long double precision.
326    """
327    if dtype == F16:
328        fbytes = 2
329        source = _convert_type(source, "half", "f")
330    elif dtype == F32:
331        fbytes = 4
332        source = _convert_type(source, "float", "f")
333    elif dtype == F64:
334        fbytes = 8
335        # no need to convert if it is already double
336    elif dtype == F128:
337        fbytes = 16
338        source = _convert_type(source, "long double", "L")
339    else:
340        raise ValueError("Unexpected dtype in source conversion: %s"%dtype)
341    return ("#define FLOAT_SIZE %d\n"%fbytes)+source
342
343
344def _convert_type(source, type_name, constant_flag):
345    # type: (str, str, str) -> str
346    """
347    Replace 'double' with *type_name* in *source*, tagging floating point
348    constants with *constant_flag*.
349    """
350    # Convert double keyword to float/long double/half.
351    # Accept an 'n' # parameter for vector # values, where n is 2, 4, 8 or 16.
352    # Assume complex numbers are represented as cdouble which is typedef'd
353    # to double2.
354    source = re.sub(r'(^|[^a-zA-Z0-9_]c?)double(([248]|16)?($|[^a-zA-Z0-9_]))',
355                    r'\1%s\2'%type_name, source)
356    # Convert floating point constants to single by adding 'f' to the end,
357    # or long double with an 'L' suffix.  OS/X complains if you don't do this.
358    source = re.sub(r'[^a-zA-Z_](\d*[.]\d+|\d+[.]\d*)([eE][+-]?\d+)?',
359                    r'\g<0>%s'%constant_flag, source)
360    return source
361
362
363def kernel_name(model_info, is_2d):
364    # type: (ModelInfo, bool) -> str
365    """
366    Name of the exported kernel symbol.
367    """
368    return model_info.name + "_" + ("Iqxy" if is_2d else "Iq")
369
370
371def indent(s, depth):
372    # type: (str, int) -> str
373    """
374    Indent a string of text with *depth* additional spaces on each line.
375    """
376    spaces = " "*depth
377    sep = "\n" + spaces
378    return spaces + sep.join(s.split("\n"))
379
380
381_template_cache = {}  # type: Dict[str, Tuple[int, str, str]]
382def load_template(filename):
383    # type: (str) -> str
384    path = joinpath(TEMPLATE_ROOT, filename)
385    mtime = getmtime(path)
386    if filename not in _template_cache or mtime > _template_cache[filename][0]:
387        with open(path) as fid:
388            _template_cache[filename] = (mtime, fid.read(), path)
389    return _template_cache[filename][1]
390
391
392_FN_TEMPLATE = """\
393double %(name)s(%(pars)s);
394double %(name)s(%(pars)s) {
395    %(body)s
396}
397
398"""
399
400def _gen_fn(name, pars, body):
401    # type: (str, List[Parameter], str) -> str
402    """
403    Generate a function given pars and body.
404
405    Returns the following string::
406
407         double fn(double a, double b, ...);
408         double fn(double a, double b, ...) {
409             ....
410         }
411    """
412    par_decl = ', '.join(p.as_function_argument() for p in pars) if pars else 'void'
413    return _FN_TEMPLATE % {'name': name, 'body': body, 'pars': par_decl}
414
415def _call_pars(prefix, pars):
416    # type: (str, List[Parameter]) -> List[str]
417    """
418    Return a list of *prefix.parameter* from parameter items.
419    """
420    return [p.as_call_reference(prefix) for p in pars]
421
422_IQXY_PATTERN = re.compile("^((inline|static) )? *(double )? *Iqxy *([(]|$)",
423                           flags=re.MULTILINE)
424def _have_Iqxy(sources):
425    # type: (List[str]) -> bool
426    """
427    Return true if any file defines Iqxy.
428
429    Note this is not a C parser, and so can be easily confused by
430    non-standard syntax.  Also, it will incorrectly identify the following
431    as having Iqxy::
432
433        /*
434        double Iqxy(qx, qy, ...) { ... fill this in later ... }
435        */
436
437    If you want to comment out an Iqxy function, use // on the front of the
438    line instead.
439    """
440    for code in sources:
441        if _IQXY_PATTERN.search(code):
442            return True
443    else:
444        return False
445
446def make_source(model_info):
447    # type: (ModelInfo) -> str
448    """
449    Generate the OpenCL/ctypes kernel from the module info.
450
451    Uses source files found in the given search path.  Returns None if this
452    is a pure python model, with no C source components.
453    """
454    if callable(model_info.Iq):
455        raise ValueError("can't compile python model")
456
457    # TODO: need something other than volume to indicate dispersion parameters
458    # No volume normalization despite having a volume parameter.
459    # Thickness is labelled a volume in order to trigger polydispersity.
460    # May want a separate dispersion flag, or perhaps a separate category for
461    # disperse, but not volume.  Volume parameters also use relative values
462    # for the distribution rather than the absolute values used by angular
463    # dispersion.  Need to be careful that necessary parameters are available
464    # for computing volume even if we allow non-disperse volume parameters.
465
466    partable = model_info.parameters
467
468    # Identify parameters for Iq, Iqxy, Iq_magnetic and form_volume.
469    # Note that scale and volume are not possible types.
470
471    # Load templates and user code
472    kernel_header = load_template('kernel_header.c')
473    dll_code = load_template('kernel_iq.c')
474    ocl_code = load_template('kernel_iq.cl')
475    #ocl_code = load_template('kernel_iq_local.cl')
476    user_code = [open(f).read() for f in model_sources(model_info)]
477
478    # Build initial sources
479    source = [kernel_header] + user_code
480
481    # Make parameters for q, qx, qy so that we can use them in declarations
482    q, qx, qy = [Parameter(name=v) for v in ('q', 'qx', 'qy')]
483    # Generate form_volume function, etc. from body only
484    if isinstance(model_info.form_volume, str):
485        pars = partable.form_volume_parameters
486        source.append(_gen_fn('form_volume', pars, model_info.form_volume))
487    if isinstance(model_info.Iq, str):
488        pars = [q] + partable.iq_parameters
489        source.append(_gen_fn('Iq', pars, model_info.Iq))
490    if isinstance(model_info.Iqxy, str):
491        pars = [qx, qy] + partable.iqxy_parameters
492        source.append(_gen_fn('Iqxy', pars, model_info.Iqxy))
493
494    # Define the parameter table
495    source.append("#define PARAMETER_TABLE \\")
496    source.append("\\\n".join(p.as_definition()
497                              for p in partable.kernel_parameters))
498
499    # Define the function calls
500    if partable.form_volume_parameters:
501        refs = _call_pars("_v.", partable.form_volume_parameters)
502        call_volume = "#define CALL_VOLUME(_v) form_volume(%s)" % (",".join(refs))
503    else:
504        # Model doesn't have volume.  We could make the kernel run a little
505        # faster by not using/transferring the volume normalizations, but
506        # the ifdef's reduce readability more than is worthwhile.
507        call_volume = "#define CALL_VOLUME(v) 1.0"
508    source.append(call_volume)
509
510    refs = ["_q[_i]"] + _call_pars("_v.", partable.iq_parameters)
511    call_iq = "#define CALL_IQ(_q,_i,_v) Iq(%s)" % (",".join(refs))
512    if _have_Iqxy(user_code):
513        # Call 2D model
514        refs = ["q[2*_i]", "q[2*_i+1]"] + _call_pars("_v.", partable.iqxy_parameters)
515        call_iqxy = "#define CALL_IQ(_q,_i,_v) Iqxy(%s)" % (",".join(refs))
516    else:
517        # Call 1D model with sqrt(qx^2 + qy^2)
518        warnings.warn("Creating Iqxy = Iq(sqrt(qx^2 + qy^2))")
519        # still defined:: refs = ["q[i]"] + _call_pars("v", iq_parameters)
520        pars_sqrt = ["sqrt(_q[2*_i]*_q[2*_i]+_q[2*_i+1]*_q[2*_i+1])"] + refs[1:]
521        call_iqxy = "#define CALL_IQ(_q,_i,_v) Iq(%s)" % (",".join(pars_sqrt))
522
523    # Fill in definitions for numbers of parameters
524    source.append("#define MAX_PD %s"%partable.max_pd)
525    source.append("#define NPARS %d"%partable.npars)
526
527    # TODO: allow mixed python/opencl kernels?
528
529    source.append("#if defined(USE_OPENCL)")
530    source.extend(_add_kernels(ocl_code, call_iq, call_iqxy, model_info.name))
531    source.append("#else /* !USE_OPENCL */")
532    source.extend(_add_kernels(dll_code, call_iq, call_iqxy, model_info.name))
533    source.append("#endif /* !USE_OPENCL */")
534    return '\n'.join(source)
535
536def _add_kernels(kernel_code, call_iq, call_iqxy, name):
537    # type: (str, str, str, str) -> List[str]
538    source = [
539        # define the Iq kernel
540        "#define KERNEL_NAME %s_Iq"%name,
541        call_iq,
542        kernel_code,
543        "#undef CALL_IQ",
544        "#undef KERNEL_NAME",
545
546        # define the Iqxy kernel from the same source with different #defines
547        "#define KERNEL_NAME %s_Iqxy"%name,
548        call_iqxy,
549        kernel_code,
550        "#undef CALL_IQ",
551        "#undef KERNEL_NAME",
552    ]
553    return source
554
555def load_kernel_module(model_name):
556    # type: (str) -> module
557    """
558    Return the kernel module named in *model_name*.
559
560    If the name ends in *.py* then load it as a custom model using
561    :func:`sasmodels.custom.load_custom_kernel_module`, otherwise
562    load it from :mod:`sasmodels.models`.
563    """
564    if model_name.endswith('.py'):
565        kernel_module = load_custom_kernel_module(model_name)
566    else:
567        from sasmodels import models
568        __import__('sasmodels.models.'+model_name)
569        kernel_module = getattr(models, model_name, None)
570    return kernel_module
571
572
573
574section_marker = re.compile(r'\A(?P<first>[%s])(?P=first)*\Z'
575                            %re.escape(string.punctuation))
576def _convert_section_titles_to_boldface(lines):
577    # type: (Sequence[str]) -> Iterator[str]
578    """
579    Do the actual work of identifying and converting section headings.
580    """
581    prior = None
582    for line in lines:
583        if prior is None:
584            prior = line
585        elif section_marker.match(line):
586            if len(line) >= len(prior):
587                yield "".join(("**", prior, "**"))
588                prior = None
589            else:
590                yield prior
591                prior = line
592        else:
593            yield prior
594            prior = line
595    if prior is not None:
596        yield prior
597
598def convert_section_titles_to_boldface(s):
599    # type: (str) -> str
600    """
601    Use explicit bold-face rather than section headings so that the table of
602    contents is not polluted with section names from the model documentation.
603
604    Sections are identified as the title line followed by a line of punctuation
605    at least as long as the title line.
606    """
607    return "\n".join(_convert_section_titles_to_boldface(s.split('\n')))
608
609def make_doc(model_info):
610    # type: (ModelInfo) -> str
611    """
612    Return the documentation for the model.
613    """
614    Iq_units = "The returned value is scaled to units of |cm^-1| |sr^-1|, absolute scale."
615    Sq_units = "The returned value is a dimensionless structure factor, $S(q)$."
616    docs = convert_section_titles_to_boldface(model_info.docs)
617    pars = make_partable(model_info.parameters.COMMON
618                         + model_info.parameters.kernel_parameters)
619    subst = dict(id=model_info.id.replace('_', '-'),
620                 name=model_info.name,
621                 title=model_info.title,
622                 parameters=pars,
623                 returns=Sq_units if model_info.structure_factor else Iq_units,
624                 docs=docs)
625    return DOC_HEADER % subst
626
627
628def demo_time():
629    # type: () -> None
630    """
631    Show how long it takes to process a model.
632    """
633    import datetime
634    from .modelinfo import make_model_info
635    from .models import cylinder
636
637    tic = datetime.datetime.now()
638    make_source(make_model_info(cylinder))
639    toc = (datetime.datetime.now() - tic).total_seconds()
640    print("time: %g"%toc)
641
642def main():
643    # type: () -> None
644    """
645    Program which prints the source produced by the model.
646    """
647    import sys
648    from .modelinfo import make_model_info
649
650    if len(sys.argv) <= 1:
651        print("usage: python -m sasmodels.generate modelname")
652    else:
653        name = sys.argv[1]
654        kernel_module = load_kernel_module(name)
655        model_info = make_model_info(kernel_module)
656        source = make_source(model_info)
657        print(source)
658
659if __name__ == "__main__":
660    main()
Note: See TracBrowser for help on using the repository browser.