Peano
Loading...
Searching...
No Matches
csv_plot.py
Go to the documentation of this file.
1import numpy as np
2import matplotlib.pyplot as plt
3from scipy import io
4import os
5import time
6import argparse
7
8pi=np.pi
9G=6.67408*1e-11
10M=0.15
11rho_c_ini=1
12rho_c0_value=1.8788*10**(-26)
13a_i=1e-3
14
15def file_len(fname):
16 with open(fname) as f:
17 for i, l in enumerate(f):
18 pass
19 return i + 1
20
21#lis=os.listdir("./")
22#for item in lis:
23# if item.endswith(".pvd") or item.endswith(".vtp"): os.remove(os.path.join("./",item))
24
25parser = argparse.ArgumentParser(description='Tracer_conversion')
26parser.add_argument("-if", "--input-file", dest="file_name", required="True", type=str, help="input csv file name (and directory)" )
27parser.add_argument("-pq", "--plot-quantity", dest="pq", default="rho", type=str, choices=["rho","v_x","v_y","v_z","E_t","P"], help="choose the quantity you want to plot" )
28parser.add_argument("-rescale", "--rescale", dest="IfRescale", action="store_true", default=False, help="if you need the rescaling using turnaround quantites" )
29parser.add_argument("-ylog", "--yscale-log", dest="Ifylog", action="store_true", default=False, help="if you need the plot with y axis in log scale" )
30parser.add_argument("-XUcode", "--x-code-unit", dest="IfXunitInCode", action="store_true", default=False, help="enable this option, if you want the plot in code unit for distance (the default one is in physical unit)" )
31args = parser.parse_args()
32
33
34file_name=args.file_name
35IfXunitInCode=args.IfXunitInCode
36#dt=args.dt;
37
38
40f=open(file_name)
41print("Read in datafile: "+file_name)
42
43#skip the description line
44dat=f.readlines()[1:]
45
46#first find out the number of timesteps
47tstep=0
48told=1e6
49for line in dat:
50 tem=list(map(float,line.split(', ')))
51 tnew=tem[0]
52 if (told-tnew)<0:
53 break
54 else:
55 tstep+=1
56 told=tnew
57
58number_of_data=len(tem)-6
59
60print("number of data entries: "+str(number_of_data))
61print("total timesteps: "+str(tstep))
62
63#second, find out how many tracer recorded here.
64N_tracer=0
65ID1=-100; ID2=-100
66coors=[]
67for line in dat:
68 tem=list(map(float,line.split(', ')))
69 if (not tem[1]==ID1) or (not tem[2]==ID2) and (not tem[6]==0):
70 ID1=tem[1]; ID2=tem[2]
71 N_tracer+=1
72 coor=[tem[3],tem[4],tem[5]]
73 coors.append(coor)
74
75coors=np.array(coors)
76print("valid tracers: "+str(len(coors)))
77
78#create the real data set
79data_set=np.zeros((N_tracer,tstep,4+number_of_data))
80
81ID1=list(map(float,dat[0].split(', ')))[1]; ID2=list(map(float,dat[0].split(', ')))[2]
82t_count=0 #counting the tsteps
83N_count=0 #counting the tracer
84for line in dat:
85 tem=list(map(float,line.split(', ')))
86 if (tem[1]==ID1) and (tem[2]==ID2):
87 data_set[N_count][t_count][0]=tem[0]; #timestep
88 data_set[N_count][t_count][1]=tem[3]; data_set[N_count][t_count][2]=tem[4]; data_set[N_count][t_count][3]=tem[5] #coordinates
89 for i in range(number_of_data):
90 data_set[N_count][t_count][4+i]=tem[6+i];
91 t_count+=1
92 else:
93 if t_count<tstep:
94 for i in range(4+number_of_data): data_set[N_count][t_count][i]=data_set[N_count][t_count-1][i];
95 ID1=tem[1]; ID2=tem[2]
96 N_count+=1
97 t_count=0
98 data_set[N_count][t_count][0]=tem[0]; #timestep
99 data_set[N_count][t_count][1]=tem[3]; data_set[N_count][t_count][2]=tem[4]; data_set[N_count][t_count][3]=tem[5] #coordinates
100 for i in range(number_of_data):
101 data_set[N_count][t_count][4+i]=tem[6+i];
102 t_count+=1
103if t_count<tstep:
104 for i in range(4+number_of_data): data_set[N_count][t_count][i]=data_set[N_count][t_count-1][i];
105
106f.close()
107
108
109
110q_dir={"rho":4,"v_x":5,"v_y":6,"v_z":7,"E_t":8}
111
112t_id=100
113q_id=args.pq
114
115for t_id in [200,400,590]:
116#==========================================================
117# Read in data
118#==========================================================
119
120 t_r_id=tstep-t_id-1
121 t_code=data_set[0,t_r_id,0]
122 data_at_t=data_set[:,t_r_id,:]
123 sort_data=data_at_t[np.argsort(data_at_t[:,1])]
124
125 coor_data=sort_data[:,1]
126 if q_id=="P":
127 e_data = np.array(sort_data[:,q_dir["E_t"]])
128 m1_data = np.array(sort_data[:,q_dir["v_x"]])
129 m2_data = np.array(sort_data[:,q_dir["v_y"]])
130 m3_data = np.array(sort_data[:,q_dir["v_z"]])
131 rho_data = np.array(sort_data[:,q_dir["rho"]])
132 else:
133 q_data=sort_data[:,q_dir[q_id]]
134 if q_id in ["v_x","v_y","v_z"]:
135 rho_data=sort_data[:,q_dir["rho"]]
136 #print(sort_data)
137
138#==========================================================
139# start to calculate the real physical quantites
140#==========================================================
141
142 t_real=(0.1694*a_i**(-0.5)-t_code/11.8)**(-3)*10**15 #*h^(-1)
143 a_scale=0.0287*(0.1694*a_i**(-0.5)-t_code/11.8)**(-2) # when code time ~ 2*(a_i^(-0.5)-1), a~1
144 print("cosmological time:", t_real)
145 print("a_scale:", a_scale)
146
147 coor_data_physical=100*a_scale*coor_data
148
149 if q_id in ["v_x","v_y","v_z"]:
150 q_id_show="$"+q_id+"/(km/s)$"
151 q_data=np.array(q_data)/np.array(rho_data) #remove density from momentum
152 q_data=q_data*10**4/a_scale #H_0*L= 100h*100h^(-1) km/s/Mpc * Mpc = 1e4
153 v_hubble=100*(2.057*10**17/t_real)*coor_data_physical #hubble velocity here
154 q_data=q_data+v_hubble
155 elif q_id=="rho":
156 q_id_show=r"$\rho/\rho_c $"
157 q_data=q_data/(a_scale**3)
158 elif q_id=="P":
159 q_id_show="P/(Pa)"
160 q_data=(5/3-1)*(e_data-0.5*(m1_data**2+m2_data**2+m3_data**2)/rho_data)
161 q_data=q_data*10**(8+6)*rho_c0_value/(a_scale**5)
162 else:
163 q_id_show="$"+q_id+"/(Code Unit)"
164
165#==============================================================
166# now do the rescaling
167#==============================================================
168
169 if args.IfRescale:
170 if q_id=="rho":
171 q_data=q_data*rho_c0_value # *h^2 kg/m^3
172 rho_scaling=1/(6*pi*G*t_real**2) #h^2 kg/m^3
173 q_data=q_data/rho_scaling #actually it is equal to code density
174 q_id_show=r"$\rho/\rho_{ta}(t) $"
175 if q_id in ["v_x","v_y","v_z"]:
176 r_ta=a_scale*100*3.086*1e19*((3*M)/(4*pi*rho_c_ini))**(1/3)*(4/3/pi)**(8/9)*(t_real/6.505/1e12)**(8/9) # km. eq(31), RAMSES
177 v_scaling=r_ta/t_real
178 q_data=q_data/v_scaling
179 q_id_show=r"$u/u_{ta}(t) $"
180 print("scaling velocity (km/s):", v_scaling)
181 if q_id=="P":
182 rho_scaling=1/(6*pi*G*t_real**2)
183 r_ta=a_scale*100*3.086*1e19*((3*M)/(4*pi*rho_c_ini))**(1/3)*(4/3/pi)**(8/9)*(t_real/6.505/1e12)**(8/9) # eq(31), RAMSES
184 P_scaling=rho_scaling*r_ta**2*1e6/(t_real**2)
185 q_data=q_data/P_scaling
186 q_id_show=r"$P/P_{ta}(t) $"
187
188 if IfXunitInCode:
189 plt.plot(coor_data,q_data,label="t= "+str(t_code)+" a= "+str(a_scale))
190 else:
191 plt.plot(coor_data_physical,q_data,label="t= "+str(t_code)+" a= "+str(a_scale))
192
193#plt.title("code time="+str(t_code))
194plt.legend()
195if IfXunitInCode:
196 plt.xlabel("$x/(Code Unit)$")
197else:
198 plt.xlabel("$x/(h^{-1}Mpc)$")
199plt.ylabel(q_id_show)
200#plt.ylim([1.0475,1.0525])
201if args.Ifylog:
202 plt.yscale("log")
203
204plt.show()
205
206
207
208
209
210
211
212
213
file_len(fname)
Definition csv_plot.py:15