o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write MPM simulator using Eigen and OpenGL, write only code
- User
Write MPM simulator using Eigen and OpenGL, write only code
- Assistant
Write MPM simulator using Eigen and OpenGL, write only code
// MPM_Simulator.cpp
// Compile with: g++ MPM_Simulator.cpp -o MPM_Simulator -lGL -lglfw -lGLEW
// Make sure to have Eigen, GLFW, and GLEW installed.
#include <Eigen/Dense>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <vector>
#include <iostream>
// Constants
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
const int GRID_SIZE = 50;
const float TIME_STEP = 0.016f;
const float GRAVITY = -9.81f;
// Particle structure
struct Particle {
Eigen::Vector2f position;
Eigen::Vector2f velocity;
Eigen::Vector2f mass;
float mass_scalar;
Eigen::Matrix2f deformation_gradient;
};
// Grid node structure
struct GridNode {
Eigen::Vector2f velocity;
float mass;
};
// Global Variables
std::vector<Particle> particles;
GridNode grid[GRID_SIZE][GRID_SIZE];
// Initialize particles
void initializeParticles() {
for(int i = 0; i < 1000; ++i){
Particle p;
p.position = Eigen::Vector2f(rand() % GRID_SIZE, GRID_SIZE - 1.0f);
p.velocity = Eigen::Vector2f(0.0f, 0.0f);
p.mass_scalar = 1.0f;
p.deformation_gradient = Eigen::Matrix2f::Identity();
particles.push_back(p);
}
}
// Initialize OpenGL
bool initOpenGL(GLFWwindow** window) {
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW\n";
return false;
}
*window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "MPM Simulator", NULL, NULL);
if (!*window) {
std::cerr << "Failed to create GLFW window\n";
glfwTerminate();
return false;
}
glfwMakeContextCurrent(*window);
glewExperimental = GL_TRUE;
if (glewInit()!=GLEW_OK) {
std::cerr << "Failed to initialize GLEW\n";
return false;
}
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
return true;
}
// Simulation Step
void simulate() {
// Reset grid
for(int i=0;i<GRID_SIZE;i++) {
for(int j=0;j<GRID_SIZE;j++) {
grid[i][j].velocity = Eigen::Vector2f::Zero();
grid[i][j].mass = 0.0f;
}
}
// Particle to Grid (P2G)
for(auto &p : particles){
int xi = static_cast<int>(p.position.x());
int yi = static_cast<int>(p.position.y());
for(int i = xi-1; i <= xi+1; ++i){
for(int j = yi-1; j <= yi+1; ++j){
if(i >=0 && i < GRID_SIZE && j >=0 && j < GRID_SIZE){
// Simple weighting
float weight = 1.0f;
grid[i][j].mass += p.mass_scalar * weight;
grid[i][j].velocity += p.velocity * p.mass_scalar * weight;
}
}
}
}
// Grid Update
for(int i=0;i<GRID_SIZE;i++) {
for(int j=0;j<GRID_SIZE;j++) {
if(grid[i][j].mass > 0){
// Gravity
grid[i][j].velocity += Eigen::Vector2f(0.0f, GRAVITY) * TIME_STEP;
// Normalize
grid[i][j].velocity /= grid[i][j].mass;
}
}
}
// Grid to Particle (G2P)
for(auto &p : particles){
Eigen::Vector2f new_velocity = Eigen::Vector2f::Zero();
int xi = static_cast<int>(p.position.x());
int yi = static_cast<int>(p.position.y());
for(int i = xi-1; i <= xi+1; ++i){
for(int j = yi-1; j <= yi+1; ++j){
if(i >=0 && i < GRID_SIZE && j >=0 && j < GRID_SIZE){
float weight = 1.0f;
new_velocity += grid[i][j].velocity * weight;
}
}
}
p.velocity = new_velocity;
p.position += p.velocity * TIME_STEP;
// Simple boundary conditions
if(p.position.x() < 0.0f) { p.position.x() = 0.0f; p.velocity.x() *= -0.5f; }
if(p.position.x() >= GRID_SIZE) { p.position.x() = GRID_SIZE - 1.0f; p.velocity.x() *= -0.5f; }
if(p.position.y() < 0.0f) { p.position.y() = 0.0f; p.velocity.y() *= -0.5f; }
if(p.position.y() >= GRID_SIZE) { p.position.y() = GRID_SIZE - 1.0f; p.velocity.y() *= -0.5f; }
}
}
// Render Particles
void renderParticles() {
glBegin(GL_POINTS);
glColor3f(1.0f, 0.5f, 0.0f);
for(auto &p : particles){
float x = p.position.x() / GRID_SIZE * 2.0f - 1.0f;
float y = p.position.y() / GRID_SIZE * 2.0f - 1.0f;
glVertex2f(x, y);
}
glEnd();
}
int main(){
GLFWwindow* window;
if(!initOpenGL(&window)){
return -1;
}
// Initialize particles
initializeParticles();
// Enable point size
glEnable(GL_PROGRAM_POINT_SIZE);
glPointSize(2.0f);
// Main loop
while(!glfwWindowShouldClose(window)){
// Simulation step
simulate();
// Render
glClear(GL_COLOR_BUFFER_BIT);
renderParticles();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}