forked from duffell/Disco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.c
283 lines (249 loc) · 8.61 KB
/
snapshot.c
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
#include "paul.h"
double get_dV( double * , double * );
void prim2cons( double * , double * , double , double );
void cons2prim( double * , double * , double , double );
void snapshot( struct domain * theDomain , char * filestart ){
struct cell ** theCells = theDomain->theCells;
int Nt = theDomain->Nt;
int Np = theDomain->Np;
int * Nr = theDomain->Nr;
int Ng = theDomain->Ng;
double * t_jph = theDomain->t_jph;
double * p_kph = theDomain->p_kph;
int rank = theDomain->rank;
int size = theDomain->size;
int * dim_rank = theDomain->dim_rank;
int * dim_size = theDomain->dim_size;
if(rank==0) printf("Generating Snapshot...\n");
int j_min = 0;
int j_max = Nt;
int k_min = 0;
int k_max = Np;
if( dim_rank[0] != 0 ) j_min = NUM_G;
if( dim_rank[0] != dim_size[0]-1 ) j_max = Nt-NUM_G;
if( dim_rank[1] != 0 ) k_min = NUM_G;
if( dim_rank[1] != dim_size[1]-1 ) k_max = Np-NUM_G;
double Rmin = theCells[0][0].riph;
double Rmax = theCells[0][Nr[0]-1].riph;
int NUM_R = theDomain->theParList.Num_R;
int i,j,k,q;
double cons_1d_avg[NUM_R*NUM_Q];
double prim_1d_avg[NUM_R*NUM_Q];
for( i=0 ; i<NUM_R*NUM_Q ; ++i ){
cons_1d_avg[i] = 0.0;
prim_1d_avg[i] = 0.0;
}
for( j=j_min ; j<j_max ; ++j ){
double thp = t_jph[j];
double thm = t_jph[j-1];
for( k=k_min ; k<k_max ; ++k ){
double php = p_kph[k];
double phm = p_kph[k-1];
int jk = j+Nt*k;
for( i=1 ; i<Nr[jk] ; ++i ){
struct cell * c = theCells[jk]+i;
double rp = c->riph;
double rm = rp - c->dr;
double ip = (NUM_R)*(rp-Rmin)/(Rmax-Rmin);
double im = (NUM_R)*(rm-Rmin)/(Rmax-Rmin);
double xp[3] = {rp,thp,php};
double xm[3] = {rm,thm,phm};
double dV = get_dV( xp , xm );
double delta_i = ip-im;
int iip = (int) ip;
int iim = (int) im;
if(iim<0) iim=0;
if(iip<0) iip=0;
if(iim>NUM_R-1) iim=NUM_R-1;
if(iip>NUM_R-1) iip=NUM_R-1;
double dip = ip-(double)iip;
double dim = (double)(iim+1)-im;
if( iip == iim ) dip = delta_i;
int iCons;
for( iCons = iim ; iCons <= iip ; ++iCons ){
double frac = 1./delta_i;
if( iCons == iim ) frac = dim/delta_i;
if( iCons == iip ) frac = dip/delta_i;
int i2 = iCons;
if( i2 < 0 ) i2 = 0;
if( i2 > NUM_R-1 ) i2 = NUM_R-1;
for( q=0 ; q<NUM_Q ; ++q ){
cons_1d_avg[i2*NUM_Q + q] += frac*c->cons[q];
prim_1d_avg[i2*NUM_Q + q] += frac*c->prim[q]*dV;
}
}
}
}
}
MPI_Allreduce( MPI_IN_PLACE , cons_1d_avg , NUM_R*NUM_Q , MPI_DOUBLE , MPI_SUM , theDomain->theComm );
MPI_Allreduce( MPI_IN_PLACE , prim_1d_avg , NUM_R*NUM_Q , MPI_DOUBLE , MPI_SUM , theDomain->theComm );
double THETA_MIN = theDomain->theParList.thmin;
double THETA_MAX = theDomain->theParList.thmax;
double PHI_MAX = theDomain->theParList.phimax;
char fname_rad[256];
strcpy( fname_rad , filestart );
strcat( fname_rad , "_radial.dat" );
char fname_th0[256];
strcpy( fname_th0 , filestart );
strcat( fname_th0 , "_rad0.dat" );
char fname_th1[256];
strcpy( fname_th1 , filestart );
strcat( fname_th1 , "_rad1.dat" );
if( rank==0 ){
FILE * pFile_1d = fopen(fname_rad,"w");
for( i=0 ; i<NUM_R ; ++i ){
double P_out[NUM_Q];
double xxm = (double)i/(double)NUM_R;
double xxp = (double)(i+1)/(double)NUM_R;
double rm = xxm*(Rmax-Rmin)+Rmin;
double rp = xxp*(Rmax-Rmin)+Rmin;
double xp[3] = {rp,THETA_MAX,PHI_MAX};
double xm[3] = {rm,THETA_MIN,0.0};
double r = (2./3.)*(rp*rp*rp-rm*rm*rm)/(rp*rp-rm*rm);
double dV = get_dV( xp , xm );
cons2prim( &(cons_1d_avg[i*NUM_Q]) , P_out , r , dV );
fprintf(pFile_1d,"%e ",r);
for( q=0 ; q<NUM_Q ; ++q ){
fprintf(pFile_1d,"%e ",P_out[q]);
fprintf(pFile_1d,"%e ",prim_1d_avg[i*NUM_Q+q]/dV);
}
fprintf(pFile_1d,"\n");
}
fclose( pFile_1d );
pFile_1d = fopen(fname_th0,"w");
for( i=0 ; i<Nr[0] ; ++i ){
struct cell * c = theCells[0]+i;
fprintf(pFile_1d,"%e ",c->riph-.5*c->dr);
for( q=0 ; q<NUM_Q ; ++q ) fprintf(pFile_1d,"%e ",c->prim[q]);
fprintf(pFile_1d,"\n");
}
fclose( pFile_1d );
}
double thetaSlice = 0.05;
double thP = t_jph[Nt-1];
double thM = t_jph[-1];
double Dth = thP-thM;
double Mth = .5*(thP+thM);
double delta = fabs(thetaSlice-Mth)/Dth;
struct { double value ; int index ; } minbuf;
minbuf.value = delta;
minbuf.index = rank;
MPI_Allreduce( MPI_IN_PLACE , &minbuf , 1 , MPI_DOUBLE_INT , MPI_MINLOC , theDomain->theComm );
int thRank = minbuf.index;
if( rank==thRank ){
j=0;
while( j<Nt && t_jph[j-1]<thetaSlice ) ++j;
if( j>0 ) --j;
FILE * pFile_1d = fopen(fname_th1,"w");
for( i=0 ; i<Nr[j] ; ++i ){
struct cell * c = theCells[j]+i;
fprintf(pFile_1d,"%e ",c->riph-.5*c->dr);
for( q=0 ; q<NUM_Q ; ++q ) fprintf(pFile_1d,"%e ",c->prim[q]);
fprintf(pFile_1d,"\n");
}
fclose( pFile_1d );
}
//////////////////////////Stupid Ebins Bullshit
double maxgam = 0.0;
for( j=0 ; j<Nt ; ++j ){
for( i=0 ; i<Nr[j] ; ++i ){
double ur = theCells[j][i].prim[UU1];
double up = theCells[j][i].prim[UU2];
if( maxgam < ur ) maxgam = sqrt(ur*ur+up*up);
}
}
MPI_Allreduce( MPI_IN_PLACE , &maxgam , 1 , MPI_DOUBLE , MPI_MAX , theDomain->theComm );
int Ngam = 1000;
double Ebin[Ngam];
double Mtot = 0.0;
int n;
for( n=0 ; n<Ngam ; ++n ) Ebin[n] = 0.0;
int jmin = Ng;
int jmax = Nt-Ng;
if( dim_rank[0]==0 ) jmin = 0;
if( dim_rank[0]==dim_size[0]-1 ) jmax = Nt;
for( j=jmin ; j<jmax ; ++j ){
for( i=0 ; i<Nr[j] ; ++i ){
double ur = theCells[j][i].prim[UU1];
double up = theCells[j][i].prim[UU2];
double gam = sqrt(ur*ur+up*up);
double ndoub = (double)Ngam*gam/maxgam;
int nn = (int) ndoub;
if( nn > Ngam ) nn = Ngam;
if( nn < 0 ) nn = 0;
Ebin[nn] += theCells[j][i].cons[TAU];
Mtot += theCells[j][i].cons[DEN];
}
}
MPI_Allreduce( MPI_IN_PLACE , Ebin , Ngam , MPI_DOUBLE , MPI_SUM , theDomain->theComm );
MPI_Allreduce( MPI_IN_PLACE , &Mtot , 1 , MPI_DOUBLE , MPI_SUM , theDomain->theComm );
char fname_ebins[256];
strcpy( fname_ebins , filestart );
strcat( fname_ebins , "_ebins.dat" );
if( rank==0 ){
FILE * gamFile = fopen( fname_ebins ,"w");
for( n=Ngam-2 ; n>=0 ; --n ){
Ebin[n] += Ebin[n+1];
}
fprintf(gamFile,"# E/M = %e\n",Ebin[0]/Mtot);
for( n=0 ; n<Ngam ; ++n ){
double gam = ((double)n/(double)Ngam)*maxgam;
fprintf(gamFile,"%e %e\n",gam,Ebin[n]/Ebin[0]);
}
fclose(gamFile);
}
///////////////////////////////////////////////
char fname_theta[256];
strcpy( fname_theta , filestart );
strcat( fname_theta , "_theta.dat" );
FILE * thFile;
if(rank==0){
thFile = fopen( fname_theta ,"w");
fclose(thFile);
}
int nrk;
double Eth[Nt];
double XEth[Nt];
double g_max[Nt];
double r_max[Nt];
for( j=j_min ; j<j_max ; ++j ){
double maxgam = 0.0;
double maxr = 0.0;
Eth[j] = 0.0;
XEth[j] = 0.0;
for( i=0 ; i<Nr[j] ; ++i ){
struct cell * c = theCells[j]+i;
Eth[j] += c->cons[TAU];
if(NUM_N>0) XEth[j] += c->cons[TAU]*c->prim[NUM_C];
double ur = c->prim[UU1];
double up = c->prim[UU2];
double gam = sqrt(ur*ur+up*up);
if( maxgam < gam ){
maxgam = gam;
}
double r = theCells[j][i].riph;
if( maxr<r && gam>.1*maxgam ){
maxr = r;
}
}
g_max[j] = maxgam;
r_max[j] = maxr;
}
for( nrk=0 ; nrk<size ; ++nrk ){
if( rank==nrk ){
FILE * thFile = fopen( fname_theta ,"a");
double xp[3] = {1.0,0.0,p_kph[0]};
double xm[3] = {0.0,0.0,p_kph[-1]};
for( j=jmin ; j<j_max ; ++j ){
xp[1] = t_jph[j];
xm[1] = t_jph[j-1];
double dOmega = get_dV(xp,xm);
fprintf(thFile,"%e ",.5*(t_jph[j]+t_jph[j-1]));
fprintf(thFile,"%e %e %e %e",r_max[j],g_max[j],Eth[j]/dOmega,XEth[j]/dOmega);
fprintf(thFile,"\n");
}
fclose(thFile);
}
MPI_Barrier(theDomain->theComm);
}
}