878 const std::vector<CoreFitSC::ParticleClass>& particleClass) {
897 const int tableWidth = 6;
898 std::vector<size_t> farIndex;
899 std::vector<double> localTable;
900 for (
size_t i = 0; i < localNum; ++i) {
901 if (particleClass[i] != Class_t::MESH && particleClass[i] != Class_t::FAR) {
905 farIndex.push_back(i);
906 localTable.insert(localTable.end(),
907 {s(0), s(1), s(2), Binc[i] >= 0 ? Qc[i] : 0.0,
908 particleClass[i] == Class_t::FAR ? 1.0 : 0.0,
909 static_cast<double>(IDc[i])});
913 const int localCount = farIndex.size();
914 std::vector<int> counts(numNodes), offsets(numNodes + 1, 0);
915 MPI_Allgather(&localCount, 1, MPI_INT, counts.data(), 1, MPI_INT, comm);
916 for (
int p = 0; p < numNodes; ++p) {
917 offsets[p + 1] = offsets[p] + counts[p];
919 const size_t numTable = offsets[numNodes];
928 for (
size_t i = 0; i < localNum; ++i) {
929 if (particleClass[i] == Class_t::CORE) {
930 sums.
add(Qc[i], Rc[i]);
935 std::vector<int> recvCounts(numNodes), displs(numNodes);
936 for (
int p = 0; p < numNodes; ++p) {
937 recvCounts[p] = tableWidth * counts[p];
938 displs[p] = tableWidth * offsets[p];
940 std::vector<double> table(tableWidth * numTable);
941 MPI_Allgatherv(localTable.data(), tableWidth * localCount, MPI_DOUBLE,
942 table.data(), recvCounts.data(), displs.data(), MPI_DOUBLE, comm);
946 std::vector<Vector_t> position(numTable);
947 std::vector<size_t> liveRow;
948 std::vector<Vector_t> livePosition;
949 for (
size_t j = 0; j < numTable; ++j) {
950 const double* entry = &table[tableWidth * j];
951 position[j] =
Vector_t({entry[0], entry[1], entry[2]});
952 if (entry[3] != 0.0) {
953 liveRow.push_back(j);
954 livePosition.push_back(position[j]);
957 const size_t numLive = liveRow.size();
965 std::vector<Vector_t> multipole(numTable);
966 std::vector<double> indicator(numTable);
969 std::vector<Vector_t> liveFarFar(numLive,
Vector_t(0.0));
970 size_t farFarStride = 1;
975 std::vector<Vector_t> sourcePosition;
976 std::vector<double> sourceCharge;
977 double liveCharge = 0.0;
978 double sampledCharge = 0.0;
979 for (
const size_t j: liveRow) {
980 const double* entry = &table[tableWidth * j];
981 liveCharge += entry[3];
983 sourcePosition.push_back(position[j]);
984 sourceCharge.push_back(entry[3]);
985 sampledCharge += entry[3];
988 if (farFarStride > 1 && sampledCharge != 0.0) {
989 const double scale = liveCharge / sampledCharge;
990 for (
double& q: sourceCharge) {
995 const double eps = std::min({
hr_m[0], gamma *
hr_m[1],
hr_m[2]});
997 sourcePosition, sourceCharge, eps, liveFarFar);
1002 std::vector<double> localResult;
1003 localResult.reserve(4 * (range.second - range.first) +
1004 3 * (liveRange.second - liveRange.first));
1005 for (
size_t j = range.first; j < range.second; ++j) {
1006 localResult.insert(localResult.end(),
1007 {multipole[j](0), multipole[j](1), multipole[j](2), indicator[j]});
1009 for (
size_t k = liveRange.first; k < liveRange.second; ++k) {
1010 localResult.insert(localResult.end(),
1011 {liveFarFar[k](0), liveFarFar[k](1), liveFarFar[k](2)});
1014 for (
int p = 0; p < numNodes; ++p) {
1018 recvCounts[p] = 4 * (rangeP.second - rangeP.first) +
1019 3 * (liveRangeP.second - liveRangeP.first);
1020 displs[p] = numResults;
1021 numResults += recvCounts[p];
1023 std::vector<double> result(numResults);
1024 MPI_Allgatherv(localResult.data(),
static_cast<int>(localResult.size()), MPI_DOUBLE,
1025 result.data(), recvCounts.data(), displs.data(), MPI_DOUBLE, comm);
1026 std::vector<Vector_t> farFar(numTable,
Vector_t(0.0));
1027 for (
int p = 0; p < numNodes; ++p) {
1031 const double* entry = &result[displs[p]];
1032 for (
size_t j = rangeP.first; j < rangeP.second; ++j, entry += 4) {
1033 multipole[j] =
Vector_t({entry[0], entry[1], entry[2]});
1034 indicator[j] = entry[3];
1036 for (
size_t k = liveRangeP.first; k < liveRangeP.second; ++k, entry += 3) {
1037 farFar[liveRow[k]] =
Vector_t({entry[0], entry[1], entry[2]});
1047 std::vector<size_t> flagged;
1048 for (
const size_t j: liveRow) {
1049 const Class_t c = (table[tableWidth * j + 4] != 0.0 ? Class_t::FAR : Class_t::MESH);
1051 flagged.push_back(j);
1056 size_t coreStride[2] = {1, 1};
1059 std::vector<Vector_t> target(flagged.size());
1060 for (
size_t k = 0; k < flagged.size(); ++k) {
1061 target[k] = position[flagged[k]];
1065 const bool split = (
static_cast<double>(flagged.size()) *
scNumCore_m > maxPairs);
1067 double numShell = 0.0;
1068 for (
size_t i = 0; i < localNum; ++i) {
1069 if (particleClass[i] == Class_t::CORE &&
1074 allreduce(&numShell, 1, std::plus<double>());
1075 const size_t numShellCore =
static_cast<size_t>(numShell);
1083 std::vector<Vector_t> corePosition[2];
1084 std::vector<double> coreCharge[2];
1085 double charges[4] = {0.0, 0.0, 0.0, 0.0};
1086 for (
size_t i = 0; i < localNum; ++i) {
1087 if (particleClass[i] != Class_t::CORE) {
1092 charges[2 * part] += Qc[i];
1095 coreCharge[part].push_back(Qc[i]);
1096 charges[2 * part + 1] += Qc[i];
1099 const bool sampled = (coreStride[0] > 1 || coreStride[1] > 1);
1101 allreduce(charges, 4, std::plus<double>());
1104 std::vector<Vector_t> exact(flagged.size(),
Vector_t(0.0));
1105 for (
int part = 0; part < 2; ++part) {
1106 if (coreStride[part] > 1 && charges[2 * part + 1] != 0.0) {
1107 const double scale = charges[2 * part] / charges[2 * part + 1];
1108 for (
double& q: coreCharge[part]) {
1113 coreCharge[part], 0.0, exact);
1115 allreduce(&(exact[0](0)), 3 *
static_cast<int>(flagged.size()), std::plus<double>());
1116 for (
size_t k = 0; k < flagged.size(); ++k) {
1117 multipole[flagged[k]] = exact[k];
1125 for (
int j = 0; j < localCount; ++j) {
1126 const size_t row = offsets[myNode] + j;
1127 const size_t i = farIndex[j];
1128 if (particleClass[i] == Class_t::FAR) {
1135 if (farFarStride > 1) {
1137 std::ostringstream message;
1138 message <<
"the far-far field of the " << numLive <<
" far particles is summed over "
1139 <<
"about 1 in " << farFarStride <<
" of them, as " << numLive <<
" x "