Skip to content

Commit

Permalink
fix the bug for parsing stresses and forces from restart md output
Browse files Browse the repository at this point in the history
  • Loading branch information
robinzyb committed Aug 26, 2024
1 parent 19e4202 commit 10c6ebf
Show file tree
Hide file tree
Showing 18 changed files with 2,153 additions and 14 deletions.
4 changes: 4 additions & 0 deletions cp2kdata/block_parser/header_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from dataclasses import dataclass
# use monty.re because it can terminate on match
from monty.re import regrep
from cp2kdata.log import get_logger

logger = get_logger(__name__)

@dataclass
class Cp2kInfo:
Expand Down Expand Up @@ -43,6 +45,8 @@ def parse_cp2k_info(filename) -> Cp2kInfo:
raise ValueError(f"More than one restart information found in {filename}")
elif num_match_restart == 1:
cp2k_restart = True
logger.debug(f"Found restart information in the output header")

elif num_match_restart == 0:
cp2k_restart = False

Expand Down
30 changes: 25 additions & 5 deletions cp2kdata/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def __init__(
# restart should be true or false
if restart is not None:
self.cp2k_info.restart = restart
logger.debug("Overwrite restart information with user provided restart = True.")

self.check_run_type(run_type=self.global_info.run_type)

Expand Down Expand Up @@ -401,8 +402,12 @@ def parse_md(self):
format_logger(info="Forces", filename=self.filename)
self.atomic_forces_list = parse_atomic_forces_list(
self.output_file)

self.atomic_forces_list = self.drop_first_info(
self.cp2k_info, self.atomic_forces_list, info="forces")

self.atomic_forces_list = self.drop_last_info(
self.cp2k_info, self.atomic_forces_list)
self.cp2k_info, self.atomic_forces_list, info="forces")

stress_file_list = glob.glob(
os.path.join(self.path_prefix, "*.stress"))
Expand All @@ -421,8 +426,13 @@ def parse_md(self):
self.stress_tensor_list = parse_stress_tensor_list(
self.output_file)

self.stress_tensor_list = self.drop_last_info(
self.cp2k_info, self.stress_tensor_list)
# stress tensor could be None if the output file doesn't contain stress information
if self.stress_tensor_list is not None:
self.stress_tensor_list = self.drop_first_info(
self.cp2k_info, self.stress_tensor_list, info="stresses")

self.stress_tensor_list = self.drop_last_info(
self.cp2k_info, self.stress_tensor_list, info="stresses")

self.num_frames = len(self.energies_list)

Expand Down Expand Up @@ -521,11 +531,21 @@ def get_vib_freq_list(self):
return self.vib_freq_list

@staticmethod
def drop_last_info(cp2k_info, array):
def drop_first_info(cp2k_info, array, info="info"):
# drop first info parsed from output if it restart from preivous MD run
if cp2k_info.restart == True:
logger.info(
f"The cp2k output is restarted from previous MD run, drop the first {info}."
)
array = array[1:]
return array

@staticmethod
def drop_last_info(cp2k_info, array, info="info"):
# drop last info parsed from output if it is terminated by request (touch EXIT)
if cp2k_info.terminated_by_request == True:
print(
"cp2kdata found the cp2k output is terminated by request, drop the last info.")
f"The cp2k output is terminated by user request, drop the last {info}.")
array = array[:-1]
return array

Expand Down
8 changes: 0 additions & 8 deletions cp2kdata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ def get_rdf(pos, r, nbin, frames, elements):
tot_gr += s_gr/frames
return tot_gr


def printtbox(arg):
"""
This function is a print decorated with a few characters so that the
print function present a characteristic string. Useful for postprocess.
"""
print("--> CP2KDATA: {0}".format(arg))

def format_logger(info, filename):
logger.debug(f"Parsing {info:10} from {filename}")

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ name = "Cp2kData"
version = "0.7.1"
description = "A Small Package to Postprocess Cp2k Output"
authors = [
{name = "Yongbin Zhuang", email = "robinzhuang@outlook.com"}
{name = "Yong-Bin Zhuang", email = "robinzhuang@outlook.com"}
]
license = {file = "LICENSE"}
readme = "README.md"
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 @@ -23,6 +23,7 @@
"tests/test_dpdata/v2022.2/aimd_npt_i",
"tests/test_dpdata/v2023.1/aimd_nvt",
"tests/test_dpdata/v2023.1/aimd_npt_f",
"tests/test_dpdata/v2023.2/aimd_nvt_restart",
"tests/test_dpdata/v2024.1/aimd_npt_i_restart"
]

