Esempio n. 1
0
    def to_constructor_args(self,
                            crystal=None,
                            qpts=None,
                            frequencies=None,
                            structure_factors=None,
                            weights=None,
                            temperature=None):
        if crystal is None:
            crystal = Crystal(*self.crystal.to_constructor_args())
        if qpts is None:
            qpts = self.qpts
        if frequencies is None:
            frequencies = self.frequencies
        if structure_factors is None:
            structure_factors = self.structure_factors
        if weights is None:
            weights = self.weights
        if temperature is None:
            temperature = self.temperature

        kwargs = {}
        # Allow setting weights=False to not include weights in kwargs, to test
        # object creation when weights is not supplied
        if weights is not False:
            kwargs['weights'] = weights
        if temperature is not None:
            kwargs['temperature'] = temperature

        return (crystal, qpts, frequencies, structure_factors), kwargs
Esempio n. 2
0
    def from_dict(cls: T, d: Dict[str, Any]) -> T:
        """
        Convert a dictionary to a StructureFactor object

        Parameters
        ----------
        d : dict
            A dictionary with the following keys/values:

            - 'crystal': dict, see Crystal.from_dict
            - 'qpts': (n_qpts, 3) float ndarray
            - 'frequencies': (n_qpts, 3*crystal.n_atoms) float ndarray
            - 'frequencies_unit': str
            - 'structure_factors': (n_qpts, 3*crystal.n_atoms) float ndarray
            - 'structure_factors_unit': str

            There are also the following optional keys:

            - 'weights': (n_qpts,) float ndarray
            - 'temperature': float
            - 'temperature_unit': str
        """
        crystal = Crystal.from_dict(d['crystal'])
        d = _process_dict(
            d,
            quantities=['frequencies', 'structure_factors', 'temperature'],
            optional=['weights', 'temperature'])
        return cls(crystal, d['qpts'], d['frequencies'],
                   d['structure_factors'], d['weights'], d['temperature'])
Esempio n. 3
0
    def to_constructor_args(self,
                            crystal=None,
                            force_constants=None,
                            sc_matrix=None,
                            cell_origins=None,
                            born=None,
                            dielectric=None):
        if crystal is None:
            crystal = Crystal(*self.crystal.to_constructor_args())
        if force_constants is None:
            force_constants = self.force_constants
        if sc_matrix is None:
            sc_matrix = self.sc_matrix
        if cell_origins is None:
            cell_origins = self.cell_origins
        if born is None:
            born = self.born
        if dielectric is None:
            dielectric = self.dielectric

        kwargs = {}
        if born is not None:
            kwargs['born'] = born
        if dielectric is not None:
            kwargs['dielectric'] = dielectric

        return (crystal, force_constants, sc_matrix, cell_origins), kwargs
Esempio n. 4
0
 def test_serialise_to_json_file(self, crystal, tmpdir):
     # Serialise
     output_file = str(tmpdir.join('tmp.test'))
     crystal.to_json_file(output_file)
     # Test file metadata
     check_json_metadata(output_file, 'Crystal')
     # Deserialise
     deserialised_crystal = Crystal.from_json_file(output_file)
     return check_crystal(crystal, deserialised_crystal)
Esempio n. 5
0
    def to_constructor_args(self, crystal=None, debye_waller=None,
                            temperature=None):
        if crystal is None:
            crystal = Crystal(*self.crystal.to_constructor_args())
        if debye_waller is None:
            debye_waller = self.debye_waller
        if temperature is None:
            temperature = self.temperature

        return crystal, debye_waller, temperature
Esempio n. 6
0
def _grid_spec_from_args(crystal: Crystal,
                           grid: Optional[Sequence[int]] = None,
                           grid_spacing: Quantity = 0.1 * ureg('1/angstrom')
                           ) -> Sequence[int]:
    """Get Monkorst-Pack mesh divisions from user arguments"""
    if grid:
        grid_spec = grid
    else:
        grid_spec = crystal.get_mp_grid_spec(spacing=grid_spacing)
    return grid_spec
Esempio n. 7
0
    def to_constructor_args(self,
                            crystal=None,
                            qpts=None,
                            frequencies=None,
                            weights=None):
        if crystal is None:
            crystal = Crystal(*self.crystal.to_constructor_args())
        if qpts is None:
            qpts = self.qpts
        if frequencies is None:
            frequencies = self.frequencies
        if weights is None:
            weights = self.weights

        kwargs = {}
        # Allow setting weights=False to not include weights in kwargs, to test
        # object creation when weights is not supplied
        if weights is not False:
            kwargs['weights'] = weights

        return (crystal, qpts, frequencies), kwargs
