From 32236231c4c9bbe7376cae0f00df6e5bcf9d34b9 Mon Sep 17 00:00:00 2001 From: william Date: Tue, 12 Mar 2024 15:36:37 -0400 Subject: [PATCH] =?UTF-8?q?Simulation=20compl=C3=A8te=20et=20fonctionnelle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- labo_physique/GraphColoring.cpp | 17 ++++++---- labo_physique/ParticleSimApplication.cpp | 43 ++++++++++++++++++++++++ labo_physique/ParticleSystem.cpp | 22 ++++++------ labo_physique/Solvers.h | 7 ++-- 4 files changed, 70 insertions(+), 19 deletions(-) diff --git a/labo_physique/GraphColoring.cpp b/labo_physique/GraphColoring.cpp index 043ad26..f1d8744 100644 --- a/labo_physique/GraphColoring.cpp +++ b/labo_physique/GraphColoring.cpp @@ -4,6 +4,8 @@ using namespace gti320; void GraphColoring::color(ParticleSystem &particleSystem) { + m_partitions.clear(); + // La palette de couleurs ColorList C; @@ -28,9 +30,10 @@ void GraphColoring::color(ParticleSystem &particleSystem) { p.color = color; if (m_partitions.size() <= color) { - m_partitions.push_back(std::vector()); + m_partitions.emplace_back(); } - m_partitions[color].push_back(i); + m_partitions[color].push_back(i * 2); + m_partitions[color].push_back(i * 2 + 1); i++; } } @@ -38,11 +41,12 @@ void GraphColoring::color(ParticleSystem &particleSystem) { int GraphColoring::findColor(const Particle &p, const std::vector &particles, const std::vector &springs, ColorList &C) const { - size_t n = C.size(); - auto count = new int[n]; + int n = (int) C.size(); + int count[n]; + memset(count, 0, sizeof(int) * n); - // TODO Trouver la premi�re couleur de la palette C qui n'est pas attribu�e � une particule voisine. - // Si une couleur est introuvable, ajouter une nouvelle couleur � la palette et retournez la couleur. + // TODO Trouver la premi�re couleur de la palette C qui n'est pas attribu�e � une particule voisine. + // Si une couleur est introuvable, ajouter une nouvelle couleur � la palette et retournez la couleur. // Utiliser la fonction findNeighbors pour assembler une liste de particules qui sont directement connect�es � la particule p par un ressort (les voisines). NeighborList neighbors = findNeighbors(p, particles, springs); @@ -56,6 +60,7 @@ GraphColoring::findColor(const Particle &p, const std::vector &particl } } + C.push_back(n); return n; } diff --git a/labo_physique/ParticleSimApplication.cpp b/labo_physique/ParticleSimApplication.cpp index 6ffbe00..3c62261 100644 --- a/labo_physique/ParticleSimApplication.cpp +++ b/labo_physique/ParticleSimApplication.cpp @@ -202,7 +202,50 @@ namespace { particleSystem.clear(); // TODO Amusez-vous. Rendu ici, vous le méritez. + const int N = 20; + const int x_start = 200; + const int y_start = 400; + const int dx = 32; + const int dy = 32; + Particle p1(Vector2f(x_start + dx * 2, y_start + dy * 4), Vector2f(0, 0), Vector2f(0, 0), 1.0); + particleSystem.addParticle(p1); + + Particle p2(Vector2f(x_start, y_start), Vector2f(0, 0), Vector2f(0, 0), 1.0); + particleSystem.addParticle(p2); + + Particle p3(Vector2f(x_start + dx * 4, y_start), Vector2f(0, 0), Vector2f(0, 0), 1.0); + particleSystem.addParticle(p3); + + Particle p4(Vector2f(x_start + dx * 2, y_start + dy * 2), Vector2f(0, 0), Vector2f(0, 0), 1.0); + particleSystem.addParticle(p4); + + Particle p5(Vector2f(x_start + dx, y_start + dy), Vector2f(0, 0), Vector2f(0, 0), 1.0); + particleSystem.addParticle(p5); + + Particle p6(Vector2f(x_start + dx * 3, y_start + dy), Vector2f(0, 0), Vector2f(0, 0), 1.0); + particleSystem.addParticle(p6); + + Spring s1(0, 1, 0, dx); + particleSystem.addSpring(s1); + Spring s2(0, 2, 0, dx); + particleSystem.addSpring(s2); + Spring s3(1, 2, 0, dx); + particleSystem.addSpring(s3); + + Spring s4(3, 4, 0, dx); + particleSystem.addSpring(s4); + Spring s5(3, 5, 0, dx); + particleSystem.addSpring(s5); + Spring s6(4, 5, 0, dx); + particleSystem.addSpring(s6); + + Spring s7(1, 4, 0, dx); + particleSystem.addSpring(s7); + Spring s8(0, 3, 0, dx); + particleSystem.addSpring(s8); + Spring s9(2, 5, 0, dx); + particleSystem.addSpring(s9); } diff --git a/labo_physique/ParticleSystem.cpp b/labo_physique/ParticleSystem.cpp index a2171f7..5e4a9df 100644 --- a/labo_physique/ParticleSystem.cpp +++ b/labo_physique/ParticleSystem.cpp @@ -147,17 +147,19 @@ void ParticleSystem::buildDfDx(Matrix &dfdx) { auto p0 = m_particles[spring.index0]; auto p1 = m_particles[spring.index1]; - Vector distance = p1.x - p0.x; - Matrix distance_m = distance.as_matrix(); - Matrix distance_t = distance_m.transpose(); - float l = distance.norm(); - Matrix l2_m = std::pow(l, 2.0f) * identity; - float l3 = std::pow(l, 3.0f); + float l = (p1.x - p0.x).norm(); + float a = spring.k * (1 - spring.l0 / l); - Matrix dd = -1.0f * (distance_m * distance_t); - Matrix term1 = spring.k * identity; - Matrix term2 = -1.0f * (1 / l3) * spring.k * spring.l0 * (l2_m + dd); + Matrix diag; + diag.setZero(); + diag(0, 0) = -a; + diag(1, 1) = -a; - dfdx.block(spring.index0, spring.index1, 2, 2) = term1 + term2; + Matrix ndiag = -1.0f * diag; + + dfdx.block(spring.index0, spring.index1, 2, 2) = diag; + dfdx.block(spring.index0 + 2, spring.index1 + 2, 2, 2) = diag; + dfdx.block(spring.index0 + 2, spring.index1, 2, 2) = ndiag; + dfdx.block(spring.index0, spring.index1 + 2, 2, 2) = ndiag; } } diff --git a/labo_physique/Solvers.h b/labo_physique/Solvers.h index 062f75a..d593d23 100644 --- a/labo_physique/Solvers.h +++ b/labo_physique/Solvers.h @@ -86,12 +86,13 @@ namespace gti320 { bool converged = false; int k = 0; + Vector nx; do { - Vector nx = x; + nx = x; for (const auto &indices: P) { #pragma omp parallel for - for (int i = 0; i < indices.size(); i++) { + for (const int &i: indices) { nx(i) = b(i); for (int j = 0; j < i; j++) { @@ -99,7 +100,7 @@ namespace gti320 { } for (int j = i + 1; j < n; j++) { - nx(i) = nx(i) - A(i, j) * x(j); + nx(i) = nx(i) - A(i, j) * nx(j); } nx(i) = nx(i) / A(i, i);