This commit is contained in:
yggdrasil75
2025-11-30 07:56:09 -05:00
parent 9767435e58
commit 265128b6b7

View File

@@ -999,37 +999,36 @@ public:
TIME_FUNCTION; TIME_FUNCTION;
if (tempMap.empty() || deltaTime <= 0) return; if (tempMap.empty() || deltaTime <= 0) return;
std::vector<std::pair<size_t, float>> tempUpdates; std::vector<std::pair<size_t, Temp*>> tempEntries;
tempUpdates.reserve(tempMap.size()); tempEntries.reserve(tempMap.size());
// Process in spatial order for better cache performance
for (auto& [id, tempObj] : tempMap) { for (auto& [id, tempObj] : tempMap) {
Vec2 pos = Positions.at(id); tempEntries.emplace_back(id, &tempObj);
float oldtemp = tempObj.temp; }
// Use smaller query radius for diffusion
auto nearbyIds = spatialGrid.queryRange(pos, neighborRadius * tempObj.conductivity); std::for_each(std::execution::par_unseq, tempEntries.begin(), tempEntries.end(),
[&](const std::pair<size_t, Temp*>& entry) {
size_t id = entry.first;
Temp* tempObj = entry.second;
Vec2 pos = Positions.at(id);
float oldtemp = tempObj->temp;
// Use smaller query radius for diffusion
auto nearbyIds = spatialGrid.queryRange(pos, neighborRadius * tempObj->conductivity);
float heatTransfer = 0.0f;
int validNeighbors = 0;
std::unordered_map<Vec2, Temp> neighborTemps; std::unordered_map<Vec2, Temp> neighborTemps;
for (size_t neighborId : nearbyIds) { for (size_t neighborId : nearbyIds) {
if (neighborId != id && tempMap.find(neighborId) != tempMap.end()) { if (neighborId != id && tempMap.find(neighborId) != tempMap.end()) {
neighborTemps.emplace(Positions.at(neighborId), tempMap.at(neighborId)); neighborTemps.emplace(Positions.at(neighborId), tempMap.at(neighborId));
} }
} }
tempObj.calLapl(pos, neighborTemps);
float newtemp = tempObj.temp;
float tempdiff = (oldtemp - newtemp) * (deltaTime / 1000);
// if (std::abs(newtemp) < EPSILON) {
// std::cout << "new temp is: " << newtemp << " old temp is: " << oldtemp << " diff*delta: " << tempdiff << std::endl;
// }
tempObj.temp = oldtemp - tempdiff;
}
// Batch update temperatures tempObj->calLapl(pos, neighborTemps);
for (const auto& [id, newTemp] : tempUpdates) { float newtemp = tempObj->temp;
tempMap.at(id).temp = newTemp; float tempdiff = (oldtemp - newtemp) * (deltaTime / 1000);
tempObj->temp = oldtemp - tempdiff;
} }
);
} }
}; };