Created
June 10, 2019 11:19
-
-
Save bartolsthoorn/ad0c11fcfe9612a577005aa8da618627 to your computer and use it in GitHub Desktop.
SeeK-path minimum set-up to just get a working JS widget with pymatgen/cif files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymatgen import Structure | |
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer | |
import seekpath | |
import json | |
structure = Structure.from_file('diamond.cif') | |
# spglib primitive cell | |
sga = SpacegroupAnalyzer(structure) | |
structure = sga.find_primitive() | |
#print(seekpath.getpaths.get_explicit_k_path(sga._cell)) | |
#print(seekpath.getpaths.get_path(sga._cell)) | |
# from seekpath_web_module.py | |
def get_json_for_visualizer(cell, relcoords, atomic_numbers): | |
from seekpath import hpkot | |
from seekpath.brillouinzone.brillouinzone import get_BZ | |
import numpy as np | |
#hpkot = seekpath_module.hpkot | |
#brillouinzone = seekpath_module.brillouinzone | |
system = (np.array(cell), np.array(relcoords), np.array(atomic_numbers)) | |
res = hpkot.get_path(system, with_time_reversal=False) | |
real_lattice = res['primitive_lattice'] | |
#rec_lattice = np.linalg.inv(real_lattice).T # Missing 2pi! | |
rec_lattice = np.array(hpkot.tools.get_reciprocal_cell_rows(real_lattice)) | |
b1, b2, b3 = rec_lattice | |
#print(brillouinzone) | |
faces_data = get_BZ(b1=b1, b2=b2, b3=b3) | |
response = {} | |
response['faces_data'] = faces_data | |
response['b1'] = b1.tolist() | |
response['b2'] = b2.tolist() | |
response['b3'] = b3.tolist() | |
## Convert to absolute | |
response['kpoints'] = { | |
k: (v[0] * b1 + v[1] * b2 + v[2] * b3).tolist() | |
for k, v in res['point_coords'].items() | |
} | |
response['kpoints_rel'] = { | |
k: [v[0], v[1], v[2]] for k, v in res['point_coords'].items() | |
} | |
response['path'] = res['path'] | |
# It should use the same logic, so give the same cell as above | |
res_explicit = seekpath.get_explicit_k_path( | |
system, with_time_reversal=False) | |
for k in res_explicit: | |
if k == 'segments' or k.startswith('explicit_'): | |
if isinstance(res_explicit[k], np.ndarray): | |
response[k] = res_explicit[k].tolist() | |
else: | |
response[k] = res_explicit[k] | |
if np.sum( | |
np.abs( | |
np.array(res_explicit['reciprocal_primitive_lattice']) - | |
np.array(res['reciprocal_primitive_lattice']))) > 1.e-7: | |
raise AssertionError("Got different reciprocal cells...") | |
# Response for JS, and path_results | |
return response, res | |
def tuple_from_pymatgen(pmgstructure): | |
""" | |
Given a pymatgen structure, return a structure tuple as expected from seekpath | |
:param pmgstructure: a pymatgen Structure object | |
:return: a structure tuple (cell, positions, numbers) as accepted | |
by seekpath. | |
""" | |
frac_coords = [site.frac_coords.tolist() for site in pmgstructure.sites] | |
structure_tuple = (pmgstructure.lattice.matrix.tolist(), frac_coords, | |
pmgstructure.atomic_numbers) | |
return structure_tuple | |
jsondata = get_json_for_visualizer(*tuple_from_pymatgen(structure))[0] | |
print("""<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" type="text/css" href="http://127.0.0.1:5000/static/css/visualizer.min.css"/> | |
<link rel="stylesheet" type="text/css" href="http://127.0.0.1:5000/static/css/visualizer_base.min.css"/> | |
<script src="http://127.0.0.1:5000/static/js/three.min.js"></script> | |
<script src="http://127.0.0.1:5000/static/js/OrbitControls.min.js"></script> | |
<script src="http://127.0.0.1:5000/static/js/jquery-3.1.0.min.js"></script> | |
<script src="http://127.0.0.1:5000/static/js/BZVisualizer.min.js"></script> | |
<script> | |
var jsondata = """+ json.dumps(jsondata) +"""; | |
var mainBZVisualizer = new BZVisualizer(); | |
$( document ).ready(function() { | |
mainBZVisualizer.loadBZ(canvasID='canvas3d',infoID='info',jsondata=jsondata); | |
//canvas3d.dispatchEvent(new Event("dblclick")); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id='canvas3dcontainer'> | |
<div id='canvas3d'></div><div id='info'></div> | |
</div> | |
</body> | |
</html> | |
""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment