In [13]:
# Import Required Python Base Packages
import sys
import unittest
import numpy as np
import random
import collections
import os
import time as tp
import glob

# Create a Safety Mechanism for Not Having MatPlotLib Installed
try:
    %matplotlib inline
    import matplotlib.pyplot as plt
    from matplotlib.pyplot import *
    from mpl_toolkits.mplot3d import Axes3D
    HAS_MATPLOTLIB = True
    
    matplotlib.rcParams['figure.figsize'] = (16, 6)
    matplotlib.rcParams['font.size'] = (14)
except ImportError:
    HAS_MATPLOTLIB = False

# Import the Amuse Base Packages
from amuse import datamodel
from amuse.units import nbody_system
from amuse.units import units
from amuse.units import constants
from amuse.datamodel import particle_attributes
from amuse.io import *
from amuse.lab import *

# Import the Amuse Stellar Packages
from amuse.ic.kingmodel import new_king_model
from amuse.ic.salpeter import new_salpeter_mass_distribution_nbody

# Import the Amuse Gravity & Close-Encounter Packages
from amuse.community.ph4.interface import ph4 as grav
from amuse.community.smalln.interface import SmallN
from amuse.community.kepler.interface import Kepler
from amuse.couple import multiples

# Importing cPickle/Pickle
try:
   import cPickle as pickle
except:
   import pickle

from tycho import analysis

In [4]:
def get_timestep_from_filenames(file_path1, file_path2):
    file_name1 = (file_path1.split("/")[-1])[:-5]
    file_name2 = (file_path2.split("/")[-1])[:-5]
    time1 = float((file_name1.split("_t")[-1]))
    time2 = float((file_name2.split("_t")[-1]))
    dt = np.abs(time2-time1)
    return dt
#get_timestep_from_filenames("/home/jglaser/Tycho/Runs/Mordin/160426/Mordin_t400.000000.hdf5", "/home/jglaser/Tycho/Runs/Mordin/160426/Mordin_t400.05000.hdf5")

In [5]:
def plot_cluster_2D(particles, step_number, current_time, cluster_name, converter, run_dir, show_plot=False, save_plot=True):
    # Skip Over Saved Plots
    #file_dir = "%s/Plots/2D_XY/" %(run_dir)
    file_dir = "/home/draco/jthornton/Tycho/Plots/"+cluster_name+"/"
    if not os.path.exists(file_dir):
            os.makedirs(file_dir)
    file_name = "%s_%08d.png" %(cluster_name, step_number)
    if not os.path.isfile(file_dir+file_name):
        # Split Objects into Stars & Planets
        number_of_planets = len([x for x in particles if x.type == "planet"])
        number_of_stars = len([x for x in particles if x.type == "star"])
        stars = particles.sorted_by_attribute('type')[number_of_planets:].copy()
        planets = particles.sorted_by_attribute('type')[:number_of_planets].copy()
        
        # Get Create Plotting Variables
        s_x = (converter.to_si(stars.x)).value_in(units.parsec)
        s_y = (converter.to_si(stars.y)).value_in(units.parsec)
        p_x = (converter.to_si(planets.x)).value_in(units.parsec)
        p_y = (converter.to_si(planets.y)).value_in(units.parsec)
        s_sizes = (converter.to_si(stars.mass)).value_in(units.MSun) * 30.0+5
        p_sizes = (converter.to_si(planets.mass)).value_in(units.MJupiter) * 30.0+30
        plot_time = (converter.to_si(current_time)).value_in(units.Myr)
        
        # Make the Plot!
        
        matplotlib.rcParams['font.family'] = 'sans-serif'
        matplotlib.rcParams['font.sans-serif'] = ['Tahoma']
        matplotlib.rcParams['figure.figsize'] = (16,9)
        subplot(111)#, axisbg='#000003')
        #gca().set_aspect('equal', adjustable='box')
        plt.axis([-25,25,-25,25])
        #plt.axis([-2,2,-1.1,1.1])
        plt.xticks(np.arange(-25, 25, 10))
        plt.grid()
        plt.scatter(p_x,p_y, marker='o', s=p_sizes, color='r')
        plt.scatter(s_x,s_y, marker='*', s=s_sizes, color='k')
        
        # Making the Labels
        plt.tick_params(axis='both', which='major', labelsize=16)
        plt.title('The %s Cluster (%i Stars, %i Planets)' %(cluster_name, number_of_stars, number_of_planets), fontsize=25)
        plt.xlabel('X-Axis (pc)', fontsize=20)
        plt.ylabel('Y-Axis (pc)', fontsize=20)
        plt.text(-17.5, -20, '%.2f Myr' %(plot_time), style='italic',bbox={'facecolor':'blue', 'alpha':0.5, 'pad':15}, fontsize=15)
        
        # Showing or Saving the Plot
        if show_plot:
            plt.show()
        if save_plot:
            plt.ioff()
            plt.savefig(file_dir+file_name, format="png", dpi=150)
            #plt.savefig(file_dir+file_name, format="png", dpi=75)
            plt.clf()
            plt.close('all')

