forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 231
Standardize Molden output and add Multiwfn-specific interface #7424
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
Open
Stardust0831
wants to merge
14
commits into
deepmodeling:develop
Choose a base branch
from
Stardust0831:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8a43eab
Fix Molden GTO normalization and coordinate conversion
Stardust0831 0bb08be
Show default values in molden.py CLI help
Stardust0831 d15a8d7
Merge branch 'deepmodeling:develop' into develop
Stardust0831 1594187
Refactor Molden writing functions and update CLI
Stardust0831 c5e0dc8
Write [Nval] metadata by default in molden_nval.py
Stardust0831 46f4b28
Add a Multiwfn interface wrapper with [Nval] and [Cell] enabled by de…
Stardust0831 cf2e393
Add option to include [Pseudo] metadata in Molden
Stardust0831 ac15e75
Refactor Molden functions for pseudopotential handling
Stardust0831 0619f19
Enhance ABACUS lowf file handling and moldengen function
Stardust0831 070a9f7
Molden wavefunction filename compatibility
Stardust0831 a0b0e25
Add default metadata writing in molden.py
Stardust0831 aab9ee0
Update tools/molden/molden.py
Stardust0831 f82508e
Refactor moldengen to remove ikpoint parameter
Stardust0831 47dc44a
Refactor Molden file handling functions
Stardust0831 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #!/usr/bin/env python3 | ||
| """Generate ABACUS Molden files with [Nval] metadata for Multiwfn. | ||
|
|
||
| Keep this script in the Multiwfn_interface directory so it can locate | ||
| tools/molden/molden.py by relative path. Users may add this directory to PATH | ||
| and run chmod +x on this file to use it as a command. | ||
| """ | ||
|
|
||
| from pathlib import Path | ||
| import argparse | ||
| import importlib.util | ||
| import os | ||
|
|
||
| DEFAULT_FOLDER = "$(pwd)" | ||
|
|
||
|
|
||
| def load_molden_module(): | ||
| """Load tools/molden/molden.py without requiring it to be installed.""" | ||
| molden_path = Path(__file__).resolve().parents[2] / "tools" / "molden" / "molden.py" | ||
| spec = importlib.util.spec_from_file_location("abacus_molden", molden_path) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| def write_molden_nval(kinds, zvals): | ||
| """Write Multiwfn-oriented Molden [Nval] values per element.""" | ||
| out = "[Nval]\n" | ||
| for elem, zval in zip(kinds, zvals): | ||
| out += f"{elem} {zval:g}\n" | ||
| return out | ||
|
|
||
|
|
||
| def make_molden_nval_section(molden, folder): | ||
| """Build [Nval] from UPF z_valence without adding this logic to tools/molden.""" | ||
| folder = os.path.abspath(folder) | ||
| kv = molden.read_abacus_input(os.path.join(folder, "INPUT")) | ||
| stru = molden.read_abacus_stru(os.path.join(folder, kv.get("stru_file", "STRU"))) | ||
| pseudo_dir = kv.get("pseudo_dir", "./") | ||
| if not os.path.isabs(pseudo_dir): | ||
| pseudo_dir = os.path.join(folder, pseudo_dir) | ||
| kinds = [spec["symbol"] for spec in stru["species"]] | ||
| fpps = [os.path.abspath(os.path.join(pseudo_dir, spec["pp_file"])) for spec in stru["species"]] | ||
| zvals = [molden.read_upf_z_valence(fpp) for fpp in fpps] | ||
| return write_molden_nval(kinds, zvals) | ||
|
|
||
|
|
||
| def insert_molden_nval(text, nval): | ||
| """Insert [Nval] after [Atoms] and before [GTO].""" | ||
| marker = "[GTO]\n" | ||
| index = text.find(marker) | ||
| if index < 0: | ||
| raise ValueError("Cannot insert [Nval]: [GTO] section was not found") | ||
| return text[:index] + nval + text[index:] | ||
|
|
||
|
|
||
| def moldengen(folder, ndigits=3, ngto=7, rel_r="2", fmolden="ABACUS_Multiwfn.molden", write_cell=True, with_nval=True, with_pseudo=False): | ||
| """Call tools/molden/molden.py with Multiwfn-oriented [Nval] metadata.""" | ||
| molden = load_molden_module() | ||
| pseudo = "pseudo" if with_pseudo else "none" | ||
| out = molden.moldengen(folder, ndigits, ngto, rel_r, fmolden, write_cell, pseudo) | ||
| if with_nval: | ||
| out = insert_molden_nval(out, make_molden_nval_section(molden, folder)) | ||
| with open(fmolden, "w") as file: | ||
| file.write(out) | ||
| return out | ||
|
|
||
|
|
||
|
|
||
| def _argparse(): | ||
| def str_to_bool(value): | ||
| if isinstance(value, bool): | ||
| return value | ||
| value = value.lower() | ||
| if value in ("true", "t", "yes", "y", "1"): | ||
| return True | ||
| if value in ("false", "f", "no", "n", "0"): | ||
| return False | ||
| raise argparse.ArgumentTypeError("expected true or false") | ||
|
|
||
| parser = argparse.ArgumentParser( | ||
| description="Generate Molden file from ABACUS LCAO calculation for Multiwfn", | ||
| formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
| ) | ||
| parser.add_argument("-f", "--folder", type=str, default=DEFAULT_FOLDER, help="the folder of the ABACUS calculation") | ||
| parser.add_argument("-n", "--ndigits", type=int, default=3, help="the number of digits for the MO coefficients") | ||
| parser.add_argument("-g", "--ngto", type=int, default=7, help="the number of GTOs to fit ABACUS NAOs") | ||
| parser.add_argument("-r", "--rel_r", type=str, default="2", help="the relative cutoff radius for the GTOs; comma-separated values enable multi-start fitting") | ||
| parser.add_argument("--with-cell", type=str_to_bool, default=True, help="whether to write the Molden [Cell] section") | ||
| parser.add_argument("--with-Nval", dest="with_nval", type=str_to_bool, default=True, help="whether to write the Molden [Nval] section") | ||
| parser.add_argument("--with-pseudo", type=str_to_bool, default=False, help="whether to write the Molden [Pseudo] section") | ||
| parser.add_argument("-o", "--output", type=str, default="ABACUS_Multiwfn.molden", help="the output Molden file name") | ||
| args = parser.parse_args() | ||
| if args.folder == DEFAULT_FOLDER: | ||
| args.folder = os.getcwd() | ||
| return args | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| args = _argparse() | ||
| moldengen(args.folder, args.ndigits, args.ngto, args.rel_r, args.output, args.with_cell, args.with_nval, args.with_pseudo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.