energy_plot.py 3.37 KB
Newer Older
Loic Hausammann's avatar
Loic Hausammann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
import matplotlib
matplotlib.use("Agg")
from pylab import *
import h5py

# Plot parameters
params = {'axes.labelsize': 10,
'axes.titlesize': 10,
'font.size': 12,
'legend.fontsize': 12,
'xtick.labelsize': 10,
'ytick.labelsize': 10,
'text.usetex': True,
 'figure.figsize' : (3.15,3.15),
'figure.subplot.left'    : 0.145,
'figure.subplot.right'   : 0.99,
'figure.subplot.bottom'  : 0.11,
'figure.subplot.top'     : 0.99,
'figure.subplot.wspace'  : 0.15,
'figure.subplot.hspace'  : 0.12,
'lines.markersize' : 6,
'lines.linewidth' : 3.,
'text.latex.unicode': True
}
rcParams.update(params)
rc('font',**{'family':'sans-serif','sans-serif':['Times']})


import numpy as np
import h5py as h5
import sys

# File containing the total energy
stats_filename = "./energy.txt"

# First snapshot
snap_filename = "Isothermal_0000.hdf5"
f = h5.File(snap_filename,'r')

# Read the units parameters from the snapshot
units = f["InternalCodeUnits"]
unit_mass = units.attrs["Unit mass in cgs (U_M)"]
unit_length = units.attrs["Unit length in cgs (U_L)"]
unit_time = units.attrs["Unit time in cgs (U_t)"]

# Read the header
header = f["Header"]
box_size = float(header.attrs["BoxSize"][0])

# Read the properties of the potential
parameters = f["Parameters"]
R200 = 100 
Vrot = float(parameters.attrs["IsothermalPotential:vrot"])
centre = [box_size/2, box_size/2, box_size/2]
f.close()

# Read the statistics summary
file_energy = np.loadtxt("energy.txt")
time_stats = file_energy[:,0]
E_kin_stats = file_energy[:,3]
E_pot_stats = file_energy[:,5]
E_tot_stats = E_kin_stats + E_pot_stats

# Read the snapshots
time_snap = np.zeros(402)
E_kin_snap = np.zeros(402)
E_pot_snap = np.zeros(402)
E_tot_snap = np.zeros(402)
Lz_snap = np.zeros(402)

# Read all the particles from the snapshots
for i in range(402):
    snap_filename = "Isothermal_%0.4d.hdf5"%i
    f = h5.File(snap_filename,'r')

    pos_x = f["PartType3/Coordinates"][:,0]
    pos_y = f["PartType3/Coordinates"][:,1]
    pos_z = f["PartType3/Coordinates"][:,2]
    vel_x = f["PartType3/Velocities"][:,0]
    vel_y = f["PartType3/Velocities"][:,1]
    vel_z = f["PartType3/Velocities"][:,2]
    mass = f["/PartType3/Masses"][:]
    
    r = np.sqrt((pos_x[:] - centre[0])**2 + (pos_y[:] - centre[1])**2 + (pos_z[:] - centre[2])**2)
    Lz = (pos_x[:] - centre[0]) * vel_y[:] - (pos_y[:] - centre[1]) * vel_x[:]

    time_snap[i] = f["Header"].attrs["Time"]
    E_kin_snap[i] = np.sum(0.5 * mass * (vel_x[:]**2 + vel_y[:]**2 + vel_z[:]**2))
    E_pot_snap[i] = np.sum(mass * Vrot**2 *  log(r))
    E_tot_snap[i] = E_kin_snap[i] + E_pot_snap[i]
    Lz_snap[i] = np.sum(Lz)

# Plot energy evolution
figure()
plot(time_stats, E_kin_stats, "r-", lw=0.5, label="Kinetic energy")
plot(time_stats, E_pot_stats, "g-", lw=0.5, label="Potential energy")
plot(time_stats, E_tot_stats, "k-", lw=0.5, label="Total energy")

plot(time_snap[::10], E_kin_snap[::10], "rD", lw=0.5, ms=2)
plot(time_snap[::10], E_pot_snap[::10], "gD", lw=0.5, ms=2)
plot(time_snap[::10], E_tot_snap[::10], "kD", lw=0.5, ms=2)

legend(loc="center right", fontsize=8, frameon=False, handlelength=3, ncol=1)
xlabel("${\\rm{Time}}$", labelpad=0)
ylabel("${\\rm{Energy}}$",labelpad=0)
xlim(0, 8)

savefig("energy.png", dpi=200)

# Plot angular momentum evolution
figure()
plot(time_snap, Lz_snap, "k-", lw=0.5, ms=2)

xlabel("${\\rm{Time}}$", labelpad=0)
ylabel("${\\rm{Angular~momentum}}$",labelpad=0)
xlim(0, 8)

savefig("angular_momentum.png", dpi=200)