Commit 4160bc2f authored by Mikkel Strange's avatar Mikkel Strange

nice updates

parent eb425c9a
"""Simple wrapper to atk output files"""
from scipy.io.netcdf import netcdf_file
class X:
def __init__(self, name=None):
self.name = name
class Atoms:
def __init__(self, fd):
self.positions = None
self.numbers = None
self.cell = None
self.pbc = None
class Hamiltonian:
def __init__(self):
self.e_entropy = None
self.e_kinetic = None
self.e_total_free = None
self.e_total_extrapolated = None
self.e_xc = None
class BandPath:
def __init__(self):
self.labels = None
self.eigenvalues = None
self.kpts = None
class WaveFunctions:
def __init__(self):
self.occupations
self.eigenvalues = None
self.ibz_kpts = None
self.band_paths = None
from configurations import conf_types
from parser_configurations2 import parse_configuration
import re
class Reader:
def __init__(self, filename):
self.f = netcdf_file(filename)
self.v = self.f.variables
self.read_atoms()
self.read_hamiltonian()
self.read_wave_functions()
self.xc = None
def read_atoms(self):
self.atoms = Atoms()
def read_hamiltonian(self):
self.hamiltonian = Hamiltonian()
def read_wave_functions(self):
self.wave_functions = WaveFunctions()
def get_number_of_structures(self):
""" Several structures could in principle be in one file
how many we are dealing with
"""
return 1
def __init__(self, fname):
self.f = netcdf_file(fname, 'r', mmap=True)
self.initialize()
def initialize(self):
self.conf_names = self.get_configuration_names()
self.calc_names = self.get_calculator_names()
self.finger_print_table = self.get_finger_print_table()
def get_configuration_names(self):
d = self.f.dimensions
conf_names = {}
for k in d.keys():
for conf_type in conf_types:
p = conf_type + '(?P<gID>_gID[0-9][0-9][0-9])_dimension'
m = re.search(p, k)
if m is not None:
g = m.group('gID')
conf_names[g[1:]] = conf_type + g
return conf_names
def get_calculator_names(self):
d = self.f.dimensions
calc_names = {}
for k in d.keys():
for conf_type in conf_types:
p = conf_type + \
'(?P<gID>_gID[0-9][0-9][0-9])_calculator_dimension'
m = re.search(p, k)
if m is not None:
g = m.group('gID')
calc_names[g[1:]] = conf_type + g + '_calculator'
return calc_names
def get_finger_print_table(self):
table = {}
if hasattr(self.f, 'fingerprint_table'):
fpt = fd.fingerprint_table
for fpg in fpt.split('#')[:-1]:
fp, g = fpg.split(':')[:2]
table[g] = fp
return table
def get_number_of_configurations(self):
return len(self.conf_names)
def get_atoms(self):
if __name__ == '__main__':
r = Reader('h2.nc')
print(r.get_configuration_names())
print(r.get_finger_print_table())
from ase import Atoms
from ase.build import bulk
class UnitCell:
def __init__(self, a, b, c, origin=None):
self.cell = [a, b, c]
class FaceCenteredCubic:
def __init__(self, a):
self.cell = bulk('X', crystalstructure='fcc', a=a).get_cell()
def primitiveVectors(self):
return self.cell
class BodyCenteredCubic:
def __init__(self, a):
self.cell = bulk('X', crystalstructure='bcc', a=a).get_cell()
def primitiveVectors(self):
return self.cell
class BulkConfiguration:
def __init__(self, bravais_lattice, elements, cartesian_coordinates=None,
fractional_coordinates=None, ghost_atoms=None,
velocities=None, tag_data=None, fast_init=False):
if elements is None:
symbols = None
else:
symbols = [e.symbol() for e in elements]
if cartesian_coordinates is not None:
positions = cartesian_coordinates
scale_atoms = False
elif fractional_coordinates is not None:
positions = fractional_coordinates
scale_atoms = True
else:
positions = None
scale_atoms = False
pbc = True
atoms = Atoms(symbols, positions, pbc=pbc)
atoms.set_cell(bravais_lattice.cell, scale_atoms=scale_atoms)
atoms.ghost_atoms = ghost_atoms
self.atoms = atoms
class MoleculeConfiguration:
def __init__(self, elements=None, cartesian_coordinates=None,
fractional_coordinates=None, ghost_atoms=None,
velocities=None, tag_data=None, fast_init=False):
if elements is None:
symbols = None
else:
symbols = [e.symbol() for e in elements]
if cartesian_coordinates is not None:
positions = cartesian_coordinates
else:
positions = None
pbc = False
atoms = Atoms(symbols, positions, pbc=pbc)
atoms.ghost_atoms = ghost_atoms
self.atoms = atoms
things = {'UnitCell': UnitCell,
'FaceCenteredCubic': FaceCenteredCubic,
'BulkConfiguration': BulkConfiguration,
'MoleculeConfiguration': MoleculeConfiguration}
conf_types = ['MoleculeConfiguration',
'BulkConfiguration']
from periodic_table import things as ptab_ns
from physical_quantities import things as physquan_ns
from configurations import conf_types, things as confs_ns
def parse_configuration(fd, name, verbose=False):
""" convert a nanolanguage python script into
ASE list of atoms.
Parameters::
fd: netcdf_file handle
name: str (i.e "BulkConfiguration_gID000")
"""
code = fd.variables[name].data[:].copy()
code = code.tostring().decode("utf-8")
if verbose:
print('parsing code:\n------------')
print(code)
things = ptab_ns.copy()
things.update(physquan_ns)
things.update(confs_ns)
exec(code, {}, things)
for obj in things.values():
for conf_type in conf_types:
if isinstance(obj, confs_ns[conf_type]):
return obj.atoms
return -1
if __name__ == '__main__':
from ase.visualize import view
from scipy.io.netcdf import netcdf_file
import sys
fd = netcdf_file(sys.argv[1], 'r')
name = 'BulkConfiguration_gID000'
atoms = parse_configuration(fd, name)
view(atoms)
# configurations = {}
# fp_gids = fd.fingerprint_table[:].decode('utf-8').split('#')
# for c in fp_gids:
# if len(c) > 0:
# fingerprint, gID = c.split(':')[:2]
# gID.strip()
# for name in ['BulkConfiguration_' + gID,
# 'MoleculeConfiguration_' + gID]:
# if name in fd.variables.keys():
# configurations[name] = fingerprint
#
# for name, fingerprint in configurations.items():
# print(name+',', fingerprint)
# atoms = parse_configuration(fd, name)
# view(atoms)
from ase.data import atomic_names, atomic_numbers, chemical_symbols,\
atomic_masses
def name(self):
return self._name
def atomicNumber(self):
return self._atomic_number
def symbol(self):
return self._symbol
def atomicMass(self):
return self._atomic_mass_amu
things = {}
for n, s, m in zip(atomic_names[1:], chemical_symbols[1:],
atomic_masses[1:]):
stuff = {'_atomic_number': atomic_numbers[s],
'_name': n,
'_symbol': s,
'_atomic_mass_amu': m,
'atomicNumber': atomicNumber,
'name': name,
'atomicMass': atomicMass,
'symbol': symbol}
element = type(n, (object,), stuff)
exec(n + ' = element()')
#exec(s + ' = ' + n)
exec('things[n] = ' + n)
# clean up
del stuff, name, atomicNumber, symbol, atomicMass, n, s, m, element
del atomic_names, atomic_numbers, chemical_symbols, atomic_masses
from ase.units import Bohr, Angstrom, kB as Kelvin, eV, Hartree
import ase.units as au
import numpy as np
class PC(list):
class PhysicalQuantity(list):
def __init__(self, val):
self.val = val
def __mul__(self, other):
......@@ -17,12 +17,17 @@ class PC(list):
def __repr__(self):
return format(self.val)
eV = PC(eV)
Angstrom = PC(Angstrom)
Hartree = PC(Hartree)
Bohr = PC(Bohr)
Kelvin = PC(Kelvin)
eV = PhysicalQuantity(au.eV)
Angstrom = PhysicalQuantity(au.Angstrom)
Hartree = PhysicalQuantity(au.Hartree)
Bohr = PhysicalQuantity(au.Bohr)
Kelvin = PhysicalQuantity(au.kB)
Hour = 1
amu = PhysicalQuantity(1.0)
things = {'eV': eV,
'Angstrom': Angstrom,
'Hartree': Hartree,
'Bohr': Bohr,
'Kelvin': Kelvin,
'Hour': 1,
'amu': amu}
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