In [49]:
def run_plotting(run_dir, cluster_name, converter, save_plot=True, show_plot=False):
    """
    This is a function created to plot a 2D projection of the Cluster for every time-step.
    The Function Returns: A bunch of plots that are either saved and/or displayed.
    """
    print "[UPDATE] Starting Bulk Plotting (%s) ..." %(tp.strftime("%Y/%m/%d-%H:%M:%S", tp.gmtime()))
    sys.stdout.flush()
    
# First get all of the times from the MasterParticleSet file names
    search = glob.glob(run_dir+cluster_name+"*.hdf5")
    i=0
    current_time = []
    for name in search:
        time = name.split("_")
        current_time.append(float(time[2][1:-5]) | nbody_system.time)
        
    step_number = 0
    for time in current_time:
        file_name = "%s_MS_t%.3f.hdf5" %(cluster_name, time.number)
        objects = read_set_from_file(run_dir+file_name, format="hdf5", close_file = True)
        plot_cluster_2D(objects, step_number, time, cluster_name, converter, run_dir, show_plot, save_plot)
        step_number+=1
    print "[UPDATE] Finished Bulk Plotting (%s)!"  %(tp.strftime("%Y/%m/%d-%H:%M:%S", tp.gmtime()))
    sys.stdout.flush()
#run_plotting('/home/jglaser/Tycho/Runs/Garrus/160426/', 'Garrus', 0.05 | nbody.time)

In [7]:
def Import_Cluster(file_path, cluster_name):
    """
    This is a function created to read in a Cluster's Stellar Parcticle Set.
    The Function Returns: The Set of Stars, AMUSE Nbody Converter, Cluster Name, & King's Model W0 Value
    """
    # Parses Variables from File_Name
    file_name = (file_path.split("/")[-1])[:-5]
    print file_name
    file_format = ("hdf5")
    w = 2.5
#    N = int((file_name.split("_")[-2])[1:])
#    total_mass = float((file_name.split("_")[-1])[1:]) | units.MSun
    
    # Reads in the Set of Stars from the Specified File Path
    stars = read_set_from_file(file_path, format=file_format, close_file=True)
    number_of_stars = len(stars)
    
    # This converter is set so that [1 mass = Cluster's Total Mass] & [1 length = 1 parsec].
#    converter = nbody_system.nbody_to_si(total_mass, 1 | units.parsec)
    return stars, cluster_name, w

In [8]:
def read_initial_state(cluster_name):
    ''' Reads in an initial state for the Tycho Module.
        file_prefix: String Value for a Prefix to the Saved File
    ''' 
# TODO: Convert the saved datasets from SI to NBody. Also everything else in this function.

# First, Define the Directory where Initial State is Stored
#    file_dir = os.getcwd()+"/InitialState"
    file_dir = "/home/draco/jthornton/Tycho/InitialState/"
    
    file_base = file_dir + cluster_name
    file_pkl = file_base + "_ic.pkl"
    file_hdf5 = file_base + "_particles.hdf5"
# Second, Read the Master AMUSE Particle Set from a HDF5 File
    file_format = "hdf5"
    master_set = read_set_from_file(file_hdf5, format=file_format, close_file=True)
# Third, unPickle the Initial Conditions Array
    ic_file = open(file_pkl, "rb")
    ic_array = pickle.load(ic_file)
    ic_file.close()
# Fourth convert ic_array.total_smass and viral_radius from strings to floats using string split
    total_smass = float(ic_array.total_smass) | units.kg
    viral_radius = float(ic_array.viral_radius) | units.m
# Fifth, Define the Master Set's Converter
    converter = nbody_system.nbody_to_si(total_smass, viral_radius)
    return master_set, ic_array, converter

Running Commands


In [38]:
w = 2.5
cluster_names = ["TestCluster4"]
runs_dir = "/home/draco/jthornton/Tycho/Run/MasterParticleSet/"

In [35]:
print runs_dir
print cluster_name


/home/draco/jthornton/Tycho/Run/MasterParticleSet/
TestCluster4

In [48]:
search = glob.glob(runs_dir+cluster_name+"*.hdf5")

i=0
current_time = []
for name in search:
    time = name.split("_")
    current_time.append(float(time[2][1:-5]))

print current_time


[0.031, 0.188, 0.344, 0.5, 0.656, 0.812, 0.969, 1.125, 1.281, 1.438, 1.594, 1.75, 1.906, 2.062, 2.219, 2.375, 2.531, 2.688, 2.844, 3.0, 3.156, 3.312, 3.469, 3.625, 3.781, 3.938, 4.094, 4.25, 4.406, 4.562, 4.719, 4.875, 5.031, 5.188, 5.344, 5.5, 5.656, 5.812, 5.969, 6.125, 6.281, 6.438, 6.594, 6.75, 6.906, 7.062, 7.219, 7.375, 7.531, 7.688]

In [46]:
Energy, Time, T, U, L, P = analysis.GetValues("BinaryParticles2", start_time = | nbody_system.time, step_time = 0.125 | nbody_system.time)

analysis.EnergyGraph(Time, Energy, T, U, "BinaryStabilityTest2")


  File "<ipython-input-46-fe3e8dc09c59>", line 1
    Energy, Time, T, U, L, P = analysis.GetValues("BinaryParticles2", start_time = | nbody_system.time, step)
                                                                                   ^
SyntaxError: invalid syntax

In [51]:
for cluster_name in cluster_names:
    master_set, ic_array, converter = read_initial_state(cluster_name)
    time_step = 0.03125
    run_plotting(runs_dir, cluster_name, converter)


[UPDATE] Starting Bulk Plotting (2017/08/01-15:38:40) ...
[UPDATE] Finished Bulk Plotting (2017/08/01-15:39:08)!

In [ ]:
Quick_Cluster = read_set_from_file("/home/draco/jthornton/BinaryTycho/Runs/400AU0Binaries2Run/160707/Master/400AU0Binaries2Run_Master_t0.050.hdf5", format="hdf5", close_file=True)

In [ ]:
plot_cluster_2D(Quick_Cluster, 5, 0.250 | nbody.time, "400AU0Binaries2Run", "/home/draco/jthornton/BinaryTycho/Runs/400AU0Binaries2Run/160707/", save_plot=False, show_plot=True)

In [60]:
particles = read_set_from_file("/home/draco/jthornton/Tycho/Run/MasterParticleSet/PlanetCluster2_MS_t0.125.hdf5", format="hdf5", close_file=True)

In [61]:
thing = particles.sorted_by_attribute('type')
print thing


                 key    host_star           id         mass  potential_in_code       radius     timestep         type           vx           vy           vz            x            y            z
                   -         none         none         mass  length**2 * time**-2       length         time         none  length * time**-1  length * time**-1  length * time**-1       length       length       length
====================  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========
12688171501684177609          357      5000000    3.186e-06    0.000e+00    8.719e-10    0.000e+00       planet    4.452e+00   -3.090e+00   -2.854e+00   -1.419e+00   -5.084e-01   -5.402e-02
 7916493687008242759          830      5000001    3.186e-06    0.000e+00    3.557e-10    0.000e+00       planet    1.915e+00   -2.334e+00   -1.768e+00    6.997e-01    1.077e-01    2.659e-02
13547572684317784710          636      5000002    3.186e-06    0.000e+00    4.241e-09    0.000e+00       planet    3.202e+00   -2.363e+00   -1.752e+00    3.722e-01   -2.584e-01   -6.994e-02
15442874016649452821           17      5000003    3.186e-06    0.000e+00    7.813e-09    0.000e+00       planet    3.498e+00   -1.461e-01   -3.881e-01   -3.281e-01    2.973e-02   -7.059e-02
 6689876198859058020          902      5000004    3.186e-06    0.000e+00    6.417e-09    0.000e+00       planet   -1.744e+00   -1.530e+00   -4.741e-01    4.807e-01    1.553e-01   -2.886e-01
