-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrownian_translational_diffusion.py
More file actions
287 lines (241 loc) · 9.69 KB
/
brownian_translational_diffusion.py
File metadata and controls
287 lines (241 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# SPDX-FileCopyrightText: 2025-2026 EasyDynamics contributors <https://github.com/easyscience>
# SPDX-License-Identifier: BSD-3-Clause
from typing import Dict
from typing import List
import numpy as np
import scipp as sc
from easyscience.variable import DescriptorNumber
from easyscience.variable import Parameter
from scipp.constants import hbar as scipp_hbar
from easydynamics.sample_model.component_collection import ComponentCollection
from easydynamics.sample_model.components import Lorentzian
from easydynamics.sample_model.diffusion_model.diffusion_model_base import DiffusionModelBase
from easydynamics.utils.utils import Numeric
from easydynamics.utils.utils import Q_type
from easydynamics.utils.utils import _validate_and_convert_Q
class BrownianTranslationalDiffusion(DiffusionModelBase):
"""Model of Brownian translational diffusion, consisting of a
Lorentzian function for each Q-value, where the width is given by
:math:`DQ^2`. Q is assumed to have units of 1/angstrom. Creates
ComponentCollections with Lorentzian components for given Q-values.
Example usage:
Q=np.linspace(0.5,2,7)
energy=np.linspace(-2, 2, 501)
scale=1.0
diffusion_coefficient = 2.4e-9 # m^2/s
diffusion_model=BrownianTranslationalDiffusion(display_name="DiffusionModel",
scale=scale, diffusion_coefficient= diffusion_coefficient)
component_collections=diffusion_model.create_component_collections(Q)
See also the examples.
"""
def __init__(
self,
display_name: str | None = 'BrownianTranslationalDiffusion',
unique_name: str | None = None,
unit: str | sc.Unit = 'meV',
scale: Numeric = 1.0,
diffusion_coefficient: Numeric = 1.0,
):
"""Initialize a new BrownianTranslationalDiffusion model.
Parameters
----------
display_name : str
Display name of the diffusion model.
unique_name : str or None
Unique name of the diffusion model. If None, a unique name
is automatically generated.
unit : str or sc.Unit, optional
Energy unit for the underlying Lorentzian components.
Defaults to "meV".
scale : float or Parameter, optional
Scale factor for the diffusion model.
diffusion_coefficient : Number, optional
Diffusion coefficient D in m^2/s.
Defaults to 1.0.
"""
if not isinstance(scale, Numeric):
raise TypeError('scale must be a number.')
if not isinstance(diffusion_coefficient, Numeric):
raise TypeError('diffusion_coefficient must be a number.')
diffusion_coefficient = Parameter(
name='diffusion_coefficient',
value=float(diffusion_coefficient),
fixed=False,
unit='m**2/s',
)
super().__init__(
display_name=display_name,
unique_name=unique_name,
unit=unit,
scale=scale,
)
self._hbar = DescriptorNumber.from_scipp('hbar', scipp_hbar)
self._angstrom = DescriptorNumber('angstrom', 1e-10, unit='m')
self._diffusion_coefficient = diffusion_coefficient
@property
def diffusion_coefficient(self) -> Parameter:
"""Get the diffusion coefficient parameter D.
Returns
-------
Parameter
Diffusion coefficient D.
"""
return self._diffusion_coefficient
@diffusion_coefficient.setter
def diffusion_coefficient(self, diffusion_coefficient: Numeric) -> None:
"""Set the diffusion coefficient parameter D."""
if not isinstance(diffusion_coefficient, (Numeric)):
raise TypeError('diffusion_coefficient must be a number.')
self._diffusion_coefficient.value = diffusion_coefficient
def calculate_width(self, Q: Q_type) -> np.ndarray:
"""Calculate the half-width at half-maximum (HWHM) for the
diffusion model.
Parameters
----------
Q : np.ndarray | Numeric | list | ArrayLike
Scattering vector in 1/angstrom
Returns
-------
np.ndarray
HWHM values in the unit of the model (e.g., meV).
"""
Q = _validate_and_convert_Q(Q)
unit_conversion_factor = self._hbar * self.diffusion_coefficient / (self._angstrom**2)
unit_conversion_factor.convert_unit(self.unit)
width = Q**2 * unit_conversion_factor.value
return width
def calculate_EISF(self, Q: Q_type) -> np.ndarray:
"""Calculate the Elastic Incoherent Structure Factor (EISF) for
the Brownian translational diffusion model.
Parameters
----------
Q : np.ndarray | Numeric | list | ArrayLike
Scattering vector in 1/angstrom
Returns
-------
np.ndarray
EISF values (dimensionless).
"""
Q = _validate_and_convert_Q(Q)
EISF = np.zeros_like(Q)
return EISF
def calculate_QISF(self, Q: Q_type) -> np.ndarray:
"""Calculate the Quasi-Elastic Incoherent Structure Factor
(QISF).
Parameters
----------
Q : np.ndarray | Numeric | list | ArrayLike
Scattering vector in 1/angstrom
Returns
-------
np.ndarray
QISF values (dimensionless).
"""
Q = _validate_and_convert_Q(Q)
QISF = np.ones_like(Q)
return QISF
def create_component_collections(
self,
Q: Q_type,
component_display_name: str = 'Brownian translational diffusion',
) -> List[ComponentCollection]:
"""Create ComponentCollection components for the Brownian
translational diffusion model at given Q values.
Args:
----------
Q : Number, list, or np.ndarray
Scattering vector values.
component_display_name : str
Name of the Lorentzian component.
Returns
-------
List[ComponentCollection]
List of ComponentCollections with Lorentzian components.
"""
Q = _validate_and_convert_Q(Q)
if not isinstance(component_display_name, str):
raise TypeError('component_name must be a string.')
component_collection_list = [None] * len(Q)
# In more complex models, this is used to scale the area of the
# Lorentzians and the delta function.
QISF = self.calculate_QISF(Q)
# Create a Lorentzian component for each Q-value, with
# width D*Q^2 and area equal to scale.
# No delta function, as the EISF is 0.
for i, Q_value in enumerate(Q):
component_collection_list[i] = ComponentCollection(
display_name=f'{self.display_name}_Q{Q_value:.2f}', unit=self.unit
)
lorentzian_component = Lorentzian(
display_name=component_display_name,
unit=self.unit,
)
# Make the width dependent on Q
dependency_expression = self._write_width_dependency_expression(Q[i])
dependency_map = self._write_width_dependency_map_expression()
lorentzian_component.width.make_dependent_on(
dependency_expression=dependency_expression,
dependency_map=dependency_map,
)
# Make the area dependent on Q
area_dependency_map = self._write_area_dependency_map_expression()
lorentzian_component.area.make_dependent_on(
dependency_expression=self._write_area_dependency_expression(QISF[i]),
dependency_map=area_dependency_map,
)
# Resolving the dependency can do weird things to the units,
# so we make sure it's correct.
lorentzian_component.width.convert_unit(self.unit)
component_collection_list[i].append_component(lorentzian_component)
return component_collection_list
def _write_width_dependency_expression(self, Q: float) -> str:
"""Write the dependency expression for the width as a function
of Q to make dependent Parameters.
Parameters
----------
Q : float
Scattering vector in 1/angstrom
Returns
-------
str
Dependency expression for the width.
"""
if not isinstance(Q, (float)):
raise TypeError('Q must be a float.')
# Q is given as a float, so we need to add the units
return f'hbar * D* {Q} **2*1/(angstrom**2)'
def _write_width_dependency_map_expression(self) -> Dict[str, DescriptorNumber]:
"""Write the dependency map expression to make dependent
Parameters.
"""
return {
'D': self.diffusion_coefficient,
'hbar': self._hbar,
'angstrom': self._angstrom,
}
def _write_area_dependency_expression(self, QISF: float) -> str:
"""Write the dependency expression for the area to make
dependent Parameters.
Returns
-------
str
Dependency expression for the area.
"""
if not isinstance(QISF, (float)):
raise TypeError('QISF must be a float.')
return f'{QISF} * scale'
def _write_area_dependency_map_expression(self) -> Dict[str, DescriptorNumber]:
"""Write the dependency map expression to make dependent
Parameters.
"""
return {
'scale': self.scale,
}
def __repr__(self):
"""String representation of the BrownianTranslationalDiffusion
model.
"""
return (
f'BrownianTranslationalDiffusion(display_name={self.display_name},'
f'diffusion_coefficient={self.diffusion_coefficient}, scale={self.scale})'
)