How to Implement Damping in Particle Simulation

AI Thread Summary
A user created a simulation of charged particles aimed at demonstrating how charges tend to cluster at the ends of a line rather than the center. The simulation faced issues with oscillation, running elastically despite attempts to introduce damping by applying an opposing acceleration proportional to velocity. The user shared their code for calculating acceleration and setting velocity, highlighting a dampening approach that was ineffective. A suggestion was made to correct a misunderstanding in the code, indicating that the user was incorrectly using position values instead of velocity values for damping. This advice led to a successful resolution of the issue, improving the simulation's behavior.
TheDemx27
Gold Member
Messages
169
Reaction score
13
So I made this simulation of charged particles: https://github.com/TheDemx27/Charge-Simulator
My goal is to be able to put the charges on a line, as I've done, and see them tend toward a final state where there is more charge at the ends of the line and less towards the center. The problem is, the simulation runs like an elastically no matter what I try. At the moment, I've tried adding an acceleration in the opposing direction of motion, proportional to the velocity. This is the main loop for calculating the acceleration and setting the velocity:

Code:
            for (int i = 0; i < charges.Count; i++) {
                for (int j = i + 1; j < charges.Count; j++) {

                    distance = GetDistVec(charges[i], charges[j]);
                    f = charges[i].q * charges[j].q * k / (distance.Magnitude * distance.Magnitude);

                    acceleration1.Magnitude = Math.Abs(f / charges[i].m);
                    acceleration2.Magnitude = Math.Abs(f / charges[j].m);
                    acceleration1.Direction = distance.Direction;
                    acceleration2.Direction = distance.Direction += Math.PI;

                    // DAMPENING
                    double c = 10;

                    accel1 = PolarToComponent(acceleration1);
                    accel2 = PolarToComponent(acceleration2);

                    accel1.x -= c * charges[i].x;
                    accel1.y -= c * charges[i].y;
                    accel2.x -= c * charges[j].x;
                    accel2.y -= c * charges[j].y;
                    // DAMPENING

                    SetVelVec(charges[i], accel1);
                    SetVelVec(charges[j], accel2);
                }
            }

This doesn't seem to do anything. If I make c large enough, the oscillations get bigger, the sim eventually spirals out of control, and I get null values for the positions.
 
Technology news on Phys.org
Hi,I'm not quite certain but it looks like you are adding a spring, not damping, variable charges.x is an x position, not velocity. Change this to charges.vx

Hope this helps.
 
  • Like
Likes TheDemx27
Henryk said:
Hi,I'm not quite certain but it looks like you are adding a spring, not damping, variable charges.x is an x position, not velocity. Change this to charges.vx

Hope this helps.
Haha, yes that helped! Wow... ok then.
Thankyou!
 
https://media.giphy.com/media/xThuWlHZT7zEPGyYP6/giphy.gif
~success~
 
  • Like
Likes Twigg, cpscdave, JorisL and 1 other person
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top