aboutsummaryrefslogtreecommitdiffstats
path: root/Plots/Chaos/ai3_stacked.py
blob: 2595572bf395ba9675fab1f42e199347953210ac (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Plot Semi-Major Axis vs. Inclination. 3 Panels. Stack.
Requires Coordinates_XX.hdf5, Resonances_XX.hdf5.

Dirlist Format:
tag_01,/path/Coordinates_01.hdf5,/path/Resonances_01.hdf5
tag_01,/path/Coordinates_02.hdf5,/path/Resonances_02.hdf5
tag_02,/path/Coordinates_01.hdf5,/path/Resonances_01.hdf5
tag_02,/path/Coordinates_02.hdf5,/path/Resonances_02.hdf5
...
tag_NN,/path/Coordinates_MM.hdf5,/path/Resonances_MM.hdf5
"""

import matplotlib as mpl; mpl.use('agg')
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sys
import other_helpers as oh
import constants as C


# List of Directories
if sys.stdin.isatty():
    print "!! No Directory List (Use Stdin)."
    sys.exit()
else:
    lines = sys.stdin.read().rstrip("\n").split("\n")
    tags = []
    crd_files = []
    res_files = []
    for line in lines:
        line = line.strip().split(',')
        tags.append(line[0])
        crd_files.append(line[1])
        res_files.append(line[2])
    print "// Reading %i Runs" % len(crd_files)

# Create Numerical IDs for Unique Tags
tag_ids = []; ktag = 0; first = True
for tag in tags:
    if not first:
        if not tag == tag_last:
            ktag += 1
    else:
        first = False
    tag_ids.append(ktag)
    tag_last = tag

# Retain Unique Tags
tags_unique = []
for tag in tags:
    if not tag in tags_unique:
        tags_unique.append(tag)

# Create Lists with Filenames to Load -- Coordinates
fnames_crd_all = []
fnames_crd_loc = []
for itag_id, tag_id in enumerate(tag_ids):
    # Append filename to local
    fnames_crd_loc.append(crd_files[itag_id])
    # Is the next tag the same as me or are we last?
    # If not, append to global and reset local. 
    # Otherwise, do nothting.
    if (itag_id == len(tag_ids)-1) or (not (tag_ids[itag_id+1] == tag_id)):
        fnames_crd_all.append(fnames_crd_loc)
        fnames_crd_loc = []

# Create Lists with Filenames to Load -- Resonances
fnames_res_all = []
fnames_res_loc = []
for itag_id, tag_id in enumerate(tag_ids):
    # Append filename to local
    fnames_res_loc.append(res_files[itag_id])
    # Is the next tag the same as me or are we last?
    # If not, append to global and reset local. 
    # Otherwise, do nothting.
    if (itag_id == len(tag_ids)-1) or (not (tag_ids[itag_id+1] == tag_id)):
        fnames_res_all.append(fnames_res_loc)
        fnames_res_loc = []

# Load Coordinates
dfo_all = []
print "// Loading Coordinates Files"
for fnames_crd_loc in fnames_crd_all:
    dfo = pd.DataFrame()
    for fname in fnames_crd_loc:
        print "** %s" % fname
        with pd.HDFStore("%s" % fname, 'r') as store:
            dfo_tmp = store['df']
            dfo = pd.concat([dfo, dfo_tmp])
            del dfo_tmp
    dfo_all.append(dfo.reset_index(drop=True))

# Load Resonances
dfr_all = []
print "// Loading Resonance Files"
for fnames_res_loc in fnames_res_all:
    dfr = pd.DataFrame()
    if not fnames_res_loc[0] == 'NONE':
        for fname in fnames_res_loc:
            print "** %s" % fname
            with pd.HDFStore("%s" % fname, 'r') as store:
                dfr_tmp = store['df']
                dfr = pd.concat([dfr, dfr_tmp])
                del dfr_tmp
        # Compute Mean Location for Resonances, MultiIndex Magic
        dfr.set_index(['nrun', 'nstep'], drop=True, append=False, inplace=True)
        dfr = dfr.mean(level='nstep')
        dfr['nstep'] = dfr.index
        dfr.reset_index(drop=True, inplace=True)
    dfr_all.append(dfr)

# Determine Step Range
nsteps = np.asarray(dfo_all[0].nstep.unique())

# Global Mass Scaling
ms, ns = oh.mkline(5.0/2000.0, 1.0, 2.0, 16.0)

# Loop Steps
print "// Processing %i Outputs per Run" % len(nsteps)
for nstep in nsteps:
    print "** Step %012d/%012d" % (nstep, nsteps[-1])

    # Setup Figure
    fig, axarr = plt.subplots(1,3)
    fig.set_size_inches(12,3)

    # Loop Runs
    for isim in [ 0, 1, 2 ]:

        # Select Axis
        ax = axarr[isim]

        # Select Sim
        dfo = dfo_all[isim]
        dfr = dfr_all[isim]

        # Pick Step
        dfo = dfo[dfo.nstep == nstep]
        if not fnames_res_all[isim][0] == 'NONE':
            dfr = dfr[dfr.nstep == nstep]

        # Remove Giant Planets
        dfo = dfo[dfo.mass < 5.0]

        # Scaling
        s = ms * np.asarray(dfo.mass) + ns
        c = np.ones([len(dfo.mass),4]) * np.array([0.6,0.6,0.6,1.0])

        # #############################################
        # #############################################
        # PLANETESIMALS
        # #############################################
        # #############################################
        
        # Plot
        ax.scatter(dfo.a, dfo.i * C.r2d, \
                   c=c, s=s**2, alpha=0.7, \
                   edgecolor='k', \
                   linewidth=0.5)

        # #############################################
        # #############################################
        # RESONANCE LOCATIONS
        # #############################################
        # #############################################

        if not fnames_res_all[isim][0] == 'NONE':

            # Resonances (Secular)
            ax.plot([dfr.nu_5.iloc[0], dfr.nu_5.iloc[0]], \
                    [0, 90], \
                    'k', lw=0.5, label='nu_5', zorder=-10)
            ax.plot([dfr.nu_6.iloc[0], dfr.nu_6.iloc[0]], \
                    [0, 90], \
                    'k', lw=0.5, label='nu_6', zorder=-10)
            if not np.isnan(dfr.nu_16_01.iloc[0]):
                ax.plot([dfr.nu_16_01.iloc[0], dfr.nu_16_01.iloc[0]], \
                        [0, 90], \
                        'k', lw=0.5, label='nu_16', zorder=-10)
            if not np.isnan(dfr.nu_16_02.iloc[0]):
                ax.plot([dfr.nu_16_02.iloc[0], dfr.nu_16_02.iloc[0]], \
                        [0, 90], \
                        'k', lw=0.5, label='nu_16', zorder=-10)

            # Resonances (MMR)
            ax.plot([dfr.two_to_one.iloc[0], dfr.two_to_one.iloc[0]], \
                    [0, 90], \
                    color=(0.6,0.6,0.6), lw=0.5, label='2:1', zorder=-10)
            ax.plot([dfr.three_to_one.iloc[0], dfr.three_to_one.iloc[0]], \
                    [0, 90], \
                    color=(0.6,0.6,0.6), lw=0.5, label='3:1', zorder=-10)
            ax.plot([dfr.three_to_two.iloc[0], dfr.three_to_two.iloc[0]], \
                    [0, 90], \
                    color=(0.6,0.6,0.6), lw=0.5, label='3:2', zorder=-10)

        # #############################################
        # #############################################
        # ANNOTATIONS
        # #############################################
        # #############################################

        if not fnames_res_all[isim][0] == 'NONE':

            # Annotate (Secular)
            ax.text(dfr.nu_5.iloc[0], 55, \
                    r'$\nu_5$', transform=ax.transData, \
                    va='center', ha='center', \
                    bbox=dict(facecolor='w', lw=0.5), fontsize='small')

            ax.text(dfr.nu_6.iloc[0], 55, \
                    r'$\nu_6$', transform=ax.transData, \
                    va='center', ha='center', \
                    bbox=dict(facecolor='w', lw=0.5), fontsize='small')
            
            if not np.isnan(dfr.nu_16_01.iloc[0]):
                ax.text(dfr.nu_16_01.iloc[0], 55, \
                        r'$\nu_{16}$', transform=ax.transData, \
                        va='center', ha='center', \
                        bbox=dict(facecolor='w', lw=0.5), fontsize='small')
            if not np.isnan(dfr.nu_16_02.iloc[0]):
                ax.text(dfr.nu_16_02.iloc[0], 55, \
                        r'$\nu_{16}$', transform=ax.transData, \
                        va='center', ha='center', \
                        bbox=dict(facecolor='w', lw=0.5), fontsize='small')

            # Annotate (MMR)
            ax.text(dfr.two_to_one.iloc[0], 47.5, \
                    r'$2:1$', transform=ax.transData, \
                    va='center', ha='center', \
                    bbox=dict(facecolor='w', lw=0.5), fontsize='x-small')

            ax.text(dfr.three_to_one.iloc[0], 47.5, \
                    r'$3:1$', transform=ax.transData, \
                    va='center', ha='center', \
                    bbox=dict(facecolor='w', lw=0.5), fontsize='x-small')

            ax.text(dfr.three_to_two.iloc[0], 47.5, \
                    r'$3:2$', transform=ax.transData, \
                    va='center', ha='center', \
                    bbox=dict(facecolor='w', lw=0.5), fontsize='x-small')

        # Number of Particles
        ax.text(0.1, 0.7, "(%i, %i, %i)" % ( np.sum(dfo.mass>=C.m_cutoff), \
                                             np.sum(dfo.mass<C.m_cutoff), \
                                             len(dfo) ), \
                va='top', ha='left', transform=ax.transAxes, \
                fontsize='small')

        # #############################################
        # #############################################
        # CUTOFF, JUPITER CROSSING
        # #############################################
        # #############################################

        # Inner Cutoff
        ax.fill_between([0.0, 0.1], 0.0, 60.0, \
                        lw=0, facecolor='r', \
                        alpha=0.1, zorder=20)

        # Title
        ax.set_title("%s" % tags_unique[isim], fontsize='small')

    # #############################################
    # #############################################
    # STYLING
    # #############################################
    # #############################################
            
    # Limits
    for ax in axarr.flatten():
        ax.set_xlim([0,5])
        ax.set_ylim([0,60.0])
        
    # Ticks
    for ax in axarr.flatten():
        ax.xaxis.set_major_locator(\
            mpl.ticker.MaxNLocator(prune='both', nbins='5'))
        ax.yaxis.set_major_locator(\
            mpl.ticker.MaxNLocator(prune='both', nbins='5'))
    
    # Hide Tick Labels
    # for ax in axarr[:3,:].flatten():
    #     plt.setp(ax.get_xticklabels(), visible=False)
    for ax in axarr[1:].flatten():
        plt.setp(ax.get_yticklabels(), visible=False)

    # Axis Labels
    # for ax in axarr[-1,:].flatten():
    #     ax.set_xlabel('Semi-Major Axis (AU)')
    # for ax in axarr[:,0].flatten():
    #     ax.set_ylabel('Eccentricity')
    for ax in axarr:
        ax.set_xlabel('Semi-Major Axis (AU)', fontsize='small')
    axarr[0].set_ylabel('Inclination (Degree)', fontsize='small')
            
    # Title, Legend
    tout = dfo.time.iloc[0]
    txtsup = "nstep = %012d // time = %.2e Yr" % (nstep, tout)
    txtsup += "  // (Embryos/Planets, Planetesimals, Total Particles)"
    fig.suptitle(txtsup, fontsize='small')

    # Join Panels
    # fig.subplots_adjust(hspace=0,wspace=0,top=0.96)
    fig.subplots_adjust(top=0.84)

    # Save Figure
    fig.savefig("ai3_stacked_%012d.png" % nstep, bbox_inches='tight')

    # Clean Up
    plt.close(fig)