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

MOved files not used by FludiSimulatorApp to deeprecated dir old_src, src dir...

MOved files not used by FludiSimulatorApp to deeprecated dir old_src, src dir now only inludes used files
parent 59d47110
No related branches found
No related tags found
No related merge requests found
Showing
with 562 additions and 4 deletions
File moved
// Camera.cpp
#include "include/Camera.h"
#include <cstdio>
#include <cstdlib>
static Camera* g_camera = nullptr;
Camera::Camera(vec3 position, vec3 target, vec3 up, float fov_, float aspect_, float zNear_, float zFar_)
: campos(position), forward(VectorSub(target, position)), upVector(up),
fov(fov_), aspectRatio(aspect_), zNear(zNear_), zFar(zFar_), rotationMatrix(IdentityMatrix())
{
float norm = sqrtf(forward.x*forward.x + forward.y*forward.y + forward.z*forward.z);
forward = ScalarMult(forward, 1.0f / norm);
}
// Processes keyboard input to control the camera
void Camera::processKey(unsigned char key)
{
switch (key)
{
case 'w':
campos += ScalarMult(forward, 0.5f);
break;
case 's':
campos -= ScalarMult(forward, 0.5f);
break;
case 'a':
campos -= ScalarMult(CrossProduct(forward, upVector), 0.5f);
break;
case 'd':
campos += ScalarMult(CrossProduct(forward, upVector), 0.5f);
break;
case 'j':
rotateSide(0.03f);
break;
case 'l':
rotateSide(-0.03f);
break;
case 'i':
// Rotate upwards
rotateUp(0.05f);
break;
case 'k':
// Rotate downwards
rotateUp(-0.05f);
break;
case 32: // SPACEBAR
campos += ScalarMult(upVector, 1.0f);
break;
case 'c':
campos -= ScalarMult(upVector, 1.0f);
break;
case 'v':
// Toggle boundary display
toggleDisplayBorder();
break;
case 0x1b: // ESC
exit(0);
}
}
void Camera::reshape(int width, int height) {
glViewport(0, 0, width, height);
aspectRatio = (float)width / (float)height;
}
mat4 Camera::getViewMatrix() const {
return lookAt(campos, VectorAdd(campos, forward), upVector);
}
mat4 Camera::getProjectionMatrix() const {
return perspective(fov, aspectRatio, zNear, zFar);
}
void Camera::rotateSide(float angle) {
mat3 rot = mat4tomat3(Ry(angle));
forward = MultMat3Vec3(rot, forward);
}
void Camera::rotateUp(float angle) {
vec3 side = CrossProduct(forward, upVector);
mat3 rot = mat4tomat3(ArbRotate(side, angle));
forward = MultMat3Vec3(rot, forward);
}
void initCamera(Camera& camera) {
g_camera = &camera;
}
File moved
File moved
File moved
File moved
// constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
const float h_BOUND_X_MIN = -6.0f;
const float h_BOUND_X_MAX = 12.0f;
const float h_BOUND_Y_MIN = 0.0f;
const float h_BOUND_Y_MAX = 10.0f;
const float h_BOUND_Z_MIN = -4.0f;
const float h_BOUND_Z_MAX = 4.0f;
const float h_GAS_CONSTANT = 0.02f;
const float h_VISCOSITY = 0.2f;
const float h_PARTICLE_MASS = 0.4f;
const float h_REST_DENSITY = 3000.0f;
const float h_PI = 3.14159265358979323846f;
const float h_H = 0.35f; // Smoothing radius
#endif // CONSTANTS_H
// Camera.h
#ifndef CAMERA_H
#define CAMERA_H
#include "VectorUtils4.h"
class Camera {
private:
vec3 campos;
vec3 forward;
vec3 upVector;
float fov;
float aspectRatio;
float zNear;
float zFar;
bool displayBorder = false;
mat4 rotationMatrix;
void rotateSide(float angle);
void rotateUp(float angle);
public:
Camera(vec3 position, vec3 target, vec3 up, float fov, float aspect, float zNear, float zFar);
void processKey(unsigned char key);
void reshape(int width, int height);
mat4 getViewMatrix() const;
mat4 getProjectionMatrix() const;
bool shouldDisplayBorder() const { return displayBorder; }
void toggleDisplayBorder() { displayBorder = !displayBorder; }
void setAspectRatio(float aspect) { aspectRatio = aspect; }
};
void initCamera(Camera& camera);
#endif
File moved
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* This file implements common mathematical operations on vector types
* (float3, float4 etc.) since these are not provided as standard by CUDA.
*
* The syntax is modeled on the Cg standard library.
*
* This is part of the Helper library includes
*
* Thanks to Linh Hah for additions and fixes.
*/
#ifndef HELPER_MATH_H
#define HELPER_MATH_H
#include "cuda_runtime.h"
typedef unsigned int uint;
typedef unsigned short ushort;
#ifndef EXIT_WAIVED
#define EXIT_WAIVED 2
#endif
#include <math.h>
////////////////////////////////////////////////////////////////////////////////
// negate
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float3 operator-(float3 &a)
{
return make_float3(-a.x, -a.y, -a.z);
}
////////////////////////////////////////////////////////////////////////////////
// addition
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float3 operator+(float3 a, float3 b)
{
return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
}
inline __host__ __device__ void operator+=(float3 &a, float3 b)
{
a.x += b.x;
a.y += b.y;
a.z += b.z;
}
inline __host__ __device__ float3 operator+(float3 a, float b)
{
return make_float3(a.x + b, a.y + b, a.z + b);
}
inline __host__ __device__ void operator+=(float3 &a, float b)
{
a.x += b;
a.y += b;
a.z += b;
}
////////////////////////////////////////////////////////////////////////////////
// subtract
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float3 operator-(float3 a, float3 b)
{
return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
}
inline __host__ __device__ void operator-=(float3 &a, float3 b)
{
a.x -= b.x;
a.y -= b.y;
a.z -= b.z;
}
inline __host__ __device__ float3 operator-(float3 a, float b)
{
return make_float3(a.x - b, a.y - b, a.z - b);
}
inline __host__ __device__ float3 operator-(float b, float3 a)
{
return make_float3(b - a.x, b - a.y, b - a.z);
}
inline __host__ __device__ void operator-=(float3 &a, float b)
{
a.x -= b;
a.y -= b;
a.z -= b;
}
////////////////////////////////////////////////////////////////////////////////
// multiply
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float3 operator*(float3 a, float3 b)
{
return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
}
inline __host__ __device__ void operator*=(float3 &a, float3 b)
{
a.x *= b.x;
a.y *= b.y;
a.z *= b.z;
}
inline __host__ __device__ float3 operator*(float3 a, float b)
{
return make_float3(a.x * b, a.y * b, a.z * b);
}
inline __host__ __device__ float3 operator*(float b, float3 a)
{
return make_float3(b * a.x, b * a.y, b * a.z);
}
inline __host__ __device__ void operator*=(float3 &a, float b)
{
a.x *= b;
a.y *= b;
a.z *= b;
}
////////////////////////////////////////////////////////////////////////////////
// divide
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float3 operator/(float3 a, float3 b)
{
return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
}
inline __host__ __device__ void operator/=(float3 &a, float3 b)
{
a.x /= b.x;
a.y /= b.y;
a.z /= b.z;
}
inline __host__ __device__ float3 operator/(float3 a, float b)
{
return make_float3(a.x / b, a.y / b, a.z / b);
}
inline __host__ __device__ void operator/=(float3 &a, float b)
{
a.x /= b;
a.y /= b;
a.z /= b;
}
inline __host__ __device__ float3 operator/(float b, float3 a)
{
return make_float3(b / a.x, b / a.y, b / a.z);
}
////////////////////////////////////////////////////////////////////////////////
// dot product
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float dot(float3 a, float3 b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
////////////////////////////////////////////////////////////////////////////////
// length
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float length(float3 v)
{
return sqrtf(dot(v, v));
}
////////////////////////////////////////////////////////////////////////////////
// normalize
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float3 normalize(float3 v)
{
float invLen = rsqrtf(dot(v, v));
return v * invLen;
}
////////////////////////////////////////////////////////////////////////////////
// floor
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float3 floorf(float3 v)
{
return make_float3(floorf(v.x), floorf(v.y), floorf(v.z));
}
////////////////////////////////////////////////////////////////////////////////
// cross product
////////////////////////////////////////////////////////////////////////////////
inline __host__ __device__ float3 cross(float3 a, float3 b)
{
return make_float3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
}
#endif
\ No newline at end of file
// kernels_cuda_pcisph.cu // kernels_cuda_pcisph.cu
#include "include/PCISPH_sim.h" #include "include/PCISPH_sim.h"
#include "settings/constants.h" #include "constants.h"
#include "include/float3_helper_math.h" #include "include/float3_helper_math.h"
#include <cuda_runtime.h> #include <cuda_runtime.h>
#include <cuda_gl_interop.h> #include <cuda_gl_interop.h>
......
File moved
File moved
...@@ -14,9 +14,9 @@ ...@@ -14,9 +14,9 @@
#include "MicroGlut.h" #include "MicroGlut.h"
#include "GL_utilities.h" #include "GL_utilities.h"
#include "PCISPH_sim.h" // Header for CUDA function declarations #include "include/PCISPH_sim.h" // Header for CUDA function declarations
#include "spawn_particles.h" #include "spawn_particles.h"
#include "Camera.h" #include "include/Camera.h"
#include "constants.h" #include "constants.h"
#define MAIN #define MAIN
......
File moved
File moved
// spawn_particles.cpp
#include "spawn_particles.h"
#include "constants.h"
#include <cmath> // For std::pow
#include <ctime>
#include <vector>
#include <cstdlib>
// Initialize fluid particles
void initParticles(
std::vector<float3> &h_positions,
std::vector<float3> &h_velocities,
const int NUM_PARTICLES,
const float GRID_SPACING)
{
// Seed the random number generator
srand(static_cast<unsigned int>(time(0)));
// Calculate particles per axis
int particlesPerAxis = static_cast<int>(ceil(pow(NUM_PARTICLES, 1.0f / 3.0f)));
int index = 0;
for (int i = 0; i < particlesPerAxis && index < NUM_PARTICLES; ++i) {
for (int j = 0; j < particlesPerAxis && index < NUM_PARTICLES; ++j) {
for (int k = 0; k < particlesPerAxis && index < NUM_PARTICLES; ++k) {
// Center particles around the origin
float x = (i - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING;
float y = (j - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING + 2.0f;
float z = (k - (particlesPerAxis - 1) / 2.0f) * GRID_SPACING;
// Add random perturbation within [-0.1*dx, 0.1*dx]
float perturbation = 0.1f * GRID_SPACING;
float rand_x = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation;
float rand_y = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation;
float rand_z = ((float)rand() / RAND_MAX - 0.5f) * 2.0f * perturbation;
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;
}
}
}
// If NUM_PARTICLES is not a perfect cube, initialize remaining particles
for (; index < NUM_PARTICLES; ++index) {
h_positions[index] = make_float3(0.0f, 10.0f, 0.0f); // Default position
h_velocities[index] = make_float3(0.0f, 0.0f, 0.0f); // Default velocity
}
}
// Create boundary particles
void createBoundaryParticles(
std::vector<float3> &boundary_positions,
std::vector<float3> &boundary_velocities,
int boundaryLayers,
float spacing)
{
// Compute the number of particles along each dimension for a given spacing
int nx = static_cast<int>((h_BOUND_X_MAX - h_BOUND_X_MIN)/spacing) + 1;
int ny = static_cast<int>((h_BOUND_Y_MAX - h_BOUND_Y_MIN)/spacing) + 1;
int nz = static_cast<int>((h_BOUND_Z_MAX - h_BOUND_Z_MIN)/spacing) + 1;
// Initialize velocities to zero for boundary
float3 zeroVel = make_float3(0.0f, 0.0f, 0.0f);
// Y-min boundary (e.g., "floor")
for (int layer = 0; layer < boundaryLayers; layer++) {
float y = h_BOUND_Y_MIN - (layer+1)*spacing; // placing layers below Ymin
for (int ix = 0; ix < nx; ix++) {
for (int iz = 0; iz < nz; iz++) {
float x = h_BOUND_X_MIN + ix*spacing;
float z = h_BOUND_Z_MIN + iz*spacing;
boundary_positions.push_back(make_float3(x, y, z));
boundary_velocities.push_back(zeroVel);
}
}
}
/*
// Y-max boundary (ceiling)
for (int layer = 0; layer < boundaryLayers; layer++) {
float y = h_BOUND_Y_MAX + (layer+1)*spacing;
for (int ix = 0; ix < nx; ix++) {
for (int iz = 0; iz < nz; iz++) {
float x = h_BOUND_X_MIN + ix*spacing;
float z = h_BOUND_Z_MIN + iz*spacing;
boundary_positions.push_back(make_float3(x, y, z));
boundary_velocities.push_back(zeroVel);
}
}
}
*/
// X-min boundary (vertical wall on the left)
int ny_cells = ny; // from earlier computation
for (int layer = 0; layer < boundaryLayers; layer++) {
float x = h_BOUND_X_MIN - (layer+1)*spacing;
for (int iy = 0; iy < ny_cells; iy++) {
for (int iz = 0; iz < nz; iz++) {
float y = h_BOUND_Y_MIN + iy*spacing;
float z = h_BOUND_Z_MIN + iz*spacing;
boundary_positions.push_back(make_float3(x, y, z));
boundary_velocities.push_back(zeroVel);
}
}
}
// X-max boundary (vertical wall on the right)
for (int layer = 0; layer < boundaryLayers; layer++) {
float x = h_BOUND_X_MAX + (layer+1)*spacing;
for (int iy = 0; iy < ny_cells; iy++) {
for (int iz = 0; iz < nz; iz++) {
float y = h_BOUND_Y_MIN + iy*spacing;
float z = h_BOUND_Z_MIN + iz*spacing;
boundary_positions.push_back(make_float3(x, y, z));
boundary_velocities.push_back(zeroVel);
}
}
}
// Z-min boundary (front wall)
for (int layer = 0; layer < boundaryLayers; layer++) {
float z = h_BOUND_Z_MIN - (layer+1)*spacing;
for (int ix = 0; ix < nx; ix++) {
for (int iy = 0; iy < ny_cells; iy++) {
float x = h_BOUND_X_MIN + ix*spacing;
float y = h_BOUND_Y_MIN + iy*spacing;
boundary_positions.push_back(make_float3(x, y, z));
boundary_velocities.push_back(zeroVel);
}
}
}
// Z-max boundary (back wall)
for (int layer = 0; layer < boundaryLayers; layer++) {
float z = h_BOUND_Z_MAX + (layer+1)*spacing;
for (int ix = 0; ix < nx; ix++) {
for (int iy = 0; iy < ny_cells; iy++) {
float x = h_BOUND_X_MIN + ix*spacing;
float y = h_BOUND_Y_MIN + iy*spacing;
boundary_positions.push_back(make_float3(x, y, z));
boundary_velocities.push_back(zeroVel);
}
}
}
}
// spawn_particles.h
#ifndef SPAWN_PARTICLES_H
#define SPAWN_PARTICLES_H
#include <vector>
#include <cuda_runtime.h> // for float3
void createBoundaryParticles(
std::vector<float3> &boundary_positions,
std::vector<float3> &boundary_velocities,
int boundaryLayers,
float spacing
);
void initParticles(
std::vector<float3> &h_positions,
std::vector<float3> &h_velocities,
const int NUM_PARTICLES,
const float GRID_SPACING
);
#endif // SPAWN_PARTICLES_H
\ No newline at end of file
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <vector> #include <vector>
#include <cuda_runtime.h> // for float3 #include <cuda_runtime.h> // for float3
namespace FluidSimulation {
void createBoundaryParticles( void createBoundaryParticles(
std::vector<float3> &boundary_positions, std::vector<float3> &boundary_positions,
std::vector<float3> &boundary_velocities, std::vector<float3> &boundary_velocities,
...@@ -18,5 +20,5 @@ void initParticles( ...@@ -18,5 +20,5 @@ void initParticles(
const int NUM_PARTICLES, const int NUM_PARTICLES,
const float GRID_SPACING const float GRID_SPACING
); );
}
#endif // SPAWN_PARTICLES_H #endif // SPAWN_PARTICLES_H
\ No newline at end of file
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <vector> #include <vector>
#include <cstdlib> #include <cstdlib>
namespace FluidSimulation {
// Initialize fluid particles // Initialize fluid particles
void initParticles( void initParticles(
std::vector<float3> &h_positions, std::vector<float3> &h_positions,
...@@ -152,3 +153,5 @@ void createBoundaryParticles( ...@@ -152,3 +153,5 @@ void createBoundaryParticles(
} }
} }
} }
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment