Skip to content

generate_kpoints

Simple script to generate a KPOINTS file for a given structure and k-point spacing.

Usage

python generate_kpoints.py

Example

python generate_kpoints.py Si/sc-001/SPOSCAR 0.15

get_KPOINTS(struct, kspace)

Prepares a KPOINTS object with a mesh of given spacing in 1/Å. Parameters:


struct : pymatgen.Structure The structure object. kspace : float The spacing of the k-point mesh in 1/Å. Returns:


kpoints : pymatgen.io.vasp.inputs.Kpoints The KPOINTS object.

Source code in example/generate_kpoints.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def get_KPOINTS(struct, kspace):
    """
    Prepares a KPOINTS object with a mesh of given spacing in 1/Å.
    Parameters:
    -----------
    struct : pymatgen.Structure
        The structure object.
    kspace : float
        The spacing of the k-point mesh in 1/Å.
    Returns:
    --------
    kpoints : pymatgen.io.vasp.inputs.Kpoints
        The KPOINTS object.
    """
    assert kspace > 0, "argument kspace must not be negative"
    b = np.array(struct.lattice.reciprocal_lattice.matrix)
    N = np.maximum(
        np.array([1, 1, 1]), np.round(np.sqrt(np.sum(b**2, axis=1)) / kspace)
    ).astype(np.int64)
    k_dict = {
        "nkpoints": 0,
        "generation_style": "Gamma",
        "kpoints": [[N[0], N[1], N[2]]],
        "usershift": [0, 0, 0],
        "comment": f"Mesh with spacing {kspace} 1/Å",
    }
    return Kpoints.from_dict(k_dict)