Changeset 70534131 in sasmodels


Ignore:
Timestamp:
Mar 19, 2016 9:37:46 AM (8 years ago)
Author:
wojciech
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:
a776125
Parents:
ad9af31 (diff), 787be86 (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.
Message:

Merge branch 'master' of https://github.com/SasView/sasmodels

Files:
3 added
1 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • example/sesansfit.py

    rbfea0c2 rd459d4e  
    4242            dy = err_data 
    4343            sample = Sample() 
    44             needs_all_q = acceptance_angle is not None 
     44            has_no_finite_acceptance = acceptance_angle is not None 
    4545        data = SESANSData1D() 
    4646        data.acceptance_angle = acceptance_angle 
    4747 
    48     data.needs_all_q = acceptance_angle is not None 
     48    data.has_no_finite_acceptance = acceptance_angle is not None 
    4949    if "radius" in initial_vals: 
    5050        radius = initial_vals.get("radius") 
  • sasmodels/compare.py

    raf92b73 r72a081d  
    369369            model = MultiplicationModel(P, S) 
    370370        else: 
    371             raise ValueError("mixture models not handled yet") 
     371            raise ValueError("sasview mixture models not supported by compare") 
    372372    else: 
    373373        model = get_model(model_info['oldname']) 
     
    424424    Return a model calculator using the OpenCL calculation engine. 
    425425    """ 
    426     def builder(model_info): 
    427         try: 
    428             return core.build_model(model_info, dtype=dtype, platform="ocl") 
    429         except Exception as exc: 
    430             print(exc) 
    431             print("... trying again with single precision") 
    432             return core.build_model(model_info, dtype='single', platform="ocl") 
    433     if model_info['composition']: 
    434         composition_type, parts = model_info['composition'] 
    435         if composition_type == 'product': 
    436             P, S = [builder(p) for p in parts] 
    437             model = product.ProductModel(P, S) 
    438         else: 
    439             raise ValueError("mixture models not handled yet") 
    440     else: 
    441         model = builder(model_info) 
     426    try: 
     427        model = core.build_model(model_info, dtype=dtype, platform="ocl") 
     428    except Exception as exc: 
     429        print(exc) 
     430        print("... trying again with single precision") 
     431        model = core.build_model(model_info, dtype='single', platform="ocl") 
    442432    calculator = DirectModel(data, model, cutoff=cutoff) 
    443433    calculator.engine = "OCL%s"%DTYPE_MAP[dtype] 
     
    450440    if dtype == 'quad': 
    451441        dtype = 'longdouble' 
    452     def builder(model_info): 
    453         return core.build_model(model_info, dtype=dtype, platform="dll") 
    454  
    455     if model_info['composition']: 
    456         composition_type, parts = model_info['composition'] 
    457         if composition_type == 'product': 
    458             P, S = [builder(p) for p in parts] 
    459             model = product.ProductModel(P, S) 
    460         else: 
    461             raise ValueError("mixture models not handled yet") 
    462     else: 
    463         model = builder(model_info) 
     442    model = core.build_model(model_info, dtype=dtype, platform="dll") 
    464443    calculator = DirectModel(data, model, cutoff=cutoff) 
    465444    calculator.engine = "OMP%s"%DTYPE_MAP[dtype] 
     
    715694        print("expected parameters: model N1 N2") 
    716695 
    717     def _get_info(name): 
    718         try: 
    719             model_info = core.load_model_info(name) 
    720         except ImportError, exc: 
    721             print(str(exc)) 
    722             print("Use one of:\n    " + models) 
    723             sys.exit(1) 
    724         return model_info 
    725  
    726696    name = args[0] 
    727     if '*' in name: 
    728         parts = [_get_info(k) for k in name.split('*')] 
    729         model_info = product.make_product_info(*parts) 
    730     else: 
    731         model_info = _get_info(name) 
     697    try: 
     698        model_info = core.load_model_info(name) 
     699    except ImportError, exc: 
     700        print(str(exc)) 
     701        print("Could not find model; use one of:\n    " + models) 
     702        sys.exit(1) 
    732703 
    733704    invalid = [o[1:] for o in flags 
     
    749720        'res'       : 0.0, 
    750721        'accuracy'  : 'Low', 
    751         'cutoff'    : 1e-5, 
     722        'cutoff'    : 0.0, 
    752723        'seed'      : -1,  # default to preset 
    753724        'mono'      : False, 
  • sasmodels/core.py

    rd5ba841 r72a081d  
    33""" 
    44 
    5 from os.path import basename, dirname, join as joinpath 
     5from os.path import basename, dirname, join as joinpath, splitext 
    66from glob import glob 
     7import imp 
    78 
    89import numpy as np 
     
    1112from . import weights 
    1213from . import generate 
    13  
     14# TODO: remove circular references between product and core 
     15# product uses call_ER/call_VR, core uses make_product_info/ProductModel 
     16#from . import product 
     17from . import mixture 
    1418from . import kernelpy 
    1519from . import kerneldll 
     
    4650    Load model info and build model. 
    4751    """ 
     52    return build_model(load_model_info(model_name), **kw) 
     53 
     54def load_model_info_from_path(path): 
     55    # Pull off the last .ext if it exists; there may be others 
     56    name = basename(splitext(path)[0]) 
     57 
     58    # Not cleaning name since don't need to be able to reload this 
     59    # model later 
     60    # Should probably turf the model from sys.modules after we are done... 
     61 
     62    # Placing the model in the 'sasmodels.custom' name space, even 
     63    # though it doesn't actually exist.  imp.load_source doesn't seem 
     64    # to care. 
     65    kernel_module = imp.load_source('sasmodels.custom.'+name, path) 
     66 
     67    # Now that we have the module, we can load it as usual 
     68    model_info = generate.make_model_info(kernel_module) 
     69    return model_info 
     70 
     71def load_model_info(model_name): 
     72    """ 
     73    Load a model definition given the model name. 
     74 
     75    This returns a handle to the module defining the model.  This can be 
     76    used with functions in generate to build the docs or extract model info. 
     77    """ 
    4878    parts = model_name.split('+') 
    4979    if len(parts) > 1: 
    50         from .mixture import MixtureModel 
    51         models = [load_model(p, **kw) for p in parts] 
    52         return MixtureModel(models) 
     80        model_info_list = [load_model_info(p) for p in parts] 
     81        return mixture.make_mixture_info(model_info_list) 
    5382 
    5483    parts = model_name.split('*') 
    5584    if len(parts) > 1: 
     85        from . import product 
    5686        # Note: currently have circular reference 
    57         from .product import ProductModel 
    5887        if len(parts) > 2: 
    5988            raise ValueError("use P*S to apply structure factor S to model P") 
    60         P, Q = [load_model(p, **kw) for p in parts] 
    61         return ProductModel(P, Q) 
    62  
    63     return build_model(load_model_info(model_name), **kw) 
    64  
    65 def load_model_info(model_name): 
    66     """ 
    67     Load a model definition given the model name. 
    68  
    69     This returns a handle to the module defining the model.  This can be 
    70     used with functions in generate to build the docs or extract model info. 
    71     """ 
     89        P_info, Q_info = [load_model_info(p) for p in parts] 
     90        return product.make_product_info(P_info, Q_info) 
     91 
    7292    #import sys; print "\n".join(sys.path) 
    7393    __import__('sasmodels.models.'+model_name) 
     
    95115    otherwise it uses the default "ocl". 
    96116    """ 
    97     source = generate.make_source(model_info) 
    98     if dtype is None: 
    99         dtype = 'single' if model_info['single'] else 'double' 
    100     if callable(model_info.get('Iq', None)): 
    101         return kernelpy.PyModel(model_info) 
     117    composition = model_info.get('composition', None) 
     118    if composition is not None: 
     119        composition_type, parts = composition 
     120        models = [build_model(p, dtype=dtype, platform=platform) for p in parts] 
     121        if composition_type == 'mixture': 
     122            return mixture.MixtureModel(model_info, models) 
     123        elif composition_type == 'product': 
     124            from . import product 
     125            P, S = parts 
     126            return product.ProductModel(model_info, P, S) 
     127        else: 
     128            raise ValueError('unknown mixture type %s'%composition_type) 
    102129 
    103130    ## for debugging: 
     
    109136    # open(model_info['name']+'.c','w').write(source) 
    110137    # source = open(model_info['name']+'.cl','r').read() 
    111  
     138    source = generate.make_source(model_info) 
     139    if dtype is None: 
     140        dtype = 'single' if model_info['single'] else 'double' 
     141    if callable(model_info.get('Iq', None)): 
     142        return kernelpy.PyModel(model_info) 
    112143    if (platform == "dll" 
    113144            or not HAVE_OPENCL 
  • sasmodels/models/core_shell_cylinder.py

    rf4878dc r01eece6  
    1212.. math:: 
    1313 
    14     P(q,\alpha) = \frac{\text{scale}}{V_s} F^2(q) + \text{background} 
     14    I(q,\alpha) = \frac{\text{scale}}{V_s} F^2(q) + \text{background} 
    1515 
    1616where 
     
    5252 
    5353To provide easy access to the orientation of the core-shell cylinder, we 
    54 define the axis of the cylinder using two angles $\theta$ and $\phi$. As 
    55 for the case of the cylinder, those angles are defined in 
    56 :num:`figure #cylinder-orientation`. 
     54define the axis of the cylinder using two angles $\theta$ and $\phi$.  
     55(see :ref:`cylinder model <cylinder-angle-definition>`) 
    5756 
    5857NB: The 2nd virial coefficient of the cylinder is calculated based on 
     
    7473distribution $p(\theta,\phi) = 1.0$. 
    7574 
    76 2013/11/26 - Description reviewed by Heenan, R. 
     75Reference 
     76--------- 
     77see, for example, Ian Livsey  J. Chem. Soc., Faraday Trans. 2, 1987,83, 1445-1452 
     78 
     792016/03/18 - Description reviewed by RKH 
    7780""" 
    7881 
     
    8386description = """ 
    8487P(q,alpha)= scale/Vs*f(q)^(2) + background, 
    85       where: f(q)= 2(core_sld - solvant_sld) 
     88      where: f(q)= 2(sld_core - solvant_sld) 
    8689        * Vc*sin[qLcos(alpha/2)] 
    8790        /[qLcos(alpha/2)]*J1(qRsin(alpha)) 
    88         /[qRsin(alpha)]+2(shell_sld-solvent_sld) 
     91        /[qRsin(alpha)]+2(sld_shell-sld_solvent) 
    8992        *Vs*sin[q(L+T)cos(alpha/2)][[q(L+T) 
    9093        *cos(alpha/2)]*J1(q(R+T)sin(alpha)) 
     
    9699    Vc: the volume of the core 
    97100    L: the length of the core 
    98         shell_sld: the scattering length density of the shell 
    99     solvent_sld: the scattering length density of the solvent 
     101        sld_shell: the scattering length density of the shell 
     102    sld_solvent: the scattering length density of the solvent 
    100103    background: the background 
    101104    T: the thickness 
     
    109112 
    110113#             ["name", "units", default, [lower, upper], "type", "description"], 
    111 parameters = [["core_sld", "1e-6/Ang^2", 4, [-inf, inf], "", 
     114parameters = [["sld_core", "1e-6/Ang^2", 4, [-inf, inf], "", 
    112115               "Cylinder core scattering length density"], 
    113               ["shell_sld", "1e-6/Ang^2", 4, [-inf, inf], "", 
     116              ["sld_shell", "1e-6/Ang^2", 4, [-inf, inf], "", 
    114117               "Cylinder shell scattering length density"], 
    115               ["solvent_sld", "1e-6/Ang^2", 1, [-inf, inf], "", 
     118              ["sld_solvent", "1e-6/Ang^2", 1, [-inf, inf], "", 
    116119               "Solvent scattering length density"], 
    117120              ["radius", "Ang", 20, [0, inf], "volume", 
     
    147150 
    148151demo = dict(scale=1, background=0, 
    149             core_sld=6, shell_sld=8, solvent_sld=1, 
     152            sld_core=6, sld_shell=8, sld_solvent=1, 
    150153            radius=45, thickness=25, length=340, 
    151154            theta=30, phi=15, 
     
    156159            phi_pd=15, phi_pd_n=1) 
    157160oldname = 'CoreShellCylinderModel' 
    158 oldpars = dict(theta='axis_theta', phi='axis_phi') 
     161oldpars = dict( sld_core='core_sld', 
     162               sld_shell='shell_sld', 
     163               sld_solvent='solvent_sld', 
     164               theta='axis_theta', phi='axis_phi') 
     165# ADDED by:  RKH  ON: 18Mar2016 renamed sld's etc 
  • sasmodels/models/cylinder.py

    raa2edb2 r01eece6  
    22# Note: model title and parameter table are inserted automatically 
    33r""" 
    4 The form factor is normalized by the particle volume. 
     4The form factor is normalized by the particle volume V = \piR^2L. 
    55 
    66Definition 
     
    1919 
    2020    F(q) = 2 (\Delta \rho) V 
    21            \frac{\sin \left(q\tfrac12 L\cos\alpha \right)} 
    22                 {q\tfrac12 L \cos \alpha} 
     21           \frac{\sin \left(\tfrac12 qL\cos\alpha \right)} 
     22                {\tfrac12 qL \cos \alpha} 
    2323           \frac{J_1 \left(q R \sin \alpha\right)}{q R \sin \alpha} 
    2424 
     
    8888title = "Right circular cylinder with uniform scattering length density." 
    8989description = """ 
    90      f(q,alpha) = 2*(sld - solvent_sld)*V*sin(qLcos(alpha/2)) 
    91                 /[qLcos(alpha/2)]*J1(qRsin(alpha/2))/[qRsin(alpha)] 
     90     f(q,alpha) = 2*(sld - sld_solvent)*V*sin(qLcos(alpha)/2)) 
     91                /[qLcos(alpha)/2]*J1(qRsin(alpha))/[qRsin(alpha)] 
    9292 
    9393            P(q,alpha)= scale/V*f(q,alpha)^(2)+background 
     
    107107parameters = [["sld", "4e-6/Ang^2", 4, [-inf, inf], "", 
    108108               "Cylinder scattering length density"], 
    109               ["solvent_sld", "1e-6/Ang^2", 1, [-inf, inf], "", 
     109              ["sld_solvent", "1e-6/Ang^2", 1, [-inf, inf], "", 
    110110               "Solvent scattering length density"], 
    111111              ["radius", "Ang", 20, [0, inf], "volume", 
     
    130130# parameters for demo 
    131131demo = dict(scale=1, background=0, 
    132             sld=6, solvent_sld=1, 
     132            sld=6, sld_solvent=1, 
    133133            radius=20, length=300, 
    134134            theta=60, phi=60, 
     
    141141# names and the target sasview model name. 
    142142oldname = 'CylinderModel' 
    143 oldpars = dict(theta='cyl_theta', phi='cyl_phi', sld='sldCyl', solvent_sld='sldSolv') 
     143oldpars = dict(theta='cyl_theta', phi='cyl_phi', sld='sldCyl', sld_solvent='sldSolv') 
    144144 
    145145 
     
    151151        ] 
    152152del qx, qy  # not necessary to delete, but cleaner 
     153# ADDED by:  RKH  ON: 18Mar2016 renamed sld's etc 
  • sasmodels/models/hayter_msa_kernel.c

    rd529d93 rce43de0  
    1414   
    1515double Iq(double QQ, 
    16       double radius_effective, double zz, double VolFrac, double Temp, double csalt, double dialec)   
     16      double radius_effective, double VolFrac, double zz, double Temp, double csalt, double dialec) 
    1717{ 
    1818    double gMSAWave[17]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17}; 
  • sasmodels/models/hollow_cylinder.py

    r2f0c07d r01eece6  
    3131    J_1(x)         &= (\sin(x)-x\cdot \cos(x)) / x^2 
    3232 
    33 where *scale* is a scale factor and $J_1$ is the 1st order 
     33where *scale* is a scale factor, $H = L/2$ and $J_1$ is the 1st order 
    3434Bessel function. 
    3535 
     
    6262length = the total length of the cylinder 
    6363sld = SLD of the shell 
    64 solvent_sld = SLD of the solvent 
     64sld_solvent = SLD of the solvent 
    6565background = incoherent background 
    6666""" 
     
    7373    ["length",      "Ang",    400.0, [0, inf],    "volume",      "Cylinder length"], 
    7474    ["sld",         "1/Ang^2",  6.3, [-inf, inf], "",            "Cylinder sld"], 
    75     ["solvent_sld", "1/Ang^2",  1,   [-inf, inf], "",            "Solvent sld"], 
     75    ["sld_solvent", "1/Ang^2",  1,   [-inf, inf], "",            "Solvent sld"], 
    7676    ["theta",       "degrees", 90,   [-360, 360], "orientation", "Theta angle"], 
    7777    ["phi",         "degrees",  0,   [-360, 360], "orientation", "Phi angle"], 
     
    113113# parameters for demo 
    114114demo = dict(scale=1.0, background=0.0, length=400.0, radius=30.0, 
    115             core_radius=20.0, sld=6.3, solvent_sld=1, theta=90, phi=0, 
     115            core_radius=20.0, sld=6.3, sld_solvent=1, theta=90, phi=0, 
    116116            radius_pd=.2, radius_pd_n=9, 
    117117            length_pd=.2, length_pd_n=10, 
     
    125125oldpars = dict(scale='scale', background='background', radius='radius', 
    126126               core_radius='core_radius', sld='sldCyl', length='length', 
    127                solvent_sld='sldSolv', phi='axis_phi', theta='axis_theta') 
     127               sld_solvent='sldSolv', phi='axis_phi', theta='axis_theta') 
    128128 
    129129# Parameters for unit tests 
  • sasmodels/product.py

    rd5ba841 r72a081d  
    6868 
    6969class ProductModel(object): 
    70     def __init__(self, P, S): 
     70    def __init__(self, model_info, P, S): 
     71        self.info = model_info 
    7172        self.P = P 
    7273        self.S = S 
    73         self.info = make_product_info(P.info, S.info) 
    7474 
    7575    def __call__(self, q_vectors): 
  • sasmodels/sasview_model.py

    r08376e7 r787be86  
    1616import math 
    1717from copy import deepcopy 
    18 import warnings 
    1918import collections 
    2019 
     
    2221 
    2322from . import core 
     23from . import generate 
    2424 
    2525def standard_models(): 
    2626    return [make_class(model_name) for model_name in core.list_models()] 
    2727 
    28 def make_class(model_name, namestyle='name'): 
     28# TODO: rename to make_class_from_name and update sasview 
     29def make_class(model_name): 
    2930    """ 
    3031    Load the sasview model defined in *kernel_module*. 
     
    3738    """ 
    3839    model_info = core.load_model_info(model_name) 
     40    return make_class_from_info(model_info) 
     41 
     42def make_class_from_file(path): 
     43    model_info = core.load_model_info_from_path(path) 
     44    return make_class_from_info(model_info) 
     45 
     46def make_class_from_info(model_info): 
    3947    def __init__(self, multfactor=1): 
    4048        SasviewModel.__init__(self) 
    4149    attrs = dict(__init__=__init__, _model_info=model_info) 
    42     ConstructedModel = type(model_info[namestyle], (SasviewModel,), attrs) 
     50    ConstructedModel = type(model_info['name'], (SasviewModel,), attrs) 
    4351    return ConstructedModel 
    4452 
  • sasmodels/sesans.py

    ra06430c rd459d4e  
    11""" 
    2 Conversion of scattering cross section from SANS in absolute 
    3 units into SESANS using a Hankel transformation 
     2Conversion of scattering cross section from SANS (I(q), or rather, ds/dO) in absolute 
     3units (cm-1)into SESANS correlation function G using a Hankel transformation, then converting 
     4the SESANS correlation function into polarisation from the SESANS experiment 
    45 
    5 Everything is in units of metres except specified otherwise 
     6Everything is in units of metres except specified otherwise (NOT TRUE!!!) 
     7Everything is in conventional units (nm for spin echo length) 
    68 
    79Wim Bouwman (w.g.bouwman@tudelft.nl), June 2013 
     
    1820    r""" 
    1921    Return a $q$ vector suitable for SESANS covering from $2\pi/ (10 R_{\max})$ 
    20     to $q_max$. 
     22    to $q_max$. This is the integration range of the Hankel transform; bigger range and  
     23    more points makes a better numerical integration. 
     24    Smaller q_min will increase reliable spin echo length range.  
     25    Rmax is the "radius" of the largest expected object and can be set elsewhere. 
     26    q_max is determined by the acceptance angle of the SESANS instrument. 
    2127    """ 
    2228    q_min = dq = 0.1 * 2*pi / Rmax 
     
    2430     
    2531def make_all_q(data): 
    26     if not data.needs_all_q: 
     32    """ 
     33    Return a $q$ vector suitable for calculating the total scattering cross section for 
     34    calculating the effect of finite acceptance angles on Time of Flight SESANS instruments. 
     35    If no acceptance is given, or unwanted (set "unwanted" flag in paramfile), no all_q vector is needed. 
     36    If the instrument has a rectangular acceptance, 2 all_q vectors are needed. 
     37    If the instrument has a circular acceptance, 1 all_q vector is needed 
     38     
     39    """ 
     40    if not data.has_no_finite_acceptance: 
    2741        return [] 
    28     elif needs_Iqxy(data): 
     42    elif data.has_yz_acceptance(data): 
    2943        # compute qx, qy 
    3044        Qx, Qy = np.meshgrid(qx, qy) 
     
    3246    else: 
    3347        # else only need q 
     48        # data.has_z_acceptance 
    3449        return [q] 
    3550 
    3651def transform(data, q_calc, Iq_calc, qmono, Iq_mono): 
     52    """ 
     53    Decides which transform type is to be used, based on the experiment data file contents (header) 
     54    (2016-03-19: currently controlled from parameters script) 
     55    nqmono is the number of q vectors to be used for the detector integration 
     56    """ 
    3757    nqmono = len(qmono) 
    3858    if nqmono == 0: 
     
    6282                  q_calc, Iq_calc) 
    6383                   
    64 def Cosine2D(data, q_calc, Iq_calc, qx, qy, Iq_mono): 
     84def call_Cosine2D(data, q_calc, Iq_calc, qx, qy, Iq_mono): 
    6585    return hankel(data.x, data.y, data.lam * 1e-9, 
    6686                  data.sample.thickness / 10, 
  • sasmodels/models/bessel.py

    r95ce773 rad9af31  
    7070             ] 
    7171 
    72 source = ["lib/polevl.c", "lib/j1_cephes.c"] 
     72source = ["lib/sas_gamma.c"] 
    7373 
    7474# No volume normalization despite having a volume parameter 
     
    7878 
    7979Iq = """ 
    80     return J1c(q); 
     80    return sas_gamma(q); 
    8181    """ 
    8282 
Note: See TracChangeset for help on using the changeset viewer.