Expand Down
4 changes: 4 additions & 0 deletions tests/test_dpdata/v2023.2/aimd_nvt_restart/H2O_restart-1.cell
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Step Time [fs] Ax [Angstrom] Ay [Angstrom] Az [Angstrom] Bx [Angstrom] By [Angstrom] Bz [Angstrom] Cx [Angstrom] Cy [Angstrom] Cz [Angstrom] Volume [Angstrom^3]
20 20.000 12.6000000000 0.0000000000 0.0000000000 0.0000000000 12.6000000000 0.0000000000 0.0000000000 0.0000000000 12.6000000000 2000.3760000000
21 21.000 12.6000000000 0.0000000000 0.0000000000 0.0000000000 12.6000000000 0.0000000000 0.0000000000 0.0000000000 12.6000000000 2000.3760000000
22 22.000 12.6000000000 0.0000000000 0.0000000000 0.0000000000 12.6000000000 0.0000000000 0.0000000000 0.0000000000 12.6000000000 2000.3760000000
4 changes: 4 additions & 0 deletions tests/test_dpdata/v2023.2/aimd_nvt_restart/H2O_restart-1.ener
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Step Nr. Time[fs] Kin.[a.u.] Temp[K] Pot.[a.u.] Cons Qty[a.u.] UsedTime[s]
20 20.000000 0.023548744 413.116461989 -68.977821523 -68.938997773 60.277769099
21 21.000000 0.021234705 372.521196861 -68.975543745 -68.938783638 18.870602752
22 22.000000 0.018025570 316.223219413 -68.972324812 -68.938309176 18.600492027
42 changes: 42 additions & 0 deletions tests/test_dpdata/v2023.2/aimd_nvt_restart/H2O_restart-frc-1.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
12
i = 20, time = 20.000, E = -68.9778215227
O 0.0102121326 0.0082450214 0.0258173651
H 0.0011582980 -0.0132266233 -0.0121033560
H -0.0123640908 0.0039438113 -0.0130423386
O 0.0005078165 -0.0022005368 0.0001827544
H -0.0125006144 -0.0143064065 -0.0000614894
H 0.0132352169 0.0161774021 -0.0019829286
O -0.0052064897 0.0009704681 0.0071012763
H -0.0087157859 0.0035490175 0.0015360158
H 0.0131859471 -0.0043854925 -0.0089008239
O 0.0295545283 0.0081232015 0.0398825263
H -0.0149641237 -0.0001739602 -0.0009636305
H -0.0147979999 -0.0081434238 -0.0401153032
12
i = 21, time = 21.000, E = -68.9755437453
O 0.0303052317 -0.0065731816 0.0424437305
H -0.0016013990 -0.0009275749 -0.0040939960
H -0.0304565436 0.0059723068 -0.0378698251
O 0.0050859703 -0.0130343569 0.0220011662
H -0.0042172351 0.0055661721 -0.0139265178
H -0.0007481943 0.0074111628 -0.0098332246
O 0.0001483569 -0.0013082448 0.0424492183
H 0.0060439705 -0.0026121984 -0.0341943831
H -0.0069766144 0.0034709851 -0.0085099322
O 0.0025323271 0.0101493554 0.0437780376
H 0.0141092427 -0.0024741181 -0.0030324709
H -0.0168895666 -0.0075244283 -0.0421495104
12
i = 22, time = 22.000, E = -68.9723248123
O 0.0335055352 -0.0223836483 0.0346104595
H -0.0046719265 0.0143965413 0.0055441867
H -0.0306213401 0.0065952030 -0.0402792055
O 0.0091840210 -0.0181660032 0.0410637382
H 0.0043256547 0.0212885655 -0.0252439822
H -0.0144456375 -0.0028994354 -0.0176576872
O 0.0137783287 -0.0069222516 0.0597275234
H 0.0149959476 -0.0060552771 -0.0511823329
H -0.0296067618 0.0121169616 -0.0088047186
O -0.0336290172 0.0063977692 0.0209944277
H 0.0395185099 -0.0028786832 -0.0055621115
H -0.0058697372 -0.0028984716 -0.0165854098
42 changes: 42 additions & 0 deletions tests/test_dpdata/v2023.2/aimd_nvt_restart/H2O_restart-pos-1.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
12
i = 20, time = 20.000, E = -68.9778215227
O 6.3316694013 10.3265616457 7.6782392523
H 6.1616125544 11.1315146003 8.2239437014
H 5.7430023264 10.3807400784 6.9336632835
O 9.1302710853 3.1889367305 0.0923448994
H 9.4202654175 3.9665554155 -0.4318636659
H 8.3566036625 2.7447792754 -0.3225657198
O 8.1855081658 2.3324901533 8.5342295228
H 8.5124785846 2.1917043131 7.6300685257
H 7.2715408397 2.6874938692 8.5035021945
O 5.8384192760 0.7389161185 0.6066193230
H 6.8100691672 0.6360080101 0.4930624889
H 5.4496269488 0.5871999743 -0.2229783996
12
i = 21, time = 21.000, E = -68.9755437453
O 6.3293442164 10.3288510035 7.6890530379
H 6.1740088133 11.1227075298 8.2233525135
H 5.7769852068 10.3802955549 6.9499194570
O 9.1250871242 3.1883703779 0.0926188564
H 9.4289965462 3.9283149440 -0.4278630874
H 8.3858327801 2.7327027774 -0.3239566886
O 8.1848476170 2.3295429395 8.5347657971
H 8.5165756164 2.1908174768 7.6719777399
H 7.2917933703 2.6717934790 8.4917815049
O 5.8419706731 0.7321749871 0.5999709745
H 6.7859792755 0.6611477233 0.4872188236
H 5.4615480885 0.5949492113 -0.2334545867
12
i = 22, time = 22.000, E = -68.9723248123
O 6.3281856704 10.3305920763 7.7009772804
H 6.1843416649 11.1136446689 8.2209119268
H 5.7987343252 10.3828672891 6.9478903583
O 9.1202725251 3.1873331964 0.0935722685
H 9.4378150638 3.8939808908 -0.4307181071
H 8.4139336323 2.7247774685 -0.3310485390
O 8.1840146377 2.3261956596 8.5367461425
H 8.5236234033 2.1884016296 7.6935468334
H 7.3114734045 2.6581072367 8.4768182088
O 5.8452782009 0.7260571216 0.5950798598
H 6.7689899409 0.6869585853 0.4797432756
H 5.4641863879 0.5990944245 -0.2660469140
42 changes: 42 additions & 0 deletions tests/test_dpdata/v2023.2/aimd_nvt_restart/H2O_restart-vel-1.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
12
i = 20, time = 20.000, E = -68.9778215227
O -0.0001207346 0.0001074056 0.0004703622
H 0.0005855913 -0.0002574846 0.0001154687
H 0.0015998529 -0.0000622831 0.0008679529
O -0.0002418940 -0.0000233290 0.0000124543
H 0.0004797014 -0.0015905338 0.0001841869
H 0.0011941590 -0.0007528683 -0.0000373856
O -0.0000234917 -0.0001275842 0.0000186304
H 0.0002868016 -0.0000755973 0.0019817826
H 0.0007269848 -0.0006756403 -0.0004509122
O 0.0001475795 -0.0003198904 -0.0003427299
H -0.0009340985 0.0011038179 -0.0002542683
H 0.0007589708 0.0004430338 -0.0000267723
12
i = 21, time = 21.000, E = -68.9755437453
O -0.0000795425 0.0000918312 0.0005236683
H 0.0005186287 -0.0004079899 -0.0000691496
H 0.0012742614 0.0000486147 0.0003249063
O -0.0002286140 -0.0000366232 0.0000280261
H 0.0003974182 -0.0016667569 0.0000261553
H 0.0013137980 -0.0004568710 -0.0001927501
O -0.0000339289 -0.0001437524 0.0000573246
H 0.0002544726 -0.0000749897 0.0014528407
H 0.0009090015 -0.0006714956 -0.0006097483
O 0.0001568165 -0.0002942565 -0.0002637349
H -0.0009396582 0.0011656603 -0.0003041364
H 0.0003321132 0.0002716318 -0.0009836074
12
i = 22, time = 22.000, E = -68.9723248123
O -0.0000272789 0.0000584253 0.0005504840
H 0.0003970076 -0.0002502477 -0.0000466458
H 0.0006807280 0.0001984450 -0.0005602733
O -0.0002087397 -0.0000628053 0.0000727154
H 0.0005128035 -0.0012912908 -0.0004154540
H 0.0010987018 -0.0003854393 -0.0005791533
O -0.0000317493 -0.0001667080 0.0001405947
H 0.0004909749 -0.0001944337 0.0003862865
H 0.0006119356 -0.0004846438 -0.0007544230
O 0.0001210032 -0.0002674999 -0.0002014346
H -0.0003303746 0.0011846554 -0.0004095307
H 0.0000510254 0.0001585657 -0.0017250385
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/test_dpdata/v2023.2/aimd_nvt_restart/deepmd/type.raw
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
0
1
1
0
1
1
0
1
1
0
1
1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
O
H
Loading

0 comments on commit 10c6ebf

Please sign in to comment.