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

Add-output-flag #24

Merged
merged 5 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ jobs:
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
- name: Get the tag name
id: get_tag_name
run: |
TAG_NAME=$(echo "${{ github.head_ref }}" | sed -e 's/release-//')
echo "tag_name=${TAG_NAME}" >> $GITHUB_ENV
- name: Create tag
run: |
git tag ${{ env.tag_name }}
git push origin ${{ env.tag_name }}
- name: Create release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: ${{ github.ref }}
tag_name: ${{ env.tag_name }}
release_name: ${{ env.tag_name }}
3 changes: 1 addition & 2 deletions .github/workflows/release-pr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: sum_dirac_dfcoef-release-pull-request
name: create-release-pull-request

on:
workflow_dispatch:
Expand Down Expand Up @@ -63,7 +63,6 @@ jobs:
git checkout -b "${{ env.BRANCH_NAME }}"
git add .
git commit -m "Release ${{ env.PACKAGE_NAME }} ${{ env.NEW_VERSION }}"
git tag "${{ env.TAG_NAME }}"
git push origin "${{ env.BRANCH_NAME }}"
gh pr create --title "Release ${{ env.PACKAGE_NAME }} ${{ env.NEW_VERSION }}" --body "Release ${{ env.PACKAGE_NAME }} ${{ env.NEW_VERSION }}"
env:
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ pip install sum_dirac_dfcoef
You can use this program with the following command!

```sh
/path/to/sum_dirac_dfcoef -i DIRAC_OUPUT_FILE_PATH -m MOLECULE_NAME
# Output to MOLECULE_NAME.out
sum_dirac_dfcoef -i DIRAC_OUPUT_FILE_PATH -m MOLECULE_NAME
# Specify output file name with -o option
sum_dirac_dfcoef -i DIRAC_OUPUT_FILE_PATH -m MOLECULE_NAME -o OUTPUT_FILE_NAME
```

(e.g.)

```sh
./sum_dirac_dfcoef -i x2c_uo2_238.out -m UO2
sum_dirac_dfcoef -i x2c_uo2_238.out -m UO2
```

A part of x2c_uo2_238.out (DIRAC output file, ... represents an omission)
Expand Down Expand Up @@ -115,6 +118,9 @@ optional arguments (--input and --mol are required)
(required) molecule specification. Write the molecular formula (e.g. Cu2O).
**DON'T write the rational formula (e.g. CH3OH)**

- -o OUTPUT, --output OUTPUT
Output file name. Default: (-m or --mol option value).out (e.g) --m H2O => print to H2O.out

- -c, --compress

Compress output. Display all coefficients on one line for each MO.
Expand All @@ -130,6 +136,12 @@ optional arguments (--input and --mol are required)
Set the decimal places. Default: 5
(e.g) --decimal=3 => print orbital with 3 decimal places (0.123, 2.456, ...). range: 1-15

- -a, --all-write
Print all MOs(Positronic and Electronic).

- -p, --positronic-write
Print only Positronic MOs.

- --debug

print debug output (Normalization constant, Sum of MO coefficient)
Expand Down
44 changes: 30 additions & 14 deletions src/sum_dirac_dfcoef/sum_dirac_dfcoef.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python3

import argparse
from io import TextIOWrapper
import os
import re
import sys

Expand Down Expand Up @@ -85,6 +87,7 @@ def parse_args() -> "argparse.Namespace":
parser = argparse.ArgumentParser(description="Summarize the coefficients from DIRAC output file that *PRIVEC option is used. (c.f. http://www.diracprogram.org/doc/master/manual/analyze/privec.html)")
parser.add_argument("-i", "--input", type=str, required=True, help="(required) file name of DIRAC output", dest="file")
parser.add_argument("-m", "--mol", type=str, required=True, help="(required) molecule specification. Write the molecular formula (e.g. Cu2O). ** DON'T write the rational formula (e.g. CH3OH) **")
parser.add_argument("-o", "--output", type=str, help="Output file name. Default: (-m or --mol option value).out (e.g) --m H2O => print to H2O.out", dest="output")
parser.add_argument("-c", "--compress", action="store_true", help="Compress output. Display all coefficients on one line for each MO. This options is useful when you want to use the result in a spreadsheet like Microsoft Excel.", dest="compress")
parser.add_argument("-t", "--threshold", type=float, default=0.1, help="threshold. Default: 0.1 %% (e.g) --threshold=0.1 => print orbital with more than 0.1 %% contribution", dest="threshold")
parser.add_argument("-d", "--decimal", type=int, default=5, choices=range(1, 16), help="Set the decimal places. Default: 5 (e.g) --decimal=3 => print orbital with 3 decimal places (0.123, 2.456, ...). range: 1-15", dest="decimal")
Expand Down Expand Up @@ -148,7 +151,7 @@ def space_separated_parsing(line: str) -> "list[str]":
return [word for word in words if word != ""]


