Changeset 7891a2a in sasmodels for sasmodels/generate.py


Ignore:
Timestamp:
May 9, 2016 9:36:28 AM (8 years ago)
Author:
Paul Kienzle <pkienzle@…>
Branches:
master, core_shell_microgels, costrafo411, magnetic_model, release_v0.94, release_v0.95, ticket-1257-vesicle-product, ticket_1156, ticket_1265_superball, ticket_822_more_unit_tests
Children:
a326b8e
Parents:
24d5b30 (diff), 7bf4757 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Paul Kienzle <pkienzle@…> (05/09/16 09:34:57)
git-committer:
Paul Kienzle <pkienzle@…> (05/09/16 09:36:28)
Message:

Merge branch 'master' into polydisp

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sasmodels/generate.py

    rda63656 r7891a2a  
    159159from __future__ import print_function 
    160160 
    161 #TODO: determine which functions are useful outside of generate 
     161# TODO: determine which functions are useful outside of generate 
    162162#__all__ = ["model_info", "make_doc", "make_source", "convert_type"] 
    163163 
    164 from os.path import abspath, dirname, join as joinpath, exists, getmtime 
     164import sys 
     165from os.path import abspath, dirname, join as joinpath, exists, isdir, getmtime 
    165166import re 
    166167import string 
     
    178179    pass 
    179180 
    180 TEMPLATE_ROOT = dirname(__file__) 
     181SIBLING_DIR = 'sasmodels-data' 
     182PACKAGE_PATH = abspath(dirname(__file__)) 
     183SIBLING_PATH = abspath(joinpath(PACKAGE_PATH, '..', 'sasmodels-data')) 
     184DATA_PATH = SIBLING_PATH if isdir(SIBLING_PATH) else PACKAGE_PATH 
     185MODEL_PATH = joinpath(DATA_PATH, 'models') 
    181186 
    182187F16 = np.dtype('float16') 
     
    232237""" 
    233238 
     239 
    234240def format_units(units): 
    235241    # type: (str) -> str 
     
    238244    """ 
    239245    return "string" if isinstance(units, list) else RST_UNITS.get(units, units) 
     246 
    240247 
    241248def make_partable(pars): 
     
    270277    return "\n".join(lines) 
    271278 
     279 
    272280def _search(search_path, filename): 
    273281    # type: (List[str], str) -> str 
     
    289297    Return a list of the sources file paths for the module. 
    290298    """ 
    291     search_path = [dirname(model_info.filename), 
    292                    abspath(joinpath(dirname(__file__), 'models'))] 
     299    search_path = [dirname(model_info.filename), MODEL_PATH] 
    293300    return [_search(search_path, f) for f in model_info.source] 
     301 
    294302 
    295303def timestamp(model_info): 
     
    307315    newest = max(getmtime(f) for f in source_files) 
    308316    return newest 
     317 
    309318 
    310319def model_templates(): 
     
    314323    # Note: kernel_iq.cl is not on this list because changing it need not 
    315324    # trigger a recompile of the dll. 
    316     return [joinpath(TEMPLATE_ROOT, filename) 
     325    return [joinpath(DATA_PATH, filename) 
    317326            for filename in ('kernel_header.c', 'kernel_iq.c')] 
     327 
    318328 
    319329def convert_type(source, dtype): 
     
    338348        source = _convert_type(source, "long double", "L") 
    339349    else: 
    340         raise ValueError("Unexpected dtype in source conversion: %s"%dtype) 
    341     return ("#define FLOAT_SIZE %d\n"%fbytes)+source 
     350        raise ValueError("Unexpected dtype in source conversion: %s" % dtype) 
     351    return ("#define FLOAT_SIZE %d\n" % fbytes)+source 
    342352 
    343353 
     
    382392def load_template(filename): 
    383393    # type: (str) -> str 
    384     path = joinpath(TEMPLATE_ROOT, filename) 
     394    path = joinpath(DATA_PATH, filename) 
    385395    mtime = getmtime(path) 
    386396    if filename not in _template_cache or mtime > _template_cache[filename][0]: 
     
    398408 
    399409""" 
    400  
    401410def _gen_fn(name, pars, body, filename, line): 
    402411    # type: (str, List[Parameter], str, str, int) -> str 
     
    417426    } 
    418427 
     428 
    419429def _call_pars(prefix, pars): 
    420430    # type: (str, List[Parameter]) -> List[str] 
     
    423433    """ 
    424434    return [p.as_call_reference(prefix) for p in pars] 
     435 
    425436 
    426437_IQXY_PATTERN = re.compile("^((inline|static) )? *(double )? *Iqxy *([(]|$)", 
     
    448459        return False 
    449460 
     461 
    450462def _add_source(source, code, path): 
    451463    """ 
    452464    Add a file to the list of source code chunks, tagged with path and line. 
    453465    """ 
    454     path = path.replace('\\','\\\\') 
    455     source.append('#line 1 "%s"'%path) 
     466    path = path.replace('\\', '\\\\') 
     467    source.append('#line 1 "%s"' % path) 
    456468    source.append(code) 
     469 
    457470 
    458471def make_source(model_info): 
     
    477490 
    478491    partable = model_info.parameters 
    479  
    480     # Identify parameters for Iq, Iqxy, Iq_magnetic and form_volume. 
    481     # Note that scale and volume are not possible types. 
    482492 
    483493    # Load templates and user code 
     
    518528    if partable.form_volume_parameters: 
    519529        refs = _call_pars("_v.", partable.form_volume_parameters) 
    520         call_volume = "#define CALL_VOLUME(_v) form_volume(%s)" % (",".join(refs)) 
     530        call_volume = "#define CALL_VOLUME(_v) form_volume(%s)"%(",".join(refs)) 
    521531    else: 
    522532        # Model doesn't have volume.  We could make the kernel run a little 
     
    552562    return '\n'.join(source) 
    553563 
     564 
    554565def _add_kernels(kernel_code, call_iq, call_iqxy, name): 
    555566    # type: (str, str, str, str) -> List[str] 
     
    571582    return source 
    572583 
     584 
    573585def load_kernel_module(model_name): 
    574586    # type: (str) -> module 
     
    588600    return kernel_module 
    589601 
     602 
    590603section_marker = re.compile(r'\A(?P<first>[%s])(?P=first)*\Z' 
    591                             %re.escape(string.punctuation)) 
     604                            % re.escape(string.punctuation)) 
    592605def _convert_section_titles_to_boldface(lines): 
    593606    # type: (Sequence[str]) -> Iterator[str] 
     
    612625        yield prior 
    613626 
     627 
    614628def convert_section_titles_to_boldface(s): 
    615629    # type: (str) -> str 
     
    622636    """ 
    623637    return "\n".join(_convert_section_titles_to_boldface(s.split('\n'))) 
     638 
    624639 
    625640def make_doc(model_info): 
     
    656671    print("time: %g"%toc) 
    657672 
     673 
    658674def main(): 
    659675    # type: () -> None 
     
    673689        print(source) 
    674690 
     691 
    675692if __name__ == "__main__": 
    676693    main() 
Note: See TracChangeset for help on using the changeset viewer.