Skip to content

Commit 12f2f64

Browse files
committed
Nexus: Switch from Zeffs to Zeff_map, update docstrings, other misc cleanup
1 parent 1c301d7 commit 12f2f64

2 files changed

Lines changed: 83 additions & 84 deletions

File tree

nexus/nexus/pseudopotential.py

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,12 @@ class PseudoSet(DevBase):
530530
Name of the program(s) that these pseudos are meant for. Due to
531531
overlap between some programs for file extension, this can
532532
occasionally contain more than one code.
533-
Zeffs : Map of str: int
533+
Zeff_map : Map of str: int
534534
A ``dict`` or ``obj`` mapping elements to their effective
535535
nuclear charges (Z-valences).
536536
pseudo_dirs : set of Path
537537
The directories that the pseudopotentials are stored in.
538-
legacy_pseudos : dict of str: PseudoSet
538+
legacy_pseudos : dict of str: PseudoSet (class attribute)
539539
Interface for creating pseudopotentials from the legacy command
540540
``ppset``.
541541
@@ -548,12 +548,12 @@ class PseudoSet(DevBase):
548548
The name of the code that the pseudos are formatted for, or
549549
if ``"detect"``, will auto-detect the code name from the
550550
file extensions.
551-
Zeffs : Map of str/Elements to int, optional
551+
Zeff_map : Map of str/Elements to int, optional
552552
A ``dict`` or ``obj`` mapping elements to their effective nuclear
553553
charges (Z-valences). If this is supplied, it will override any
554554
parts of the code that may try to parse the pseudopotential to
555555
get the Z-valence.
556-
skip_invalid : bool, default=False
556+
skip_invalid : bool, default=False (keyword-only)
557557
If ``True``, then this will emit a warning rather than raise an
558558
error if a file is not found or if the file does not have a
559559
valid name.
@@ -575,7 +575,7 @@ def __init__(
575575
self,
576576
pseudos : Iterable[PathLike] | Mapping[Elements | str, PathLike],
577577
codes : str | Iterable[str] = "detect",
578-
Zeffs : Mapping[PathLike, int] | None = None,
578+
Zeff_map : Mapping[PathLike, int] | None = None,
579579
*,
580580
skip_invalid: bool = False,
581581
):
@@ -652,7 +652,7 @@ def __init__(
652652
msg = f"`codes` must be either 'detect', str, or an iterable of str, but has type `{type(codes).__name__}`"
653653
raise TypeError(msg)
654654

655-
self.Zeffs = dict(Zeffs) if Zeffs is not None else {}
655+
self.Zeff_map = dict(Zeff_map) if Zeff_map is not None else {}
656656

657657
self.pseudo_dirs: set[Path] = set()
658658
for pseudo in self.pseudos.values():
@@ -732,7 +732,7 @@ def from_dir(
732732
cls,
733733
pseudo_dir: PathLike,
734734
code : str = "detect",
735-
Zeffs : Mapping[PathLike, int] | None = None,
735+
Zeff_map : Mapping[PathLike, int] | None = None,
736736
ext_filter: str | Iterable[str] | None = None,
737737
pattern : str | Pattern | None = None,
738738
*,
@@ -750,7 +750,7 @@ def from_dir(
750750
The name of the code that the pseudos are formatted for,
751751
or if ``"detect"``, will auto-detect the code name from the
752752
file extensions.
753-
Zeffs : Map of str/Elements to int, optional
753+
Zeff_map : Map of str/Elements to int, optional
754754
A ``dict`` or ``obj`` mapping elements to their effective
755755
nuclear charges (Z-valences). If this is supplied, it will
756756
override any parts of the code that may try to parse the
@@ -768,7 +768,7 @@ def from_dir(
768768
should include a leading ``.``, e.g. ``.xml``, not ``xml``.
769769
pattern : str or Pattern, optional
770770
A string or regex pattern to use to filter files by name.
771-
skip_invalid : bool, default=False
771+
skip_invalid : bool, default=False (keyword-only)
772772
If ``True``, then this will emit a warning rather than raise an
773773
error if a file is not found or if the file does not have a
774774
valid name.
@@ -909,19 +909,19 @@ def from_dir(
909909
return cls(
910910
codes = code,
911911
pseudos = pseudos,
912-
Zeffs = Zeffs,
912+
Zeff_map = Zeff_map,
913913
skip_invalid = skip_invalid,
914914
)
915915
#end def from_dir
916916

917917
@classmethod
918918
def from_mixed_dir(
919919
cls,
920-
pseudo_dir: PathLike,
921-
codes : str | list[str] | None = None,
922-
extensions: Mapping[str, set[str]] | None = None,
923-
patterns : Mapping[str, str | Pattern] | None = None,
924-
code_Zeffs: Mapping[str, Mapping[str, int]] | None = None,
920+
pseudo_dir : PathLike,
921+
codes : str | list[str] | None = None,
922+
extensions : Mapping[str, set[str]] | None = None,
923+
patterns : Mapping[str, str | Pattern] | None = None,
924+
code_Zeff_map: Mapping[str, Mapping[str, int]] | None = None,
925925
*,
926926
skip_invalid: bool = False,
927927
) -> dict[Literal["espresso", "gamess", "vasp", "qmcpack", "rmg", "pyscf"], PseudoSet]:
@@ -938,22 +938,23 @@ def from_mixed_dir(
938938
into their respective groups. If this is set to ``detect``
939939
and filters is ``None``, then it will use all known codes
940940
and file extensions to filter the pseudos.
941-
filters : Map of str to set of str, optional
942-
A dictionary mapping codes to the suffixes corresponding to
943-
those labels. If this is not provided, then the filters are
944-
automatically populated by the codes in ``codes``.
941+
extensions : Map of str to set of str, optional
942+
A dictionary mapping codes to the file extensions
943+
corresponding to those labels. If this is not provided, then
944+
the filters are automatically populated by the codes in
945+
``codes``.
945946
patterns : Map of str to str or Pattern, optional
946947
A dictionary mapping codes to strings or regex patterns,
947948
used to filter out files.
948-
code_Zeffs : Map of str to Map of str/Elements to int, optional
949+
code_Zeff_map : Map of str to Map of str/Elements to int, optional
949950
A ``dict`` or ``obj`` for each code that maps elements to
950951
their effective nuclear charges (Z-valences). If this is
951952
supplied, it will override any parts of the code that may
952953
try to parse the pseudopotential to get the Z-valence.
953-
skip_invalid : bool, default=False
954-
If ``True``, then this will emit a warning rather than raise an
955-
error if a file is not found or if the file does not have a
956-
valid name.
954+
skip_invalid : bool, default=False (keyword-only)
955+
If ``True``, then this will emit a warning rather than raise
956+
an error if a file is not found or if the file does not have
957+
a valid name.
957958
958959
Returns
959960
-------
@@ -1006,23 +1007,23 @@ def from_mixed_dir(
10061007
... for lbl, psp in ps_set.pseudos.items():
10071008
... print(f" {lbl}: {psp}")
10081009
espresso pseudos:
1009-
H: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/H.ccECP.upf
1010-
C: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/C.ccECP.upf
1010+
H: /path/to/pseudo_dir/H.ccECP.upf
1011+
C: /path/to/pseudo_dir/C.ccECP.upf
10111012
gamess pseudos:
1012-
C: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/C.BFD.gms
1013-
H: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/H.BFD.gms
1013+
C: /path/to/pseudo_dir/C.BFD.gms
1014+
H: /path/to/pseudo_dir/H.BFD.gms
10141015
vasp pseudos:
1015-
C: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/C/POTCAR
1016-
H: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/H/POTCAR
1016+
C: /path/to/pseudo_dir/C/POTCAR
1017+
H: /path/to/pseudo_dir/H/POTCAR
10171018
qmcpack pseudos:
1018-
C: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/C.ccECP.xml
1019-
H: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/H.ccECP.xml
1019+
C: /path/to/pseudo_dir/C.ccECP.xml
1020+
H: /path/to/pseudo_dir/H.ccECP.xml
10201021
rmg pseudos:
1021-
C: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/C.USPP.upf
1022-
H: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/H.USPP.upf
1022+
C: /path/to/pseudo_dir/C.USPP.upf
1023+
H: /path/to/pseudo_dir/H.USPP.upf
10231024
pyscf pseudos:
1024-
C: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/C.BFD.gth
1025-
H: /home/brock/Documents/github/qmcpack/ignored_files/pseudo_dir/H.BFD.gth
1025+
C: /path/to/pseudo_dir/C.BFD.gth
1026+
H: /path/to/pseudo_dir/H.BFD.gth
10261027
"""
10271028
psp_dir = Path(pseudo_dir).resolve()
10281029

@@ -1074,13 +1075,13 @@ def from_mixed_dir(
10741075
code = PseudoSet._check_code_str(c)
10751076
checked_filters[code] = extensions.get(c)
10761077

1077-
if code_Zeffs is None:
1078-
code_Zeffs = {}
1079-
elif codes is not None and not set(codes) >= set(code_Zeffs.keys()):
1078+
if code_Zeff_map is None:
1079+
code_Zeff_map = {}
1080+
elif codes is not None and not set(codes) >= set(code_Zeff_map.keys()):
10801081
msg = (
1081-
"Mismatch between provided code Zeffs and codes!\n"
1082+
"Mismatch between provided code Zeff map and codes!\n"
10821083
f"Provided codes: {tuple(codes)}\n"
1083-
f"Zeff keys: {tuple(code_Zeffs.keys())}"
1084+
f"Zeff keys: {tuple(code_Zeff_map.keys())}"
10841085
)
10851086
raise ValueError(msg)
10861087

@@ -1100,7 +1101,7 @@ def from_mixed_dir(
11001101
pseudos[code] = PseudoSet.from_dir(
11011102
pseudo_dir = psp_dir,
11021103
code = code,
1103-
Zeffs = code_Zeffs.get(code),
1104+
Zeff_map = code_Zeff_map.get(code),
11041105
ext_filter = suffixes,
11051106
pattern = patterns.get(code),
11061107
skip_invalid = skip_invalid,
@@ -1165,7 +1166,7 @@ def get_pseudos(
11651166
return pps
11661167
#end def get_pseudos
11671168

1168-
def get_Zeffs(
1169+
def get_Zeff(
11691170
self,
11701171
elem_labels: Iterable[Elements | str] | PhysicalSystem,
11711172
*,
@@ -1177,7 +1178,7 @@ def get_Zeffs(
11771178
----------
11781179
elem_labels : list of Elements or list of str or PhysicalSystem
11791180
The elements or system to get Z-valences for.
1180-
missing_as_ae : bool, default=False
1181+
missing_as_ae : bool, default=False (keyword-only)
11811182
Assume any elements for which a pseudopotential can not be
11821183
found are all-electron, and use their atomic number as the
11831184
value for the Z-valence. If this is not supplied, and if the
@@ -1201,40 +1202,40 @@ def get_Zeffs(
12011202
elem_labels = elem_labels.ion_labels
12021203

12031204
elem_labels = set(elem_labels) # Unique only, saves on iteration.
1204-
Z_effs = {}
1205+
Z_eff_map = {}
12051206
for label in elem_labels:
1206-
if label in self.Zeffs:
1207-
Z_effs[label] = self.Zeffs[label]
1207+
if label in self.Zeff_map:
1208+
Z_eff_map[label] = self.Zeff_map[label]
12081209
elif label in self.pseudos:
12091210
f_ext = self.pseudos[label].suffix.lower()
12101211
if f_ext == ".upf":
1211-
Z_effs[label] = read_upf_z_valence(self.pseudos[label])
1212+
Z_eff_map[label] = read_upf_z_valence(self.pseudos[label])
12121213
elif f_ext in (".gms", ".gamess"):
12131214
msg = (
12141215
"Z-valence parsing not implemented for GAMESS pseudopotentials!\n"
12151216
"You must supply Z-valences manually until this feature is added."
12161217
)
12171218
raise NotImplementedError(msg)
12181219
elif f_ext in ["potcar", ".vasp"]:
1219-
Z_effs[label] = read_potcar_z_valence(self.pseudos[label])
1220+
Z_eff_map[label] = read_potcar_z_valence(self.pseudos[label])
12201221
elif f_ext == ".xml":
1221-
Z_effs[label] = read_qmcpack_xml_z_valence(self.pseudos[label])
1222+
Z_eff_map[label] = read_qmcpack_xml_z_valence(self.pseudos[label])
12221223
else:
12231224
msg = f"File extension '{f_ext}' is not parseable by Nexus, can not extract Z-valence!"
1224-
raise ValueError(msg)
1225+
raise NotImplementedError(msg)
12251226
elif missing_as_ae:
12261227
is_elem, element = Elements.is_element(label, return_element=True)
12271228
if not is_elem:
12281229
msg = f"Can not determine element for label '{label}'"
12291230
raise ValueError(msg)
12301231
else:
1231-
Z_effs[label] = element.atomic_number
1232+
Z_eff_map[label] = element.atomic_number
12321233
else:
12331234
msg = f"No pseudopotential found for label {label}!"
12341235
raise ValueError(msg)
12351236

1236-
return Z_effs
1237-
#end def get_Zeffs
1237+
return Z_eff_map
1238+
#end def get_Zeff
12381239

12391240
@classmethod
12401241
def _register_legacy_ppset(cls, label: str) -> None:
@@ -1272,11 +1273,11 @@ def __repr__(self) -> str:
12721273
else:
12731274
rep += "},\n"
12741275

1275-
rep += " Zeffs = {"
1276-
if len(self.Zeffs) > 0:
1277-
lbl_len = max(map(len, self.Zeffs.keys()))+2
1276+
rep += " Zeff_map = {"
1277+
if len(self.Zeff_map) > 0:
1278+
lbl_len = max(map(len, self.Zeff_map.keys()))+2
12781279
rep += "\n"
1279-
for lbl, zeff in self.Zeffs.items():
1280+
for lbl, zeff in self.Zeff_map.items():
12801281
lbl = f"'{lbl}'"
12811282
rep += f"{' '*8}{lbl:<{lbl_len}}: {zeff!s},\n"
12821283
rep += " },\n"
@@ -1285,9 +1286,6 @@ def __repr__(self) -> str:
12851286
rep += ")\n"
12861287
return rep
12871288
#end def __repr__
1288-
1289-
def __str__(self) -> str:
1290-
return repr(self) # Override bad str with custom repr.
12911289
#end class PseudoSet
12921290

12931291
# real pseudopotentials

0 commit comments

Comments
 (0)