def parse_molecule_input(args: "argparse.Namespace", elements: "list[str]") -> Atoms:
def parse_molecule_input(args: "argparse.Namespace") -> Atoms:
"""
Parse the molecule input and return the Atoms object.

Expand Down Expand Up @@ -342,28 +345,38 @@ def check_end_vector_print(
return False


def write_results(args: "argparse.Namespace", data_all_mo: "list[Data_per_MO]") -> None:
def get_output_path(args: "argparse.Namespace") -> str:
if args.output is None:
output_name = f"{args.mol}.out"
output_path = os.path.join(os.getcwd(), output_name)
else:
output_name = args.output
output_path = os.path.abspath(output_name)
return output_path


def write_results(args: "argparse.Namespace", file: TextIOWrapper, data_all_mo: "list[Data_per_MO]") -> None:
"""
Write results to stdout
"""

for mo in data_all_mo:
digit_int = len(str(int(mo.mo_energy))) # number of digits of integer part
print(
f"{mo.mo_info} {mo.mo_energy:{digit_int}.{args.decimal}f}",
end="" if args.compress else "\n",
)
# File write but if args.compress is True \n is not added
mo_info_energy = f"{mo.mo_info} {mo.mo_energy:{digit_int}.{args.decimal}f}" + ("\n" if not args.compress else "")
file.write(mo_info_energy)

d: Data_per_orbital_types
for d in mo.data_per_orbital_types:
if args.compress:
orb_type = str(d.orbital_type)
output_str = f" {orb_type} {d.mo_percentage:.{args.decimal}f}"
print(output_str, end="")
file.write(output_str)
else:
orb_type = str(d.orbital_type).ljust(11, " ")
output_str = f"{orb_type} {d.mo_percentage:{args.decimal+4}.{args.decimal}f} %"
print(output_str)
print() # "\n"
output_str = f"{orb_type} {d.mo_percentage:{args.decimal+4}.{args.decimal}f} %\n"
file.write(output_str)
file.write("\n") # add empty line
debug_print_wrapper(args, f"Normalization constant is {mo.norm_constant:.{args.decimal}f}")
debug_print_wrapper(args, f"sum of coefficient {mo.sum_coefficients:.{args.decimal}f}")

Expand All @@ -384,7 +397,7 @@ def main() -> None:

args: "argparse.Namespace" = parse_args()
dirac_file: str = get_dirac_filename(args)
atoms: Atoms = parse_molecule_input(args, elements)
atoms: Atoms = parse_molecule_input(args)

data_all_electronic_mo: "list[Data_per_MO]" = []
data_all_positronic_mo: "list[Data_per_MO]" = []
Expand Down Expand Up @@ -472,12 +485,15 @@ def main() -> None:
is_reading_coefficients = True
get_coefficient(words, atoms, coefficients, elements)
# End of reading file
output_path = get_output_path(args)
file = open(output_path, "w")
if args.all_write or args.positronic_write:
if not args.no_sort:
data_all_positronic_mo.sort(key=lambda x: x.mo_energy)
write_results(args, data_all_positronic_mo)
print() # Add a blank line
write_results(args, file, data_all_positronic_mo)
file.write("\n") # Add a blank line
if args.all_write or not args.positronic_write: # Electronic
if not args.no_sort:
data_all_electronic_mo.sort(key=lambda x: x.mo_energy)
write_results(args, data_all_electronic_mo)
write_results(args, file, data_all_electronic_mo)
file.close()
38 changes: 16 additions & 22 deletions test/unit_test.py → test/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,15 @@ def test_sum_dirac_dfcoeff_compress(ref_filename: str, result_filename: str, inp
result_filepath = os.path.join(test_path, "results", result_filename)
input_filepath = os.path.join(test_path, "data", input_filename)

test_command = f"sum_dirac_dfcoef -m {mol} -i {input_filepath} {options}"
test_command = f"sum_dirac_dfcoef -m {mol} -i {input_filepath} -o {result_filepath} {options}"
print(test_command)
with open(result_filepath, "w") as file_output:
process = subprocess.run(
test_command,
shell=True,
stdout=file_output,
encoding="utf-8",
stderr=file_output,
)
if process.returncode != 0:
sys.exit(f"{test_command} failed with return code {process.returncode}")
process = subprocess.run(
test_command,
shell=True,
encoding="utf-8",
)
if process.returncode != 0:
sys.exit(f"{test_command} failed with return code {process.returncode}")

ref_file: "list[list[str]]" = [re.split(" +", line.rstrip("\n")) for line in list(filter(lambda val: val != "", open(ref_filepath, "r").read().splitlines()))]
out_file: "list[list[str]]" = [re.split(" +", line.rstrip("\n")) for line in list(filter(lambda val: val != "", open(result_filepath, "r").read().splitlines()))]
Expand Down Expand Up @@ -108,18 +105,15 @@ def test_sum_dirac_dfcoeff(ref_filename: str, result_filename: str, input_filena
result_filepath = os.path.join(test_path, "results", result_filename)
input_filepath = os.path.join(test_path, "data", input_filename)

test_command = f"sum_dirac_dfcoef -m {mol} -i {input_filepath} {options}"
test_command = f"sum_dirac_dfcoef -m {mol} -i {input_filepath} -o {result_filepath} {options}"
print(test_command)
with open(result_filepath, "w") as file_output:
process = subprocess.run(
test_command,
shell=True,
stdout=file_output,
encoding="utf-8",
stderr=file_output,
)
if process.returncode != 0:
sys.exit(f"{test_command} failed with return code {process.returncode}")
process = subprocess.run(
test_command,
shell=True,
encoding="utf-8",
)
if process.returncode != 0:
sys.exit(f"{test_command} failed with return code {process.returncode}")

ref_file: "list[list[str]]" = [re.split(" +", line.rstrip("\n")) for line in list(filter(lambda val: val != "", open(ref_filepath, "r").read().splitlines()))]
out_file: "list[list[str]]" = [re.split(" +", line.rstrip("\n")) for line in list(filter(lambda val: val != "", open(result_filepath, "r").read().splitlines()))]
Expand Down