Skip to content

Commit 8768208

Browse files
committed
update some type
1 parent 7d9c0f7 commit 8768208

4 files changed

Lines changed: 71 additions & 24 deletions

File tree

src/mdkits/util/arg_type.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import click, os
2-
from . import cp2k_input_parsing
2+
from ase.io import read
3+
import numpy as np
4+
from ase.collections import g2
5+
from mdkits.util import os_operation, cp2k_input_parsing, out_err
36

47

58
class CellType(click.ParamType):
@@ -8,16 +11,17 @@ class CellType(click.ParamType):
811
def convert(self, value, param, ctx):
912
if isinstance(value, str):
1013
if ',' not in value:
11-
cell = cp2k_input_parsing.parse_cell(value)
14+
cell = cp2k_input_parsing.parse_cell()
1215
return cell
1316
else:
1417
cell = [float(x) for x in value.split(',')]
1518

1619
if len(cell) == 3:
17-
click.echo(f"system cell: x = {cell[0]}, y = {cell[1]}, z = {cell[2]}, a = {90}\u00B0, b = {90}\u00B0, c = {90}\u00B0")
18-
return cell + [90, 90, 90]
20+
cell += [90, 90, 90]
21+
out_err.cell_output(cell)
22+
return cell
1923
elif len(cell) == 6:
20-
click.echo(f"system cell: x = {cell[0]}, y = {cell[1]}, z = {cell[2]}, a = {cell[3]}\u00B0, b = {cell[4]}\u00B0, c = {cell[5]}\u00B0")
24+
out_err.cell_output(cell)
2125
return cell
2226
else:
2327
self.fail(f"{value} is not a valid cell parameter", param, ctx)
@@ -37,16 +41,45 @@ def convert(self, value, param, ctx):
3741
self.fail(f"{value} is not a valid frame range", param, ctx)
3842

3943

40-
from ase.collections import g2
44+
class StructureType(click.ParamType):
45+
name = "structure file type"
46+
def convert(self, value, param, ctx):
47+
no_cell=np.array([0., 0., 0., 90., 90., 90.])
48+
if isinstance(value, str):
49+
if os.path.exists(value):
50+
try:
51+
atoms = read(value)
52+
except:
53+
self.fail(f"{value} is not a valid structure file", param, ctx)
54+
55+
if np.array_equal(atoms.cell.cellpar(), no_cell):
56+
cell = cp2k_input_parsing.parse_cell()
57+
atoms.set_cell(cell)
58+
59+
return atoms
60+
else:
61+
self.fail(f"{value} is not exists", param, ctx)
62+
63+
64+
4165
class MoleculeType(click.Choice):
4266
name = "mocular type"
4367
def __init__(self):
4468
super().__init__(self)
4569
g2.names.append(click.Path(exists=True))
4670
self.choices = tuple(g2.names)
4771

72+
class AdsSiteType(click.Choice):
73+
name = "adsorption site"
74+
def __init__(self):
75+
super().__init__(self)
76+
site = ['ontop', 'hollow','fcc', 'hcp', 'bridge', 'shortbridge', 'longbridge']
77+
self.choices = tuple(site)
78+
4879

4980

5081
Cell = CellType()
5182
FrameRange = FrameRangeType()
5283
Molecule = MoleculeType()
84+
AdsSite = AdsSiteType()
85+
Structure = StructureType()

src/mdkits/util/cp2k_input_parsing.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,35 @@
55

66

77
import sys
8+
from mdkits.util import os_operation, out_err
89

910

10-
def parse_cell(cp2k_input_file):
11+
def parse_cell():
1112
"""
1213
function: parse cell information from cp2k input file
1314
parameter:
1415
cp2k_input_file: filename of cp2k input
1516
return:
1617
cell: list with 6 number
1718
"""
18-
try:
19-
with open(cp2k_input_file, 'r') as f:
20-
cell = []
21-
for line in f:
22-
if "ABC" in line:
23-
xyz = line.split()[-3:]
24-
cell.extend(xyz)
25-
if "ALPHA_BETA_GAMMA" in line:
26-
abc = line.split()[-3:]
27-
cell.extend(abc)
28-
if len(cell) == 3:
29-
cell.extend([90.0, 90.0, 90.0])
19+
for file in os_operation.default_input:
20+
try:
21+
with open(file, 'r') as f:
22+
cell = []
23+
for line in f:
24+
if "ABC" in line:
25+
xyz = line.split()[-3:]
26+
cell.extend(xyz)
27+
if "ALPHA_BETA_GAMMA" in line:
28+
abc = line.split()[-3:]
29+
cell.extend(abc)
30+
if len(cell) == 3:
31+
cell.extend([90.0, 90.0, 90.0])
3032

31-
print(f"system cell: x = {cell[0]}, y = {cell[1]}, z = {cell[2]}, a = {cell[3]}\u00B0, b = {cell[4]}\u00B0, c = {cell[5]}\u00B0")
32-
return cell
33-
except FileNotFoundError:
34-
print(f'cp2k input file name "{cp2k_input_file}" is not found, assign a cp2k input file or assign a "cell"')
35-
sys.exit(1)
33+
out_err.cell_output(cell)
34+
return cell
35+
except FileNotFoundError:
36+
sys.exit(f"cant parse cell information from {','.join(os_operation.default_input)}, assign a cell")
3637

3738

3839
#def get_cell(cp2k_input_file, cell=None):

src/mdkits/util/os_operation.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ def sort_word_and_number(unsort_list):
3333
sorted_list = sorted(unsort_list, key=fns)
3434

3535
return sorted_list
36+
37+
38+
def default_input():
39+
default_input_name = os.environ.get("DEFAULT_INPUT", "input.inp,setup.inp,cell.inc").split(',')
40+
return default_input_name

src/mdkits/util/out_err.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""
2+
output and error for cli
3+
"""
4+
5+
6+
def cell_output(cell: list):
7+
print(f"system cell: x = {cell[0]}, y = {cell[1]}, z = {cell[2]}, a = {cell[3]}\u00B0, b = {cell[4]}\u00B0, c = {cell[5]}\u00B0")
8+

0 commit comments

Comments
 (0)