Kindfield
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Friends Pages
plot_three_particle_contribution.py
Go to the documentation of this file.
1 import h5py
2 from pylab import *
3 from glob import glob
4 from scipy.interpolate import griddata
5 from collections import OrderedDict
6 import os
7 from argparse import ArgumentParser
8 
9 def find_nearest_index(array, value):
10  idx = (np.abs(array-value)).argmin()
11  return idx
12 
13 parser = ArgumentParser()
14 parser.add_argument("--three_body_states", nargs="+")
15 parser.add_argument("--two_body_states", nargs="+")
16 parser.add_argument("--id", default="tmp")
17 args = parser.parse_args()
18 output_dir = os.path.abspath("tmp")
19 
20 if args.id != "tmp":
21  try:
22  from sumatra.projects import load_project
23  output_dir = os.path.join(os.path.abspath(load_project().data_store.root), args.id)
24  except ImportError:
25  pass
26 
27 if not os.path.exists(output_dir):
28  os.makedirs(output_dir)
29 
30 states_files = args.two_body_states
31 if len(states_files) == 1:
32  states_files = glob(states_files[0])
33 
34 energy_min = inf
35 energy_max = -inf
36 
37 energies = []
38 r12s = []
39 
40 for statesFile in states_files:
41  f = h5py.File(statesFile, "r")
42  atomsMeta = f.get("atomMeta")
43  energyOffset = atomsMeta.attrs["energyOffset"]
44  if len(atomsMeta) != 2:
45  raise Exception("Wrong number of atoms in atomsMeta. Found %d, should be 2." % len(atomsMeta))
46  states = f.get("/states")
47  for stateName in states:
48  atoms = states.get(stateName)
49  #r12 = atoms[1]["x"] - atoms[0]["x"]
50  #r13 = sqrt((atoms[2]["x"] - atoms[0]["x"])**2 + (atoms[2]["y"] - atoms[0]["y"])**2)
51  #angle = arctan2(atoms[2]["y"], atoms[2]["x"])
52  r12 = atoms.attrs["r12"]
53  energy = atoms.attrs["energy"] - energyOffset
54 
55  r12s.append(r12)
56  energies.append(energy)
57 
58  energy_min = min(energy_min, energy)
59  energy_max = max(energy_max, energy)
60  f.close()
61 
62 # Sort r12 and energies by r12
63 r12s, energies = [list(x) for x in zip(*sorted(zip(r12s, energies), key=lambda pair: pair[0]))]
64 
65 two_body_r12s = array(r12s)
66 two_body_energies = array(energies)
67 
68 states_files = args.three_body_states
69 if len(states_files) == 1:
70  states_files = glob(states_files[0])
71 
72 plots = {}
73 
74 energy_min = inf
75 energy_max = -inf
76 
77 energy_differences_min = inf
78 energy_differences_max = -inf
79 
80 for statesFile in states_files:
81  f = h5py.File(statesFile, "r")
82  atomsMeta = f.get("atomMeta")
83  states = f.get("/states")
84  energyOffset = atomsMeta.attrs["energyOffset"]
85  for stateName in states:
86  atoms = states.get(stateName)
87  #r12 = atoms[1]["x"] - atoms[0]["x"]
88  #r13 = sqrt((atoms[2]["x"] - atoms[0]["x"])**2 + (atoms[2]["y"] - atoms[0]["y"])**2)
89  #angle = arctan2(atoms[2]["y"], atoms[2]["x"])
90  r12 = atoms.attrs["r12"]
91  r13 = atoms.attrs["r13"]
92 
93  angle = atoms.attrs["angle"]
94  plot_name = "%.4f" % angle
95  energy = atoms.attrs["energy"] - energyOffset
96 
97  atom2_x = r12
98 #
99  atom3_x = cos(angle) * r13
100  atom3_y = sin(angle) * r13
101 #
102  r23_vector = array([atom3_x - atom2_x, atom3_y - 0.0, 0.0 - 0.0])
103  r23 = linalg.norm(r23_vector)
104 
105  index12 = find_nearest_index(two_body_r12s, r12)
106  index13 = find_nearest_index(two_body_r12s, r13)
107  index23 = find_nearest_index(two_body_r12s, r23)
108 
109  energy_difference = energy - two_body_energies[index12] - two_body_energies[index13] - two_body_energies[index23]
110 
111  if r12 < 6.0 and r13 < 6.0:
112  energy_difference = 0.0
113 
114  if not plots.has_key(plot_name):
115  plots[plot_name] = {"r12s_r13s": [], "energies": [], "energy_differences": []}
116  plots[plot_name]["r12s_r13s"].append([r12, r13])
117  plots[plot_name]["energies"].append(energy)
118  plots[plot_name]["energy_differences"].append(energy_difference)
119 
120  r12_min = atomsMeta.attrs["r12Min"]
121  r12_max = atomsMeta.attrs["r12Max"]
122  r13_min = atomsMeta.attrs["r13Min"]
123  r13_max = atomsMeta.attrs["r13Max"]
124  angle_min = atomsMeta.attrs["angleMin"]
125  angle_max = atomsMeta.attrs["angleMax"]
126 
127  energy_min = min(energy_min, energy)
128  energy_max = max(energy_max, energy)
129 
130  energy_differences_min = min(energy_differences_min, energy_difference)
131  energy_differences_max = max(energy_differences_max, energy_difference)
132  f.close()
133 
134 print "Angle min:", angle_min, "max:", angle_max
135 print "r13 min:", r13_min, "max:", r13_max
136 
137 
138 r12s = linspace(r12_min, r12_max, 25)
139 r13s = linspace(r13_min, r13_max, 25)
140 grid_r12s, grid_r13s = meshgrid(r12s, r13s)
141 n_plots = len(plots)
142 n_plots_per_dim = int(sqrt(n_plots) + 1)
143 plot_counter = 1
144 plots = OrderedDict(sorted(plots.items()))
145 
146 #vmin = energy_min
147 #vmax = energy_min + (energy_max - energy_min) * 0.5
148 vmin = energy_differences_min
149 vmax = energy_differences_max
150 #vmin = 0
151 #vmax = 0.5
152 #vmax = energy_differences_min + (energy_differences_max - energy_differences_min) * 0.1
153 print "vmin,vmax: ", vmin, vmax
154 
155 fig = figure(figsize=(10,10))
156 for plot_name in plots:
157 
158  ax = fig.add_subplot(n_plots_per_dim, n_plots_per_dim, plot_counter)
159  ax.set_aspect("equal")
160  title(plot_name)
161 
162  values = plots[plot_name]
163 
164  grid_energies = griddata(array(values["r12s_r13s"]), array(values["energies"]), (grid_r12s, grid_r13s), method="nearest")
165  grid_energy_differences = griddata(array(values["r12s_r13s"]), array(values["energy_differences"]), (grid_r12s, grid_r13s), method="nearest")
166  #grid_energies = griddata(array(values["r12s_r13s"]), array(values["energy_differences"]), (grid_r12s, grid_r13s), method="nearest")
167 
168 # img = imshow(grid_energies, origin="lower", vmin=vmin, vmax=vmax,
169 # extent=[r12_min, r12_max, r13_min, r13_max],
170 # interpolation="nearest",
171 # aspect=(r12_max - r12_min) / (r13_max - r13_min))
172 
173  img = contour(r12s, r13s, grid_energy_differences, 50, vmin=vmin, vmax=vmax)
174  colorbar()
175 
176  plot_counter += 1
177 
178 fig.tight_layout()
179 #savefig(output_file + ".pdf")
180 #savefig(output_file + ".png")
181