-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpkd.c
6827 lines (6211 loc) · 200 KB
/
pkd.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
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
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <sys/time.h>
#ifdef CRAY_XT3
#include "../xdr/types.h"
#include "../xdr/xdr.h"
#else
#include <rpc/types.h>
#include <rpc/xdr.h>
#endif
#include "pkd.h"
#include "ewald.h"
#include "grav.h"
#include "walk.h"
#include "opentype.h"
#include "mdl.h"
#include "tipsydefs.h"
#ifdef COLLISIONS
#include "ssdefs.h" /* in turn includes ssio.h */
#include "collision.h"
#endif
#ifdef AGGS
#include "aggs.h"
#endif
#ifdef SLIDING_PATCH
#include <sys/types.h> /* for getpid() */
#include <unistd.h> /* ditto */
#include <time.h> /* for time() */
#ifndef SGN
int SGN(double x);
#define SGN(x) ((x) < 0.0 ? (-1) : (x) > 0.0 ? 1 : 0)
#endif
#endif
#ifdef _LARGE_FILES
#define fseek fseeko
#endif
double pkdGetTimer(PKD pkd,int iTimer)
{
return(pkd->ti[iTimer].sec);
}
double pkdGetSystemTimer(PKD pkd,int iTimer)
{
return(pkd->ti[iTimer].system_sec);
}
double pkdGetWallClockTimer(PKD pkd,int iTimer)
{
return(pkd->ti[iTimer].wallclock_sec);
}
void pkdClearTimer(PKD pkd,int iTimer)
{
int i;
if (iTimer >= 0) {
pkd->ti[iTimer].sec = 0.0;
pkd->ti[iTimer].system_sec = 0.0;
pkd->ti[iTimer].wallclock_sec = 0.0;
pkd->ti[iTimer].iActive = 0;
}
else {
for (i=0;i<MAX_TIMERS;++i) {
pkd->ti[i].sec = 0.0;
pkd->ti[i].system_sec = 0.0;
pkd->ti[i].wallclock_sec = 0.0;
pkd->ti[i].iActive = 0;
}
}
}
void pkdStartTimer(PKD pkd,int iTimer)
{
struct timezone tz;
struct timeval tv;
tz.tz_minuteswest = 0;
tz.tz_dsttime = 0;
pkd->ti[iTimer].iActive++;
if (pkd->ti[iTimer].iActive == 1) {
pkd->ti[iTimer].stamp = mdlCpuTimer(pkd->mdl);
gettimeofday(&tv,&tz);
pkd->ti[iTimer].wallclock_stamp = tv.tv_sec + 1e-6*(double) tv.tv_usec;
#ifndef _CRAYMPP
{
struct rusage ru;
getrusage(0,&ru);
pkd->ti[iTimer].system_stamp = (double)ru.ru_stime.tv_sec + 1e-6*(double)ru.ru_stime.tv_usec;
}
#endif
}
}
void pkdStopTimer(PKD pkd,int iTimer)
{
double sec;
struct timeval tv;
struct timezone tz;
sec = -pkd->ti[iTimer].stamp;
pkd->ti[iTimer].stamp = mdlCpuTimer(pkd->mdl);
sec += pkd->ti[iTimer].stamp;
if (sec < 0.0) sec = 0.0;
pkd->ti[iTimer].sec += sec;
sec = -pkd->ti[iTimer].wallclock_stamp;
tz.tz_minuteswest = 0;
tz.tz_dsttime = 0;
gettimeofday( &tv, &tz );
pkd->ti[iTimer].wallclock_stamp = tv.tv_sec + 1e-6*(double)tv.tv_usec;
sec += pkd->ti[iTimer].wallclock_stamp;
if (sec < 0.0) sec = 0.0;
pkd->ti[iTimer].wallclock_sec += sec;
#ifndef _CRAYMPP
{
struct rusage ru;
sec = -pkd->ti[iTimer].system_stamp;
getrusage(0,&ru);
pkd->ti[iTimer].system_stamp = ((double)ru.ru_stime.tv_sec + 1e-6*(double)ru.ru_stime.tv_usec);
sec += pkd->ti[iTimer].system_stamp;
if (sec < 0.0) sec = 0.0;
pkd->ti[iTimer].system_sec += sec;
}
#endif
pkd->ti[iTimer].iActive--;
}
void pkdInitialize(PKD *ppkd,MDL mdl,int iOrder,int nStore,int nLvl,
FLOAT *fPeriod,FLOAT dxInflow,FLOAT dxOutflow,int nDark,int nGas,int nStar)
{
PKD pkd;
int j;
pkd = (PKD)malloc(sizeof(struct pkdContext));
mdlassert(mdl,pkd != NULL);
pkd->mdl = mdl;
pkd->iOrder = iOrder;
pkd->idSelf = mdlSelf(mdl);
pkd->nThreads = mdlThreads(mdl);
pkd->nStore = nStore;
pkd->nLocal = 0;
pkd->nDark = nDark;
pkd->nGas = nGas;
pkd->nStar = nStar;
pkd->nMaxOrderGas = NIORDERGASBUFFER + nGas - 1;
pkd->nMaxOrderDark = NIORDERGASBUFFER + nGas + nDark - 1;
pkd->nRejects = 0;
for (j=0;j<3;++j) {
pkd->fPeriod[j] = fPeriod[j];
}
pkd->dxInflow = dxInflow;
pkd->dxOutflow = dxOutflow;
pkd->sinkLog.nLog = 0; /* Nothing in log, even if not needed */
/*
** Allocate the main particle store.
** Need to use mdlMalloc() since the particles will need to be
** visible to all other processors thru mdlAquire() later on.
**
** We need one EXTRA storage location at the very end to use for
** calculating acceleration on arbitrary positions in space, for example
** determining the force on the sun. The easiest way to do this is to
** allocate one hidden particle, which won't interfere with the rest of
** the code (hopefully). pkd->pStore[pkd->nStore] is this particle.
*/
pkd->pStore = mdlMalloc(pkd->mdl,(nStore+1)*sizeof(PARTICLE));
mdlassert(pkd->mdl,pkd->pStore != NULL);
pkd->kdNodes = NULL;
pkd->piLeaf = NULL;
pkd->kdTop = NULL;
/*
** Allocate initial interaction lists
*/
pkd->nMaxPart = 500;
pkd->nMaxCellSoft = 500;
pkd->nMaxCellNewt = 500;
pkd->nSqrtTmp = 500;
pkd->ilp = malloc(pkd->nMaxPart*sizeof(ILP));
mdlassert(pkd->mdl,pkd->ilp != NULL);
pkd->ilcs = malloc(pkd->nMaxCellSoft*sizeof(ILCS));
mdlassert(pkd->mdl,pkd->ilcs != NULL);
pkd->ilcn = malloc(pkd->nMaxCellNewt*sizeof(ILCN));
mdlassert(pkd->mdl,pkd->ilcn != NULL);
pkd->sqrttmp = malloc(pkd->nSqrtTmp*sizeof(double));
mdlassert(pkd->mdl,pkd->sqrttmp != NULL);
pkd->d2a = malloc(pkd->nSqrtTmp*sizeof(double));
mdlassert(pkd->mdl,pkd->d2a != NULL);
/*
** Ewald stuff!
*/
pkd->nMaxEwhLoop = 100;
pkd->ewt = malloc(pkd->nMaxEwhLoop*sizeof(EWT));
mdlassert(pkd->mdl,pkd->ewt != NULL);
/*
* Cooling
*/
#ifdef GASOLINE
#ifndef NOCOOLING
pkd->Cool = CoolInit();
#endif
#endif
*ppkd = pkd;
}
void pkdFinish(PKD pkd)
{
if (pkd->kdNodes) {
/*
** Close caching space and free up nodes.
*/
mdlFinishCache(pkd->mdl,CID_CELL);
mdlFree(pkd->mdl,pkd->kdNodes);
}
if (pkd->kdTop) free(pkd->kdTop);
if (pkd->piLeaf) free(pkd->piLeaf);
free(pkd->ilp);
free(pkd->ilcs);
free(pkd->ilcn);
free(pkd->sqrttmp);
free(pkd->d2a);
free(pkd->ewt);
#if defined(GASOLINE) && !defined(NOCOOLING)
CoolFinalize(pkd->Cool);
#endif
mdlFree(pkd->mdl,pkd->pStore);
free(pkd);
}
void pkdSeek(PKD pkd,FILE *fp,int nStart,int bStandard)
{
long lStart;
/*
** Seek according to true XDR size structures when bStandard is true.
** This may be a bit dicey, but it should work as long
** as no one changes the tipsy binary format!
*/
if (bStandard) lStart = 32;
else lStart = sizeof(struct dump);
if (nStart > pkd->nGas) {
if (bStandard) lStart += pkd->nGas*48L;
else lStart += pkd->nGas*sizeof(struct gas_particle);
nStart -= pkd->nGas;
if (nStart > pkd->nDark) {
if (bStandard) lStart += pkd->nDark*36L;
else lStart += pkd->nDark*sizeof(struct dark_particle);
nStart -= pkd->nDark;
if (bStandard) lStart += nStart*44L;
else lStart += nStart*sizeof(struct star_particle);
}
else {
if (bStandard) lStart += nStart*36L;
else lStart += nStart*sizeof(struct dark_particle);
}
}
else {
if (bStandard) lStart += nStart*48L;
else lStart += nStart*sizeof(struct gas_particle);
}
assert(fseek(fp,lStart,0) == 0);
}
void pkdGenericSeek(PKD pkd,FILE *fp,long nStart,int iHeader,int iElement)
{
long lStart;
lStart = iHeader + nStart*((long)iElement);
fseek(fp,lStart,0);
}
void pkdReadTipsy(PKD pkd,char *pszFileName,int nStart,int nLocal,
int bStandard,int iReadIOrder,double dvFac,double dTuFac)
{
FILE *fp,*fpiord = NULL;
#if defined(SIMPLESF) || defined(STARFORM)
FILE *fpmStar = NULL, *fptCoolAgain = NULL;
#endif
#ifdef TWOPHASE
FILE *fpmhot = NULL, *fpuhot = NULL, *fpigasorder = NULL;
#endif
int i,j, iSetMask;
PARTICLE *p;
struct dark_particle dp;
struct gas_particle gp;
struct star_particle sp;
float fTmp;
pkd->nLocal = nLocal;
pkd->nActive = nLocal;
/*
** General initialization.
*/
for (i=0;i<nLocal;++i) {
p = &pkd->pStore[i];
TYPEClear(p);
p->iRung = 0;
p->fWeight = 1.0;
p->fDensity = 0.0;
p->fBall2 = 0.0;
p->fBallMax = 0.0;
#ifdef GASOLINE
p->dt = FLT_MAX;
p->dtNew = FLT_MAX;
p->u = 0.0;
p->uPred = 0.0;
#ifdef TWOPHASE
p->fMassHot = 0;
#endif
#ifdef UNONCOOL
p->uHot = 0.;
p->uHotPred = 0.;
p->uHotDot = 0.;
#endif
#ifdef STARFORM
p->uDotFB = 0.0;
p->uDotESF = 0.0;
p->fNSN = 0.0;
p->fNSNtot = 0.0;
p->fMOxygenOut = 0.0;
p->fMIronOut = 0.0;
p->fMFracOxygen = 0.0;
p->fMFracIron = 0.0;
p->fTimeCoolIsOffUntil = 0.0;
#endif
#ifdef VARALPHA
p->alpha = ALPHAMIN;
p->alphaPred = ALPHAMIN;
p->divv = 0;
p->dvds = 0;
#endif
#ifdef CULLENDEHNEN
p->alpha = 0;
p->dTime_divv = FLT_MAX;
p->divv_old = 0;
#endif
#ifndef NOCOOLING
/* Place holders -- later fixed in pkdInitEnergy */
CoolDefaultParticleData( &p->CoolParticle );
#endif
p->c = 0.0;
p->fMetals = 0.0;
p->fTimeForm = 1e37;
#endif
#ifdef NEED_VPRED
for (j=0;j<3;++j) {
p->vPred[j] = 0.0;
}
#endif
}
/*
** Seek past the header and up to nStart.
*/
fp = fopen(pszFileName,"r");
mdlassert(pkd->mdl,fp != NULL);
/*
** Seek to right place in file.
*/
pkdSeek(pkd,fp,nStart,bStandard);
/* Open iOrder file if requested */
if (iReadIOrder) {
char atmp[512];
sprintf(atmp,"%s.iord",pszFileName);
fpiord = fopen(atmp,"r");
mdlassert(pkd->mdl,fpiord != NULL);
/*
** Seek to right place in file
*/
switch(iReadIOrder) {
case 1:
pkdGenericSeek(pkd,fpiord,nStart,sizeof(int),sizeof(int));
break;
case 2:
pkdGenericSeek(pkd,fpiord,nStart,sizeof(int),sizeof(long long));
if (bStandard) assert(sizeof(long)==sizeof(long long));
break;
case 3:
pkdGenericSeek(pkd,fpiord,nStart,sizeof(int),sizeof(pkd->pStore[0].iOrder));
if (bStandard) assert(sizeof(int)==sizeof(pkd->pStore[0].iOrder));
break;
default:
fprintf(stderr,"Don't understand iOrder format: %d\n",iReadIOrder);
mdlassert(pkd->mdl,0);
}
}
#ifdef TWOPHASE
{
char atmp[512];
sprintf(atmp,"%s.MassHot",pszFileName);
fpmhot = fopen(atmp,"r");
if (fpmhot!=NULL) pkdGenericSeek(pkd,fpmhot,nStart,sizeof(int),sizeof(float));
else if(pkd->idSelf == 0) fprintf(stderr, "Could not open %s, skipped.\n",atmp);
sprintf(atmp,"%s.uHot",pszFileName);
fpuhot = fopen(atmp,"r");
if (fpuhot!=NULL) pkdGenericSeek(pkd,fpuhot,nStart,sizeof(int),sizeof(float));
else if(pkd->idSelf == 0) fprintf(stderr, "Could not open %s, skipped.\n",atmp);
sprintf(atmp,"%s.igasOrder",pszFileName);
fpigasorder = fopen(atmp,"r");
if (fpigasorder!=NULL) pkdGenericSeek(pkd,fpigasorder,nStart,sizeof(int),4);
else if(pkd->idSelf == 0) fprintf(stderr, "Could not open %s, skipped.\n",atmp);
}
#endif
#ifdef STARFORM
{
char atmp[512];
sprintf(atmp,"%s.coolontime",pszFileName);
fptCoolAgain = fopen(atmp,"r");
if (fptCoolAgain!=NULL) {
/*
** Seek to right place in file
*/
pkdGenericSeek(pkd,fptCoolAgain,nStart,sizeof(int),sizeof(float));
}
else {
if(pkd->idSelf == 0)
fprintf(stderr, "Could not open %s, skipped.\n",atmp);
}
}
#endif
/*
** Read Stuff!
*/
if (bStandard) {
FLOAT vTemp;
long LongTmp;
int IntTmp;
XDR xdrs,xdrsiord;
xdrstdio_create(&xdrs,fp,XDR_DECODE);
#ifdef TWOPHASE
XDR xdrsmhot,xdrsuhot,xdrsigasorder;
if (fpmhot) xdrstdio_create(&xdrsmhot,fpmhot,XDR_DECODE);
if (fpuhot) xdrstdio_create(&xdrsuhot,fpuhot,XDR_DECODE);
if (fpigasorder) xdrstdio_create(&xdrsigasorder,fpigasorder,XDR_DECODE);
#endif
#ifdef STARFORM
XDR xdrstoc;
if (fptCoolAgain) xdrstdio_create(&xdrstoc,fptCoolAgain,XDR_DECODE);
#endif
if (iReadIOrder) xdrstdio_create(&xdrsiord,fpiord,XDR_DECODE);
for (i=0;i<nLocal;++i) {
p = &pkd->pStore[i];
p->iOrder = nStart + i; /* temporary */
#if NIORDERGASBUFFER
if (p->iOrder >= pkd->nGas) p->iOrder += NIORDERGASBUFFER;
#endif
if (pkdIsGasByOrder(pkd,p)) {
iSetMask = TYPE_GAS; /* saves identity based on Tipsy file in case iOrder changed */
xdr_float(&xdrs,&fTmp);
p->fMass = fTmp;
#ifdef TWOPHASE
if (fpmhot) { xdr_float(&xdrsmhot,&fTmp); p->fMassHot = fTmp; }
if (fpuhot) { xdr_float(&xdrsuhot,&fTmp); p->uHot = p->uHotPred = fTmp; }
if (fpigasorder) { xdr_int(&xdrsigasorder,&IntTmp); p->iGasOrder = IntTmp; }
if (p->fMassHot > 0) TYPESet(p,TYPE_TWOPHASE);
assert(p->fMassHot < p->fMass);
#endif
assert(p->fMass > 0.0);
for (j=0;j<3;++j) {
xdr_float(&xdrs,&fTmp);
p->r[j] = fTmp;
}
for (j=0;j<3;++j) {
xdr_float(&xdrs,&fTmp);
vTemp = fTmp;
p->v[j] = dvFac*vTemp;
#ifdef NEED_VPRED
p->vPred[j] = dvFac*vTemp;
#endif
}
#ifdef GASOLINE
xdr_float(&xdrs,&fTmp);
p->fDensity = fTmp;
/*
** Convert Temperature to Thermal energy.
*/
xdr_float(&xdrs,&fTmp);
vTemp = fTmp;
p->u = dTuFac*vTemp;
p->uPred = dTuFac*vTemp;
// Special purpose hack for testing noncooling
xdr_float(&xdrs,&fTmp);
p->fSoft = fTmp;
p->fSoft0 = fTmp;
xdr_float(&xdrs,&fTmp);
p->fMetals = fTmp;
p->fMetalsPred = fTmp;
#ifdef STARFORM
/* O and Fe ratio based on Asplund et al 2009 */
if (p->fMetals && !p->fMFracOxygen &&
!p->fMFracIron) {
p->fMFracOxygen = 0.43 * p->fMetals;
p->fMFracIron = 0.098 * p->fMetals;
}
#endif
#else /* now not GASOLINE */
xdr_float(&xdrs,&fTmp); /* Dens */
xdr_float(&xdrs,&fTmp); /* T */
xdr_float(&xdrs,&fTmp); /* eps */
p->fSoft = fTmp;
p->fSoft0 = fTmp;
xdr_float(&xdrs,&fTmp); /* metals */
#endif
xdr_float(&xdrs,&fTmp);
p->fPot = fTmp;
}
else if (pkdIsDarkByOrder(pkd,p)) {
iSetMask = TYPE_DARK;
xdr_float(&xdrs,&fTmp);
p->fMass = fTmp;
assert(p->fMass >= 0.0);
for (j=0;j<3;++j) {
xdr_float(&xdrs,&fTmp);
p->r[j] = fTmp;
}
for (j=0;j<3;++j) {
xdr_float(&xdrs,&fTmp);
vTemp = fTmp;
p->v[j] = dvFac*vTemp;
}
xdr_float(&xdrs,&fTmp);
p->fSoft = fTmp;
p->fSoft0 = fTmp;
xdr_float(&xdrs,&fTmp);
p->fPot = fTmp;
}
else if (pkdIsStarByOrder(pkd,p)) {
iSetMask = TYPE_STAR;
xdr_float(&xdrs,&fTmp);
p->fMass = fTmp;
#ifdef STARFORM
p->fMassForm = fTmp;
#endif
assert(p->fMass >= 0.0);
for (j=0;j<3;++j) {
xdr_float(&xdrs,&fTmp);
p->r[j] = fTmp;
}
for (j=0;j<3;++j) {
xdr_float(&xdrs,&fTmp);
vTemp = fTmp;
p->v[j] = dvFac*vTemp;
}
#ifdef GASOLINE
xdr_float(&xdrs,&fTmp);
p->fMetals = fTmp;
p->fMetalsPred = fTmp;
#ifdef STARFORM
/* O and Fe ratio based on Asplund et al 2009 */
if (p->fMetals && !p->fMFracOxygen &&
!p->fMFracIron) {
p->fMFracOxygen = 0.43 * p->fMetals;
p->fMFracIron = 0.098 * p->fMetals;
}
#endif
xdr_float(&xdrs,&fTmp);
p->fTimeForm = fTmp;
#else /* not GASOLINE */
xdr_float(&xdrs,&fTmp);
xdr_float(&xdrs,&fTmp);
#endif
xdr_float(&xdrs,&fTmp);
p->fSoft = fTmp;
p->fSoft0 = fTmp;
xdr_float(&xdrs,&fTmp);
p->fPot = fTmp;
}
else mdlassert(pkd->mdl,0); /* unrecognized type */
/* particle read from tipsy */
#ifdef STARFORM
if (fptCoolAgain!=NULL) {
xdr_float(&xdrstoc,&fTmp);
if (pkdIsGasByOrder(pkd,p)) p->fTimeCoolIsOffUntil = fTmp;
}
#endif
#ifdef INFLOWOUTFLOW
if (p->r[0] < pkd->dxInflow) { TYPESet(p,TYPE_INFLOW); assert(p->v[0] > 0); }
if (p->r[0] > pkd->dxOutflow) { TYPESet(p,TYPE_OUTFLOW); assert(p->v[0] > 0); }
#endif
TYPESet(p,iSetMask);
/* Read iOrder last so byOrder Types not messed up */
switch (iReadIOrder) {
case 0:
break;
case 1:
xdr_int(&xdrsiord,&IntTmp);
// fread(&IntTmp,sizeof(IntTmp),1,fpiord);
p->iOrder = IntTmp;
break;
case 2:
xdr_longlong_t(&xdrsiord,(quad_t *) &LongTmp);
// fread(&LongTmp,sizeof(LongTmp),1,fpiord);
p->iOrder = LongTmp;
break;
case 3:
xdr_int(&xdrsiord,&IntTmp);
// see assert above -- I have to assume iOrder is int
p->iOrder = IntTmp;
break;
}
}
xdr_destroy(&xdrs);
#ifdef STARFORM
// xdr_destroy(&xdrstoc);
#endif
// if (iReadIOrder) xdr_destroy(&xdrsiord);
} /* standard read done */
/* native format read */
else {
long long LongTmp;
int IntTmp;
for (i=0;i<nLocal;++i) {
p = &pkd->pStore[i];
p->iOrder = nStart + i;
#if NIORDERGASBUFFER
if (p->iOrder >= pkd->nGas) p->iOrder += NIORDERGASBUFFER;
#endif
if (pkdIsGasByOrder(pkd,p)) {
iSetMask = TYPE_GAS;
fread(&gp,sizeof(struct gas_particle),1,fp);
for (j=0;j<3;++j) {
p->r[j] = gp.pos[j];
p->v[j] = dvFac*gp.vel[j];
#ifdef NEED_VPRED
p->vPred[j] = dvFac*gp.vel[j];
#endif
}
p->fMass = gp.mass;
assert(p->fMass >= 0.0);
p->fSoft = gp.eps;
p->fSoft0 = gp.eps;
p->fPot = gp.phi;
#ifdef GASOLINE
p->fDensity = gp.rho;
p->u = dTuFac*gp.temp;
p->uPred = dTuFac*gp.temp;
p->fMetals = gp.metals;
p->fMetalsPred = gp.metals;
#ifdef STARFORM
/* O and Fe ratio based on Asplund et al 2009 */
if (p->fMetals && !p->fMFracOxygen && !p->fMFracIron) {
p->fMFracOxygen = 0.43 * p->fMetals;
p->fMFracIron = 0.098 * p->fMetals;
}
#endif
#endif
}
else if (pkdIsDarkByOrder(pkd,p)) {
iSetMask = TYPE_DARK;
fread(&dp,sizeof(struct dark_particle),1,fp);
for (j=0;j<3;++j) {
p->r[j] = dp.pos[j];
p->v[j] = dvFac*dp.vel[j];
}
p->fMass = dp.mass;
assert(p->fMass >= 0.0);
p->fSoft = dp.eps;
p->fSoft0 = dp.eps;
p->fPot = dp.phi;
}
else if (pkdIsStarByOrder(pkd,p)) {
iSetMask = TYPE_STAR;
fread(&sp,sizeof(struct star_particle),1,fp);
for (j=0;j<3;++j) {
p->r[j] = sp.pos[j];
p->v[j] = dvFac*sp.vel[j];
}
p->fMass = sp.mass;
#ifdef STARFORM
p->fMassForm = sp.mass;
#endif
assert(p->fMass >= 0.0);
p->fSoft = sp.eps;
p->fSoft0 = sp.eps;
p->fPot = sp.phi;
#ifdef GASOLINE
p->fMetals = sp.metals;
p->fTimeForm = sp.tform;
#ifdef STARFORM
/* O and Fe ratio based on Asplund et al 2009 */
if (p->fMetals && !p->fMFracOxygen
&& !p->fMFracIron) {
p->fMFracOxygen = 0.43 * p->fMetals;
p->fMFracIron = 0.098 * p->fMetals;
}
#endif
#endif
}
else mdlassert(pkd->mdl,0); /* unrecognized particle type */
/* tipsy particle read done */
#ifdef TWOPHASE
if (fpmhot!=NULL) { fread(&fTmp,sizeof(float),1,fpmhot);
if (pkdIsGasByOrder(pkd,p)) p->fMassHot = fTmp; }
if (fpuhot!=NULL) { fread(&fTmp,sizeof(float),1,fpuhot);
if (pkdIsGasByOrder(pkd,p)) p->uHot = p->uHotPred = fTmp; }
if (fpigasorder!=NULL) { fread(&IntTmp,sizeof(int),1,fpigasorder);
p->iGasOrder = IntTmp; }
assert(p->fMassHot < p->fMass);
#endif
#if defined(SIMPLESF) || defined(STARFORM)
if (fptCoolAgain!=NULL) {
fread(&fTmp,sizeof(float),1,fptCoolAgain);
#if defined(STARFORM)
if (pkdIsGasByOrder(pkd,p)) p->fTimeCoolIsOffUntil = fTmp;
#else
if (pkdIsGasByOrder(pkd,p)) p->fTimeForm = fTmp;
#endif
}
if (fpmStar!=NULL) {
fread(&fTmp,sizeof(float),1,fpmStar);
if (pkdIsGasByOrder(pkd,p)) p->fMassForm = fTmp;
}
#endif
#ifdef INFLOWOUTFLOW
if (p->r[0] < pkd->dxInflow) { TYPESet(p,TYPE_INFLOW); assert(p->v[0] > 0); }
if (p->r[0] > pkd->dxOutflow) { TYPESet(p,TYPE_OUTFLOW); assert(p->v[0] > 0); }
#endif
TYPESet(p,iSetMask); /* needed to get max order info */
/* Read iOrder last so Types not messed up */
switch (iReadIOrder) {
case 0:
#if !(NIORDERGASBUFFER)
p->iOrder = nStart + i; /* This should be redundant */
#endif
break;
case 1:
fread(&IntTmp,sizeof(IntTmp),1,fpiord);
p->iOrder = IntTmp;
break;
case 2:
fread(&LongTmp,sizeof(LongTmp),1,fpiord);
p->iOrder = LongTmp;
break;
case 3:
fread(&p->iOrder,sizeof(p->iOrder),1,fpiord);
break;
}
}
} /* native read done */
if (fpiord!=NULL) fclose(fpiord);
fclose(fp);
}
void pkdCalcBound(PKD pkd,BND *pbnd,BND *pbndActive,BND *pbndTreeActive, BND *pbndBall,BNDDT *pbndDt)
{
/* Faster by assuming active order */
int i,j;
FLOAT fBall,r;
/*
** Initialize the bounds to 0 at the beginning
*/
for (j=0;j<3;++j) {
pbnd->fMin[j] = FLOAT_MAXVAL;
pbnd->fMax[j] = -FLOAT_MAXVAL;
pbndActive->fMin[j] = FLOAT_MAXVAL;
pbndActive->fMax[j] = -FLOAT_MAXVAL;
pbndBall->fMin[j] = FLOAT_MAXVAL;
pbndBall->fMax[j] = -FLOAT_MAXVAL;
}
/*
** Calculate Active Bounds assume TreeActive is the same as Active (Gnah!)
*/
for (i=0;i<pkd->nActive;++i) {
fBall = pkd->pStore[i].fBallMax;
for (j=0;j<3;++j) {
r = pkd->pStore[i].r[j];
if (r < pbnd->fMin[j]) pbnd->fMin[j] = r;
if (r > pbnd->fMax[j]) pbnd->fMax[j] = r;
if (r < pbndActive->fMin[j]) pbndActive->fMin[j] = r;
if (r > pbndActive->fMax[j]) pbndActive->fMax[j] = r;
if (r-fBall < pbndBall->fMin[j]) pbndBall->fMin[j] = r-fBall;
if (r+fBall > pbndBall->fMax[j]) pbndBall->fMax[j] = r+fBall;
}
}
/*
** Calculate Local Bounds.
*/
for (i=pkd->nActive;i<pkd->nLocal;++i) {
for (j=0;j<3;++j) {
r = pkd->pStore[i].r[j];
if (r < pbnd->fMin[j]) pbnd->fMin[j] = r;
if (r > pbnd->fMax[j]) pbnd->fMax[j] = r;
}
}
for (j=0;j<3;++j) {
pbndTreeActive->fMin[j] = pbndActive->fMin[j];
pbndTreeActive->fMax[j] = pbndActive->fMax[j];
}
}
void pkdCalcBound_old(PKD pkd,BND *pbnd,BND *pbndActive,BND *pbndTreeActive, BND *pbndBall)
{
int i,j;
FLOAT fBall;
/*
** Initialize the bounds to 0 at the beginning
*/
for (j=0;j<3;++j) {
pbnd->fMin[j] = FLOAT_MAXVAL;
pbnd->fMax[j] = -FLOAT_MAXVAL;
pbndActive->fMin[j] = FLOAT_MAXVAL;
pbndActive->fMax[j] = -FLOAT_MAXVAL;
pbndTreeActive->fMin[j] = FLOAT_MAXVAL;
pbndTreeActive->fMax[j] = -FLOAT_MAXVAL;
pbndBall->fMin[j] = FLOAT_MAXVAL;
pbndBall->fMax[j] = -FLOAT_MAXVAL;
}
/*
** Calculate Local Bounds.
*/
for (i=0;i<pkd->nLocal;++i) {
for (j=0;j<3;++j) {
if (pkd->pStore[i].r[j] < pbnd->fMin[j])
pbnd->fMin[j] = pkd->pStore[i].r[j];
if (pkd->pStore[i].r[j] > pbnd->fMax[j])
pbnd->fMax[j] = pkd->pStore[i].r[j];
}
}
/*
** Calculate Active Bounds.
*/
for (i=0;i<pkd->nLocal;++i) {
if (TYPEQueryACTIVE(&(pkd->pStore[i]))) {
for (j=0;j<3;++j) {
if (pkd->pStore[i].r[j] < pbndActive->fMin[j])
pbndActive->fMin[j] = pkd->pStore[i].r[j];
if (pkd->pStore[i].r[j] > pbndActive->fMax[j])
pbndActive->fMax[j] = pkd->pStore[i].r[j];
}
}
}
/*
** Calculate TreeActive Bounds.
*/
for (i=0;i<pkd->nLocal;++i) {
if (TYPEQueryTREEACTIVE(&(pkd->pStore[i]))) {
for (j=0;j<3;++j) {
if (pkd->pStore[i].r[j] < pbndTreeActive->fMin[j])
pbndTreeActive->fMin[j] = pkd->pStore[i].r[j];
if (pkd->pStore[i].r[j] > pbndTreeActive->fMax[j])
pbndTreeActive->fMax[j] = pkd->pStore[i].r[j];
}
}
}
/*
** Calculate fBall Bounds on TreeActive Particles
*/
for (i=0;i<pkd->nLocal;++i) {
if (TYPEQueryTREEACTIVE(&(pkd->pStore[i]))) {
fBall = pkd->pStore[i].fBallMax;
for (j=0;j<3;++j) {
if (pkd->pStore[i].r[j]-fBall < pbndBall->fMin[j])
pbndBall->fMin[j] = pkd->pStore[i].r[j]-fBall;
if (pkd->pStore[i].r[j]+fBall > pbndBall->fMax[j])
pbndBall->fMax[j] = pkd->pStore[i].r[j]+fBall;
}
}
}
}
void pkdGasWeight(PKD pkd)
{
PARTICLE *p;
int i;
for(i=0;i<pkdLocal(pkd);++i) {
p = &pkd->pStore[i];
if (TYPETest( p, TYPE_GAS )) p->fWeight=1.0;
else p->fWeight=0.0;
}
}
void pkdRungDDWeight(PKD pkd, int iMaxRung, double dWeight)
{
PARTICLE *p;
int i;
float fRungWeight[50],sum;
mdlassert(pkd->mdl,iMaxRung<50);
fRungWeight[0]=1.0;
sum=1.0;
for (i=1;i<=iMaxRung;i++) {
sum*=2.0;
fRungWeight[i] = dWeight* sum + (1-dWeight);
}
for(i=0;i<pkdLocal(pkd);++i) {
p = &pkd->pStore[i];
p->fWeight *= fRungWeight[p->iRung];
}
}
/*
** Partition particles between iFrom and iTo into those < fSplit and
** those >= to fSplit. Find number and weight in each partition.
*/
int pkdWeight(PKD pkd,int d,FLOAT fSplit,int iSplitSide,int iFrom,int iTo,
int *pnLow,int *pnHigh,FLOAT *pfLow,FLOAT *pfHigh)
{
int i,iPart;
FLOAT fLower,fUpper;
/*
** First partition the memory about fSplit for particles iFrom to iTo.
*/
if (iSplitSide) {
iPart = pkdLowerPart(pkd,d,fSplit,iFrom,iTo);
*pnLow = pkdLocal(pkd)-iPart;
*pnHigh = iPart;
}
else {
iPart = pkdUpperPart(pkd,d,fSplit,iFrom,iTo);
*pnLow = iPart;
*pnHigh = pkdLocal(pkd)-iPart;
}
/*
** Calculate the lower weight and upper weight BETWEEN the particles
** iFrom to iTo!
*/
fLower = 0.0;
for (i=iFrom;i<iPart;++i) {
fLower += pkd->pStore[i].fWeight;
}
fUpper = 0.0;
for (i=iPart;i<=iTo;++i) {
fUpper += pkd->pStore[i].fWeight;
}
if (iSplitSide) {
*pfLow = fUpper;
*pfHigh = fLower;
}
else {
*pfLow = fLower;
*pfHigh = fUpper;
}
return(iPart);
}
/*
** Partition particles between iFrom and iTo into those < fSplit and
** those >= to fSplit. Find number and weight in each partition.
*/
int pkdWeightWrap(PKD pkd,int d,FLOAT fSplit,FLOAT fSplit2, int iSplitSide,int iFrom,int iTo,
int *pnLow,int *pnHigh,FLOAT *pfLow,FLOAT *pfHigh)
{
int i,iPart;
FLOAT fLower,fUpper;
/*
** First partition the memory about fSplit for particles iFrom to iTo.
*/