14870671889060267399          605      5000005    3.186e-06    0.000e+00    4.411e-09    0.000e+00       planet   -3.532e+00    4.952e-01    1.499e-02    2.930e-01   -1.576e-01    2.358e-01
 6378417169658115074           22      5000006    3.186e-06   -9.445e-01    1.916e-09    9.766e-04       planet    2.802e+00    5.461e+00   -1.543e+00   -3.390e-01   -1.397e-01    9.340e-01
 1106515780574101331          715      5000007    3.186e-06    0.000e+00    3.104e-10    0.000e+00       planet    6.188e-01   -4.862e+00    4.099e+00    2.687e-01   -1.716e-01   -5.114e-01
 6819230334122571541          552      5000008    3.186e-06    0.000e+00    2.530e-10    0.000e+00       planet    1.257e+01    3.322e+00   -4.012e+00   -1.965e-01    5.466e-01    4.457e-01
 9796211659175692946          787      5000009    3.186e-06   -5.542e-01    5.954e-11    1.953e-03       planet    1.496e+01    1.072e+01   -1.016e+00    1.324e+00    1.180e+00    5.043e-03
10556452273497789294          764      5000010    3.186e-06    0.000e+00    1.806e-09    0.000e+00       planet    4.855e+00   -1.371e+00    1.166e+00   -1.285e-02   -1.251e-02   -8.398e-01
18135779873876082339          394      5000011    3.186e-06    0.000e+00    2.703e-10    0.000e+00       planet   -5.938e+00    7.928e-01   -3.630e+00    1.116e-01   -1.434e-01   -1.662e-03
  392725148722633240          203      5000012    3.186e-06    0.000e+00    3.536e-09    0.000e+00       planet    3.838e+00    3.297e+00   -4.257e-01    3.276e-01    2.065e-01   -3.688e-01
 6898256886448312336          793      5000013    3.186e-06    0.000e+00    1.161e-09    0.000e+00       planet   -3.295e+00    4.095e+00   -2.063e+00   -9.729e-01   -2.090e-01    4.039e-01
 3429423018814741410          784      5000014    3.186e-06    0.000e+00    1.682e-09    0.000e+00       planet   -3.678e+00    3.815e+00   -1.310e+00    2.561e-01   -1.052e+00    7.811e-01
 5003198063156395105          362      5000015    3.186e-06   -1.176e+00    6.550e-09    4.883e-04       planet    1.784e+00    4.255e+00    1.497e+00    2.705e-01    6.505e-01   -1.674e-01
 1154529845069929007          111      5000016    3.186e-06    0.000e+00    1.664e-11    0.000e+00       planet    1.743e+01   -1.443e+01   -8.887e-01   -3.580e-01    1.162e+00    6.002e-01
 4982740129478408815          554      5000017    3.186e-06    0.000e+00    3.038e-10    0.000e+00       planet    8.391e+00   -1.173e+00    1.393e+00   -1.119e+00    4.221e-01   -3.117e-01
 2583525875142920674          187      5000018    3.186e-06    0.000e+00    5.140e-09    0.000e+00       planet   -5.599e-01    3.575e+00   -8.573e-01   -3.660e-02   -3.588e-01    2.566e-01
 6374590985356689536          568      5000019    3.186e-06    0.000e+00    4.483e-09    0.000e+00       planet   -1.498e+00    3.405e+00   -2.818e-02   -4.587e-03   -9.156e-03    2.178e-01
                 ...          ...          ...          ...          ...          ...          ...          ...          ...          ...          ...          ...          ...          ...
