Kindfield
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Friends Pages
density.py
Go to the documentation of this file.
1 # Plot the atoms and the bonds ################################################
2 from pylab import *
3 from mayavi import mlab
4 from os.path import join
5 import h5py
6 from argparse import ArgumentParser
7 from glob import glob
8 import os
9 import os.path
10 
11 parser = ArgumentParser()
12 parser.add_argument("results_path")
13 parser.add_argument("--contours", nargs="+", type=float)
14 parser.add_argument("--id", nargs='?', default="tmp")
15 args = parser.parse_args()
16 
17 output_dir = "tmp"
18 
19 if args.id != "tmp":
20  try:
21  from sumatra.projects import load_project
22  output_dir = os.path.join(os.path.abspath(load_project().data_store.root), args.id)
23  except ImportError:
24  pass
25 
26 if not os.path.exists(output_dir):
27  os.makedirs(output_dir)
28 
29 def draw_atoms(atoms, atom_meta):
30  n_electrons = 0
31  counter = 0
32  for atom in atoms:
33  if atom_meta[counter]["type"] == 1:
34  color = (1, 1, 1)
35  scale = 0.3
36  elif atom_meta[counter]["type"] == 6:
37  color = (0, 0, 0)
38  scale = 0.6
39  elif atom_meta[counter]["type"] == 8:
40  color = (1, 0, 0)
41  scale = 0.6
42  else:
43  color = (1, 1, 0)
44  scale = 0.2
45  mlab.points3d(atom[0], atom[1], atom[2],
46  scale_factor=scale,
47  resolution=20,
48  color=color,
49  scale_mode='none')
50  counter += 1
51  return n_electrons
52 
53 density_file_name = os.path.join(args.results_path, "density.h5")
54 
55 atoms_data_file = h5py.File(join(args.results_path, "results.h5"))
56 atom_meta = atoms_data_file.get("atomMeta")[:]
57 atoms = atoms_data_file.get("state")[:]
58 atoms_data_file.close()
59 
60 density_file = h5py.File(density_file_name)
61 data = density_file.get("dataset")[:]
62 density_file.close()
63 
64 print "Data min,max:",data.min(),data.max()
65 
66 X,Y,Z = mgrid[-3:3:1j*data.shape[0], -3:3:1j*data.shape[1], -3:3:1j*data.shape[2]]
67 
68 mlab.figure(2, bgcolor=(1, 1, 1), size=(1280, 720))
69 mlab.clf()
70 n_electrons = draw_atoms(atoms, atom_meta)
71 data_max_min_diff = (data.max() - data.min())
72 if args.contours:
73  contours = args.contours
74 else:
75  contours = linspace(0.001,0.9,10).tolist()
76 iso = mlab.contour3d(X, Y, Z, data, vmin=contours[0], vmax=contours[-1], opacity=0.1013, contours=contours, colormap="GnBu", line_width=0.1)
77 
78 mlab.savefig(os.path.join(output_dir, "density.x3d"))
79 #iso.actor.property.representation = "wireframe"
80 mlab.savefig(os.path.join(output_dir, "density.png"))