I was using gradient, switched to laplace.

This commit is contained in:
yggdrasil75
2025-11-28 17:24:07 -05:00
parent 439e0c0a2a
commit 9767435e58
4 changed files with 30 additions and 49 deletions

View File

@@ -394,7 +394,7 @@ public:
}
size_t getOrCreatePositionVec(const Vec2& pos, float radius = 0.0f, bool create = true) {
TIME_FUNCTION;
//TIME_FUNCTION; //called too many times and average time is less than 0.0000001 so ignore it.
if (radius == 0.0f) {
Vec2 gridPos = spatialGrid.worldToGrid(pos);
auto cellIt = spatialGrid.grid.find(gridPos);
@@ -423,7 +423,7 @@ public:
}
std::vector<size_t> getPositionVecRegion(const Vec2& pos, float radius = 1.0f) const {
TIME_FUNCTION;
//TIME_FUNCTION;
float searchRadius = (radius == 0.0f) ? std::numeric_limits<float>::epsilon() : radius;
// Get candidates from spatial grid
@@ -987,7 +987,7 @@ public:
// if (tempIT != tempMap.end()) {
// Temp oldtemp = tempIT->second;
// tempMap.erase(id);
// float newtemp = Temp::calGrad(pos, getTemps(id));
// float newtemp = Temp::calLapl(pos, getTemps(id));
// float newtempMult = (newtemp-oldtemp.temp) * timestep;
// oldtemp.temp = newtempMult;
// tempMap.emplace(id, oldtemp);
@@ -1017,7 +1017,7 @@ public:
neighborTemps.emplace(Positions.at(neighborId), tempMap.at(neighborId));
}
}
tempObj.calGrad(pos, neighborTemps);
tempObj.calLapl(pos, neighborTemps);
float newtemp = tempObj.temp;
float tempdiff = (oldtemp - newtemp) * (deltaTime / 1000);
// if (std::abs(newtemp) < EPSILON) {

View File

@@ -78,52 +78,33 @@ public:
return num / den;
}
void calGrad(const Vec2& testPos, const std::unordered_map<Vec2, Temp>& others) {
int meh;
//std::cout << meh++ << std::endl;
std::vector<std::pair<Vec2, float>> nearbyPoints;
for (const auto& [point, ctemp] : others) {
if (point.distance(testPos) <= 25) {
nearbyPoints.emplace_back(point, ctemp.temp);
}
}
//std::cout << meh++ << std::endl;
float sumX = 0,sumY = 0,sumT = 0,sumX2 = 0,sumY2 = 0,sumXY = 0,sumXT = 0, sumYT = 0;
int n = nearbyPoints.size();
//std::cout << meh++ << std::endl;
for (const auto& [point, ctemp] : nearbyPoints) {
float x = point.x - testPos.x;
float y = point.y - testPos.y;
void calLapl(const Vec2& testPos, const std::unordered_map<Vec2, Temp>& others) {
//TIME_FUNCTION;
float dt = 0.032;
float sumWeights = 0.0f;
float sumTempWeights = 0.0f;
float searchRadius = 25.0f;
sumX += x;
sumY += y;
sumT += ctemp;
sumX2 += x * x;
sumY2 += y * y;
sumXY += x * y;
sumXT += x * ctemp;
sumYT += y * ctemp;
for (const auto& [point, tempObj] : others) {
float dist = testPos.distance(point);
if (dist < 0.001f || dist > searchRadius) continue;
float weight = 1.0f / (dist * dist);
sumTempWeights += weight * tempObj.temp;
sumWeights += weight;
}
//std::cout << meh++ << std::endl;
float det = sumX2 * sumY2 - sumXY * sumXY;
Vec2 calpoint;
if (std::abs(det) < 1e-10) {
calpoint = Vec2(0, 0); // Singular matrix, cannot solve
}
else {
float a = (sumXT * sumY2 - sumYT * sumXY) / det;
float b = (sumX2 * sumYT - sumXY * sumXT) / det;
calpoint = Vec2(a, b); // ∇T = (∂T/∂x, ∂T/∂y)
}
//std::cout << meh++ << std::endl;
Vec2 closest = findClosestPoint(testPos, others);
Vec2 displacement = testPos - closest;
if (sumWeights < 1e-10f) return;
float estimatedTemp = temp + (calpoint.x * displacement.x + calpoint.y * displacement.y);
temp = estimatedTemp;
float equilibriumTemp = sumTempWeights / sumWeights;
float rate = this->diffusivity * 0.01f;
float lerpFactor = 1.0f - std::exp(-rate * dt);
this->temp += (equilibriumTemp - this->temp) * lerpFactor;
}
};