singlepointforceparser.py 1.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2015-2018 Lauri Himanen, Fawzi Mohamed, Ankit Kariryaa
# 
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

15
16
import logging
import numpy as np
17
from nomadcore.baseclasses import AbstractBaseParser
18
19
20
logger = logging.getLogger("nomad")


21
class CP2KSinglePointForceParser(AbstractBaseParser):
22
23
24
25
    """Used to parse out a force file printed out by a CP2K single point
    calculation. It is not exactly an ZYX file, so here we define separate
    parser.
    """
26
27
    def __init__(self, parser_context):
        super(CP2KSinglePointForceParser, self).__init__(parser_context)
28

29
    def parse(self, filepath):
30
31
        start = False
        forces = []
32
        with open(filepath) as f:
33
34
35
36
37
38
39
40
41
42
43
            for line in f:
                if line.startswith(" # Atom"):
                    start = True
                    continue
                elif line.startswith(" SUM"):
                    forces = np.array(forces)
                    self.backend.addArrayValues("atom_forces", forces, unit="forceAu")
                    break
                elif start:
                    components = [float(comp) for comp in line.split()[-3:]]
                    forces.append(components)