Esempio n. 8
0
    def from_dict(cls: T, d: Dict[str, Any]) -> T:
        """
        Convert a dictionary to a QpointFrequencies object

        Parameters
        ----------
        d
            A dictionary with the following keys/values:

            - 'crystal': dict, see Crystal.from_dict
            - 'qpts': (n_qpts, 3) float ndarray
            - 'frequencies': (n_qpts, n_branches) float ndarray
            - 'frequencies_unit': str

            There are also the following optional keys:

            - 'weights': (n_qpts,) float ndarray
        """
        crystal = Crystal.from_dict(d['crystal'])
        d = _process_dict(d, quantities=['frequencies'], optional=['weights'])
        return cls(crystal, d['qpts'], d['frequencies'],
                   d['weights'])
Esempio n. 9
0
def _qpts_cart_to_frac(qpts: Quantity,
                       crystal: Crystal) -> np.ndarray:
    """Convert set of q-points from Cartesian to fractional coordinates

    Parameters
    ----------

    qpts
        Array of q-points in Cartesian coordinates.
    crystal
        Crystal structure determining reciprocal lattice

    Returns
    -------
    np.ndarray
        Dimensionless array of q-points in fractional coordinates
    """
    lattice = crystal.reciprocal_cell()

    return np.linalg.solve(lattice.to(ureg('1/bohr')).magnitude.T,
                           qpts.to(ureg('1/bohr')).magnitude.T
                           ).T
Esempio n. 10
0
    def from_dict(cls: T, d: Dict[str, Any]) -> T:
        """
        Convert a dictionary to a QpointPhononModes object

        Parameters
        ----------
        d : dict
            A dictionary with the following keys/values:

            - 'crystal': dict, see Crystal.from_dict
            - 'qpts': (n_qpts, 3) float ndarray
            - 'frequencies': (n_qpts, 3*crystal.n_atoms) float ndarray
            - 'frequencies_unit': str
            - 'eigenvectors': (n_qpts, 3*crystal.n_atoms, crystal.n_atoms, 3) complex ndarray

            There are also the following optional keys:

            - 'weights': (n_qpts,) float ndarray
        """
        crystal = Crystal.from_dict(d['crystal'])
        d = _process_dict(d, quantities=['frequencies'], optional=['weights'])
        return cls(crystal, d['qpts'], d['frequencies'], d['eigenvectors'],
                   d['weights'])
Esempio n. 11
0
    def from_dict(cls, d):
        """
        Convert a dictionary to a DebyeWaller object

        Parameters
        ----------
        d : dict
            A dictionary with the following keys/values:

            - 'crystal': dict, see Crystal.from_dict
            - 'debye_waller': (n_atoms, 3, 3) float ndarray
            - 'debye_waller_unit': str
            - 'temperature': float
            - 'temperature_unit': str

        Returns
        -------
        DebyeWaller
        """
        crystal = Crystal.from_dict(d['crystal'])
        d = _process_dict(d,
                          quantities=['debye_waller', 'temperature'],
                          optional=['temperature'])
        return cls(crystal, d['debye_waller'], d['temperature'])
Esempio n. 12
0
 def create_from_dict(self, request):
     expected_crystal = request.param
     d = expected_crystal.to_dict()
     crystal = Crystal.from_dict(d)
     return crystal, expected_crystal
Esempio n. 13
0
 def create_from_cell_vectors(self, request):
     cell_vectors, expected_crystal = request.param
     return Crystal.from_cell_vectors(cell_vectors), expected_crystal
Esempio n. 14
0
 def test_faulty_creation(self, inject_faulty_elements):
     faulty_args, expected_exception = inject_faulty_elements
     with pytest.raises(expected_exception):
         Crystal(*faulty_args)
Esempio n. 15
0
 def nontrivial_crystal(self, request):
     """Some arbitrary non-diagonal lattices"""
     return Crystal(np.asarray(request.param, dtype=float
                               ) * ureg('angstrom'),
                    np.array([[0., 0., 0.]]),
                    np.array(['Si']), np.array([28.055]) * ureg('amu'))
Esempio n. 16
0
 def trivial_crystal(self):
     return Crystal((np.array([[1., 0., 0.], [0., 2, 0.], [0., 0., 3]])
                     * ureg('angstrom')),
                    np.array([[0., 0., 0.]]),
                    np.array(['Si']), np.array([28.055]) * ureg('amu'))
Esempio n. 17
0
 def create_from_constructor(self, request):
     expected_crystal = request.param
     crystal = Crystal(*expected_crystal.to_constructor_args())
     return crystal, expected_crystal
Esempio n. 18
0
 def serialise_to_dict(self, request):
     crystal, expected_crystal = request.param
     # Convert to dict, then back to object to test
     crystal_dict = crystal.to_dict()
     crystal_from_dict = Crystal.from_dict(crystal_dict)
     return crystal_from_dict, expected_crystal
Esempio n. 19
0
def get_crystal_from_json_file(filepath):
    return Crystal.from_json_file(filepath)