Changeset 7891a2a in sasmodels for sasmodels/generate.py
- Timestamp:
- May 9, 2016 9:36:28 AM (8 years ago)
- 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)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/generate.py
rda63656 r7891a2a 159 159 from __future__ import print_function 160 160 161 # TODO: determine which functions are useful outside of generate161 # TODO: determine which functions are useful outside of generate 162 162 #__all__ = ["model_info", "make_doc", "make_source", "convert_type"] 163 163 164 from os.path import abspath, dirname, join as joinpath, exists, getmtime 164 import sys 165 from os.path import abspath, dirname, join as joinpath, exists, isdir, getmtime 165 166 import re 166 167 import string … … 178 179 pass 179 180 180 TEMPLATE_ROOT = dirname(__file__) 181 SIBLING_DIR = 'sasmodels-data' 182 PACKAGE_PATH = abspath(dirname(__file__)) 183 SIBLING_PATH = abspath(joinpath(PACKAGE_PATH, '..', 'sasmodels-data')) 184 DATA_PATH = SIBLING_PATH if isdir(SIBLING_PATH) else PACKAGE_PATH 185 MODEL_PATH = joinpath(DATA_PATH, 'models') 181 186 182 187 F16 = np.dtype('float16') … … 232 237 """ 233 238 239 234 240 def format_units(units): 235 241 # type: (str) -> str … … 238 244 """ 239 245 return "string" if isinstance(units, list) else RST_UNITS.get(units, units) 246 240 247 241 248 def make_partable(pars): … … 270 277 return "\n".join(lines) 271 278 279 272 280 def _search(search_path, filename): 273 281 # type: (List[str], str) -> str … … 289 297 Return a list of the sources file paths for the module. 290 298 """ 291 search_path = [dirname(model_info.filename), 292 abspath(joinpath(dirname(__file__), 'models'))] 299 search_path = [dirname(model_info.filename), MODEL_PATH] 293 300 return [_search(search_path, f) for f in model_info.source] 301 294 302 295 303 def timestamp(model_info): … … 307 315 newest = max(getmtime(f) for f in source_files) 308 316 return newest 317 309 318 310 319 def model_templates(): … … 314 323 # Note: kernel_iq.cl is not on this list because changing it need not 315 324 # trigger a recompile of the dll. 316 return [joinpath( TEMPLATE_ROOT, filename)325 return [joinpath(DATA_PATH, filename) 317 326 for filename in ('kernel_header.c', 'kernel_iq.c')] 327 318 328 319 329 def convert_type(source, dtype): … … 338 348 source = _convert_type(source, "long double", "L") 339 349 else: 340 raise ValueError("Unexpected dtype in source conversion: %s" %dtype)341 return ("#define FLOAT_SIZE %d\n" %fbytes)+source350 raise ValueError("Unexpected dtype in source conversion: %s" % dtype) 351 return ("#define FLOAT_SIZE %d\n" % fbytes)+source 342 352 343 353 … … 382 392 def load_template(filename): 383 393 # type: (str) -> str 384 path = joinpath( TEMPLATE_ROOT, filename)394 path = joinpath(DATA_PATH, filename) 385 395 mtime = getmtime(path) 386 396 if filename not in _template_cache or mtime > _template_cache[filename][0]: … … 398 408 399 409 """ 400 401 410 def _gen_fn(name, pars, body, filename, line): 402 411 # type: (str, List[Parameter], str, str, int) -> str … … 417 426 } 418 427 428 419 429 def _call_pars(prefix, pars): 420 430 # type: (str, List[Parameter]) -> List[str] … … 423 433 """ 424 434 return [p.as_call_reference(prefix) for p in pars] 435 425 436 426 437 _IQXY_PATTERN = re.compile("^((inline|static) )? *(double )? *Iqxy *([(]|$)", … … 448 459 return False 449 460 461 450 462 def _add_source(source, code, path): 451 463 """ 452 464 Add a file to the list of source code chunks, tagged with path and line. 453 465 """ 454 path = path.replace('\\', '\\\\')455 source.append('#line 1 "%s"' %path)466 path = path.replace('\\', '\\\\') 467 source.append('#line 1 "%s"' % path) 456 468 source.append(code) 469 457 470 458 471 def make_source(model_info): … … 477 490 478 491 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.482 492 483 493 # Load templates and user code … … 518 528 if partable.form_volume_parameters: 519 529 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)) 521 531 else: 522 532 # Model doesn't have volume. We could make the kernel run a little … … 552 562 return '\n'.join(source) 553 563 564 554 565 def _add_kernels(kernel_code, call_iq, call_iqxy, name): 555 566 # type: (str, str, str, str) -> List[str] … … 571 582 return source 572 583 584 573 585 def load_kernel_module(model_name): 574 586 # type: (str) -> module … … 588 600 return kernel_module 589 601 602 590 603 section_marker = re.compile(r'\A(?P<first>[%s])(?P=first)*\Z' 591 % re.escape(string.punctuation))604 % re.escape(string.punctuation)) 592 605 def _convert_section_titles_to_boldface(lines): 593 606 # type: (Sequence[str]) -> Iterator[str] … … 612 625 yield prior 613 626 627 614 628 def convert_section_titles_to_boldface(s): 615 629 # type: (str) -> str … … 622 636 """ 623 637 return "\n".join(_convert_section_titles_to_boldface(s.split('\n'))) 638 624 639 625 640 def make_doc(model_info): … … 656 671 print("time: %g"%toc) 657 672 673 658 674 def main(): 659 675 # type: () -> None … … 673 689 print(source) 674 690 691 675 692 if __name__ == "__main__": 676 693 main()
Note: See TracChangeset
for help on using the changeset viewer.