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