forked from kcroker/g2munge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g2munge.c
1216 lines (942 loc) · 33.4 KB
/
g2munge.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
/************************************************************************
*
* g2munge.c - Munges Gadget2 SnapFormat=1 IC files with
* accelerations in desirable ways. For use with
* single files only, though extension to multiple files
* would be trivial to implement
*
* Copyright(c) 2014 Kevin Croker
* Contains bits of code originally appearing in read_snapshot.c, Copyright(c) Vorkel Springel
* included with Gadget2 public distribution
*
* GPL v3
*
* Functions:
* + merge : combine two snapshots
* + stats : give [min,max] of the physical quantites
* + xlate : Euclidian translation of a snapshot
* + dump : Dump a list of positions in ASCII
* + ascii : Read positions in ASCII
* + grid : Dump a grid of uniformly spaced squirticles
* + rand : Randomly place random particles
* - scale : mechanically scale a dataset (is this useful?)
* Let l'/l = R
* Let k be the order of the potential (as in U(ar) = a^k U(r) )
* Then Landau doth spaketh (L1.10)
* v'/v = R^(k/2)
* E'/E = R^k
* M'/M (angular momentum) = R^(1+k/2)
* (NOTE: k = -1 for Newtonian gravity)
* ~ strip : remove everything EXCEPT a type of particle from the snapshot
* + flatten : set all particle types to a single particle type
* + binit : bin the particles within radius ranges of whatever
* + ident : makes an identical copy of the input (for debugging really)
* + burn : imprint a density onto an initial condition assuming spherical symmetry
* + mass : allows you to set particle type masses
*
* Consider eventual sick visualizations using OpenGL:https://sites.google.com/site/...
* ...dlampetest/python/vectorized-particle-system-and-geometry-shaders
*
*************************************************************************/
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_histogram.h>
#include <gsl/gsl_rng.h>
#include "tkgadget2.h"
#include "burns.h"
// CAREFUL: Returns an allocated object, you have to free it explicitly!
// I think r is the origin...
gsl_histogram *binit(snapshot *snap, float radius, int nbins, float *r) {
gsl_histogram *h;
particle_data *P;
h = gsl_histogram_alloc(nbins);
gsl_histogram_set_ranges_uniform(h, 0.0, radius);
for(P = snap->P + snap->NumPart; P > snap->P; --P)
gsl_histogram_increment(h, mag3(r, P->Pos));
return h;
}
gsl_histogram *binitk(snapshot *snap, float radius, int nbins, float *r) {
gsl_histogram *h;
particle_data *P;
int res;
double *ranges, *ksums;
int k;
h = gsl_histogram_alloc(nbins);
ranges = (double *)malloc(sizeof(double *)*(nbins+1));
ksums = (double *)malloc(sizeof(double *)*(nbins+1));
//
// From solving the recurrence relation
//
// Calculate the partial sums
// ksums[0] = 0;
// for(k = 1; k <= nbins; ++k)
// ksums[k] = ksums[k-1] + pow(k, 1.0/3.0);
// Set up the ranges
ranges[0] = 0.0;
for(k = 1; k <= nbins; ++k)
ranges[k] = pow(k, 1.0/3.0) * radius / pow(nbins, 1.0/3.0);
// Clean up and assign ranges
//free(ksums);
gsl_histogram_set_ranges(h, ranges, nbins+1);
free(ranges);
//gsl_histogram_set_ranges_uniform(h, 0.0, radius);
for(P = snap->P + snap->NumPart; P > snap->P; --P)
gsl_histogram_increment(h, mag3(r, P->Pos));
return h;
}
void dumpr(snapshot *snapA) {
int total, k, i;
particle_data *P;
// Do Ids respect particle type??
// Dump out the particles in different groups for cool colorization options
total = 0;
printf("# g2munge: %d\n", snapA->NumPart);
for(i = 0; i < 6; ++i) {
// Output empty groups so that when we plot in gnuplot, groups
// can be consistently indexed within the file
//if(snapA->head.npart[i] == 0)
// continue;
printf("\n# Group %d: %d\n", i, snapA->head.npart[i]);
for(k = 0; k < snapA->head.npart[i]; ++k) {
P = snapA->sortedP[total];
//
// yes, its slow to do this check every time. If necessary cleave into two loops
//
printf("%u %f %f %f %f %f %f %f %f\n",
P->Id,
P->Pos[0], P->Pos[1], P->Pos[2],
P->Vel[0], P->Vel[1], P->Vel[2],
snapA->head.mass[i] ? snapA->head.mass[i] : P->Mass,
!i ? P->U : -1);
++total;
}
}
}
void extrema(snapshot *snap, float *eP, float *eV, float *eA) {
particle_data *P;
int k;
// XXX won't work, minima need to be initiall +NAN, maxima need to be -NAN
for(k = 0; k < 3; ++k) {
// Set maxima
eP[2*k] = -INFINITY;
eP[2*k + 1] = INFINITY;
eV[2*k] = -INFINITY;
eV[2*k + 1] = INFINITY;
eA[2*k] = -INFINITY;
eA[2*k + 1] = INFINITY;
}
for(P = snap->P + snap->NumPart; P > snap->P; --P) {
// Do min-max
for(k = 0; k < 3; ++k) {
P->Pos[k] > eP[2*k] ? eP[2*k] = P->Pos[k] : (P->Pos[k] < eP[2*k + 1] ? eP[2*k+1] = P->Pos[k] : 0);
P->Vel[k] > eV[2*k] ? eV[2*k] = P->Vel[k] : (P->Vel[k] < eV[2*k + 1] ? eV[2*k+1] = P->Vel[k] : 0);
P->Acc[k] > eA[2*k] ? eA[2*k] = P->Acc[k] : (P->Acc[k] < eA[2*k + 1] ? eA[2*k+1] = P->Acc[k] : 0);
}
}
}
const char *usage = "Usage: %s <snapshot filename | - (stdin)> <command string>\n";
int main(int argc, char **argv)
{
char input_fname[200];
int type, snapshot_number, files;
float r[3], cm[3], L[3], E, vsq, totalMass;
float maxPos[6], maxVel[6], maxAcc[6];
int i, sanity, k;
snapshot snapA, snapB, snapC;
particle_data *P, *pA, *pB, *pC;
snapshot *writeout;
profileFunc burnf;
gsl_histogram *strips, *realbins;
gsl_rng *entropy;
float Rmax, e, density;
size_t index;
float params[10];
particle_data **pIter, **pIter2;
int numprev;
// Load in the parameters
if(argc < 3) {
fprintf(stderr, usage, argv[0]);
exit(1);
}
// Hardcoding the numfiles are we?
files = 1; /* number of files per snapshot */
// This will always be the source unless ascii
// in which case these parameters are ignored
//
// If we don't load a snapshot, zero out snapA
//
if(strcmp(argv[2], "ascii") && strcmp(argv[2], "grid") && strcmp(argv[2], "rand") && strcmp(argv[2], "nodetest")) {
// Stop doing this name basename shit, makes it really hard to automate
sprintf(input_fname, "%s", argv[1]);
load_snapshot(&snapA, input_fname, files);
}
else
memset(&snapA, 0, sizeof(snapshot));
// Initially, don't write anything out
writeout = NULL;
// unit_conversion(); /* optional stuff */
// Parse the command string
if(!strcmp(argv[2], "burn")) {
// 3 4
// burn <resolution> [args]
// tophatburn: [args] = <cut> <0|1>
//
// Prunes particles until a region (0, radius) is overdense by some specified function
// Function is specified by function pointer
// Presently assumes that the input snapshot's distribution is uniform
// but this isn't hard to generalize (use binit)
//
if(argc < 5) {
fprintf(stderr, "Usage: burn <resolution> <mask> [args]\nPresent masks are:\n\ttophat\n\tthin\n");
exit(2);
}
// Get the resolution
e = atof(argv[3]);
fprintf(stderr, "burn/NOTIFY: read resolution of %f\n", e);
// Determine extent of the dataset
// This will upperbound the radius
extrema(&snapA, maxPos, maxVel, maxAcc);
// Check negative extent as well!
// Note, I wrote extrema to produce [max,min] which is retarded...
for(i = 0; i < 3; ++i) {
if(abs(maxPos[i+1]) > maxPos[i])
maxPos[i] = abs(maxPos[i+1]);
}
Rmax = gsl_hypot3(maxPos[0], maxPos[2], maxPos[4]);
fprintf(stderr, "burn/NOTIFY: computed Rmax of %f\n", Rmax);
// Go one shell further out
//Rmax += e;
// Define your function
// Note the function is 1 where things are to be kept
// Note the function is 0 where things are to be completely cut
if(!strcmp(argv[4], "thin")) {
if(argc != 7) {
fprintf(stderr, "Usage: burn/thin: <radius> <factor between 0.0 and 1.0>\n");
exit(3);
}
params[0] = atof(argv[5]);
params[1] = atof(argv[6]);
fprintf(stderr, "burn/thin/NOTIFY: thinning beyond %f by a factor of %f\n", params[0], params[1]);
burnf = thinBurn;
}
else if(!strcmp(argv[4], "tophat")) {
if(argc != 7) {
fprintf(stderr, "Usage: burn/tophat: <radius> <0/1: 1 to invert the tophat>\n");
exit(3);
}
params[0] = atof(argv[5]);
params[1] = atof(argv[6]);
burnf = tophatBurn;
}
// Cut into shells
strips = gsl_histogram_alloc(ceil(Rmax/e));
gsl_histogram_set_ranges_uniform(strips, 0.0, Rmax+e);
// Definitely need to make a first pass and compute the density in the regions!
memset(r, 0, sizeof(float)*3);
realbins = binit(&snapA, Rmax+e, ceil(Rmax/e), r);
// Make sure to floor, or else we won't prune enough later on...
for(i = 0; i < ceil(Rmax/e); ++i)
strips->bin[i] = floor ( (1.0 - (*burnf)(i*e, params)) * realbins->bin[i]);
#undef DEBUG
#ifdef DEBUG
// Do the bins sum to numpart?
for(i = 0, sanity = 0; i < ceil(Rmax/e); ++i)
sanity += (int)realbins->bin[i];
fprintf(stderr, "burn: realbins contain %d particles (of %d)\n", sanity, snapA.NumPart);
// Print off the deltas
for(i = 0; i < ceil(Rmax/e); ++i)
fprintf(stderr, "burn: delta[%d] = %d\n", i, (int)(realbins->bin[i] - strips->bin[i]));
#endif
// We don't need realbins anymore, free it
gsl_histogram_free(realbins);
// Use the sorted array and shuffle it
// XXX Not seeding...
entropy = gsl_rng_alloc(gsl_rng_taus);
gsl_ran_shuffle(entropy, snapA.sortedP, snapA.NumPart, sizeof(particle_data *));
gsl_rng_free(entropy);
#ifdef DEBUG
// Do an N(N-1)/2 check of the consistency of the pointer list
for(pIter = snapA.sortedP;
pIter < snapA.sortedP + snapA.NumPart;
++pIter) {
for(pIter2 = pIter+1;
pIter2 < snapA.sortedP + snapA.NumPart;
++pIter2) {
// Check for duplicate
if(*pIter == *pIter2)
fprintf(stderr, "burn: DUPLICATE IN SORTED LIST at %d\n", (int)(pIter2 - snapA.sortedP)/sizeof(particle_data *));
}
}
#endif
// Keep track of actually pruned count
k = 0;
sanity = 0;
// Prune
for(pIter = snapA.sortedP, i = 0;
pIter < snapA.sortedP + snapA.NumPart;
++pIter, ++i) {
// GO FETCH
P = *pIter;
e = mag3(P->Pos, r);
gsl_histogram_find(strips, e, &index);
// if(gsl_histogram_get(strips, index) > 0.0) {
// fprintf(stderr, "burn: BEFORE strips[%d] = %d\n", index, (int)strips->bin[index]);
if(strips->bin[index] > 0.0) {
// Writeout expects header statistics to honestly reflect eventual P array
--snapA.head.npart[P->Type];
// Was it massive?
// (XXX possible) note that checking the mass directly won't work
// if they are flagged massive in the header, but still given no mass here
// Header valus is what matters. Question is, were they particles with
// a dynamic mass spec. The answer is yes if the header says 0
if(snapA.head.mass[P->Type] == 0.0)
--snapA.Nmassive;
// Was it gassive?
if(P->Type == 0)
--snapA.Ngas;
// Flag for prune
P->Type = -1;
// Record that we pruned
//gsl_histogram_accumulate(strips, e, -1.0);
strips->bin[index] -= 1.0;
//fprintf(stderr, "burn: AFTER strips[%d] = %d\n", index, (int)strips->bin[index]);
// Update the count of prunes
++k;
}
#ifdef DEBUG
else {
// Particle was NOT removed, output its sex life
fprintf(stderr, "burn: particle UNPRUNED @ (%f, %f, %f) with mag %f\n", P->Pos[0], P->Pos[1], P->Pos[2], e);
fprintf(stderr, "burn:\t index %d, remaining here %d\n", index, (int)strips->bin[index]);
++sanity;
}
#endif
}
#ifdef DEBUG
fprintf(stderr, "burn: processed %d (of %d) particles\n\tskipped %d particles, pruned %d particles, sum %d\n", i, snapA.NumPart, sanity, k, sanity + k);
// Did we empty all the strips?
sanity = 0;
for(i = 0; i < strips->n; ++i) {
fprintf(stderr, "burn: strips->bin[%d] = %d\n", i, (int)strips->bin[i]);
sanity += (int)strips->bin[i];
}
fprintf(stderr, "burn: strip residuals %d (should be zero)\n", sanity);
fflush(stderr);
#endif
gsl_histogram_free(strips);
// Note that snapA.NumPart still contains the number of objects allocated within the P array
if( !(pA = (particle_data *)malloc(sizeof(particle_data)*(snapA.NumPart - k)))) {
fprintf(stderr, "burnr/DEATH: could not allocate prune buffer, shitting out the mouth...\n");
exit(3);
}
// Would be better to just avoid these when writing out, instead of doing ridiculous memory copy
// operations, but this is more straightforward right now and does not risk breaking working code
// also, doing the writeout means comparing repeatedly over very many passes over the P struct
// (one for each section)...
// And id's have to be reset
P = snapA.P;
pC = pA;
for(++P, i = 0; P <= snapA.P + snapA.NumPart; ++P) {
if(P->Type < 0)
continue;
else {
P->Id = ++i;
memcpy(pC++, P, sizeof(particle_data));
}
}
// Now, replace the P array
free(snapA.P + 1);
snapA.P = pA - 1;
#ifdef DEBUG
fprintf(stderr, "burn/DEBUG: particles before %d\n", snapA.NumPart);
#endif
// Now update the global number of particles
snapA.NumPart -= k;
#ifdef DEBUG
fprintf(stderr, "burn/DEBUG: particles after %d\n", snapA.NumPart);
#endif
// And update the npartTotal array which we always seem to forget about
memcpy(snapA.head.npartTotal, snapA.head.npart, sizeof(int)*6);
// Flag for writeout
writeout = &snapA;
}
else if(!strcmp(argv[2], "retype")) {
sanity = atoi(argv[3]);
i = atoi(argv[4]);
k = atoi(argv[5]);
P = snapA.P + 1 + snapA.NumPart;
fprintf(stderr, "retype: shifting %d particles of type %d into type %d...\n", sanity, i, k);
while(--P > snapA.P && sanity > 0) {
if(P->Type == i) {
P->Type = k;
sanity--;
// Update headers
snapA.head.npart[i]--;
snapA.head.npartTotal[i]--;
snapA.head.npart[k]++;
snapA.head.npartTotal[k]++;
}
}
fprintf(stderr, "retype: %d particles unshifted\n", sanity);
// Update mass table
snapA.head.mass[k] = snapA.head.mass[i];
// Flag for writeout
writeout = &snapA;
}
else if(!strcmp(argv[2], "rand")) {
//
// rand <#> <max l> <mass> [randoms?]
//
// Produce a random arrangement of particles in a cube side length [0,l]
// with N particles
//
if(argc != 6 && argc != 8) {
fprintf(stderr, "Usage: rand <#> <max l> <mass> [<random mod> <random base>]\n");
exit(1);
}
snapA.NumPart = atoi(argv[3]);
Rmax = atof(argv[4]);
if( !(snapA.P = (particle_data *)malloc(sizeof(particle_data)*snapA.NumPart))) {
exit(2);
}
fprintf(stderr, "rand: placing %d particles randomly in a box of size %f\n", snapA.NumPart, Rmax);
// Zero it all out, and 1 index it
memset(snapA.P, 0, sizeof(particle_data)*snapA.NumPart);
P = snapA.P;
--snapA.P;
sanity = 1;
entropy = gsl_rng_alloc(gsl_rng_taus);
for(i = 0; i < snapA.NumPart; ++i) {
P->Pos[0] = Rmax * gsl_rng_uniform(entropy);
P->Pos[1] = Rmax * gsl_rng_uniform(entropy);
P->Pos[2] = Rmax * gsl_rng_uniform(entropy);
if(argc > 6) {
P->Type = (rand() % atoi(argv[6])) + atoi(argv[7]);
}
else
P->Type = 1;
// Accumulate here in case of randoms
snapA.head.npartTotal[P->Type]++;
snapA.head.npart[P->Type]++;
(P++)->Id = sanity++;
}
//gsl_rng_free(entropy);
// Set other things in the header that need to be set
for(i = 0; i < 6; ++i)
snapA.head.mass[i] = atof(argv[5]);
// Flag for writeout
writeout = &snapA;
}
else if(!strcmp(argv[2], "nodetest")) {
//
// nodetest <#> <max l> <mass>
//
// Produce a nested layout of particles such that their locations fall in the center
// of an oct tree of depth
//
snapA.NumPart = atoi(argv[3]);
Rmax = atof(argv[4]);
if(snapA.NumPart < 0)
exit(1);
if( !(snapA.P = (particle_data *)malloc(sizeof(particle_data)*snapA.NumPart))) {
exit(2);
}
// Zero it all out, and 1 index it
memset(snapA.P, 0, sizeof(particle_data)*snapA.NumPart);
i = snapA.NumPart;
P = snapA.P;
while(P != snapA.P + i) {
P->Pos[0] = Rmax / 2.0;
P->Pos[1] = Rmax / 2.0;
P->Pos[2] = Rmax / 2.0;
P->Type = 0;
P->Id = (P - snapA.P) + 1;
Rmax /= 2.0;
++P;
}
snapA.head.npartTotal[1] = i;
snapA.head.npart[1] = i;
snapA.head.mass[1] = 1.0;
--snapA.P;
writeout = &snapA;
}
else if(!strcmp(argv[2], "grid")) {
//
// grid <# density> <max l> <mass> [randoms?]
//
// Produce a grid arrangement of particles in a cube side length [0,l]
// with N particles
//
// Initialize
memset(&snapA, 0, sizeof(snapshot));
if(argc != 6 && argc != 8) {
fprintf(stderr, "Usage: grid <# density> <max l> <mass> [<random mod> <random base>]\n");
exit(1);
}
// Initialize
memset(&snapA, 0, sizeof(snapshot));
// Determine the increment
Rmax = atof(argv[4]);
i = ceil(pow(atof(argv[3]) * gsl_pow_3(Rmax), 1.0/3.0));
snapA.NumPart = gsl_pow_3(i);
fprintf(stderr, "grid: populating with %d particles\n", snapA.NumPart);
e = Rmax / i;
fprintf(stderr, "grid: Rmax = %f, linear step is %e\n", Rmax, e);
if( !(snapA.P = (particle_data *)malloc(sizeof(particle_data)*snapA.NumPart))) {
exit(2);
}
// Zero it all out, and 1 index it
memset(snapA.P, 0, sizeof(particle_data)*snapA.NumPart);
--snapA.P;
// Create the particles
P = snapA.P + 1;
sanity = 1;
//entropy = gsl_rng_alloc(gsl_rng_taus);
// Check that the types are in the right range
if(atoi(argv[6]) - 1 + atoi(argv[7]) > 5) {
fprintf(stderr, "grid: ERROR %d + rand() %% %d exceeds permissable type of [0,5]\n", atoi(argv[7]), atoi(argv[6]));
exit(3);
}
// XXX
// This code makes all the types right next to each other! I guess this was good for error testing!
for(r[0] = 0.0; r[0] < Rmax; r[0] += e) {
for(r[1] = 0.0; r[1] < Rmax; r[1] += e) {
for(r[2] = 0.0; r[2] < Rmax; r[2] += e) {
// Bail if we've made enough
if(sanity > snapA.NumPart) {
// Output particle deficit
fprintf(stderr, "grid: final dimensions filled (%f, %f, %f)\n", r[0], r[1], r[2]);
r[1] = r[0] = Rmax + 1.0;
break;
}
memcpy(P->Pos, r, sizeof(float)*3);
if(argc > 6) {
P->Type = (rand() % atoi(argv[6])) + atoi(argv[7]);
}
else
P->Type = 1;
// Accumulate here in case of randoms
snapA.head.npartTotal[P->Type]++;
snapA.head.npart[P->Type]++;
(P++)->Id = sanity++;
}
}
}
// Set other things in the header that need to be set
for(i = 0; i < 6; ++i)
snapA.head.mass[i] = atof(argv[5]);
// Flag for writeout
writeout = &snapA;
}
else if(!strcmp(argv[2], "binit")) {
//
// binit <radius> <bins> [x] [y] [z]
//
// Note binit() returns the histogram because its useful, so we must explicitly free it
if(argc == 5) {
memset(r, 0, sizeof(float)*3);
strips = binit(&snapA, atof(argv[3]), atoi(argv[4]), r);
}
else if(argc == 8) {
r[0] = atof(argv[5]);
r[1] = atof(argv[6]);
r[2] = atof(argv[7]);
strips = binit(&snapA, atof(argv[3]), atoi(argv[4]), r);
}
else {
fprintf(stderr, "Usage: binit <radius> <bins> [x] [y] [z]\n");
exit(2);
}
// Go over the histogram and output the bins
gsl_histogram_fprintf(stdout, strips, "%e", "%e");
gsl_histogram_free(strips);
}
else if(!strcmp(argv[2], "binitk")) {
//
// binit <radius> <bins> [x] [y] [z]
//
// Note binit() returns the histogram because its useful, so we must explicitly free it
if(argc == 5) {
memset(r, 0, sizeof(float)*3);
strips = binitk(&snapA, atof(argv[3]), atoi(argv[4]), r);
}
else if(argc == 8) {
r[0] = atof(argv[5]);
r[1] = atof(argv[6]);
r[2] = atof(argv[7]);
strips = binitk(&snapA, atof(argv[3]), atoi(argv[4]), r);
}
else {
fprintf(stderr, "Usage: binitk <radius> <bins> [x] [y] [z]\n");
exit(2);
}
// Go over the histogram and output the bins
gsl_histogram_fprintf(stdout, strips, "%e", "%e");
gsl_histogram_free(strips);
}
else if(!strcmp(argv[2], "ident")) {
// Just write out what we read in
writeout=&snapA;
}
else if(!strcmp(argv[2], "strip")) {
// XXX Not yet implemented
if(argc != 4) {
fprintf(stderr, "Usage: strip <type to keep>\n");
exit(2);
}
// Shallow copy snapshot A into B
memcpy(&snapB, &snapA, sizeof(snapshot));
// Allocate a new particle_data struct
snapB.P = (particle_data *)malloc(sizeof(particle_data)*snapA.NumPart);
if(!snapB.P) {
fprintf(stderr, "strip: failed to allocate buffer, dying\n");
exit(3);
}
// Zero out the particle count in the target
snapB.NumPart = 0;
// Signal to write out
writeout = &snapB;
}
else if(!strcmp(argv[2], "flatten")) {
if(argc != 4) {
fprintf(stderr, "Usage: flatten <new type>\n");
exit(2);
}
i = atoi(argv[3]);
if(i < 0 || i > 5) {
fprintf(stderr, "flatter/ERROR: type must be in (0,5), given %d\n", i);
exit(4);
}
// Reset individual particle numbers
memset(snapA.head.npart, 0, sizeof(int)*6);
snapA.Nmassive = 0;
snapA.Ngas = 0;
memset(snapA.head.npartTotal, 0, sizeof(int)*6);
// Tag all particles as new type i
snapA.head.npart[i] = snapA.NumPart;
snapA.head.npartTotal[i] = snapA.NumPart;
// Did we become gas?
if(i == 0)
snapA.Ngas = snapA.NumPart;
// Did we become massive?
if(snapA.head.mass[i] == 0)
snapA.Nmassive = snapA.NumPart;
// Change each individual particle
P = snapA.P;
for(++P; P <= snapA.P + snapA.NumPart; ++P) {
// Should really be a proper double comparison...
if(snapA.head.mass[P->Type] > 0.0) {
// The original type originally had uniform mass. Does the destination have the same property?
// If not, we stash the previous uniform mass into the new per-partice mass
if(snapA.head.mass[i] > 0.0);
else
P->Mass = snapA.head.mass[P->Type];
}
// XXX If we became a gas, we should probably acquire a temperature...
if(i == 0)
fprintf(stderr, "flatten/WARNING: becoming a gas, but U will not be set to anything sane!\n");
// Change type
P->Type = i;
}
// Flag to write it out
writeout = &snapA;
}
else if(!strcmp(argv[2], "ascii")) {
//
// ascii
//
// creates an IC and outputs to stdout for ascii values given in the dump format
//
// # g2munge: N\n
// \n
// # Group x: N_x\n
// %u %f %f %f %f %f %f %f %f\n
// ...
// \n
// # Group x+1: N_{x+1}\n
// ...
//
// Where the floats appear as: id Xpos Ypos Zpos Xvel Yvel Zvel mass enerugi
// i.e. delimited by space, with newlines denoting next particle
//
// (NOTE: this is also the format that is produced during a dump, so a dump can be read back
// into Gadget format if desired.)
//
// Signal to the abuser
fprintf(stderr, "ascii/NOTIFY: ignoring listed input basefile and snapshot...\n");
// Attempt to parse out the total particle counts
if(!scanf("# g2munge: %d\n", &snapA.NumPart)) {
fprintf(stderr, "ascii/ERROR: could not determine total particle count, header line bad?\n");
exit(4);
}
else
fprintf(stderr, "ascii/NOTIFY: searching for %d particles...\n", snapA.NumPart);
// Allocate stuffs
if( !(snapA.P = (particle_data *)malloc(snapA.NumPart * sizeof(particle_data)))) {
fprintf(stderr, "ascii/ERROR: could not allocate my buffs! death\n");
exit(3);
}
// Adjust for weird indexing
P = snapA.P;
--snapA.P;
// Number of previously read particles
numprev = 0;
while(!feof(stdin)) {
// Read in a block "\n"
if(scanf("\n# Group %d: %d\n", &k, &i) < 2) {
fprintf(stderr, "ascii/ERROR: could not determine (easily) number or type\n");
exit(4);
}
if(k < 0 || k > 5) {
fprintf(stderr, "ascii/ERROR: unknown type %d\n", k);
exit(4);
}
fprintf(stderr, "ascii/NOTIFY: Found type %d, reading %d of them...\n", k, i);
// Set things
snapA.head.npart[k] = i;
snapA.head.npartTotal[k] = i;
snapA.head.num_files = 1;
// Gas check
if(k == 0)
snapA.Ngas = i;
// Reuse variables:
// type = a flag for whether they were all the same mass
type = 1;
while(i-- > 0) {
// Directly read em' in
if( scanf("%u %f %f %f %f %f %f %f %f\n",
&(P->Id),
P->Pos, (P->Pos)+1, (P->Pos)+2,
P->Vel, (P->Vel)+1, (P->Vel)+2,
&(P->Mass), &(P->U)) < 8 ) {
fprintf(stderr, "ascii/ERROR: unable to read expected particle %d from the end\n", snapA.head.npart[k]-i);
exit(4);
}
// See if we should just tag these masses in the header
// Short-circuit if we've already detected discrepancy
if( (P > snapA.P + numprev) && type && (P-1)->Mass != P->Mass)
type = 0;
// Got one, so go to the next particle
++P;
}
// This is how many we read on this round
numprev = P - snapA.P;
// If we read anything for these guys...
if(snapA.head.npart[k] > 0) {
// If they were all the same type, set uniform mass in the header. If
// not, update the number of specific massive particles in the header
if(type) {
snapA.head.mass[k] = (P-1)->Mass;
fprintf(stderr, "ascii/NOTIFY: masses for type %d set uniformly to %f\n", k, (P-1)->Mass);
}
else {
snapA.Nmassive += snapA.head.npart[k];
fprintf(stderr, "ascii/NOTIFY: particles of type %d have distinct masses\n", k);
}
}
}
// Flag for writeout
writeout = &snapA;
}
else if(!strcmp(argv[2], "xlate")) {
if(argc != 6) {
fprintf(stderr, "Usage: xlate <x> <y> <z>\n");
exit(2);
}
r[0] = atof(argv[3]);
r[1] = atof(argv[4]);
r[2] = atof(argv[5]);
// Just do it here
P = snapA.P;
for(++P; P <= snapA.P + snapA.NumPart; ++P) {
// For some reason, things are not stored as gsl_vectors. Okay.
P->Pos[0] += r[0];
P->Pos[1] += r[1];
P->Pos[2] += r[2];
}
// Signal to write it out
writeout = &snapA;
}
else if(!strcmp(argv[2], "dump"))
dumpr(&snapA);
else if(!strcmp(argv[2], "merge")) {
if(argc != 4) {
fprintf(stderr, "Usage: merge <snapshot B filename>\n");
exit(2);
}
else {
// Hardcoding numfiles again, load up the B snapshot
files = 1; /* number of files per snapshot */
//sprintf(input_fname, "%s_%03d", argv[3], atoi(argv[4]));
load_snapshot(&snapB, argv[3], files);