-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfishstep.c
1023 lines (812 loc) · 26.3 KB
/
fishstep.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
/*
* (C) 2020 Janne Heikkarainen <[email protected]>
*
* All rights reserved.
*
* This file is part of Fishstep N-body Simulator.
*
* Fishstep N-body Simulator is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fishstep N-body Simulator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fishstep N-body Simulator. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <fftw3.h>
#if SDL
#include <SDL.h>
#endif
#if TIFF_ENABLE
#include <tiffio.h>
#endif
#include "fishstep.h"
// number of posix threads
#define NUM_THREADS 4
// draw every Nth frame
#define FRAMESKIP 2
// graphics scale up factor
#define SCALE 1
// number of cells per dimension
#define NUM_CELLS 512
// number of particles
#define NUM_PARTICLES 5*512000
// minimum and maximum geometries
#define XMIN 0
#define XMAX NUM_CELLS
// number of dimensions
#define NUM_DIMS 2
// particle mass
#define MASS (double)(1.0/(double)(NUM_PARTICLES));
#define DT 0.0095;
#define G 4.0*M_PI*M_PI;
struct thread_data thread_data_array[NUM_THREADS];
/* structure for RGB color */
struct color{
double r;
double g;
double b;
};
void colormap(double val, struct color *col){
int nn;
double x_map[6]={0.0, 0.2, 0.45, 0.7, 0.85, 1.0};
double r_map[6]={0.0, 0.0, 0.0, 1.0, 1.0, 0.65};
double g_map[6]={0.0, 0.0, 1.0, 1.0, 0.0, 0.0};
double b_map[6]={0.65, 1.0, 1.0, 0.0, 0.0, 0.0};
/* crop value to fit the map */
if(val>0.99)
val=0.99;
val=1.0-val;
/* linearly interpolate the value from the colormap */
for(nn=0;nn<(6-1);nn++){
if(val>x_map[nn]&&val<x_map[nn+1]){
col->r=r_map[nn]+(val-x_map[nn])*(r_map[nn+1]-r_map[nn])/(x_map[nn+1]-x_map[nn]);
col->g=g_map[nn]+(val-x_map[nn])*(g_map[nn+1]-g_map[nn])/(x_map[nn+1]-x_map[nn]);
col->b=b_map[nn]+(val-x_map[nn])*(b_map[nn+1]-b_map[nn])/(x_map[nn+1]-x_map[nn]);
break;
}
}
}
#if TIFF_ENABLE
void writeframe(char* path, SDL_Surface *screen){
TIFF *file;
Uint8 *p;
int ii;
int width=screen->w;
int height=screen->h;
file=TIFFOpen(path,"w");
if(file){
TIFFSetField(file, TIFFTAG_IMAGEWIDTH, (uint32) width);
TIFFSetField(file, TIFFTAG_IMAGELENGTH, (uint32) height);
TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(file, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(file, TIFFTAG_EXTRASAMPLES, 0);
TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(file, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField(file, TIFFTAG_IMAGEDESCRIPTION, "");
p = (Uint8 *)screen->pixels;
for (ii = height - 1; ii >= 0; ii--) {
if (TIFFWriteScanline(file, p, ii, 0) < 0) {
TIFFClose(file);
printf("Error writing TIFF file.\n");
exit(1);
}
p += 3 * width * sizeof(Uint8);
}
TIFFClose(file);
}
}
#endif
// particle kick-drift thread
void *integration_kick_drift_thread(void *threadarg){
// loop variables
int ii;
// thread slice boundaries
int lo;
int hi;
// thread variables
double *r, *v, *a;
double dt;
// thread data pointer
struct thread_data *my_data;
// thread enumeration variable
int thread_id;
// set up pointers
my_data=(struct thread_data *) threadarg;
r=my_data->r;
v=my_data->v;
a=my_data->a;
dt=my_data->dt;
thread_id=my_data->thread_id;
// compute thread slice
lo=thread_id*NUM_PARTICLES/NUM_THREADS;
hi=(thread_id+1)*NUM_PARTICLES/NUM_THREADS;
for(ii=lo; ii<hi; ii++) {
v[ii*NUM_DIMS + 0] += 0.5*dt*a[ii*NUM_DIMS + 0];
v[ii*NUM_DIMS + 1] += 0.5*dt*a[ii*NUM_DIMS + 1];
r[ii*NUM_DIMS + 0] += dt*v[ii*NUM_DIMS + 0];
r[ii*NUM_DIMS + 1] += dt*v[ii*NUM_DIMS + 1];
// enforce periodic boundaries
if(r[ii*NUM_DIMS + 0] > (double)(NUM_CELLS)) {
r[ii*NUM_DIMS + 0] -= (double)(NUM_CELLS);
}
else if(r[ii*NUM_DIMS + 0] < (double)(0)) {
r[ii*NUM_DIMS + 0] += (double)(NUM_CELLS);
}
if(r[ii*NUM_DIMS + 1] > (double)(NUM_CELLS)) {
r[ii*NUM_DIMS + 1] -= (double)(NUM_CELLS);
}
else if(r[ii*NUM_DIMS + 1] < (double)(0)) {
r[ii*NUM_DIMS + 1] += (double)(NUM_CELLS);
}
}
pthread_exit(NULL);
}
// particle kick thread
void *integration_kick_thread(void *threadarg){
// loop variables
int ii;
// thread slice boundaries
int lo;
int hi;
// thread variables
double *v, *a;
double dt;
// thread data pointer
struct thread_data *my_data;
// thread enumeration variable
int thread_id;
// set up pointers
my_data=(struct thread_data *) threadarg;
v=my_data->v;
a=my_data->a;
dt=my_data->dt;
thread_id=my_data->thread_id;
// compute thread slice
lo=thread_id*NUM_PARTICLES/NUM_THREADS;
hi=(thread_id+1)*NUM_PARTICLES/NUM_THREADS;
for(ii=lo; ii<hi; ii++) {
v[ii*NUM_DIMS + 0] += 0.5*dt*a[ii*NUM_DIMS + 0];
v[ii*NUM_DIMS + 1] += 0.5*dt*a[ii*NUM_DIMS + 1];
}
pthread_exit(NULL);
}
void *zero_rho_field_thread(void *threadarg){
// loop variables
int nn;
// thread slice boundaries
int lo;
int hi;
// thread variables
double *sub_rho;
// thread data pointer
struct thread_data *my_data;
// thread enumeration variable
int thread_id;
// set up pointers
my_data=(struct thread_data *) threadarg;
sub_rho=my_data->rho;
thread_id=my_data->thread_id;
// compute thread slice
lo=thread_id*(NUM_THREADS*NUM_CELLS*NUM_CELLS)/NUM_THREADS;
hi=(thread_id+1)*(NUM_THREADS*NUM_CELLS*NUM_CELLS)/NUM_THREADS;
for(nn=lo; nn<hi; nn++) {
sub_rho[nn] = 0.0;
}
pthread_exit(NULL);
}
void *copy_rho_to_complex_thread(void *threadarg){
// loop variables
int nn;
// thread slice boundaries
int lo;
int hi;
// thread variables
fftw_complex *rho_complex;
double *rho;
// thread data pointer
struct thread_data *my_data;
// thread enumeration variable
int thread_id;
// set up pointers
my_data=(struct thread_data *) threadarg;
rho=my_data->rho;
rho_complex=my_data->rho_complex;
thread_id=my_data->thread_id;
// compute thread slice
lo=thread_id*(NUM_CELLS*NUM_CELLS)/NUM_THREADS;
hi=(thread_id+1)*(NUM_CELLS*NUM_CELLS)/NUM_THREADS;
for(nn=lo; nn<hi; nn++) {
rho_complex[nn][0] = (double)(4.0*M_PI*M_PI)*rho[nn];
rho_complex[nn][1] = 0.0;
}
pthread_exit(NULL);
}
void *compute_rho_field_thread(void *threadarg){
// loop variables
int nn;
// thread slice boundaries
int lo;
int hi;
// thread variables
int ii,jj;
double *r, *rho;
double dr[2];
// thread data pointer
struct thread_data *my_data;
// thread enumeration variable
int thread_id;
// set up pointers
my_data=(struct thread_data *) threadarg;
r=my_data->r;
rho=my_data->rho;
thread_id=my_data->thread_id;
// compute thread slice
lo=thread_id*NUM_PARTICLES/NUM_THREADS;
hi=(thread_id+1)*NUM_PARTICLES/NUM_THREADS;
for(nn=lo; nn<hi; nn++) {
ii = floor(r[nn*NUM_DIMS + 0]);
jj = floor(r[nn*NUM_DIMS + 1]);
// cloud in cell field interpolation
dr[0] = r[nn*NUM_DIMS + 0] - (double)(ii);
dr[1] = r[nn*NUM_DIMS + 1] - (double)(jj);
rho[jj*NUM_CELLS + ii] += (1.0 - dr[0])*(1.0 - dr[1])*MASS;
rho[jj*NUM_CELLS + modulo(ii+1, NUM_CELLS)] += (dr[0])*(1.0 - dr[1])*MASS;
rho[modulo(jj+1, NUM_CELLS)*NUM_CELLS + ii] += (1.0 - dr[0])*(dr[1])*MASS;
rho[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)] += (dr[0])*(dr[1])*MASS;
}
pthread_exit(NULL);
}
void compute_rho_field(double *r, double *rho) {
// loop variables
int nn;
int ii, jj;
// interpolation variables
double dr[2];
// zero rho field
for(nn=0; nn<NUM_CELLS*NUM_CELLS; nn++) {
rho[nn] = 0.0;
}
// interpolate particles into cells
for(nn=0; nn<NUM_PARTICLES; nn++) {
ii = floor(r[nn*NUM_DIMS + 0]);
jj = floor(r[nn*NUM_DIMS + 1]);
// cloud in cell field interpolation
dr[0] = r[nn*NUM_DIMS + 0] - (double)(ii);
dr[1] = r[nn*NUM_DIMS + 1] - (double)(jj);
rho[jj*NUM_CELLS + ii] += (1.0 - dr[0])*(1.0 - dr[1])*MASS;
rho[jj*NUM_CELLS + modulo(ii+1, NUM_CELLS)] += (dr[0])*(1.0 - dr[1])*MASS;
rho[modulo(jj+1, NUM_CELLS)*NUM_CELLS + ii] += (1.0 - dr[0])*(dr[1])*MASS;
rho[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)] += (dr[0])*(dr[1])*MASS;
}
}
void *acceleration_interpolation_thread(void *threadarg){
// loop variables
int nn;
// thread slice boundaries
int lo;
int hi;
// thread variables
int ii,jj;
double *r, *a, *phi;
double dr[2];
double g_x, g_y;
// thread data pointer
struct thread_data *my_data;
// thread enumeration variable
int thread_id;
// set up pointers
my_data=(struct thread_data *) threadarg;
r=my_data->r;
a=my_data->a;
phi=my_data->phi;
thread_id=my_data->thread_id;
// compute thread slice
lo=thread_id*NUM_PARTICLES/NUM_THREADS;
hi=(thread_id+1)*NUM_PARTICLES/NUM_THREADS;
for(nn=lo; nn<hi; nn++) {
// calculate grid positions
ii = floor(r[nn*NUM_DIMS + 0]);
jj = floor(r[nn*NUM_DIMS + 1]);
// calculate offset in grid
dr[0] = r[nn*NUM_DIMS + 0] - (double)(ii);
dr[1] = r[nn*NUM_DIMS + 1] - (double)(jj);
// calculate force grid interpolation for 2nd order differences
g_x = (phi[jj*NUM_CELLS + modulo(ii-1, NUM_CELLS)] - phi[jj*NUM_CELLS + modulo(ii+1, NUM_CELLS)])/2.0 * (1.0 - dr[0])*(1.0 - dr[1]);
g_x += (phi[jj*NUM_CELLS + ii] - phi[jj*NUM_CELLS + modulo(ii+2, NUM_CELLS)])/2.0 * (dr[0])*(1.0-dr[1]);
g_x += (phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii-1, NUM_CELLS)] - phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)])/2.0 * (1.0 - dr[0])*(dr[1]);
g_x += (phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + ii] - phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii+2, NUM_CELLS)])/2.0 * (dr[0])*(dr[1]);
g_y = (phi[modulo(jj-1, NUM_CELLS)*NUM_CELLS + ii] - phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + ii])/2.0 * (1.0 - dr[0])*(1.0 - dr[1]);
g_y += (phi[modulo(jj-1, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)] - phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)])/2.0 * (dr[0])*(1.0-dr[1]);
g_y += (phi[jj*NUM_CELLS + ii] - phi[modulo(jj+2, NUM_CELLS)*NUM_CELLS + ii])/2.0 * (1.0 - dr[0])*(dr[1]);
g_y += (phi[jj*NUM_CELLS + modulo(ii+1, NUM_CELLS)] - phi[modulo(jj+2, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)])/2.0 * (dr[0])*(dr[1]);
a[nn*NUM_DIMS + 0] = g_x;
a[nn*NUM_DIMS + 1] = g_y;
}
pthread_exit(NULL);
}
void compute_acceleration(double *r, double *a, double *phi) {
int nn;
int ii, jj;
double dr[2];
double g_x, g_y;
for(nn=0; nn<NUM_PARTICLES; nn++) {
// calculate grid positions
ii = floor(r[nn*NUM_DIMS + 0]);
jj = floor(r[nn*NUM_DIMS + 1]);
// calculate offset in grid
dr[0] = r[nn*NUM_DIMS + 0] - (double)(ii);
dr[1] = r[nn*NUM_DIMS + 1] - (double)(jj);
// calculate force grid interpolation for 2nd order differences
g_x = (phi[jj*NUM_CELLS + modulo(ii-1, NUM_CELLS)] - phi[jj*NUM_CELLS + modulo(ii+1, NUM_CELLS)])/2.0 * (1.0 - dr[0])*(1.0 - dr[1]);
g_x += (phi[jj*NUM_CELLS + ii] - phi[jj*NUM_CELLS + modulo(ii+2, NUM_CELLS)])/2.0 * (dr[0])*(1.0-dr[1]);
g_x += (phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii-1, NUM_CELLS)] - phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)])/2.0 * (1.0 - dr[0])*(dr[1]);
g_x += (phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + ii] - phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii+2, NUM_CELLS)])/2.0 * (dr[0])*(dr[1]);
g_y = (phi[modulo(jj-1, NUM_CELLS)*NUM_CELLS + ii] - phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + ii])/2.0 * (1.0 - dr[0])*(1.0 - dr[1]);
g_y += (phi[modulo(jj-1, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)] - phi[modulo(jj+1, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)])/2.0 * (dr[0])*(1.0-dr[1]);
g_y += (phi[jj*NUM_CELLS + ii] - phi[modulo(jj+2, NUM_CELLS)*NUM_CELLS + ii])/2.0 * (1.0 - dr[0])*(dr[1]);
g_y += (phi[jj*NUM_CELLS + modulo(ii+1, NUM_CELLS)] - phi[modulo(jj+2, NUM_CELLS)*NUM_CELLS + modulo(ii+1, NUM_CELLS)])/2.0 * (dr[0])*(dr[1]);
a[nn*NUM_DIMS + 0] = g_x;
a[nn*NUM_DIMS + 1] = g_y;
}
}
/* compute time difference in seconds and nanoseconds */
void timediff(struct timespec start, struct timespec end, struct timespec *out){
/* compute time difference */
if(end.tv_nsec<start.tv_nsec){
out->tv_nsec=end.tv_nsec-start.tv_nsec+1000000000;
out->tv_sec=end.tv_sec-start.tv_sec-1;
}
else{
out->tv_nsec=end.tv_nsec-start.tv_nsec;
out->tv_sec=end.tv_sec-start.tv_sec;
}
}
int main(int argc, char *argv[])
{
// loop variables
int nn;
int tt;
int steps;
int ii;
int jj;
// simulation variables
double *r;
double *v;
double *a;
// initial condition variables
double r2[NUM_DIMS];
double dd;
double vv;
double *rho, *sub_rho;
double *phi;
double *green;
double *gr_hat;
double k_x, k_y;
fftw_complex *rho_hat, *green_rho_hat, *rho_complex, *phi_complex;
fftw_plan rho_plan, phi_plan;
// time step variable
double dt=DT;
struct color *col=0;
#if SDL
// SDL variables
SDL_Surface *screen;
SDL_Event event;
Uint8 *pixels;
// SDL loop variables
int ii2;
int jj2;
// gfx variables
double d;
#endif
// program state flag
int done;
// posix thread variables
int thread_rc;
pthread_t threads[NUM_THREADS];
pthread_attr_t attr;
void *thread_status;
// time measurement variables
struct timespec int_time;
struct timespec time1, time2;
#if TIFF_ENABLE
// tiff writer variables
char filename[128];
int tiff_frame;
#endif
// init pseudorandom number generator
srand(time(0));
#if SDL
// open a SDL window
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(SCALE*NUM_CELLS, SCALE*NUM_CELLS, 24, SDL_SWSURFACE);
SDL_WM_SetCaption("Fishstep", "Fishstep");
#endif
// allocate simulation vectors
// displacement
r=(double *)malloc(2*NUM_PARTICLES*sizeof(double));
if(!r){
printf("Out of memory: r not allocated.\n");
exit(1);
}
// velocity
v=(double *)malloc(2*NUM_PARTICLES*sizeof(double));
if(!v){
printf("Out of memory: v not allocated.\n");
exit(1);
}
// acceleration
a=(double *)malloc(2*NUM_PARTICLES*sizeof(double));
if(!a){
printf("Out of memory: a not allocated.\n");
exit(1);
}
// density
rho=(double *)malloc(NUM_CELLS*NUM_CELLS*sizeof(double));
if(!rho){
printf("Out of memory: rho not allocated.\n");
exit(1);
}
sub_rho=(double *)malloc(NUM_THREADS*NUM_CELLS*NUM_CELLS*sizeof(double));
if(!sub_rho){
printf("Out of memory: sub_rho not allocated.\n");
exit(1);
}
// potential
phi=(double *)malloc(NUM_CELLS*NUM_CELLS*sizeof(double));
if(!phi){
printf("Out of memory: phi not allocated.\n");
exit(1);
}
// green's function
green=(double *)malloc((NUM_CELLS)*(NUM_CELLS)*sizeof(double));
if(!green){
printf("Out of memory: green not allocated.\n");
exit(1);
}
gr_hat=(double *)malloc(NUM_CELLS*NUM_CELLS*sizeof(double));
if(!gr_hat){
printf("Out of memory: gr_hat not allocated.\n");
exit(1);
}
rho_complex = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*NUM_CELLS*NUM_CELLS);
if(!rho_complex){
printf("Out of memory: rho_complex not allocated.\n");
exit(1);
}
rho_hat = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*NUM_CELLS*NUM_CELLS);
if(!rho_hat){
printf("Out of memory: rho_hat not allocated.\n");
exit(1);
}
phi_complex = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*NUM_CELLS*NUM_CELLS);
if(!phi_complex){
printf("Out of memory: phi_complex not allocated.\n");
exit(1);
}
green_rho_hat = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*NUM_CELLS*NUM_CELLS);
if(!green_rho_hat){
printf("Out of memory: green_rho_hat not allocated.\n");
exit(1);
}
col=(struct color*)malloc(sizeof(struct color));
if(!col){
printf("Out of memory: col not allocated.\n");
exit(1);
}
// generate initial condition
for(nn=0; nn<NUM_PARTICLES; nn++) {
r[nn*NUM_DIMS + 0] = NUM_CELLS*(double)(rand())/RAND_MAX;
r[nn*NUM_DIMS + 1] = NUM_CELLS*(double)(rand())/RAND_MAX;
r2[0] = r[nn*NUM_DIMS + 0] - NUM_CELLS/2;
r2[1] = r[nn*NUM_DIMS + 1] - NUM_CELLS/2;
while(vector_norm(r2, 2) > 28.0){
r[nn*NUM_DIMS + 0] = NUM_CELLS*(double)(rand())/RAND_MAX;
r[nn*NUM_DIMS + 1] = NUM_CELLS*(double)(rand())/RAND_MAX;
r2[0] = r[nn*NUM_DIMS + 0] - NUM_CELLS/2;
r2[1] = r[nn*NUM_DIMS + 1] - NUM_CELLS/2;
}
dd = vector_norm(r2, 2);
vv = sqrt(2.0*4.0*M_PI*M_PI*1.4825*dd);
v[nn*NUM_DIMS + 0] = -r2[1]*vv/dd;
v[nn*NUM_DIMS + 1] = r2[0]*vv/dd;
//v[nn*NUM_DIMS + 0] = 0.0;
//v[nn*NUM_DIMS + 1] = 0.0;
a[nn*NUM_DIMS + 0] = 0.0;
a[nn*NUM_DIMS + 1] = 0.0;
}
// compute gravitational green's function in k-space
for(jj=0; jj<NUM_CELLS/2; jj++) {
for(ii=0; ii<NUM_CELLS/2; ii++) {
k_x = 2.0*M_PI*(ii+1)/(double)(NUM_CELLS);
k_y = 2.0*M_PI*(jj+1)/(double)(NUM_CELLS);
green[jj*NUM_CELLS + ii] = -(1.0/4.0)*1.0/(sin(k_x/2.0)*sin(k_x/2.0)+sin(k_y/2.0)*sin(k_y/2.0));
gr_hat[jj*NUM_CELLS + ii] = green[jj*NUM_CELLS + ii];
gr_hat[jj*NUM_CELLS + (NUM_CELLS-ii-1)] = green[jj*NUM_CELLS + ii];
gr_hat[(NUM_CELLS-jj-1)*NUM_CELLS + (NUM_CELLS-ii-1)] = green[jj*NUM_CELLS + ii];
gr_hat[(NUM_CELLS-jj-1)*NUM_CELLS + ii] = green[jj*NUM_CELLS + ii];
}
}
#if FFTW3_THREADS
// initialize FFTW threads
if(!fftw_init_threads()){
printf("FFTW threads failed.\n");
exit(1);
}
fftw_plan_with_nthreads(NUM_THREADS);
#endif
// plan for fft2(rho)
rho_plan = fftw_plan_dft_2d(NUM_CELLS, NUM_CELLS, rho_complex, rho_hat, FFTW_FORWARD, FFTW_ESTIMATE);
if(!rho_plan){
printf("FFTW rho_plan failed.\n");
exit(1);
}
// plan for ifft2(gr_hat * rho_hat)
phi_plan = fftw_plan_dft_2d(NUM_CELLS, NUM_CELLS, green_rho_hat, phi_complex, FFTW_BACKWARD, FFTW_ESTIMATE);
if(!phi_plan){
printf("FFTW phi_plan failed.\n");
exit(1);
}
// start timer
clock_gettime(CLOCK_MONOTONIC, &time1);
// integrate time steps
done=0;
tt=0;
steps=0;
#if TIFF_ENABLE
tiff_frame=0;
#endif
while(!done){
#if SDL
// check for SDL event
if (SDL_PollEvent(&event)) {
switch (event.type) {
// close button clicked
case SDL_QUIT:
done=1;
break;
}
}
#endif
// main simulation loop
// ********************
// create kick-drift integration threads
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(nn=0;nn<NUM_THREADS;nn++){
thread_data_array[nn].thread_id=nn;
thread_data_array[nn].r=r;
thread_data_array[nn].v=v;
thread_data_array[nn].a=a;
thread_data_array[nn].dt=dt;
thread_rc=pthread_create(&threads[nn], &attr, integration_kick_drift_thread, (void *) &thread_data_array[nn]);
if(thread_rc){
printf("ERROR: pthread_create() returned %d.\n", thread_rc);
exit(-1);
}
}
/* join threads */
pthread_attr_destroy(&attr);
for(nn=0; nn < NUM_THREADS; nn++){
thread_rc=pthread_join(threads[nn], &thread_status);
if(thread_rc){
printf("ERROR: pthread_join() returned %d.\n", thread_rc);
exit(-1);
}
}
// create zero thread rho field threads
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(nn=0;nn<NUM_THREADS;nn++){
thread_data_array[nn].thread_id=nn;
thread_data_array[nn].rho=sub_rho;
thread_rc=pthread_create(&threads[nn], &attr, zero_rho_field_thread, (void *) &thread_data_array[nn]);
if(thread_rc){
printf("ERROR: pthread_create() returned %d.\n", thread_rc);
exit(-1);
}
}
/* join threads */
pthread_attr_destroy(&attr);
for(nn=0; nn < NUM_THREADS; nn++){
thread_rc=pthread_join(threads[nn], &thread_status);
if(thread_rc){
printf("ERROR: pthread_join() returned %d.\n", thread_rc);
exit(-1);
}
}
// create rho field interpolation calculation threads
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(nn=0;nn<NUM_THREADS;nn++){
thread_data_array[nn].thread_id=nn;
thread_data_array[nn].r=r;
thread_data_array[nn].rho=&sub_rho[nn*NUM_CELLS*NUM_CELLS];
thread_rc=pthread_create(&threads[nn], &attr, compute_rho_field_thread, (void *) &thread_data_array[nn]);
if(thread_rc){
printf("ERROR: pthread_create() returned %d.\n", thread_rc);
exit(-1);
}
}
/* join threads */
pthread_attr_destroy(&attr);
for(nn=0;nn<NUM_THREADS;nn++){
thread_rc=pthread_join(threads[nn], &thread_status);
if(thread_rc){
printf("ERROR: pthread_join() returned %d.\n", thread_rc);
exit(-1);
}
}
// sum sub_rho fields into rho
for(ii=0;ii<NUM_CELLS*NUM_CELLS;ii++){
rho[ii] = 0.0;
for(nn=0;nn<NUM_THREADS;nn++){
rho[ii] += sub_rho[nn*NUM_CELLS*NUM_CELLS + ii];
}
}
// create rho to complex copy threads
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(nn=0;nn<NUM_THREADS;nn++){
thread_data_array[nn].thread_id=nn;
thread_data_array[nn].rho=rho;
thread_data_array[nn].rho_complex=rho_complex;
thread_rc=pthread_create(&threads[nn], &attr, copy_rho_to_complex_thread, (void *) &thread_data_array[nn]);
if(thread_rc){
printf("ERROR: pthread_create() returned %d.\n", thread_rc);
exit(-1);
}
}
/* join threads */
pthread_attr_destroy(&attr);
for(nn=0;nn<NUM_THREADS;nn++){
thread_rc=pthread_join(threads[nn], &thread_status);
if(thread_rc){
printf("ERROR: pthread_join() returned %d.\n", thread_rc);
exit(-1);
}
}
// compute rho_hat
fftw_execute(rho_plan);
// multiply green's function and density in k-space
for(jj=0; jj<NUM_CELLS; jj++) {
for(ii=0; ii<NUM_CELLS; ii++) {
green_rho_hat[jj*NUM_CELLS + ii][0] = gr_hat[jj*NUM_CELLS + ii] * rho_hat[jj*NUM_CELLS + ii][0];
green_rho_hat[jj*NUM_CELLS + ii][1] = gr_hat[jj*NUM_CELLS + ii] * rho_hat[jj*NUM_CELLS + ii][1];
}
}
// compute phi_complex
fftw_execute(phi_plan);
// compute real(phi_complex)
for(ii=0; ii<NUM_CELLS*NUM_CELLS; ii++) {
phi[ii] = 1.0/(NUM_CELLS) * phi_complex[ii][0];
}
// create acceleration interpolation threads
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(nn=0;nn<NUM_THREADS;nn++){
thread_data_array[nn].thread_id=nn;
thread_data_array[nn].r=r;
thread_data_array[nn].a=a;
thread_data_array[nn].phi=phi;
thread_data_array[nn].dt=dt;
thread_rc=pthread_create(&threads[nn], &attr, acceleration_interpolation_thread, (void *) &thread_data_array[nn]);
if(thread_rc){
printf("ERROR: pthread_create() returned %d.\n", thread_rc);
exit(-1);
}
}
/* join threads */
pthread_attr_destroy(&attr);
for(nn=0;nn<NUM_THREADS;nn++){
thread_rc=pthread_join(threads[nn], &thread_status);
if(thread_rc){
printf("ERROR: pthread_join() returned %d.\n", thread_rc);
exit(-1);
}
}
// create kick integration threads
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(nn=0;nn<NUM_THREADS;nn++){
thread_data_array[nn].thread_id=nn;
thread_data_array[nn].r=r;
thread_data_array[nn].v=v;
thread_data_array[nn].a=a;
thread_data_array[nn].dt=dt;
thread_rc=pthread_create(&threads[nn], &attr, integration_kick_thread, (void *) &thread_data_array[nn]);
if(thread_rc){
printf("ERROR: pthread_create() returned %d.\n", thread_rc);
exit(-1);
}
}
/* join threads */
pthread_attr_destroy(&attr);
for(nn=0;nn<NUM_THREADS;nn++){
thread_rc=pthread_join(threads[nn], &thread_status);
if(thread_rc){
printf("ERROR: pthread_join() returned %d.\n", thread_rc);
exit(-1);
}
}
// end main simulation loop
// ************************
// draw state
if(steps == FRAMESKIP-1) {
// timer stop
clock_gettime(CLOCK_MONOTONIC, &time2);
#if SDL
pixels=(Uint8 *)screen->pixels;
SDL_LockSurface(screen);
for(jj=0; jj<NUM_CELLS; jj++){
for(ii=0; ii<NUM_CELLS; ii++){
// get field value
d=NUM_PARTICLES*4.0*rho[jj*NUM_CELLS+ii];
//d=-0.1*phi[jj*NUM_CELLS+ii];
// clip value
if(d>255.0) {
d=255.0;
}
else if(d<0.0) {
d=0.0;
}
//d=d/256.0;
// update framebuffer
for(jj2=0; jj2<SCALE; jj2++){
for(ii2=0; ii2<SCALE; ii2++){
// display heatmap style field
//colormap(d, col);
//pixels[3*(SCALE*jj+jj2)*screen->w+3*(SCALE*ii+ii2)+0]=(Uint8)(col->r*255.0);
//pixels[3*(SCALE*jj+jj2)*screen->w+3*(SCALE*ii+ii2)+1]=(Uint8)(col->g*255.0);
//pixels[3*(SCALE*jj+jj2)*screen->w+3*(SCALE*ii+ii2)+2]=(Uint8)(col->b*255.0);
// display density of particles
pixels[3*(SCALE*jj+jj2)*screen->w+3*(SCALE*ii+ii2)+0]=(Uint8)(d);
pixels[3*(SCALE*jj+jj2)*screen->w+3*(SCALE*ii+ii2)+1]=(Uint8)(d);
pixels[3*(SCALE*jj+jj2)*screen->w+3*(SCALE*ii+ii2)+2]=(Uint8)(d);
}
}
}
}
SDL_UnlockSurface(screen);
SDL_Flip(screen);
#endif
timediff(time1, time2, &int_time);
// print state
printf("tt: %d steps/s: %d\n", tt, (int)(steps/((double)(int_time.tv_sec)+(double)(int_time.tv_nsec)*1.0E-9)));
#if TIFF_ENABLE
sprintf(filename, "/home/janne808/testrun/%08d.tif", tiff_frame++);
writeframe(filename, screen);
#endif
// timer start
clock_gettime(CLOCK_MONOTONIC, &time1);