|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from MDANSE.Framework.NewQVectors.QVector import (QVecGen, |
| 5 | + QVecGeneratorProtocol, |
| 6 | + QVectorData, |
| 7 | + QVectorGenerator) |
| 8 | +from MDANSE.MolecularDynamics.UnitCell import UnitCell |
| 9 | +from numpy.typing import ArrayLike |
| 10 | + |
| 11 | +@QVectorGenerator.register("ListQVectors") |
| 12 | +class ListQVectors(QVectorGenerator): |
| 13 | + """Return Q vectors from a provided list of vectors. |
| 14 | +
|
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + qvectors : ArrayLike |
| 18 | + Sequence of vectors to return. |
| 19 | + hkl : bool |
| 20 | + Whether vectors are provided in reciprocal lattice units. |
| 21 | + lattice : Optional[UnitCell] |
| 22 | + Lattice to generate within. |
| 23 | +
|
| 24 | + Raises |
| 25 | + ------ |
| 26 | + ValueError |
| 27 | + If hkl and no lattice provided. |
| 28 | +
|
| 29 | + Examples |
| 30 | + -------- |
| 31 | + >>> qvec = ListQVectors([[1, 2, 3], [2, 0, 5]]) |
| 32 | + >>> for i in qvec: |
| 33 | + ... print(i.q) |
| 34 | + [1. 2. 3.] |
| 35 | + [2. 0. 5.] |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self, |
| 39 | + qvectors: ArrayLike, |
| 40 | + *, |
| 41 | + lattice: Optional[UnitCell] = None, |
| 42 | + **kwargs): |
| 43 | + |
| 44 | + super().__init__(lattice=lattice, **kwargs) |
| 45 | + self.qvectors = np.array(qvectors) |
| 46 | + |
| 47 | + if self.qvectors.shape != (len(self.qvectors), 3): |
| 48 | + raise ValueError(f"`qvectors` ({qvectors.shape}) must be an (N, 3) sequence.") |
| 49 | + |
| 50 | + |
| 51 | + def generate(self, lattice: Optional[UnitCell] = None) -> QVecGen: |
| 52 | + lattice = lattice if lattice is not None else self.lattice |
| 53 | + constructor = self.qvec_gen |
| 54 | + |
| 55 | + while self._ind < len(self): |
| 56 | + new_ind = yield constructor(self.qvectors[self._ind], lattice) |
| 57 | + |
| 58 | + self._ind += 1 |
| 59 | + if new_ind is not None: |
| 60 | + self.reset(new_ind) |
| 61 | + |
| 62 | + def __len__(self) -> int: |
| 63 | + return len(self.qvectors) |
| 64 | + |
| 65 | + def reset(self, value: int = 0): |
| 66 | + super().reset(value) |
| 67 | + |
| 68 | + |
| 69 | +@QVectorGenerator.register("GeneratorQVectors") |
| 70 | +class GeneratorQVectors(QVectorGenerator): |
| 71 | + """Return Q Vectors as generated from a generator function. |
| 72 | +
|
| 73 | + Parameters |
| 74 | + ---------- |
| 75 | + qvectors : QVecGen | Generator[ArrayLike, int, None] |
| 76 | + Generator which returns QVectorData. |
| 77 | + lattice : Optional[UnitCell] |
| 78 | + Lattice in which to generate Q Vectors. |
| 79 | +
|
| 80 | + Examples |
| 81 | + -------- |
| 82 | + FIXME: Add docs. |
| 83 | +
|
| 84 | + """ |
| 85 | + def __init__(self, |
| 86 | + qvectors: QVecGeneratorProtocol, |
| 87 | + *args, |
| 88 | + lattice: Optional[UnitCell] = None, |
| 89 | + returns_qvec_data: bool = True, |
| 90 | + **kwargs): |
| 91 | + super().__init__(lattice=lattice, **kwargs) |
| 92 | + |
| 93 | + self.generator = qvectors |
| 94 | + self.args = args |
| 95 | + self.returns_qvec_data = returns_qvec_data |
| 96 | + |
| 97 | + def generate(self, lattice: Optional[UnitCell] = None) -> QVecGen: |
| 98 | + lattice = lattice if lattice is not None else self.lattice |
| 99 | + constructor = self.qvec_gen |
| 100 | + |
| 101 | + for qvec in self.generator(*self.args, lattice): |
| 102 | + yield qvec if self.returns_qvec_data else constructor(qvec, lattice) |
| 103 | + |
| 104 | + def reset(self, val: None = None): |
| 105 | + raise NotImplementedError("Cannot reset this generator") |
0 commit comments