diff --git a/Apps/Liquefied/LiquefiedApp.cpp b/Apps/Liquefied/LiquefiedApp.cpp index c93afd2..293fb38 100644 --- a/Apps/Liquefied/LiquefiedApp.cpp +++ b/Apps/Liquefied/LiquefiedApp.cpp @@ -216,5 +216,8 @@ namespace Liq else if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_R) == GLFW_PRESS) Engine::LiquiefiedParams::resetSimulation = true; + + else if(glfwGetKey(Engine::Window::GetWindow(), GLFW_KEY_D) == GLFW_PRESS) + Engine::LiquiefiedParams::showDebugWindow = !Engine::LiquiefiedParams::showDebugWindow; } } diff --git a/Apps/Liquefied/LiquefiedInterface.cpp b/Apps/Liquefied/LiquefiedInterface.cpp index 4936564..46ef3a4 100644 --- a/Apps/Liquefied/LiquefiedInterface.cpp +++ b/Apps/Liquefied/LiquefiedInterface.cpp @@ -32,21 +32,6 @@ namespace Liq } ImGui::Separator(); - // --- Numerical value monitoring - ImGui::NewLine(); - ImGui::Separator(); - CenterText("Numerical value monitoring"); - ImGui::Separator(); - ImGui::NewLine(); - for(auto const& entry : Engine::Monitoring::values) - { - ImGui::Text("\t%s (min): %5.3f at (%d, %d)", entry.first, entry.second.min, entry.second.x, entry.second.y); - ImGui::Text("\t%s (max): %5.3f at (%d, %d)", entry.first, entry.second.max, entry.second.x, entry.second.y); - ImGui::Text("\t%s (val): %5.3f at (%d, %d)", entry.first, entry.second.val, entry.second.x, entry.second.y); - ImGui::NewLine(); - } - ImGui::Separator(); - // --- Simulation settings ImGui::NewLine(); ImGui::Separator(); @@ -63,8 +48,40 @@ namespace Liq Input_u32("##Input1", &Engine::LiquiefiedParams::turbinePower, 10, 100, ImGuiInputTextFlags_CharsDecimal); ImGui::NewLine(); ImGui::Separator(); + + // --- Numerical value monitoring + ImGui::NewLine(); + ImGui::Separator(); + CenterText("Numerical value monitoring"); + ImGui::Separator(); + ImGui::NewLine(); + for(const auto& entry : Engine::Monitoring::loggedValues) + { + ImGui::Text(" %14s: %+5.5f at (%3d, %3d)", entry.first.c_str(), entry.second.val, entry.second.x, entry.second.y); + } + ImGui::NewLine(); + ImGui::Separator(); } ImGui::End(); + + if(Engine::LiquiefiedParams::showDebugWindow) + { + ImGui::SetNextWindowBgAlpha(_debugWindowAlpha); + ImGui::SetNextWindowPos(_debugWindowPos, ImGuiCond_Always, _overlayPivot); + ImGui::SetNextWindowSize(_debugWindowSize); + + if(ImGui::Begin("DebugWindow", nullptr, _windowFlags | ImGuiWindowFlags_AlwaysVerticalScrollbar)) + { + CenterText("Divergence"); + ImGui::Separator(); + + for(const auto& entry : Engine::Monitoring::buffer) + { + ImGui::Text("(%3d, %3d): %+5.5f", entry.x, entry.y, entry.val); + } + } + ImGui::End(); + } } void LiquefiedInterface::AddBufferBar() const @@ -100,6 +117,10 @@ namespace Liq ImGui::SetCursorPosY(45.0f); ImGui::SetCursorPosX(485.0f); ImGui::Checkbox("Scientific Colors (C) |", &Engine::LiquiefiedParams::scientificColorScheme); + + ImGui::SetCursorPosY(10.0f); + ImGui::SetCursorPosX(725.0f); + ImGui::Checkbox("Show debug window (D) |", &Engine::LiquiefiedParams::showDebugWindow); } ImGui::End(); } diff --git a/Apps/Liquefied/LiquefiedInterface.hpp b/Apps/Liquefied/LiquefiedInterface.hpp index 3974b02..3c093f2 100644 --- a/Apps/Liquefied/LiquefiedInterface.hpp +++ b/Apps/Liquefied/LiquefiedInterface.hpp @@ -7,8 +7,13 @@ namespace Liq class LiquefiedInterface final : public Engine::Interface { private: - ImVec2 _bufferBarSize = ImVec2((float)Engine::WindowParams::WIDTH - _sidebarWidth, 80.0f); - ImVec2 _bufferBarPos = ImVec2(0.0f, 0.0f); + float _bufferBarHeight = 80.0f; + ImVec2 _bufferBarSize = ImVec2((float)Engine::WindowParams::WIDTH - _sidebarWidth, _bufferBarHeight); + ImVec2 _bufferBarPos = ImVec2(0.0f, 0.0f); + float _debugWindowAlpha = 0.6f; + float _debugWindowWidth = 250.0f; + ImVec2 _debugWindowPos = ImVec2((float)Engine::WindowParams::WIDTH - _sidebarWidth, _bufferBarHeight); + ImVec2 _debugWindowSize = ImVec2(_debugWindowWidth,(float)Engine::WindowParams::HEIGHT); void AddSideBar() const; void AddBufferBar() const; diff --git a/Engine/Application/GlobalParams.hpp b/Engine/Application/GlobalParams.hpp index 079293c..6ced968 100644 --- a/Engine/Application/GlobalParams.hpp +++ b/Engine/Application/GlobalParams.hpp @@ -171,6 +171,7 @@ namespace Engine inline static bool scientificColorScheme = false; inline static bool pauseSimulation = true; inline static bool resetSimulation = false; + inline static bool showDebugWindow = true; inline static Integrator integratorChoice = Integrator::ForwardEuler; }; } \ No newline at end of file diff --git a/Engine/Core/Monitoring.cpp b/Engine/Core/Monitoring.cpp index caa5dc3..46d37b7 100644 --- a/Engine/Core/Monitoring.cpp +++ b/Engine/Core/Monitoring.cpp @@ -4,37 +4,43 @@ namespace Engine { // ----- Public ----- - void Monitoring::MinMaxAvgAt(const char* name, const float value, const uint32 x, const uint32 y) + void Monitoring::LogToBuffer(const char* name, const float val, const uint32 x, const uint32 y) { - //Add new value if it's not already getting monitored - if(values.find(name) == values.end()) + //Add to normal monitoring + buffer[_index] = {val, x, y}; + _index = (_index + 1) % LiquiefiedParams::LIQUID_NUM_CELLS; + + Log(name, val, x, y); + } + + void Monitoring::Log(const char* name, const float val, const uint32 x, const uint32 y) + { + std::string s_max = std::string(name).append("_max"); + std::string s_min = std::string(name).append("_min"); + + //Check for new entry + if(loggedValues.find(s_max) == loggedValues.end() && + loggedValues.find(s_min) == loggedValues.end()) { - values[name] = {value, value, value, x, y}; + loggedValues.emplace(std::pair(s_max, {val, x, y})); + loggedValues.emplace(std::pair(s_min, {val, x, y})); return; } - //Get current struct - MinMaxAvg_t valueStruct = values[name]; - - //Add min if it's less than current min - if(valueStruct.min > value) - valueStruct.min = value; - //Add max if it's more than current max - else if(valueStruct.max < value) - valueStruct.max = value; - - //Add value and position - valueStruct.val = value; - valueStruct.x = x; - valueStruct.y = y; + //Else check min and max + if(val > loggedValues[s_max].val) + loggedValues.at(s_max) = {val, x, y}; - //Assign new values - values[name] = valueStruct; + if(val < loggedValues[s_min].val) + loggedValues.at(s_min) = {val, x, y}; } void Monitoring::Reset() { - for(auto& val : values) - val.second = {0.0f, 0.0f, 0.0f, UINT32_MAX, UINT32_MAX}; + _index = 0; + std::memset(&buffer[0], 0, LiquiefiedParams::LIQUID_NUM_CELLS); + _max = FLT_MIN; + _min = FLT_MAX; + loggedValues.clear(); } } \ No newline at end of file diff --git a/Engine/Core/Monitoring.hpp b/Engine/Core/Monitoring.hpp index 32e8445..5113f37 100644 --- a/Engine/Core/Monitoring.hpp +++ b/Engine/Core/Monitoring.hpp @@ -1,19 +1,29 @@ #pragma once #include "Types.hpp" +#include "GlobalParams.hpp" +#include #include +#include namespace Engine { class Monitoring { + private: + inline static uint32 _index = 0; + inline static float _max = FLT_MIN; + inline static float _min = FLT_MAX; + public: + inline static LogFloatXY buffer[LiquiefiedParams::LIQUID_NUM_CELLS] = {}; + inline static std::map loggedValues = std::map(); + Monitoring() = delete; - static void MinMaxAvgAt(const char* name, float value, uint32 x, uint32 y); + static void LogToBuffer(const char* name, float val, uint32 x, uint32 y); + static void Log(const char* name, float val, uint32 x, uint32 y); static void Reset(); - - inline static std::map values = std::map(); }; } \ No newline at end of file diff --git a/Engine/Core/Types.hpp b/Engine/Core/Types.hpp index 67e6fa7..bdf465a 100644 --- a/Engine/Core/Types.hpp +++ b/Engine/Core/Types.hpp @@ -40,12 +40,10 @@ namespace Engine #define COLOR_GREEN glm::vec3(0.0f, 1.0f, 0.0f) #define COLOR_BLUE glm::vec3(0.0f, 0.0f, 1.0f) - typedef struct MinMaxAvg + typedef struct LogFloatXY { - float min; - float max; float val; uint32 x; uint32 y; - } MinMaxAvg_t; + } LogFloatXY_t; } \ No newline at end of file diff --git a/Engine/Physics/FluidSimulation/FluidSimulator.cpp b/Engine/Physics/FluidSimulation/FluidSimulator.cpp index 3cf9140..5bcffca 100644 --- a/Engine/Physics/FluidSimulation/FluidSimulator.cpp +++ b/Engine/Physics/FluidSimulation/FluidSimulator.cpp @@ -98,10 +98,10 @@ namespace Engine //Calculate pressure float pressure = divergence / s_sum; - Monitoring::MinMaxAvgAt("Pressure", pressure, x, y); //Apply overrelaxation to speed up convergence pressure *= OVERRELAX; + Monitoring::Log("Pressure", pressure, x, y); //Push all velocities out by the same amout to force incompressibility _grid.u_At(x, y) += pressure * leftNeighbor; @@ -128,8 +128,7 @@ namespace Engine //Monitor the divergence to make sure that the fluid is incompressible const float divergence = _grid.u_At(x+1, y) - _grid.u_At(x, y) + _grid.v_At(x, y+1) - _grid.v_At(x, y); - Monitoring::MinMaxAvgAt("Div", divergence, x, y); - Logger::Print("Divergence: " + std::to_string(divergence) + " at (" + std::to_string(x) + ", " + std::to_string(y) + ")"); + Monitoring::LogToBuffer("Divergence", divergence, x, y); //Skip border cells if(_grid.s_At(x, y) == 0) @@ -219,10 +218,12 @@ namespace Engine { float dt = (float)Window::GetDeltaTime_sec(); AddForces(dt); - CorrectForces(); + //CorrectForces(); //Not necessary + //Diffuse //? Project(dt); AdvectVelocity(dt); AdvectSmoke(dt); + //Project(dt); } void FluidSimulator::Reset() diff --git a/Engine/Physics/FluidSimulation/StaggeredGrid.hpp b/Engine/Physics/FluidSimulation/StaggeredGrid.hpp index 9ccf208..a35697a 100644 --- a/Engine/Physics/FluidSimulation/StaggeredGrid.hpp +++ b/Engine/Physics/FluidSimulation/StaggeredGrid.hpp @@ -9,11 +9,11 @@ namespace Engine struct StaggeredGrid { private: - //Horizontal u-component is sampled at the centers of the vertical cell faces. + //Horizontal velocity (u-component) is sampled at the centers of the vertical cell faces. float _u[LiquiefiedParams::LIQUID_NUM_CELLS] = {0.0f}; float _u_temp[LiquiefiedParams::LIQUID_NUM_CELLS] = {0.0f}; - //Vertical is sampled at the centers of the horizontal cell faces. + //Vertical velocity (v-component) is sampled at the centers of the horizontal cell faces. float _v[LiquiefiedParams::LIQUID_NUM_CELLS] = {0.0f}; float _v_temp[LiquiefiedParams::LIQUID_NUM_CELLS] = {0.0f}; @@ -28,6 +28,8 @@ namespace Engine const uint32 width = LiquiefiedParams::SIMULATION_WIDTH; const uint32 height = LiquiefiedParams::SIMULATION_HEIGHT; + //ToDo: float* u = &_u[0]; + //Spatial discretization const float dx = 1.0f;