11# -*- coding: utf-8 -*-
22"""Command line utilities to create and inspect `StructureData` nodes from CP2K input files."""
33
4- import re
5-
64import click
7- import numpy as np
85
96from aiida .cmdline .params import options
107from aiida .cmdline .utils import decorators , echo
118
129from . import cmd_data
10+ from ..utils .structure import structure_from_cp2k_inp
1311
1412
1513@cmd_data .group ('structure' )
@@ -24,79 +22,10 @@ def cmd_structure():
2422def cmd_import (filename , dry_run ):
2523 """Import a `StructureData` from a CP2K input file."""
2624
27- # pylint: disable=import-outside-toplevel,invalid-name,too-many-locals,too-many-statements,too-many-branches
28-
29- from cp2k_input_tools .parser import CP2KInputParser
30- from aiida .orm .nodes .data .structure import StructureData , Kind , Site , symop_fract_from_ortho
31- from ase .geometry .cell import cell_to_cellpar , cellpar_to_cell
32-
33- # the following was taken from aiida-quantumespresso
34- VALID_ELEMENTS_REGEX = re .compile (
35- r"""
36- ^(
37- H | He |
38- Li | Be | B | C | N | O | F | Ne |
39- Na | Mg | Al | Si | P | S | Cl | Ar |
40- K | Ca | Sc | Ti | V | Cr | Mn | Fe | Co | Ni | Cu | Zn | Ga | Ge | As | Se | Br | Kr |
41- Rb | Sr | Y | Zr | Nb | Mo | Tc | Ru | Rh | Pd | Ag | Cd | In | Sn | Sb | Te | I | Xe |
42- Cs | Ba | Hf | Ta | W | Re | Os | Ir | Pt | Au | Hg | Tl | Pb | Bi | Po | At | Rn |
43- Fr | Ra | Rf | Db | Sg | Bh | Hs | Mt |
44- La | Ce | Pr | Nd | Pm | Sm | Eu | Gd | Tb | Dy | Ho | Er | Tm | Yb | Lu | # Lanthanides
45- Ac | Th | Pa | U | Np | Pu | Am | Cm | Bk | Cf | Es | Fm | Md | No | Lr | # Actinides
46- )
47- """ , re .VERBOSE | re .IGNORECASE )
48-
49- parser = CP2KInputParser ()
50- tree = parser .parse (filename )
51- force_eval_no = - 1
52-
53- for force_eval_no , force_eval in enumerate (tree ['+force_eval' ]):
54- try :
55- cell = force_eval ['+subsys' ]['+cell' ]
56- kinds = force_eval ['+subsys' ]['+kind' ]
57- coord = force_eval ['+subsys' ]['+coord' ]
58- break # for now grab the first &COORD found
59- except KeyError :
60- continue
61- else :
62- echo .echo_critical ('no STRUCTURE, CELL, or KIND found in the given input file' )
63-
64- # CP2K can get its cell information in two ways:
65- # - A, B, C: cell vectors
66- # - ABC: scaling of cell vectors, ALPHA_BETA_GAMMA: angles between the cell vectors (optional)
67-
68- if 'a' in cell :
69- unit_cell = np .array ([cell ['a' ], cell ['b' ], cell ['c' ]]) # unit vectors given
70- cellpar = cell_to_cellpar (unit_cell )
71- elif 'abc' in cell :
72- cellpar = cell ['abc' ] + cell .get ('alpha_beta_gamma' , [90. , 90. , 90. ])
73- unit_cell = cellpar_to_cell (cellpar )
74- else :
75- echo .echo_critical ('incomplete &CELL section' )
76-
77- pbc = [c in cell .get ('periodic' , 'XYZ' ) for c in 'XYZ' ]
78-
79- structure = StructureData (cell = unit_cell , pbc = pbc )
80-
81- if coord .get ('scaled' , False ):
82- tmat = symop_fract_from_ortho (cellpar )
83- else :
84- tmat = np .eye (3 )
85-
86- for kind in kinds :
87- name = kind ['_' ]
88-
89- try :
90- # prefer the ELEMENT keyword, fallback to extracting from name
91- element = kind .get ('element' , VALID_ELEMENTS_REGEX .search (name )[0 ])
92- except TypeError :
93- echo .echo_critical ('ELEMENT not set and unable to extract from {name}' .format (name = name ))
94-
95- structure .append_kind (Kind (name = name , symbols = element ))
96-
97- for name , position , _ in parser .coords (force_eval_no ):
98- # positions can be scaled, apply transformation matrix
99- structure .append_site (Site (kind_name = name , position = tmat @ np .array (position )))
25+ try :
26+ structure = structure_from_cp2k_inp (filename )
27+ except ValueError as exc :
28+ echo .echo_critical (str (exc ))
10029
10130 formula = structure .get_formula ()
10231
0 commit comments