Commit a0887c3a authored by Ask Hjorth Larsen's avatar Ask Hjorth Larsen

add shortcut functions to add metadata directly from ase Atoms objects or cube files

parent df3b05e5
import numpy as np
from nomadcore.unit_conversion.unit_conversion import convert_unit
def ase_atoms_to_section_system(backend, atoms, new_section=True):
"""Add ASE Atoms object as metainfo to section_system.
If new_section is True, open and close a new section_system,
returning its gIndex."""
if new_section:
gIndex = backend.openSection('section_system')
backend.addArrayValues('atom_labels',
np.array(atoms.get_chemical_symbols()))
backend.addArrayValues('atom_positions',
convert_unit(atoms.positions, 'angstrom'))
backend.addArrayValues('simulation_cell',
convert_unit(atoms.cell, 'angstrom'))
backend.addArrayValues('configuration_periodic_dimensions',
np.array(atoms.pbc))
# Return system ref if we opened it, else None:
if new_section:
backend.closeSection('section_system', gIndex)
return gIndex
import numpy as np
from ase.io import read
from nomadcore.atoms2nomad import ase_atoms_to_section_system
class CubeError(OSError):
pass
def read_cube_file(backend, file_name):
try:
d = read(file_name, format = 'cube', full_output = True)
except Exception as err:
raise CubeError(err)
data = d['data']
atoms = d['atoms']
origin = d['origin']
nx, ny, nz = data.shape
displacements = np.array([atoms.cell[i]/data.shape[i] for i in range(3)])
system = ase_atoms_to_section_system(backend, atoms)
singleconfig = backend.openSection('section_single_configuration_calculation')
volumetric = backend.openSection('section_volumetric_data')
backend.addValue('volumetric_data_nx', nx)
backend.addValue('volumetric_data_ny', ny)
backend.addValue('volumetric_data_nz', nz)
backend.addArrayValues('volumetric_data_origin', origin)
backend.addArrayValues('volumetric_data_displacements', displacements)
backend.addValue('volumetric_data_multiplicity', 1)
backend.addArrayValues('volumetric_data_values', data[None])
backend.closeSection('section_volumetric_data', volumetric)
backend.addValue('single_configuration_calculation_to_system_ref', system)
backend.closeSection('section_single_configuration_calculation', singleconfig)
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