From b429b1b98d0f3b245ab90202b7e3a477ad0c00c2 Mon Sep 17 00:00:00 2001 From: Yggdrasil75 Date: Fri, 28 Nov 2025 11:01:25 -0500 Subject: [PATCH] pushing to home. --- tests/g2temp.cpp | 32 ++++++++++---------- util/grid/grid2.hpp | 73 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 75 insertions(+), 30 deletions(-) diff --git a/tests/g2temp.cpp b/tests/g2temp.cpp index f7d8059..69881cd 100644 --- a/tests/g2temp.cpp +++ b/tests/g2temp.cpp @@ -85,25 +85,25 @@ void Preview(Grid2& grid) { } void livePreview(Grid2& grid, AnimationConfig config) { - std::lock_guard lock(previewMutex); + // std::lock_guard lock(previewMutex); - currentPreviewFrame = grid.getTempAsFrame(Vec2(0,0), Vec2(config.height,config.width), Vec2(256,256),frame::colormap::RGBA); - // Vec2 min; - // Vec2 max; - // grid.getBoundingBox(min, max); - //currentPreviewFrame = grid.getTempAsFrame(min,max, Vec2(1024,1024)); + // currentPreviewFrame = grid.getTempAsFrame(Vec2(0,0), Vec2(config.height,config.width), Vec2(256,256),frame::colormap::RGBA); + // // Vec2 min; + // // Vec2 max; + // // grid.getBoundingBox(min, max); + // //currentPreviewFrame = grid.getTempAsFrame(min,max, Vec2(1024,1024)); - glGenTextures(1, &textu); - glBindTexture(GL_TEXTURE_2D, textu); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); + // glGenTextures(1, &textu); + // glBindTexture(GL_TEXTURE_2D, textu); + // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + // glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); - glBindTexture(GL_TEXTURE_2D, textu); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, currentPreviewFrame.getWidth(), currentPreviewFrame.getHeight(), - 0, GL_RGBA, GL_UNSIGNED_BYTE, currentPreviewFrame.getData().data()); + // glBindTexture(GL_TEXTURE_2D, textu); + // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, currentPreviewFrame.getWidth(), currentPreviewFrame.getHeight(), + // 0, GL_RGBA, GL_UNSIGNED_BYTE, currentPreviewFrame.getData().data()); - updatePreview = true; + // updatePreview = true; } std::vector> pickSeeds(Grid2 grid, AnimationConfig config) { @@ -286,7 +286,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) { } //expandPixel(grid,config,seeds); - grid.diffuseTemps(100); + grid.diffuseTempsOptimized(100); std::lock_guard lock(state.mutex); state.grid = grid; diff --git a/util/grid/grid2.hpp b/util/grid/grid2.hpp index 0400a4c..963c140 100644 --- a/util/grid/grid2.hpp +++ b/util/grid/grid2.hpp @@ -12,6 +12,8 @@ #include "../simblocks/temp.hpp" #include #include +#include +#include const float EPSILON = 0.0000000000000000000000001; @@ -305,15 +307,13 @@ public: return addObject(pos,newc,1.0); } - //add pixel (default color and default size provided) size_t addObject(const Vec2& pos, const Vec4& color, float size = 1.0f) { size_t id = Positions.set(pos); - Pixels.insert({id, GenericPixel(id, color, pos)}); + Pixels.emplace(id, GenericPixel(id, color, pos)); spatialGrid.insert(id, pos); return id; } - // Set default background color for empty spaces void setDefault(const Vec4& color) { defaultBackgroundColor = color; } @@ -406,6 +406,7 @@ public: } } if (create) { + return addObject(pos, defaultBackgroundColor, 1.0f); } throw std::out_of_range("Position not found"); @@ -818,13 +819,11 @@ public: Vec2 pos = Positions.at(id); std::vector candidates = spatialGrid.queryRange(pos, neighborRadius); - // Filter out self and apply exact distance check std::vector neighbors; float radiusSq = neighborRadius * neighborRadius; for (size_t candidateId : candidates) { - if (candidateId != id && - pos.distanceSquared(Positions.at(candidateId)) <= radiusSq) { + if (candidateId != id && pos.distanceSquared(Positions.at(candidateId)) <= radiusSq) { neighbors.push_back(candidateId); } } @@ -836,7 +835,6 @@ public: Vec2 pos = Positions.at(id); std::vector candidates = spatialGrid.queryRange(pos, neighborRadius); - // Filter out self and apply exact distance check std::vector neighbors; float radiusSq = dist * dist; @@ -981,15 +979,62 @@ public: void diffuseTemps(int timestep) { TIME_FUNCTION; + std::cout << "diffusing temps with a timestep of " << timestep << std::endl; if (tempMap.empty() || timestep < 1) return; - std::unordered_map cTemps; - cTemps.reserve(tempMap.size()); + #pragma omp parallel for for (const auto& [id, pos] : Positions) { - float oldtemp = getTemp(id); - tempMap.erase(id); - float newtemp = Temp::calGrad(pos, getTemps(id)); - float newtempMult = (newtemp-oldtemp) * timestep; - setTemp(id, newtempMult); + auto tempIT = tempMap.find(id); + if (tempIT != tempMap.end()) { + Temp oldtemp = tempIT->second; + tempMap.erase(id); + float newtemp = Temp::calGrad(pos, getTemps(id)); + float newtempMult = (newtemp-oldtemp.temp) * timestep; + oldtemp.temp = newtempMult; + tempMap.emplace(id, oldtemp); + } + } + } + + void diffuseTempsOptimized(float deltaTime) { + TIME_FUNCTION; + if (tempMap.empty() || deltaTime <= 0) return; + + std::vector> tempUpdates; + tempUpdates.reserve(tempMap.size()); + + // Process in spatial order for better cache performance + for (const auto& [id, tempObj] : tempMap) { + Vec2 pos = Positions.at(id); + + // Use smaller query radius for diffusion + auto nearbyIds = spatialGrid.queryRange(pos, neighborRadius * tempObj.conductivity); + + float heatTransfer = 0.0f; + int validNeighbors = 0; + + for (size_t neighborId : nearbyIds) { + if (neighborId != id && tempMap.find(neighborId) != tempMap.end()) { + float tempDiff = tempMap.at(neighborId).temp - tempObj.temp; + float distance = pos.distance(Positions.at(neighborId)); + + if (distance > EPSILON) { + // Basic heat conduction formula + float conductivity = tempObj.conductivity * (tempObj.diffusivity + tempMap.at(neighborId).diffusivity); + heatTransfer += conductivity * tempDiff / distance; + validNeighbors++; + } + } + } + + if (validNeighbors > 0) { + float newTemp = tempObj.temp + heatTransfer * deltaTime / validNeighbors; + tempUpdates.emplace_back(id, newTemp); + } + } + + // Batch update temperatures + for (const auto& [id, newTemp] : tempUpdates) { + tempMap.at(id).temp = newTemp; } } };