Skip to content

Commit 8678f94

Browse files
Add lags to vafs (#318)
* Add lags to vafs --------- Co-authored-by: ElliottKasoar <[email protected]>
1 parent 15a6b49 commit 8678f94

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

janus_core/processing/post_process.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ def compute_vaf(
190190
fft: bool = False,
191191
index: SliceLike = (0, None, 1),
192192
filter_atoms: MaybeSequence[MaybeSequence[Optional[int]]] = ((None),),
193-
) -> NDArray[float64]:
193+
time_step: float = 1.0,
194+
) -> tuple[NDArray[float64], list[NDArray[float64]]]:
194195
"""
195196
Compute the velocity autocorrelation function (VAF) of `data`.
196197
@@ -212,11 +213,36 @@ def compute_vaf(
212213
filter_atoms : MaybeSequence[MaybeSequence[Optional[int]]]
213214
Compute the VAF averaged over subsets of the system.
214215
Default is all atoms.
216+
time_step : float
217+
Time step for scaling lags to align with input data.
218+
Default is 1 (i.e. no scaling).
215219
216220
Returns
217221
-------
218-
MaybeSequence[NDArray[float64]]
222+
lags : numpy.ndarray
223+
Lags at which the VAFs have been computed.
224+
vafs : list[numpy.ndarray]
219225
Computed VAF(s).
226+
227+
Notes
228+
-----
229+
`filter_atoms` is given as a series of sequences of atoms, where
230+
each element in the series denotes a VAF subset to calculate and
231+
each sequence determines the atoms (by index) to be included in that VAF.
232+
233+
E.g.
234+
235+
.. code-block: Python
236+
237+
# Species indices in cell
238+
na = (1, 3, 5, 7)
239+
cl = (2, 4, 6, 8)
240+
241+
compute_vaf(..., filter_atoms=(na, cl))
242+
243+
Would compute separate VAFs for each species.
244+
245+
By default, one VAF will be computed for all atoms in the structure.
220246
"""
221247
# Ensure if passed scalars they are turned into correct dimensionality
222248
if not isinstance(filter_atoms, Sequence):
@@ -270,17 +296,24 @@ def compute_vaf(
270296

271297
vafs /= n_steps - np.arange(n_steps)
272298

299+
lags = np.arange(n_steps) * time_step
300+
273301
if fft:
274302
vafs = np.fft.fft(vafs, axis=0)
275-
276-
vafs = [
277-
np.average([vafs[used_atoms[i]] for i in atoms], axis=0)
278-
for atoms in filter_atoms
279-
]
303+
lags = np.fft.fftfreq(n_steps, time_step)
304+
305+
vafs = (
306+
lags,
307+
[
308+
np.average([vafs[used_atoms[i]] for i in atoms], axis=0)
309+
for atoms in filter_atoms
310+
],
311+
)
280312

281313
if filenames:
282-
for filename, vaf in zip(filenames, vafs):
314+
for vaf, filename in zip(vafs[1], filenames):
283315
with open(filename, "w", encoding="utf-8") as out_file:
284-
print(*vaf, file=out_file, sep="\n")
316+
for lag, dat in zip(lags, vaf):
317+
print(lag, dat, file=out_file)
285318

286319
return vafs

tests/test_post_process.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,21 @@ def test_vaf(tmp_path):
179179
vaf_filter = ((3, 4), (1, 2, 3))
180180

181181
data = read(DATA_PATH / "lj-traj.xyz", index=":")
182-
vaf = post_process.compute_vaf(data)
182+
lags, vaf = post_process.compute_vaf(data)
183183
expected = np.loadtxt(DATA_PATH / "vaf-lj.dat")
184184

185185
assert isinstance(vaf, list)
186186
assert len(vaf) == 1
187187
assert isinstance(vaf[0], np.ndarray)
188188
assert vaf[0] == approx(expected, rel=1e-9)
189189

190-
vaf = post_process.compute_vaf(data, fft=True)
190+
lags, vaf = post_process.compute_vaf(data, fft=True)
191191

192192
assert isinstance(vaf, list)
193193
assert len(vaf) == 1
194194
assert isinstance(vaf[0], np.ndarray)
195195

196-
vaf = post_process.compute_vaf(
196+
lags, vaf = post_process.compute_vaf(
197197
data, filter_atoms=vaf_filter, filenames=[tmp_path / name for name in vaf_names]
198198
)
199199

@@ -205,5 +205,8 @@ def test_vaf(tmp_path):
205205
assert (tmp_path / name).exists()
206206
expected = np.loadtxt(DATA_PATH / name)
207207
written = np.loadtxt(tmp_path / name)
208+
w_lag, w_vaf = written[:, 0], written[:, 1]
209+
208210
assert vaf[i] == approx(expected, rel=1e-9)
209-
assert vaf[i] == approx(written, rel=1e-9)
211+
assert lags == approx(w_lag, rel=1e-9)
212+
assert vaf[i] == approx(w_vaf, rel=1e-9)

0 commit comments

Comments
 (0)