source: sasmodels/sasmodels/product.py @ 263daec

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

refactor: move dispersion_mesh alongside build_details in kernel.py

  • Property mode set to 100644
File size: 5.4 KB
Line 
1"""
2Product model
3-------------
4
5The product model multiplies the structure factor by the form factor,
6modulated by the effective radius of the form.  The resulting model
7has a attributes of both the model description (with parameters, etc.)
8and the module evaluator (with call, release, etc.).
9
10To use it, first load form factor P and structure factor S, then create
11*ProductModel(P, S)*.
12"""
13import numpy as np  # type: ignore
14
15from .modelinfo import suffix_parameter, ParameterTable, ModelInfo
16from .kernel import KernelModel, Kernel, dispersion_mesh
17
18try:
19    from typing import Tuple
20    from .modelinfo import ParameterSet
21    from .details import CallDetails
22except ImportError:
23    pass
24
25# TODO: make estimates available to constraints
26#ESTIMATED_PARAMETERS = [
27#    ["est_effect_radius", "A", 0.0, [0, np.inf], "", "Estimated effective radius"],
28#    ["est_volume_ratio", "", 1.0, [0, np.inf], "", "Estimated volume ratio"],
29#]
30
31# TODO: core_shell_sphere model has suppressed the volume ratio calculation
32# revert it after making VR and ER available at run time as constraints.
33def make_product_info(p_info, s_info):
34    # type: (ModelInfo, ModelInfo) -> ModelInfo
35    """
36    Create info block for product model.
37    """
38    p_id, p_name, p_pars = p_info.id, p_info.name, p_info.parameters
39    s_id, s_name, s_pars = s_info.id, s_info.name, s_info.parameters
40    p_set = set(p.id for p in p_pars.call_parameters)
41    s_set = set(p.id for p in s_pars.call_parameters)
42
43    if p_set & s_set:
44        # there is some overlap between the parameter names; tag the
45        # overlapping S parameters with name_S
46        s_list = [(suffix_parameter(par, "_S") if par.id in p_set else par)
47                  for par in s_pars.kernel_parameters]
48        combined_pars = p_pars.kernel_parameters + s_list
49    else:
50        combined_pars = p_pars.kernel_parameters + s_pars.kernel_parameters
51    parameters = ParameterTable(combined_pars)
52
53    model_info = ModelInfo()
54    model_info.id = '*'.join((p_id, s_id))
55    model_info.name = ' X '.join((p_name, s_name))
56    model_info.filename = None
57    model_info.title = 'Product of %s and %s'%(p_name, s_name)
58    model_info.description = model_info.title
59    model_info.docs = model_info.title
60    model_info.category = "custom"
61    model_info.parameters = parameters
62    #model_info.single = p_info.single and s_info.single
63    model_info.structure_factor = False
64    model_info.variant_info = None
65    #model_info.tests = []
66    #model_info.source = []
67    # Iq, Iqxy, form_volume, ER, VR and sesans
68    model_info.composition = ('product', [p_info, s_info])
69    return model_info
70
71class ProductModel(KernelModel):
72    def __init__(self, model_info, P, S):
73        # type: (ModelInfo, KernelModel, KernelModel) -> None
74        self.info = model_info
75        self.P = P
76        self.S = S
77
78    def __call__(self, q_vectors):
79        # type: (List[np.ndarray]) -> Kernel
80        # Note: may be sending the q_vectors to the GPU twice even though they
81        # are only needed once.  It would mess up modularity quite a bit to
82        # handle this optimally, especially since there are many cases where
83        # separate q vectors are needed (e.g., form in python and structure
84        # in opencl; or both in opencl, but one in single precision and the
85        # other in double precision).
86        p_kernel = self.P.make_kernel(q_vectors)
87        s_kernel = self.S.make_kernel(q_vectors)
88        return ProductKernel(self.info, p_kernel, s_kernel)
89
90    def release(self):
91        # type: (None) -> None
92        """
93        Free resources associated with the model.
94        """
95        self.P.release()
96        self.S.release()
97
98
99class ProductKernel(Kernel):
100    def __init__(self, model_info, p_kernel, s_kernel):
101        # type: (ModelInfo, Kernel, Kernel) -> None
102        self.info = model_info
103        self.p_kernel = p_kernel
104        self.s_kernel = s_kernel
105
106    def __call__(self, details, weights, values, cutoff):
107        # type: (CallDetails, np.ndarray, np.ndarray, float) -> np.ndarray
108        effect_radius, vol_ratio = call_ER_VR(self.p_kernel.info, vol_pars)
109
110        p_details, s_details = details.parts
111        p_res = self.p_kernel(p_details, weights, values, cutoff)
112        s_res = self.s_kernel(s_details, weights, values, cutoff)
113        #print s_fixed, s_pd, p_fixed, p_pd
114
115        return values[0]*(p_res*s_res) + values[1]
116
117    def release(self):
118        # type: () -> None
119        self.p_kernel.release()
120        self.s_kernel.release()
121
122def call_ER_VR(model_info, pars):
123    """
124    Return effect radius and volume ratio for the model.
125    """
126    if model_info.ER is None and model_info.VR is None:
127        return 1.0, 1.0
128
129    value, weight = _vol_pars(model_info, pars)
130
131    if model_info.ER is not None:
132        individual_radii = model_info.ER(*value)
133        effect_radius = np.sum(weight*individual_radii) / np.sum(weight)
134    else:
135        effect_radius = 1.0
136
137    if model_info.VR is not None:
138        whole, part = model_info.VR(*value)
139        volume_ratio = np.sum(weight*part)/np.sum(weight*whole)
140    else:
141        volume_ratio = 1.0
142
143    return effect_radius, volume_ratio
144
145def _vol_pars(model_info, pars):
146    # type: (ModelInfo, ParameterSet) -> Tuple[np.ndarray, np.ndarray]
147    vol_pars = [get_weights(p, pars)
148                for p in model_info.parameters.call_parameters
149                if p.type == 'volume']
150    value, weight = dispersion_mesh(model_info, vol_pars)
151    return value, weight
152
Note: See TracBrowser for help on using the repository browser.