Commit bf92ab97 authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Started working on MD parsing, imporoved the CSV tool.

parent fe625fdc
......@@ -34,7 +34,7 @@ def iread(filepath, columns, delimiter=r"\s+", comments=r"#", start=None, end=No
if line:
return compiled_delimiter.split(line)
else:
return []
return None
def is_end(line):
"""Check if the given line matches the separator pattern.
......@@ -69,22 +69,11 @@ def iread(filepath, columns, delimiter=r"\s+", comments=r"#", start=None, end=No
# Start iterating
configuration = []
started = False
with open(filepath, "r") as f:
for line in f: # This actually reads line by line and only keeps the current line in memory
# If a start regex is provided, use it to detect the start of a configuration
if is_start(line):
started = True
continue
# If separator encountered, yield the stored configuration
if is_end(line):
started = False
if configuration:
yield np.array(configuration)
configuration = []
elif start is not None and started:
# If no starting and ending condition are provided, read configuration by line
if start is None and end is None:
with open(filepath, "r") as f:
for line in f: # This actually reads line by line and only keeps the current line in memory
# Ignore comments, separate by delimiter
vals = split_line(line)
line_forces = []
......@@ -102,9 +91,48 @@ def iread(filepath, columns, delimiter=r"\s+", comments=r"#", start=None, end=No
return
else:
line_forces.append(value)
configuration.append(line_forces)
yield np.array(line_forces)
# If starting and ending condition are provided, after starting condition
# is detected, add the values from lines to a new array that is returned
# when the end condition is met
elif start is not None and end is not None:
with open(filepath, "r") as f:
for line in f: # This actually reads line by line and only keeps the current line in memory
# If a start regex is provided, use it to detect the start of a configuration
if is_start(line):
started = True
continue
# If separator encountered, yield the stored configuration
if is_end(line):
started = False
if configuration:
yield np.array(configuration)
configuration = []
elif start is not None and started:
# Ignore comments, separate by delimiter
vals = split_line(line)
line_forces = []
if vals:
for column in columns:
try:
value = vals[column]
except IndexError:
logger.warning("The given index '{}' could not be found on the line '{}'. The given delimiter or index could be wrong.".format(column, line))
return
try:
value = float(value)
except ValueError:
logger.warning("Could not cast value '{}' to float. Currently only floating point values are accepted".format(value))
return
else:
line_forces.append(value)
configuration.append(line_forces)
# The last configuration is yielded even if separator is not present at
# the end of file or is not given at all
if configuration:
yield np.array(configuration)
# The last configuration is yielded even if separator is not present at
# the end of file or is not given at all
if configuration:
yield np.array(configuration)
......@@ -47,6 +47,7 @@ object Cp2kParser extends SimpleExternalParserGenerator(
"parser-cp2k/cp2kparser/versions/cp2k262/__init__.py",
"parser-cp2k/cp2kparser/versions/cp2k262/singlepointparser.py",
"parser-cp2k/cp2kparser/versions/cp2k262/geooptparser.py",
"parser-cp2k/cp2kparser/versions/cp2k262/mdparser.py",
"parser-cp2k/cp2kparser/versions/cp2k262/singlepointforceparser.py",
"parser-cp2k/cp2kparser/versions/cp2k262/inputparser.py",
"parser-cp2k/cp2kparser/versions/cp2k262/commonmatcher.py",
......
......@@ -15,4 +15,7 @@ object Cp2kParserSpec extends Specification {
"test geo_opt with json" >> {
ParserRun.parse(Cp2kParser, "parsers/cp2k/test/examples/geo_opt/H2O.out", "json") must_== ParseResult.ParseSuccess
}
"test md with json" >> {
ParserRun.parse(Cp2kParser, "parsers/cp2k/test/examples/md/H2O-32.out", "json") must_== ParseResult.ParseSuccess
}
}
# Step Nr. Time[fs] Kin.[a.u.] Temp[K] Pot.[a.u.] Cons Qty[a.u.] UsedTime[s]
0 0.000000 0.007125335 300.000000000 -34.330396471 -34.323271136 0.000000000
1 0.500000 0.006533348 275.075405378 -34.329778993 -34.323245645 5.596860174
2 1.000000 0.005583688 235.091633019 -34.328790653 -34.323206964 2.152504481
3 1.500000 0.004815598 202.752506973 -34.327998978 -34.323183380 2.176433628
4 2.000000 0.004566544 192.266488819 -34.327754290 -34.323187747 2.145689724
5 2.500000 0.004788928 201.629598676 -34.327997890 -34.323208962 2.183614561
6 3.000000 0.005184860 218.299664775 -34.328412394 -34.323227533 2.156728805
7 3.500000 0.005470470 230.324748558 -34.328704052 -34.323233583 1.372403022
8 4.000000 0.005526692 232.691881533 -34.328757407 -34.323230715 2.158405874
9 4.500000 0.005371243 226.146979313 -34.328598255 -34.323227013 2.157126734
10 5.000000 0.005062914 213.165337396 -34.328287038 -34.323224123 2.141358878
6
i = 0, time = 0.000, E = -34.3303964710
O 2.2803980000 9.1465390000 5.0886960000
O 1.2517030000 2.4062610000 7.7699080000
H 1.7620190000 9.8204290000 5.5284540000
H 3.0959870000 9.1070880000 5.5881860000
H 0.5541290000 2.9826340000 8.0820240000
H 1.7712570000 2.9547790000 7.1821810000
6
i = 1, time = 0.500, E = -34.3297789925
O 2.2807055575 9.1465838595 5.0883919888
O 1.2498929917 2.4074289284 7.7692382491
H 1.7550641677 9.8178358845 5.5212255966
H 3.1112981302 9.0969170746 5.5956143531
H 0.5663993495 2.9792400173 8.0854791515
H 1.7745194198 2.9517247311 7.1939396477
6
i = 2, time = 1.000, E = -34.3287906528
O 2.2810938851 9.1465143736 5.0880057320
O 1.2481430886 2.4083371258 7.7685912598
H 1.7468317341 9.8169710643 5.5154127828
H 3.1265398790 9.0869161313 5.6032028662
H 0.5758344344 2.9781042647 8.0902773264
H 1.7797622574 2.9504835298 7.2036786282
6
i = 3, time = 1.500, E = -34.3279989781
O 2.2816569948 9.1463220223 5.0875919643
O 1.2464808250 2.4089259514 7.7679581102
H 1.7376017910 9.8176768475 5.5110703534
H 3.1398366791 9.0773366634 5.6100676793
H 0.5816795339 2.9798971014 8.0968435085
H 1.7872918494 2.9513202206 7.2111353246
6
i = 4, time = 2.000, E = -34.3277542904
O 2.2824536844 9.1460139717 5.0872014341
O 1.2449189507 2.4091830380 7.7673245034
H 1.7278297775 9.8195589149 5.5080907122
H 3.1497224743 9.0684159063 5.6155281103
H 0.5837476503 2.9848496112 8.1053394370
H 1.7970573968 2.9542015292 7.2164224211
6
i = 5, time = 2.500, E = -34.3279978897
O 2.2835075421 9.1456102023 5.0868767781
O 1.2434524580 2.4091462088 7.7666758161
H 1.7181028654 9.8220570351 5.5062468114
H 3.1552105753 9.0603654046 5.6191356782
H 0.5824842170 2.9926888277 8.1156135109
H 1.8086404408 2.9588109314 7.2199843450
6
i = 6, time = 3.000, E = -34.3284123938
O 2.2848116168 9.1451387997 5.0866494510
O 1.2420611951 2.4088940254 7.7660011551
H 1.7090732160 9.8245375564 5.5052476019
H 3.1557721856 9.0533599447 5.6206772705
H 0.5788535575 3.0026992793 8.1272265032
H 1.8213326965 2.9646247282 7.2224917951
6
i = 7, time = 3.500, E = -34.3287040522
O 2.2863340532 9.1446310518 5.0865370925
O 1.2407164792 2.4085282817 7.7652951031
H 1.7013784598 9.8263832260 5.5047906840
H 3.1513029381 9.0475240318 5.6201716574
H 0.5741065883 3.0138823014 8.1395347678
H 1.8342810097 2.9710164962 7.2247109095
6
i = 8, time = 4.000, E = -34.3287574070
O 2.2880225370 9.1441174852 5.0865411149
O 1.2393883154 2.4081553744 7.7645574288
H 1.6955650581 9.8270646411 5.5046034983
H 3.1421140712 9.0429166182 5.6178695663
H 0.5695320041 3.0251531775 8.1517995491
H 1.8466448643 2.9773546982 7.2273850117
6
i = 9, time = 4.500, E = -34.3285982555
O 2.2898069785 9.1436252146 5.0866444773
O 1.2380505706 2.4078718032 7.7637918013
H 1.6920290647 9.8261894866 5.5044715741
H 3.1289538455 9.0395163855 5.6142598682
H 0.5662695747 3.0355160242 8.1632992842
H 1.8577079508 2.9830741256 7.2311516146
6
i = 10, time = 5.000, E = -34.3282870379
O 2.2916014875 9.1431763260 5.0868100688
O 1.2366834078 2.4077552776 7.7630044423
H 1.6909790671 9.8235337924 5.5042564094
H 3.1130341664 9.0372111810 5.6100739746
H 0.5652070478 3.0441761067 8.1734257299
H 1.8669280879 2.9877213524 7.2364955946
&FORCE_EVAL
METHOD QS
&DFT
BASIS_SET_FILE_NAME ../../BASIS_SET
POTENTIAL_FILE_NAME ../../GTH_POTENTIALS
&MGRID
CUTOFF 200
REL_CUTOFF 30
&END MGRID
&QS
EPS_DEFAULT 1.0E-12
WF_INTERPOLATION PS
EXTRAPOLATION_ORDER 3
&END QS
&SCF
SCF_GUESS ATOMIC
&PRINT
&RESTART OFF
&END
&END
&END SCF
&XC
&XC_FUNCTIONAL Pade
&END XC_FUNCTIONAL
&END XC
&END DFT
&SUBSYS
&CELL
ABC 9.8528 9.8528 9.8528
&END CELL
&COORD
O 2.280398 9.146539 5.088696
O 1.251703 2.406261 7.769908
H 1.762019 9.820429 5.528454
H 3.095987 9.107088 5.588186
H 0.554129 2.982634 8.082024
H 1.771257 2.954779 7.182181
&END COORD
&KIND H
BASIS_SET DZVP-GTH-PADE
POTENTIAL GTH-PADE-q1
&END KIND
&KIND O
BASIS_SET DZVP-GTH-PADE
POTENTIAL GTH-PADE-q6
&END KIND
&END SUBSYS
&END FORCE_EVAL
&GLOBAL
PROJECT H2O-32
RUN_TYPE MD
PRINT_LEVEL MEDIUM
&END GLOBAL
&MOTION
&MD
ENSEMBLE NVE
STEPS 10
TIMESTEP 0.5
TEMPERATURE 300.0
&END MD
&END MOTION
This diff is collapsed.
......@@ -687,12 +687,79 @@ class TestMD(unittest.TestCase):
cls.results = get_results("md/nve", "section_run")
def test_ensemble_type(self):
result = self.results["x_cp2k_md_ensemble_type"]
result = self.results["ensemble_type"]
self.assertEqual(result, "NVE")
# md = self.results["x_cp2k_section_md"][0]
# for key, value in md.simpleValues.iteritems():
# print "{}: {}".format(key, value)
def test_sampling_method(self):
result = self.results["sampling_method"]
self.assertEqual(result, "molecular_dynamics")
def test_atom_positions(self):
result = self.results["atom_positions"]
expected_start = convert_unit(
np.array([
[2.2803980000, 9.1465390000, 5.0886960000],
[1.2517030000, 2.4062610000, 7.7699080000],
[1.7620190000, 9.8204290000, 5.5284540000],
[3.0959870000, 9.1070880000, 5.5881860000],
[0.5541290000, 2.9826340000, 8.0820240000],
[1.7712570000, 2.9547790000, 7.1821810000],
]),
"angstrom"
)
expected_end = convert_unit(
np.array([
[2.2916014875, 9.1431763260, 5.0868100688],
[1.2366834078, 2.4077552776, 7.7630044423],
[1.6909790671, 9.8235337924, 5.5042564094],
[3.1130341664, 9.0372111810, 5.6100739746],
[0.5652070478, 3.0441761067, 8.1734257299],
[1.8669280879, 2.9877213524, 7.2364955946],
]),
"angstrom"
)
self.assertTrue(np.array_equal(result[0,:], expected_start))
self.assertTrue(np.array_equal(result[-1,:], expected_end))
def test_frame_sequence_potential_energy(self):
result = self.results["frame_sequence_potential_energy"]
expected_result = convert_unit(
np.array([
-34.330396471,
-34.329778993,
-34.328790653,
-34.327998978,
-34.327754290,
-34.327997890,
-34.328412394,
-34.328704052,
-34.328757407,
-34.328598255,
-34.328287038,
]),
"hartree"
)
self.assertTrue(np.array_equal(result, expected_result))
def test_frame_sequence_kinetic_energy(self):
result = self.results["frame_sequence_kinetic_energy"]
expected_result = convert_unit(
np.array([
0.007125335,
0.006533348,
0.005583688,
0.004815598,
0.004566544,
0.004788928,
0.005184860,
0.005470470,
0.005526692,
0.005371243,
0.005062914,
]),
"hartree"
)
self.assertTrue(np.array_equal(result, expected_result))
#===============================================================================
......@@ -701,19 +768,19 @@ if __name__ == '__main__':
logger.setLevel(logging.ERROR)
suites = []
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestErrors))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestXCFunctional))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestEnergyForce))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestStressTensorMethods))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSelfInteractionCorrectionMethod))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestConfigurationPeriodicDimensions))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSCFConvergence))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestForceFiles))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestPreprocessor))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOpt))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOptTrajFormats))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOptOptimizers))
# suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOptTrajectory))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestErrors))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestXCFunctional))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestEnergyForce))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestStressTensorMethods))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSelfInteractionCorrectionMethod))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestConfigurationPeriodicDimensions))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestSCFConvergence))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestForceFiles))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestPreprocessor))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOpt))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOptTrajFormats))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOptOptimizers))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestGeoOptTrajectory))
suites.append(unittest.TestLoader().loadTestsFromTestCase(TestMD))
alltests = unittest.TestSuite(suites)
unittest.TextTestRunner(verbosity=0).run(alltests)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment