- Timestamp:
- Oct 30, 2018 8:56:38 AM (6 years ago)
- Branches:
- master
- Children:
- 4e96703
- Parents:
- 1657e21 (diff), c6084f1 (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. - Location:
- sasmodels
- Files:
-
- 1 added
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/__init__.py
re65c3ba ra1ec908 14 14 defining new models. 15 15 """ 16 __version__ = "0.9 7"16 __version__ = "0.98" 17 17 18 18 def data_files(): -
sasmodels/compare.py
r01dba26 r12f77e9 369 369 370 370 # Limit magnetic SLDs to a smaller range, from zero to iron=5/A^2 371 if par.name. startswith('M0:'):371 if par.name.endswith('_M0'): 372 372 return np.random.uniform(0, 5) 373 373 … … 539 539 magnetic_pars = [] 540 540 for p in parameters.user_parameters(pars, is2d): 541 if any(p.id. startswith(x) for x in ('M0:', 'mtheta:', 'mphi:')):541 if any(p.id.endswith(x) for x in ('_M0', '_mtheta', '_mphi')): 542 542 continue 543 543 if p.id.startswith('up:'): … … 551 551 pdtype=pars.get(p.id+"_pd_type", 'gaussian'), 552 552 relative_pd=p.relative_pd, 553 M0=pars.get( 'M0:'+p.id, 0.),554 mphi=pars.get( 'mphi:'+p.id, 0.),555 mtheta=pars.get( 'mtheta:'+p.id, 0.),553 M0=pars.get(p.id+'_M0', 0.), 554 mphi=pars.get(p.id+'_mphi', 0.), 555 mtheta=pars.get(p.id+'_mtheta', 0.), 556 556 ) 557 557 lines.append(_format_par(p.name, **fields)) … … 619 619 if suppress: 620 620 for p in pars: 621 if p. startswith("M0:"):621 if p.endswith("_M0"): 622 622 pars[p] = 0 623 623 else: … … 625 625 first_mag = None 626 626 for p in pars: 627 if p. startswith("M0:"):627 if p.endswith("_M0"): 628 628 any_mag |= (pars[p] != 0) 629 629 if first_mag is None: -
sasmodels/convert.py
ra69d8cd r610ef23 165 165 if version == (3, 1, 2): 166 166 oldpars = _hand_convert_3_1_2_to_4_1(name, oldpars) 167 if version < (4, 2, 0): 168 oldpars = _rename_magnetic_pars(oldpars) 167 169 return oldpars 170 171 def _rename_magnetic_pars(pars): 172 """ 173 Change from M0:par to par_M0, etc. 174 """ 175 keys = list(pars.items()) 176 for k in keys: 177 if k.startswith('M0:'): 178 pars[k[3:]+'_M0'] = pars.pop(k) 179 elif k.startswith('mtheta:'): 180 pars[k[7:]+'_mtheta'] = pars.pop(k) 181 elif k.startswith('mphi:'): 182 pars[k[5:]+'_mphi'] = pars.pop(k) 183 elif k.startswith('up:'): 184 pars['up_'+k[3:]] = pars.pop(k) 185 return pars 168 186 169 187 def _hand_convert_3_1_2_to_4_1(name, oldpars): -
sasmodels/custom/__init__.py
r0f48f1e rd321747 12 12 import sys 13 13 import os 14 from os.path import basename, splitext 14 from os.path import basename, splitext, join as joinpath, exists, dirname 15 15 16 16 try: … … 18 18 from importlib.util import spec_from_file_location, module_from_spec # type: ignore 19 19 def load_module_from_path(fullname, path): 20 # type: (str, str) -> "module" 20 21 """load module from *path* as *fullname*""" 21 22 spec = spec_from_file_location(fullname, os.path.expanduser(path)) … … 27 28 import imp 28 29 def load_module_from_path(fullname, path): 30 # type: (str, str) -> "module" 29 31 """load module from *path* as *fullname*""" 30 32 # Clear out old definitions, if any … … 37 39 return module 38 40 41 _MODULE_CACHE = {} # type: Dict[str, Tuple("module", int)] 42 _MODULE_DEPENDS = {} # type: Dict[str, List[str]] 43 _MODULE_DEPENDS_STACK = [] # type: List[str] 39 44 def load_custom_kernel_module(path): 45 # type: str -> "module" 40 46 """load SAS kernel from *path* as *sasmodels.custom.modelname*""" 41 47 # Pull off the last .ext if it exists; there may be others 42 48 name = basename(splitext(path)[0]) 43 # Placing the model in the 'sasmodels.custom' name space. 44 kernel_module = load_module_from_path('sasmodels.custom.'+name, 45 os.path.expanduser(path)) 46 return kernel_module 49 path = os.path.expanduser(path) 50 51 # Reload module if necessary. 52 if need_reload(path): 53 # Assume the module file is the only dependency 54 _MODULE_DEPENDS[path] = set([path]) 55 56 # Load the module while pushing it onto the dependency stack. If 57 # this triggers any submodules, then they will add their dependencies 58 # to this module as the "working_on" parent. Pop the stack when the 59 # module is loaded. 60 _MODULE_DEPENDS_STACK.append(path) 61 module = load_module_from_path('sasmodels.custom.'+name, path) 62 _MODULE_DEPENDS_STACK.pop() 63 64 # Include external C code in the dependencies. We are looking 65 # for module.source and assuming that it is a list of C source files 66 # relative to the module itself. Any files that do not exist, 67 # such as those in the standard libraries, will be ignored. 68 # TODO: look in builtin module path for standard c sources 69 # TODO: share code with generate.model_sources 70 c_sources = getattr(module, 'source', None) 71 if isinstance(c_sources, (list, tuple)): 72 _MODULE_DEPENDS[path].update(_find_sources(path, c_sources)) 73 74 # Cache the module, and tag it with the newest timestamp 75 timestamp = max(os.path.getmtime(f) for f in _MODULE_DEPENDS[path]) 76 _MODULE_CACHE[path] = module, timestamp 77 78 #print("loading", os.path.basename(path), _MODULE_CACHE[path][1], 79 # [os.path.basename(p) for p in _MODULE_DEPENDS[path]]) 80 81 # Add path and all its dependence to the parent module, if there is one. 82 if _MODULE_DEPENDS_STACK: 83 working_on = _MODULE_DEPENDS_STACK[-1] 84 _MODULE_DEPENDS[working_on].update(_MODULE_DEPENDS[path]) 85 86 return _MODULE_CACHE[path][0] 87 88 def need_reload(path): 89 # type: str -> bool 90 """ 91 Return True if any path dependencies have a timestamp newer than the time 92 when the path was most recently loaded. 93 """ 94 # TODO: fails if a dependency has a modification time in the future 95 # If the newest dependency has a time stamp in the future, then this 96 # will be recorded as the cached time. When a second dependency 97 # is updated to the current time stamp, it will still be considered 98 # older than the current build and the reload will not be triggered. 99 # Could instead treat all future times as 0 here and in the code above 100 # which records the newest timestamp. This will force a reload when 101 # the future time is reached, but other than that should perform 102 # correctly. Probably not worth the extra code... 103 _, cache_time = _MODULE_CACHE.get(path, (None, -1)) 104 depends = _MODULE_DEPENDS.get(path, [path]) 105 #print("reload", any(cache_time < os.path.getmtime(p) for p in depends)) 106 #for f in depends: print(">>> ", f, os.path.getmtime(f)) 107 return any(cache_time < os.path.getmtime(p) for p in depends) 108 109 def _find_sources(path, source_list): 110 # type: (str, List[str]) -> List[str] 111 """ 112 Return a list of the sources relative to base file; ignore any that 113 are not found. 114 """ 115 root = dirname(path) 116 found = [] 117 for source_name in source_list: 118 source_path = joinpath(root, source_name) 119 if exists(source_path): 120 found.append(source_path) 121 return found -
sasmodels/kernelpy.py
r108e70e r12eec1e 37 37 self.info = model_info 38 38 self.dtype = np.dtype('d') 39 logger.info(" loadpython model " + self.info.name)39 logger.info("make python model " + self.info.name) 40 40 41 41 def make_kernel(self, q_vectors): -
sasmodels/model_test.py
r012cd34 r12eec1e 47 47 import sys 48 48 import unittest 49 import traceback 49 50 50 51 try: … … 74 75 # pylint: enable=unused-import 75 76 76 77 77 def make_suite(loaders, models): 78 78 # type: (List[str], List[str]) -> unittest.TestSuite … … 86 86 *models* is the list of models to test, or *["all"]* to test all models. 87 87 """ 88 ModelTestCase = _hide_model_case_from_nose()89 88 suite = unittest.TestSuite() 90 89 … … 95 94 skip = [] 96 95 for model_name in models: 97 if model_name in skip: 98 continue 99 model_info = load_model_info(model_name) 100 101 #print('------') 102 #print('found tests in', model_name) 103 #print('------') 104 105 # if ispy then use the dll loader to call pykernel 106 # don't try to call cl kernel since it will not be 107 # available in some environmentes. 108 is_py = callable(model_info.Iq) 109 110 # Some OpenCL drivers seem to be flaky, and are not producing the 111 # expected result. Since we don't have known test values yet for 112 # all of our models, we are instead going to compare the results 113 # for the 'smoke test' (that is, evaluation at q=0.1 for the default 114 # parameters just to see that the model runs to completion) between 115 # the OpenCL and the DLL. To do this, we define a 'stash' which is 116 # shared between OpenCL and DLL tests. This is just a list. If the 117 # list is empty (which it will be when DLL runs, if the DLL runs 118 # first), then the results are appended to the list. If the list 119 # is not empty (which it will be when OpenCL runs second), the results 120 # are compared to the results stored in the first element of the list. 121 # This is a horrible stateful hack which only makes sense because the 122 # test suite is thrown away after being run once. 123 stash = [] 124 125 if is_py: # kernel implemented in python 126 test_name = "%s-python"%model_name 127 test_method_name = "test_%s_python" % model_info.id 96 if model_name not in skip: 97 model_info = load_model_info(model_name) 98 _add_model_to_suite(loaders, suite, model_info) 99 100 return suite 101 102 def _add_model_to_suite(loaders, suite, model_info): 103 ModelTestCase = _hide_model_case_from_nose() 104 105 #print('------') 106 #print('found tests in', model_name) 107 #print('------') 108 109 # if ispy then use the dll loader to call pykernel 110 # don't try to call cl kernel since it will not be 111 # available in some environmentes. 112 is_py = callable(model_info.Iq) 113 114 # Some OpenCL drivers seem to be flaky, and are not producing the 115 # expected result. Since we don't have known test values yet for 116 # all of our models, we are instead going to compare the results 117 # for the 'smoke test' (that is, evaluation at q=0.1 for the default 118 # parameters just to see that the model runs to completion) between 119 # the OpenCL and the DLL. To do this, we define a 'stash' which is 120 # shared between OpenCL and DLL tests. This is just a list. If the 121 # list is empty (which it will be when DLL runs, if the DLL runs 122 # first), then the results are appended to the list. If the list 123 # is not empty (which it will be when OpenCL runs second), the results 124 # are compared to the results stored in the first element of the list. 125 # This is a horrible stateful hack which only makes sense because the 126 # test suite is thrown away after being run once. 127 stash = [] 128 129 if is_py: # kernel implemented in python 130 test_name = "%s-python"%model_info.name 131 test_method_name = "test_%s_python" % model_info.id 132 test = ModelTestCase(test_name, model_info, 133 test_method_name, 134 platform="dll", # so that 135 dtype="double", 136 stash=stash) 137 suite.addTest(test) 138 else: # kernel implemented in C 139 140 # test using dll if desired 141 if 'dll' in loaders or not use_opencl(): 142 test_name = "%s-dll"%model_info.name 143 test_method_name = "test_%s_dll" % model_info.id 128 144 test = ModelTestCase(test_name, model_info, 129 test_method_name,130 platform="dll", # so that131 dtype="double",132 stash=stash)145 test_method_name, 146 platform="dll", 147 dtype="double", 148 stash=stash) 133 149 suite.addTest(test) 134 else: # kernel implemented in C 135 136 # test using dll if desired 137 if 'dll' in loaders or not use_opencl(): 138 test_name = "%s-dll"%model_name 139 test_method_name = "test_%s_dll" % model_info.id 140 test = ModelTestCase(test_name, model_info, 141 test_method_name, 142 platform="dll", 143 dtype="double", 144 stash=stash) 145 suite.addTest(test) 146 147 # test using opencl if desired and available 148 if 'opencl' in loaders and use_opencl(): 149 test_name = "%s-opencl"%model_name 150 test_method_name = "test_%s_opencl" % model_info.id 151 # Using dtype=None so that the models that are only 152 # correct for double precision are not tested using 153 # single precision. The choice is determined by the 154 # presence of *single=False* in the model file. 155 test = ModelTestCase(test_name, model_info, 156 test_method_name, 157 platform="ocl", dtype=None, 158 stash=stash) 159 #print("defining", test_name) 160 suite.addTest(test) 161 162 return suite 150 151 # test using opencl if desired and available 152 if 'opencl' in loaders and use_opencl(): 153 test_name = "%s-opencl"%model_info.name 154 test_method_name = "test_%s_opencl" % model_info.id 155 # Using dtype=None so that the models that are only 156 # correct for double precision are not tested using 157 # single precision. The choice is determined by the 158 # presence of *single=False* in the model file. 159 test = ModelTestCase(test_name, model_info, 160 test_method_name, 161 platform="ocl", dtype=None, 162 stash=stash) 163 #print("defining", test_name) 164 suite.addTest(test) 165 163 166 164 167 def _hide_model_case_from_nose(): … … 348 351 return abs(target-actual)/shift < 1.5*10**-digits 349 352 350 def run_one(model): 351 # type: (str) -> str 352 """ 353 Run the tests for a single model, printing the results to stdout. 354 355 *model* can by a python file, which is handy for checking user defined 356 plugin models. 353 # CRUFT: old interface; should be deprecated and removed 354 def run_one(model_name): 355 # msg = "use check_model(model_info) rather than run_one(model_name)" 356 # warnings.warn(msg, category=DeprecationWarning, stacklevel=2) 357 try: 358 model_info = load_model_info(model_name) 359 except Exception: 360 output = traceback.format_exc() 361 return output 362 363 success, output = check_model(model_info) 364 return output 365 366 def check_model(model_info): 367 # type: (ModelInfo) -> str 368 """ 369 Run the tests for a single model, capturing the output. 370 371 Returns success status and the output string. 357 372 """ 358 373 # Note that running main() directly did not work from within the … … 369 384 # Build a test suite containing just the model 370 385 loaders = ['opencl'] if use_opencl() else ['dll'] 371 models = [model] 372 try: 373 suite = make_suite(loaders, models) 374 except Exception: 375 import traceback 376 stream.writeln(traceback.format_exc()) 377 return 386 suite = unittest.TestSuite() 387 _add_model_to_suite(loaders, suite, model_info) 378 388 379 389 # Warn if there are no user defined tests. … … 390 400 for test in suite: 391 401 if not test.info.tests: 392 stream.writeln("Note: %s has no user defined tests."%model )402 stream.writeln("Note: %s has no user defined tests."%model_info.name) 393 403 break 394 404 else: … … 406 416 output = stream.getvalue() 407 417 stream.close() 408 return output418 return result.wasSuccessful(), output 409 419 410 420 -
sasmodels/modelinfo.py
r7b9e4dd rbd547d0 466 466 self.is_asymmetric = any(p.name == 'psi' for p in self.kernel_parameters) 467 467 self.magnetism_index = [k for k, p in enumerate(self.call_parameters) 468 if p.id. startswith('M0:')]468 if p.id.endswith('_M0')] 469 469 470 470 self.pd_1d = set(p.name for p in self.call_parameters … … 586 586 if self.nmagnetic > 0: 587 587 full_list.extend([ 588 Parameter('up :frac_i', '', 0., [0., 1.],588 Parameter('up_frac_i', '', 0., [0., 1.], 589 589 'magnetic', 'fraction of spin up incident'), 590 Parameter('up :frac_f', '', 0., [0., 1.],590 Parameter('up_frac_f', '', 0., [0., 1.], 591 591 'magnetic', 'fraction of spin up final'), 592 Parameter('up :angle', 'degrees', 0., [0., 360.],592 Parameter('up_angle', 'degrees', 0., [0., 360.], 593 593 'magnetic', 'spin up angle'), 594 594 ]) … … 596 596 for p in slds: 597 597 full_list.extend([ 598 Parameter( 'M0:'+p.id, '1e-6/Ang^2', 0., [-np.inf, np.inf],598 Parameter(p.id+'_M0', '1e-6/Ang^2', 0., [-np.inf, np.inf], 599 599 'magnetic', 'magnetic amplitude for '+p.description), 600 Parameter( 'mtheta:'+p.id, 'degrees', 0., [-90., 90.],600 Parameter(p.id+'_mtheta', 'degrees', 0., [-90., 90.], 601 601 'magnetic', 'magnetic latitude for '+p.description), 602 Parameter( 'mphi:'+p.id, 'degrees', 0., [-180., 180.],602 Parameter(p.id+'_mphi', 'degrees', 0., [-180., 180.], 603 603 'magnetic', 'magnetic longitude for '+p.description), 604 604 ]) … … 640 640 641 641 Parameters marked as sld will automatically have a set of associated 642 magnetic parameters ( m0:p, mtheta:p, mphi:p), as well as polarization643 information (up :theta, up:frac_i, up:frac_f).642 magnetic parameters (p_M0, p_mtheta, p_mphi), as well as polarization 643 information (up_theta, up_frac_i, up_frac_f). 644 644 """ 645 645 # control parameters go first … … 668 668 result.append(expanded_pars[name]) 669 669 if is2d: 670 for tag in ' M0:', 'mtheta:', 'mphi:':671 if tag+namein expanded_pars:672 result.append(expanded_pars[ tag+name])670 for tag in '_M0', '_mtheta', '_mphi': 671 if name+tag in expanded_pars: 672 result.append(expanded_pars[name+tag]) 673 673 674 674 # Gather the user parameters in order … … 703 703 append_group(p.id) 704 704 705 if is2d and 'up :angle' in expanded_pars:705 if is2d and 'up_angle' in expanded_pars: 706 706 result.extend([ 707 expanded_pars['up :frac_i'],708 expanded_pars['up :frac_f'],709 expanded_pars['up :angle'],707 expanded_pars['up_frac_i'], 708 expanded_pars['up_frac_f'], 709 expanded_pars['up_angle'], 710 710 ]) 711 711 … … 793 793 info.structure_factor = getattr(kernel_module, 'structure_factor', False) 794 794 info.profile_axes = getattr(kernel_module, 'profile_axes', ['x', 'y']) 795 # Note: custom.load_custom_kernel_module assumes the C sources are defined 796 # by this attribute. 795 797 info.source = getattr(kernel_module, 'source', []) 796 798 info.c_code = getattr(kernel_module, 'c_code', None) … … 1014 1016 for k in range(control+1, p.length+1) 1015 1017 if p.length > 1) 1018 for p in self.parameters.kernel_parameters: 1019 if p.length > 1 and p.type == "sld": 1020 for k in range(control+1, p.length+1): 1021 base = p.id+str(k) 1022 hidden.update((base+"_M0", base+"_mtheta", base+"_mphi")) 1016 1023 return hidden -
sasmodels/models/bcc_paracrystal.py
r2d81cfe rda7b26b 1 1 r""" 2 .. warning:: This model and this model description are under review following 3 concerns raised by SasView users. If you need to use this model, 4 please email help@sasview.org for the latest situation. *The 5 SasView Developers. September 2018.* 6 2 7 Definition 3 8 ---------- … … 13 18 14 19 I(q) = \frac{\text{scale}}{V_p} V_\text{lattice} P(q) Z(q) 15 16 20 17 21 where *scale* is the volume fraction of spheres, $V_p$ is the volume of the … … 97 101 98 102 Authorship and Verification 99 --------------------------- -103 --------------------------- 100 104 101 105 * **Author:** NIST IGOR/DANSE **Date:** pre 2010 -
sasmodels/models/be_polyelectrolyte.py
ref07e95 rca77fc1 1 1 r""" 2 .. note:: Please read the Validation section below. 3 2 4 Definition 3 5 ---------- … … 11 13 12 14 I(q) = K\frac{q^2+k^2}{4\pi L_b\alpha ^2} 13 \frac{1}{1+r_{0}^ 2(q^2+k^2)(q^2-12hC_a/b^2)} + background15 \frac{1}{1+r_{0}^4(q^2+k^2)(q^2-12hC_a/b^2)} + background 14 16 15 17 k^2 = 4\pi L_b(2C_s + \alpha C_a) 16 18 17 r_{0}^2 = \frac{ 1}{\alpha \sqrt{C_a} \left( b/\sqrt{48\pi L_b}\right)}19 r_{0}^2 = \frac{b}{\alpha \sqrt{C_a 48\pi L_b}} 18 20 19 21 where 20 22 21 23 $K$ is the contrast factor for the polymer which is defined differently than in 22 other models and is given in barns where $1 barn = 10^{-24}cm^2$. $K$ is24 other models and is given in barns where 1 $barn = 10^{-24}$ $cm^2$. $K$ is 23 25 defined as: 24 26 … … 29 31 a = b_p - (v_p/v_s) b_s 30 32 31 where $b_p$ and $b_s$ are sum of the scattering lengths of the atoms 32 constituting the monomer of the polymer and the sum of the scattering lengths 33 of the atoms constituting the solvent molecules respectively, and $v_p$ and 34 $v_s$ are the partial molar volume of the polymer and the solvent respectively 35 36 $L_b$ is the Bjerrum length(|Ang|) - **Note:** This parameter needs to be 37 kept constant for a given solvent and temperature! 38 39 $h$ is the virial parameter (|Ang^3|/mol) - **Note:** See [#Borue]_ for the 40 correct interpretation of this parameter. It incorporates second and third 41 virial coefficients and can be Negative. 42 43 $b$ is the monomer length(|Ang|), $C_s$ is the concentration of monovalent 44 salt(mol/L), $\alpha$ is the ionization degree (ionization degree : ratio of 45 charged monomers to total number of monomers), $C_a$ is the polymer molar 46 concentration(mol/L), and $background$ is the incoherent background. 33 where: 34 35 - $b_p$ and $b_s$ are **sum of the scattering lengths of the atoms** 36 constituting the polymer monomer and the solvent molecules, respectively. 37 38 - $v_p$ and $v_s$ are the partial molar volume of the polymer and the 39 solvent, respectively. 40 41 - $L_b$ is the Bjerrum length (|Ang|) - **Note:** This parameter needs to be 42 kept constant for a given solvent and temperature! 43 44 - $h$ is the virial parameter (|Ang^3|) - **Note:** See [#Borue]_ for the 45 correct interpretation of this parameter. It incorporates second and third 46 virial coefficients and can be *negative*. 47 48 - $b$ is the monomer length (|Ang|). 49 50 - $C_s$ is the concentration of monovalent salt(1/|Ang^3| - internally converted from mol/L). 51 52 - $\alpha$ is the degree of ionization (the ratio of charged monomers to the total 53 number of monomers) 54 55 - $C_a$ is the polymer molar concentration (1/|Ang^3| - internally converted from mol/L) 56 57 - $background$ is the incoherent background. 47 58 48 59 For 2D data the scattering intensity is calculated in the same way as 1D, … … 52 63 53 64 q = \sqrt{q_x^2 + q_y^2} 65 66 Validation 67 ---------- 68 69 As of the last revision, this code is believed to be correct. However it 70 needs further validation and should be used with caution at this time. The 71 history of this code goes back to a 1998 implementation. It was recently noted 72 that in that implementation, while both the polymer concentration and salt 73 concentration were converted from experimental units of mol/L to more 74 dimensionally useful units of 1/|Ang^3|, only the converted version of the 75 polymer concentration was actually being used in the calculation while the 76 unconverted salt concentration (still in apparent units of mol/L) was being 77 used. This was carried through to Sasmodels as used for SasView 4.1 (though 78 the line of code converting the salt concentration to the new units was removed 79 somewhere along the line). Simple dimensional analysis of the calculation shows 80 that the converted salt concentration should be used, which the original code 81 suggests was the intention, so this has now been corrected (for SasView 4.2). 82 Once better validation has been performed this note will be removed. 54 83 55 84 References … … 66 95 67 96 * **Author:** NIST IGOR/DANSE **Date:** pre 2010 68 * **Last Modified by:** Paul Kienzle **Date:** July 24, 201669 * **Last Reviewed by:** Paul Butler and Richard Heenan **Date:** October 07, 201697 * **Last Modified by:** Paul Butler **Date:** September 25, 2018 98 * **Last Reviewed by:** Paul Butler **Date:** September 25, 2018 70 99 """ 71 100 … … 92 121 ["contrast_factor", "barns", 10.0, [-inf, inf], "", "Contrast factor of the polymer"], 93 122 ["bjerrum_length", "Ang", 7.1, [0, inf], "", "Bjerrum length"], 94 ["virial_param", "Ang^3 /mol", 12.0, [-inf, inf], "", "Virial parameter"],123 ["virial_param", "Ang^3", 12.0, [-inf, inf], "", "Virial parameter"], 95 124 ["monomer_length", "Ang", 10.0, [0, inf], "", "Monomer length"], 96 125 ["salt_concentration", "mol/L", 0.0, [-inf, inf], "", "Concentration of monovalent salt"], … … 102 131 103 132 def Iq(q, 104 contrast_factor =10.0,105 bjerrum_length =7.1,106 virial_param =12.0,107 monomer_length =10.0,108 salt_concentration =0.0,109 ionization_degree =0.05,110 polymer_concentration =0.7):133 contrast_factor, 134 bjerrum_length, 135 virial_param, 136 monomer_length, 137 salt_concentration, 138 ionization_degree, 139 polymer_concentration): 111 140 """ 112 :param q: Input q-value 113 :param contrast_factor: Contrast factor of the polymer 114 :param bjerrum_length: Bjerrum length 115 :param virial_param: Virial parameter 116 :param monomer_length: Monomer length 117 :param salt_concentration: Concentration of monovalent salt 118 :param ionization_degree: Degree of ionization 119 :param polymer_concentration: Polymer molar concentration 120 :return: 1-D intensity 141 :params: see parameter table 142 :return: 1-D form factor for polyelectrolytes in low salt 143 144 parameter names, units, default values, and behavior (volume, sld etc) are 145 defined in the parameter table. The concentrations are converted from 146 experimental mol/L to dimensionaly useful 1/A3 in first two lines 121 147 """ 122 148 123 concentration = polymer_concentration * 6.022136e-4 124 125 k_square = 4.0 * pi * bjerrum_length * (2*salt_concentration + 126 ionization_degree * concentration) 127 128 r0_square = 1.0/ionization_degree/sqrt(concentration) * \ 149 concentration_pol = polymer_concentration * 6.022136e-4 150 concentration_salt = salt_concentration * 6.022136e-4 151 152 k_square = 4.0 * pi * bjerrum_length * (2*concentration_salt + 153 ionization_degree * concentration_pol) 154 155 r0_square = 1.0/ionization_degree/sqrt(concentration_pol) * \ 129 156 (monomer_length/sqrt((48.0*pi*bjerrum_length))) 130 157 … … 133 160 134 161 term2 = 1.0 + r0_square**2 * (q**2 + k_square) * \ 135 (q**2 - (12.0 * virial_param * concentration /(monomer_length**2)))162 (q**2 - (12.0 * virial_param * concentration_pol/(monomer_length**2))) 136 163 137 164 return term1/term2 … … 174 201 175 202 # Accuracy tests based on content in test/utest_other_models.py 203 # Note that these should some day be validated beyond this self validation 204 # (circular reasoning). -- i.e. the "good value," at least for those with 205 # non zero salt concentrations, were obtained by running the current 206 # model in SasView and copying the appropriate result here. 207 # PDB -- Sep 26, 2018 176 208 [{'contrast_factor': 10.0, 177 209 'bjerrum_length': 7.1, … … 184 216 }, 0.001, 0.0948379], 185 217 186 # Additional tests with larger range of parameters187 218 [{'contrast_factor': 10.0, 188 219 'bjerrum_length': 100.0, 189 220 'virial_param': 3.0, 190 'monomer_length': 1.0,191 'salt_concentration': 10.0,192 'ionization_degree': 2.0,193 'polymer_concentration': 10.0,221 'monomer_length': 5.0, 222 'salt_concentration': 1.0, 223 'ionization_degree': 0.1, 224 'polymer_concentration': 1.0, 194 225 'background': 0.0, 195 }, 0.1, -3.75693800588],226 }, 0.1, 0.253469484], 196 227 197 228 [{'contrast_factor': 10.0, 198 229 'bjerrum_length': 100.0, 199 230 'virial_param': 3.0, 200 'monomer_length': 1.0,201 'salt_concentration': 10.0,202 'ionization_degree': 2.0,203 'polymer_concentration': 10.0,204 'background': 100.0205 }, 5.0, 100.029142149],231 'monomer_length': 5.0, 232 'salt_concentration': 1.0, 233 'ionization_degree': 0.1, 234 'polymer_concentration': 1.0, 235 'background': 1.0, 236 }, 0.05, 1.738358122], 206 237 207 238 [{'contrast_factor': 100.0, 208 239 'bjerrum_length': 10.0, 209 'virial_param': 180.0,210 'monomer_length': 1.0,240 'virial_param': 12.0, 241 'monomer_length': 10.0, 211 242 'salt_concentration': 0.1, 212 243 'ionization_degree': 0.5, 213 244 'polymer_concentration': 0.1, 214 'background': 0.0,215 }, 200., 1.80664667511e-06],245 'background': 0.01, 246 }, 0.5, 0.012881893], 216 247 ] -
sasmodels/models/fcc_paracrystal.py
r2d81cfe rda7b26b 3 3 #note - calculation requires double precision 4 4 r""" 5 .. warning:: This model and this model description are under review following 6 concerns raised by SasView users. If you need to use this model, 7 please email help@sasview.org for the latest situation. *The 8 SasView Developers. September 2018.* 9 10 Definition 11 ---------- 12 5 13 Calculates the scattering from a **face-centered cubic lattice** with 6 14 paracrystalline distortion. Thermal vibrations are considered to be … … 8 16 Paracrystalline distortion is assumed to be isotropic and characterized by 9 17 a Gaussian distribution. 10 11 Definition12 ----------13 18 14 19 The scattering intensity $I(q)$ is calculated as … … 23 28 is the paracrystalline structure factor for a face-centered cubic structure. 24 29 25 Equation (1) of the 1990 reference is used to calculate $Z(q)$, using 26 equations (23)-(25) from the 1987 paper for $Z1$, $Z2$, and $Z3$. 30 Equation (1) of the 1990 reference\ [#CIT1990]_ is used to calculate $Z(q)$, 31 using equations (23)-(25) from the 1987 paper\ [#CIT1987]_ for $Z1$, $Z2$, and 32 $Z3$. 27 33 28 34 The lattice correction (the occupied volume of the lattice) for a … … 88 94 ---------- 89 95 90 Hideki Matsuoka et. al. *Physical Review B*, 36 (1987) 1754-1765 91 (Original Paper) 96 .. [#CIT1987] Hideki Matsuoka et. al. *Physical Review B*, 36 (1987) 1754-1765 97 (Original Paper) 98 .. [#CIT1990] Hideki Matsuoka et. al. *Physical Review B*, 41 (1990) 3854 -3856 99 (Corrections to FCC and BCC lattice structure calculation) 92 100 93 Hideki Matsuoka et. al. *Physical Review B*, 41 (1990) 3854 -3856 94 (Corrections to FCC and BCC lattice structure calculation) 101 Authorship and Verification 102 --------------------------- 103 104 * **Author:** NIST IGOR/DANSE **Date:** pre 2010 105 * **Last Modified by:** Paul Butler **Date:** September 29, 2016 106 * **Last Reviewed by:** Richard Heenan **Date:** March 21, 2016 95 107 """ 96 108 -
sasmodels/models/sc_paracrystal.py
r2d81cfe rda7b26b 1 1 r""" 2 .. warning:: This model and this model description are under review following 3 concerns raised by SasView users. If you need to use this model, 4 please email help@sasview.org for the latest situation. *The 5 SasView Developers. September 2018.* 6 7 Definition 8 ---------- 9 2 10 Calculates the scattering from a **simple cubic lattice** with 3 11 paracrystalline distortion. Thermal vibrations are considered to be … … 5 13 Paracrystalline distortion is assumed to be isotropic and characterized 6 14 by a Gaussian distribution. 7 8 Definition9 ----------10 15 11 16 The scattering intensity $I(q)$ is calculated as … … 20 25 $Z(q)$ is the paracrystalline structure factor for a simple cubic structure. 21 26 22 Equation (16) of the 1987 reference is used to calculate $Z(q)$, using 23 equations (13)-(15) from the 1987 paper for Z1, Z2, and Z3. 27 Equation (16) of the 1987 reference\ [#CIT1987]_ is used to calculate $Z(q)$, 28 using equations (13)-(15) from the 1987 paper\ [#CIT1987]_ for $Z1$, $Z2$, and 29 $Z3$. 24 30 25 31 The lattice correction (the occupied volume of the lattice) for a simple cubic … … 91 97 Reference 92 98 --------- 93 Hideki Matsuoka et. al. *Physical Review B,* 36 (1987) 1754-176594 (Original Paper)95 99 96 Hideki Matsuoka et. al. *Physical Review B,* 41 (1990) 3854 -3856 97 (Corrections to FCC and BCC lattice structure calculation) 100 .. [#CIT1987] Hideki Matsuoka et. al. *Physical Review B*, 36 (1987) 1754-1765 101 (Original Paper) 102 .. [#CIT1990] Hideki Matsuoka et. al. *Physical Review B*, 41 (1990) 3854 -3856 103 (Corrections to FCC and BCC lattice structure calculation) 104 105 Authorship and Verification 106 --------------------------- 107 108 * **Author:** NIST IGOR/DANSE **Date:** pre 2010 109 * **Last Modified by:** Paul Butler **Date:** September 29, 2016 110 * **Last Reviewed by:** Richard Heenan **Date:** March 21, 2016 98 111 """ 99 112 -
sasmodels/models/spinodal.py
r475ff58 r93fe8a1 12 12 where $x=q/q_0$, $q_0$ is the peak position, $I_{max}$ is the intensity 13 13 at $q_0$ (parameterised as the $scale$ parameter), and $B$ is a flat 14 background. The spinodal wavelength is given by $2\pi/q_0$. 14 background. The spinodal wavelength, $\Lambda$, is given by $2\pi/q_0$. 15 16 The definition of $I_{max}$ in the literature varies. Hashimoto *et al* (1991) 17 define it as 18 19 .. math:: 20 I_{max} = \Lambda^3\Delta\rho^2 21 22 whereas Meier & Strobl (1987) give 23 24 .. math:: 25 I_{max} = V_z\Delta\rho^2 26 27 where $V_z$ is the volume per monomer unit. 15 28 16 29 The exponent $\gamma$ is equal to $d+1$ for off-critical concentration … … 28 41 29 42 H. Furukawa. Dynamics-scaling theory for phase-separating unmixing mixtures: 30 Growth rates of droplets and scaling properties of autocorrelation functions. 31 Physica A 123,497 (1984). 43 Growth rates of droplets and scaling properties of autocorrelation functions. 44 Physica A 123, 497 (1984). 45 46 H. Meier & G. Strobl. Small-Angle X-ray Scattering Study of Spinodal 47 Decomposition in Polystyrene/Poly(styrene-co-bromostyrene) Blends. 48 Macromolecules 20, 649-654 (1987). 49 50 T. Hashimoto, M. Takenaka & H. Jinnai. Scattering Studies of Self-Assembling 51 Processes of Polymer Blends in Spinodal Decomposition. 52 J. Appl. Cryst. 24, 457-466 (1991). 32 53 33 54 Revision History … … 35 56 36 57 * **Author:** Dirk Honecker **Date:** Oct 7, 2016 37 * **Revised:** Steve King **Date:** Sep 7, 201858 * **Revised:** Steve King **Date:** Oct 25, 2018 38 59 """ 39 60 -
sasmodels/sasview_model.py
raa25fc7 r12f77e9 67 67 #: set of defined models (standard and custom) 68 68 MODELS = {} # type: Dict[str, SasviewModelType] 69 # TODO: remove unused MODEL_BY_PATH cache once sasview no longer references it 69 70 #: custom model {path: model} mapping so we can check timestamps 70 71 MODEL_BY_PATH = {} # type: Dict[str, SasviewModelType] 72 #: Track modules that we have loaded so we can determine whether the model 73 #: has changed since we last reloaded. 74 _CACHED_MODULE = {} # type: Dict[str, "module"] 71 75 72 76 def find_model(modelname): … … 111 115 Load a custom model given the model path. 112 116 """ 113 model = MODEL_BY_PATH.get(path, None)114 if model is not None and model.timestamp == getmtime(path):115 #logger.info("Model already loaded %s", path)116 return model117 118 117 #logger.info("Loading model %s", path) 118 119 # Load the kernel module. This may already be cached by the loader, so 120 # only requires checking the timestamps of the dependents. 119 121 kernel_module = custom.load_custom_kernel_module(path) 120 if hasattr(kernel_module, 'Model'): 121 model = kernel_module.Model 122 123 # Check if the module has changed since we last looked. 124 reloaded = kernel_module != _CACHED_MODULE.get(path, None) 125 _CACHED_MODULE[path] = kernel_module 126 127 # Turn the module into a model. We need to do this in even if the 128 # model has already been loaded so that we can determine the model 129 # name and retrieve it from the MODELS cache. 130 model = getattr(kernel_module, 'Model', None) 131 if model is not None: 122 132 # Old style models do not set the name in the class attributes, so 123 133 # set it here; this name will be overridden when the object is created … … 132 142 model_info = modelinfo.make_model_info(kernel_module) 133 143 model = make_model_from_info(model_info) 134 model.timestamp = getmtime(path)135 144 136 145 # If a model name already exists and we are loading a different model, … … 148 157 _previous_name, model.name, model.filename) 149 158 150 MODELS[model.name] = model 151 MODEL_BY_PATH[path] = model 152 return model 159 # Only update the model if the module has changed 160 if reloaded or model.name not in MODELS: 161 MODELS[model.name] = model 162 163 return MODELS[model.name] 153 164 154 165 … … 377 388 hidden.add('background') 378 389 self._model_info.parameters.defaults['background'] = 0. 390 391 # Update the parameter lists to exclude any hidden parameters 392 self.magnetic_params = tuple(pname for pname in self.magnetic_params 393 if pname not in hidden) 394 self.orientation_params = tuple(pname for pname in self.orientation_params 395 if pname not in hidden) 379 396 380 397 self._persistency_dict = {} … … 791 808 return value, [value], [1.0] 792 809 810 @classmethod 811 def runTests(cls): 812 """ 813 Run any tests built into the model and captures the test output. 814 815 Returns success flag and output 816 """ 817 from .model_test import check_model 818 return check_model(cls._model_info) 819 793 820 def test_cylinder(): 794 821 # type: () -> float … … 878 905 Model = _make_standard_model('sphere') 879 906 model = Model() 880 model.setParam(' M0:sld', 8)907 model.setParam('sld_M0', 8) 881 908 q = np.linspace(-0.35, 0.35, 500) 882 909 qx, qy = np.meshgrid(q, q) -
sasmodels/special.py
rdf69efa rfba9ca0 113 113 The standard math function, tgamma(x) is unstable for $x < 1$ 114 114 on some platforms. 115 116 sas_gammaln(x): 117 log gamma function sas_gammaln\ $(x) = \log \Gamma(|x|)$. 118 119 The standard math function, lgamma(x), is incorrect for single 120 precision on some platforms. 121 122 sas_gammainc(a, x), sas_gammaincc(a, x): 123 Incomplete gamma function 124 sas_gammainc\ $(a, x) = \int_0^x t^{a-1}e^{-t}\,dt / \Gamma(a)$ 125 and complementary incomplete gamma function 126 sas_gammaincc\ $(a, x) = \int_x^\infty t^{a-1}e^{-t}\,dt / \Gamma(a)$ 115 127 116 128 sas_erf(x), sas_erfc(x): … … 207 219 from numpy import pi, nan, inf 208 220 from scipy.special import gamma as sas_gamma 221 from scipy.special import gammaln as sas_gammaln 222 from scipy.special import gammainc as sas_gammainc 223 from scipy.special import gammaincc as sas_gammaincc 209 224 from scipy.special import erf as sas_erf 210 225 from scipy.special import erfc as sas_erfc -
sasmodels/weights.py
r3d58247 rf41027b 231 231 )) 232 232 233 SAS_WEIGHTS_PATH = "~/.sasview/weights" 234 def load_weights(pattern=None): 235 # type: (str) -> None 236 """ 237 Load dispersion distributions matching the given glob pattern 238 """ 239 import logging 240 import os 241 import os.path 242 import glob 243 import traceback 244 from .custom import load_custom_kernel_module 245 if pattern is None: 246 path = os.environ.get("SAS_WEIGHTS_PATH", SAS_WEIGHTS_PATH) 247 pattern = os.path.join(path, "*.py") 248 for filename in sorted(glob.glob(os.path.expanduser(pattern))): 249 try: 250 #print("loading weights from", filename) 251 module = load_custom_kernel_module(filename) 252 MODELS[module.Dispersion.type] = module.Dispersion 253 except Exception as exc: 254 logging.error(traceback.format_exc(exc)) 233 255 234 256 def get_weights(disperser, n, width, nsigmas, value, limits, relative):
Note: See TracChangeset
for help on using the changeset viewer.