Skip to content
Snippets Groups Projects
Commit aacef5ac authored by Martina Stella's avatar Martina Stella
Browse files

adding Onetep

parent 16fe5159
Branches
Tags
No related merge requests found
Showing
with 3422 additions and 5 deletions
[NOMAD Laboratory CoE](http://nomad-lab.eu) parser for [ONETEP](http://www2.tcm.phy.cam.ac.uk/onetep/).
# CASTEP Parser
The official version lives at https://gitlab.rzg.mpg.de/nomad-lab/parser-onetep .
This relies on python-common and metainfo, you probably want to check out
the [nomad-lab-base](https://gitlab.rzg.mpg.de/nomad-lab/nomad-lab-base) project
that contains this one and its dependencies.
\ No newline at end of file
This is the parser for [CASTEP](http://www.castep.org/).
It is part of the [NOMAD Laboratory](http://nomad-lab.eu).
The official version lives at
git@gitlab.mpcdf.mpg.de:nomad-lab/parser-castep.git
you can browse it at
https://gitlab.mpcdf.mpg.de/nomad-lab/parser-castep
It relies on having the nomad-meta-info and the python common repositories one level higher.
The simplest way to have this is to check out nomad-lab-base recursively:
git clone --recursive git@gitlab.mpcdf.mpg.de:nomad-lab/nomad-lab-base.git
then this will be in parsers/castep.
# TEST OUTPUT FILES
Few output files where to test the parser are provided in the directory test examples.
FILE NAME | FILE DESCRIPTION
__________________|___________________________________________________
"Si2.castep_v_1" --> Single Point Calculation (minimum verbosity)
"Si2.castep_v_2" --> Single Point Calculation (medium verbosity)
"Si2.castep_v_3" --> Single Point Calculation (maximum verbosity)
"Si2.castep_b_v_1" --> Band Structure Calculation (minimum verbosity)
"Si2.castep_b_v_2" --> Band Structure Calculation (medium verbosity)
"Si2.castep_b_v_3" --> Band Structure Calculation (maximum verbosity)
\ No newline at end of file
File added
File added
import setup_paths
import numpy as np
import nomadcore.ActivateLogging
from nomadcore.caching_backend import CachingLevel
from nomadcore.simple_parser import mainFunction
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from OnetepCommon import get_metaInfo
import logging, os, re, sys
###############################################################
# This is the parser for the *.band file of Onetep.
# NB: this parser store self consistent eigenvalues and
# relative k points for single configuration calculations
###############################################################
logger = logging.getLogger("nomad.OnetepBandParser")
class OnetepBandParserContext(object):
"""Context for parsing Onetep *.band file.
The onClose_ functions allow processing and writing of cached values after a section is closed.
They take the following arguments:
backend: Class that takes care of wrting and caching of metadata.
gIndex: Index of the section that is closed.
section: The cached values and sections that were found in the section that is closed.
"""
def __init__(self, writeMetaData = True):
"""Args:
writeMetaData: Deteremines if metadata is written or stored in class attributes.
"""
self.writeMetaData = writeMetaData
def startedParsing(self, fInName, parser):
"""Function is called when the parsing starts and the compiled parser is obtained.
Args:
fInName: The file name on which the current parser is running.
parser: The compiled parser. Is an object of the class SimpleParser in nomadcore.simple_parser.py.
"""
self.parser = parser
# get unit from metadata for band energies
# allows to reset values if the same superContext is used to parse different files
self.k_count = 0
self.k_nr = 0
self.e_nr = 0
self.eigenvalues_kpoints = []
self.eigenvalues_values = []
self.e_spin_1 = []
self.e_spin_2 = []
self.n_spin = 0
# Reading the number of spins
def onClose_x_Onetep_section_spin_number(self, backend, gIndex, section):
self.n_spin = section['x_Onetep_spin_number']
# Storing the k point coordinates
def onClose_x_Onetep_section_scf_k_points(self, backend, gIndex, section):
"""trigger called when _section_eigenvalues"""
# Processing k points (given in fractional coordinates)
#get cached values of x_Onetep_store_k_points
k_st = section['x_Onetep_store_scf_k_points']
self.k_count = len(k_st)
self.k_nr += 1
for i in range(0, self.k_count):
k_st[i] = k_st[i].split()
k_st[i] = [float(j) for j in k_st[i]]
k_st_int = k_st[i]
self.eigenvalues_kpoints.append(k_st_int)
# Storing the eigenvalues
def onClose_x_Onetep_section_scf_eigenvalues(self, backend, gIndex, section):
"""trigger called when _section_eigenvalues"""
Ha_to_J = 4.35974e-18
#get cached values of Onetep_store_k_points
e_st = section['x_Onetep_store_scf_eigenvalues']
e_st_0 = e_st
e_st_0 = [x * Ha_to_J for x in e_st_0]
def split_list(lista):
half = len(lista)/2
return lista[:half], lista[half:]
e_st_1, e_st_2 = split_list(e_st)
e_st_1 = [x * Ha_to_J for x in e_st_1]
e_st_2 = [x * Ha_to_J for x in e_st_2]
if self.n_spin[0] == 1:
self.e_nr = len(e_st_0)
self.e_spin_1.append(e_st_0)
self.e_spin_2 = []
else:
self.e_nr = len(e_st_1)
self.e_spin_1.append(e_st_1)
self.e_spin_2.append(e_st_2)
################################################################################
###################### MAIN PARSER STARTS HERE ###############################
################################################################################
def build_OnetepBandFileSimpleMatcher():
"""Builds the SimpleMatcher to parse the *.cell file of Onetep.
SimpleMatchers are called with 'SM (' as this string has length 4,
which allows nice formating of nested SimpleMatchers in python.
Returns:
SimpleMatcher that parses *.cell file of Onetep.
"""
return SM (name = 'Root1',
startReStr = "",
sections = ['section_run'],
forwardMatch = True,
weak = True,
subMatchers = [
SM (name = 'Root2',
startReStr = "",
sections = ['section_single_configuration_calculation'],
forwardMatch = True,
weak = True,
subMatchers = [
SM(startReStr = r"Number\sof\sk\-points\s*[0-9]+\s*",
sections = ['x_Onetep_section_spin_number'],
forwardMatch = True,
subMatchers = [
SM(r"Number\sof\sspin\scomponents\s*(?P<x_Onetep_spin_number>[1-2]+)")
]),
SM(startReStr = r"K\-point\s*[0-9]+\s*",
sections = ["x_Onetep_section_scf_k_points"],
forwardMatch = True,
repeats = True,
subMatchers = [
SM(r"K\-point\s*[0-9]+\s*(?P<x_Onetep_store_scf_k_points> [-\d\.]+\s+[-\d\.]+\s+[-\d\.]+)",
repeats = True),
SM(name = 'Eigen',
startReStr = r"Spin component\s*1\s*",
#endReStr = r"Spin component\s*2\s*",
sections = ['x_Onetep_section_scf_eigenvalues'],
repeats = True,
subMatchers = [
SM(r"\s*(?P<x_Onetep_store_scf_eigenvalues> [-\d\.]+)",
repeats = True)
]), # CLOSING Onetep_section_scf_eigenvalues
]) # CLOSING Onetep_section_k_points
]), # CLOSING section_single_configuration_calculation
])
def get_cachingLevelForMetaName(metaInfoEnv, CachingLvl):
"""Sets the caching level for the metadata.
Args:
metaInfoEnv: metadata which is an object of the class InfoKindEnv in nomadcore.local_meta_info.py.
CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
This allows to run the parser without opening new sections.
Returns:
Dictionary with metaname as key and caching level as value.
"""
# manually adjust caching of metadata
cachingLevelForMetaName = {
'section_run': CachingLvl,
'section_single_configuration_calculation': CachingLvl,
}
# Set all band metadata to Cache as they need post-processsing.
for name in metaInfoEnv.infoKinds:
if name.startswith('x_Onetep_'):
cachingLevelForMetaName[name] = CachingLevel.Cache
return cachingLevelForMetaName
def main(CachingLvl):
"""Main function.
Set up everything for the parsing of the Onetep *.cell file and run the parsing.
Args:
CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
This allows to run the parser without opening new sections.
"""
# get band.out file description
OneteBandFileSimpleMatcher = build_OnetepBandFileSimpleMatcher()
# loading metadata from nomad-meta-info/meta_info/nomad_meta_info/Onetep.nomadmetainfo.json
metaInfoPath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../../../../nomad-meta-info/meta_info/nomad_meta_info/Onetep.nomadmetainfo.json"))
metaInfoEnv = get_metaInfo(metaInfoPath)
# set parser info
parserInfo = {'name':'Onetep-cell-parser', 'version': '1.0'}
# get caching level for metadata
cachingLevelForMetaName = get_cachingLevelForMetaName(metaInfoEnv, CachingLvl)
# start parsing
mainFunction(mainFileDescription = OneteBandFileSimpleMatcher,
metaInfoEnv = metaInfoEnv,
parserInfo = parserInfo,
cachingLevelForMetaName = cachingLevelForMetaName,
superContext = OnetepBandParserContext())
if __name__ == "__main__":
main(CachingLevel.Forward)
File added
import setup_paths
import numpy as np
import nomadcore.ActivateLogging
from nomadcore.caching_backend import CachingLevel
from nomadcore.simple_parser import mainFunction
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from OnetepCommon import get_metaInfo
import logging, os, re, sys
############################################################
# This is the parser for the *.cell file of Onetep.
############################################################
logger = logging.getLogger("nomad.OnetepCellParser")
class OnetepCellParserContext(object):
"""Context for parsing Onetep *.cell file.
The onClose_ functions allow processing and writing of cached values after a section is closed.
They take the following arguments:
backend: Class that takes care of wrting and caching of metadata.
gIndex: Index of the section that is closed.
section: The cached values and sections that were found in the section that is closed.
"""
def __init__(self, writeMetaData = True):
"""Args:
writeMetaData: Deteremines if metadata is written or stored in class attributes.
"""
self.writeMetaData = writeMetaData
def startedParsing(self, fInName, parser):
"""Function is called when the parsing starts and the compiled parser is obtained.
Args:
fInName: The file name on which the current parser is running.
parser: The compiled parser. Is an object of the class SimpleParser in nomadcore.simple_parser.py.
"""
self.parser = parser
# get unit from metadata for band energies
# allows to reset values if the same superContext is used to parse different files
self.band_energies = None
self.band_k_points = None
self.band_occupations = None
self.k_crd = []
self.k_sgt_start_end = []
def onClose_section_k_band(self, backend, gIndex, section):
"""Trigger called when section_k_band is closed.
Store the parsed values and write them if writeMetaData is True.
"""
k_p = section['x_Onetep_store_k_path']
k_count = len(k_p)
self.k_crd = []
for i in range(0, k_count):
k_p[i] = k_p[i].split()
k_p[i] = [float(j) for j in k_p[i]]
k_p_int = k_p[i]
self.k_crd.append(k_p_int)
self.k_sgt_start_end = []
for i in range(k_count-1):
k_sgt = [ self.k_crd[i], self.k_crd[i+1] ]
self.k_sgt_start_end.append(k_sgt)
backend.addArrayValues('x_Onetep_k_path', np.asarray(self.k_crd))
# backend.addArrayValues('band_segm_start_end', np.asarray(self.k_sgt_start_end))
# backend.addValue('number_of_k_point_segments', len(self.k_sgt_start_end))
def build_OnetepCellFileSimpleMatcher():
"""Builds the SimpleMatcher to parse the *.cell file of Onetep.
SimpleMatchers are called with 'SM (' as this string has length 4,
which allows nice formating of nested SimpleMatchers in python.
Returns:
SimpleMatcher that parses *.cell file of Onetep.
"""
return SM (name = 'Root1',
startReStr = "",
sections = ['section_run'],
forwardMatch = True,
weak = True,
subMatchers = [
SM (name = 'Root2',
startReStr = "",
sections = ['section_single_configuration_calculation'],
forwardMatch = True,
weak = True,
subMatchers = [
SM(startReStr = r"\s*\%block bs\_kpoint\_path\s*",
sections = ['section_k_band'],
forwardMatch = True,
subMatchers = [
SM (r"(?P<x_Onetep_store_k_path>[\d\.]+\s+[\d\.]+\s+[\d\.]+)", repeats = True)
]),
SM(startReStr = r"\s*\%BLOCK BS\_KPOINT\_PATH\s*",
sections = ['section_k_band'],
forwardMatch = True,
subMatchers = [
SM (r"(?P<x_Onetep_store_k_path>[\d\.]+\s+[\d\.]+\s+[\d\.]+)", repeats = True)
]),
#SM (name = 'Root3',
# startReStr = r"\s*\%block bs\_kpoint\_path\s*",
# sections = ['section_k_band'],
# forwardMatch = True,
# weak = True,
# subMatchers = [
# SM (r"(?P<Onetep_store_k_path>[\d\.]+\s+[\d\.]+\s+[\d\.]+)", repeats = True)
# ]),
])
])
def get_cachingLevelForMetaName(metaInfoEnv, CachingLvl):
"""Sets the caching level for the metadata.
Args:
metaInfoEnv: metadata which is an object of the class InfoKindEnv in nomadcore.local_meta_info.py.
CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
This allows to run the parser without opening new sections.
Returns:
Dictionary with metaname as key and caching level as value.
"""
# manually adjust caching of metadata
cachingLevelForMetaName = {
'section_k_band': CachingLvl,
'section_run': CachingLvl,
'section_single_configuration_calculation': CachingLvl,
}
# Set all band metadata to Cache as they need post-processsing.
for name in metaInfoEnv.infoKinds:
if name.startswith('x_Onetep_'):
cachingLevelForMetaName[name] = CachingLevel.Cache
return cachingLevelForMetaName
def main(CachingLvl):
"""Main function.
Set up everything for the parsing of the Onetep *.cell file and run the parsing.
Args:
CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
This allows to run the parser without opening new sections.
"""
# get band.out file description
OneteCellFileSimpleMatcher = build_OnetepCellFileSimpleMatcher()
# loading metadata from nomad-meta-info/meta_info/nomad_meta_info/Onetep.nomadmetainfo.json
metaInfoPath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../../../../nomad-meta-info/meta_info/nomad_meta_info/Onetep.nomadmetainfo.json"))
metaInfoEnv = get_metaInfo(metaInfoPath)
# set parser info
parserInfo = {'name':'Onetep-cell-parser', 'version': '1.0'}
# get caching level for metadata
cachingLevelForMetaName = get_cachingLevelForMetaName(metaInfoEnv, CachingLvl)
# start parsing
mainFunction(mainFileDescription = OneteCellFileSimpleMatcher,
metaInfoEnv = metaInfoEnv,
parserInfo = parserInfo,
cachingLevelForMetaName = cachingLevelForMetaName,
superContext = OnetepCellParserContext())
if __name__ == "__main__":
main(CachingLevel.Forward)
File added
import setup_paths
import numpy as np
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from nomadcore.unit_conversion.unit_conversion import convert_unit
import json, os, re
############################################################
# This file contains functions that are needed
# by more than one parser.
############################################################
def get_metaInfo(filePath):
"""Loads metadata.
Args:
filePath: Location of metadata.
Returns:
metadata which is an object of the class InfoKindEnv in nomadcore.local_meta_info.py.
"""
metaInfoEnv, warnings = loadJsonFile(filePath = filePath, dependencyLoader = None, extraArgsHandling = InfoKindEl.ADD_EXTRA_ARGS, uri = None)
return metaInfoEnv
\ No newline at end of file
File added
import setup_paths
import numpy as np
import nomadcore.ActivateLogging
from nomadcore.caching_backend import CachingLevel
from nomadcore.simple_parser import mainFunction
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from OnetepCommon import get_metaInfo
import logging, os, re, sys
############################################################
# This is the parser for the *.md file of Onetep.
############################################################
logger = logging.getLogger("nomad.OnetepMDParser")
class OnetepMDParserContext(object):
"""Context for parsing Onetep *.md file.
The onClose_ functions allow processing and writing of cached values after a section is closed.
They take the following arguments:
backend: Class that takes care of wrting and caching of metadata.
gIndex: Index of the section that is closed.
section: The cached values and sections that were found in the section that is closed.
"""
def __init__(self, writeMetaData = True):
"""Args:
writeMetaData: Deteremines if metadata is written or stored in class attributes.
"""
self.writeMetaData = writeMetaData
self.frame_temperature =[]
self.frame_pressure = []
self.total_energy = []
self.hamiltonian = []
self.kinetic = []
self.md_forces =[]
self.md_veloc =[]
self.frame_atom_label =[]
self.total_velocities =[]
self.total_forces =[]
self.atom_label = []
self.frame_stress_tensor =[]
self.total_positions =[]
self.frame_cell=[]
self.vector_velocities=[]
self.frame_time =[]
def startedParsing(self, fInName, parser):
"""Function is called when the parsing starts and the compiled parser is obtained.
Args:
fInName: The file name on which the current parser is running.
parser: The compiled parser. Is an object of the class SimpleParser in nomadcore.simple_parser.py.
"""
self.parser = parser
def onClose_x_Onetep_section_md(self, backend, gIndex, section):
temp = section ['x_Onetep_md_temperature']
vet_veloc = section ['x_Onetep_md_cell_vectors_vel']
velocities = section ['x_Onetep_md_veloc']
vet = section ['x_Onetep_md_cell_vectors']
forces = section ['x_Onetep_md_forces']
atoms_lab = section ['x_Onetep_md_lab']
position = section ['x_Onetep_md_positions']
press = section ['x_Onetep_md_pressure']
energies = section['x_Onetep_md_energies']
stress_tensor = section ['x_Onetep_md_stress_tensor']
Hr_J_converter = float(4.35974e-18)
HrK_to_K_coverter= float(3.1668114e-6)
for i in range(len(temp)):
temp[i] = temp[i]/HrK_to_K_coverter
self.frame_temperature.append(temp[i])
for i in range(len(press)):
press[i] = press[i] / 10e9
self.frame_pressure.append(press[i])
for i in range(len(temp)):
energies[i] = energies[i].split()
energies[i] = [float(j) for j in energies[i]]
energy_list = energies[i]
energy_list = [x * Hr_J_converter for x in energy_list]
self.total_energy.append(energy_list[0])
self.hamiltonian.append(energy_list[1])
self.kinetic.append(energy_list[2])
if vet:
self.cell =[]
for i in range(len(vet)):
vet[i] = vet[i].split()
vet[i] = [float(j) for j in vet[i]]
vet_list = vet[i]
self.cell.append(vet_list)
self.frame_cell.append(self.cell)
if vet_veloc:
self.vet_vel =[]
for i in range(len(vet_veloc)):
vet_veloc[i] = vet_veloc[i].split()
vet_veloc[i] = [float(k) for k in vet_veloc[i]]
v_vet = vet_veloc[i]
self.vet_vel.append(v_vet)
self.vector_velocities.append(self.vet_vel)
if stress_tensor is not None:
self.stress_tensor_value =[]
for s in stress_tensor:
s = s.split()
s = [float(k) for k in s]
stress_tens_int = s
stress_tens_int = [x / 10e9 for x in stress_tens_int]
self.stress_tensor_value.append(stress_tens_int)
self.frame_stress_tensor.append(self.stress_tensor_value)
if position:
self.at_nr = len(position)
self.atom_position=[]
for i in range(0, self.at_nr):
position[i] = position[i].split()
position[i] = [float(j) for j in position[i]]
pos_list = position[i]
self.atom_position.append(pos_list)
self.total_positions.append(self.atom_position)
if velocities is not None:
self.md_veloc =[]
for j in range(len(velocities)):
velocities[j] = velocities[j].split()
velocities[j] = [float(k) for k in velocities[j]]
v_st_int = velocities[j]
self.md_veloc.append(v_st_int)
self.total_velocities.append(self.md_veloc)
if forces is not None:
self.md_forces = []
for f in forces:
f = f.split()
f = [float(k) for k in f]
f_st_int = f
self.md_forces.append(f_st_int)
self.total_forces.append(self.md_forces)
def build_OnetepMDFileSimpleMatcher():
"""Builds the SimpleMatcher to parse the *.md file of Onetep.
SimpleMatchers are called with 'SM (' as this string has length 4,
which allows nice formating of nested SimpleMatchers in python.
Returns:
SimpleMatcher that parses *.md file of Onetep.
"""
return SM (name = 'Root1',
startReStr = "",
sections = ['section_run'],
forwardMatch = True,
weak = True,
subMatchers = [
SM (name = 'Root2',
startReStr =r"\s*(?P<x_Onetep_md_energies>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sE\s*",
endReStr ="/n",
sections = ['x_Onetep_section_md'],
repeats = True,
subMatchers = [
SM (r"\s*(?P<x_Onetep_md_temperature>[-+0-9.eEdD]+)\s*\<\-\-\sT\s*"),
SM (r"\s*(?P<x_Onetep_md_pressure>[-+0-9.eEdD]+)\s*\<\-\-\sP\s*"),
SM (r"\s*(?P<x_Onetep_md_cell_vectors>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sh\s",repeats = True),
SM (r"\s*(?P<x_Onetep_md_cell_vectors_vel>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sh[a-z]\s*",repeats = True),
SM (r"\s*(?P<x_Onetep_md_stress_tensor>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sS\s",repeats = True),
SM(r"\s(?P<x_Onetep_md_lab>[A-Za-z]+\s*[0-9.]+)\s*(?P<x_Onetep_md_positions>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sR\s*",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_md_veloc>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sV\s*",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_md_forces>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sF\s*",repeats = True,endReStr ="/n"),
]),
])
def get_cachingLevelForMetaName(metaInfoEnv, CachingLvl):
"""Sets the caching level for the metadata.
Args:
metaInfoEnv: metadata which is an object of the class InfoKindEnv in nomadcore.local_meta_info.py.
CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
This allows to run the parser without opening new sections.
Returns:
Dictionary with metaname as key and caching level as value.
"""
# manually adjust caching of metadata
cachingLevelForMetaName = { 'x_Onetep_md_energies':CachingLevel.Cache,
'x_Onetep_md_forces':CachingLevel.Cache,
'x_Onetep_md_veloc':CachingLevel.Cache,
'x_Onetep_md_lab':CachingLevel.Cache,
'x_Onetep_md_positions':CachingLevel.Cache,
'x_Onetep_md_temperature':CachingLevel.Cache,
'x_Onetep_md_pressure':CachingLevel.Cache,
'x_Onetep_md_cell_vectors_vel':CachingLevel.Cache,
'x_Onetep_md_cell_vectors':CachingLevel.Cache,
'x_Onetep_md_stress_tensor':CachingLevel.Cache,
'section_run': CachingLvl,
'x_Onetep_section_md': CachingLvl,
# 'section_single_configuration_calculation': CachingLvl,
}
# Set all band metadata to Cache as they need post-processsing.
# for name in metaInfoEnv.infoKinds:
# if name.startswith('Onetep_'):
# cachingLevelForMetaName[name] = CachingLevel.Cache
return cachingLevelForMetaName
def main(CachingLvl):
"""Main function.
Set up everything for the parsing of the Onetep *.cell file and run the parsing.
Args:
CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
This allows to run the parser without opening new sections.
"""
# get band.out file description
OnetepMDFileSimpleMatcher = build_OnetepMDFileSimpleMatcher()
# loading metadata from nomad-meta-info/meta_info/nomad_meta_info/Onetep.nomadmetainfo.json
metaInfoPath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../../../../nomad-meta-info/meta_info/nomad_meta_info/Onetep.nomadmetainfo.json"))
metaInfoEnv = get_metaInfo(metaInfoPath)
# set parser info
parserInfo = {'name':'Onetep-md-parser', 'version': '1.0'}
# get caching level for metadata
cachingLevelForMetaName = get_cachingLevelForMetaName(metaInfoEnv, CachingLvl)
# start parsing
mainFunction(mainFileDescription = OnetepMDFileSimpleMatcher,
metaInfoEnv = metaInfoEnv,
parserInfo = parserInfo,
cachingLevelForMetaName = cachingLevelForMetaName,
superContext = OnetepMDParserContext())
if __name__ == "__main__":
main(CachingLevel.Forward)
File added
This diff is collapsed.
import setup_paths
import numpy as np
import nomadcore.ActivateLogging
from nomadcore.caching_backend import CachingLevel
from nomadcore.simple_parser import mainFunction
from nomadcore.simple_parser import SimpleMatcher as SM
from nomadcore.local_meta_info import loadJsonFile, InfoKindEl
from OnetepCommon import get_metaInfo
import logging, os, re, sys
############################################################
# This is the parser for the *.md file of Onetep.
############################################################
logger = logging.getLogger("nomad.OnetepTSParser")
class OnetepTSParserContext(object):
"""Context for parsing Onetep *.md file.
The onClose_ functions allow processing and writing of cached values after a section is closed.
They take the following arguments:
backend: Class that takes care of wrting and caching of metadata.
gIndex: Index of the section that is closed.
section: The cached values and sections that were found in the section that is closed.
"""
def __init__(self, writeMetaData = True):
"""Args:
writeMetaData: Deteremines if metadata is written or stored in class attributes.
"""
self.writeMetaData = writeMetaData
self.total_energy = []
self.total_energy_final = []
self.total_energy_pro = []
self.md_forces =[]
self.md_veloc =[]
self.frame_atom_label =[]
self.total_forces =[]
self.atom_label = []
self.frame_stress_tensor =[]
self.total_positions =[]
self.frame_cell=[]
self.frame_cell_final=[]
self.vector_velocities=[]
self.frame_time =[]
self.path_ts = []
self.path_final = []
self.path_pro = []
self.total_positions_final=[]
self.total_positions_pro=[]
def startedParsing(self, fInName, parser):
"""Function is called when the parsing starts and the compiled parser is obtained.
Args:
fInName: The file name on which the current parser is running.
parser: The compiled parser. Is an object of the class SimpleParser in nomadcore.simple_parser.py.
"""
self.parser = parser
def onClose_x_Onetep_section_ts_store(self, backend, gIndex, section):
vet = section ['x_Onetep_ts_cell_vectors_store']
forces_ts = section ['x_Onetep_ts_forces_store']
position = section ['x_Onetep_ts_positions_store']
energy = section['x_Onetep_ts_energy']
# path_step = section ['x_Onetep_ts_path']
# for i in range (len(path_step)):
# self.path_ts.append(path_step[i])
Hr_J_converter = float(4.35974e-18)
HrK_to_K_coverter= float(3.1668114e-6)
for i in energy:
energy = [x * Hr_J_converter for x in energy]
self.total_energy.extend(energy)
if vet:
self.cell =[]
for i in range(len(vet)):
vet[i] = vet[i].split()
vet[i] = [float(j) for j in vet[i]]
vet_list = vet[i]
self.cell.append(vet_list)
self.frame_cell.append(self.cell)
if position:
self.at_nr = len(position)
self.atom_position=[]
for i in range(0, self.at_nr):
position[i] = position[i].split()
position[i] = [float(j) for j in position[i]]
pos_list = position[i]
self.atom_position.append(pos_list)
self.total_positions.append(self.atom_position)
if forces_ts is not None:
self.ts_forces = []
for f in forces_ts:
f = f.split()
f = [float(k) for k in f]
f_st_intts = f
self.ts_forces.append(f_st_intts)
self.total_forces.append(self.ts_forces)
def onClose_x_Onetep_section_ts_final_store(self, backend, gIndex, section):
# path_final_ts = section ['x_Onetep_ts_path_ts_final']
vet_final = section ['x_Onetep_ts_cell_vectors_final_store']
forces_final = section ['x_Onetep_ts_forces_final_store']
position_final = section ['x_Onetep_ts_positions_final_store']
energy_final = section['x_Onetep_ts_energy_final_store']
# for i in range (len(path_final_ts)):
# self.path_final = path_final_ts[i]
Hr_J_converter = float(4.35974e-18)
HrK_to_K_coverter= float(3.1668114e-6)
self.total_energy_final = Hr_J_converter * energy_final[0]
# self.total_energy_final = energy_final
if vet_final:
self.cell_final =[]
for i in range(len(vet_final)):
vet_final[i] = vet_final[i].split()
vet_final[i] = [float(j) for j in vet_final[i]]
vetf_list = vet_final[i]
self.cell_final.append(vetf_list)
# self.frame_cell_final.append(self.cell_final)
if position_final:
self.at_nr = len(position_final)
self.atomf_position=[]
for i in range(0, self.at_nr):
position_final[i] = position_final[i].split()
position_final[i] = [float(j) for j in position_final[i]]
posf_list = position_final[i]
self.atomf_position.append(posf_list)
# self.total_positions_final.append(self.atomf_position)
if forces_final is not None:
self.md_forces_final = []
for f in forces_final:
f = f.split()
f = [float(k) for k in f]
f_st_intf = f
self.md_forces_final.append(f_st_intf)
# self.total_forces_final.append(self.md_forces_final)
def onClose_x_Onetep_section_ts_product_store(self, backend, gIndex, section):
# path_product = section ['x_Onetep_ts_path_product']
vet_pro = section ['x_Onetep_ts_cell_vectors_pro_store']
forces_pro = section ['x_Onetep_ts_forces_pro_store']
position_pro = section ['x_Onetep_ts_positions_pro_store']
energy_pro = section['x_Onetep_ts_energy_product_store']
Hr_J_converter = float(4.35974e-18)
HrK_to_K_coverter= float(3.1668114e-6)
# for i in range (len(path_product)):
# self.path_pro = path_product[i]
self.total_energy_pro = energy_pro[0] * Hr_J_converter
if vet_pro:
self.cell_pro =[]
for i in range(len(vet_pro)):
vet_pro[i] = vet_pro[i].split()
vet_pro[i] = [float(j) for j in vet_pro[i]]
vetp_list = vet_pro[i]
self.cell_pro.append(vetp_list)
# self.frame_cell_final.append(self.cell_final)
if position_pro:
self.at_nr = len(position_pro)
self.atomp_position=[]
for i in range(0, self.at_nr):
position_pro[i] = position_pro[i].split()
position_pro[i] = [float(j) for j in position_pro[i]]
posp_list = position_pro[i]
self.atomp_position.append(posp_list)
# self.total_positions_final.append(self.atomf_position)
if forces_pro is not None:
self.md_forces_pro = []
for f in forces_pro:
f = f.split()
f = [float(k) for k in f]
f_st_intp = f
self.md_forces_pro.append(f_st_intp)
def onClose_section_run(self, backend, gIndex, section):
path_product = section ['x_Onetep_ts_path_product']
path_final_ts = section ['x_Onetep_ts_path_ts_final']
path_step = section ['x_Onetep_ts_path']
for i in range (len(path_step)):
self.path_ts.append(path_step[i])
for i in range (len(path_product)):
self.path_pro = path_product[i]
for i in range (len(path_final_ts)):
self.path_final = path_final_ts[i]
def build_OnetepTSFileSimpleMatcher():
"""Builds the SimpleMatcher to parse the *.md file of Onetep.
SimpleMatchers are called with 'SM (' as this string has length 4,
which allows nice formating of nested SimpleMatchers in python.
Returns:
SimpleMatcher that parses *.md file of Onetep.
"""
return SM (name = 'Root1',
startReStr = "",
sections = ['section_run'],
forwardMatch = True,
weak = True,
subMatchers = [
SM (name = 'Root2',
startReStr =r"\sLST\s*[0-9.]\s*(?P<x_Onetep_ts_path>[-+0-9.eEdD]+)\s*",
endReStr ="/n",
sections = ['x_Onetep_section_ts_store'],
repeats = True,
subMatchers = [
SM (r"\s*(?P<x_Onetep_ts_energy>[-+0-9.eEdD]+)\s*[-+0-9.eEdD]+\s*\<\-\-\sE\s*"),
SM (r"\s*(?P<x_Onetep_ts_cell_vectors_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sh\s",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_ts_positions_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sR\s*",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_ts_forces_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sF\s*",repeats = True,endReStr ="/n"),
]),
SM (name = 'Root3',
startReStr =r"\sQST\s*[0-9.]\s*(?P<x_Onetep_ts_path>[-+0-9.eEdD]+)\s*",
endReStr ="/n",
sections = ['x_Onetep_section_ts_store'],
repeats = True,
subMatchers = [
SM (r"\s*(?P<x_Onetep_ts_energy>[-+0-9.eEdD]+)\s*[-+0-9.eEdD]+\s*\<\-\-\sE\s*"),
SM (r"\s*(?P<x_Onetep_ts_cell_vectors_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sh\s",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_ts_positions_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sR\s*",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_ts_forces_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sF\s*",repeats = True,endReStr ="/n"),
]),
SM (name = 'Root4',
startReStr =r"\sTS\s*0\s*(?P<x_Onetep_ts_path_ts_final>[-+0-9.eEdD]+)\s*",
endReStr ="/n",
sections = ['x_Onetep_section_ts_final_store'],
repeats = True,
subMatchers = [
SM (r"\s*(?P<x_Onetep_ts_energy_final_store>[-+0-9.eEdD]+)\s*[-+0-9.eEdD]+\s*\<\-\-\sE\s*"),
SM (r"\s*(?P<x_Onetep_ts_cell_vectors_final_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sh\s",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_ts_positions_final_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sR\s*",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_ts_forces_final_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sF\s*",repeats = True,endReStr ="/n"),
]),
SM (name = 'Root5',
startReStr =r"\sPRO\s*0\s*(?P<x_Onetep_ts_path_product>[-+0-9.eEdD]+)\s*",
endReStr ="/n",
sections = ['x_Onetep_section_ts_product_store'],
repeats = True,
subMatchers = [
SM (r"\s*(?P<x_Onetep_ts_energy_product_store>[-+0-9.eEdD]+)\s*[-+0-9.eEdD]+\s*\<\-\-\sE\s*"),
SM (r"\s*(?P<x_Onetep_ts_cell_vectors_pro_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sh\s",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_ts_positions_pro_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sR\s*",repeats = True),
SM(r"\s[A-Za-z]+\s*[0-9.]+\s*(?P<x_Onetep_ts_forces_pro_store>[-+0-9.eEdD]+\s*[-+0-9.eEdD]+\s*[-+0-9.eEdD]+)\s*\<\-\-\sF\s*",repeats = True,endReStr ="/n"),
]),
])
def get_cachingLevelForMetaName(metaInfoEnv, CachingLvl):
"""Sets the caching level for the metadata.
Args:
metaInfoEnv: metadata which is an object of the class InfoKindEnv in nomadcore.local_meta_info.py.
CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
This allows to run the parser without opening new sections.
Returns:
Dictionary with metaname as key and caching level as value.
"""
# manually adjust caching of metadata
cachingLevelForMetaName = {
'section_run': CachingLvl,
'x_Onetep_section_ts_store': CachingLvl,
'x_Onetep_section_ts_final_store': CachingLvl,
'x_Onetep_section_ts_product_store': CachingLvl,
}
# Set all band metadata to Cache as they need post-processsing.
for name in metaInfoEnv.infoKinds:
if name.startswith('x_Onetep_'):
cachingLevelForMetaName[name] = CachingLevel.Cache
return cachingLevelForMetaName
def main(CachingLvl):
"""Main function.
Set up everything for the parsing of the Onetep *.cell file and run the parsing.
Args:
CachingLvl: Sets the CachingLevel for the sections k_band, run, and single_configuration_calculation.
This allows to run the parser without opening new sections.
"""
# get band.out file description
OnetepTSFileSimpleMatcher = build_OnetepTSFileSimpleMatcher()
# loading metadata from nomad-meta-info/meta_info/nomad_meta_info/Onetep.nomadmetainfo.json
metaInfoPath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)),"../../../../nomad-meta-info/meta_info/nomad_meta_info/Onetep.nomadmetainfo.json"))
metaInfoEnv = get_metaInfo(metaInfoPath)
# set parser info
parserInfo = {'name':'Onetep-ts-parser', 'version': '1.0'}
# get caching level for metadata
cachingLevelForMetaName = get_cachingLevelForMetaName(metaInfoEnv, CachingLvl)
# start parsing
mainFunction(mainFileDescription = OnetepTSFileSimpleMatcher,
metaInfoEnv = metaInfoEnv,
parserInfo = parserInfo,
cachingLevelForMetaName = cachingLevelForMetaName,
superContext = OnetepTSParserContext())
if __name__ == "__main__":
main(CachingLevel.Forward)
\ No newline at end of file
File added
import sys, os, os.path
baseDir = os.path.dirname(os.path.abspath(__file__))
commonDir = os.path.normpath(os.path.join(baseDir,"../../../../python-common/common/python"))
if not commonDir in sys.path:
sys.path.insert(0, commonDir)
File added
File added
File added
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment