check.py 6.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
""" @package ./examples/galaxy_merger_star_formation_3d/check.py
Code that checks results of 3d galaxy merger problem

created by Rainer Weinberger, last modified 09.03.2019
"""


""" load libraries """
import sys    ## system calls
import numpy as np    ## load numpy
import h5py    ## load h5py; needed to read snapshots
import matplotlib.pyplot as plt    ## plot stuff
from scipy.interpolate import interp1d    ## inetrpolation

simulation_directory = str(sys.argv[1])
Rainer Weinberger's avatar
Rainer Weinberger committed
16
print("galaxy_merger_star_formation_3d: checking simulation output in directory " + simulation_directory) 
17
18

createReferenceSolution = False
Rainer Weinberger's avatar
Rainer Weinberger committed
19
createFigures = True
20
21
22
23
24
25
26
27

FloatType = np.float64  # double precision: np.float64, for single use np.float32
Gcgs = 6.6738e-8


""" compare to reference output """
data = np.loadtxt(simulation_directory+"/output/sfr.txt")
if createReferenceSolution:
Rainer Weinberger's avatar
Rainer Weinberger committed
28
    np.savetxt(simulation_directory+"/sfr_reduced_ref.txt", data[::50,:])
29

Rainer Weinberger's avatar
Rainer Weinberger committed
30
data_ref = np.loadtxt(simulation_directory+"/sfr_reduced_ref.txt")
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
time_ref = data_ref[:,0]
mstar_ref =  data_ref[:,5]

time = data[:,0]
mstar =  data[:,5]

time_probe = np.linspace(0.5,2.8,10)
delta_mass_stars = np.zeros(time_probe.shape)
avg_mass_stars = np.zeros(time_probe.shape)

for i, t in enumerate(time_probe):
    delta_mass_stars[i]  = np.interp(t, time, mstar)
    delta_mass_stars[i] -= np.interp(t, time_ref, mstar_ref)
    avg_mass_stars[i]  = 0.5 * np.interp(t, time, mstar)
    avg_mass_stars[i] += 0.5 * np.interp(t, time_ref, mstar_ref)


if createFigures:
    fig, ax = plt.subplots(2,3, figsize=np.array([6.9,5.35]), sharex=True, sharey=True )
    fig.subplots_adjust(hspace=0.0, wspace=0.0, left=0.1,right=0.98, bottom=0.1, top=0.98)

    ax[0, 0].set_xticks([300,320,340])
    ax[0, 0].set_yticks([300,325,350])

for i_plot, i_snap in enumerate([10,11,12,21,22,24]):
    filename = '/output/snap_%03d.hdf5'%i_snap
    print(simulation_directory+filename)
    try:
        data = h5py.File(simulation_directory+filename, "r")
    except:
        break
    ## figure
    pos1 = np.array( data['PartType1']['Coordinates'], dtype=np.float64 )
    pos2 = np.array( data['PartType2']['Coordinates'], dtype=np.float64 )
    pos3 = np.array( data['PartType3']['Coordinates'], dtype=np.float64 )
    pos4 = np.array( data['PartType4']['Coordinates'], dtype=np.float64 )
    
    ix = np.int(i_plot % 3)
    iy = np.int(i_plot / 3)
    
    if createFigures:
        ax[iy,ix].scatter(pos2[:, 0], pos2[:, 1], marker='.', c='k', s=0.05, alpha=0.5, rasterized=True, zorder=2)
        ax[iy,ix].scatter(pos3[:, 0], pos3[:, 1], marker='.', c='k', s=0.05, alpha=0.5, rasterized=True, zorder=2)
        ax[iy,ix].scatter(pos4[:, 0], pos4[:, 1], marker='.', c='red', s=0.05, alpha=0.2, rasterized=True, zorder=3)
        ax[iy,ix].set_aspect('equal')
        ax[iy,ix].set_xlim([295,355])
        ax[iy,ix].set_ylim([290,360])
    

if createFigures:
    ## trajectory
    filename = '/output/snap_%03d.hdf5'%0
    data = h5py.File(simulation_directory+filename, "r")
    pos2 = np.array( data['PartType2']['Coordinates'], dtype=np.float64 )
    ids2 = np.array( data['PartType2']['ParticleIDs'], dtype=np.int32 )
    i_gal1 = np.where(pos2[:,0] < 320)[0]
    i_gal2 = np.where(pos2[:,0] > 330)[0]
    id_gal1 = ids2[i_gal1]
    id_gal2 = ids2[i_gal2]
    
    
    pos_gal1 = np.zeros([32,3], dtype=np.float64)
    pos_gal2 = np.zeros([32,3], dtype=np.float64)
    
    for i_snap in np.arange(32):
        filename = '/output/snap_%03d.hdf5'%i_snap
        try:
            data = h5py.File(simulation_directory+filename, "r")
        except:
            break
        pos2 = np.array( data['PartType2']['Coordinates'], dtype=np.float64 )
        ids2 = np.array( data['PartType2']['ParticleIDs'], dtype=np.int32 )
        ids, i_gal1, dummy = np.intersect1d(ids2, id_gal1, return_indices=True)
        ids, i_gal2, dummy = np.intersect1d(ids2, id_gal2, return_indices=True)
        
        pos_gal1[i_snap,:] = np.average(pos2[i_gal1,:], axis=0)
        pos_gal2[i_snap,:] = np.average(pos2[i_gal2,:], axis=0)
        
    p1x = interp1d(np.arange(32), pos_gal1[:,0], kind='cubic')
    p1y = interp1d(np.arange(32), pos_gal1[:,1], kind='cubic')
    p2x = interp1d(np.arange(32), pos_gal2[:,0], kind='cubic')
    p2y = interp1d(np.arange(32), pos_gal2[:,1], kind='cubic')
    
    
    interp = np.linspace(0,10,1000)
    ax[0,0].plot(p1x(interp), p1y(interp), 'b:', alpha=0.5, lw=4, zorder=1)
    ax[0,0].plot(p2x(interp), p2y(interp), 'b--', alpha=0.5, lw=4, zorder=1)
    
    interp = np.linspace(0,11,1000)
    ax[0,1].plot(p1x(interp), p1y(interp), 'b:', alpha=0.5, lw=4, zorder=1)
    ax[0,1].plot(p2x(interp), p2y(interp), 'b--', alpha=0.5, lw=4, zorder=1)
    
    interp = np.linspace(0,12,1000)
    ax[0,2].plot(p1x(interp), p1y(interp), 'b:', alpha=0.5, lw=4, zorder=1)
    ax[0,2].plot(p2x(interp), p2y(interp), 'b--', alpha=0.5, lw=4, zorder=1)
    
    interp = np.linspace(11,21,1000)
    ax[1,0].plot(p1x(interp), p1y(interp), 'b:', alpha=0.5, lw=4, zorder=1)
    ax[1,0].plot(p2x(interp), p2y(interp), 'b--', alpha=0.5, lw=4, zorder=1)
    
    interp = np.linspace(12,22,1000)
    ax[1,1].plot(p1x(interp), p1y(interp), 'b:', alpha=0.5, lw=4, zorder=1)
    ax[1,1].plot(p2x(interp), p2y(interp), 'b--', alpha=0.5, lw=4, zorder=1)
    
    interp = np.linspace(14,24,1000)
    ax[1,2].plot(p1x(interp), p1y(interp), 'b:', alpha=0.5, lw=4, zorder=1)
    ax[1,2].plot(p2x(interp), p2y(interp), 'b--', alpha=0.5, lw=4, zorder=1)
    
    ax[0,0].set_ylabel("[kpc]")
    ax[1,0].set_ylabel("[kpc]")
    ax[1,0].set_xlabel("[kpc]")
    ax[1,1].set_xlabel("[kpc]")
    ax[1,2].set_xlabel("[kpc]")
    
    fig.savefig(simulation_directory+'./stars_evolution.pdf')
    
    """ figure -- optional """
    fig=plt.figure()
    ax = plt.axes([0.15,0.15,0.8,0.8])
    ax.plot(time, mstar * 1e10/0.6774, label="new")
    ax.plot(time_ref, mstar_ref * 1e10/0.6774, label="ref")
    ax.set_xlabel(r"time")
    ax.set_ylabel(r"Stellar mass formed [M$_\odot$]")
    ax.legend(loc=2)
    fig.savefig(simulation_directory+"./Mstar_time.pdf")


""" check if deviations within tolerance """
abs_tolerance = 3.e8 / (1.e10 / 0.6774)
if np.max( np.abs(delta_mass_stars ) ) > abs_tolerance:
    print("Error: stellar mass deviaties from reference more than %g (Msun/Mpc^3): "% abs_tolerance )
    print("times:")
    print(time_probe)
    print("relative deviation:")
    print(delta_mass_stars / avg_mass_stars)
    print("absolute deviation (1e6 Msun):")
    print(delta_mass_stars*1.0e4/0.6774)
    sys.exit(1)

""" if everything is ok """
sys.exit(0)