-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsave.cpp
300 lines (280 loc) · 9.1 KB
/
save.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
#include "save.hpp"
#include <exception>
#include <iostream>
#include <string.h>
#include <cassert>
#include "physconst.h"
using namespace GadgetWriter;
using namespace std;
#define BUFFER 48
gadget_header generate_header(std::valarray<int64_t> & npart, double Omega, double OmegaBaryon, double OmegaNuPart, double OmegaLambda, double HubbleParam, double Box, double InitTime, double UnitMass_in_g, double UnitLength_in_cm, double UnitVelocity_in_cm_per_s, bool combined_neutrinos)
{
gadget_header header = {};
//No factor of h^2 because mass is in 10^10 M_sun/h
double scale = 3* HUBBLE* HUBBLE / (UnitMass_in_g / pow(UnitLength_in_cm, 3) * 8 * M_PI * GRAVITY) * pow(Box,3);
/*Set masses*/
for(int i = 0; i < N_TYPE; i++)
header.mass[i] = 0;
/*Don't forget to set masses correctly when the CDM actually incorporates other species*/
double OmegaCDM = Omega;
if(npart[BARYON_TYPE]) {
header.mass[BARYON_TYPE] = OmegaBaryon * scale / npart[BARYON_TYPE];
OmegaCDM -= OmegaBaryon;
}
if(npart[NEUTRINO_TYPE]){
header.mass[NEUTRINO_TYPE] = OmegaNuPart * scale / npart[NEUTRINO_TYPE];
OmegaCDM -=OmegaNuPart;
}
/*For the "edit the transfer function" neutrino simulation method, we would *not* want to do this.
* For true kspace neutrinos, we do. */
else if (!combined_neutrinos){
OmegaCDM-=OmegaNuPart;
}
if(npart[DM_TYPE])
header.mass[DM_TYPE] = OmegaCDM * scale / npart[DM_TYPE];
for(int i=0; i< N_TYPE; ++i){
header.NallHW[i] = ( npart[i] >> 32);
header.npartTotal[i] = npart[i] - ((uint64_t)header.NallHW[i] << 32);
header.npart[i] = npart[i];
}
header.time = InitTime;
header.redshift = 1.0 / InitTime - 1;
header.BoxSize = Box;
header.Omega0 = Omega;
header.OmegaB = OmegaBaryon;
header.OmegaLambda = OmegaLambda;
header.HubbleParam = HubbleParam;
/*Various flags; Most set by gadget later*/
header.flag_sfr = 0;
header.flag_feedback = 0;
header.flag_cooling = 0;
header.flag_stellarage = 0;
header.flag_metals = 0;
header.flag_entropy_instead_u=0;
header.flag_doubleprecision=0;
header.flag_ic_info=1;
header.lpt_scalingfactor=1;
header.UnitLength_in_cm = UnitLength_in_cm;
header.UnitMass_in_g = UnitMass_in_g;
header.UnitVelocity_in_cm_per_s = UnitVelocity_in_cm_per_s;
return header;
}
template <typename T> class BufferedWrite
{
public:
BufferedWrite(GWriteBaseSnap& snap, int64_t NumPart, int ItemsPart, const std::string groupstring) :
snap(snap), NumPart(NumPart), ItemsPart(ItemsPart), groupstring(groupstring), blockmaxlen(BUFFER * 1024 * 1024)
{
if(!(block = (T *) malloc(blockmaxlen*ItemsPart*sizeof(T))))
throw std::ios_base::failure("Failed to allocate "+std::to_string(ItemsPart*sizeof(float)*blockmaxlen/1024/1024)+" MB for write buffer");
}
int64_t do_write(int type, void * data, int64_t np_write, int64_t begin)
{
int64_t retval;
try{
retval = dynamic_cast<GWriteSnap&>(snap).WriteBlocks(groupstring, type, data, np_write, begin);
} catch (const std::bad_cast & e) {
#ifdef HAVE_BGFL
try {
retval = dynamic_cast<GWriteBigSnap&>(snap).WriteBlocks(groupstring, type, data, np_write, begin, dtype().c_str(), ItemsPart);
} catch(const std::bad_cast & e) {
#endif
std::cout << e.what() << '\n';
exit(1);
#ifdef HAVE_BGFL
}
#endif
}
return retval;
}
int64_t writeparticles(int type)
{
int64_t written=0, pc = 0;
for(int64_t i = 0; i < NumPart; i++){
for(int64_t k = 0; k < ItemsPart; k++){
assert(ItemsPart*pc + k < blockmaxlen*ItemsPart);
block[ItemsPart * pc + k] = setter(i,k,type);
}
pc++;
if(pc >= blockmaxlen){
if(do_write(type, block, pc,written) != pc)
throw std::ios_base::failure("Could not write data at particle "+std::to_string(i));
written+=pc;
pc = 0;
}
}
if(pc > 0 && do_write(type, block, pc,written) != pc)
throw std::ios_base::failure("Could not write final data for: "+groupstring);
return written;
}
protected:
virtual T setter(size_t i, int k, int type) = 0;
virtual ~BufferedWrite()
{
free(block);
}
private:
std::string dtype(void) {
throw std::runtime_error("Need to specialise dtype for class");
}
T * block;
GWriteBaseSnap& snap;
const int64_t NumPart;
const int64_t ItemsPart;
const std::string groupstring;
const int64_t blockmaxlen;
};
template <> std::string BufferedWrite<float>::dtype(void) {
return "f4";
};
template <> std::string BufferedWrite<double>::dtype(void) {
return "f8";
};
template <> std::string BufferedWrite<id_type>::dtype(void) {
return "i"+std::to_string(sizeof(id_type));
};
class PosBufferedWrite : public BufferedWrite<float>
{
public:
PosBufferedWrite(GWriteBaseSnap& snap, int64_t NumPart, lpt_data * outdata, part_grid & Pgrid) :
BufferedWrite(snap, NumPart, 3, name(snap.GetFormat())),
Pgrid(Pgrid), outdata(outdata)
{}
private:
std::string name(int format)
{
switch(format)
{
case 3:
return "Coordinates";
case 4:
return "Position";
default:
return "POS ";
}
}
virtual float setter(size_t i, int k, int type)
{
double value = Pgrid.Pos(i,k, type) + Pgrid.get_shift(type);
if(outdata)
value += outdata->GetDisp(i,k);
value = periodic_wrap(value, Pgrid.GetBox());
return value;
}
part_grid & Pgrid;
lpt_data * outdata;
};
class VelBufferedWrite : public BufferedWrite<float>
{
public:
VelBufferedWrite(GWriteBaseSnap& snap, int64_t NumPart, FermiDiracVel * therm_vels, lpt_data * outdata) :
BufferedWrite(snap, NumPart, 3, name(snap.GetFormat())),
therm_vels(therm_vels), outdata(outdata),vtherm(0.,3)
{
}
private:
std::string name(int format)
{
switch(format)
{
case 3:
return "Velocities";
case 4:
return "Velocity";
default:
return "VEL ";
}
}
virtual float setter(size_t i, int k, int type)
{
if(k == 0)
get_new_therm_vels();
assert(k >= 0 || k <= 2);
float value = vtherm[k];
if(outdata)
value += outdata->GetVel(i,k);
return value;
}
void get_new_therm_vels()
{
if(!therm_vels)
return;
vtherm = therm_vels->get_thermal_speeds();
}
FermiDiracVel *therm_vels;
lpt_data * outdata;
std::valarray<float> vtherm;
};
class IDBufferedWrite : public BufferedWrite<id_type>
{
public:
IDBufferedWrite(GWriteBaseSnap& snap, int64_t NumPart, int64_t FirstId) :
BufferedWrite(snap, NumPart, 1, name(snap.GetFormat())),
FirstId(FirstId)
{
}
private:
std::string name(int format)
{
switch(format)
{
case 3:
return "ParticleIDs";
case 4:
return "ID";
default:
return "ID ";
}
}
virtual id_type setter(size_t i, int k, int type)
{
return i + FirstId;
}
const int64_t FirstId;
};
/*Class to write zero energies*/
class EnergyBufferedWrite : public BufferedWrite<float>
{
public:
EnergyBufferedWrite(GWriteBaseSnap& snap, int64_t NumPart) :
BufferedWrite(snap, NumPart, 1, snap.GetFormat() > 2 ? "InternalEnergy" : "U ")
{}
private:
virtual float setter(size_t i, int k, int type)
{
return 0;
}
};
int64_t write_particle_data(GWriteBaseSnap& snap, int type, lpt_data * outdata, part_grid& Pgrid, FermiDiracVel *therm_vels, int64_t FirstId)
{
const int64_t NPcbrt = Pgrid.GetNumPart(type);
const int64_t NumPart = NPcbrt*NPcbrt*NPcbrt;
printf("\nWriting IC-file\n");
{
PosBufferedWrite pp(snap, NumPart, outdata, Pgrid);
pp.writeparticles(type);
}
{
VelBufferedWrite pp(snap, NumPart, therm_vels, outdata);
pp.writeparticles(type);
}
{
IDBufferedWrite pp(snap, NumPart, FirstId);
pp.writeparticles(type);
}
if(type == BARYON_TYPE)
{
EnergyBufferedWrite pp(snap, NumPart);
pp.writeparticles(type);
}
printf("Finished writing IC file.\n");
FirstId+=NumPart;
return FirstId;
}
double periodic_wrap(double x, double box)
{
x = fmod(x, box);
if (x < 0)
x += box;
return x;
}