Skip to content

Commit f00582a

Browse files
committed
Use ChatGPT to improve documentation.
1 parent deee1fa commit f00582a

File tree

2 files changed

+98
-45
lines changed

2 files changed

+98
-45
lines changed

src/pymatgen/core/composition.py

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,41 +36,80 @@
3636

3737
@total_ordering
3838
class Composition(collections.abc.Hashable, collections.abc.Mapping, MSONable, Stringify):
39-
"""Represents a Composition, which is essentially a {element:amount} mapping
40-
type. Composition is written to be immutable and hashable,
41-
unlike a standard Python dict.
42-
43-
Note that the key can be either an Element or a Species. Elements and Species
44-
are treated differently. i.e., a Fe2+ is not the same as a Fe3+ Species and
45-
would be put in separate keys. This differentiation is deliberate to
46-
support using Composition to determine the fraction of a particular Species.
47-
48-
Works almost completely like a standard python dictionary, except that
49-
__getitem__ is overridden to return 0 when an element is not found.
50-
(somewhat like a defaultdict, except it is immutable).
51-
52-
Also adds more convenience methods relevant to compositions, e.g.
53-
get_fraction.
54-
55-
It should also be noted that many Composition related functionality takes
56-
in a standard string as a convenient input. For example,
57-
even though the internal representation of a Fe2O3 composition is
58-
{Element("Fe"): 2, Element("O"): 3}, you can obtain the amount of Fe
59-
simply by comp["Fe"] instead of the more verbose comp[Element("Fe")].
60-
61-
>>> comp = Composition("LiFePO4")
62-
>>> comp.get_atomic_fraction(Element("Li"))
63-
0.14285714285714285
64-
>>> comp.num_atoms
65-
7.0
66-
>>> comp.reduced_formula
67-
'LiFePO4'
68-
>>> comp.formula
69-
'Li1 Fe1 P1 O4'
70-
>>> comp.get_wt_fraction(Element("Li"))
71-
0.04399794666951898
72-
>>> comp.num_atoms
73-
7.0
39+
"""
40+
Represents a `Composition`, a mapping of {element/species: amount} with
41+
enhanced functionality tailored for handling chemical compositions. The class
42+
is immutable, hashable, and designed for robust usage in material science
43+
and chemistry computations.
44+
45+
Key Features:
46+
- Supports both `Element` and `Species` as keys, with differentiation
47+
between oxidation states (e.g., Fe2+ and Fe3+ are distinct keys).
48+
- Behaves like a dictionary but returns 0 for missing keys, making it
49+
similar to a `defaultdict` while remaining immutable.
50+
- Provides numerous utility methods for chemical computations, such as
51+
calculating fractions, weights, and formula representations.
52+
53+
Highlights:
54+
- **Input Flexibility**: Accepts formulas as strings, dictionaries, or
55+
keyword arguments for construction.
56+
- **Convenience Methods**: Includes `get_fraction`, `reduced_formula`,
57+
and weight-related utilities.
58+
- **Enhanced Formula Representation**: Supports reduced, normalized, and
59+
IUPAC-sorted formulas.
60+
61+
Examples:
62+
>>> comp = Composition("LiFePO4")
63+
>>> comp.get_atomic_fraction(Element("Li"))
64+
0.14285714285714285
65+
>>> comp.num_atoms
66+
7.0
67+
>>> comp.reduced_formula
68+
'LiFePO4'
69+
>>> comp.formula
70+
'Li1 Fe1 P1 O4'
71+
>>> comp.get_wt_fraction(Element("Li"))
72+
0.04399794666951898
73+
>>> comp.num_atoms
74+
7.0
75+
76+
Attributes:
77+
- `amount_tolerance` (float): Tolerance for distinguishing composition
78+
amounts. Default is 1e-8 to minimize floating-point errors.
79+
- `charge_balanced_tolerance` (float): Tolerance for verifying charge balance.
80+
- `special_formulas` (dict): Custom formula mappings for specific compounds
81+
(e.g., `"LiO"` → `"Li2O2"`).
82+
- `oxi_prob` (dict or None): Prior probabilities of oxidation states, used
83+
for oxidation state guessing.
84+
85+
Functionality:
86+
- Arithmetic Operations: Add, subtract, multiply, or divide compositions.
87+
For example:
88+
>>> comp1 = Composition("Fe2O3")
89+
>>> comp2 = Composition("FeO")
90+
>>> result = comp1 + comp2 # Produces "Fe3O4"
91+
- Representation:
92+
- `formula`: Full formula string with elements sorted by electronegativity.
93+
- `reduced_formula`: Simplified formula with minimal ratios.
94+
- `hill_formula`: Hill notation (C and H prioritized, others alphabetically sorted).
95+
- Utilities:
96+
- `get_atomic_fraction`: Returns the atomic fraction of a given element/species.
97+
- `get_wt_fraction`: Returns the weight fraction of a given element/species.
98+
- `is_element`: Checks if the composition is a pure element.
99+
- `reduced_composition`: Normalizes the composition by the greatest common denominator.
100+
- `fractional_composition`: Returns the normalized composition where sums equal 1.
101+
- Oxidation State Handling:
102+
- `oxi_state_guesses`: Suggests charge-balanced oxidation states.
103+
- `charge_balanced`: Checks if the composition is charge balanced.
104+
- `add_charges_from_oxi_state_guesses`: Assigns oxidation states based on guesses.
105+
- Validation:
106+
- `valid`: Ensures all elements/species are valid.
107+
108+
Notes:
109+
- When constructing from strings, both `Element` and `Species` types are
110+
handled. For example:
111+
- `Composition("Fe2+")` differentiates Fe2+ from Fe3+.
112+
- `Composition("Fe2O3")` auto-parses standard formulas.
74113
"""
75114

76115
# Tolerance in distinguishing different composition amounts.

src/pymatgen/io/common.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,32 @@
2828

2929
class VolumetricData(MSONable):
3030
"""
31-
Simple volumetric object. Used to read LOCPOT/CHGCAR files produced by
32-
vasp as well as cube files produced by other codes.
31+
A representation of volumetric data commonly used in atomistic simulation outputs,
32+
such as LOCPOT/CHGCAR files from VASP or cube files from other codes.
3333
3434
Attributes:
35-
structure (Structure): Structure associated with the Volumetric Data object.
36-
is_spin_polarized (bool): True if run is spin polarized.
37-
dim (tuple): Tuple of dimensions of volumetric grid in each direction (nx, ny, nz).
38-
data (dict): Actual data as a dict of {string: np.array}. The string are "total"
39-
and "diff", in accordance to the output format of Vasp LOCPOT and
40-
CHGCAR files where the total spin density is written first, followed
41-
by the difference spin density.
42-
ngridpts (int): Total number of grid points in volumetric data.
35+
structure (Structure):
36+
The crystal structure associated with the volumetric data.
37+
Represents the lattice and atomic coordinates using the `Structure` class.
38+
39+
is_spin_polarized (bool):
40+
Indicates if the simulation is spin-polarized. True for spin-polarized data
41+
(contains both total and spin-difference densities), False otherwise.
42+
43+
dim (tuple[int, int, int]):
44+
The dimensions of the 3D volumetric grid along each axis in the format
45+
(nx, ny, nz), where nx, ny, and nz represent the number of grid points
46+
in the x, y, and z directions, respectively.
47+
48+
data (dict[str, np.ndarray]):
49+
A dictionary containing the volumetric data. Keys include:
50+
- `"total"`: A 3D NumPy array representing the total spin density.
51+
- `"diff"` (optional): A 3D NumPy array representing the spin-difference
52+
density (spin up - spin down). Typically present in spin-polarized calculations.
53+
54+
ngridpts (int):
55+
The total number of grid points in the volumetric data, calculated as
56+
`nx * ny * nz` using the grid dimensions.
4357
"""
4458

4559
def __init__(

0 commit comments

Comments
 (0)