Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

3 Dimensional repelling particle simulation

by JPaul (Hermit)
on Mar 18, 2002 at 23:57 UTC ( [id://152613]=perlquestion: print w/replies, xml ) Need Help??

JPaul has asked for the wisdom of the Perl Monks concerning the following question:

Greetings all,
I'm attempting to work out how to produce a 3D simulation of repelling particles, but I just can't seem to wrap my head around the theory behind it.

I have a list of nodes (particles) and links (connections between particles). Each link has a certain 'charge' to it, indicating the resultant distance between nodes.
I randomly give each node a co-ordinate in space, and then tell the simulator to work out the negative charges and thusly the co-ordinates of nodes to produce the correct distance between nodes.
Clear as mud? Basically each particle repels all other particles, the charge of the links between particles determines the strength of the repulsion between those particles, and thusly the distance between them.
So when I set the simulation going, it will push all the linked nodes away from each other as far as the charges suggest, moving them around in 3D space and at the end of the simulation, outputting correct co-ordinates of the nodes.

I just can't work out how to... well... do any of it. Can anyone point me off in the right direction?

JP,
-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

  • Comment on 3 Dimensional repelling particle simulation

Replies are listed 'Best First'.
Re: 3 Dimensional repelling particle simulation
by Zaxo (Archbishop) on Mar 19, 2002 at 00:33 UTC

    If you want to imitate electrical charges, the charge is a property of the particle, not the "link". The electrostatic force between two is given by the product of the two charges divided by the square of the distance between them. Your link construct is good for keeping track of the distances.

    A real physical model would need to keep track of magnetic effects and radiation, too. For that, a construct called the vector potential will be useful.

    You may also be missing the dynamical aspect of the problem. The inertia of the particles determines their response to a force (F=ma and its relativistic descendant).

    An elementary physics text will explain how to use that, with examples and exercises.

    After Compline,
    Zaxo

Re: 3 Dimensional repelling particle simulation
by rbc (Curate) on Mar 19, 2002 at 00:19 UTC
    Hi JP,

    I humbly suggest some of my perl mods.
    you could download CECALA.tar.gz from CPAN

    you'll want Perspective.pm, Vector3D.pm, and Viewport.pm
    from that archieve.

    But these are in no way as good a OpenGL
Re: 3 Dimensional repelling particle simulation
by rjray (Chaplain) on Mar 19, 2002 at 01:20 UTC

    Not to sound harsh, but since you pointed out to the one reply that offered actual Perl that "I know perl. I'm already past that stage", how is this a Perl programming issue? It seems more like a particle physics or electron dynamics problem. You might be better-suited to asking for help in those types of forums, and come to this forum when you need help on things more specific to Perl.

    --rjray

      Greetings again,
      I have asked this question wrong - and indeed, was somewhat harsh to the first reply.
      You are quite right - this is, technically, not a perl question per se - but since this code will be implemented in perl, I hoped it would be close enough that I would be able to arouse the brains of others into helping me with this conundrum

      I felt that the question posed showed enough complexity that a very simple "How to use foreach" was clearly not what I wanted in an answer thus the short, sharp nature of my reply.
      HOWEVER in saying that, the fact that jhanna gave me an explanation that quite possibly a number of monks would say 'What a moron, go read a book' is a good thing.
      So, thanks jhanna, my apologies for being short - but that wasn't quite the answer I was looking for.

      I'm going to restate my question like I should have in the first place. The possibly misleading manner I asked it in was how the simulation was explained to me:
      I was playing around with making my own IP map. Nothing large, just my local network for a laugh and to show off. The nodes are the IP addresses, the links are the routes between them.
      You do multiple traceroutes, from multiple locations to create an interlinked "map" (Which I have neatly stored in a MySQL DB). The "distance" between nodes is calculated from the average "distance", in milliseconds, from IP addresses in the traceroute.
      The links are like springs, their repulsion charge is the given distance, and when set going in the simulation, the nodes will push each other apart in such a way that they place themselves roughly in space to equate the "distances" from other IP addresses...
      I want all the co-ordinates, after the simulation has run and spaced them accordingly, to dump into POVray.

      I hope this is more clear,
      JP,
      -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

        Well, I'm no theoretical mathematician, but I think your problem is going to involve compromise on some level. In fact, for even three nodes, it's possible that the "link distance" is insolvable, if the ping times between A and B exceed the sum of the ping times from A to C and C to B. So whatever algorithm you choose, it's going to involve some sort of first order approximation, and a satisfaction threshold, rather than some rigid math. Maybe that will help decide how to do it.

        -- Randal L. Schwartz, Perl hacker

        Randomly (or equally for that matter) place them on the surface of a very very large sphere. The force between particles is the distance minus the ping. Scale it by a factor of 100 or so (mass of particle). That will be your acceleration (distance and acceleration are vectors remember) for that link. Calculate one for each link the particle has. Store the acceleration per link for later. Go to the next particle and repeat. Note acceleration(a->b) == -acceleration(b->a) if all particles have equal mass. Once you have all of the acceleration vectors for each particle-particle link visit each particle, sum it's acceleration vector (should point roughly towards 0,0,0) and move the particle acceleration times time (assume 1). Repeat. You will have to tune it a bit, what to use for mass, how far apart to start, maybe modify the acceleration calculation to push out harder when particles are too close than it would pull when the particles were too far apart. Over time/iterations it just might converge ;). The usual method would be to stop when the accelerations are all under a certain amount (should approach zero).

Re: 3 Dimensional repelling particle simulation
by shotgunefx (Parson) on Mar 19, 2002 at 00:44 UTC
    Maybe a little OT but you may want to check out Computer Graphics: Prinical and Practice by Foley & vanDam (and others). There was quite a bit dealing with particle systems. New versions of the book have actual C code. The older ones just had math notation. (I found it very hard to fathom due to my lack of formal mathematics education.) I bought mine years ago and it was fairly expensive (~$70 USD.)

    -Lee

    "To be civilized is to deny one's nature."
Re: 3 Dimensional repelling particle simulation
by jhanna (Scribe) on Mar 19, 2002 at 00:14 UTC
    for $node (@nodes) { for $link ($node->{links}) { &updatePosition($link->node,$link); } }
    What this may not take into account is that in real life all nodes move at once, not one at a time, but it might help you get started.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: 3 Dimensional repelling particle simulation
by braughing (Sexton) on Mar 24, 2002 at 18:11 UTC
    To get a pair of nodes to have an equilibrium distance that they gradually work towards I use springs, not charges. The magnitude of the force on each node is proportional to the (signed) difference between the natural length of the spring and the distance between the two nodes, and the force is directed parallel to the separation vector between them (repulsive if they're too close together and attractive if they're too far apart). Put friction in if you want an equilibrium to be reached. I like making Platonic polyhedra out of elastic and bouncing them around.

    Put two like charges in deep space and the distance between them will become very large.

    Update: Since it's a non-Perl question anyway, here's the C++ subroutines for the important bit.

    // double position[12][3] and double velocity[12][3] are global // arrays of 3D vectors // int edge[0] and int edge[1] are the indices in position[][3] and // velocity[][3] of the two nodes making up this edge, or link // double friction is about 0.9999 // double natural is about 1 void icosahedron::cord(const int *edge) { double s[3], rr; rr=0; for (int i=0; i<3; i++) { s[i]=position[edge[1]][i]-position[edge[0]][i]; rr+=s[i]*s[i]; } if (rr>0) { double strain=spring*(natural/sqrt(rr)-1); for (int i=0; i<3; i++) { rr=strain*s[i]; velocity[edge[0]][i]-=rr; velocity[edge[1]][i]+=rr; } } } //------------------------------------------------- void icosahedron::move() { double *pos,*vel; for (pos=*position, vel=*velocity; pos-*position<36; pos++, vel++) *pos+=*vel; for (vel=*velocity; vel-*velocity<36; vel+=3) *vel*=friction; }
    I hope this is helpful.
    Update: removed reference to 'fun'. Finite-element analysis is not fun.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://152613]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found