Skip to content

Commit

Permalink
Merge pull request #33 from robinzyb/devel
Browse files Browse the repository at this point in the history
Add support for NPT_F AIMD of cp2k version 7.1
  • Loading branch information
robinzyb authored Dec 26, 2023
2 parents a272002 + ceac57d commit 39b268f
Show file tree
Hide file tree
Showing 17 changed files with 15,102 additions and 8 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ pip install cp2kdata
```

## From source
One can download the source code of cp2kdata by
```bash
git clone https://github.com/robinzyb/cp2kdata.git cp2kdata
```
then use `pip` to install the module from source

```bash
cd cp2kdata
pip install .
```

Expand Down
42 changes: 38 additions & 4 deletions cp2kdata/block_parser/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from ase.geometry.cell import cellpar_to_cell
from ase.geometry.cell import cell_to_cellpar
from typing import List
from .header_info import Cp2kInfo
from ..units import au2A

ALL_CELL_RE = re.compile(
r"""
Expand Down Expand Up @@ -44,17 +46,34 @@ def parse_all_cells(output_file):
else:
return None

ALL_MD_CELL_RE_V7 = re.compile(
r"""
\sCELL\sLNTHS\[bohr\]\s{13}=\s
\s{3}(?P<a>\d\.\d{7}E(\+|\-)\d{2})
\s{3}(?P<b>\d\.\d{7}E(\+|\-)\d{2})
\s{3}(?P<c>\d\.\d{7}E(\+|\-)\d{2})
\n
#skip one line
(.{80}\n){1}
#parse angles
\sCELL\sANGLS\[deg\]\s{14}=\s
\s{3}(?P<alpha>\d\.\d{7}E(\+|\-)\d{2})
\s{3}(?P<beta>\d\.\d{7}E(\+|\-)\d{2})
\s{3}(?P<gamma>\d\.\d{7}E(\+|\-)\d{2})
""",
re.VERBOSE
)

ALL_MD_CELL_RE = re.compile(
ALL_MD_CELL_RE_V2023 = re.compile(
r"""
#parse cell lengths
\sMD\|\sCell\slengths\s\[ang\]\s{9}
\sMD\|\sCell\slengths\s\[bohr\]\s{8}
\s{2}(?P<a>\d\.\d{8}E(\+|\-)\d{2})
\s{2}(?P<b>\d\.\d{8}E(\+|\-)\d{2})
\s{2}(?P<c>\d\.\d{8}E(\+|\-)\d{2})
\n
#skip two lines
(.{80}\n){2}
#skip three lines
(.{80}\n){3}
#parse angles
(\sMD\|\sCell\sangles\s\[deg\]\s{10}
\s{2}(?P<alpha>\d\.\d{8}E(\+|\-)\d{2})
Expand All @@ -65,11 +84,23 @@ def parse_all_cells(output_file):
)

def parse_all_md_cells(output_file: List[str],
cp2k_info: Cp2kInfo,
init_cell_info=None):
# init_cell_info are used for npt_I parse.
# because npt_I doesn't include angle info in MD| block

# notice that the cell of step 0 is excluded from MD| block

# choose parser according to cp2k_info.version
if cp2k_info.version in ['2023.1']:
ALL_MD_CELL_RE = ALL_MD_CELL_RE_V2023
elif cp2k_info.version in ['7.1']:
ALL_MD_CELL_RE = ALL_MD_CELL_RE_V7
else:
WARNING = f"cp2k version={cp2k_info.version} is not supported yet \
for parsing MD cell from cp2k log files."
raise NotImplementedError(WARNING)

all_md_cells = []
if init_cell_info is None:
# for NPT_F parser, cell info is complete in MD| block
Expand All @@ -78,6 +109,9 @@ def parse_all_md_cells(output_file: List[str],
cell = [match["a"], match["b"], match["c"],
match["alpha"], match["beta"], match["gamma"]]
cell = np.array(cell, dtype=float)
# convert bohr to angstrom
cell[:3] = cell[:3] * au2A
# make sure cell length are in angstrom and cell angles are in degree before sent to cellpar_to_cell
cell = cellpar_to_cell(cell)
all_md_cells.append(cell)
else:
Expand Down
40 changes: 36 additions & 4 deletions cp2kdata/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def __init__(self, output_file=None, run_type: str=None, path_prefix=".", **kwar
self.atom_kind_list = None

# -- start parse necessary information --

# sometimes I use self.filename and sometimes I use self.output_file
# self.filename is used for parsing information by monty package.
if self.filename:
with open(self.filename, 'r') as fp:
self.output_file = fp.read()
Expand Down Expand Up @@ -381,7 +382,31 @@ def parse_md(self):
self.num_frames = len(self.energies_list)

# here parse cell information

WARNING_MSG_PARSE_CELL_FROM_OUTPUT = \
(
"\n"
"cp2kdata is parsing md cell information from output file.\n"
"The raw data of cell information are lengths and angles,\n"
"which are latter transformed to cell matrices by codes.\n"
"However, the a axis of the cell are always assumed to be aligned to "
"the x axis of the coordinate.\n"
"Make sure the a axis in real cell matrices are always aligned to x axis.\n"
"Otherwise, parsing cell information from `-1.cell` file is recommended.\n"

"CP2K input setting\n"
"------------------\n"
"&MOTION\n"
" &PRINT\n"
" &CELL\n"
" &EACH\n"
" MD 1\n"
" &END EACH\n"
" &END CELL\n"
" &END PRINT\n"
"&END MOTION\n"
"------------------\n"
)

WARNING_MSG = "cp2kdata obtains more than one initial cell from the output file, \
please check if your output file has duplicated header information."

Expand All @@ -393,6 +418,7 @@ def parse_md(self):
self.all_cells = parse_md_cell(cell_file_list[0])
elif self.filename:
print(f"Parsing Cells Information from {self.filename}")
print(WARNING_MSG_PARSE_CELL_FROM_OUTPUT)
# parse the first cell
first_cell = parse_all_cells(self.output_file)
assert first_cell.shape == (1, 3, 3), WARNING_MSG
Expand All @@ -401,14 +427,17 @@ def parse_md(self):

elif (self.md_info.ensemble_type == "NPT_F"):
if cell_file_list:
# all cells include initial cell
self.all_cells = parse_md_cell(cell_file_list[0])
elif self.filename:
print(f"Parsing Cells Information from {self.filename}")
print(WARNING_MSG_PARSE_CELL_FROM_OUTPUT)
# only parse the first cell
first_cell = parse_all_cells(self.output_file)
assert first_cell.shape == (1, 3, 3), WARNING_MSG
# parse the rest of the cells
self.all_cells = parse_all_md_cells(self.output_file)
self.all_cells = parse_all_md_cells(self.output_file,
cp2k_info=self.cp2k_info)
# prepend the first cell
self.all_cells = np.insert(self.all_cells, 0, first_cell[0], axis=0)

Expand All @@ -417,11 +446,14 @@ def parse_md(self):
self.all_cells = parse_md_cell(cell_file_list[0])
elif self.filename:
print(f"Parsing Cells Information from {self.filename}")
print(WARNING_MSG_PARSE_CELL_FROM_OUTPUT)
# only parse the first cell
first_cell = parse_all_cells(self.output_file)
assert first_cell.shape == (1, 3, 3), WARNING_MSG
# parse the rest of the cells
self.all_cells = parse_all_md_cells(self.output_file, init_cell_info=first_cell[0])
self.all_cells = parse_all_md_cells(self.output_file,
cp2k_info=self.cp2k_info,
init_cell_info=first_cell[0])
# prepend the first cell
self.all_cells = np.insert(self.all_cells, 0, first_cell[0], axis=0)

Expand Down
1 change: 1 addition & 0 deletions tests/test_dpdata/test_labeledsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"tests/test_dpdata/v7.1/aimd",
"tests/test_dpdata/v7.1/aimd_virial",
"tests/test_dpdata/v7.1/aimd_virial_in_output",
"tests/test_dpdata/v7.1/aimd_npt_f",
"tests/test_dpdata/v9.1/xTBmd_npt_i",
"tests/test_dpdata/v2022.1/aimd",
"tests/test_dpdata/v2023.1/aimd_nvt",
Expand Down
12 changes: 12 additions & 0 deletions tests/test_dpdata/v7.1/aimd_npt_f/cp2k-1.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Step Time [fs] Ax [Angstrom] Ay [Angstrom] Az [Angstrom] Bx [Angstrom] By [Angstrom] Bz [Angstrom] Cx [Angstrom] Cy [Angstrom] Cz [Angstrom] Volume [Angstrom^3]
0 0.000 10.8816000000 0.0000000000 0.0000000000 0.0000000000 11.7514000000 0.0000000000 -1.1598908200 0.0000000000 13.0710380200 1671.4463633218
1 0.500 10.8814341200 -0.0002217195 -0.0003203866 -0.0002394422 11.7521186805 -0.0005344379 -1.1602579887 -0.0005708197 13.0713505249 1671.5586930675
2 1.000 10.8811912125 -0.0004442515 -0.0006285721 -0.0004797627 11.7527459024 -0.0010696326 -1.1606022904 -0.0011423941 13.0715657479 1671.6338953287
3 1.500 10.8808545323 -0.0006684499 -0.0009278013 -0.0007218845 11.7532621447 -0.0016057383 -1.1609258375 -0.0017148027 13.0716636857 1671.6640155624
4 2.000 10.8804020295 -0.0008949101 -0.0012223081 -0.0009664509 11.7536414355 -0.0021424621 -1.1612313657 -0.0022876557 13.0716198956 1671.6387872865
5 2.500 10.8798110576 -0.0011240088 -0.0015165912 -0.0012138687 11.7538572966 -0.0026792379 -1.1615218647 -0.0028602818 13.0714107000 1671.5478780533
6 3.000 10.8790613427 -0.0013559841 -0.0018148565 -0.0014643947 11.7538864701 -0.0032154100 -1.1618002258 -0.0034319251 13.0710158255 1671.3822233554
7 3.500 10.8781362088 -0.0015910057 -0.0021207053 -0.0017182115 11.7537104302 -0.0037503422 -1.1620689973 -0.0040018584 13.0704191016 1671.1345254833
8 4.000 10.8770227323 -0.0018292110 -0.0024370403 -0.0019754666 11.7533155286 -0.0042834585 -1.1623302888 -0.0045694252 13.0696081858 1670.7992658377
9 4.500 10.8757114133 -0.0020707183 -0.0027660925 -0.0022362865 11.7526925232 -0.0048142510 -1.1625857674 -0.0051340471 13.0685739905 1670.3725147474
10 5.000 10.8741956665 -0.0023156293 -0.0031094830 -0.0025007800 11.7518359120 -0.0053422777 -1.1628366788 -0.0056952212 13.0673100682 1669.8516797212
12 changes: 12 additions & 0 deletions tests/test_dpdata/v7.1/aimd_npt_f/cp2k-1.ener
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Step Nr. Time[fs] Kin.[a.u.] Temp[K] Pot.[a.u.] Cons Qty[a.u.] UsedTime[s]
0 0.000000 0.270509287 298.150000000 -1434.890430434 -1423.113274920 0.000000000
1 0.500000 0.479498232 528.493491743 -1435.112554337 -1423.126030681 83.453355431
2 1.000000 0.912615879 1005.867217728 -1435.565067163 -1423.145191274 9.512562810
3 1.500000 1.288595822 1420.264893143 -1435.949289652 -1423.153337717 8.374228416
4 2.000000 1.448048957 1596.010993008 -1436.105922341 -1423.150511726 8.403625603
5 2.500000 1.390457558 1532.534889466 -1436.041562293 -1423.143753361 8.440521950
6 3.000000 1.191243695 1312.965299372 -1435.836195657 -1423.137547390 8.425562744
7 3.500000 0.932162631 1027.411262168 -1435.572982185 -1423.133231174 8.508526123
8 4.000000 0.674429735 743.343148291 -1435.313037878 -1423.130658398 8.454226865
9 4.500000 0.456403263 503.038672115 -1435.094231704 -1423.129314540 8.428309161
10 5.000000 0.298943461 329.489585377 -1434.936965694 -1423.128735307 8.459636181
Loading

0 comments on commit 39b268f

Please sign in to comment.