Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix d2k function #3932

Merged
merged 12 commits into from
Aug 6, 2024
61 changes: 29 additions & 32 deletions src/pymatgen/io/aims/sets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def _read_previous(
prev_dir (str or Path): The previous directory for the calculation
"""
prev_structure: Structure | Molecule | None = None
prev_parameters = {}
prev_params = {}
prev_results: dict[str, Any] = {}

if prev_dir:
Expand All @@ -243,7 +243,7 @@ def _read_previous(
# jobflow_remote)
split_prev_dir = str(prev_dir).split(":")[-1]
with open(f"{split_prev_dir}/parameters.json") as param_file:
prev_parameters = json.load(param_file, cls=MontyDecoder)
prev_params = json.load(param_file, cls=MontyDecoder)

try:
aims_output: Sequence[Structure | Molecule] = read_aims_output(
Expand All @@ -256,7 +256,7 @@ def _read_previous(
except (IndexError, AimsParseError):
pass

return prev_structure, prev_parameters, prev_results
return prev_structure, prev_params, prev_results

@staticmethod
def _get_properties(
Expand Down Expand Up @@ -308,12 +308,9 @@ def _get_input_parameters(
Returns:
dict: The input object
"""
# Get the default configuration
# FHI-aims recommends using their defaults so bare-bones default parameters
parameters: dict[str, Any] = {
"xc": "pbe",
"relativistic": "atomic_zora scalar",
}
# Get the default config
# FHI-aims recommends using their defaults so bare-bones default params
params: dict[str, Any] = {"xc": "pbe", "relativistic": "atomic_zora scalar"}

# Override default parameters with previous parameters
prev_parameters = {} if prev_parameters is None else copy.deepcopy(prev_parameters)
Expand All @@ -327,25 +324,25 @@ def _get_input_parameters(
kpt_settings["density"] = density

parameter_updates = self.get_parameter_updates(structure, prev_parameters)
parameters = recursive_update(parameters, parameter_updates)
params = recursive_update(params, parameter_updates)

# Override default parameters with user_params
parameters = recursive_update(parameters, self.user_params)
if ("k_grid" in parameters) and ("density" in kpt_settings):
params = recursive_update(params, self.user_params)
if ("k_grid" in params) and ("density" in kpt_settings):
warn(
"WARNING: the k_grid is set in user_params and in the kpt_settings,"
" using the one passed in user_params.",
stacklevel=1,
)
elif isinstance(structure, Structure) and ("k_grid" not in parameters):
elif isinstance(structure, Structure) and ("k_grid" not in params):
density = kpt_settings.get("density", 5.0)
even = kpt_settings.get("even", True)
parameters["k_grid"] = self.d2k(structure, density, even)
elif isinstance(structure, Molecule) and "k_grid" in parameters:
params["k_grid"] = self.d2k(structure, density, even)
elif isinstance(structure, Molecule) and "k_grid" in params:
warn("WARNING: removing unnecessary k_grid information", stacklevel=1)
del parameters["k_grid"]
del params["k_grid"]

return parameters
return params

def get_parameter_updates(
self,
Expand All @@ -366,7 +363,7 @@ def get_parameter_updates(
def d2k(
self,
structure: Structure,
kptdensity: float | list[float] = 5.0,
kpt_density: float | list[float] = 5.0,
even: bool = True,
) -> Iterable[float]:
"""Convert k-point density to Monkhorst-Pack grid size.
Expand All @@ -376,15 +373,15 @@ def d2k(
Args:
structure (Structure): Contains unit cell and
information about boundary conditions.
kptdensity (float | list[float]): Required k-point
kpt_density (float | list[float]): Required k-point
density. Default value is 5.0 point per Ang^-1.
even (bool): Round up to even numbers.

Returns:
dict: Monkhorst-Pack grid size in all directions
"""
recipcell = structure.lattice.inv_matrix
return self.d2k_recipcell(recipcell, structure.lattice.pbc, kptdensity, even)
recip_cell = structure.lattice.inv_matrix.transpose()
return self.d2k_recip_cell(recip_cell, structure.lattice.pbc, kpt_density, even)

def k2d(self, structure: Structure, k_grid: np.ndarray[int]):
"""Generate the kpoint density in each direction from given k_grid.
Expand All @@ -398,36 +395,36 @@ def k2d(self, structure: Structure, k_grid: np.ndarray[int]):
Returns:
dict: Density of kpoints in each direction. result.mean() computes average density
"""
recipcell = structure.lattice.inv_matrix
densities = k_grid / (2 * np.pi * np.sqrt((recipcell**2).sum(axis=1)))
recip_cell = structure.lattice.inv_matrix.transpose()
densities = k_grid / (2 * np.pi * np.sqrt((recip_cell**2).sum(axis=1)))
return np.array(densities)

@staticmethod
def d2k_recipcell(
recipcell: np.ndarray,
def d2k_recip_cell(
recip_cell: np.ndarray,
pbc: Sequence[bool],
kptdensity: float | Sequence[float] = 5.0,
kpt_density: float | tuple[float, float, float] = 5.0,
even: bool = True,
) -> Sequence[int]:
"""Convert k-point density to Monkhorst-Pack grid size.

Args:
recipcell (Cell): The reciprocal cell
recip_cell (Cell): The reciprocal cell
pbc (Sequence[bool]): If element of pbc is True
then system is periodic in that direction
kptdensity (float or list[floats]): Required k-point
density. Default value is 3.5 point per Ang^-1.
kpt_density (float or list[floats]): Required k-point
density. Default value is 5 points per Ang^-1.
even(bool): Round up to even numbers.

Returns:
dict: Monkhorst-Pack grid size in all directions
"""
if not isinstance(kptdensity, Iterable):
kptdensity = 3 * [float(kptdensity)]
if not isinstance(kpt_density, Iterable):
kpt_density = 3 * [float(kpt_density)]
kpts: list[int] = []
for i in range(3):
if pbc[i]:
k = 2 * np.pi * np.sqrt((recipcell[i] ** 2).sum()) * float(kptdensity[i])
k = 2 * np.pi * np.sqrt((recip_cell[i] ** 2).sum()) * float(kpt_density[i])
if even:
kpts.append(2 * int(np.ceil(k / 2)))
else:
Expand Down
Loading