440void pitchAngleDiffusion(dccrg::Dccrg<spatial_cell::SpatialCell,dccrg::Cartesian_Geometry>& mpiGrid,
const uint popID){
452 const Real epsilon = 0.0;
454 phiprof::Timer diffusionTimer {
"pitch-angle-diffusion"};
458 size_t numberOfLocalCells = LocalCells.size();
460 std::vector<Real> dtTotalDiff(numberOfLocalCells, 0.0);
462 int maxThreadsPerBlock = Hashinator::defaults::MAX_BLOCKSIZE;
463 int blocksPerSpatialCell = (
nbins_v*
nbins_mu+maxThreadsPerBlock-1)/maxThreadsPerBlock;
466 int totalNumberOfVelocityBlocks = 0;
467 for (
size_t CellIdx = 0; CellIdx < numberOfLocalCells; CellIdx++) {
469 const auto CellID = LocalCells[CellIdx];
474 totalNumberOfVelocityBlocks += numberOfVelocityBlocks;
546 std::vector<bool> spatialLoopComplete(numberOfLocalCells,
false);
549 #pragma omp parallel for
550 for (
size_t CellIdx = 0; CellIdx < numberOfLocalCells; CellIdx++) {
551 bool currentSpatialLoopComplete;
553 std::array<Real,3> b;
556 const auto CellID = LocalCells[CellIdx];
561 popID, CellIdx, currentSpatialLoopComplete,
566 spatialLoopComplete[CellIdx] = currentSpatialLoopComplete;
567 host_sparsity[CellIdx] = sparsity;
568 host_bValues[3*CellIdx] = b[0];
569 host_bValues[3*CellIdx+1] = b[1];
570 host_bValues[3*CellIdx+2] = b[2];
571 host_nu0Values[CellIdx] = nu0;
574 bool allSpatialCellTimeLoopsComplete =
true;
576 for (
size_t CellIdx = 0; CellIdx < numberOfLocalCells; CellIdx++) {
577 if(!spatialLoopComplete[CellIdx]){
578 allSpatialCellTimeLoopsComplete =
false;
583 if(allSpatialCellTimeLoopsComplete){
595 #pragma omp parallel for
596 for (
size_t CellIdx = 0; CellIdx < numberOfLocalCells; CellIdx++) {
597 const auto CellID = LocalCells[CellIdx];
617 dim3 threadsPerBlock_massConservation(
WID,
WID,
WID);
618 int blocksPerGrid_massConservation = numberOfLocalCells;
621 calculateDensity_kernel<<<blocksPerGrid_massConservation, threadsPerBlock_massConservation>>>(
622 dev_densityPreAdjust,
629 while (!allSpatialCellTimeLoopsComplete) {
632 int remappedCellIdx = 0;
634 for (
size_t CellIdx = 0; CellIdx < numberOfLocalCells; CellIdx++) {
636 host_remappedCellIdxArray[CellIdx] = remappedCellIdx;
638 if(spatialLoopComplete[CellIdx]){
642 const auto CellID = LocalCells[CellIdx];
649 host_smallCellIdxArray[remappedCellIdx] = CellIdx;
667 int totalThreadsPerBlock_getCellIndexArray = Hashinator::defaults::MAX_BLOCKSIZE/2;
669 int blocksPerGrid_getCellIndexArray = (maxThreadIndex_getCellIndexArray+totalThreadsPerBlock_getCellIndexArray-1)/totalThreadsPerBlock_getCellIndexArray;
673 phiprof::Timer cellIdxArrayTimer {
"getCellIndexArray_kernel"};
674 getCellIndexArray_kernel<<<blocksPerGrid_getCellIndexArray, totalThreadsPerBlock_getCellIndexArray>>>(
683 cellIdxArrayTimer.stop();
685 dim3 threadsPerBlock_build2dArrayOfFvmu(
WID,
WID,
WID);
686 int blocksPerGrid_build2dArrayOfFvmu = maxBlockIndex;
689 phiprof::Timer builFvmuTimer {
"build2dArrayOfFvmu_kernel"};
690 build2dArrayOfFvmu_kernel<<<blocksPerGrid_build2dArrayOfFvmu, threadsPerBlock_build2dArrayOfFvmu>>>(
707 builFvmuTimer.stop();
709 int totalThreadsPerBlock_dividefByCount = Hashinator::defaults::MAX_BLOCKSIZE/2;
710 int maxThreadIndex_dividefByCount = numberOfLocalCells*
nbins_v*
nbins_mu;
711 int blocksPerGrid_dividefByCount = (maxThreadIndex_dividefByCount+totalThreadsPerBlock_dividefByCount-1)/totalThreadsPerBlock_dividefByCount;
714 phiprof::Timer divideFByCountTimer {
"dividefByCount_kernel"};
715 dividefByCount_kernel<<<blocksPerGrid_dividefByCount, totalThreadsPerBlock_dividefByCount>>>(
716 dev_smallCellIdxArray,
721 maxThreadIndex_dividefByCount
725 divideFByCountTimer.stop();
727 int lastBlockSize =
nbins_v*
nbins_mu-(blocksPerSpatialCell-1)*maxThreadsPerBlock;
728 int totalThreadsPerBlock_computeDerivativesCFLDdt;
729 if(blocksPerSpatialCell == 1){
732 totalThreadsPerBlock_computeDerivativesCFLDdt = maxThreadsPerBlock;
734 int blocksPerGrid_computeDerivativesCFLDdt = numberOfLocalCells*blocksPerSpatialCell;
735 int sharedMemorySize = totalThreadsPerBlock_computeDerivativesCFLDdt *
sizeof(
Real);
738 phiprof::Timer computeDerivativesTimer {
"computeDerivativesCFLDdt_kernel"};
740 dev_smallCellIdxArray,
746 dev_potentialDdtValues,
754 blocksPerSpatialCell,
759 computeDerivativesTimer.stop();
761 int totalThreadsPerBlock_reduceDdtValues;
762 if(blocksPerSpatialCell < maxThreadsPerBlock){
765 totalThreadsPerBlock_reduceDdtValues = maxThreadsPerBlock;
767 int blocksPerGrid_reduceDdtValues = numberOfLocalCells;
768 int sharedMemorySize_reduceDdtValues = totalThreadsPerBlock_reduceDdtValues *
sizeof(
Real);
771 phiprof::Timer reduceDdtValuesTimer {
"reduceDdtValues_kernel"};
774 dev_potentialDdtValues,
780 reduceDdtValuesTimer.stop();
787 for (
size_t CellIdx = 0; CellIdx < numberOfLocalCells; CellIdx++) {
788 if(spatialLoopComplete[CellIdx]){
792 if (host_Ddt[CellIdx] > RemainT) {
793 host_Ddt[CellIdx] = RemainT;
795 dtTotalDiff[CellIdx] += host_Ddt[CellIdx];
801 dim3 threadsPerBlock_computeNewCellValues(
WID,
WID,
WID);
802 int blocksPerGrid_computeNewCellValues = maxBlockIndex;
805 phiprof::Timer newCellValuesTimer {
"computeNewCellValues_kernel"};
806 computeNewCellValues_kernel<<<blocksPerGrid_computeNewCellValues, threadsPerBlock_computeNewCellValues>>>(
824 newCellValuesTimer.stop();
828 for (
size_t CellIdx = 0; CellIdx < numberOfLocalCells; CellIdx++) {
829 allSpatialCellTimeLoopsComplete =
true;
831 allSpatialCellTimeLoopsComplete =
false;
833 spatialLoopComplete[CellIdx] =
true;
840 dim3 threadsPerBlock_massConservation(
WID,
WID,
WID);
841 int blocksPerGrid_massConservation = numberOfLocalCells;
844 calculateDensity_kernel<<<blocksPerGrid_massConservation, threadsPerBlock_massConservation>>>(
851 conserveMass_kernel<<<blocksPerGrid_massConservation, threadsPerBlock_massConservation>>>(
852 dev_densityPreAdjust,
862 diffusionTimer.stop();