/
Simulation3D.cpp
1126 lines (1001 loc) · 42.4 KB
/
Simulation3D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <fstream>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <boost/timer.hpp>
#include <sys/time.h>
#include "Simulation3D.hpp"
#define _USE_MATH_DEFINES
BoundaryLocation determineBoundary(mpi::communicator& world) {
if(world.rank() == 0 && world.rank() == world.size()-1)
return DOUBLE_BDY;
else if(world.rank() == 0)
return LOWER_BDY;
else if(world.rank() == world.size() - 1)
return UPPER_BDY;
return NO_BDY;
}
Simulation3D::Simulation3D(double L_x, double L_y, double L_z,
double T,
unsigned int n_cells, unsigned int n_steps,
unsigned int procs_x, unsigned int procs_y, unsigned int procs_z,
unsigned int block_size,
std::string& dump_dir,
Simulation3DInitializer* init, mpi::communicator & world) :
world(world),
xLine(world.split(world.rank() / procs_x)),
yLine(world.split(world.rank() % procs_x + (world.rank() / (procs_x*procs_y)) * procs_x)),
zLine(world.split(world.rank() % (procs_x*procs_y))),
nSteps(n_steps),
currentStep(0),
dx(L_x/n_cells),
dy(L_y/n_cells),
dz(L_z/n_cells),
dt(T/n_steps),
blockSize(block_size),
preFactorX(LIGHTSPEED*dt/(2*dx)),
preFactorY(LIGHTSPEED*dt/(2*dy)),
preFactorZ(LIGHTSPEED*dt/(2*dz)),
E(new double[3*blockSize*blockSize*blockSize]),
B(new double[3*blockSize*blockSize*blockSize]),
tmp_field(new double[3*blockSize*blockSize*blockSize]),
rhsx(new double[blockSize*blockSize*blockSize]),
rhsy(new double[blockSize*blockSize*blockSize]),
rhsz(new double[blockSize*blockSize*blockSize]),
rhs_ptrs_x(new double*[blockSize*blockSize]),
rhs_ptrs_y(new double*[blockSize*blockSize]),
rhs_ptrs_z(new double*[blockSize*blockSize]),
dumpDir(dump_dir)
{
procsX = xLine.size();
procsY = yLine.size();
procsZ = zLine.size();
VacuumMatrixInitializer mat_init_x = VacuumMatrixInitializer(dx, dt, blockSize, determineBoundary(xLine));
VacuumMatrixInitializer mat_init_y = VacuumMatrixInitializer(dy, dt, blockSize, determineBoundary(yLine));
VacuumMatrixInitializer mat_init_z = VacuumMatrixInitializer(dz, dt, blockSize, determineBoundary(zLine));
VacuumCouplingInitializer coupling_init_x = VacuumCouplingInitializer(& mat_init_x, blockSize, xLine);
VacuumCouplingInitializer coupling_init_y = VacuumCouplingInitializer(& mat_init_y, blockSize, yLine);
VacuumCouplingInitializer coupling_init_z = VacuumCouplingInitializer(& mat_init_z, blockSize, zLine);
std::vector<AbstractMatrixInitializer*> mat_inits_x(blockSize*blockSize, & mat_init_x);
std::vector<AbstractMatrixInitializer*> mat_inits_y(blockSize*blockSize, & mat_init_y);
std::vector<AbstractMatrixInitializer*> mat_inits_z(blockSize*blockSize, & mat_init_z);
std::vector<AbstractCouplingInitializer*> coupling_inits_x(blockSize*blockSize, & coupling_init_x);
std::vector<AbstractCouplingInitializer*> coupling_inits_y(blockSize*blockSize, & coupling_init_y);
std::vector<AbstractCouplingInitializer*> coupling_inits_z(blockSize*blockSize, & coupling_init_z);
guardB = allocateGuardStorage();
guardE = allocateGuardStorage();
init->setOffsets(xLine, yLine, zLine);
initFields(init);
xUpdateRHSs = init->initCollection(mat_inits_x, coupling_inits_x, blockSize, xLine);
yUpdateRHSs = init->initCollection(mat_inits_y, coupling_inits_y, blockSize, yLine);
zUpdateRHSs = init->initCollection(mat_inits_z, coupling_inits_z, blockSize, zLine);
guardSendbuf = new double[3*blockSize*blockSize];
}
Simulation3D::~Simulation3D() {
delete[] guardB[0][0]; delete[] guardB[0]; delete[] guardB;
delete[] guardE[0][0]; delete[] guardE[0]; delete[] guardE;
delete[] rhsx; delete[] rhsy; delete[] rhsz;
delete[] tmp_field;
}
void Simulation3D::initFields(Simulation3DInitializer* init) {
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy=0; iy < blockSize; iy++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
init->populate(E, B, ix, iy, iz);
}
}
}
for(unsigned int iy=0; iy < blockSize; iy++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
init->populateGuard(& guardE[0][0][(blockSize*iy + iz)*3],
& guardB[0][0][(blockSize*iy + iz)*3],
-1, iy, iz);
init->populateGuard(& guardE[0][1][(blockSize*iy + iz)*3],
& guardB[0][1][(blockSize*iy + iz)*3],
blockSize, iy, iz);
}
}
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
init->populateGuard(& guardE[1][0][(blockSize*ix + iz)*3],
& guardB[1][0][(blockSize*ix + iz)*3],
ix, -1, iz);
init->populateGuard(& guardE[1][1][(blockSize*ix + iz)*3],
& guardB[1][1][(blockSize*ix + iz)*3],
ix, blockSize, iz);
}
}
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy=0; iy < blockSize; iy++) {
init->populateGuard(& guardE[2][0][(blockSize*ix + iy)*3],
& guardB[2][0][(blockSize*ix + iy)*3],
ix, iy, -1);
init->populateGuard(& guardE[2][1][(blockSize*ix + iy)*3],
& guardB[2][1][(blockSize*ix + iy)*3],
ix, iy, blockSize);
}
}
for(unsigned int i=0; i < blockSize*blockSize; i++) {
rhs_ptrs_x[i] = & rhsx[blockSize*i];
rhs_ptrs_y[i] = & rhsy[blockSize*i];
rhs_ptrs_z[i] = & rhsz[blockSize*i];
}
}
void Simulation3D::simulate(bool dump, unsigned int dump_periodicity, unsigned int total_dumps) {
unsigned int n_dumps = 0;
timeval t1, t2;
gettimeofday(& t1, NULL);
unsigned int steps_per_timing = 100;
unsigned long* timings = new unsigned long[nSteps / steps_per_timing];
double* center_e_fields = new double[nSteps];
std::cout << std::setprecision(10);
for(currentStep=0; currentStep <= nSteps; currentStep++) {
if (currentStep % dump_periodicity == 0 && n_dumps < total_dumps && dump) {
std::ostringstream filename(std::ios::out);
filename << dumpDir << "/dump3D_" << currentStep / dump_periodicity << "dt" << 10.0/nSteps << "dx" << dx << ".h5";
this->dumpFields(filename.str());
n_dumps++;
}
if (currentStep % steps_per_timing == 0 && world.rank() == 0) {
gettimeofday(& t2, NULL);
if(currentStep > 0)
timings[currentStep / steps_per_timing - 1] =
1000000*(t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec);
// std::cout << 1000000*(t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) << std::endl;
t1=t2;
}
if (xLine.rank() == 1 && yLine.rank() == 1) { // FIXME - GENERALIZE
center_e_fields[currentStep] = E[(blockSize*blockSize*blockSize/2 + blockSize*blockSize/2 + blockSize/2)*3];
}
if(currentStep < nSteps)
this->timeStep();
}
if(world.rank() == 0)
dumpTimings(timings, nSteps / steps_per_timing, steps_per_timing);
if(xLine.rank() == 1 && yLine.rank() == 1)
dumpCenterFields(center_e_fields, nSteps);
delete[] timings;
delete[] center_e_fields;
}
void Simulation3D::dumpCenterFields(double* eFields, hsize_t nSteps) {
std::ostringstream filename(std::ios::out);
filename << dumpDir << "/center_fields_dx" << dx << "_dt" << dt << ".h5";
hid_t file_id=H5Fcreate(filename.str().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
hid_t fieldspace=H5Screate_simple(1, & nSteps, NULL);
hid_t dx_space = H5Screate(H5S_SCALAR);
hid_t dt_space = H5Screate(H5S_SCALAR);
hid_t field_dset_id = H5Dcreate(file_id, "timings", H5T_NATIVE_DOUBLE, fieldspace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hid_t dx_attr_id = H5Acreate(field_dset_id, "dx", H5T_NATIVE_DOUBLE, dx_space,
H5P_DEFAULT, H5P_DEFAULT);
hid_t dt_attr_id = H5Acreate(field_dset_id, "dt", H5T_NATIVE_DOUBLE, dt_space,
H5P_DEFAULT, H5P_DEFAULT);
herr_t status = H5Dwrite(field_dset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL,
H5P_DEFAULT, eFields);
status = H5Awrite(dx_attr_id, H5T_NATIVE_DOUBLE, & dx);
status = H5Awrite(dt_attr_id, H5T_NATIVE_DOUBLE, & dt);
H5Sclose(fieldspace); H5Sclose(dx_space); H5Sclose(dt_space);
H5Aclose(dx_attr_id); H5Aclose(dt_attr_id);
H5Dclose(field_dset_id);
H5Fclose(file_id);
}
void Simulation3D::dumpTimings(unsigned long* timings, hsize_t total_timings,
unsigned int steps_per_timing) {
std::ostringstream filename(std::ios::out);
filename << dumpDir << "/timing_s" << blockSize << "_p" << world.size() << ".h5";
hid_t file_id=H5Fcreate(filename.str().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
hid_t timingspace=H5Screate_simple(1, & total_timings, NULL);
hid_t dx_space = H5Screate(H5S_SCALAR);
hid_t dt_space = H5Screate(H5S_SCALAR);
hid_t bs_space = H5Screate(H5S_SCALAR);
hid_t ns_space = H5Screate(H5S_SCALAR);
hid_t alg_name_space = H5Screate(H5S_SCALAR);
hid_t atype = H5Tcopy(H5T_C_S1);
#ifndef YEE
H5Tset_size(atype, xUpdateRHSs->getAlgName().length());
#else
H5Tset_size(atype, std::string("Yee").length());
#endif
H5Tset_strpad(atype, H5T_STR_NULLTERM);
hid_t timing_dset_id = H5Dcreate(file_id, "timings", H5T_NATIVE_LONG, timingspace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hid_t dx_attr_id = H5Acreate(timing_dset_id, "dx", H5T_NATIVE_DOUBLE, dx_space,
H5P_DEFAULT, H5P_DEFAULT);
hid_t dt_attr_id = H5Acreate(timing_dset_id, "dt", H5T_NATIVE_DOUBLE, dt_space,
H5P_DEFAULT, H5P_DEFAULT);
hid_t bs_attr_id = H5Acreate(timing_dset_id, "blockSize", H5T_NATIVE_UINT, bs_space,
H5P_DEFAULT, H5P_DEFAULT);
hid_t ns_attr_id = H5Acreate(timing_dset_id, "stepsPerTiming", H5T_NATIVE_UINT, ns_space,
H5P_DEFAULT, H5P_DEFAULT);
hid_t alg_attr_id = H5Acreate(timing_dset_id,"communicationStrategy", atype, alg_name_space,
H5P_DEFAULT, H5P_DEFAULT);
herr_t status = H5Dwrite(timing_dset_id, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL,
H5P_DEFAULT, timings);
status = H5Awrite(dx_attr_id, H5T_NATIVE_DOUBLE, & dx);
status = H5Awrite(dt_attr_id, H5T_NATIVE_DOUBLE, & dt);
status = H5Awrite(bs_attr_id, H5T_NATIVE_UINT, & blockSize);
status = H5Awrite(ns_attr_id, H5T_NATIVE_UINT, & steps_per_timing);
#ifndef YEE
status = H5Awrite(alg_attr_id, atype, xUpdateRHSs->getAlgName().c_str());
#else
status = H5Awrite(alg_attr_id, atype, "Yee");
#endif
H5Sclose(timingspace); H5Sclose(dx_space); H5Sclose(dt_space);
H5Sclose(alg_name_space); H5Sclose(bs_space); H5Sclose(ns_space);
H5Tclose(atype);
H5Aclose(dx_attr_id); H5Aclose(dt_attr_id); H5Aclose(bs_attr_id);
H5Aclose(alg_attr_id); H5Aclose(ns_attr_id);
H5Dclose(timing_dset_id);
H5Fclose(file_id);
}
void Simulation3D::dumpFields(std::string filename) {
unsigned int p_x = xLine.rank(), p_y = yLine.rank(), p_z = zLine.rank();
hsize_t start[4];
start[0] = xLine.rank()*blockSize; start[1] = yLine.rank()*blockSize;
start[2] = zLine.rank()*blockSize; start[3] = 0;
hsize_t rhsx_start[3];
rhsx_start[0] = p_y*blockSize; rhsx_start[1] = p_z*blockSize; rhsx_start[2] = p_x*blockSize;
hsize_t rhsy_start[3];
rhsy_start[0] = p_x*blockSize; rhsy_start[1] = p_z*blockSize; rhsy_start[2] = p_y*blockSize;
hsize_t rhsz_start[3];
rhsz_start[0] = p_x*blockSize; rhsz_start[1] = p_y*blockSize; rhsz_start[2] = p_z*blockSize;
hsize_t rhs_count[3];
rhs_count[0] = blockSize; rhs_count[1] = blockSize; rhs_count[2] = blockSize;
hsize_t count[4];
count[0] = blockSize; count[1] = blockSize; count[2] = blockSize; count[3] = 3;
hsize_t dims[4];
dims[0] = blockSize*procsX; dims[1] = blockSize*procsY; dims[2] = blockSize*procsZ; dims[3] = 3;
hsize_t mem_dims[4];
mem_dims[0] = blockSize; mem_dims[1] = blockSize; mem_dims[2] = blockSize; mem_dims[3] = 3;
hid_t fa_p_list = H5Pcreate(H5P_FILE_ACCESS);
H5Pset_fapl_mpio(fa_p_list, world, MPI_INFO_NULL);
hid_t file_id=H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, fa_p_list);
H5Pclose(fa_p_list);
hsize_t rhs_dims[3];
rhs_dims[0] = blockSize*procsX; rhs_dims[1] = blockSize*procsY; rhs_dims[2] = blockSize*procsZ;
hsize_t rhs_mem_dims[3];
rhs_mem_dims[0] = blockSize; rhs_mem_dims[1] = blockSize; rhs_mem_dims[2] = blockSize;
hid_t E_filespace = H5Screate_simple(4, dims, NULL);
hid_t E_memspace = H5Screate_simple(4, mem_dims, NULL);
hid_t B_filespace = H5Screate_simple(4, dims, NULL);
hid_t B_memspace = H5Screate_simple(4, mem_dims, NULL);
hid_t rhsx_filespace = H5Screate_simple(3, rhs_dims, NULL);
hid_t rhsx_memspace = H5Screate_simple(3, rhs_mem_dims, NULL);
hid_t rhsy_filespace = H5Screate_simple(3, rhs_dims, NULL);
hid_t rhsy_memspace = H5Screate_simple(3, rhs_mem_dims, NULL);
hid_t rhsz_filespace = H5Screate_simple(3, rhs_dims, NULL);
hid_t rhsz_memspace = H5Screate_simple(3, rhs_mem_dims, NULL);
hid_t E_dset_id = H5Dcreate(file_id, "E", H5T_NATIVE_DOUBLE, E_filespace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hid_t E_wr_p_list = H5Pcreate(H5P_DATASET_XFER);
// H5Pset_dxpl_mpio(E_wr_p_list, H5FD_MPIO_COLLECTIVE);
H5Sselect_hyperslab(E_filespace, H5S_SELECT_SET, start, NULL, count, NULL);
herr_t status = H5Dwrite(E_dset_id, H5T_NATIVE_DOUBLE, E_memspace, E_filespace,
E_wr_p_list, E);
H5Dclose(E_dset_id);
H5Sclose(E_filespace);
H5Sclose(E_memspace);
H5Pclose(E_wr_p_list);
hid_t B_dset_id = H5Dcreate(file_id, "B", H5T_NATIVE_DOUBLE, B_filespace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hid_t B_wr_p_list = H5Pcreate(H5P_DATASET_XFER);
// H5Pset_dxpl_mpio(B_wr_p_list, H5FD_MPIO_COLLECTIVE);
H5Sselect_hyperslab(B_filespace, H5S_SELECT_SET, start, NULL, count, NULL);
status = H5Dwrite(B_dset_id, H5T_NATIVE_DOUBLE, B_memspace, B_filespace,
B_wr_p_list, B);
H5Dclose(B_dset_id);
H5Sclose(B_filespace);
H5Sclose(B_memspace);
H5Pclose(B_wr_p_list);
hid_t rhsx_dset_id = H5Dcreate(file_id, "rhsx", H5T_NATIVE_DOUBLE, rhsx_filespace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hid_t rhsx_wr_p_list = H5Pcreate(H5P_DATASET_XFER);
// H5Pset_dxpl_mpio(rhsx_wr_p_list, H5FD_MPIO_COLLECTIVE);
H5Sselect_hyperslab(rhsx_filespace, H5S_SELECT_SET, rhsx_start, NULL, rhs_count, NULL);
status = H5Dwrite(rhsx_dset_id, H5T_NATIVE_DOUBLE, rhsx_memspace, rhsx_filespace,
rhsx_wr_p_list, rhsx);
H5Dclose(rhsx_dset_id);
H5Sclose(rhsx_filespace);
H5Sclose(rhsx_memspace);
H5Pclose(rhsx_wr_p_list);
hid_t rhsy_dset_id = H5Dcreate(file_id, "rhsy", H5T_NATIVE_DOUBLE, rhsy_filespace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hid_t rhsy_wr_p_list = H5Pcreate(H5P_DATASET_XFER);
// H5Pset_dxpl_mpio(rhsy_wr_p_list, H5FD_MPIO_COLLECTIVE);
H5Sselect_hyperslab(rhsy_filespace, H5S_SELECT_SET, rhsy_start, NULL, rhs_count, NULL);
status = H5Dwrite(rhsy_dset_id, H5T_NATIVE_DOUBLE, rhsy_memspace, rhsy_filespace,
rhsy_wr_p_list, rhsy);
H5Dclose(rhsy_dset_id);
H5Sclose(rhsy_filespace);
H5Sclose(rhsy_memspace);
H5Pclose(rhsy_wr_p_list);
hid_t rhsz_dset_id = H5Dcreate(file_id, "rhsz", H5T_NATIVE_DOUBLE, rhsz_filespace,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
hid_t rhsz_wr_p_list = H5Pcreate(H5P_DATASET_XFER);
// H5Pset_dxpl_mpio(rhsx_wr_p_list, H5FD_MPIO_COLLECTIVE);
H5Sselect_hyperslab(rhsz_filespace, H5S_SELECT_SET, rhsz_start, NULL, rhs_count, NULL);
status = H5Dwrite(rhsz_dset_id, H5T_NATIVE_DOUBLE, rhsz_memspace, rhsz_filespace,
rhsz_wr_p_list, rhsz);
H5Dclose(rhsz_dset_id);
H5Sclose(rhsz_filespace);
H5Sclose(rhsz_memspace);
H5Pclose(rhsz_wr_p_list);
H5Fclose(file_id);
}
double*** Simulation3D::allocateGuardStorage() {
double* guardStorage = new double[6*3*blockSize*blockSize]; // 6 faces, 3 components, n^2
double** innerIndexStorage = new double*[6]; // Pointers to storage for each face
double*** guard = new double**[3]; // Pointers to the pair of pointers associated with each axis
innerIndexStorage[0] = guardStorage;
for (unsigned int i = 1; i < 6; i++) innerIndexStorage[i] = innerIndexStorage[i-1] + 3*blockSize*blockSize;
for (unsigned int i = 0; i < 3; i++) guard[i] = & innerIndexStorage[2*i];
return guard;
}
void Simulation3D::timeStep() {
std::ostringstream filename(std::ios::out);
#ifndef YEE
#ifndef NO_IMPLICIT_SOLVE
implicitUpdateM();
#endif
// filename << dumpDir << "/post_impM" << currentStep << ".h5";
// dumpFields(filename.str());
// filename.str("");
#ifndef NO_EXPLICIT_SOLVE
explicitUpdateP();
#endif
// filename << dumpDir << "/post_expP" << currentStep << ".h5";
// dumpFields(filename.str());
// filename.str("");
#ifndef NO_IMPLICIT_SOLVE
implicitUpdateP();
#endif
// filename << dumpDir << "/post_impP" << currentStep << ".h5";
// dumpFields(filename.str());
// filename.str("");
#ifndef NO_EXPLICIT_SOLVE
explicitUpdateM();
#endif
// filename << dumpDir << "/post_expM" << currentStep << ".h5";
// dumpFields(filename.str());
#else
yeeUpdate();
#endif
}
void Simulation3D::implicitUpdateM() {
std::ostringstream filename(std::ios::out);
#ifndef TOTAL_REDUCED_ONLY
populateRHSM();
#endif
// filename << dumpDir << "/PreTD" << currentStep << ".h5";
// dumpFields(filename.str());
// filename.str("");
xUpdateRHSs->doLines(rhs_ptrs_x);
yUpdateRHSs->doLines(rhs_ptrs_y);
zUpdateRHSs->doLines(rhs_ptrs_z);
#ifndef TOTAL_REDUCED_ONLY
writeRHSM();
// filename << dumpDir << "/PostTD" << currentStep << ".h5";
// dumpFields(filename.str());
// filename.str("");
getGuardF(B, guardB, UPWARDS);
implicitMSubstituteB();
getGuardF(E, guardE, DOWNWARDS);
#endif
// filename << dumpDir << "/post_Subst.h5";
// dumpFields(filename.str());
}
void Simulation3D::implicitUpdateP() {
std::ostringstream filename(std::ios::out);
#ifndef TOTAL_REDUCED_ONLY
populateRHSP();
#endif
// filename << dumpDir << "/PreTD" << currentStep << ".h5";
// dumpFields(filename.str());
// filename.str("");
xUpdateRHSs->doLines(rhs_ptrs_x);
yUpdateRHSs->doLines(rhs_ptrs_y);
zUpdateRHSs->doLines(rhs_ptrs_z);
#ifndef TOTAL_REDUCED_ONLY
writeRHSP();
getGuardF(B, guardB, UPWARDS);
implicitPSubstituteB();
getGuardF(E, guardE, DOWNWARDS);
#endif
}
void Simulation3D::explicitUpdateP() {
getGuardF(B, guardB, UPWARDS);
// set guard pointers
double* guardEXH = guardE[0][1]; double* guardEYH = guardE[1][1]; double* guardEZH = guardE[2][1];
double* guardBXL = guardB[0][0]; double* guardBYL = guardB[1][0]; double* guardBZL = guardB[2][0];
// dz update (B_x couples witha d E_y / dz)
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy = 0; iy < blockSize; iy++) {
double lastB = guardBZL[(blockSize*ix + iy)*3];
double lastE;
for(unsigned int iz = 0; iz < blockSize; iz++) {
unsigned int offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int offset_zp1 = ((ix*blockSize + iy)*blockSize + (iz+1))*3;
lastE = E[offset + 1];
E[offset + 1] += preFactorZ*(B[offset] - lastB);
lastB = B[offset];
if(iz + 1 != blockSize)
B[offset] += preFactorZ*(E[offset_zp1 + 1] - lastE);
else
B[offset] += preFactorZ*(guardEZH[(blockSize*ix + iy)*3 + 1] - lastE);
}
}
}
// dx update (B_y couples with d E_z / dx)
for(unsigned int iy = 0; iy < blockSize; iy++) {
for(unsigned int iz = 0; iz < blockSize; iz++) {
double lastB = guardBXL[(blockSize*iy + iz)*3 + 1];
double lastE;
for(unsigned int ix = 0; ix < blockSize; ix++) {
unsigned int offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int offset_xp1 = (((ix+1)*blockSize + iy)*blockSize + iz)*3;
lastE = E[offset + 2];
E[offset + 2] += preFactorX*(B[offset + 1] - lastB);
lastB = B[offset + 1];
if(ix + 1 != blockSize)
B[offset + 1] += preFactorX*(E[offset_xp1 + 2] - lastE);
else
B[offset + 1] += preFactorX*(guardEXH[(blockSize*iy + iz)*3 + 2] - lastE);
}
}
}
// dy update (B_z couples with d E_x / dy)
for(unsigned int ix = 0; ix < blockSize; ix++) {
for(unsigned int iz = 0; iz < blockSize; iz++) {
double lastB = guardBYL[(blockSize*ix + iz)*3 + 2];
double lastE;
for(unsigned int iy = 0; iy < blockSize; iy++) {
unsigned int offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int offset_yp1 = ((ix*blockSize + (iy+1))*blockSize + iz)*3;
lastE = E[offset];
E[offset] += preFactorY*(B[offset + 2] - lastB);
lastB = B[offset + 2];
if(iy + 1 != blockSize)
B[offset + 2] += preFactorY*(E[offset_yp1] - lastE);
else
B[offset + 2] += preFactorY*(guardEYH[(blockSize*ix + iz)*3] - lastE);
}
}
}
getGuardF(E, guardE, DOWNWARDS);
}
void Simulation3D::explicitUpdateM() {
getGuardF(B, guardB, UPWARDS);
// update B
double* guardEXH = guardE[0][1]; double* guardEYH = guardE[1][1]; double* guardEZH = guardE[2][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy = 0; iy < blockSize; iy++) {
for(unsigned int iz = 0; iz < blockSize; iz++) {
unsigned int offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int offset_xp1 = (((ix+1)*blockSize + iy)*blockSize + iz)*3;
unsigned int offset_yp1 = ((ix*blockSize + (iy+1))*blockSize + iz)*3;
unsigned int offset_zp1 = ((ix*blockSize + iy)*blockSize + (iz+1))*3;
for(unsigned int i=0; i < 3; i++) tmp_field[offset + i] = B[offset + i];
// B_x couples with d E_z / dy
if(iy + 1 < blockSize)
B[offset] -= preFactorY*(E[offset_yp1 + 2] - E[offset + 2]);
else
B[offset] -= preFactorY*(guardEYH[(blockSize*ix + iz)*3 + 2] - E[offset + 2]);
// B_y couples d E_x / dz
if(iz + 1 < blockSize)
B[offset + 1] -= preFactorZ*(E[offset_zp1] - E[offset]);
else
B[offset + 1] -= preFactorZ*(guardEZH[(blockSize*ix + iy)*3] - E[offset]);
// B_z couples with d E_y / dx
if(ix + 1 < blockSize)
B[offset + 2] -= preFactorX*(E[offset_xp1 + 1] - E[offset + 1]);
else {
B[offset + 2] -= preFactorX*(guardEXH[(blockSize*iy + iz)*3 + 1] - E[offset + 1]);
}
}
}
}
// update E
double* guardBXL = guardB[0][0]; double* guardBYL = guardB[1][0]; double* guardBZL = guardB[2][0];
double* guardBXH = guardB[0][1]; double* guardBYH = guardB[1][1]; double* guardBZH = guardB[2][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy = 0; iy < blockSize; iy++) {
for(unsigned int iz = 0; iz < blockSize; iz++) {
unsigned int offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int offset_xm1 = (((ix-1)*blockSize + iy)*blockSize + iz)*3;
unsigned int offset_ym1 = ((ix*blockSize + (iy-1))*blockSize + iz)*3;
unsigned int offset_zm1 = ((ix*blockSize + iy)*blockSize + (iz-1))*3;
// d B_z / dx couples with E_y
if(ix > 0)
E[offset + 1] -= preFactorX*(tmp_field[offset + 2] - tmp_field[offset_xm1 + 2]);
else
E[offset + 1] -= preFactorX*(tmp_field[offset + 2] - guardBXL[(blockSize*iy + iz)*3 + 2]);
// d B_x / dy couples with E_z
if(iy > 0)
E[offset + 2] -= preFactorY*(tmp_field[offset] - tmp_field[offset_ym1]);
else
E[offset + 2] -= preFactorY*(tmp_field[offset] - guardBYL[(blockSize*ix + iz)*3]);
// d B_y / dz couples with E_x
if(iz > 0)
E[offset] -= preFactorZ*(tmp_field[offset + 1] - tmp_field[offset_zm1 + 1]);
else
E[offset] -= preFactorZ*(tmp_field[offset + 1] - guardBZL[(blockSize*ix + iy)*3 + 1]);
}
}
}
getGuardF(E, guardE, DOWNWARDS);
}
void Simulation3D::implicitPSubstituteB() {
double* guardBL = guardB[0][0]; double* guardBH = guardB[0][1];
double* guardEH = guardE[0][1];
for(unsigned int iy=0; iy < blockSize; iy++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int guard_offsetB = (blockSize*iy + iz)*3 + 1;
unsigned int guard_offsetE = (blockSize*iy + iz)*3 + 2;
// E_z coupled with d B_y / dx
for(unsigned int ix=0; ix < blockSize; ix++) {
unsigned int B_offset2 = ((ix*blockSize + iy)*blockSize + iz)*3 + 1;
unsigned int B_offset = (((ix-1)*blockSize + iy)*blockSize + iz)*3 + 1;
unsigned int E_offset = (((ix)*blockSize + iy)*blockSize + iz)*3 + 2;
if(ix > 0)
E[E_offset] += preFactorX*(B[B_offset2] - B[B_offset]);
else
E[E_offset] += preFactorX*(B[B_offset2] - guardBL[guard_offsetB]);
}
}
}
guardBL = guardB[2][0]; guardBH = guardB[2][1]; guardEH = guardE[2][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int guard_offsetB = (blockSize*ix + iy)*3;
unsigned int guard_offsetE = (blockSize*ix + iy)*3 + 1;
// E_y coupled with d B_x / dz
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int B_offset2 = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + (iz-1))*3;
unsigned int E_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 1;
if(iz > 0)
E[E_offset] += preFactorZ*(B[B_offset2] - B[B_offset]);
else
E[E_offset] += preFactorZ*(B[B_offset2] - guardBL[guard_offsetB]);
}
}
}
guardBL = guardB[1][0]; guardBH = guardB[1][1]; guardEH = guardE[1][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int guard_offsetB = (blockSize*ix + iz)*3 + 2;
unsigned int guard_offsetE = (blockSize*ix + iz)*3;
// E_x coupled with d B_z / dy
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int B_offset = ((ix*blockSize + (iy-1))*blockSize + iz)*3 + 2;
unsigned int B_offset2 = ((ix*blockSize + iy)*blockSize + iz)*3 + 2;
unsigned int E_offset = ((ix*blockSize + iy)*blockSize + iz)*3;
if(iy > 0)
E[E_offset] += preFactorY*(B[B_offset2] - B[B_offset]);
else
E[E_offset] += preFactorY*(B[B_offset2] - guardBL[guard_offsetB]);
}
}
}
}
void Simulation3D::implicitMSubstituteB() {
double* guardBL = guardB[0][0]; double* guardBH = guardB[0][1];
double* guardEH = guardE[0][1];
for(unsigned int iy=0; iy < blockSize; iy++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int guard_offsetB = (blockSize*iy + iz)*3 + 2;
unsigned int guard_offsetE = (blockSize*iy + iz)*3 + 1;
// E_y coupled with d B_z / dx
for(unsigned int ix=0; ix < blockSize; ix++) {
unsigned int B_offset2 = ((ix*blockSize + iy)*blockSize + iz)*3 + 2;
unsigned int B_offset = (((ix-1)*blockSize + iy)*blockSize + iz)*3 + 2;
unsigned int E_offset = (((ix)*blockSize + iy)*blockSize + iz)*3 + 1;
if(ix > 0)
E[E_offset] -= preFactorX*(B[B_offset2] - B[B_offset]);
else {
E[E_offset] -= preFactorX*(B[B_offset2] - guardBL[guard_offsetB]);
}
}
}
}
guardBL = guardB[2][0]; guardBH = guardB[2][1]; guardEH = guardE[2][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int guard_offsetB = (blockSize*ix + iy)*3 + 1;
unsigned int guard_offsetE = (blockSize*ix + iy)*3;
// E_x coupled with d B_y / dz
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int B_offset2 = ((ix*blockSize + iy)*blockSize + iz)*3 + 1;
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + (iz-1))*3 + 1;
unsigned int E_offset = ((ix*blockSize + iy)*blockSize + iz)*3;
if(iz > 0)
E[E_offset] -= preFactorZ*(B[B_offset2] - B[B_offset]);
else
E[E_offset] -= preFactorZ*(B[B_offset2] - guardBL[guard_offsetB]);
}
}
}
guardBL = guardB[1][0]; guardBH = guardB[1][1]; guardEH = guardE[1][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int guard_offsetB = (blockSize*ix + iz)*3;
unsigned int guard_offsetE = (blockSize*ix + iz)*3 + 2;
// E_z coupled with d B_x / dy
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int B_offset = ((ix*blockSize + (iy-1))*blockSize + iz)*3;
unsigned int B_offset2 = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int E_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 2;
if(iy > 0)
E[E_offset] -= preFactorY*(B[B_offset2] - B[B_offset]);
else
E[E_offset] -= preFactorY*(B[B_offset2] - guardBL[guard_offsetB]);
}
}
}
}
void Simulation3D::populateRHSM() {
double* guardEH = guardE[0][1];
for(unsigned int iy=0; iy < blockSize; iy++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int rhs_offset = blockSize*blockSize*iy + blockSize*iz;
unsigned int guard_offset = (blockSize*iy + iz)*3 + 1;
// B_z coupled with d E_y / dx
for(unsigned int ix=0; ix < blockSize; ix++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 2;
unsigned int E_offset = (((ix)*blockSize + iy)*blockSize + iz)*3 + 1;
unsigned int E_offset2 = (((ix+1)*blockSize + iy)*blockSize + iz)*3 + 1;
if(ix+1 < blockSize)
rhsx[rhs_offset + ix] = B[B_offset] - preFactorX*(E[E_offset2]-E[E_offset]);
else {
rhsx[rhs_offset + ix] = B[B_offset] - preFactorX*(guardEH[guard_offset]-E[E_offset]);
}
}
}
}
guardEH = guardE[2][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int rhs_offset = blockSize*(blockSize*ix + iy);
unsigned int guard_offset = (blockSize*ix + iy)*3;
// B_y coupled with d E_x / dz
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 1;
unsigned int E_offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int E_offset2 = ((ix*blockSize + iy)*blockSize + (iz+1))*3;
if(iz+1 < blockSize)
rhsz[rhs_offset + iz] = B[B_offset] - preFactorZ*(E[E_offset2]-E[E_offset]);
else
rhsz[rhs_offset + iz] = B[B_offset] - preFactorZ*(guardEH[guard_offset] -E[E_offset]);
}
}
}
guardEH = guardE[1][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int rhs_offset = blockSize*(blockSize*ix + iz);
unsigned int guard_offset = (blockSize*ix + iz)*3 + 2;
// B_x coupled with d E_z / dy
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int E_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 2;
unsigned int E_offset2 = ((ix*blockSize + (iy+1))*blockSize + iz)*3 + 2;
if(iy+1 < blockSize)
rhsy[rhs_offset + iy] = B[B_offset] - preFactorY*(E[E_offset2]-E[E_offset]);
else
rhsy[rhs_offset + iy] = B[B_offset] - preFactorY*(guardEH[guard_offset]-E[E_offset]);
}
}
}
}
void Simulation3D::populateRHSP() {
double* guardEH = guardE[0][1];
for(unsigned int iy=0; iy < blockSize; iy++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int rhs_offset = blockSize*blockSize*iy + blockSize*iz;
unsigned int guard_offset = (blockSize*iy + iz)*3 + 2;
// B_y coupled with d E_z / dx
for(unsigned int ix=0; ix < blockSize; ix++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 1;
unsigned int E_offset = (((ix)*blockSize + iy)*blockSize + iz)*3 + 2;
unsigned int E_offset2 = (((ix+1)*blockSize + iy)*blockSize + iz)*3 + 2;
if(ix+1 < blockSize)
rhsx[rhs_offset + ix] = B[B_offset] + preFactorX*(E[E_offset2]-E[E_offset]);
else
rhsx[rhs_offset + ix] = B[B_offset] + preFactorX*(guardEH[guard_offset]-E[E_offset]);
}
}
}
guardEH = guardE[2][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int rhs_offset = blockSize*(blockSize*ix + iy);
unsigned int guard_offset = (blockSize*ix + iy)*3 + 1;
// B_x coupled with d E_y / dz
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int E_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 1;
unsigned int E_offset2 = ((ix*blockSize + iy)*blockSize + (iz+1))*3 + 1;
if(iz+1 < blockSize)
rhsz[rhs_offset + iz] = B[B_offset] + preFactorZ*(E[E_offset2]-E[E_offset]);
else
rhsz[rhs_offset + iz] = B[B_offset] + preFactorZ*(guardEH[guard_offset] -E[E_offset]);
}
}
}
guardEH = guardE[1][1];
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int rhs_offset = blockSize*(blockSize*ix + iz);
unsigned int guard_offset = (blockSize*ix + iz)*3;
// B_z coupled with d E_x / dy
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 2;
unsigned int E_offset = ((ix*blockSize + iy)*blockSize + iz)*3;
unsigned int E_offset2 = ((ix*blockSize + (iy+1))*blockSize + iz)*3;
if(iy+1 < blockSize)
rhsy[rhs_offset + iy] = B[B_offset] + preFactorY*(E[E_offset2]-E[E_offset]);
else {
rhsy[rhs_offset + iy] = B[B_offset] + preFactorY*(guardEH[guard_offset]-E[E_offset]);
}
}
}
}
}
void Simulation3D::writeRHSP() {
for(unsigned int iy=0; iy < blockSize; iy++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int rhs_offset = blockSize*blockSize*iy + blockSize*iz;
for(unsigned int ix=0; ix < blockSize; ix++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 1;
B[B_offset] = rhsx[rhs_offset + ix];
}
}
}
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int rhs_offset = blockSize*(blockSize*ix + iy);
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3;
B[B_offset] = rhsz[rhs_offset + iz];
}
}
}
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int rhs_offset = blockSize*(blockSize*ix + iz);
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 2;
B[B_offset] = rhsy[rhs_offset + iy];
}
}
}
}
void Simulation3D::writeRHSM() {
for(unsigned int iy=0; iy < blockSize; iy++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int rhs_offset = blockSize*blockSize*iy + blockSize*iz;
for(unsigned int ix=0; ix < blockSize; ix++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 2;
B[B_offset] = rhsx[rhs_offset + ix];
}
}
}
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int rhs_offset = blockSize*(blockSize*ix + iy);
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3 + 1;
B[B_offset] = rhsz[rhs_offset + iz];
}
}
}
for(unsigned int ix=0; ix < blockSize; ix++) {
for(unsigned int iz=0; iz < blockSize; iz++) {
unsigned int rhs_offset = blockSize*(blockSize*ix + iz);
for(unsigned int iy=0; iy < blockSize; iy++) {
unsigned int B_offset = ((ix*blockSize + iy)*blockSize + iz)*3;
B[B_offset] = rhsy[rhs_offset + iy];
}
}
}
}
// FIXME - THIS IS A VERY NAUGHTY FUNCTION. YOU SHOULD BE ASHAMED
void Simulation3D::getGuardF(double* F, double*** guardF, commDir dir) {
int p = xLine.rank();
int n_p = xLine.size();
unsigned int i_send = dir == UPWARDS ? blockSize - 1 : 0;
int p_shift = dir == UPWARDS ? 1 : -1;
double* guardDest = dir == UPWARDS ? guardF[0][0] : guardF[0][1];
bool up = dir == UPWARDS;
bool partner_up = ((p + p_shift >= 0) && !up) || ((p + p_shift < n_p) && up);
bool partner_down = ((p - p_shift >= 0) && up) || ((p - p_shift < n_p) && !up);
fillSendbufX(i_send, F);
if(p % 2 == 0) {
if(partner_up)
xLine.send(p + p_shift, 0, guardSendbuf, 3*blockSize*blockSize);
if(partner_down)
xLine.recv(p - p_shift, 0, guardDest, 3*blockSize*blockSize);
else if (F == B) {
i_send = i_send == 0 ? blockSize - 1 : 0;
fillSendbufX(i_send, F);
std::copy(guardSendbuf, guardSendbuf + 3*blockSize*blockSize, guardDest);
}
else
std::fill_n(guardDest, 3*blockSize*blockSize, 0);
}
else {
if(partner_down)
xLine.recv(p - p_shift, 0, guardDest, 3*blockSize*blockSize);
else if (F == B) {
i_send = i_send == 0 ? blockSize - 1 : 0;
fillSendbufX(i_send, F);
std::copy(guardSendbuf, guardSendbuf + 3*blockSize*blockSize, guardDest);
}
else
std::fill_n(guardDest, 3*blockSize*blockSize, 0);
if(partner_up)
xLine.send(p + p_shift, 0, guardSendbuf, 3*blockSize*blockSize);
}
p = yLine.rank();
n_p = yLine.size();
i_send = dir == UPWARDS ? blockSize - 1 : 0;
partner_up = ((p + p_shift >= 0) && !up) || ((p + p_shift < n_p) && up);
partner_down = ((p - p_shift >= 0) && up) || ((p - p_shift < n_p) && !up);
guardDest = dir == UPWARDS ? guardF[1][0] : guardF[1][1];
fillSendbufY(i_send, F);
if(p % 2 == 0) {
if(partner_up)
yLine.send(p + p_shift, 0, guardSendbuf, 3*blockSize*blockSize);
if(partner_down)
yLine.recv(p - p_shift, 0, guardDest, 3*blockSize*blockSize);
else if (F == B) {
i_send = i_send == 0 ? blockSize - 1 : 0;
fillSendbufY(i_send, F);
std::copy(guardSendbuf, guardSendbuf + 3*blockSize*blockSize, guardDest);
}
else
std::fill_n(guardDest, 3*blockSize*blockSize, 0);
}
else {
if(partner_down) {
yLine.recv(p - p_shift, 0, guardDest, 3*blockSize*blockSize);
}
else if (F == B) {
i_send = i_send == 0 ? blockSize - 1 : 0;
fillSendbufY(i_send, F);
std::copy(guardSendbuf, guardSendbuf + 3*blockSize*blockSize, guardDest);
}
else
std::fill_n(guardDest, 3*blockSize*blockSize, 0);
if(partner_up)
yLine.send(p + p_shift, 0, guardSendbuf, 3*blockSize*blockSize);
}
p = zLine.rank();
n_p = zLine.size();
i_send = dir == UPWARDS ? blockSize - 1 : 0;
partner_up = ((p + p_shift >= 0) && !up) || ((p + p_shift < n_p) && up);
partner_down = ((p - p_shift >= 0) && up) || ((p - p_shift < n_p) && !up);
guardDest = dir == UPWARDS ? guardF[2][0] : guardF[2][1];
fillSendbufZ(i_send, F);
if(p % 2 == 0) {
if(partner_up)
zLine.send(p + p_shift, 0, guardSendbuf, 3*blockSize*blockSize);
if(partner_down)
zLine.recv(p - p_shift, 0, guardDest, 3*blockSize*blockSize);
else if (F == B) {
i_send = i_send == 0 ? blockSize - 1 : 0;
fillSendbufZ(i_send, F);
std::copy(guardSendbuf, guardSendbuf + 3*blockSize*blockSize, guardDest);
}