Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

Commit 18f07b1

Browse files
committed
Bugfix & refactor
1 parent bbf45eb commit 18f07b1

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

tilde/core/api.py

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Functionality exposed as an API
33
# Author: Evgeny Blokhin
44

5-
__version__ = "0.9.0"
5+
__version__ = "0.9.1"
66

77
import os, sys
88
import re
@@ -26,6 +26,7 @@
2626
from sqlalchemy import func
2727
from sqlalchemy.orm.exc import NoResultFound
2828

29+
import json as _json
2930
import ujson as json
3031
import six
3132

@@ -114,9 +115,9 @@ def __init__(self, settings=settings):
114115
# compiling table columns:
115116
if hasattr(self.Apps[appname]['appmodule'], 'cell_wrapper'):
116117
self.hierarchy.append({
117-
'cid': (2000+n),
118+
'cid': (2000 + n),
118119
'category': appmanifest['appcaption'],
119-
'sort': (2000+n),
120+
'sort': (2000 + n),
120121
'has_column': True,
121122
'cell_wrapper': getattr(self.Apps[appname]['appmodule'], 'cell_wrapper')
122123
})
@@ -190,7 +191,7 @@ def formula(self, atom_sequence):
190191
formula = ''
191192
for atom in atoms:
192193
n = len(types[labels[atom]])
193-
if n==1: n = ''
194+
if n == 1: n = ''
194195
else: n = str(n)
195196
formula += atom + n
196197
return formula
@@ -387,7 +388,7 @@ def classify(self, calc, symprec=None):
387388
if len(calc.info['elements']) == 1: calc.info['expanded'] = 1
388389
if not calc.info['expanded']:
389390
calc.info['expanded'] = reduce(gcd, calc.info['contents'])
390-
for n, i in enumerate([x//calc.info['expanded'] for x in calc.info['contents']]):
391+
for n, i in enumerate([x // calc.info['expanded'] for x in calc.info['contents']]):
391392
if i == 1:
392393
calc.info['standard'] += calc.info['elements'][n]
393394
else:
@@ -445,10 +446,10 @@ def classify(self, calc, symprec=None):
445446
if found.error:
446447
return None, found.error
447448

448-
calc.info['sg'] = found.i
449-
calc.info['ng'] = found.n
450-
calc.info['symmetry'] = found.symmetry
451-
calc.info['spg'] = "%s — %s" % (found.n, found.i)
449+
calc.info['sg'] = found.sg
450+
calc.info['ng'] = found.ng
451+
calc.info['spg'] = "%s — %s" % (found.ng, found.sg)
452+
calc.info['symmetry'] = found.system
452453
calc.info['pg'] = found.pg
453454
calc.info['dg'] = found.dg
454455

@@ -580,10 +581,10 @@ def save(self, calc, session):
580581
for f in calc.related_files:
581582
calc.download_size += os.stat(f).st_size
582583

583-
ormcalc = model.Calculation(checksum = checksum)
584+
ormcalc = model.Calculation(checksum=checksum)
584585

585586
if calc._calcset:
586-
ormcalc.meta_data = model.Metadata(chemical_formula = calc.info['standard'], download_size = calc.download_size)
587+
ormcalc.meta_data = model.Metadata(chemical_formula=calc.info['standard'], download_size=calc.download_size)
587588

588589
for child in session.query(model.Calculation).filter(model.Calculation.checksum.in_(calc._calcset)).all():
589590
ormcalc.children.append(child)
@@ -619,40 +620,43 @@ def save(self, calc, session):
619620
phonons_json[-1]['ph_k_degeneracy'] = calc.phonons['ph_k_degeneracy'][bzpoint]
620621

621622
ormcalc.phonons = model.Phonons()
622-
ormcalc.spectra.append( model.Spectra(kind = model.Spectra.PHONON, eigenvalues = json.dumps(phonons_json)) )
623+
ormcalc.spectra.append( model.Spectra(kind=model.Spectra.PHONON, eigenvalues=json.dumps(phonons_json)) )
623624

624625
# prepare electron data for saving TODO re-structure this
625626
for task in ['dos', 'bands']: # projected?
626627
if calc.electrons[task]:
627628
calc.electrons[task] = calc.electrons[task].todict()
628629

629630
if calc.electrons['dos'] or calc.electrons['bands']:
630-
ormcalc.electrons = model.Electrons(gap = calc.info['bandgap'])
631+
ormcalc.electrons = model.Electrons(gap=calc.info['bandgap'])
631632
if 'bandgaptype' in calc.info:
632633
ormcalc.electrons.is_direct = 1 if calc.info['bandgaptype'] == 'direct' else -1
633634
ormcalc.spectra.append(model.Spectra(
634-
kind = model.Spectra.ELECTRON,
635-
dos = json.dumps(calc.electrons['dos']),
636-
bands = json.dumps(calc.electrons['bands']),
637-
projected = json.dumps(calc.electrons['projected']),
638-
eigenvalues = json.dumps(calc.electrons['eigvals'])
635+
kind=model.Spectra.ELECTRON,
636+
dos=json.dumps(calc.electrons['dos']),
637+
bands=json.dumps(calc.electrons['bands']),
638+
projected=json.dumps(calc.electrons['projected']),
639+
eigenvalues=json.dumps(calc.electrons['eigvals'])
639640
))
640641

641642
# construct ORM for other props
642643
calc.related_files = list(map(virtualize_path, calc.related_files))
643-
ormcalc.meta_data = model.Metadata(location = calc.info['location'], finished = calc.info['finished'], raw_input = calc.info['input'], modeling_time = calc.info['duration'], chemical_formula = html_formula(calc.info['standard']), download_size = calc.download_size, filenames = json.dumps(calc.related_files))
644+
ormcalc.meta_data = model.Metadata(location=calc.info['location'], finished=calc.info['finished'], raw_input=calc.info['input'], modeling_time=calc.info['duration'], chemical_formula=html_formula(calc.info['standard']), download_size=calc.download_size, filenames=json.dumps(calc.related_files))
644645

645646
codefamily = model.Codefamily.as_unique(session, content = calc.info['framework'])
646-
codeversion = model.Codeversion.as_unique(session, content = calc.info['prog'])
647+
codeversion = model.Codeversion.as_unique(session, content=calc.info['prog'])
647648

648649
codeversion.instances.append(ormcalc.meta_data)
649650
codefamily.versions.append(codeversion)
650651

651-
pot = model.Pottype.as_unique(session, name = calc.info['H'])
652+
pot = model.Pottype.as_unique(session, name=calc.info['H'])
652653
pot.instances.append(ormcalc)
653-
ormcalc.recipinteg = model.Recipinteg(kgrid = calc.info['k'], kshift = calc.info['kshift'], smearing = calc.info['smear'], smeartype = calc.info['smeartype'])
654-
ormcalc.basis = model.Basis(kind = calc.info['ansatz'], content = json.dumps(calc.electrons['basis_set']) if calc.electrons['basis_set'] else None)
655-
ormcalc.energy = model.Energy(convergence = json.dumps(calc.convergence), total = calc.info['energy'])
654+
ormcalc.recipinteg = model.Recipinteg(kgrid=calc.info['k'], kshift=calc.info['kshift'], smearing=calc.info['smear'], smeartype=calc.info['smeartype'])
655+
ormcalc.basis = model.Basis(
656+
kind=calc.info['ansatz'],
657+
content=_json.dumps(calc.electrons['basis_set']) if calc.electrons['basis_set'] else None # NB. ujson fails on NaN
658+
)
659+
ormcalc.energy = model.Energy(convergence=json.dumps(calc.convergence), total=calc.info['energy'])
656660

657661
ormcalc.spacegroup = model.Spacegroup(n=calc.info['ng'])
658662
ormcalc.struct_ratios = model.Struct_ratios(chemical_formula=calc.info['standard'], formula_units=calc.info['expanded'], nelem=calc.info['nelem'], dimensions=calc.info['dims'])
@@ -661,11 +665,15 @@ def save(self, calc, session):
661665

662666
for n, ase_repr in enumerate(calc.structures):
663667
is_final = True if n == len(calc.structures)-1 else False
664-
struct = model.Structure(step = n, final = is_final)
668+
struct = model.Structure(step=n, final=is_final)
665669

666670
s = cell_to_cellpar(ase_repr.cell)
667-
struct.lattice = model.Lattice(a=s[0], b=s[1], c=s[2], alpha=s[3], beta=s[4], gamma=s[5], a11=ase_repr.cell[0][0], a12=ase_repr.cell[0][1], a13=ase_repr.cell[0][2], a21=ase_repr.cell[1][0], a22=ase_repr.cell[1][1], a23=ase_repr.cell[1][2], a31=ase_repr.cell[2][0], a32=ase_repr.cell[2][1], a33=ase_repr.cell[2][2])
668-
671+
struct.lattice = model.Lattice(
672+
a=s[0], b=s[1], c=s[2], alpha=s[3], beta=s[4], gamma=s[5],
673+
a11=ase_repr.cell[0][0], a12=ase_repr.cell[0][1], a13=ase_repr.cell[0][2],
674+
a21=ase_repr.cell[1][0], a22=ase_repr.cell[1][1], a23=ase_repr.cell[1][2],
675+
a31=ase_repr.cell[2][0], a32=ase_repr.cell[2][1], a33=ase_repr.cell[2][2]
676+
)
669677
#rmts = ase_repr.get_array('rmts') if 'rmts' in ase_repr.arrays else [None for j in range(len(ase_repr))]
670678
charges = ase_repr.get_array('charges') if 'charges' in ase_repr.arrays else [None for j in range(len(ase_repr))]
671679
magmoms = ase_repr.get_array('magmoms') if 'magmoms' in ase_repr.arrays else [None for j in range(len(ase_repr))]

0 commit comments

Comments
 (0)