Skip to content

Commit

Permalink
add property Elements to Entry class
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed Aug 5, 2023
1 parent 8f3678c commit 36219e4
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions pymatgen/entries/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
from __future__ import annotations

from abc import ABCMeta, abstractmethod
from typing import Literal
from typing import TYPE_CHECKING, Literal

import numpy as np
from monty.json import MSONable

from pymatgen.core.composition import Composition

if TYPE_CHECKING:
from pymatgen.core import DummySpecies, Element, Species


__author__ = "Shyue Ping Ong, Anubhav Jain, Ayush Gupta"
__copyright__ = "Copyright 2020, The Materials Project"
__version__ = "1.1"
Expand All @@ -34,46 +38,45 @@ class Entry(MSONable, metaclass=ABCMeta):
"""

def __init__(
self,
composition: Composition | str | dict[str, float],
energy: float,
) -> None:
def __init__(self, composition: Composition | str | dict[str, float], energy: float) -> None:
"""
Initializes an Entry.
Args:
composition (Composition): Composition of the entry. For
flexibility, this can take the form of all the typical input
taken by a Composition, including a {symbol: amt} dict,
a string formula, and others.
flexibility, this can take the form of all the typical input taken by a
Composition, including a {symbol: amt} dict, a string formula, and others.
energy (float): Energy of the entry.
"""
self._composition = Composition(composition)
self._energy = energy

@property
def is_element(self) -> bool:
""":return: Whether composition of entry is an element."""
# NOTE _composition rather than composition as GrandPDEntry
# edge case exists if we have a compound where chempots are
# given for all bar one element type
"""Whether composition of entry is an element."""
# NOTE _composition rather than composition as GrandPDEntry edge case exists if we
# have a compound where chempots are given for all bar one element type
return self._composition.is_element

@property
def composition(self) -> Composition:
""":return: the composition of the entry."""
"""The composition of the entry."""
return self._composition

@property
@abstractmethod
def energy(self) -> float:
""":return: the energy of the entry."""
"""The energy of the entry."""
raise NotImplementedError

@property
def elements(self) -> list[Element | Species | DummySpecies]:
"""The set of elements in the entry."""
return self._composition.elements

@property
def energy_per_atom(self) -> float:
""":return: the energy per atom of the entry."""
"""The energy per atom of the entry."""
return self.energy / self.composition.num_atoms

def __repr__(self):
Expand Down

0 comments on commit 36219e4

Please sign in to comment.