-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathio.cpp
More file actions
212 lines (201 loc) · 6.14 KB
/
io.cpp
File metadata and controls
212 lines (201 loc) · 6.14 KB
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
#include "io.h"
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while(getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
return split(s, delim, elems);
}
int filename_gen(char *f_txt, char*f_distr, char*f_mass, char*f_star, char*f_Ek, char*cp, char*filefrag, char*filecoag)
{
int i;
// printf("please input the number of the txt file:\n");
start2 = clock();
//scanf("%s", f_txt);
end2 = clock();
strcpy(f_distr, f_txt);
strcpy(f_mass, f_txt);
strcpy(f_star, f_txt);
strcpy(cp,f_txt);
strcpy(f_Ek,f_txt);
strcpy(filefrag, f_txt);
strcpy(filecoag, f_txt);
i = 0;
do
{
i++;
}
while(f_distr[i] != '.');
do
{
f_distr[i] = '\0';
f_mass[i] = '\0';
f_star[i] = '\0';
f_Ek[i] = '\0';
cp[i] = '\0';
filefrag[i] = '\0';
filecoag[i] = '\0';
i++;
}
while(f_distr[i] != '\0' || f_mass[i] != '\0');
strcat(f_distr, "-distr_r.txt");
strcat(f_mass, "-massd.txt");
strcat(f_star, "-star.txt");
strcat(f_Ek, "-distr_Ek40.txt");
strcat(cp, "-cp.txt");
strcat(filefrag, "-frag.txt");
strcat(filecoag, "-coag.txt");
printf("outputfiles will be %s %s %s %s %s %s %s %s\n",f_txt,f_distr,f_mass,f_star,f_Ek,cp,filefrag,filecoag);
return 0;
}
int print_paras(FILE *fp, double back_mass, double TimeContinue, double total_mass_core,char * f_rst)
{
//float al = alpha, be = beta, q_be = q_beta, sf = SF_rate * myr, g1 = Gauss1, g2 = Gauss2, fif = fifa;
float BEmass = mBE, rb = Rb/pc, bm = (double)back_mass/Msolar, timestep = Timestep / yr, eva = Evap_rate*100*kyr,drho = Drho, rhoc1=rhob*Drho*1e16;
if(TimeContinue)
{
fprintf(fp,"this continues file %s\ninitial time is: %fmyr\n", f_rst, TimeContinue/myr);
}
fprintf(fp, "bin: %d\nalpha: %f\nbeta: %f\nSF_rate: %f /myr\nGaussian sigma1: %f\nGaussian sigema2: %f\nfilling factor: %f\n",bin, alpha_sf, beta, SF_rate*myr, Gauss1, Gauss2, (double)total_mass_core/back_mass/drho);
//printf("BEmass=%f\n",BEmass/Msolar);
fprintf(fp, "Rb: %f pc\nBackground mass: %f Msolar\ntimestep: %f yr\nevaporation rate: %f%% per kyr\nrhoc: %fe-16\nDensity contrast: %f\n", rb, bm, timestep, eva, rhoc1, drho);
fprintf(fp, "v_m index: %f\n", v_m);
fprintf(fp, "Bonnor-Ebert mass: %f Msolar\n", BEmass/Msolar);
fprintf(fp, "dn/dq = q^q_beta, q_beta=%f\n", q_beta);
fprintf(fp, "\ncondensation kappa=%f\n", kappa);
fprintf(fp, "\nvelocity dispersion=%f\n", vc);
return 0;
}
int readdata(double *mytime, double *TimeContinue, int *ff, int *ff2, char *f_rst)
{
int i;
// char y[25], z[25];
// FILE *fpdata;
string line;
vector <string> data;
ifstream fpdata;
strcat(f_rst,"-cp.txt");
fpdata.open(f_rst);
getline(fpdata,line);
data = split(line,'=');
TimeContinue[0] = atof(data[1].c_str());
mytime[0] = TimeContinue[0];
getline(fpdata,line);
data = split(line,'=');
star_mass = atof(data[1].c_str());
getline(fpdata,line);
data = split(line,'=');
back_mass = atof(data[1].c_str());
getline(fpdata,line);
data = split(line,'=');
ghost_mass = atof(data[1].c_str());
printf("read from %s, with TimeContinue = %lf, star_mass = %lf, back_mass = %lf,ghost_mass=%lf\n", f_rst, *TimeContinue, star_mass, back_mass, ghost_mass);
getline(fpdata,line);
getline(fpdata,line);
for(i = 0; i<100; i++)
{
getline(fpdata,line);
data = split(line,' ');
n[i] = atof(data[1].c_str());
v_d[i] = atof(data[2].c_str());
n_star[i] = atof(data[3].c_str());
// fscanf(fpdata, "%lf", n+i);
// fscanf(fpdata, "%lf", v_d+i);
// fscanf(fpdata, "%lf", n_star);
}
getline(fpdata,line);
data = split(line,' ');
*ff = atoi(data[0].c_str());
getline(fpdata,line);
data = split(line,' ');
*ff2 = atoi(data[0].c_str());
// cout << *ff << *ff2 << endl;
// fscanf(fpdata, "%d\n%d", ff, ff2);
fpdata.close();
return 0;
}
int dataoutput2(double result[15][100], double result_star[15][100], double Ek_evo[15][100], int countcolumn, char*f_distr, char *f_star, char*f_Ek, int countcolumncf, char *filefrag, char *filecoag, double frag[15][100], double coag[15][100], double TimeContinue)
{
int i = 0, j = 0;
FILE* fptemp, *fpfrag, *fpcoag, *fpEk, *fpstar;
fptemp = fopen(f_distr,"w");
fpEk = fopen(f_Ek, "w");
fpstar = fopen(f_star, "w");
fpfrag = fopen(filefrag, "w");
fpcoag = fopen(filecoag, "w");
for(i = 0; i < 100; i++) /* 100 is the total number of bins*/
{
if(TimeContinue==0)
{
fprintf(fptemp,"%f\t",m_insolar[i]);
fprintf(fpEk,"%f\t",m_insolar[i]);
fprintf(fpstar,"%f\t",m_insolar[i]);
fprintf(fpfrag,"%f\t",m_insolar[i]);
fprintf(fpcoag,"%f\t",m_insolar[i]);
}
for(j = 0; j < countcolumn; j++)
{
fprintf(fptemp, "%f\t", result[j][i]);
fprintf(fpfrag, "%f\t", frag[j][i]);
fprintf(fpcoag, "%f\t", coag[j][i]);
fprintf(fpEk, "%f\t", Ek_evo[j][i]/pow(10,40));
fprintf(fpstar, "%f\t", result_star[j][i]);
}
fprintf(fptemp, "\n");
fprintf(fpfrag, "\n");
fprintf(fpcoag, "\n");
fprintf(fpEk, "\n");
fprintf(fpstar, "\n");
// for(j = 0; j < countcolumncf - 1; j++)
// {
//!! fprintf(fpfrag, "%f\t", frag[j][i]);
//!! fprintf(fpcoag, "%f\t", coag[j][i]);
// }
//!! fprintf(fpfrag, "%f\n", frag[j][i]);
//!! fprintf(fpcoag, "%f\n", coag[j][i]);
}
fclose(fpEk);
fclose(fptemp);
fclose(fpstar);
fclose(fpfrag);
fclose(fpcoag);
return 1;
}
int finalize(double mytime, FILE *fp, char *cp, int ff, int ff2)
{
int i;
double time1;
time1 = (double)(mytime/myr);
fprintf(fp, "%lf myr\n", time1);
fprintf(fp, "cost time: %fs\n", (double)(finish - start + start2 - end2)/CLOCKS_PER_SEC);
fclose(fp);
FILE *fcp;
fcp = fopen(cp,"w");
fprintf(fcp, "#mytime = %f\n", mytime);
fprintf(fcp, "#star_mass = %f\n", star_mass);
fprintf(fcp, "#back_mass = %f\n", back_mass);
fprintf(fcp, "#ghost_mass = %f\n", ghost_mass);
fprintf(fcp, "#Nbin N VD NSTAR\n", ghost_mass);
fprintf(fcp, "#[1] [2] [3] [4]\n", ghost_mass);
for(i = 0; i<100; i++)
{
fprintf(fcp, "%d %f %f %f\n", i,n[i],v_d[i],n_star[i]);
}
/*fprintf(fcp, "\n");
*for(i = 0; i<100; i++)
*{
* fprintf(fcp, "%f\n", v_d[i]);
*}
*for(i = 0; i<100; i++)
*{
* fprintf(fcp, "%f\n", n_star[i]);
*}*/
fprintf(fcp, "%d\n%d\n", ff, ff2);
fclose(fcp);
return 1;
};