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
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'])
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
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)
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
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
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
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'])
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
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'])
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'])
def create_from_dict(self, request): expected_crystal = request.param d = expected_crystal.to_dict() crystal = Crystal.from_dict(d) return crystal, expected_crystal
def create_from_cell_vectors(self, request): cell_vectors, expected_crystal = request.param return Crystal.from_cell_vectors(cell_vectors), expected_crystal
def test_faulty_creation(self, inject_faulty_elements): faulty_args, expected_exception = inject_faulty_elements with pytest.raises(expected_exception): Crystal(*faulty_args)
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'))
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'))
def create_from_constructor(self, request): expected_crystal = request.param crystal = Crystal(*expected_crystal.to_constructor_args()) return crystal, expected_crystal
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
def get_crystal_from_json_file(filepath): return Crystal.from_json_file(filepath)