We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Charged Particles
Pages: 1 2 3 
Charged Particles (Read 5612 times)
Re: Charged Particles
Reply #30 - Dec 16th, 2009, 12:06pm
 
I really shouldn't post things when I've got a cold, am half-asleep and not thinking straight...  I've just realised why it's actually right to do it 'twice':  since doing it only once means the force is only ever applied in one direction (in fact I'm sure this topic has come up before).  I still feel it should be possible to optimise the code; but I could most likely be wrong.  I'm almost inspired to have a go at a similar style of sketch to explore further!

The optimisation I suggested is still useful for things like collision detection though Wink
Re: Charged Particles
Reply #31 - Dec 16th, 2009, 12:57pm
 
Yeah, that's right. Optimization wise, the calculation of force needs to be done only once for both particles. So here's some code. I downloaded giles' code and made my changes in red color. I've moved the particles.move() out of the main loop and created a separate loop. This doesn't increase the amount of calculation but will better conserve energy and momentum. Remember some of the particle pairs (+ and -) sometimes start taking off in one direction all of sudden. It's because you calculated for one particle and move it before you move the other affected particle. My way only moves particles after all calculation is over and hopfully will not get the sudden take off pairs anymore. I'd really like to have a final copy of Giles' program to show my students how someone not in the field of physics or physical science is able to do a superb job on a difficult physics problem.

I admit, as a physicist, I should have been more careful on this. My last code only calculates half interactions. I'd right if I were calculating potential energy. I need coffee, my lame excuse.

 for(int i=0; i<numparticles; i++)
 {
   
   float xaccel=0, yaccel=0;
   for(int j=i+1; j<numparticles; j++)
   {
     float ijdist = dist(particles[i].xpos, particles[i].ypos, particles[j].xpos, particles[j].ypos);
     float theta = atan2(particles[i].ypos-particles[j].ypos, particles[i].xpos-particles[j].xpos);
       
       
       
       if(ijdist>20) //attractive or repulsive forces depending on charges
       {
         xaccel+=  particles[i].charge * particles[j].charge * (force/(ijdist*ijdist)) * cos(theta);  
         yaccel+=  particles[i].charge * particles[j].charge * (force/(ijdist*ijdist)) * sin(theta);  
       }
       
       else if(ijdist>0.1)  
       {
         xaccel+=  (force/(pow(ijdist,4))) * cos(theta);  
         yaccel+=  (force/(pow(ijdist,4))) * sin(theta);  
       }  
       
       
       particles[i].xspeed+=xaccel;
       particles[i].yspeed+=yaccel;
       particles[j].xspeed-=xaccel;
       particles[j].yspeed-=yaccel;

  }
}

for (int i=0;i<numparticles; i++)
{
  if(particles[i].affected)
  {
    particles[i].move();
  }
  particles[i].render();
}
Re: Charged Particles
Reply #32 - Dec 16th, 2009, 2:48pm
 
Ah...  I did think you could just do that (i.e. apply the negative force to the other particle in the same loop - though things are obviously a little less straightforward in other circumstances where a particle property affects the force (e.g. mass/gravity)) and almost posted the code; but I was seeing (or maybe imagining) some odd behaviour where distant particles also seemed to be strongly affected by the motion of another particle.  Is that expected behaviour?  I guess it could happen when a chain of alternately charged particles links the two groups...

One other thing I notice is that what is probably 'the right way' doesn't produce quite such an interesting result.  Things seem somewhat more static when you apply the motion after all forces are calculated - which kind of makes sense.  For instance you don't see chains of particles following each other around so much: I suspect that's caused by moving a particle before applying its force to another and therefore pulling in a particular direction...  That's a glitch I'd be tempted to leave in Wink
Re: Charged Particles
Reply #33 - Dec 16th, 2009, 3:31pm
 
Have you tried running this? At first it seems to have more realistic behaviour than my last version. As you add more and more particles, some strange behaviour starts occurring. All the particles on the screen will suddenly jerk in one direction. Chains will form, but they don't move around spontaneously as in my last version.

When I conceived of this program I was sort of expecting the particles to sort of pack together like in an ionic solid, for example - sodium chloride (but in 2D).  Because this would minimise the potential energy. Maybe if you used the Leonard-Jones model and had the ionic attractions in the right proportion to these van der Waals -type attractions (and repulsions at close range), it would work out that way.

As an artist, I'm interetsed in creating systems with interesting behaviour.... so the physical model is a conceptual starting point, but then, if things "go wrong", I'm happy to pursue it in that direction (which is an aesthetic direction). I know my program doesn't have realistic properties as it seems to generate infinite amounts of kinetic energy. Even though there is a significant damping, which should cause the energy be dissipated, the particles continue to move and chase each other around - like they have an internal energy source. So they are more like living things in that way. If it was modelled in a physically accurate way, I would expect it to move towards some equilibrium position and then remain static. That could be very interesting - as you added more particles you could see the system re-adjust and reach a new equilibrium.
Re: Charged Particles
Reply #34 - Dec 16th, 2009, 3:34pm
 
Snap - I posted the above at the same time as you, Blindfish - some of the same thoughts!

But if either of you want to develop this further, feel free... At the moment I feel like leaving it where it is, but may revisit at some point.
Re: Charged Particles
Reply #35 - Dec 17th, 2009, 9:04am
 
I'm glad that both of you're having some fun. The program doesn't have to do exact physics to be interesting. Yes, distant objects do interact and pull one another. On my side, I implemented simultaneous updating positions and r^4 repulsive forces. Two opposite charges with oscillate forever (true but not fun). Sometimes I can make two charge go around their common center while spining together. I'll use this next time I teach physics of electricity and magnetism. I also changed from random charge to positive/negative upon left/right mouse click.
Pages: 1 2 3