Skip to content
Snippets Groups Projects
Commit 714bd6b9 authored by Adam Nyberg's avatar Adam Nyberg
Browse files

Added options to choose betwen 2 parrticle innit scenarios

parent af78614d
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ namespace FluidSimulation { ...@@ -17,6 +17,7 @@ namespace FluidSimulation {
m_viscosity = h_VISCOSITY; m_viscosity = h_VISCOSITY;
m_simulationStepsPerFrame = 2; m_simulationStepsPerFrame = 2;
m_mode = SimulationMode::OneLargeCube;
} }
FluidSimulation::~FluidSimulation() { FluidSimulation::~FluidSimulation() {
...@@ -32,7 +33,7 @@ namespace FluidSimulation { ...@@ -32,7 +33,7 @@ namespace FluidSimulation {
std::vector<float3> h_positions(m_numParticles); std::vector<float3> h_positions(m_numParticles);
std::vector<float3> h_velocities(m_numParticles); std::vector<float3> h_velocities(m_numParticles);
float gridSpacing = 0.5 * m_smoothingRadius; float gridSpacing = 0.5 * m_smoothingRadius;
initParticles(h_positions, h_velocities, m_numParticles, gridSpacing); initParticles(h_positions, h_velocities, m_numParticles, gridSpacing, m_mode);
FluidSimulationGPU::resetSimulation( FluidSimulationGPU::resetSimulation(
h_positions.data(), h_velocities.data(), m_numParticles); h_positions.data(), h_velocities.data(), m_numParticles);
...@@ -47,6 +48,12 @@ namespace FluidSimulation { ...@@ -47,6 +48,12 @@ namespace FluidSimulation {
updateConstants(); updateConstants();
} }
void FluidSimulation::setSimulationMode(SimulationMode newMode)
{
m_mode = newMode;
Reset();
}
void FluidSimulation::updateConstants() { void FluidSimulation::updateConstants() {
// Send updated parameters to CUDA // Send updated parameters to CUDA
FluidSimulationGPU::updateConstantsOnDevice( FluidSimulationGPU::updateConstantsOnDevice(
...@@ -61,7 +68,7 @@ namespace FluidSimulation { ...@@ -61,7 +68,7 @@ namespace FluidSimulation {
std::vector<float3> h_positions(m_numParticles); std::vector<float3> h_positions(m_numParticles);
std::vector<float3> h_velocities(m_numParticles); std::vector<float3> h_velocities(m_numParticles);
float gridSpacing = 0.5 * m_smoothingRadius; float gridSpacing = 0.5 * m_smoothingRadius;
initParticles(h_positions, h_velocities, m_numParticles, gridSpacing); initParticles(h_positions, h_velocities, m_numParticles, gridSpacing, m_mode);
// Initialize particle device arrays // Initialize particle device arrays
FluidSimulationGPU::initializeFluidDeviceArrays(h_positions.data(), h_velocities.data(), m_numParticles); FluidSimulationGPU::initializeFluidDeviceArrays(h_positions.data(), h_velocities.data(), m_numParticles);
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include "imgui.h" #include "imgui.h"
#include "settings/constants.h" #include "settings/constants.h"
#include "include/SimulationMode.h"
namespace FluidSimulation { namespace FluidSimulation {
...@@ -60,7 +61,7 @@ namespace FluidSimulation { ...@@ -60,7 +61,7 @@ namespace FluidSimulation {
// Particle Mass // Particle Mass
ImGui::Text("Particle Mass"); ImGui::Text("Particle Mass");
ImGui::SliderFloat("##ParticleMass", &particleMass, 0.01f, 0.1f, "%.3f"); ImGui::SliderFloat("##ParticleMass", &particleMass, 0.01f, 1.4f, "%.3f");
// Rest Density // Rest Density
static int restDensityInt = static_cast<int>(restDensity); static int restDensityInt = static_cast<int>(restDensity);
...@@ -141,12 +142,25 @@ namespace FluidSimulation { ...@@ -141,12 +142,25 @@ namespace FluidSimulation {
m_fluidSimulation->Reset(); m_fluidSimulation->Reset();
} }
ImGui::PushItemWidth(-1); // Make sliders take up the full available width
ImGui::Separator();
static int selectedOption = static_cast<int>(m_fluidSimulation->getSimulationMode()); // Sync with current mode
const char* options[] = { "One Large Cube", "Two Small Cubes" };
if (ImGui::Combo("##DropDownMenu", &selectedOption, options, IM_ARRAYSIZE(options)))
{
m_fluidSimulation->setSimulationMode(static_cast<SimulationMode>(selectedOption));
}
ImGui::Separator(); ImGui::Separator();
static int stepsPerFrame = m_fluidSimulation->getSimulationStepsPerFrame(); static int stepsPerFrame = m_fluidSimulation->getSimulationStepsPerFrame();
// Full-width sliders // Full-width sliders
ImGui::PushItemWidth(-1); // Make sliders take up the full available width
// Particle Mass // Particle Mass
ImGui::Text("Simulation steps/frame"); ImGui::Text("Simulation steps/frame");
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include <cuda_runtime.h> #include <cuda_runtime.h>
#include <GL/glew.h> #include <GL/glew.h>
#include "SimulationMode.h"
namespace FluidSimulation { namespace FluidSimulation {
...@@ -22,6 +23,8 @@ namespace FluidSimulation { ...@@ -22,6 +23,8 @@ namespace FluidSimulation {
float m_smoothingRadius; float m_smoothingRadius;
int m_simulationStepsPerFrame; int m_simulationStepsPerFrame;
SimulationMode m_mode;
public: public:
FluidSimulation(int numParticles); FluidSimulation(int numParticles);
~FluidSimulation(); ~FluidSimulation();
...@@ -44,13 +47,15 @@ namespace FluidSimulation { ...@@ -44,13 +47,15 @@ namespace FluidSimulation {
float getSmoothingRadius() { return m_smoothingRadius; } float getSmoothingRadius() { return m_smoothingRadius; }
int getSimulationStepsPerFrame() { return m_simulationStepsPerFrame; }; int getSimulationStepsPerFrame() { return m_simulationStepsPerFrame; };
SimulationMode getSimulationMode() { return m_mode; };
void setParticleMass(float particleMass) { m_particleMass = particleMass; }; void setParticleMass(float particleMass) { m_particleMass = particleMass; };
void setRestDensity(float restDensity) { m_restDensity = restDensity; }; void setRestDensity(float restDensity) { m_restDensity = restDensity; };
void setViscosity(float visocisty) { m_viscosity = visocisty; }; void setViscosity(float visocisty) { m_viscosity = visocisty; };
void setSmoothingRadius(float smoothRadius) { m_smoothingRadius = smoothRadius; }; void setSmoothingRadius(float smoothRadius) { m_smoothingRadius = smoothRadius; };
void setSimulationStepsPerFrame(int steps) { m_simulationStepsPerFrame = steps; }; void setSimulationStepsPerFrame(int steps) { m_simulationStepsPerFrame = steps; };
void setSimulationMode(SimulationMode newMode);
void ResetFluidParameters(); void ResetFluidParameters();
}; };
......
#ifndef SIMULATION_MODE_H
#define SIMULATION_MODE_H
namespace FluidSimulation
{
enum class SimulationMode
{
OneLargeCube,
TwoSmallCubes
};
}
#endif // SIMULATION_MODE_H
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include <cuda_runtime.h> // for float3 #include <cuda_runtime.h> // for float3
#include "SimulationMode.h"
namespace FluidSimulation { namespace FluidSimulation {
...@@ -18,7 +19,8 @@ void initParticles( ...@@ -18,7 +19,8 @@ void initParticles(
std::vector<float3> &h_positions, std::vector<float3> &h_positions,
std::vector<float3> &h_velocities, std::vector<float3> &h_velocities,
const int NUM_PARTICLES, const int NUM_PARTICLES,
const float GRID_SPACING const float GRID_SPACING,
SimulationMode mode
); );
} }
#endif // SPAWN_PARTICLES_H #endif // SPAWN_PARTICLES_H
\ No newline at end of file
...@@ -13,75 +13,61 @@ void initParticles( ...@@ -13,75 +13,61 @@ void initParticles(
std::vector<float3> &h_positions, std::vector<float3> &h_positions,
std::vector<float3> &h_velocities, std::vector<float3> &h_velocities,
const int NUM_PARTICLES, const int NUM_PARTICLES,
const float GRID_SPACING) const float GRID_SPACING,
SimulationMode mode
)
{ {
// Seed the random number generator // Seed the random number generator
srand(static_cast<unsigned int>(time(0))); srand(static_cast<unsigned int>(time(0)));
// Calculate particles per axis auto initializeCube = [&](int startIndex, int particlesPerAxis, float offsetX, float offsetY, float offsetZ) {
int particlesPerAxis = static_cast<int>(ceil(pow(NUM_PARTICLES, 1.0f / 3.0f))); int index = startIndex;
for (int i = 0; i < particlesPerAxis && index < NUM_PARTICLES; ++i) {
int index = 0; for (int j = 0; j < particlesPerAxis && index < NUM_PARTICLES; ++j) {
for (int i = 0; i < particlesPerAxis && index < NUM_PARTICLES; ++i) { for (int k = 0; k < particlesPerAxis && index < NUM_PARTICLES; ++k) {
for (int j = 0; j < particlesPerAxis && index < NUM_PARTICLES; ++j) { float x = (i - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING + offsetX;
for (int k = 0; k < particlesPerAxis && index < NUM_PARTICLES; ++k) { float y = (j - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING + offsetY;
// Center particles around the origin float z = (k - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING + offsetZ;
float x = (i - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING;
float y = (j - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING + 4.0f; float perturbation = 0.1f * GRID_SPACING;
float z = (k - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING + 0.5; float rand_x = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation;
float rand_y = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation;
// Add random perturbation within [-0.1*dx, 0.1*dx] float rand_z = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation;
float perturbation = 0.1f * GRID_SPACING;
float rand_x = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation; h_positions[index] = make_float3(x + rand_x, y + rand_y, z + rand_z);
float rand_y = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation; h_velocities[index] = make_float3(0.0f, 0.0f, 0.0f);
float rand_z = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation; ++index;
}
h_positions[index] = make_float3(
x + rand_x,
y + rand_y,
z + rand_z
);
// Initialize velocities
h_velocities[index] = make_float3(0.0f, 0.0f, 0.0f);
++index;
} }
} }
} return index; // Return the next index
/* };
for (int i = 0; i < particlesPerAxis && index < NUM_PARTICLES; ++i) {
for (int j = 0; j < particlesPerAxis && index < NUM_PARTICLES; ++j) { int particlesPerAxis = 0;
for (int k = 0; k < particlesPerAxis && index < NUM_PARTICLES; ++k) { int index = 0;
// Center particles around the origin
float x = (i - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING + 9; switch (mode)
float y = (j - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING + 2.0f; {
float z = (k - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING - 0.5; case SimulationMode::OneLargeCube:
particlesPerAxis = static_cast<int>(ceil(pow(NUM_PARTICLES, 1.0f / 3.0f)));
// Add random perturbation within [-0.1*dx, 0.1*dx] index = initializeCube(0, particlesPerAxis, 0.0f, 4.0f, 0.5f);
float perturbation = 0.1f * GRID_SPACING; break;
float rand_x = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation;
float rand_y = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation; case SimulationMode::TwoSmallCubes:
float rand_z = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation; {
int halfParticles = NUM_PARTICLES / 2; // Split particles evenly between two cubes
h_positions[index] = make_float3( particlesPerAxis = static_cast<int>(ceil(pow(halfParticles, 1.0f / 3.0f)));
x + rand_x,
y + rand_y, index = initializeCube(0, particlesPerAxis, -4.0f, 4.0f, 1.0f);
z + rand_z index = initializeCube(index, particlesPerAxis, 8.0f, 4.0f, -1.0f);
); break;
// Initialize velocities
h_velocities[index] = make_float3(0.0f, 0.0f, 0.0f);
++index;
}
} }
} }
*/
// If NUM_PARTICLES is not a perfect cube, initialize remaining particles // Initialize remaining particles, if any
for (; index < NUM_PARTICLES; ++index) { for (; index < NUM_PARTICLES; ++index) {
h_positions[index] = make_float3(0.0f, 10.0f, 0.0f); // Default position h_positions[index] = make_float3(0.0f, 10.0f, 0.0f);
h_velocities[index] = make_float3(0.0f, 0.0f, 0.0f); // Default velocity h_velocities[index] = make_float3(0.0f, 0.0f, 0.0f);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment