Skip to content

Commit

Permalink
Remove class declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
ladinesa committed Jan 10, 2025
1 parent eb3fb41 commit f2be5fc
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 307 deletions.
42 changes: 24 additions & 18 deletions src/nomad_parser_vasp/parsers/outcar_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Dict, List, Any
from typing import TYPE_CHECKING, Any, Dict, List

if TYPE_CHECKING:
from nomad.datamodel.datamodel import (
Expand All @@ -8,19 +8,16 @@
BoundLogger,
)

import numpy as np
import re

import numpy as np
from nomad.datamodel import EntryArchive
from nomad.parsing.file_parser import TextParser, Quantity
from nomad.parsing.file_parser.mapping_parser import (
MetainfoParser,
TextParser as MappingTextParser,
Path,
)

from nomad_parser_vasp.schema_packages.vasp_package import Simulation
from nomad.parsing.file_parser import Quantity, TextParser
from nomad.parsing.file_parser.mapping_parser import MetainfoParser, Path
from nomad.parsing.file_parser.mapping_parser import TextParser as MappingTextParser
from nomad_simulations.schema_packages.general import Simulation

from nomad_parser_vasp.schema_packages.vasp_package import OUTCAR_ANNOTATION_KEY

RE_N = r'[\n\r]'

Expand Down Expand Up @@ -377,6 +374,12 @@ def get_data(self, source: Any, **kwargs) -> Any:
parser = Path(path=path)
return parser.get_data(source)

def get_forces(self, source: Any) -> Dict[str, Any]:
value = self.get_data(source, path='.positions_forces | [1]')
if value is None:
return {}
return dict(forces=value, npoints=len(value), rank=[3])

def get_energy_contributions(
self, source: Dict[str, Any], **kwargs
) -> List[Dict[str, Any]]:
Expand All @@ -397,7 +400,14 @@ def get_eigenvalues(
data = []
for nspin in range(ispin):
eigs, occs = eigenvalues[nspin].T[1:3]
data.append(dict(eigenvalues=eigs.T, occupations=occs.T, n_bands=n_bands))
data.append(
dict(
eigenvalues=eigs.T,
occupations=occs.T,
n_bands=n_bands,
npoints=n_kpts,
)
)
return data

def get_xc_functionals(self, parameters: Dict[str, Any]) -> List[Dict[str, Any]]:
Expand Down Expand Up @@ -433,13 +443,9 @@ def get_xc_functionals(self, parameters: Dict[str, Any]) -> List[Dict[str, Any]]
else:
metagga = parameters.get('METAGGA')
if metagga:
functionals = self.xc_functional_mapping.get(
metagga, [metagga]
)
functionals = self.xc_functional_mapping.get(metagga, [metagga])
else:
functionals = self.xc_functional_mapping.get(
parameters.get('GGA'), []
)
functionals = self.xc_functional_mapping.get(parameters.get('GGA'), [])
for functional in functionals:
xc_functionals.append({'name': functional})
return xc_functionals
Expand All @@ -457,7 +463,7 @@ def parse(
archive_data_parser = MetainfoParser()
archive_data = Simulation()
archive_data_parser.data_object = archive_data
archive_data_parser.annotation_key = 'outcar'
archive_data_parser.annotation_key = OUTCAR_ANNOTATION_KEY

# set up outcar parser
source_parser = OutcarParser()
Expand Down
4 changes: 2 additions & 2 deletions src/nomad_parser_vasp/parsers/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Dict, List, Any
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from nomad.datamodel.datamodel import (
Expand All @@ -8,8 +8,8 @@
BoundLogger,
)

from nomad_parser_vasp.parsers.xml_parser import VASPXMLParser
from nomad_parser_vasp.parsers.outcar_parser import VASPOutcarParser
from nomad_parser_vasp.parsers.xml_parser import VASPXMLParser


class VASPParser:
Expand Down
29 changes: 21 additions & 8 deletions src/nomad_parser_vasp/parsers/xml_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Dict, List, Any
from typing import TYPE_CHECKING, Any, Dict, List

import numpy as np

Expand All @@ -11,9 +11,13 @@
)

from nomad.config import config
from nomad.parsing.file_parser.mapping_parser import MetainfoParser, XMLParser, Path
from nomad.parsing.file_parser.mapping_parser import MetainfoParser, Path, XMLParser
from nomad_simulations.schema_packages.general import Simulation

from nomad_parser_vasp.schema_packages.vasp_package import Simulation
from nomad_parser_vasp.schema_packages.vasp_package import (
XML2_ANNOTATION_KEY,
XML_ANNOTATION_KEY,
)

configuration = config.get_plugin_entry_point(
'nomad_parser_vasp.parsers:vasp_parser_entry_point'
Expand All @@ -24,8 +28,11 @@ class VasprunParser(XMLParser):
def mix_alpha(self, mix: float, cond: bool) -> float:
return mix if cond else 0

def get_eigenvalues(self, array: list) -> np.ndarray:
return np.array(array).T[1] if array is not None else np.array([])
def get_eigenvalues(self, array: list) -> Dict[str, Any]:
if array is None:
return {}
transposed = np.transpose(array)
return dict(eigenvalues=transposed[0], occupations=transposed[1])

def get_energy_contributions(
self, source: Dict[str, Any], **kwargs
Expand All @@ -36,7 +43,7 @@ def get_energy_contributions(
if c.get(f'{self.attribute_prefix}name') not in kwargs.get('exclude', [])
]

def get_data(self, source: Dict[str, Any], **kwargs) -> float:
def get_data(self, source: Dict[str, Any], **kwargs) -> Any:
if source.get(self.value_key):
return source[self.value_key]
path = kwargs.get('path')
Expand All @@ -46,6 +53,12 @@ def get_data(self, source: Dict[str, Any], **kwargs) -> float:
parser = Path(path=path)
return parser.get_data(source)

def get_forces(self, source: Dict[str, Any]) -> Dict[str, Any]:
value = self.get_data(source, path='.varray.v')
if value is None:
return {}
return dict(forces=value, npoints=len(value), rank=[3])


class VASPXMLParser:
def parse(
Expand All @@ -58,7 +71,7 @@ def parse(
logger.info(self.__class__.__name__, parameter=configuration.parameter)

data_parser = MetainfoParser()
data_parser.annotation_key = 'xml'
data_parser.annotation_key = XML_ANNOTATION_KEY

data = Simulation()
xml_parser = VasprunParser(filepath=mainfile)
Expand All @@ -67,7 +80,7 @@ def parse(
xml_parser.convert(data_parser)

data_parser = MetainfoParser()
data_parser.annotation_key = 'xml2'
data_parser.annotation_key = XML2_ANNOTATION_KEY
data_parser.data_object = data
xml_parser.convert(data_parser)

Expand Down
Loading

0 comments on commit f2be5fc

Please sign in to comment.