-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_trees_to_lhalo.c
1328 lines (1137 loc) · 59.1 KB
/
convert_trees_to_lhalo.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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <float.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "convert_trees_to_lhalo.h"
#include "sglib.h"
#include "progressbar.h"
#ifdef USE_STRINGPARSE
#include "stringparse.h"
#include "check_syscalls.h"
#endif
/* #define FIELD_CHECKER(field, Nmax, fieldname) (XASSERT((field == -1) || (field >=0 && field < Nmax), fieldname " = %"PRId64" must be " */
int CTREES_UPID_FEATURE = 0;
int64_t read_forests(const char *filename, const char *output_dir, int64_t **f, int64_t **t)
{
char forest_bin_file[MAXLEN];
my_snprintf(forest_bin_file, MAXLEN, "%s/forests.bin", output_dir);
FILE *forests_fp = fopen(forest_bin_file,"r");
int64_t ntrees;
if(forests_fp == NULL) {
char buffer[MAXBUFSIZE];
const char comment = '#';
/* By passing the comment character, getnumlines
will return the actual number of lines, ignoring
the first header line.
*/
ntrees = getnumlines(filename, comment);
*f = my_malloc(sizeof(int64_t), ntrees);
*t = my_malloc(sizeof(int64_t), ntrees);
int64_t *forests = *f;
int64_t *tree_roots = *t;
int64_t ntrees_found = 0;
FILE *fp = my_fopen(filename, "r");
while(fgets(buffer, MAXBUFSIZE, fp) != NULL) {
if(buffer[0] == comment) {
continue;
} else {
const int nitems_expected = 2;
XASSERT(ntrees_found < ntrees,
"ntrees=%"PRId64" should be less than ntrees_found=%"PRId64"\n", ntrees, ntrees_found);
int nitems = sscanf(buffer, "%"SCNd64" %"SCNd64, &(tree_roots[ntrees_found]), &(forests[ntrees_found]));
XASSERT(nitems == nitems_expected,
"Expected to parse %d long integers but found `%s' in the buffer. nitems = %d \n",
nitems_expected, buffer, nitems);
ntrees_found++;
}
}
XASSERT(ntrees == ntrees_found,
"ntrees=%"PRId64" should be equal to ntrees_found=%"PRId64"\n", ntrees, ntrees_found);
fclose(fp);
/* Output the forests in binary */
forests_fp = my_fopen(forest_bin_file,"w");
size_t dummy = sizeof(*forests);
my_fwrite(&dummy, sizeof(dummy), 1, forests_fp);
my_fwrite(&ntrees, sizeof(ntrees), 1, forests_fp);
my_fwrite(forests, sizeof(*forests), ntrees, forests_fp);
my_fwrite(tree_roots, sizeof(*tree_roots), ntrees, forests_fp);
fclose(forests_fp);
} else {
/*Found the binary forests file -> read it in */
size_t dummy;
my_fread(&dummy, sizeof(dummy), 1, forests_fp);
my_fread(&ntrees, sizeof(ntrees), 1, forests_fp);
assert(ntrees >= 0);
*f = my_malloc(sizeof(int64_t), ntrees);
*t = my_malloc(sizeof(int64_t), ntrees);
int64_t *forests = *f;
int64_t *tree_roots = *t;
assert(dummy == sizeof(*forests));
my_fread(forests, sizeof(*forests), ntrees, forests_fp);
my_fread(tree_roots, sizeof(*tree_roots), ntrees, forests_fp);
fclose(forests_fp);
}
return ntrees;
}
int64_t read_locations(const char *filename, const int64_t ntrees, struct locations *l, int64_t *num_files, int64_t *box_div)
{
char buffer[MAXBUFSIZE];
int64_t max_fileid = 0;
const char comment = '#';
/* By passing the comment character, getnumlines
will return the actual number of lines, ignoring
the first header line.
*/
struct locations *locations = l;
int64_t ntrees_found = 0;
FILE *fp = my_fopen(filename, "r");
while(fgets(buffer, MAXBUFSIZE, fp) != NULL) {
if(buffer[0] == comment) {
continue;
} else {
const int nitems_expected = 4;
char linebuf[MAXLEN];
XASSERT(ntrees_found < ntrees,
"ntrees=%"PRId64" should be less than ntrees_found=%"PRId64"\n",
ntrees, ntrees_found);
int nitems = sscanf(buffer, "%"SCNd64" %"SCNd64 " %"SCNd64 "%s", &(locations[ntrees_found].tree_root),
&(locations[ntrees_found].fileid), &(locations[ntrees_found].offset), linebuf);
XASSERT(locations[ntrees_found].offset >= 0,
"offset=%"PRId64" for ntree =%"PRId64" must be positive.\nFile = `%s'\nbuffer = `%s'\n",
locations[ntrees_found].offset,ntrees_found,filename, buffer);
/* The filename is separated out to save memory but I want to ensure that the actual filename does
not get truncated. The filename field might actually be removed later. */
my_snprintf(locations[ntrees_found].filename, LOCATIONS_FILENAME_SIZE, "%s", linebuf);
XASSERT(nitems == nitems_expected,
"Expected to parse two long integers but found `%s' in the buffer\n",
buffer);
ntrees_found++;
}
}
XASSERT(ntrees == ntrees_found, "ntrees=%"PRId64" should be equal to ntrees_found=%"PRId64"\n", ntrees, ntrees_found);
fclose(fp);
for(int64_t i=0;i<ntrees_found;i++){
if (locations[i].fileid > max_fileid) {
max_fileid = locations[i].fileid;
}
}
/* number of files is one greater from 0 based indexing of C files */
*num_files = max_fileid + 1;
const int box_divisions = (int) round(cbrt(*num_files));
const int box_cube = box_divisions * box_divisions * box_divisions;
XASSERT( (box_cube) == (*num_files),
"box_divisions^3=%d should be equal to nfiles=%"PRId64"\n",
box_cube, *num_files);
*box_div = box_divisions;
return ntrees_found;
}
void sort_forests(const int64_t ntrees, int64_t *forests, int64_t *tree_roots)
{
#define MULTIPLE_ARRAY_EXCHANGER(type,a,i,j) { SGLIB_ARRAY_ELEMENTS_EXCHANGER(int64_t, tree_roots,i,j); \
SGLIB_ARRAY_ELEMENTS_EXCHANGER(int64_t, forests, i, j) }
SGLIB_ARRAY_QUICK_SORT(int64_t, tree_roots, ntrees, SGLIB_NUMERIC_COMPARATOR , MULTIPLE_ARRAY_EXCHANGER);
#undef MULTIPLE_ARRAY_EXCHANGER
}
int compare_locations_tree_roots(const void *l1, const void *l2)
{
const struct locations *aa = (const struct locations *) l1;
const struct locations *bb = (const struct locations *) l2;
return (aa->tree_root < bb->tree_root) ? -1:1;
}
int compare_locations_fid(const void *l1, const void *l2)
{
const struct locations *aa = (const struct locations *) l1;
const struct locations *bb = (const struct locations *) l2;
return (aa->forestid < bb->forestid) ? -1:1;
}
int compare_locations_file_offset(const void *l1, const void *l2)
{
const struct locations *aa = (const struct locations *) l1;
const struct locations *bb = (const struct locations *) l2;
const int file_id_cmp = (aa->fileid == bb->fileid) ? 0:((aa->fileid < bb->fileid) ? -1:1);
if(file_id_cmp == 0) {
/* trees are in same file -> sort by offset */
return (aa->offset < bb->offset) ? -1:1;
} else {
return file_id_cmp;
}
return 0;
}
int compare_locations_fid_file_offset(const void *l1, const void *l2)
{
const struct locations *aa = (const struct locations *) l1;
const struct locations *bb = (const struct locations *) l2;
if(aa->forestid != bb->forestid) {
return (aa->forestid < bb->forestid) ? -1:1;
} else {
/* The trees are in the same forest. Check filename */
const int file_id_cmp = (aa->fileid == bb->fileid) ? 0:((aa->fileid < bb->fileid) ? -1:1);
if(file_id_cmp == 0) {
/* trees are in same file -> sort by offset */
return (aa->offset < bb->offset) ? -1:1;
} else {
return file_id_cmp;
}
}
return 0;
}
void sort_locations_on_treeroot(const int64_t ntrees, struct locations *locations)
{
qsort(locations, ntrees, sizeof(*locations), compare_locations_tree_roots);
}
void sort_locations_file_offset(const int64_t ntrees, struct locations *locations)
{
qsort(locations, ntrees, sizeof(*locations), compare_locations_file_offset);
}
void sort_locations_on_fid(const int64_t ntrees, struct locations *locations)
{
qsort(locations, ntrees, sizeof(*locations), compare_locations_fid);
}
void sort_locations_on_fid_file_offset(const int64_t ntrees, struct locations *locations)
{
qsort(locations, ntrees, sizeof(*locations), compare_locations_fid_file_offset);
}
void assign_forest_ids(const int64_t ntrees, struct locations *locations, int64_t *forests, int64_t *tree_roots)
{
/* Sort forests by tree roots -> necessary for assigning forest ids */
sort_forests(ntrees, forests, tree_roots);
sort_locations_on_treeroot(ntrees, locations);
/* forests and tree_roots are sorted together, on tree_roots */
/* locations is sorted on tree roots */
for(int64_t i=0;i<ntrees;i++) {
XASSERT(tree_roots[i] == locations[i].tree_root,
"tree roots[%"PRId64"] = %"PRId64" does not equal tree roots in locations = %"PRId64"\n",
i, tree_roots[i], locations[i].tree_root);
locations[i].forestid = forests[i];
}
}
struct forest_info * assign_trees_in_forest_to_same_file(const int64_t ntrees, struct locations *locations, struct locations *output_locations, const int64_t nfiles)
{
sort_locations_on_fid_file_offset(ntrees, locations);
sort_locations_on_fid_file_offset(ntrees, output_locations);
int64_t nforests = ntrees;/* Maximum value the nforests can have */
struct forest_info *forest_info = my_calloc(sizeof(struct forest_info), 1);
forest_info->nforests = 1;
forest_info->forestid = my_calloc(sizeof(*(forest_info->forestid)), nforests);
forest_info->fileid = my_calloc(sizeof(*(forest_info->fileid)), nforests);
forest_info->num_trees = my_calloc(sizeof(*(forest_info->num_trees)), nforests);
forest_info->num_halos = my_calloc(sizeof(*(forest_info->num_halos)), nforests);
forest_info->num_ascii_bytes = my_calloc(sizeof(*(forest_info->num_ascii_bytes)), nforests);
forest_info->num_binary_bytes = my_calloc(sizeof(*(forest_info->num_binary_bytes)), nforests);
forest_info->offset = my_calloc(sizeof(*(forest_info->offset)), nforests);
forest_info->filename = my_calloc(sizeof(*(forest_info->filename)), nforests);
int64_t *histogram_fileids = my_calloc(sizeof(*histogram_fileids), nfiles);
/* the fun begins here -> in case, assign all trees from a forest into the same file */
int64_t start_forestid = locations[0].forestid;
int64_t min_fileid = locations[0].fileid;
int64_t max_fileid = locations[0].fileid;
int64_t start_index_forest = 0;
int64_t end_index_forest = 1;
int64_t num_trees_moved = 0;
fprintf(stderr, ANSI_COLOR_MAGENTA"Assigning all trees in a forest into the same file...."ANSI_COLOR_RESET"\n");
/* setup the progressbar */
int interrupted=0;
init_my_progressbar(ntrees, &interrupted);
int64_t forest_index = 0;
forest_info->forestid[forest_index] = start_forestid;
forest_info->num_trees[forest_index] = 1;
for(int64_t i=1;i<ntrees;i++) {
my_progressbar(i, &interrupted);
if(locations[i].forestid == start_forestid) {
nforests--;
forest_info->num_trees[forest_index]++;
if(locations[i].fileid < min_fileid) {
min_fileid = locations[i].fileid;
}
if(locations[i].fileid > min_fileid) {
max_fileid = locations[i].fileid;
}
end_index_forest++;
continue;
} else {
int64_t max_common_fileid = -1;
if(min_fileid == max_fileid) {
for(int64_t j=start_index_forest;j<end_index_forest;j++) {
output_locations[j].fileid = min_fileid;
}
max_common_fileid = min_fileid;
} else {
/* fprintf(stderr,"For forest id = %"PRId64" trees are stored in separate files (min, max) = (%"PRId64", %"PRId64")\n", */
/* start_forestid, min_fileid, max_fileid); */
/* interrupted=1; */
/* create a histogram of the fileids */
memset(histogram_fileids, 0, sizeof(*histogram_fileids) * nfiles);
for(int64_t j=start_index_forest;j<end_index_forest;j++) {
histogram_fileids[locations[j].fileid]++;
}
int64_t max_common_value = 0;
for(int64_t j=0;j<nfiles;j++) {
if(histogram_fileids[j] > max_common_value) {
max_common_value = histogram_fileids[j];
max_common_fileid = j;
}
}
for(int64_t j=start_index_forest;j<end_index_forest;j++) {
output_locations[j].fileid = max_common_fileid;
if(output_locations[j].fileid != locations[j].fileid) {
num_trees_moved++;
/* fprintf(stderr,"Moved tree = %10"PRId64" from fileid=%3"PRId64" to fileid=%3"PRId64"\n",locations[j].tree_root, locations[j].fileid, output_locations[j].fileid); */
/* interrupted=1; */
}
}
}
/* Update the forest_info */
XASSERT((max_common_fileid >=0 && max_common_fileid < nfiles),
"max common fileid = %"PRId64" is not within bounds [0,%"PRId64")\n",
max_common_fileid, nfiles);
forest_info->fileid[forest_index] = max_common_fileid;
for(int64_t j=start_index_forest;j<end_index_forest;j++) {
forest_info->num_ascii_bytes[forest_index] += locations[j].bytes;
}
/* We have a new forest on the current line */
forest_index++;
forest_info->nforests++;
start_forestid = locations[i].forestid;
forest_info->num_trees[forest_index] = 1;
forest_info->forestid[forest_index] = start_forestid;
start_index_forest = i;
end_index_forest = i+1;
min_fileid = locations[i].fileid;
max_fileid = locations[i].fileid;
}
}
finish_myprogressbar(&interrupted);
free(histogram_fileids);
if(num_trees_moved > 0) {
fprintf(stderr,"Number of trees moved into different files = %"PRId64"\n",num_trees_moved);
}
fprintf(stderr, ANSI_COLOR_GREEN"Assigning all trees in a forest into the same file.......done"ANSI_COLOR_RESET"\n\n");
XASSERT(forest_info->nforests == nforests,
"forest_info->nforests = %"PRId64" must equal nforests = %"PRId64"\n",
forest_info->nforests, nforests);
/* free the extra memory claimed by forest_info */
forest_info->forestid = my_realloc(forest_info->forestid, sizeof(*(forest_info->forestid)), nforests, "struct forest_info (forestid)");
forest_info->fileid = my_realloc(forest_info->fileid, sizeof(*(forest_info->fileid)), nforests, "struct forest_info (fileid)");
forest_info->num_trees = my_realloc(forest_info->num_trees, sizeof(*(forest_info->num_trees)), nforests, "struct forest_info (num_trees)");
forest_info->num_halos = my_realloc(forest_info->num_halos, sizeof(*(forest_info->num_halos)), nforests, "struct forest_info (num_halos)");
forest_info->num_ascii_bytes = my_realloc(forest_info->num_ascii_bytes, sizeof(*(forest_info->num_ascii_bytes)), nforests, "struct forest_info (num_ascii_bytes)");
forest_info->num_binary_bytes = my_realloc(forest_info->num_binary_bytes, sizeof(*(forest_info->num_binary_bytes)), nforests, "struct forest_info (num_binary_bytes)");
forest_info->offset = my_realloc(forest_info->offset, sizeof(*(forest_info->offset)), nforests, "struct forest_info (offset)");
forest_info->filename = my_realloc(forest_info->filename, sizeof(*(forest_info->filename)), nforests, "struct forest_info (filename)");
XASSERT(sizeof(*(forest_info->filename)) > 8,
"size of filename holder = %zu must be larger than a pointer size",
sizeof(*(forest_info->filename)));
return forest_info;
}
int64_t read_tree_into_forest(int64_t *nhalos_allocated, struct output_dtype **source_forest, const int64_t forest_offset,
struct additional_info **source_info,
#ifdef USE_FGETS
FILE *fp,
int64_t offset,
#else
int fd,
off_t offset,
#endif
const size_t bytes,
const float inv_part_mass)
{
int64_t nhalos = 0;
size_t bytes_read = 0;
#ifdef USE_FGETS
my_fseek(fp, offset, SEEK_SET);
#endif
while(bytes_read < bytes) {
char buffer[MAXLEN+1]={'\0'};
const int64_t bytes_to_read = (bytes - bytes_read) > MAXLEN ? MAXLEN:(bytes - bytes_read);
#if 0
fprintf(stderr,"NEED TO READ %zu BYTES bytes_read = %zu bytes_to_read = %"PRId64"\n", bytes, bytes_read, bytes_to_read);
#endif
#ifdef USE_FGETS
#error FGETS does not work yet
char *ret = fgets(buffer, bytes_to_read, fp);
my_fread(buffer, sizeof(char), bytes_to_read, fp);
const int64_t bytes_this_read = (ret == NULL) ? 0:(int64_t) strlen(buffer);
#else
int64_t bytes_this_read = (int64_t) pread(fd, buffer, bytes_to_read, offset);
if(buffer[bytes_this_read-1] != '\n') {
for(int64_t i=bytes_this_read-2;i>=0;i--) {
if(buffer[i] == '\n') {
buffer[i+1] = '\0';
break;
}
bytes_this_read = i;
}
}
/* fprintf(stderr,"bytes_this_read = %"PRId64" len = %zu\n",bytes_this_read, strlen(buffer)); */
/* offset += bytes_this_read; */
#endif
#if 0
fprintf(stderr,"STARTING with NEW BUFFER. buffer = `%s'\n+++++++++++++++++++++++++++\n",buffer);
#endif
int64_t start_pos=0;
while(start_pos < bytes_this_read) {
int64_t end_pos=start_pos;
while(end_pos < bytes_this_read && buffer[end_pos] != '\n') {
end_pos++;
}
/* blank line ?*/
if(end_pos == start_pos || (end_pos == start_pos + 1) ) {
start_pos = end_pos + 1;
continue;
}
if(end_pos < bytes_this_read) {
XASSERT(buffer[end_pos] == '\n',"buffer[%"PRId64"] = `%c' is not a new-line\n",
end_pos, buffer[end_pos]);
}
buffer[end_pos] = '\0';
#if 0
fprintf(stderr,"PARSING LINE = `%s'\n******\n",&buffer[start_pos]);
#endif
/* found a new-line but before parsing check that enough memory has been allocated */
if((forest_offset+nhalos) >= *nhalos_allocated) {
*nhalos_allocated += 100000;
*source_forest = my_realloc(*source_forest, sizeof(**source_forest), *nhalos_allocated, "struct source forest");
*source_info = my_realloc(*source_info, sizeof(**source_info), *nhalos_allocated, "struct source info");
}
struct output_dtype *forest = *source_forest + forest_offset;
struct additional_info *info = *source_info + forest_offset;
const int nitems_expected = 21;
#ifndef USE_STRINGPARSE
const int nitems = sscanf(&buffer[start_pos],
"%lf %"SCNd64" %lf %"SCNd64" %*d "
"%"SCNd64" %"SCNd64" %*d %*d "
/*the first two fields are sam_mvir and mvir. one of the two need to be assigned to Mvir.
I have chosen Mvir from halofinder (second column in "%*f %f") */
"%*f %f %*f %*f %f "
"%*d %*f %f "
"%f %f %f "
"%f %f %f "
"%f %f %f "
"%*f %*d %*d %*d %*d %d "
"%*d %*d %*f %*f %*f %f %f ",
&info[nhalos].scale,
&info[nhalos].id,
&info[nhalos].desc_scale,
&info[nhalos].descid,
&info[nhalos].pid,
&info[nhalos].upid,
&forest[nhalos].Mvir,
&forest[nhalos].VelDisp,
&forest[nhalos].Vmax,
&(forest[nhalos].Pos[0]), &(forest[nhalos].Pos[1]), &(forest[nhalos].Pos[2]),
&(forest[nhalos].Vel[0]), &(forest[nhalos].Vel[1]), &(forest[nhalos].Vel[2]),
&(forest[nhalos].Spin[0]), &(forest[nhalos].Spin[1]), &(forest[nhalos].Spin[2]),
&forest[nhalos].SnapNum,
&forest[nhalos].M_Mean200, &forest[nhalos].M_TopHat);
#else
buffer[end_pos] = '\n';
//Use Peter Behroozi's custom string parser.
SHORT_PARSETYPE;
#define NUM_INPUTS 39
enum short_parsetype stypes[NUM_INPUTS] = {
LF, LD, LF, LD, K,
LD, LD, K, K,
K, F, K, K, F,
K, K, F,
F, F, F,
F, F, F,
F, F, F,
K, K, K, K, K, D,
K, K, K, K, K, F, F,
};
enum parsetype types[NUM_INPUTS];
for (int ii=0; ii<NUM_INPUTS; ii++) types[ii] = stypes[ii];
void *data[NUM_INPUTS] = {&(info[nhalos].scale),
&(info[nhalos].id),
&(info[nhalos].desc_scale),
&(info[nhalos].descid),
NULL,
&(info[nhalos].pid),
&(info[nhalos].upid),
NULL,
NULL,
NULL,
&(forest[nhalos].Mvir),
NULL,
NULL,
&(forest[nhalos].VelDisp),
NULL,
NULL,
&(forest[nhalos].Vmax),
&(forest[nhalos].Pos[0]), &(forest[nhalos].Pos[1]), &(forest[nhalos].Pos[2]),
&(forest[nhalos].Vel[0]), &(forest[nhalos].Vel[1]), &(forest[nhalos].Vel[2]),
&(forest[nhalos].Spin[0]), &(forest[nhalos].Spin[1]), &(forest[nhalos].Spin[2]),
NULL,
NULL,
NULL,
NULL,
NULL,
&(forest[nhalos].SnapNum),
NULL,
NULL,
NULL,
NULL,
NULL,
&(forest[nhalos].M_Mean200),
&(forest[nhalos].M_TopHat),
};
//parse string
//stringparse returns int64_t. to be consistent with the previous
//section I made nitems as an int
int nitems = (int) stringparse(&buffer[start_pos], data, (enum parsetype *)types, NUM_INPUTS);
if (nitems == NUM_INPUTS) {
nitems = nitems_expected;
}
buffer[end_pos] = '\0';
#undef NUM_INPUTS
#endif
XASSERT(nitems == nitems_expected,
ANSI_COLOR_RED"could not parse buffer = `%s' correctly. expected = %d found = %d\n"
"start_pos = %"PRId64" end_pos = %"PRId64"\n"
"Try deleting the locations.bin and forests.bin files in the output directory and re-running the code\n",
&buffer[start_pos], nitems_expected, nitems,start_pos, end_pos);
/* correctly parsed a halo. Fix things into LHalotree convention */
const float inv_halo_mass = 1.0f/forest[nhalos].Mvir;
for(int k=0;k<3;k++) {
forest[nhalos].Spin[k] *= inv_halo_mass;
}
/* Convert masses to 10^10 Msun/h */
forest[nhalos].Mvir *= 1e-10;
forest[nhalos].M_Mean200 *= 1e-10;
forest[nhalos].M_TopHat *= 1e-10;
/* Calculate the (approx.) number of particles in this halo */
forest[nhalos].Len = (int) roundf(forest[nhalos].Mvir * inv_part_mass);
/* Initialize other fields to indicate they are not populated */
forest[nhalos].FileNr = -1;
forest[nhalos].SubhaloIndex = (int) (forest_offset + nhalos);
forest[nhalos].SubHalfMass = -1.0f;
/* Carry the Rockstar/Ctrees generated haloID through */
forest[nhalos].MostBoundID = info[nhalos].id;
/* All the mergertree indices */
forest[nhalos].Descendant = -1;
forest[nhalos].FirstProgenitor = -1;
forest[nhalos].NextProgenitor = -1;
forest[nhalos].FirstHaloInFOFgroup = -1;
forest[nhalos].NextHaloInFOFgroup = -1;
/* Convert the snapshot index output by Consistent Trees
into the snapshot number as reported by the simulation */
forest[nhalos].SnapNum += SNAP_OFFSET;
#if 0
if(num_lines_printed < 10000) {
fprintf(stdout,"MY PARSING produces: "
"%lf %"PRId64" %lf %"PRId64" "
"%"PRId64" %"PRId64" "
"%f %f "
"%f "
"%f %f %f "
"%f %f %f "
"%f %f %f "
"%d "
"%f %f \n",
info[nhalos].scale,
info[nhalos].id,
info[nhalos].desc_scale,
info[nhalos].descid,
info[nhalos].pid,
info[nhalos].upid,
forest[nhalos].Mvir,
forest[nhalos].VelDisp,
forest[nhalos].Vmax,
forest[nhalos].Pos[0], forest[nhalos].Pos[1], forest[nhalos].Pos[2],
forest[nhalos].Vel[0], forest[nhalos].Vel[1], forest[nhalos].Vel[2],
forest[nhalos].Spin[0], forest[nhalos].Spin[1], forest[nhalos].Spin[2],
forest[nhalos].SnapNum,
forest[nhalos].M_Mean200, forest[nhalos].M_TopHat);
num_lines_printed++;
}
#endif
#ifndef USE_FGETS
offset += (end_pos - start_pos + 1);
bytes_read += (end_pos - start_pos + 1);
#endif
nhalos++;
start_pos = end_pos + 1;
#if 0
fprintf(stderr,"start_pos = %"PRId64"\n",start_pos);
#endif
}
}
return nhalos;
}
int64_t compute_numbytes_with_off(const int64_t off, const int64_t start)
{
return off - start; /* Or should there be a -1?*/
}
int64_t compute_numbytes(FILE *fp, const int64_t start)
{
const int64_t off = ftello(fp);
return compute_numbytes_with_off(off, start);
}
int64_t write_forests_and_locations(const char *filename, const int64_t ntrees, const struct locations *locations)
{
int64_t bytes = 0;
FILE *fp = my_fopen(filename,"w");
bytes += fprintf(fp,"############## Value-Added Consistent-Trees locations.dat file. #####################\n");
bytes += fprintf(fp,"## ForestID TreeRootID FileID Offset Filename Bytes \n");
bytes += fprintf(fp,"#####################################################################################\n");
for(int64_t i=0;i<ntrees;i++) {
bytes += fprintf(fp," %13"PRId64" %13"PRId64" %6"PRId64" %13"PRId64" %s %16"PRId64"\n",
locations[i].forestid, locations[i].tree_root, locations[i].fileid,
locations[i].offset, locations[i].filename, locations[i].bytes);
}
fclose(fp);
return bytes;
}
int64_t find_fof_halo(const int64_t totnhalos, const struct additional_info *info, const int start_loc, const int64_t upid, int verbose, int64_t calldepth)
{
XASSERT(totnhalos < INT_MAX,
"Totnhalos must be less than %d. Otherwise indexing with int (start_loc) will break\n", INT_MAX);
int64_t loc = -1;
assert(info[start_loc].pid != -1);
if(calldepth >= 3) {
verbose = 1;
}
assert(calldepth <= 30);
while(start_loc >= 0 && start_loc < totnhalos && info[start_loc].pid != -1) {
/* if(info[start_loc].id == 3058456321) { */
/* fprintf(stderr,"info[%d].id = 3058456321. pid = %"PRId64" upid = %"PRId64"\n", */
/* start_loc, info[start_loc].pid, info[start_loc].upid); */
/* } */
/* if(verbose == 1) { */
/* fprintf(stderr,"start_loc = %d id = %"PRId64" pid = %"PRId64"\n", start_loc, info[start_loc].id, info[start_loc].pid); */
/* fprintf(stderr,"scale = %lf pid = %"PRId64" upid = %"PRId64"\n", */
/* info[start_loc].scale, info[start_loc].pid, info[start_loc].upid); */
/* } */
if(upid > info[start_loc].id) {
for(int64_t k=start_loc+1;k<totnhalos;k++) {
if(info[k].id == upid) {
loc = k;
break;
}
}
} else {
for(int64_t k=start_loc-1;k>=0;k--) {
if(info[k].id == upid) {
loc = k;
break;
}
}
}
if( ! (loc >= 0 && loc < totnhalos) ) {
return -1;
}
if(info[loc].pid == -1) {
return loc;
} else {
/* if(verbose == 1) { */
/* fprintf(stderr,"calling find_fof_halo again with loc =%"PRId64" (int) loc = %d start_loc was =%d\n",loc, (int) loc, start_loc); */
/* fprintf(stderr,"scale = %lf id = %"PRId64" pid = %"PRId64" upid = %"PRId64" calldepth=%"PRId64"\n", */
/* info[loc].scale, info[loc].id, info[loc].pid, info[loc].upid,calldepth); */
/* } */
calldepth++;
return find_fof_halo(totnhalos, info, (int) loc, info[loc].upid, verbose, calldepth);
}
}
return -1;
}
int fix_upid(const int64_t totnhalos, struct output_dtype *forest, struct additional_info *info, int *interrupted, const int verbose)
{
int max_snapnum = -1;
/*First sort everything on ID */
#define ID_COMPARATOR(x, y) ((x.id > y.id ? 1:(x.id < y.id ? -1:0)))
#define SCALE_ID_COMPARATOR(x,y) ((x.scale > y.scale ? -1:(x.scale < y.scale ? 1:ID_COMPARATOR(x, y))) )
#define MULTIPLE_ARRAY_EXCHANGER(type,a,i,j) { \
SGLIB_ARRAY_ELEMENTS_EXCHANGER(struct output_dtype, forest,i,j); \
SGLIB_ARRAY_ELEMENTS_EXCHANGER(struct additional_info, info, i, j) \
}
SGLIB_ARRAY_HEAP_SORT(struct additional_info, info, totnhalos, SCALE_ID_COMPARATOR, MULTIPLE_ARRAY_EXCHANGER);
/* Change upid to id, so we can sort the fof's and subs to be contiguous */
//I am paranoid -> so I am going to set all FOF upid's first and then
//use the upid again. Two loops are required but that relaxes any assumptions
//about ordering of fof/subhalos.
for(int64_t i=0;i<totnhalos;i++) {
info[i].upid = (info[i].pid == -1) ? info[i].id:info[i].upid;
if(forest[i].SnapNum > max_snapnum) {
max_snapnum = forest[i].SnapNum;
}
}
for(int64_t i=0;i<totnhalos;i++) {
if(info[i].pid == -1) {
continue;
}
/* Only (sub)subhalos should reach here */
/*Check if upid points to host halo with pid == -1*/
const int64_t upid = info[i].upid;
int64_t calldepth=0;
/* fprintf(stderr,"CALLING FIND FOF HALO with i = %"PRId64" id = %"PRId64" upid = %"PRId64"\n", i, info[i].id, upid); */
const int64_t loc = find_fof_halo(totnhalos, info, i, upid, verbose, calldepth);
XASSERT(loc >=0 && loc < totnhalos,
"could not locate fof halo for i = %"PRId64" id = %"PRId64" upid = %"PRId64" loc=%"PRId64"\n",
i, info[i].id, upid, loc);
const int64_t new_upid = info[loc].id;
if(new_upid != upid && CTREES_UPID_FEATURE == 0) {
fprintf(stderr, ANSI_COLOR_RED "Fixing upid for i=%"PRId64" original upid =%"PRId64" new fof upid = %"PRId64 ANSI_COLOR_RESET"\n",
i, upid, new_upid);
CTREES_UPID_FEATURE = 1;
*interrupted = 1;
}
info[i].upid = new_upid;
info[i].pid = new_upid;
}
#undef ID_COMPARATOR
#undef SCALE_ID_COMPARATOR
#undef MULTIPLE_ARRAY_EXCHANGER
return max_snapnum;
}
void assign_mergertree_indices(const int64_t totnhalos, struct output_dtype *forest, struct additional_info *info, const int max_snapnum)
{
/* fprintf(stderr,"IN MERGERTREE totnhalos = %"PRId64"\n",totnhalos); */
const int nsnapshots = max_snapnum + 1;
double *scales = my_malloc(sizeof(*scales), nsnapshots);
for(int i=0;i<nsnapshots;i++) {
scales[i] = DBL_MAX;
}
int64_t *start_scale = my_calloc(sizeof(*start_scale), nsnapshots);
for(int i=0;i<nsnapshots;i++) {
start_scale[i] = -1;
}
int64_t *end_scale = my_calloc(sizeof(*end_scale), nsnapshots);
/* Sort the trees based on scale, upid, and pid */
/* Descending sort on scale, and then ascending sort on upid.
The pid sort is so that the FOF halo comes before the (sub-)subhalos.
The last id sort is such that the ordering of (sub-)subhalos
is unique (stable sort, since id's are unique)
*/
#define ID_COMPARATOR(x, y) ((x.id > y.id ? 1:(x.id < y.id ? -1: 0)))
#define PID_COMPARATOR(x, y) ((x.pid > y.pid ? 1:(x.pid < y.pid ? -1:ID_COMPARATOR(x,y))))
#define UPID_COMPARATOR(x, y) ((x.upid > y.upid ? 1:(x.upid < y.upid ? -1:PID_COMPARATOR(x,y))))
/* Note, the negated order in the scale comparison . this ensures descending sort */
#define SCALE_UPID_COMPARATOR(x,y) ((x.scale > y.scale ? -1:(x.scale < y.scale ? 1:UPID_COMPARATOR(x, y))) )
#define MULTIPLE_ARRAY_EXCHANGER(type,a,i,j) { \
SGLIB_ARRAY_ELEMENTS_EXCHANGER(struct output_dtype, forest,i,j); \
SGLIB_ARRAY_ELEMENTS_EXCHANGER(struct additional_info, info, i, j) \
}
/* I am using heap sort rather than qsort because the forest is already somewhat sorted. For sorted data,
qsort behaves closer to O(N^2).
*/
SGLIB_ARRAY_HEAP_SORT(struct additional_info, info, totnhalos, SCALE_UPID_COMPARATOR, MULTIPLE_ARRAY_EXCHANGER);
/* Fix subs of subs first */
int64_t FirstHaloInFOFgroup=-1;
int64_t fof_id=-1;
for(int64_t i=0;i<totnhalos;i++) {
/* fprintf(stderr,ANSI_COLOR_RED"FirstHaloInFOFgroup = %"PRId64 ANSI_COLOR_RESET"\n",FirstHaloInFOFgroup); */
const int snapnum = forest[i].SnapNum;
XASSERT(snapnum >= 0 && snapnum < nsnapshots,
"snapnum = %d is outside range [0, %d)\n",
snapnum, nsnapshots);
scales[snapnum] = info[i].scale;
end_scale[snapnum] = i;
if(start_scale[snapnum] == -1) {
start_scale[snapnum] = i;
}
if(info[i].pid == -1) {
XASSERT(i < INT_MAX,
"Assigning to integer i = %"PRId64" is more than %d\n",
i, INT_MAX);
forest[i].FirstHaloInFOFgroup = (int) i;
forest[i].NextHaloInFOFgroup = -1;
FirstHaloInFOFgroup = i;
fof_id = info[i].id;
continue;
} else {
if(FirstHaloInFOFgroup == -1) {
fprintf(stderr,"About to crash\n");
for(int64_t k=0;k<totnhalos;k++) {
fprintf(stderr,"%03d %12.5lf %10"PRId64" %10"PRId64" %10"PRId64" %12.4g\n",
forest[k].SnapNum, info[k].scale, info[k].upid, info[k].pid, info[k].id, forest[k].Mvir);
}
}
XASSERT(FirstHaloInFOFgroup != -1,
"Processing subhalos i=%"PRId64" but have not encountered FOF yet..bug\n"
"id = %"PRId64" pid = %"PRId64" upid = %"PRId64" snapnum = %d\n",
i,info[i].id, info[i].pid, info[i].upid, forest[i].SnapNum);
if(info[i].upid == fof_id) {
XASSERT(FirstHaloInFOFgroup < INT_MAX,
"Assigning FirstHaloInFOFgroup = %"PRId64". Must be less than %d\n",
FirstHaloInFOFgroup, INT_MAX);
forest[i].FirstHaloInFOFgroup = FirstHaloInFOFgroup;
} else {
/* Should not reach here..I have already sorted the forest such that the FOF appears before the subs */
fprintf(stderr,"ERROR: the sort did not place the FOF before the subs. BUG IN CTREES OR IN SORT\n");
for(int64_t k=0;k<totnhalos;k++) {
fprintf(stderr,"%03d %12.5lf %10"PRId64" %10"PRId64" %10"PRId64" %12.4g\n",
forest[k].SnapNum, info[k].scale, info[k].upid, info[k].pid, info[k].id, forest[k].Mvir);
}
fprintf(stderr,"i = %"PRId64" id = %"PRId64" pid = %"PRId64" fof_id = %"PRId64" upid = %"PRId64" FirstHaloInFOFgroup = %"PRId64"\n",
i, info[i].id, info[i].pid, fof_id, info[i].upid, FirstHaloInFOFgroup);
exit(EXIT_FAILURE);
}
int64_t insertion_point = FirstHaloInFOFgroup;
while(forest[insertion_point].NextHaloInFOFgroup != -1) {
const int64_t nexthalo = forest[insertion_point].NextHaloInFOFgroup;
XASSERT(nexthalo >=0 && nexthalo < totnhalos,
"Inserting next halo in FOF group into invalid index. nexthalo = %"PRId64" totnhalos = %"PRId64"\n",
nexthalo, totnhalos);
/* if(forest[nexthalo].Mvir < forest[i].Mvir) { */
/* forest[i].NextHaloInFOFgroup = nexthalo; */
/* break; */
/* } */
insertion_point = nexthalo;
}
XASSERT(i < INT_MAX,
"Assigning FirstHaloInFOFgroup = %"PRId64". Must be less than %d\n",
i, INT_MAX);
forest[insertion_point].NextHaloInFOFgroup = i;
}
}
/* Now figure out merger tree pointers. Need to set descendant, firstprogenitor and nextprogenitor.
*/
for(int64_t i=0;i<totnhalos;i++) {
if(info[i].descid == -1) {
forest[i].Descendant = -1;
continue;
}
int desc_snapnum = nsnapshots-1;
const double desc_scale = info[i].desc_scale;
const int64_t descid = info[i].descid;
const double max_epsilon_scale = 1.0e-4;
while((desc_snapnum >= 0) &&
(fabs(scales[desc_snapnum] - desc_scale) > max_epsilon_scale) ) {
desc_snapnum--;
}
XASSERT(desc_snapnum >= 0 && desc_snapnum < nsnapshots && (fabs(scales[desc_snapnum] - desc_scale) <= 1e-4),
"Could not locate desc_snapnum. desc_snapnum = %d nsnapshots = %d \n",
desc_snapnum, nsnapshots);
/*start_scale and end_scale are inclusive. Hence the stopping condition is "<=" rather than simply "<" */
int64_t desc_loc = start_scale[desc_snapnum];
while(desc_loc >= start_scale[desc_snapnum] && desc_loc <= end_scale[desc_snapnum] && info[desc_loc].id != descid) {
desc_loc++;
}
XASSERT(desc_loc >= start_scale[desc_snapnum] && desc_loc <= end_scale[desc_snapnum],
"Desc loc = %"PRId64" for snapnum = %d is outside range [%"PRId64", %"PRId64"]\n",
desc_loc, desc_snapnum, start_scale[desc_snapnum], end_scale[desc_snapnum]);
XASSERT(info[desc_loc].id == descid,
"Should have found descendant id = %"PRId64" but info[%"PRId64"]=%"PRId64" instead \n",
descid, desc_loc, info[desc_loc].id);
XASSERT(desc_loc < INT_MAX,
"desc_loc = %"PRId64" must be less than INT_MAX = %d\n",
desc_loc, INT_MAX);
forest[i].Descendant = desc_loc;
//Now assign first progenitor + next progenitor
if(forest[desc_loc].FirstProgenitor == -1) {
forest[desc_loc].FirstProgenitor = i;
forest[i].NextProgenitor = -1;
} else {
/* The descendant halo already has progenitors. Figure out the correct
order -- should this halo be FirstProgenitor?
Not necessary but ensure nextprog are ordered by mass.
*/
const int first_prog = forest[desc_loc].FirstProgenitor;
XASSERT(first_prog >= 0 && first_prog < totnhalos,
"first_prog=%d must lie within [0, %"PRId64"\n",
first_prog, totnhalos);
if(forest[first_prog].Mvir < forest[i].Mvir) {
XASSERT(i < INT_MAX,
"Assigning Nextprogenitor = %"PRId64" to an int will result in garbage. INT_MAX = %d\n",
i, INT_MAX);
forest[desc_loc].FirstProgenitor = i;
forest[i].NextProgenitor = first_prog;
} else {
int64_t insertion_point = first_prog;
while(forest[insertion_point].NextProgenitor != -1) {
const int64_t next_prog = forest[insertion_point].NextProgenitor;
XASSERT(next_prog >=0 && next_prog < totnhalos,
"Inserting next progenitor into invalid index. insertion_point = %"PRId64" totnhalos = %"PRId64"\n",
next_prog, totnhalos);
/* if(forest[next_prog].Mvir < forest[i].Mvir) { */