-
Notifications
You must be signed in to change notification settings - Fork 29
/
InOutput.cpp
2484 lines (2298 loc) · 79.2 KB
/
InOutput.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// IO routines
#ifdef HAVE_VALUES_H
#include <values.h>
#else
#include <float.h>
#endif
#include "ParallelGravity.h"
#include "DataManager.h"
#include "TipsyFile.h"
#include "OrientedBox.h"
#include "Reductions.h"
#include "InOutput.h"
#include "ckio.h"
#include <errno.h>
#include <float.h>
using namespace TypeHandling;
using namespace SFC;
using namespace std;
template <typename TPos, typename TVel>
void load_tipsy_gas(Tipsy::TipsyReader &r, GravityParticle &p, double dTuFac)
{
Tipsy::gas_particle_t<TPos, TVel> gp;
CkMustAssert(r.getNextGasParticle_t(gp), "failed to read gas particle!");
p.mass = gp.mass;
p.position = gp.pos;
p.velocity = gp.vel;
p.soft = gp.hsmooth;
#ifdef CHANGESOFT
p.fSoft0 = gp.hsmooth;
#endif
p.iType = TYPE_GAS;
p.fDensity = gp.rho;
p.fMetals() = gp.metals;
// O and Fe ratio based on Asplund et al 2009
p.fMFracOxygen() = 0.43*gp.metals;
p.fMFracIron() = 0.098*gp.metals;
#ifdef DIFFUSION
p.fMetalsPred() = gp.metals;
p.fMFracOxygenPred() = 0.43*gp.metals;
p.fMFracIronPred() = 0.098*gp.metals;
#endif
p.u() = dTuFac*gp.temp;
p.uPred() = dTuFac*gp.temp;
// Initial estimate of sound speed.
double gamma = GAMMA_NONCOOL;
double gammam1 = gamma - 1.0;
p.c() = sqrt(gamma*gammam1*p.uPred());
p.vPred() = gp.vel;
p.fBallMax() = FLT_MAX; // N.B. don't use DOUBLE_MAX here:
// fBallMax*fBallMax should not overflow.
p.fESNrate() = 0.0;
p.fTimeCoolIsOffUntil() = 0.0;
p.dTimeFB() = 0.0;
#ifdef NEED_DT
p.dt = FLT_MAX;
#endif
#ifdef DTADJUST
p.dtNew() = FLT_MAX;
#ifndef COOLING_NONE
p.uDot() = 0.0; // Used in initial timestep
#endif
p.PdV() = 0.0; // Used in initial timestep
#endif
#ifdef SUPERBUBBLE
p.cpHotInit() = 0;
p.uHot() = 0.0;
p.uHotPred() = 0.0;
p.uHotDot() = 0.0;
p.massHot() = 0.0;
p.fThermalCond() = 0.0;
p.fPromoteSum() = 0.0;
p.fPromoteSumuPred() = 0.0;
p.fPromoteuPredInit() = 0.0;
#endif
}
template <typename TPos, typename TVel>
void load_tipsy_dark(Tipsy::TipsyReader &r, GravityParticle &p)
{
Tipsy::dark_particle_t<TPos, TVel> dp;
CkMustAssert(r.getNextDarkParticle_t(dp), "failed to read dark particle!");
p.mass = dp.mass;
p.position = dp.pos;
p.velocity = dp.vel;
p.soft = dp.eps;
#ifdef CHANGESOFT
p.fSoft0 = dp.eps;
#endif
p.fDensity = 0.0;
p.iType = TYPE_DARK;
#ifdef COLLISION
p.dtKep = 0;
p.vPred() = dp.vel;
p.dtCol = DBL_MAX;
p.iOrderCol = -1;
p.w = Vector3D<double>(0.);
#endif
}
template <typename TPos, typename TVel>
void load_tipsy_star(Tipsy::TipsyReader &r, GravityParticle &p)
{
Tipsy::star_particle_t<TPos, TVel> sp;
CkMustAssert(r.getNextStarParticle_t(sp), "failed to read star particle!");
p.mass = sp.mass;
p.position = sp.pos;
p.velocity = sp.vel;
p.soft = sp.eps;
#ifdef CHANGESOFT
p.fSoft0 = sp.eps;
#endif
#ifdef COLLISION
p.dtCol = DBL_MAX;
p.iOrderCol = -1;
p.w = Vector3D<double>(0.);
#endif
p.fDensity = 0.0;
p.iType = TYPE_STAR;
p.fStarMetals() = sp.metals;
// Metals to O and Fe based on Asplund et al 2009
p.fStarMFracOxygen() = 0.43*sp.metals;
p.fStarMFracIron() = 0.098*sp.metals;
p.fMassForm() = sp.mass;
p.fTimeForm() = sp.tform;
p.iGasOrder() = -1;
#ifdef COOLING_MOLECULARH
p.dStarLymanWerner() = 0.0;
#endif
}
/// @brief determine if we are on a TreePiece that will read
/// particles.
/// @param iIndex thisIndex of my TreePiece
/// @param nPieces Total number of TreePieces.
/// @return True if we are on a loading PE, false otherwise
///
/// We only want one TreePiece per PE to open and read the tipsy
/// file. This saves file opens/closes and reduces the number of
/// messages needed for the initial domain decomposition.
static bool isLoadingPiece(int iIndex, int nPieces)
{
bool bLoading = true; ///< return value
int nLoadingPEs = CkNumPes();
#ifdef ROUND_ROBIN_WITH_OCT_DECOMP
/// In this case, the pieces have been assigned to PEs in round-robin.
if(iIndex >= nLoadingPEs) bLoading = false;
#else
/// The following is based on the DefaultMap procNum(). See
/// cklocation.C:compute_binsize() in the charm++ source code.
/// The first (nPiece % nPE) PEs have one more Piece than the rest.
int nPiecesPerPE = nPieces/nLoadingPEs;
int nRem = nPieces - nPiecesPerPE*nLoadingPEs;
int nSmallBlockSize = nPiecesPerPE;
int nLargeBlockSize = nPiecesPerPE + 1;
int nLargeBlocks = nRem;
int iLargeBlockBound = nLargeBlocks * nLargeBlockSize;
if (iIndex < iLargeBlockBound) {
if (iIndex % nLargeBlockSize > 0) {
bLoading = false; // not the first piece in a large block
}
}
else {
if ((iIndex - iLargeBlockBound) % nSmallBlockSize > 0) {
bLoading = false; // not the first piece in a small block
}
}
#endif
return bLoading;
}
void TreePiece::loadTipsy(const std::string& filename,
const double dTuFac, // Convert Temperature
const bool bDoublePos,
const bool bDoubleVel,
const CkCallback& cb) {
LBTurnInstrumentOff();
basefilename = filename;
Tipsy::TipsyReader r(filename, bDoublePos, bDoubleVel);
if(!r.status()) {
cerr << thisIndex << ": TreePiece: Fatal: Couldn't open tipsy file!" << endl;
cb.send(0); // Fire off callback
return;
}
Tipsy::header tipsyHeader = r.getHeader();
nTotalParticles = tipsyHeader.nbodies;
nTotalSPH = tipsyHeader.nsph;
nTotalDark = tipsyHeader.ndark;
nTotalStar = tipsyHeader.nstar;
dStartTime = tipsyHeader.time;
bool skipLoad = !isLoadingPiece(thisIndex, numTreePieces);
if(skipLoad){
myNumParticles = 0;
nStartRead = -1;
contribute(cb);
return;
}
// find your load offset into input file
int myIndex = CkMyPe();
int numLoadingPEs = CkNumPes();
myNumParticles = nTotalParticles / numLoadingPEs;
int excess = nTotalParticles % numLoadingPEs;
int64_t startParticle = ((int64_t) myNumParticles) * myIndex;
if(myIndex < excess) {
myNumParticles++;
startParticle += myIndex;
}
else {
startParticle += excess;
}
if(startParticle >= nTotalParticles) {
CkError("Bad startParticle: %ld, nPart: %ld, myIndex: %d, nLoading: %d\n",
startParticle, nTotalParticles, myIndex, numLoadingPEs);
}
CkAssert(startParticle < nTotalParticles);
nStartRead = startParticle;
if(verbosity > 2)
cerr << "TreePiece " << thisIndex << " PE " << CkMyPe() << " Taking " << myNumParticles
<< " of " << nTotalParticles
<< " particles: [" << startParticle << "," << startParticle+myNumParticles << ")" << endl;
// allocate an array for myParticles
nStore = (int)((myNumParticles + 2)*(1.0 + dExtraStore));
myParticles = new GravityParticle[nStore];
// Are we loading SPH?
if(startParticle < nTotalSPH) {
if(startParticle + myNumParticles <= nTotalSPH)
myNumSPH = myNumParticles;
else
myNumSPH = nTotalSPH - startParticle;
}
else {
myNumSPH = 0;
}
nStoreSPH = (int)(myNumSPH*(1.0 + dExtraStore));
if(nStoreSPH > 0)
mySPHParticles = new extraSPHData[nStoreSPH];
// Are we loading stars?
if(startParticle + myNumParticles > nTotalSPH + nTotalDark) {
if(startParticle <= nTotalSPH + nTotalDark)
myNumStar = startParticle + myNumParticles
- (nTotalSPH + nTotalDark);
else
myNumStar = myNumParticles;
}
else {
myNumStar = 0;
}
allocateStars();
CkMustAssert(r.seekParticleNum(startParticle), "Couldn't seek to my particles!");
Tipsy::gas_particle gp;
Tipsy::dark_particle dp;
Tipsy::star_particle sp;
int iSPH = 0;
int iStar = 0;
for(unsigned int i = 0; i < myNumParticles; ++i) {
if(i + startParticle < (unsigned int) tipsyHeader.nsph) {
myParticles[i+1].extraData = &mySPHParticles[iSPH];
if(!bDoublePos)
load_tipsy_gas<float,float>(r, myParticles[i+1],
dTuFac) ;
else if(!bDoubleVel)
load_tipsy_gas<double,float>(r, myParticles[i+1],
dTuFac) ;
else
load_tipsy_gas<double,double>(r, myParticles[i+1],
dTuFac) ;
iSPH++;
} else if(i + startParticle < (unsigned int) tipsyHeader.nsph
+ tipsyHeader.ndark) {
if(!bDoublePos)
load_tipsy_dark<float,float>(r, myParticles[i+1]);
else if(!bDoubleVel)
load_tipsy_dark<double,float>(r, myParticles[i+1]);
else
load_tipsy_dark<double,double>(r, myParticles[i+1]);
} else {
myParticles[i+1].extraData = &myStarParticles[iStar];
if(!bDoublePos)
load_tipsy_star<float,float>(r, myParticles[i+1]);
else if(!bDoubleVel)
load_tipsy_star<double,float>(r, myParticles[i+1]);
else
load_tipsy_star<double,double>(r, myParticles[i+1]);
iStar++;
}
// File corruption checks
CkAssert(myParticles[i+1].mass >= 0.0);
CkAssert(myParticles[i+1].soft >= 0.0);
#ifdef SIDMINTERACT
myParticles[i+1].iNSIDMInteractions = 0;
#endif
myParticles[i+1].rung = 0;
myParticles[i+1].fBall = 0.0;
myParticles[i+1].iOrder = i + startParticle;
#ifdef SPLITGAS
if(myParticles[i+1].iOrder < tipsyHeader.nsph) myParticles[i+1].iSplitOrder() = i + startParticle;
if(myParticles[i+1].iOrder >= tipsyHeader.nsph) myParticles[i+1].iOrder += tipsyHeader.nsph;
#endif
#if COSMO_STATS > 1
myParticles[i+1].intcellmass = 0;
myParticles[i+1].intpartmass = 0;
myParticles[i+1].extcellmass = 0;
myParticles[i+1].extpartmass = 0;
#endif
#if COSMO_STATS > 0
piecemass += myParticles[i+1].mass;
#endif
boundingBox.grow(myParticles[i+1].position);
}
myParticles[0].key = firstPossibleKey;
myParticles[myNumParticles+1].key = lastPossibleKey;
contribute(cb);
}
/// @brief return maximum iOrders.
/// Like the above, but the file has already been read
void TreePiece::getMaxIOrds(const CkCallback& cb)
{
CmiInt8 nMaxOrd[3] = {0, 0, 0}; // 0 -> gas, 1 -> dark, 2 -> all
for(int i = 0; i < myNumParticles; i++) {
int64_t dummy;
dummy = myParticles[i+1].iOrder;
if(dummy > nMaxOrd[0] && myParticles[i+1].isGas())
nMaxOrd[0] = dummy;
if(dummy > nMaxOrd[1] && myParticles[i+1].isDark())
nMaxOrd[1] = dummy;
if(dummy > nMaxOrd[2])
nMaxOrd[2] = dummy;
}
contribute(3*sizeof(CmiInt8), nMaxOrd, CkReduction::max_long, cb);
}
void TreePiece::readTipsyArray(OutputParams& params, const CkCallback& cb)
{
params.dm = dm; // pass cooling information
FILE *infile = CmiFopen((params.fileName+"." + params.sTipsyExt).c_str(),
"r+");
if(infile == NULL)
CkError("Bad open of %s.%s: %s\n", params.fileName.c_str(),
params.sTipsyExt.c_str(), strerror(errno));
CkMustAssert(infile != NULL, "Cannot open tipsy array file\n");
// Check if its a binary file
unsigned int iDum;
XDR xdrs;
xdrstdio_create(&xdrs, infile, XDR_DECODE);
xdr_u_int(&xdrs,&iDum);
xdr_destroy(&xdrs);
if(iDum == nTotalParticles) { // We've got a binary file; read it
int64_t seek_pos;
if(params.bVector)
seek_pos = sizeof(iDum) + nStartRead*(int64_t)sizeof(float)*3;
else
seek_pos = sizeof(iDum) + nStartRead*(int64_t)sizeof(float);
fseek(infile, seek_pos, SEEK_SET);
xdrstdio_create(&xdrs, infile, XDR_DECODE);
for(unsigned int i = 0; i < myNumParticles; ++i) {
if(params.bFloat) {
if(params.bVector) {
Vector3D<float> vValue;
xdr_template(&xdrs, &vValue);
params.setVValue(&myParticles[i+1], vValue);
}
else {
float dValue;
xdr_float(&xdrs, &dValue);
params.setDValue(&myParticles[i+1], dValue);
}
}
else {
int iValue;
xdr_template(&xdrs, &iValue);
params.setIValue(&myParticles[i+1], iValue);
}
}
xdr_destroy(&xdrs);
}
else { // assume we've got an ASCII file
int64_t nTot;
fseek(infile, 0, SEEK_SET);
int nread;
nread = fscanf(infile, "%ld\n", &nTot);
CkAssert(nread == 1);
int nDim = 1; // Dimensions to read
if(params.bVector) {
nDim = 3;
CkAssert(packed == 0);
}
for(int iDim = 0; iDim < nDim; iDim++) {
for(int i = 0; i < nStartRead; i++) {
double dummy;
nread = fscanf(infile, "%lf\n", &dummy);
CkAssert(nread == 1);
}
for(int i = 0; i < myNumParticles; i++) {
if(params.bFloat) {
double dDummy;
nread = fscanf(infile, "%lf\n", &dDummy);
CkAssert(nread == 1);
if(params.bVector) {
Vector3D<double> vDummy
= params.vValue(&myParticles[i+1]);
vDummy[iDim] = dDummy;
params.setVValue(&myParticles[i+1], vDummy);
}
else
params.setDValue(&myParticles[i+1], dDummy);
}
else {
int64_t iDummy;
nread = fscanf(infile, "%ld\n", &iDummy);
CkAssert(nread == 1);
params.setIValue(&myParticles[i+1], iDummy);
}
}
}
}
CmiFclose(infile);
contribute(cb);
}
static double fh_time; // gross, but quick way to get time
/// Returns total number of particles in a given file
/// @param filename data file of interest
int64_t ncGetCount(std::string filename)
{
FILE* infile = CmiFopen(filename.c_str(), "rb");
if(!infile) {
return 0; // Assume there is none of this particle type
}
XDR xdrs;
FieldHeader fh;
xdrstdio_create(&xdrs, infile, XDR_DECODE);
if(!xdr_template(&xdrs, &fh)) {
throw XDRException("Couldn't read header from file!");
}
if(fh.magic != FieldHeader::MagicNumber) {
throw XDRException("This file does not appear to be a field file (magic number doesn't match).");
}
if(fh.dimensions != 3 && fh.dimensions != 1) {
throw XDRException("Wrong dimension.");
}
fh_time = fh.time;
xdr_destroy(&xdrs);
fclose(infile);
return fh.numParticles;
}
static void *readFieldData(const std::string filename, FieldHeader &fh, unsigned int dim,
int64_t numParticles, int64_t startParticle)
{
FILE* infile = CmiFopen(filename.c_str(), "rb");
if(!infile) {
string smess("Couldn't open field file: ");
smess += filename;
throw XDRException(smess);
}
XDR xdrs;
xdrstdio_create(&xdrs, infile, XDR_DECODE);
if(!xdr_template(&xdrs, &fh)) {
throw XDRException("Couldn't read header from file!");
}
if(fh.magic != FieldHeader::MagicNumber) {
throw XDRException("This file does not appear to be a field file (magic number doesn't match).");
}
if(fh.dimensions != dim) {
throw XDRException("Wrong dimension of positions.");
}
void* data = readField(fh, &xdrs, numParticles, startParticle);
if(data == 0) {
throw XDRException("Had problems reading in the field");
}
xdr_destroy(&xdrs);
fclose(infile);
return data;
}
/// @brief load attributes common to all particles
static void load_NC_base(std::string filename, int64_t startParticle,
int myNum, GravityParticle *myParts)
{
FieldHeader fh;
// Positions
if(verbosity && startParticle == 0)
CkPrintf("loading positions\n");
void *data = readFieldData(filename + "/pos", fh, 3, myNum,
startParticle);
for(int i = 0; i < myNum; ++i) {
switch(fh.code) {
case float32:
myParts[i].position = static_cast<Vector3D<float> *>(data)[i];
break;
case float64:
myParts[i].position = static_cast<Vector3D<double> *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
// velocities
if(verbosity && startParticle == 0)
CkPrintf("loading velocities\n");
data = readFieldData(filename + "/vel", fh, 3, myNum,
startParticle);
for(int i = 0; i < myNum; ++i) {
switch(fh.code) {
case float32:
myParts[i].velocity = static_cast<Vector3D<float> *>(data)[i];
break;
case float64:
myParts[i].velocity = static_cast<Vector3D<double> *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
// masses
if(verbosity && startParticle == 0)
CkPrintf("loading masses\n");
data = readFieldData(filename + "/mass", fh, 1, myNum,
startParticle);
for(int i = 0; i < myNum; ++i) {
switch(fh.code) {
case float32:
myParts[i].mass = static_cast<float *>(data)[i];
break;
case float64:
myParts[i].mass = static_cast<double *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
// softenings
if(verbosity && startParticle == 0)
CkPrintf("loading softenings\n");
data = readFieldData(filename + "/soft", fh, 1, myNum,
startParticle);
for(int i = 0; i < myNum; ++i) {
switch(fh.code) {
case float32:
myParts[i].soft = static_cast<float *>(data)[i];
#ifdef CHANGESOFT
myParts[i].fSoft0 = static_cast<float *>(data)[i];
#endif
break;
case float64:
myParts[i].soft = static_cast<double *>(data)[i];
#ifdef CHANGESOFT
myParts[i].fSoft0 = static_cast<double *>(data)[i];
#endif
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
}
static void load_NC_gas(std::string filename, int64_t startParticle,
int myNumSPH, GravityParticle *myParts,
extraSPHData *mySPHParts, double dTuFac)
{
if(verbosity && startParticle == 0)
CkPrintf("loading gas\n");
load_NC_base(filename, startParticle, myNumSPH, myParts);
for(int i = 0; i < myNumSPH; ++i) {
myParts[i].iType = TYPE_GAS;
myParts[i].extraData = &mySPHParts[i];
myParts[i].fBallMax() = FLT_MAX; // N.B. don't use DOUBLE_MAX here:
// fBallMax*fBallMax should not overflow.
myParts[i].fESNrate() = 0.0;
myParts[i].fTimeCoolIsOffUntil() = 0.0;
myParts[i].dTimeFB() = 0.0;
#ifdef NEED_DT
myParts[i].dt = FLT_MAX;
#endif
#ifdef DTADJUST
myParts[i].dtNew() = FLT_MAX;
#ifndef COOLING_NONE
myParts[i].uDot() = 0.0; // Used in initial timestep
#endif
myParts[i].PdV() = 0.0; // Used in initial timestep
#endif
#ifdef SUPERBUBBLE
myParts[i].cpHotInit() = 0;
myParts[i].uHot() = 0.0;
myParts[i].uHotPred() = 0.0;
myParts[i].uHotDot() = 0.0;
myParts[i].massHot() = 0.0;
myParts[i].fThermalCond() = 0.0;
myParts[i].fPromoteSum() = 0.0;
myParts[i].fPromoteSumuPred() = 0.0;
myParts[i].fPromoteuPredInit() = 0.0;
#endif
}
FieldHeader fh;
// Density
if(verbosity && startParticle == 0)
CkPrintf("loading densities\n");
void *data = readFieldData(filename + "/GasDensity", fh, 1, myNumSPH,
startParticle);
for(int i = 0; i < myNumSPH; ++i) {
switch(fh.code) {
case float32:
myParts[i].fDensity = static_cast<float *>(data)[i];
break;
case float64:
myParts[i].fDensity = static_cast<double *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
if(ncGetCount(filename + "/OxMassFrac") > 0) {
// Oxygen
if(verbosity && startParticle == 0)
CkPrintf("loading Oxygen\n");
data = readFieldData(filename + "/OxMassFrac", fh, 1, myNumSPH,
startParticle);
for(int i = 0; i < myNumSPH; ++i) {
switch(fh.code) {
case float32:
myParts[i].fMFracOxygen() = static_cast<float *>(data)[i];
break;
case float64:
myParts[i].fMFracOxygen() = static_cast<double *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
}
if(ncGetCount(filename + "/FeMassFrac") > 0) {
// Iron
if(verbosity && startParticle == 0)
CkPrintf("loading Iron\n");
data = readFieldData(filename + "/FeMassFrac", fh, 1, myNumSPH,
startParticle);
for(int i = 0; i < myNumSPH; ++i) {
switch(fh.code) {
case float32:
myParts[i].fMFracIron() = static_cast<float *>(data)[i];
break;
case float64:
myParts[i].fMFracIron() = static_cast<double *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
}
// Temperature
if(verbosity && startParticle == 0)
CkPrintf("loading temperature\n");
data = readFieldData(filename + "/temperature", fh, 1, myNumSPH,
startParticle);
for(int i = 0; i < myNumSPH; ++i) {
switch(fh.code) {
case float32:
myParts[i].u() = dTuFac*static_cast<float *>(data)[i];
break;
case float64:
myParts[i].u() = dTuFac*static_cast<double *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
myParts[i].uPred() = myParts[i].u();
// Initial estimate of sound speed.
double gamma = GAMMA_NONCOOL;
double gammam1 = gamma - 1.0;
myParts[i].c() = sqrt(gamma*gammam1*myParts[i].uPred());
}
deleteField(fh, data);
}
static void load_NC_dark(std::string filename, int64_t startParticle,
int myNumDark, GravityParticle *myParts)
{
if(verbosity && startParticle == 0)
CkPrintf("loading darks\n");
load_NC_base(filename, startParticle, myNumDark, myParts);
#ifdef COLLISION
if(verbosity && startParticle == 0)
CkPrintf("loading spins\n");
FieldHeader fh;
void *data = readFieldData(filename + "/spin", fh, 3, myNumDark,
startParticle);
for(int i = 0; i < myNumDark; ++i) {
switch(fh.code) {
case float32:
myParts[i].w = static_cast<Vector3D<float> *>(data)[i];
break;
case float64:
myParts[i].w = static_cast<Vector3D<double> *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
#endif
for(int i = 0; i < myNumDark; ++i) {
myParts[i].fDensity = 0.0;
myParts[i].iType = TYPE_DARK;
}
}
static void load_NC_star(std::string filename, int64_t startParticle,
int myNumStar, GravityParticle *myParts,
extraStarData *myStarParts)
{
if(verbosity && startParticle == 0)
CkPrintf("loading stars\n");
for(int i = 0; i < myNumStar; ++i) {
myParts[i].iType = TYPE_STAR;
myParts[i].extraData = &myStarParts[i];
myParts[i].fDensity = 0.0;
myParts[i].iGasOrder() = -1;
}
load_NC_base(filename, startParticle, myNumStar, myParts);
FieldHeader fh;
// Oxygen
if(verbosity && startParticle == 0)
CkPrintf("loading Oxygen\n");
void *data = readFieldData(filename + "/OxMassFrac", fh, 1, myNumStar,
startParticle);
for(int i = 0; i < myNumStar; ++i) {
switch(fh.code) {
case float32:
myParts[i].fStarMFracOxygen() = static_cast<float *>(data)[i];
break;
case float64:
myParts[i].fStarMFracOxygen() = static_cast<double *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
// Iron
if(verbosity && startParticle == 0)
CkPrintf("loading Iron\n");
data = readFieldData(filename + "/FeMassFrac", fh, 1, myNumStar,
startParticle);
for(int i = 0; i < myNumStar; ++i) {
switch(fh.code) {
case float32:
myParts[i].fStarMFracIron() = static_cast<float *>(data)[i];
break;
case float64:
myParts[i].fStarMFracIron() = static_cast<double *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
// Formation Time
if(verbosity && startParticle == 0)
CkPrintf("loading timeform\n");
data = readFieldData(filename + "/timeform", fh, 1, myNumStar,
startParticle);
for(int i = 0; i < myNumStar; ++i) {
switch(fh.code) {
case float32:
myParts[i].fTimeForm() = static_cast<float *>(data)[i];
break;
case float64:
myParts[i].fTimeForm() = static_cast<double *>(data)[i];
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
for(int i = 0; i < myNumStar; ++i) {
myParts[i].fMassForm() = myParts[i].mass;
#ifdef COOLING_MOLECULARH
myParts[i].dStarLymanWerner() = 0.0;
#endif
}
}
void TreePiece::loadNChilada(const std::string& filename,
const double dTuFac, // Convert Temperature
const CkCallback& cb) {
LBTurnInstrumentOff();
basefilename = filename;
nTotalSPH = ncGetCount(filename + "/gas/pos");
nTotalDark = ncGetCount(filename + "/dark/pos");
nTotalStar = ncGetCount(filename + "/star/pos");
nTotalParticles = nTotalSPH + nTotalDark + nTotalStar;
CkMustAssert(nTotalParticles > 0, "No particles can be read. Check file permissions\n");
dStartTime = fh_time;
bool skipLoad = !isLoadingPiece(thisIndex, numTreePieces);
if(skipLoad){
myNumParticles = 0;
nStartRead = -1;
contribute(cb);
return;
}
// find your load offset into input file
int myIndex = CkMyPe();
int numLoadingPEs = CkNumPes();
myNumParticles = nTotalParticles / numLoadingPEs;
int excess = nTotalParticles % numLoadingPEs;
int64_t startParticle = ((int64_t)myNumParticles) * myIndex;
if(myIndex < excess) {
myNumParticles++;
startParticle += myIndex;
}
else {
startParticle += excess;
}
if(startParticle >= nTotalParticles) {
CkError("Bad startParticle: %ld, nPart: %ld, myIndex: %d, nLoading: %d\n",
startParticle, nTotalParticles, myIndex, numLoadingPEs);
}
CkAssert(startParticle < nTotalParticles);
nStartRead = startParticle;
if(verbosity > 2)
cerr << "TreePiece " << thisIndex << " PE " << CkMyPe() << " Taking " << myNumParticles
<< " of " << nTotalParticles
<< " particles: [" << startParticle << "," << startParticle+myNumParticles << ")" << endl;
// allocate an array for myParticles
nStore = (int)((myNumParticles + 2)*(1.0 + dExtraStore));
myParticles = new GravityParticle[nStore];
// Are we loading SPH?
if(startParticle < nTotalSPH) {
if(startParticle + myNumParticles <= nTotalSPH)
myNumSPH = myNumParticles;
else
myNumSPH = nTotalSPH - startParticle;
}
else {
myNumSPH = 0;
}
nStoreSPH = (int)(myNumSPH*(1.0 + dExtraStore));
if(nStoreSPH > 0)
mySPHParticles = new extraSPHData[nStoreSPH];
// Are we loading stars?
if(startParticle + myNumParticles > nTotalSPH + nTotalDark) {
if(startParticle <= nTotalSPH + nTotalDark)
myNumStar = startParticle + myNumParticles
- (nTotalSPH + nTotalDark);
else
myNumStar = myNumParticles;
}
else {
myNumStar = 0;
}
allocateStars();
if(myNumSPH > 0) {
load_NC_gas(filename + "/gas", startParticle, myNumSPH,
&myParticles[1], mySPHParticles, dTuFac);
}
int myNumDark = myNumParticles - myNumSPH - myNumStar;
startParticle -= nTotalSPH;
if(startParticle < 0)
startParticle = 0;
if(myNumDark > 0) {
load_NC_dark(filename + "/dark", startParticle, myNumDark,
&myParticles[myNumSPH + 1]);
}
startParticle = nStartRead - nTotalSPH - nTotalDark;
if(startParticle < 0)
startParticle = 0;
if(myNumStar > 0) {
load_NC_star(filename + "/star", startParticle, myNumStar,
&myParticles[myNumSPH + myNumDark + 1],
myStarParticles);
}
for(int i = 0; i < myNumParticles; ++i) {
myParticles[i+1].rung = 0;
myParticles[i+1].fBall = 0.0;
myParticles[i+1].iOrder = i + nStartRead;
#ifdef SIDMINTERACT
myParticles[i+1].iNSIDMInteractions = 0;
#endif
#ifdef SPLITGAS
if(myParticles[i+1].iOrder < nTotalSPH) myParticles[i+1].iSplitOrder() = i + nStartRead;
if(myParticles[i+1].iOrder >= nTotalSPH) myParticles[i+1].iOrder += nTotalSPH;
#endif
boundingBox.grow(myParticles[i+1].position);
}
myParticles[0].key = firstPossibleKey;
myParticles[myNumParticles+1].key = lastPossibleKey;
contribute(cb);
}
/// Generic read of binary (NChilada) array format into floating point
/// or integer particle attribute (NOTE MISNOMER)
void TreePiece::readFloatBinary(OutputParams& params, int bParaRead,
const CkCallback& cb)
{
FieldHeader fh;
void *data;
int64_t startParticle = nStartRead;
params.dm = dm; // pass cooling information
if((params.iType & TYPE_GAS) && (myNumSPH > 0)) {
data = readFieldData(params.fileName + "/gas/" + params.sNChilExt, fh,
1, myNumSPH, nStartRead);
for(int i = 0; i < myNumSPH; ++i) {
switch(fh.code) {
case int32:
params.setIValue(&myParticles[i+1], static_cast<int *>(data)[i]);
break;
case int64:
params.setIValue(&myParticles[i+1], static_cast<int64_t *>(data)[i]);
break;
case float32:
params.setDValue(&myParticles[i+1], static_cast<float *>(data)[i]);
break;
case float64:
params.setDValue(&myParticles[i+1], static_cast<double *>(data)[i]);
break;
default:
throw XDRException("I don't recognize the type of this field!");
}
}
deleteField(fh, data);
}
int myNumDark = myNumParticles - myNumSPH - myNumStar;
startParticle -= nTotalSPH;
if(startParticle < 0) startParticle = 0;
if((params.iType & TYPE_DARK) && (myNumDark > 0)) {
data = readFieldData(params.fileName + "/dark/" + params.sNChilExt, fh,
1, myNumDark, startParticle);
for(int i = 0; i < myNumDark; ++i) {
switch(fh.code) {
case int32:
params.setIValue(&myParticles[myNumSPH+i+1], static_cast<int *>(data)[i]);
break;
case int64:
params.setIValue(&myParticles[myNumSPH+i+1], static_cast<int64_t *>(data)[i]);
break;
case float32:
params.setDValue(&myParticles[myNumSPH+i+1], static_cast<float *>(data)[i]);