[17bbadd] | 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 |
---|
| 14 | |
---|
[6d6508e] | 15 | from .details import dispersion_mesh |
---|
[f619de7] | 16 | from .modelinfo import suffix_parameter, ParameterTable, ModelInfo |
---|
| 17 | from .kernel import KernelModel, Kernel |
---|
| 18 | |
---|
| 19 | try: |
---|
| 20 | from typing import Tuple |
---|
| 21 | from .modelinfo import ParameterSet |
---|
| 22 | from .details import CallDetails |
---|
| 23 | except ImportError: |
---|
| 24 | pass |
---|
[17bbadd] | 25 | |
---|
[6d6508e] | 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 | #] |
---|
[17bbadd] | 31 | |
---|
[3c6d5bc] | 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. |
---|
[17bbadd] | 34 | def make_product_info(p_info, s_info): |
---|
[f619de7] | 35 | # type: (ModelInfo, ModelInfo) -> ModelInfo |
---|
[17bbadd] | 36 | """ |
---|
| 37 | Create info block for product model. |
---|
| 38 | """ |
---|
[f619de7] | 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) |
---|
[6d6508e] | 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 |
---|
[f619de7] | 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 |
---|
[6d6508e] | 50 | else: |
---|
[f619de7] | 51 | combined_pars = p_pars.kernel_parameters + s_pars.kernel_parameters |
---|
| 52 | parameters = ParameterTable(combined_pars) |
---|
[6d6508e] | 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" |
---|
[f619de7] | 62 | model_info.parameters = parameters |
---|
[6d6508e] | 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 = [] |
---|
[fcd7bbd] | 68 | # Iq, Iqxy, form_volume, ER, VR and sesans |
---|
[6d6508e] | 69 | model_info.composition = ('product', [p_info, s_info]) |
---|
[17bbadd] | 70 | return model_info |
---|
| 71 | |
---|
[f619de7] | 72 | class ProductModel(KernelModel): |
---|
[72a081d] | 73 | def __init__(self, model_info, P, S): |
---|
[f619de7] | 74 | # type: (ModelInfo, KernelModel, KernelModel) -> None |
---|
[72a081d] | 75 | self.info = model_info |
---|
[17bbadd] | 76 | self.P = P |
---|
| 77 | self.S = S |
---|
| 78 | |
---|
| 79 | def __call__(self, q_vectors): |
---|
[f619de7] | 80 | # type: (List[np.ndarray]) -> Kernel |
---|
[17bbadd] | 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). |
---|
[f619de7] | 87 | p_kernel = self.P.make_kernel(q_vectors) |
---|
| 88 | s_kernel = self.S.make_kernel(q_vectors) |
---|
[17bbadd] | 89 | return ProductKernel(self.info, p_kernel, s_kernel) |
---|
| 90 | |
---|
| 91 | def release(self): |
---|
[f619de7] | 92 | # type: (None) -> None |
---|
[17bbadd] | 93 | """ |
---|
| 94 | Free resources associated with the model. |
---|
| 95 | """ |
---|
| 96 | self.P.release() |
---|
| 97 | self.S.release() |
---|
| 98 | |
---|
| 99 | |
---|
[f619de7] | 100 | class ProductKernel(Kernel): |
---|
[17bbadd] | 101 | def __init__(self, model_info, p_kernel, s_kernel): |
---|
[f619de7] | 102 | # type: (ModelInfo, Kernel, Kernel) -> None |
---|
[17bbadd] | 103 | self.info = model_info |
---|
| 104 | self.p_kernel = p_kernel |
---|
| 105 | self.s_kernel = s_kernel |
---|
| 106 | |
---|
[6d6508e] | 107 | def __call__(self, details, weights, values, cutoff): |
---|
[f619de7] | 108 | # type: (CallDetails, np.ndarray, np.ndarray, float) -> np.ndarray |
---|
[17bbadd] | 109 | effect_radius, vol_ratio = call_ER_VR(self.p_kernel.info, vol_pars) |
---|
| 110 | |
---|
| 111 | p_fixed[SCALE] = s_volfraction |
---|
| 112 | p_fixed[BACKGROUND] = 0.0 |
---|
| 113 | s_fixed[SCALE] = scale |
---|
| 114 | s_fixed[BACKGROUND] = 0.0 |
---|
[35b4c47] | 115 | s_fixed[2] = s_volfraction/vol_ratio |
---|
| 116 | s_pd[0] = [effect_radius], [1.0] |
---|
[17bbadd] | 117 | |
---|
[6d6508e] | 118 | p_res = self.p_kernel(p_details, p_weights, p_values, cutoff) |
---|
| 119 | s_res = self.s_kernel(s_details, s_weights, s_values, cutoff) |
---|
[35b4c47] | 120 | #print s_fixed, s_pd, p_fixed, p_pd |
---|
[17bbadd] | 121 | |
---|
| 122 | return p_res*s_res + background |
---|
| 123 | |
---|
| 124 | def release(self): |
---|
[f619de7] | 125 | # type: () -> None |
---|
[17bbadd] | 126 | self.p_kernel.release() |
---|
[f619de7] | 127 | self.s_kernel.release() |
---|
[17bbadd] | 128 | |
---|
[f619de7] | 129 | def call_ER_VR(model_info, pars): |
---|
[6d6508e] | 130 | """ |
---|
| 131 | Return effect radius and volume ratio for the model. |
---|
| 132 | """ |
---|
[f619de7] | 133 | if model_info.ER is None and model_info.VR is None: |
---|
| 134 | return 1.0, 1.0 |
---|
| 135 | |
---|
| 136 | value, weight = _vol_pars(model_info, pars) |
---|
[6d6508e] | 137 | |
---|
[f619de7] | 138 | if model_info.ER is not None: |
---|
| 139 | individual_radii = model_info.ER(*value) |
---|
| 140 | effect_radius = np.sum(weight*individual_radii) / np.sum(weight) |
---|
| 141 | else: |
---|
| 142 | effect_radius = 1.0 |
---|
| 143 | |
---|
| 144 | if model_info.VR is not None: |
---|
| 145 | whole, part = model_info.VR(*value) |
---|
| 146 | volume_ratio = np.sum(weight*part)/np.sum(weight*whole) |
---|
| 147 | else: |
---|
| 148 | volume_ratio = 1.0 |
---|
[6d6508e] | 149 | |
---|
| 150 | return effect_radius, volume_ratio |
---|
[f619de7] | 151 | |
---|
| 152 | def _vol_pars(model_info, pars): |
---|
| 153 | # type: (ModelInfo, ParameterSet) -> Tuple[np.ndarray, np.ndarray] |
---|
| 154 | vol_pars = [get_weights(p, pars) |
---|
| 155 | for p in model_info.parameters.call_parameters |
---|
| 156 | if p.type == 'volume'] |
---|
| 157 | value, weight = dispersion_mesh(model_info, vol_pars) |
---|
| 158 | return value, weight |
---|
| 159 | |
---|