-
Notifications
You must be signed in to change notification settings - Fork 0
/
g2output.cpp
325 lines (264 loc) · 9.37 KB
/
g2output.cpp
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
313
314
315
316
317
318
319
320
321
322
323
324
325
/****************************
OUTPUT FILE
****************************/
/*
This header file contains all the functions for the output of the data. Note that the outputslice() function needs to be generalized for n-fields and right now only gives meaning full output for 1 field. The only information outputed is the value of the field for the slice, the times atwhich the slices were output and the info.dat file.
Copyright (2013):
Kenyon College
John T. Giblin, Jr
Tate Deskins and Hillary Child
Last Updated: 06.27.2013
*/
#include "g2header.h" //contains declerations for program functions.
//readable time variables
time_t tStart,tCurrent,tLast,tFinish; // Keep track of elapsed clock time
/*****************
output stuffs
*****************/
void outputfield(int first)//outputs the field values
{
static FILE *slicefield;
//static int fld,i,j,k;
static char name[5000];
sprintf(name,"./slices/slices_fields_%d.dat", first);
//sprintf(name,"slices_field%d_%d.dat", fld, first);
slicefield=fopen(name,"w");
#if field_outdim==3//outputs slice for 3dimensions
for(int i=0;i<N;i+=field_sliceskip){
for(int j=0;j<N;j+=field_sliceskip){
for(int k=0;k<N;k+=field_sliceskip){
for(fld=0;fld<nflds,fld++){
fprintf(slicefield,"%Le ", field[0][fld][i][j][k]);//,rescale_B*dfield[0][fld][i][j][k]);
fprintf(slicefield,"\n");
} //loop over fields
fprintf(slicefield,"\n");
} //loop over k
fprintf(slicefield,"\n");
}
}
#elif field_outdim==2//outputs slice for 2dimensions
for(int j=0;j<N;j+=field_sliceskip)
{
for(int k=0;k<N;k+=field_sliceskip)
{
for(int fld=0;fld<nflds;fld++)
{
fprintf(slicefield,"%Le ", field[0][fld][0][j][k]);
}
fprintf(slicefield, "%Le ", sqrt( pow(field[0][1][0][j][k],2)+pow(field[0][2][0][j][k],2)) ); //radial bit
fprintf(slicefield, "\n");
}
fprintf(slicefield, "\n");
}
#elif field_outdim==1//outputs slice for 1dimension
for(int k=0;k<N;k+=field_sliceskip)
{
for(int fld=0;fld<nflds;fld++)
{
fprintf(slicefield,"%Le ", field[0][fld][0][j][k]);
}
fprintf(slicefield, "\n");
}
#endif
fclose(slicefield);
}
void meansvars()//calculates the mean and variance of each field
{
char name_[5000];
static FILE *meansvarsout_;
int i, j, k, fld;
long double av,av_sq,var;
long double evecV[nflds][nflds], mdiagV[nflds], mass_sq[nflds];
sprintf(name_,"./slices/meansvariances.dat");
meansvarsout_=fopen(name_,"a");
fprintf(meansvarsout_,"%Lf",t);
for(fld=0;fld<nflds;fld++)
{
av=0.;
var=0.;
// Calculate field mean
LOOP
av += field[0][fld][i][j][k];
av = av/(long double)gridsize; // Convert sum to average
av_sq = av*av; // Calculate mean squared for finding variance
// Calculate variance
LOOP
var += field[0][fld][i][j][k]*field[0][fld][i][j][k] - av_sq;
var = var/(long double)gridsize; // Convert sum to variance
// Output means and variances to files
fprintf(meansvarsout_," %Le %Le",av,var);
// Check for instability. See if the field has grown exponentially and become non-numerical at any point.
if((av!=0. && av/av!=1.)) // These two separate checks between them work on all the compilers I've tested
{
printf("Unstable solution developed. Field %d not numerical at t=%Le\n",fld,t);
output_parameters();
fflush(meansvarsout_);
exit(1);
}
} // End of loop over fields
av=0;
var=0;
LOOP //average of radial component
av+=sqrt(pow(field[0][1][i][j][k],2)+pow(field[0][2][i][j][k],2)+pow(field[0][3][i][j][k],2)+pow(field[0][4][i][j][k],2) );
av = av/(long double)gridsize;
av_sq=av*av;
LOOP
var += pow(field[0][1][i][j][k],2)+pow(field[0][2][i][j][k],2)+pow(field[0][3][i][j][k],2) +pow(field[0][4][i][j][k],2) - av_sq;
var = var/(long double)gridsize;
fprintf(meansvarsout_," %Le %Le",av,var);
fprintf(meansvarsout_,"\n");
fclose(meansvarsout_);
}
int slicewaitf()
{
return (int) (endtime/dt/slicenumber);//calculates number of timesteps to wait for slicenumber slices by time endtime
}
void outputslice()//externally called function for outputing the data from the run
{
static FILE *slicetime;
static int first=0;
static int lastslice,lastspec;
static int slicewaitm;
static FILE *masstime;
static FILE *conservationtime;
if(first==0)
{
if(slicewait==0)//determins number of steps to wait between each slice output based off of g2parameters.h
slicewaitm=slicewaitf();
else
slicewaitm=slicewait;
lastslice=slicewaitm;
lastspec=specnumber;
}
lastslice++;
if(lastslice>=slicewaitm)//this statement delays the output every slicewait*dt
{
#if field_outdim!=0
outputfield(first);//outputs field slice
#endif
#if spec_output==1//outputs spectra if last spectra output was not too soon (see g2paramters.h)
lastspec++;
if (lastspec>=specnumber) {
specOut(first/specnumber);
lastspec=0;
}
#endif
#if var_output!=0
meansvars();//outputs mean and variance of fields //TODO move mass plotting to here so it doesn't have to only plot with spectra?
#endif
long double avPot=0;
long double avKin=0;
calcE0 ( &avKin, &avPot);
/*times file output */
//this routine outputs time, scalefactor, scalfactor derivative, hubble constant, and energy componets at each slice output
slicetime=fopen("./slices/slices_time.dat","a");
//slicetime=fopen("slices_time.dat","a");
fprintf(slicetime,"%Le %Le %Le %Le %Le %Le %Le %Le \n", t,edkin[0],edpot[0],edgrad[0],edrho[0], avKin, avPot,a[0] ); //some zeroes to not mess up mathematica notebooks
fclose(slicetime);
//static FILE *masstime;
masstime = fopen("./slices/mass_vs_time.dat", "a");
fprintf(masstime, "%Le %Le \n", t, Vrrcalc(0) );//mass_sq[1], mass_sq[2]);
fclose(masstime);
conservationtime = fopen("./slices/energy_conservation.dat", "a");
fprintf(conservationtime, "%Le %Le %Le %Le \n", t, edrho[0], 3*pow(a[0]/adot[0],-2), (edrho[0] - 3*pow(a[0]/adot[0],-2) )/(3*pow(a[0]/adot[0],-2) )); //t, total energy, energy expression in friedmann equation
fclose(conservationtime);
first++;
lastslice=0;
}
}
void output_parameters()//this creats info.dat which contains information about run parameters and statistics
{
static FILE *info;
static int first=1;
if(first) // At beginning of run output run parameters
{
char expanType[13];
#if expansion_type==1
sprintf(expanType,"a dot");
#elif expansion_type==2
sprintf(expanType,"a double dot");
#else
sprintf(expanType,"no");
#endif
info=fopen("info.txt","a");
fprintf(info,"--------------------------\n");
fprintf(info,"Model Specific Information\n");
fprintf(info,"--------------------------\n");
modelinfo(info);
fprintf(info,"\n--------------------------\n");
fprintf(info,"General Program Information\n");
fprintf(info,"-----------------------------\n");
fprintf(info,"Grid size=%d^3\n",N);
fprintf(info,"L=%Le\n",L);
fprintf(info,"dt=%Le, dx=%Le\n",dt,dx);
fprintf(info,"end time=%Le\n",endtime);
fprintf(info,"B=%Le\n",rescale_B);
fprintf(info, "\nUsing %s expansion\n",expanType);
fprintf(info, "\nUsing %d cores\n",tot_num_thrds);
fprintf(info, "%d momentum bins for spectra\n",((int)(sqrt(3.)*N/2+1)));
time(&tStart);
fprintf(info,"\nRun began at %s",ctime(&tStart)); // Output date in readable form
first=0;
}
else // If not at beginning, record elapsed time for run
{
time(&tFinish);
fprintf(info,"Run ended at %s",ctime(&tFinish)); // Output ending date
fprintf(info,"\nRun from t=%Le to t=%Le took ",starttime,t);
readable_time((int)(tFinish-tStart),info);
fprintf(info,"\n");
// fprintf(info, "\nFinal scale factor is %Le\n",a[0]);
}
fflush(info);
return;
}
// Convert a time in seconds to a more readable form and print the results
void readable_time(int tt, FILE *info)
{
int tminutes=60,thours=60*tminutes,tdays=24*thours;
if(tt==0)
{
fprintf(info,"less than 1 second\n");
return;
}
// Days
if(tt>tdays)
{
fprintf(info,"%d days",tt/tdays);
tt = tt%tdays;
if(tt>0)
fprintf(info,", ");
}
// Hours
if(tt>thours)
{
fprintf(info,"%d hours",tt/thours);
tt = tt%thours;
if(tt>0)
fprintf(info,", ");
}
// Minutes
if(tt>tminutes)
{
fprintf(info,"%d minutes",tt/tminutes);
tt = tt%tminutes;
if(tt>0)
fprintf(info,", ");
}
// Seconds
if(tt>0)
fprintf(info,"%d seconds",tt);
fprintf(info,"\n");
return;
}
void screenout()//this calculates the time ellapsed from last screen output before outputting current program time.
{
time(&tCurrent);
if(screentime==0)
return;
if(tCurrent-tLast>screentime)
{
printf("%Lf\n",t);
time(&tLast);
}
}