Skip to content

Commit

Permalink
Improved value monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed May 3, 2024
1 parent e89be3c commit a5568b4
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 52 deletions.
3 changes: 3 additions & 0 deletions Apps/Liquefied/LiquefiedApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
51 changes: 36 additions & 15 deletions Apps/Liquefied/LiquefiedInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
9 changes: 7 additions & 2 deletions Apps/Liquefied/LiquefiedInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Engine/Application/GlobalParams.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
50 changes: 28 additions & 22 deletions Engine/Core/Monitoring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, LogFloatXY_t>(s_max, {val, x, y}));
loggedValues.emplace(std::pair<std::string, LogFloatXY_t>(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();
}
}
16 changes: 13 additions & 3 deletions Engine/Core/Monitoring.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
#pragma once

#include "Types.hpp"
#include "GlobalParams.hpp"

#include <cstring>
#include <map>
#include <string>

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<std::string, LogFloatXY_t> loggedValues = std::map<std::string, LogFloatXY_t>();

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<const char*, MinMaxAvg_t> values = std::map<const char*, MinMaxAvg_t>();
};
}
6 changes: 2 additions & 4 deletions Engine/Core/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 5 additions & 4 deletions Engine/Physics/FluidSimulation/FluidSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions Engine/Physics/FluidSimulation/StaggeredGrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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;

Expand Down

0 comments on commit a5568b4

Please sign in to comment.