forked from christophmschaefer/miluphcuda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrk2adaptive.cu
1350 lines (1239 loc) · 55.2 KB
/
rk2adaptive.cu
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
/**
* @author Christoph Schaefer [email protected] and Thomas I. Maindl
*
* @section LICENSE
* Copyright (c) 2019 Christoph Schaefer
*
* This file is part of miluphcuda.
*
* miluphcuda 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.
*
* miluphcuda 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 miluphcuda. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "rk2adaptive.h"
#include "miluph.h"
#include "timeintegration.h"
#include "parameter.h"
#include "memory_handling.h"
#include "rhs.h"
#include "pressure.h"
#include "boundary.h"
extern __device__ double endTimeD, currentTimeD;
extern __device__ double substep_currentTimeD;
extern __device__ double dt;
extern __device__ double dtmax;
extern __device__ int isRelaxed;
extern __device__ int blockCount;
extern __device__ int errorSmallEnough;
extern __device__ double dtNewErrorCheck;
extern __device__ double dtNewAlphaCheck;
extern __device__ double maxPosAbsError;
extern __constant__ double b21;
extern __constant__ double b31;
extern __constant__ double b32;
extern __constant__ double c1;
extern __constant__ double c2;
extern __constant__ double c3;
extern __device__ double maxVelAbsError;
extern __device__ double maxDensityAbsError;
extern __device__ double maxEnergyAbsError;
extern __device__ double maxPressureAbsChange;
extern __device__ double maxDamageTimeStep;
extern __device__ double maxalphaDiff;
extern __constant__ double safety;
__constant__ __device__ double rk_epsrel_d;
extern double L_ini;
__global__ void limitTimestep(double *forcesPerBlock , double *courantPerBlock)
{
__shared__ double sharedForces[NUM_THREADS_LIMITTIMESTEP];
__shared__ double sharedCourant[NUM_THREADS_LIMITTIMESTEP];
int i, j, k, m;
double forces = 1e100, courant = 1e100;
double temp;
double sml;
double ax;
#if DIM > 1
double ay;
#endif
#if DIM == 3
double az;
#endif
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i+= blockDim.x * gridDim.x) {
ax = p.ax[i];
#if DIM > 1
ay = p.ay[i];
#endif
#if DIM == 3
az = p.az[i];
#endif
temp = ax*ax;
#if DIM > 1
temp += ay*ay;
#endif
#if DIM == 3
temp += az*az;
#endif
sml = p.h[i];
if (temp > 0) {
temp = sqrt(sml / sqrt(temp));
forces = min(forces, temp);
}
temp = sml / p.cs[i];
courant = min(courant, temp);
}
i = threadIdx.x;
sharedForces[i] = forces;
sharedCourant[i] = courant;
for (j = NUM_THREADS_LIMITTIMESTEP / 2; j > 0; j /= 2) {
__syncthreads();
if (i < j) {
k = i + j;
sharedForces[i] = forces = min(forces, sharedForces[k]);
sharedCourant[i] = courant = min(courant, sharedCourant[k]);
}
}
// write block result to global memory
if (i == 0) {
k = blockIdx.x;
forcesPerBlock[k] = forces;
courantPerBlock[k] = courant;
m = gridDim.x - 1;
if (m == atomicInc((unsigned int *)&blockCount, m)) {
// last block, so combine all block results
for (j = 0; j <= m; j++) {
forces = min(forces, forcesPerBlock[j]);
courant = min(courant, courantPerBlock[j]);
}
// set new timestep
dt = min(COURANT*courant, forces*0.2);
dt = min(dt, endTimeD - currentTimeD);
if (dt > dtmax) {
printf("<limittimestep> timestep %g is larger than maximum timestep %g, reducing to %g\n", dt, dtmax, dtmax);
dt = dtmax;
}
// reset block count
blockCount = 0;
}
}
}
/*
the runge-kutta 2nd order integrator with adaptive timestep
see cuda-paper for details
*/
void rk2Adaptive()
{
int rkstep;
int errorSmallEnough_host;
double dtNewErrorCheck_host = 0.0;
#if PALPHA_POROSITY
double dtNewAlphaCheck_host = -1.0;
double dt_alphanew = 0;
double dt_alphaold = 0;
#endif
double *maxPosAbsErrorPerBlock;
double *maxVelAbsErrorPerBlock;
#if FRAGMENTATION
double dt_damagenew = 0;
double dt_damageold = 0;
#endif
/* first of all copy the rk_epsrel to the device */
cudaVerify(cudaMemcpyToSymbol(rk_epsrel_d, ¶m.rk_epsrel, sizeof(double)));
// allocate memory for runge kutta second order
cudaVerify(cudaMalloc((void**)&maxPosAbsErrorPerBlock, sizeof(double)*numberOfMultiprocessors));
cudaVerify(cudaMalloc((void**)&maxVelAbsErrorPerBlock, sizeof(double)*numberOfMultiprocessors));
#if INTEGRATE_DENSITY
double *maxDensityAbsErrorPerBlock;
cudaVerify(cudaMalloc((void**)&maxDensityAbsErrorPerBlock , sizeof(double)*numberOfMultiprocessors));
#endif
#if INTEGRATE_ENERGY
double *maxEnergyAbsErrorPerBlock;
cudaVerify(cudaMalloc((void**)&maxEnergyAbsErrorPerBlock, sizeof(double)*numberOfMultiprocessors));
#endif
#if FRAGMENTATION
double *maxDamageTimeStepPerBlock;
cudaVerify(cudaMalloc((void**)&maxDamageTimeStepPerBlock, sizeof(double)*numberOfMultiprocessors));
#endif
#if PALPHA_POROSITY
double *maxalphaDiffPerBlock;
cudaVerify(cudaMalloc((void**)&maxalphaDiffPerBlock, sizeof(double)*numberOfMultiprocessors));
double *maxPressureAbsChangePerBlock;
cudaVerify(cudaMalloc((void**)&maxPressureAbsChangePerBlock, sizeof(double)*numberOfMultiprocessors));
#endif
// alloc mem for multiple rhs and copy immutables
int allocate_immutables = 0;
for (rkstep = 0; rkstep < 3; rkstep++) {
allocate_particles_memory(&rk_device[rkstep], allocate_immutables);
copy_particles_immutables_device_to_device(&rk_device[rkstep], &p_device);
#if GRAVITATING_POINT_MASSES
allocate_pointmass_memory(&rk_pointmass_device[rkstep], allocate_immutables);
copy_pointmass_immutables_device_to_device(&rk_pointmass_device[rkstep], &pointmass_device);
#endif
}
// set the symbol pointers
cudaVerify(cudaMemcpyToSymbol(rk, &rk_device, sizeof(struct Particle) * 3));
#if GRAVITATING_POINT_MASSES
cudaVerify(cudaMemcpyToSymbol(rk_pointmass, &rk_pointmass_device, sizeof(struct Pointmass) * 3));
#endif
int lastTimestep = startTimestep + numberOfTimesteps;
int timestep;
int nsteps_cnt = 0;
double dt_host_old = timePerStep;
currentTime = startTime;
double endTime = startTime;
double substep_currentTime;
cudaVerify(cudaDeviceSynchronize());
cudaVerify(cudaMemcpyToSymbol(currentTimeD, ¤tTime, sizeof(double)));
for (timestep = startTimestep; timestep < lastTimestep; timestep++) {
fprintf(stderr, "calculating step %d\n", timestep);
fprintf(stdout, "\nstep %d / %d\n", timestep, lastTimestep);
endTime += timePerStep;
fprintf(stdout, "currenttime: %e \t endtime: %e\n", currentTime, endTime);
if (nsteps_cnt == 0) {
if (timePerStep > param.maxtimestep) {
cudaVerify(cudaMemcpyToSymbol(dt, ¶m.maxtimestep, sizeof(double)));
dt_host_old = param.maxtimestep;
} else {
cudaVerify(cudaMemcpyToSymbol(dt, &timePerStep, sizeof(double)));
}
if (param.verbose) fprintf(stdout, "Starting with timestep %.17e\n", dt_host_old);
} else {
cudaVerify(cudaMemcpyToSymbol(dt, &dt_host_old, sizeof(double)));
dt_host = dt_host_old;
if (param.verbose) fprintf(stdout, "Continuing with timestep %.17e\n", dt_host_old);
}
nsteps_cnt++;
cudaVerify(cudaMemcpyToSymbol(endTimeD, &endTime, sizeof(double)));
// checking for changes in angular momentum
if (param.angular_momentum_check > 0) {
double L_current = calculate_angular_momentum();
double L_change_relative;
if (L_ini > 0) {
L_change_relative = fabs((L_ini - L_current)/L_ini);
}
if (param.verbose) {
fprintf(stdout, "Checking angular momentum conservation.\n");
fprintf(stdout, "Initial angular momentum: %.17e\n", L_ini);
fprintf(stdout, "Current angular momentum: %.17e\n", L_current);
fprintf(stdout, "Relative change: %.17e\n", L_change_relative);
}
if (L_change_relative > param.angular_momentum_check) {
fprintf(stderr, "Conservation of angular momentum violated. Exiting.\n");
exit(111);
}
}
while (currentTime < endTime) {
// get the correct time
substep_currentTime = currentTime;
cudaVerify(cudaMemcpyToSymbol(substep_currentTimeD, &substep_currentTime, sizeof(double)));
cudaVerify(cudaDeviceSynchronize());
// copy particle data to first runge kutta step
copy_particles_variables_device_to_device(&rk_device[RKFIRST], &p_device);
cudaVerify(cudaDeviceSynchronize());
#if GRAVITATING_POINT_MASSES
copy_pointmass_variables_device_to_device(&rk_pointmass_device[RKFIRST], &pointmass_device);
cudaVerify(cudaDeviceSynchronize());
#endif
// calculate first right hand side with rk[RKFIRST]_device
cudaVerify(cudaMemcpyToSymbol(p, &rk_device[RKFIRST], sizeof(struct Particle)));
cudaVerify(cudaMemcpyToSymbol(pointmass, &rk_pointmass_device[RKFIRST], sizeof(struct Pointmass)));
rightHandSide();
cudaVerify(cudaDeviceSynchronize());
#if FRAGMENTATION
cudaVerify(cudaMemcpyFromSymbol(&dt_damageold, dt, sizeof(double)));
/* add function for best timestep with fragmentation here */
cudaVerifyKernel((damageMaxTimeStep<<<numberOfMultiprocessors, NUM_THREADS_ERRORCHECK>>>(
maxDamageTimeStepPerBlock
)));
cudaVerify(cudaMemcpyFromSymbol(&dt_damagenew, dt, sizeof(double)));
if (dt_damagenew < dt_damageold && param.verbose) {
fprintf(stdout, "current time: %e \t\t reducing timestep due to damage evolution from suggested time step %g to %g\n", currentTime, dt_damageold, dt_damagenew);
dt_host = dt_damagenew;
dt_host_old = dt_host;
}
#endif
// remember values of first step
copy_particles_variables_device_to_device(&rk_device[RKSTART], &rk_device[RKFIRST]);
copy_particles_derivatives_device_to_device(&rk_device[RKSTART], &rk_device[RKFIRST]);
#if GRAVITATING_POINT_MASSES
copy_pointmass_variables_device_to_device(&rk_pointmass_device[RKSTART], &rk_pointmass_device[RKFIRST]);
copy_pointmass_derivatives_device_to_device(&rk_pointmass_device[RKSTART], &rk_pointmass_device[RKFIRST]);
#endif
// remember accels due to gravity
if (param.selfgravity) {
copy_gravitational_accels_device_to_device(&rk_device[RKSTART], &rk_device[RKFIRST]);
}
#define SMALLEST_DT_ALLOWED 1e-30
// integrate with adaptive timestep
while (TRUE) {
cudaVerify(cudaDeviceSynchronize());
// set rk[RKFIRST] variables
cudaVerifyKernel((integrateFirstStep<<<numberOfMultiprocessors, NUM_THREADS_RK2_INTEGRATE_STEP>>>()));
cudaVerify(cudaDeviceSynchronize());
// get derivatives for second step
// this happens at t = t0 + h/2
cudaVerify(cudaMemcpyFromSymbol(&dt_host, dt, sizeof(double)));
if (dt_host < SMALLEST_DT_ALLOWED) {
fprintf(stderr, "Timestep is smaller than SMALLEST_DT_ALLOWED. Stopping here.\n");
exit(1);
}
substep_currentTime = currentTime + dt_host*0.5;
cudaVerify(cudaMemcpyToSymbol(substep_currentTimeD, &substep_currentTime, sizeof(double)));
cudaVerify(cudaMemcpyToSymbol(p, &rk_device[RKFIRST], sizeof(struct Particle)));
#if GRAVITATING_POINT_MASSES
cudaVerify(cudaMemcpyToSymbol(pointmass, &rk_pointmass_device[RKFIRST], sizeof(struct Pointmass)));
#endif
rightHandSide();
cudaVerify(cudaDeviceSynchronize());
// integrate second step
cudaVerifyKernel((integrateSecondStep<<<numberOfMultiprocessors, NUM_THREADS_RK2_INTEGRATE_STEP>>>()));
cudaVerify(cudaDeviceSynchronize());
if (param.selfgravity) {
copy_gravitational_accels_device_to_device(&rk_device[RKSECOND], &rk_device[RKFIRST]);
}
// get derivatives for the 3rd (and last) step
// this happens at t = t0 + h
cudaVerify(cudaMemcpyToSymbol(p, &rk_device[RKSECOND], sizeof(struct Particle)));
#if GRAVITATING_POINT_MASSES
cudaVerify(cudaMemcpyToSymbol(pointmass, &rk_pointmass_device[RKSECOND], sizeof(struct Pointmass)));
#endif
substep_currentTime = currentTime + dt_host;
cudaVerify(cudaMemcpyToSymbol(substep_currentTimeD, &substep_currentTime, sizeof(double)));
rightHandSide();
cudaVerify(cudaDeviceSynchronize());
// integrate third step
cudaVerify(cudaMemcpyToSymbol(p, &p_device, sizeof(struct Particle)));
#if GRAVITATING_POINT_MASSES
cudaVerify(cudaMemcpyToSymbol(pointmass, &pointmass_device, sizeof(struct Pointmass)));
#endif
cudaVerifyKernel((integrateThirdStep<<<numberOfMultiprocessors, NUM_THREADS_RK2_INTEGRATE_STEP>>>()));
cudaVerify(cudaDeviceSynchronize());
// calculate errors
// following Stephen Oxley 1999, Modelling the Capture Theory for the
// Origin of Planetary Systems
cudaVerifyKernel((checkError<<<numberOfMultiprocessors, NUM_THREADS_ERRORCHECK>>>(
maxPosAbsErrorPerBlock, maxVelAbsErrorPerBlock
#if INTEGRATE_DENSITY
, maxDensityAbsErrorPerBlock
#endif
#if INTEGRATE_ENERGY
, maxEnergyAbsErrorPerBlock
#endif
#if PALPHA_POROSITY
, maxPressureAbsChangePerBlock
#endif
)));
/* get info about the quality of the time step: if errorSmallEnough is TRUE, then
the integration is successful and the timestep size is raised. if errorSmallEnough
is FALSE, the timestep size is lowered and the step is repeated */
cudaVerify(cudaDeviceSynchronize());
cudaVerify(cudaMemcpyFromSymbol(&dtNewErrorCheck_host, dtNewErrorCheck, sizeof(double)));
cudaVerify(cudaMemcpyFromSymbol(&errorSmallEnough_host, errorSmallEnough, sizeof(int)));
#if PALPHA_POROSITY
/* special checks for the convergence of the p(alpha) crush curve stuff */
if (errorSmallEnough_host) {
dt_alphaold = dt_host;
//cudaVerify(cudaDeviceSynchronize());
//cudaVerify(cudaMemcpyFromSymbol(&dt_alphaold, dtNewErrorCheck, sizeof(double)));
/* checking if the distention change is within the set limit */
cudaVerifyKernel((alphaMaxTimeStep<<<numberOfMultiprocessors, NUM_THREADS_ERRORCHECK>>>(
maxalphaDiffPerBlock
)));
cudaVerify(cudaMemcpyFromSymbol(&dt_alphanew, dtNewAlphaCheck, sizeof(double)));
if (dt_alphanew < dt_alphaold && param.verbose && dt_alphanew > 0) {
fprintf(stdout, "current time step: %e is too large for distention. lowering it to %e\n", dt_alphaold, dt_alphanew);
}
}
dtNewAlphaCheck_host = -1.0;
cudaVerify(cudaDeviceSynchronize());
cudaVerify(cudaMemcpyFromSymbol(&dtNewAlphaCheck_host, dtNewAlphaCheck, sizeof(double)));
cudaVerify(cudaMemcpyFromSymbol(&errorSmallEnough_host, errorSmallEnough, sizeof(int)));
#endif
/* last time step was okay, forward time and continue with new time step size */
if (errorSmallEnough_host) {
currentTime += dt_host;
cudaVerifyKernel((BoundaryConditionsAfterIntegratorStep<<<numberOfMultiprocessors, NUM_THREADS_ERRORCHECK>>>(interactions)));
}
double errPos, errVel, errDensity = 0;
cudaVerify(cudaMemcpyFromSymbol(&errPos, maxPosAbsError, sizeof(double)));
cudaVerify(cudaMemcpyFromSymbol(&errVel, maxVelAbsError, sizeof(double)));
#if INTEGRATE_DENSITY
cudaVerify(cudaMemcpyFromSymbol(&errDensity, maxDensityAbsError, sizeof(double)));
#endif
cudaVerify(cudaDeviceSynchronize());
if (param.verbose) printf("total relative max error: %g (locations: %e, velocities: %e, density: %e) with timestep %e\n", max(max(errPos, errVel), errDensity) / param.rk_epsrel, errPos, errVel, errDensity, dt_host);
#if PALPHA_POROSITY
if (param.verbose)
printf("Current time: %g \t dt: %g \t dtNewErrorCheck: %g \t dtNewAlphaCheck: %g \n", currentTime, dt_host, dtNewErrorCheck_host, dtNewAlphaCheck_host);
#endif
/* set new time step for next step */
#if PALPHA_POROSITY
dt_host_old = dt_host;
if (dtNewAlphaCheck_host <= 0) {
dt_host = dtNewErrorCheck_host;
} else {
dt_host = min(dtNewErrorCheck_host, dtNewAlphaCheck_host);
}
#else
dt_host_old = dt_host;
dt_host = dtNewErrorCheck_host;
#endif
/* check if time step is too large */
/* and lower if necessary */
if (currentTime + dt_host > endTime) {
dt_host_old = dt_host;
dt_host = endTime - currentTime;
}
cudaVerify(cudaDeviceSynchronize());
/* tell the gpu the new time step size and the current time */
cudaVerify(cudaMemcpyToSymbol(currentTimeD, ¤tTime, sizeof(double)));
cudaVerify(cudaMemcpyToSymbol(dt, &dt_host, sizeof(double)));
if (errorSmallEnough_host) {
cudaVerify(cudaMemcpyFromSymbol(¤tTime, currentTimeD, sizeof(double)));
if (param.verbose) {
fprintf(stdout, "last error small enough: current time %.17e with timestep %.17e new timestep %.17e, time to next output is %.17e \n", currentTime, dt_host_old, dt_host, endTime-currentTime);
}
break; // break while(true) -> continue with next timestep
} else {
// integration not successful, dt has been lowered, try another round
if (param.verbose) {
fprintf(stdout, "error too large >>>>>>>>>>>> current time: %e timestep lowered to %e\n", currentTime, dt_host);
}
// copy back the initial values of particles
copy_particles_variables_device_to_device(&rk_device[RKFIRST], &rk_device[RKSTART]);
copy_particles_derivatives_device_to_device(&rk_device[RKFIRST], &rk_device[RKSTART]);
#if GRAVITATING_POINT_MASSES
copy_pointmass_variables_device_to_device(&rk_pointmass_device[RKFIRST], &rk_pointmass_device[RKSTART]);
copy_pointmass_derivatives_device_to_device(&rk_pointmass_device[RKFIRST], &rk_pointmass_device[RKSTART]);
#endif
cudaVerify(cudaDeviceSynchronize());
}
} // loop until error small enough
} // current time < end time loop
// write results
#if FRAGMENTATION
cudaVerify(cudaDeviceSynchronize());
cudaVerifyKernel((damageLimit<<<numberOfMultiprocessors*4, NUM_THREADS_PC_INTEGRATOR>>>()));
cudaVerify(cudaDeviceSynchronize());
#endif
copyToHostAndWriteToFile(timestep, lastTimestep);
} // timestep loop
// free memory
// free mem of rksteps
int free_immutables = 0;
for (rkstep = 0; rkstep < 3; rkstep++) {
free_particles_memory(&rk_device[rkstep], free_immutables);
#if GRAVITATING_POINT_MASSES
free_pointmass_memory(&rk_pointmass_device[rkstep], free_immutables);
#endif
}
cudaVerify(cudaFree(maxPosAbsErrorPerBlock));
cudaVerify(cudaFree(maxVelAbsErrorPerBlock));
#if FRAGMENTATION
cudaVerify(cudaFree(maxDamageTimeStepPerBlock));
#endif
#if INTEGRATE_ENERGY
cudaVerify(cudaFree(maxEnergyAbsErrorPerBlock));
#endif
#if INTEGRATE_DENSITY
cudaVerify(cudaFree(maxDensityAbsErrorPerBlock));
#endif
#if PALPHA_POROSITY
cudaVerify(cudaFree(maxalphaDiffPerBlock));
#endif
}
__global__ void integrateFirstStep(void)
{
int i;
#if GRAVITATING_POINT_MASSES
// loop for the point masses
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numPointmasses; i+= blockDim.x * gridDim.x) {
rk_pointmass[RKFIRST].x[i] = rk_pointmass[RKSTART].x[i] + dt * b21 * rk_pointmass[RKSTART].vx[i];
#if DIM > 1
rk_pointmass[RKFIRST].y[i] = rk_pointmass[RKSTART].y[i] + dt * b21 * rk_pointmass[RKSTART].vy[i];
#endif
#if DIM > 2
rk_pointmass[RKFIRST].z[i] = rk_pointmass[RKSTART].z[i] + dt * b21 * rk_pointmass[RKSTART].vz[i];
#endif
rk_pointmass[RKFIRST].vx[i] = rk_pointmass[RKSTART].vx[i] + dt * b21 * rk_pointmass[RKSTART].ax[i];
#if DIM > 1
rk_pointmass[RKFIRST].vy[i] = rk_pointmass[RKSTART].vy[i] + dt * b21 * rk_pointmass[RKSTART].ay[i];
#endif
#if DIM > 2
rk_pointmass[RKFIRST].vz[i] = rk_pointmass[RKSTART].vz[i] + dt * b21 * rk_pointmass[RKSTART].az[i];
#endif
}
#endif // GRAVITATING_POINT_MASSES
// loop for the particles
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i+= blockDim.x * gridDim.x) {
//printf("START: vx: %g \t %g :dxdt \t\t\t vy: %g \t %g :dydt\n", velxStart[i], dxdtStart[i], velyStart[i], dydtStart[i]);
#if INTEGRATE_DENSITY
rk[RKFIRST].rho[i] = rk[RKSTART].rho[i] + dt * b21 * rk[RKSTART].drhodt[i];
#endif
#if INTEGRATE_SML
rk[RKFIRST].h[i] = rk[RKSTART].h[i] + dt * b21 * rk[RKSTART].dhdt[i];
#else
rk[RKFIRST].h[i] = rk[RKSTART].h[i];
#endif
#if INTEGRATE_ENERGY
rk[RKFIRST].e[i] = rk[RKSTART].e[i] + dt * b21 * rk[RKSTART].dedt[i];
#endif
#if FRAGMENTATION
rk[RKFIRST].d[i] = rk[RKSTART].d[i] + dt * b21 * rk[RKSTART].dddt[i];
rk[RKFIRST].numActiveFlaws[i] = rk[RKSTART].numActiveFlaws[i];
#if PALPHA_POROSITY
rk[RKFIRST].damage_porjutzi[i] = rk[RKSTART].damage_porjutzi[i] + dt * b21 * rk[RKSTART].ddamage_porjutzidt[i];
#endif
#endif
#if INVISCID_SPH
rk[RKFIRST].beta[i] = rk[RKSTART].beta[i] + dt * b21 * rk[RKSTART].dbetadt[i];
#endif
#if SOLID
int j, k;
for (j = 0; j < DIM; j++) {
for (k = 0; k < DIM; k++) {
rk[RKFIRST].S[stressIndex(i,j,k)] = rk[RKSTART].S[stressIndex(i,j,k)] + dt * b21 * rk[RKSTART].dSdt[stressIndex(i,j,k)];
}
}
#endif
#if JC_PLASTICITY
rk[RKFIRST].ep[i] = rk[RKSTART].ep[i] + dt * b21 * rk[RKSTART].edotp[i];
rk[RKFIRST].T[i] = rk[RKSTART].T[i] + dt * b21 * rk[RKSTART].dTdt[i];
#endif
#if PALPHA_POROSITY
rk[RKFIRST].alpha_jutzi[i] = rk[RKSTART].alpha_jutzi[i] + dt * b21 * rk[RKSTART].dalphadt[i];
// rk[RKFIRST].p is the pressure at the begin of the new timestep
// this pressure has to be compared to the pressure at the end of the timestep
rk[RKFIRST].pold[i] = rk[RKFIRST].p[i];
#endif
#if SIRONO_POROSITY
rk[RKFIRST].rho_0prime[i] = rk[RKSTART].rho_0prime[i];
rk[RKFIRST].rho_c_plus[i] = rk[RKSTART].rho_c_plus[i];
rk[RKFIRST].rho_c_minus[i] = rk[RKSTART].rho_c_minus[i];
rk[RKFIRST].compressive_strength[i] = rk[RKSTART].compressive_strength[i];
rk[RKFIRST].tensile_strength[i] = rk[RKSTART].tensile_strength[i];
rk[RKFIRST].shear_strength[i] = rk[RKSTART].shear_strength[i];
rk[RKFIRST].K[i] = rk[RKSTART].K[i];
rk[RKFIRST].flag_rho_0prime[i] = rk[RKSTART].flag_rho_0prime[i];
rk[RKFIRST].flag_plastic[i] = rk[RKSTART].flag_plastic[i];
#endif
#if EPSALPHA_POROSITY
rk[RKFIRST].alpha_epspor[i] = rk[RKSTART].alpha_epspor[i] + dt * b21 * rk[RKSTART].dalpha_epspordt[i];
rk[RKFIRST].epsilon_v[i] = rk[RKSTART].epsilon_v[i] + dt * b21 * rk[RKSTART].depsilon_vdt[i];
#endif
rk[RKFIRST].x[i] = rk[RKSTART].x[i] + dt * b21 * rk[RKSTART].dxdt[i];
#if DIM > 1
rk[RKFIRST].y[i] = rk[RKSTART].y[i] + dt * b21 * rk[RKSTART].dydt[i];
#endif
#if DIM > 2
rk[RKFIRST].z[i] = rk[RKSTART].z[i] + dt * b21 * rk[RKSTART].dzdt[i];
#endif
rk[RKFIRST].vx[i] = rk[RKSTART].vx[i] + dt * b21 * rk[RKSTART].ax[i];
#if DIM > 1
rk[RKFIRST].vy[i] = rk[RKSTART].vy[i] + dt * b21 * rk[RKSTART].ay[i];
#endif
#if DIM > 2
rk[RKFIRST].vz[i] = rk[RKSTART].vz[i] + dt * b21 * rk[RKSTART].az[i];
#endif
}
}
__global__ void integrateSecondStep(void)
{
int i;
#if GRAVITATING_POINT_MASSES
// loop for pointmasses
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numPointmasses; i+= blockDim.x * gridDim.x) {
rk_pointmass[RKSECOND].vx[i] = rk_pointmass[RKSTART].vx[i] + dt * (b31 * rk_pointmass[RKSTART].ax[i] + b32 * rk_pointmass[RKFIRST].ax[i]);
#if DIM > 1
rk_pointmass[RKSECOND].vy[i] = rk_pointmass[RKSTART].vy[i] + dt * (b31 * rk_pointmass[RKSTART].ay[i] + b32 * rk_pointmass[RKFIRST].ay[i]);
#endif
#if DIM == 3
rk_pointmass[RKSECOND].vz[i] = rk_pointmass[RKSTART].vz[i] + dt * (b31 * rk_pointmass[RKSTART].az[i] + b32 * rk_pointmass[RKFIRST].az[i]);
#endif
rk_pointmass[RKSECOND].x[i] = rk_pointmass[RKSTART].x[i] + dt * (b31 * rk_pointmass[RKSTART].vx[i] + b32 * rk_pointmass[RKFIRST].vx[i]);
#if DIM > 1
rk_pointmass[RKSECOND].y[i] = rk_pointmass[RKSTART].y[i] + dt * (b31 * rk_pointmass[RKSTART].vy[i] + b32 * rk_pointmass[RKFIRST].vy[i]);
#endif
#if DIM == 3
rk_pointmass[RKSECOND].z[i] = rk_pointmass[RKSTART].z[i] + dt * (b31 * rk_pointmass[RKSTART].vz[i] + b32 * rk_pointmass[RKFIRST].vz[i]);
#endif
}
#endif // GRAVITATING_POINT_MASSES
// loop for particles
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i+= blockDim.x * gridDim.x) {
#if INTEGRATE_DENSITY
rk[RKSECOND].rho[i] = rk[RKSTART].rho[i] + dt * (b31 * rk[RKSTART].drhodt[i] + b32 * rk[RKFIRST].drhodt[i]);
#endif
#if INTEGRATE_SML
rk[RKSECOND].h[i] = rk[RKSTART].h[i] + dt * (b31 * rk[RKSTART].dhdt[i] + b32 * rk[RKFIRST].dhdt[i]);
#else
rk[RKSECOND].h[i] = rk[RKSTART].h[i];
#endif
#if INTEGRATE_ENERGY
rk[RKSECOND].e[i] = rk[RKSTART].e[i] + dt * (b31 * rk[RKSTART].dedt[i] + b32 * rk[RKFIRST].dedt[i]);
#endif
#if FRAGMENTATION
rk[RKSECOND].d[i] = rk[RKSTART].d[i] + dt * (b31 * rk[RKSTART].dddt[i] + b32 * rk[RKFIRST].dddt[i]);
rk[RKSECOND].numActiveFlaws[i] = rk[RKFIRST].numActiveFlaws[i];
#if PALPHA_POROSITY
rk[RKSECOND].damage_porjutzi[i] = rk[RKSTART].damage_porjutzi[i] + dt * (b31 * rk[RKSTART].ddamage_porjutzidt[i] + b32 * rk[RKFIRST].ddamage_porjutzidt[i]);
#endif
#endif
#if JC_PLASTICITY
rk[RKSECOND].ep[i] = rk[RKSTART].ep[i] + dt * (b31 * rk[RKSTART].edotp[i] + b32 * rk[RKFIRST].edotp[i]);
rk[RKSECOND].T[i] = rk[RKSTART].T[i] + dt * (b31 * rk[RKSTART].dTdt[i] + b32 * rk[RKFIRST].dTdt[i]);
#endif
#if PALPHA_POROSITY
rk[RKSECOND].alpha_jutzi[i] = rk[RKSTART].alpha_jutzi[i] + dt * (b31 * rk[RKSTART].dalphadt[i] + b32 * rk[RKFIRST].dalphadt[i]);
rk[RKSECOND].pold[i] = rk[RKFIRST].pold[i];
#endif
#if SIRONO_POROSITY
rk[RKSECOND].rho_0prime[i] = rk[RKFIRST].rho_0prime[i];
rk[RKSECOND].rho_c_plus[i] = rk[RKFIRST].rho_c_plus[i];
rk[RKSECOND].rho_c_minus[i] = rk[RKFIRST].rho_c_minus[i];
rk[RKSECOND].compressive_strength[i] = rk[RKFIRST].compressive_strength[i];
rk[RKSECOND].tensile_strength[i] = rk[RKFIRST].tensile_strength[i];
rk[RKSECOND].shear_strength[i] = rk[RKFIRST].shear_strength[i];
rk[RKSECOND].K[i] = rk[RKFIRST].K[i];
rk[RKSECOND].flag_rho_0prime[i] = rk[RKFIRST].flag_rho_0prime[i];
rk[RKSECOND].flag_plastic[i] = rk[RKFIRST].flag_plastic[i];
#endif
#if EPSALPHA_POROSITY
rk[RKSECOND].alpha_epspor[i] = rk[RKSTART].alpha_epspor[i] + dt * (b31 * rk[RKSTART].dalpha_epspordt[i] + b32 * rk[RKFIRST].dalpha_epspordt[i]);
rk[RKSECOND].epsilon_v[i] = rk[RKSTART].epsilon_v[i] + dt * (b31 * rk[RKSTART].depsilon_vdt[i] + b32 * rk[RKFIRST].depsilon_vdt[i]);
#endif
#if INVISCID_SPH
rk[RKSECOND].beta[i] = rk[RKSTART].beta[i] + dt * (b31 * rk[RKSTART].dbetadt[i] + b32 * rk[RKFIRST].dbetadt[i]);
#endif
#if SOLID
int j;
for (j = 0; j < DIM*DIM; j++) {
rk[RKSECOND].S[i*DIM*DIM+j] = rk[RKSTART].S[i*DIM*DIM+j] + dt * (b31 * rk[RKSTART].dSdt[i*DIM*DIM+j] + b32 * rk[RKFIRST].dSdt[i*DIM*DIM+j]);
}
#endif
rk[RKSECOND].vx[i] = rk[RKSTART].vx[i] + dt * (b31 * rk[RKSTART].ax[i] + b32 * rk[RKFIRST].ax[i]);
#if DIM > 1
rk[RKSECOND].vy[i] = rk[RKSTART].vy[i] + dt * (b31 * rk[RKSTART].ay[i] + b32 * rk[RKFIRST].ay[i]);
#endif
#if DIM == 3
rk[RKSECOND].vz[i] = rk[RKSTART].vz[i] + dt * (b31 * rk[RKSTART].az[i] + b32 * rk[RKFIRST].az[i]);
#endif
rk[RKSECOND].x[i] = rk[RKSTART].x[i] + dt * (b31 * rk[RKSTART].dxdt[i] + b32 * rk[RKFIRST].dxdt[i]);
#if DIM > 1
rk[RKSECOND].y[i] = rk[RKSTART].y[i] + dt * (b31 * rk[RKSTART].dydt[i] + b32 * rk[RKFIRST].dydt[i]);
#endif
#if DIM == 3
rk[RKSECOND].z[i] = rk[RKSTART].z[i] + dt * (b31 * rk[RKSTART].dzdt[i] + b32 * rk[RKFIRST].dzdt[i]);
#endif
}
}
__global__ void integrateThirdStep(void)
{
int i;
int d;
#if GRAVITATING_POINT_MASSES
// loop pointmasses
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numPointmasses; i+= blockDim.x * gridDim.x) {
pointmass.vx[i] = rk_pointmass[RKSTART].vx[i] + dt/6.0 * (c1 * rk_pointmass[RKSTART].ax[i] + c2 * rk_pointmass[RKFIRST].ax[i] + c3 * rk_pointmass[RKSECOND].ax[i]);
pointmass.ax[i] = 1./6.0 *(c1 * rk_pointmass[RKSTART].ax[i] + c2 * rk_pointmass[RKFIRST].ax[i] + c3 * rk_pointmass[RKSECOND].ax[i]);
#if DIM > 1
pointmass.vy[i] = rk_pointmass[RKSTART].vy[i] + dt/6.0 * (c1 * rk_pointmass[RKSTART].ay[i] + c2 * rk_pointmass[RKFIRST].ay[i] + c3 * rk_pointmass[RKSECOND].ay[i]);
pointmass.ay[i] = 1./6.0 *(c1 * rk_pointmass[RKSTART].ay[i] + c2 * rk_pointmass[RKFIRST].ay[i] + c3 * rk_pointmass[RKSECOND].ay[i]);
#endif
#if DIM > 2
pointmass.vz[i] = rk_pointmass[RKSTART].vz[i] + dt/6.0 * (c1 * rk_pointmass[RKSTART].az[i] + c2 * rk_pointmass[RKFIRST].az[i] + c3 * rk_pointmass[RKSECOND].az[i]);
pointmass.az[i] = 1./6.0 *(c1 * rk_pointmass[RKSTART].az[i] + c2 * rk_pointmass[RKFIRST].az[i] + c3 * rk_pointmass[RKSECOND].az[i]);
#endif
pointmass.x[i] = rk_pointmass[RKSTART].x[i] + dt/6.0 * (c1 * rk_pointmass[RKSTART].vx[i] + c2 * rk_pointmass[RKFIRST].vx[i] + c3 * rk_pointmass[RKSECOND].vx[i]);
#if DIM > 1
pointmass.y[i] = rk_pointmass[RKSTART].y[i] + dt/6.0 * (c1 * rk_pointmass[RKSTART].vy[i] + c2 * rk_pointmass[RKFIRST].vy[i] + c3 * rk_pointmass[RKSECOND].vy[i]);
#endif
#if DIM > 2
pointmass.z[i] = rk_pointmass[RKSTART].z[i] + dt/6.0 * (c1 * rk_pointmass[RKSTART].vz[i] + c2 * rk_pointmass[RKFIRST].vz[i] + c3 * rk_pointmass[RKSECOND].vz[i]);
#endif
}
#endif // GRAVITATING_POINT_MASSES
// loop particles
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i+= blockDim.x * gridDim.x) {
//printf("THIRD: vx: %g \t %g :dxdt \t\t\t vy: %g \t %g :dydt\n", velxSecond[i], dxdtSecond[i], velySecond[i], dydtSecond[i]);
#if INTEGRATE_DENSITY
p.rho[i] = rk[RKSTART].rho[i] + dt/6.0 *
( c1 * rk[RKSTART].drhodt[i]
+ c2 * rk[RKFIRST].drhodt[i]
+ c3 * rk[RKSECOND].drhodt[i]);
p.drhodt[i] = 1./6.*(c1 * rk[RKSTART].drhodt[i]
+ c2 * rk[RKFIRST].drhodt[i]
+ c3 * rk[RKSECOND].drhodt[i]);
#else
p.rho[i] = rk[RKSECOND].rho[i];
#endif
#if INTEGRATE_SML
p.h[i] = rk[RKSTART].h[i] + dt/6.0 *
( c1 * rk[RKSTART].dhdt[i]
+ c2 * rk[RKFIRST].dhdt[i]
+ c3 * rk[RKSECOND].dhdt[i]);
p.dhdt[i] = 1./6.*(c1 * rk[RKSTART].dhdt[i]
+ c2 * rk[RKFIRST].dhdt[i]
+ c3 * rk[RKSECOND].dhdt[i]);
#else
p.h[i] = rk[RKSECOND].h[i];
#endif
#if INTEGRATE_ENERGY
p.e[i] = rk[RKSTART].e[i] + dt/6.0 *
( c1 * rk[RKSTART].dedt[i]
+ c2 * rk[RKFIRST].dedt[i]
+ c3 * rk[RKSECOND].dedt[i]);
p.dedt[i] = 1./6.* (c1 * rk[RKSTART].dedt[i]
+ c2 * rk[RKFIRST].dedt[i]
+ c3 * rk[RKSECOND].dedt[i]);
#endif
#if PALPHA_POROSITY
double dp = rk[RKSECOND].p[i] - rk[RKSTART].p[i];
#endif
#if FRAGMENTATION
p.d[i] = rk[RKSTART].d[i] + dt/6.0 *
( c1 * rk[RKSTART].dddt[i]
+ c2 * rk[RKFIRST].dddt[i]
+ c3 * rk[RKSECOND].dddt[i]);
p.dddt[i] = 1./6. * (c1 * rk[RKSTART].dddt[i]
+ c2 * rk[RKFIRST].dddt[i]
+ c3 * rk[RKSECOND].dddt[i]);
#if PALPHA_POROSITY
if (dp > 0.0) {
p.damage_porjutzi[i] = rk[RKSTART].damage_porjutzi[i] + dt/6.0 *
( c1 * rk[RKSTART].ddamage_porjutzidt[i]
+ c2 * rk[RKFIRST].ddamage_porjutzidt[i]
+ c3 * rk[RKSECOND].ddamage_porjutzidt[i]);
p.ddamage_porjutzidt[i] = 1./6. * (c1 * rk[RKSTART].ddamage_porjutzidt[i]
+ c2 * rk[RKFIRST].ddamage_porjutzidt[i]
+ c3 * rk[RKSECOND].ddamage_porjutzidt[i]);
} else {
p.d[i] = p.d[i];
p.damage_porjutzi[i] = rk[RKSTART].damage_porjutzi[i];
}
#endif
#endif
#if JC_PLASTICITY
p.ep[i] = rk[RKSTART].ep[i] + dt/6.0 *
( c1 * rk[RKSTART].edotp[i]
+ c2 * rk[RKFIRST].edotp[i]
+ c3 * rk[RKSECOND].edotp[i]);
p.T[i] = rk[RKSTART].T[i] + dt/6.0 *
( c1 * rk[RKSTART].dTdt[i]
+ c2 * rk[RKFIRST].dTdt[i]
+ c3 * rk[RKSECOND].dTdt[i]);
p.edotp[i] = 1./6. * ( c1 * rk[RKSTART].edotp[i]
+ c2 * rk[RKFIRST].edotp[i]
+ c3 * rk[RKSECOND].edotp[i]);
p.dTdt[i] = 1./6. * ( c1 * rk[RKSTART].dTdt[i]
+ c2 * rk[RKFIRST].dTdt[i]
+ c3 * rk[RKSECOND].dTdt[i]);
#endif
#if PALPHA_POROSITY
if (dp > 0.0) {
p.alpha_jutzi[i] = rk[RKSTART].alpha_jutzi[i] + dt/6.0 *
( c1 * rk[RKSTART].dalphadt[i]
+ c2 * rk[RKFIRST].dalphadt[i]
+ c3 * rk[RKSECOND].dalphadt[i]);
p.dalphadt[i] = 1./6. * (c1 * rk[RKSTART].dalphadt[i]
+ c2 * rk[RKFIRST].dalphadt[i]
+ c3 * rk[RKSECOND].dalphadt[i]);
} else {
p.alpha_jutzi[i] = rk[RKSTART].alpha_jutzi[i];
}
#endif
#if EPSALPHA_POROSITY
p.alpha_epspor[i] = rk[RKSTART].alpha_epspor[i] + dt/6.0 *
( c1 * rk[RKSTART].dalpha_epspordt[i]
+ c2 * rk[RKFIRST].dalpha_epspordt[i]
+ c3 * rk[RKSECOND].dalpha_epspordt[i]);
p.dalpha_epspordt[i] = 1./6. *
( c1 * rk[RKSTART].dalpha_epspordt[i]
+ c2 * rk[RKFIRST].dalpha_epspordt[i]
+ c3 * rk[RKSECOND].dalpha_epspordt[i]);
p.epsilon_v[i] = rk[RKSTART].epsilon_v[i] + dt/6.0 *
( c1 * rk[RKSTART].depsilon_vdt[i]
+ c2 * rk[RKFIRST].depsilon_vdt[i]
+ c3 * rk[RKSECOND].depsilon_vdt[i]);
p.depsilon_vdt[i] = 1./6. *
( c1 * rk[RKSTART].depsilon_vdt[i]
+ c2 * rk[RKFIRST].depsilon_vdt[i]
+ c3 * rk[RKSECOND].depsilon_vdt[i]);
#endif
#if INVISCID_SPH
p.beta[i] = rk[RKSTART].beta[i] + dt/6.0 *
( c1 * rk[RKSTART].dbetadt[i]
+ c2 * rk[RKFIRST].dbetadt[i]
+ c3 * rk[RKSECOND].dbetadt[i]);
p.dbetadt[i] = 1./6. * (c1 * rk[RKSTART].dbetadt[i]
+ c2 * rk[RKFIRST].dbetadt[i]
+ c3 * rk[RKSECOND].dbetadt[i]);
#endif
#if SOLID
int j;
for (j = 0; j < DIM*DIM; j++) {
p.S[i*DIM*DIM+j] = rk[RKSTART].S[i*DIM*DIM+j] + dt/6.0 *
( c1 * rk[RKSTART].dSdt[i*DIM*DIM+j]
+ c2 * rk[RKFIRST].dSdt[i*DIM*DIM+j]
+ c3 * rk[RKSECOND].dSdt[i*DIM*DIM+j]);
p.dSdt[i*DIM*DIM+j] = 1./6. *
( c1 * rk[RKSTART].dSdt[i*DIM*DIM+j]
+ c2 * rk[RKFIRST].dSdt[i*DIM*DIM+j]
+ c3 * rk[RKSECOND].dSdt[i*DIM*DIM+j]);
}
#endif
p.vx[i] = rk[RKSTART].vx[i] + dt/6.0 * (c1 * rk[RKSTART].ax[i] + c2 * rk[RKFIRST].ax[i] + c3 * rk[RKSECOND].ax[i]);
p.ax[i] = 1./6.0 *(c1 * rk[RKSTART].ax[i] + c2 * rk[RKFIRST].ax[i] + c3 * rk[RKSECOND].ax[i]);
p.g_ax[i] = 1./6.0 *(c1 * rk[RKSTART].g_ax[i] + c2 * rk[RKFIRST].g_ax[i] + c3 * rk[RKSECOND].g_ax[i]);
#if DIM > 1
p.vy[i] = rk[RKSTART].vy[i] + dt/6.0 * (c1 * rk[RKSTART].ay[i] + c2 * rk[RKFIRST].ay[i] + c3 * rk[RKSECOND].ay[i]);
p.ay[i] = 1./6.0 *(c1 * rk[RKSTART].ay[i] + c2 * rk[RKFIRST].ay[i] + c3 * rk[RKSECOND].ay[i]);
p.g_ay[i] = 1./6.0 *(c1 * rk[RKSTART].g_ay[i] + c2 * rk[RKFIRST].g_ay[i] + c3 * rk[RKSECOND].g_ay[i]);
#endif
#if DIM > 2
p.vz[i] = rk[RKSTART].vz[i] + dt/6.0 * (c1 * rk[RKSTART].az[i] + c2 * rk[RKFIRST].az[i] + c3 * rk[RKSECOND].az[i]);
p.az[i] = 1./6.0 *(c1 * rk[RKSTART].az[i] + c2 * rk[RKFIRST].az[i] + c3 * rk[RKSECOND].az[i]);
p.g_az[i] = 1./6.0 *(c1 * rk[RKSTART].g_az[i] + c2 * rk[RKFIRST].g_az[i] + c3 * rk[RKSECOND].g_az[i]);
#endif
p.x[i] = rk[RKSTART].x[i] + dt/6.0 * (c1 * rk[RKSTART].dxdt[i] + c2 * rk[RKFIRST].dxdt[i] + c3 * rk[RKSECOND].dxdt[i]);
#if DIM > 1
p.y[i] = rk[RKSTART].y[i] + dt/6.0 * (c1 * rk[RKSTART].dydt[i] + c2 * rk[RKFIRST].dydt[i] + c3 * rk[RKSECOND].dydt[i]);
#endif
#if DIM > 2
p.z[i] = rk[RKSTART].z[i] + dt/6.0 * (c1 * rk[RKSTART].dzdt[i] + c2 * rk[RKFIRST].dzdt[i] + c3 * rk[RKSECOND].dzdt[i]);
#endif
/* remember some more values */
p.noi[i] = rk[RKSECOND].noi[i];
p.p[i] = rk[RKSECOND].p[i];
#if PALPHA_POROSITY
p.pold[i] = rk[RKSECOND].p[i];
#endif
#if SIRONO_POROSITY
p.rho_0prime[i] = rk[RKSECOND].rho_0prime[i];
p.rho_c_plus[i] = rk[RKSECOND].rho_c_plus[i];
p.rho_c_minus[i] = rk[RKSECOND].rho_c_minus[i];
p.compressive_strength[i] = rk[RKSECOND].compressive_strength[i];
p.tensile_strength[i] = rk[RKSECOND].tensile_strength[i];
p.shear_strength[i] = rk[RKSECOND].shear_strength[i];
p.K[i] = rk[RKSECOND].K[i];
p.flag_rho_0prime[i] = rk[RKSECOND].flag_rho_0prime[i];
p.flag_plastic[i] = rk[RKSECOND].flag_plastic[i];
#endif
p.cs[i] = rk[RKSECOND].cs[i];
#if FRAGMENTATION
p.numActiveFlaws[i] = rk[RKSECOND].numActiveFlaws[i];
#endif
#if SOLID
p.local_strain[i] = rk[RKSECOND].local_strain[i];
#endif
#if NAVIER_STOKES
for (d = 0; d < DIM*DIM; d++) {
p.Tshear[i*DIM*DIM+d] = rk[RKSECOND].Tshear[i*DIM*DIM+d];
}
#endif
#if 0
#warning experimental superstuff in rk2adaptive...
if (p_rhs.materialId[i] == EOS_TYPE_IGNORE) {
p.x[i] = 1e12+1e6*i;
p.y[i] = 1e12+1e6*i;
}
#endif
}
}
#if FRAGMENTATION
#define MAX_DAMAGE_CHANGE 1e-2
/* set maximum time step for damage evolution */
__global__ void damageMaxTimeStep(double *maxDamageTimeStepPerBlock)
{
__shared__ double sharedMaxDamageTimeStep[NUM_THREADS_ERRORCHECK];
double localMaxDamageTimeStep = 0;
double tmp = 0;
double dtsuggested = 0;
int i, j, k, m;
for (i = threadIdx.x + blockIdx.x * blockDim.x; i < numParticles; i+= blockDim.x * gridDim.x) {
if (rk[RKFIRST].dddt[i] > 0) {
tmp = 1./ ( (rk[RKFIRST].d[i] + MAX_DAMAGE_CHANGE) / rk[RKFIRST].dddt[i] );
localMaxDamageTimeStep = max(tmp, localMaxDamageTimeStep);
}
}
i = threadIdx.x;
sharedMaxDamageTimeStep[i] = localMaxDamageTimeStep;
for (j = NUM_THREADS_ERRORCHECK / 2; j > 0; j /= 2) {
__syncthreads();
if (i < j) {
k = i + j;
sharedMaxDamageTimeStep[i] = localMaxDamageTimeStep = max(localMaxDamageTimeStep, sharedMaxDamageTimeStep[k]);
}
}
// write block result to global memory
if (i == 0) {
k = blockIdx.x;
maxDamageTimeStepPerBlock[k] = localMaxDamageTimeStep;
m = gridDim.x - 1;
if (m == atomicInc((unsigned int *)&blockCount, m)) {
// last block, so combine all block results
for (j = 0; j <= m; j++) {
localMaxDamageTimeStep = max(localMaxDamageTimeStep, maxDamageTimeStepPerBlock[j]);
}
maxDamageTimeStep = localMaxDamageTimeStep;
// reset block count
blockCount = 0;
if (maxDamageTimeStep > 0) {
dtsuggested = 1./maxDamageTimeStep;
if (dtsuggested > dtmax) {
// printf("<damageMaxTimeStep> timestep %g is larger than maximum timestep %g, reducing to %g\n", dtsuggested, dtmax, dtmax);
dtsuggested = dtmax;
}
if (dtsuggested < dt) {
dt = dtsuggested;
if (currentTimeD+dt > endTimeD) {
dt = endTimeD - currentTimeD;
}
}
}