14729045142448084325            0          981    3.021e-03   -7.654e-01    4.389e-03    3.125e-02         star   -6.566e-01    1.291e-02   -2.822e-01    5.379e-01   -9.942e-01    5.711e-01
15754815678706513988            0          982    7.073e-04   -1.034e+00    1.028e-03    1.562e-02         star    3.397e-01   -3.497e-01   -3.356e-01   -1.920e-01   -7.167e-01   -3.067e-01
17994983706795499655            0          983    3.495e-03   -1.205e+00    5.077e-03    3.906e-03         star    4.218e-01    1.350e-01   -4.045e-01   -2.923e-01   -1.778e-01   -5.771e-01
  455427535711177645            0          984    1.811e-04    0.000e+00    2.632e-04    0.000e+00         star    1.641e-01    5.680e-02    2.489e-01    3.676e-02   -1.816e-01   -6.575e-01
 4905564471336831873            0          985    3.816e-04   -8.552e-01    5.544e-04    7.812e-03         star    7.130e-01    1.236e-02   -2.146e-01   -3.901e-01    9.317e-01    5.034e-01
 9510455309765107822            0          986    5.727e-04    0.000e+00    8.321e-04    0.000e+00         star   -2.374e-01    2.769e-02   -2.649e-01   -1.214e-01    5.233e-01    8.641e-01
 9914374994176841857            0          987    1.070e-04    0.000e+00    1.554e-04    0.000e+00         star    6.697e-01    2.499e-01   -4.087e-01    3.550e-01    4.670e-01    1.402e-02
 4276984574616459088            0          988    5.282e-04   -8.880e-01    7.673e-04    1.562e-02         star    1.247e-01   -4.893e-01   -3.144e-01   -7.278e-01   -1.492e-01   -7.316e-01
   18951702819672282            0          989    1.274e-03    0.000e+00    1.851e-03    0.000e+00         star    5.029e-02   -3.639e-01    3.773e-01   -1.662e-02   -3.232e-01    4.249e-01
16230933777007014859            0          990    3.771e-05   -9.610e-01    5.479e-05    3.125e-02         star   -1.399e-01    1.618e-01    4.142e-01    1.292e-01   -7.349e-01    5.356e-01
10823144802655090911            0          991    2.438e-04    0.000e+00    3.542e-04    0.000e+00         star   -1.143e-01   -9.269e-02    4.189e-01    9.768e-01   -1.787e-02   -2.415e-01
 1696298999065680437            0          992    2.653e-03   -1.304e+00    3.854e-03    7.812e-03         star    4.431e-01   -5.834e-01   -9.322e-01   -2.801e-02    4.144e-01   -1.094e-01
 9616841058595555971            0          993    4.460e-04   -9.969e-01    6.480e-04    1.562e-02         star    1.746e-01   -7.249e-01   -4.748e-02    6.664e-01    4.655e-01   -5.072e-02
11697193010544706832            0          994    3.759e-03   -9.577e-01    5.460e-03    3.906e-03         star    2.435e-01   -4.509e-01    7.507e-01    5.268e-01   -6.662e-01    4.335e-01
11456687046597257931            0          995    1.059e-03   -1.301e+00    1.538e-03    1.953e-03         star    1.461e-01    6.158e-01   -2.629e-01    4.305e-01   -1.103e-01   -2.257e-01
14725133058838686991            0          996    2.157e-04   -1.186e+00    3.133e-04    1.562e-02         star   -4.111e-01    5.814e-01   -6.478e-02    5.113e-01    1.936e-01   -1.673e-01
  412836740052573207            0          997    7.697e-05    0.000e+00    1.118e-04    0.000e+00         star   -6.609e-01   -2.447e-01    1.000e+00    3.004e-01    3.384e-02    2.372e-01
18101560651685470841            0          998    7.413e-05   -1.006e+00    1.077e-04    1.562e-02         star    3.603e-03    3.791e-01   -9.622e-02   -8.124e-01   -2.471e-02    1.042e-01
 1665160397863885728            0          999    5.861e-04   -1.272e+00    8.514e-04    1.562e-02         star   -5.043e-01   -6.112e-01   -5.226e-01    3.914e-01    1.937e-01   -1.840e-01
 5003673756422732912            0         1000    8.798e-04   -9.397e-01    1.278e-03    3.906e-03         star    2.671e-01   -3.165e-01   -3.343e-01    2.698e-01   -3.320e-01   -9.421e-01
====================  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========

In [ ]:
initial_state = "/home/draco/jthornton/Tycho/InitialState/movieTest2_particles.hdf5"
final_state = "/home/draco/jthornton/Tycho/Runs/MasterParticleSet/movieTest2_MS_t99.800.hdf5"

MasterSet1 = []
MasterSet2 = []

MasterSet1 = read_set_from_file(format = 'hdf5')
MasterSet2 = read_set_from_file()