Moleculars physics

Don’t be shy to ask news if I don’t post for a long time.

I see you post on KaiKostack thread , hope to have news soon too :slight_smile:

Thanks alot and take care of you too!

New video of a empty pumpkin test:
http://www.youtube.com/watch?v=8rDUEcITCME

Looking great Pyroevil!

Thanks peddie!
Not bad but in late for halloween! :wink:
If I start a Santa Claus to simulate now I can get it for Christmas :slight_smile:

Hahaha! :slight_smile:

Sorry if I don’t give news about a long time.
I trying to compile my script in cython for have gain in speed and the result I get is 2.5-3x faster.
I don’t know if I did it wrong , because I see a lot of cython improvement with 10-20x gain
and some case of 50-100x faster.

Btw , I receive my bugtracker ticket answer about subframe particles access and it’s cannot be fixed. I’m a bit dissapointed because
controlling particles with python is really nice but useless if I have not access to all parameters.

For that , I’m starting to think about did my own python verlet particles engine to have all the power than Blender can’t give to me.
Controlling my own substep where a need it. But I’m a bit afraid about Python performance … hope cython can give a good boost with good processing time.I’m afraid about the limit of my knowledge too but I’m reading a bit of vector math now :S Not always easy for a non-programmer/non-developper/non-coder like me but a lot of example is available on the net if I did my search correctly on google.

have you used static variables ?

Yes I think…:o

Here is my .pyx code:


from math import trunc
cdef ptrunc(float n,int nb):
    cdef int vPrec = 5
    cdef int result= int(trunc(n*(10**vPrec)))
    return result
cpdef ApplyCns(dctCns,ParLoc,ParVel,float vMass,float vStiff,float vDamping,float vLimitMax,float vLimitMin,float vTimeStep,int vCnsTotal,int vBrokenTotal):
    cdef int vPrec = 5
    cdef vBrokenCns = []
 
    cdef Loc1,Loc2,V1,V2,Force1,Force2
    cdef float Length,tLength,InitLength,LenghtX,LenghtY,LenghtZ,Vx,Vy,Vz,V,ForceSpring,ForceDamper,ForceX,ForceY,ForceZ
    cdef int ii
    for ii in dctCns:
        Loc1 = ParLoc[dctCns[ii][0]]
        Loc2 = ParLoc[dctCns[ii][1]]
 
        Length=(Loc2-Loc1).length
        tLength=(ptrunc(Length,vPrec)*(0.1**vPrec))
 
        InitLength = dctCns[ii][2]
 
        vStiff=(dctCns[ii][4])
 
        if tLength != InitLength:
 
            V1=ParVel[dctCns[ii][0]]
            V2=ParVel[dctCns[ii][1]]
 
            LengthX=Loc2[0]-Loc1[0]
            LengthY=Loc2[1]-Loc1[1]
            LengthZ=Loc2[2]-Loc1[2]
 
            Vx=V2[0]-V1[0]
            Vy=V2[1]-V1[1]
            Vz=V2[2]-V1[2]
 
            V=(Vx*LengthX+Vy*LengthY+Vz*LengthZ)/Length
 
            ForceSpring=(Length-InitLength)*vStiff
 
            ForceDamper=vDamping*V
 
            ForceX=(ForceSpring+ForceDamper)*LengthX/Length
            ForceY=(ForceSpring+ForceDamper)*LengthY/Length
            ForceZ=(ForceSpring+ForceDamper)*LengthZ/Length
 
            Force1=[ForceX,ForceY,ForceZ]
            Force2=[-ForceX,-ForceY,-ForceZ]
 
            ParVel[dctCns[ii][0]]=ForceApply(ParVel[dctCns[ii][0]],Force1,vMass,vTimeStep)
            ParVel[dctCns[ii][1]]=ForceApply(ParVel[dctCns[ii][1]],Force2,vMass,vTimeStep)
            if tLength > (InitLength*(vLimitMax-dctCns[ii][3])) or tLength < (InitLength*(vLimitMin+dctCns[ii][3])):
                vBrokenTotal = vBrokenTotal+1
                vBrokenCns.append(ii)
 
    dctCns = RemoveCns(vBrokenCns,dctCns,vCnsTotal,vBrokenTotal)
    vBrokenCns=[]
 
    return ParVel,dctCns,vBrokenTotal
cdef ForceApply(objvel,force,vMass,vTimeStep):
    cdef float vVel1x = objvel[0]
    cdef float vVel1y = objvel[1] 
    cdef float vVel1z = objvel[2]
 
    cdef float vVel2x = (((force[0])*vMass)*(vTimeStep*2)) + (vVel1x)
    cdef float vVel2y = (((force[1])*vMass)*(vTimeStep*2)) + (vVel1y)
    cdef float vVel2z = (((force[2])*vMass)*(vTimeStep*2)) + (vVel1z)
    return [vVel2x,vVel2y,vVel2z]
cdef RemoveCns(vBrokenCns,dctCns,vCnsTotal,vBrokenTotal):
    print("    >"+str(len(vBrokenCns))+" broken link on "+str(len(dctCns)) +": ( " + str(vBrokenTotal)+"/"+str(vCnsTotal)+" or " +str(round(((vBrokenTotal/vCnsTotal)*100),2))+ "%)")
    if len(vBrokenCns) >= 1:      
        for i in vBrokenCns:
            del dctCns[i]
    vBrokenCns=[]
    return dctCns
 
 

dctCns = Dictionay of contrained particles with is index (to avoid blender obj)
-­>dctCns[Constraint Number][0] = index number of first particles (integer)
-­>dctCns[Constraint Number][1] = index number of second particles (integer)
-­>dctCns[Constraint Number][2] = init lenght of the constraint (float)
-­>dctCns[Constraint Number][3] = Max stretch before constraint break (float)
-­>dctCns[Constraint Number][4] = Stiffness of the constraint (float)
ParLoc= List of particles location with matched index to dctCns
ParVel= List of particles velocity with matched index to dctCns

Others variable are normal I think. Not complex list or dict
A script in blender collect all information and send it to the module to process. I time performance from module only.Anyway , tranfert data from blender to the module and from the module to Blender is really fast.

Any comment about how I use cython?

A new video with my own verlet particles system (with inelastic self collision):

It’s a rough integration. Calculate all distance between each particles in not optimal.I need to integrate a kind of octree too just calculate particles near of each others.

Another test where I integrate friction with the “invisble wall” , friction between particle and air damping:

It’s work great , more then was I expected.
The major thing I need to integrate is a kdtree or octree to optimize inter-collision of particles.

I find a python kdtree here : http://code.google.com/p/python-kdtree/
It’s very powerfull , kdtree generation take 13sec for 250 000particles but requesting to find the nearest neighbours particles of a particle or of a coordinate take 0.000002sec !And I can do a search for the 2 … 3 … 500 nearest neighbours and the time to find it is approximatly the same !!! The only problem is I cannot update particles position without have to rebuild the entire tree. I need to find a script with implementation of adding particles or changing particles corrdinate with self-balancing kd-tree. And just by reading wikipedia articles to see it’s seem to not be easy : http://en.wikipedia.org/wiki/K-d_tree#Adding_elements

My second option is octree. Octree seem to be easy to build and change I think. I need to find a good integration of it before to thinking about create my own. I’m not sure how octree exactly works like did a request like nearest neighbours.

My last option is to code my one tree. It’s I tree I have imaginated and it’s work. But it’s for sure is not the most efficient. Why ? because it’s soo simple than it’s impossible nobody thinking about it before me. I talk with my brother( I real coder/developper , ex-worker at Ubisoft and currently return at university to improve is skill ) yesterday and he think my idea is not bad but have probably limitation when time to come implementing other collision gemetry objects in the system than “same size” self-colliding particles.

My best guest for performance is Kd-Tree. Apparently XSI-ICE use it. I just need to find a self-balanced one :confused:. A lot of google search and reading to do !

Pyroevil, you are doing a fantastic job!

Really impressed.

Thank for your support Peddie !
The only one fan of my project :wink:

Yesterday I’m starting to play with KDtree and put it in my script.
For now I’m starting from this script:
http://code.google.com/p/python-kdtree/
with available cython/C optimized version: http://code-redefined.blogspot.com/2011/03/cython-made-my-python-code-go.html with stuning performance (12minutes in python again 8seconds in cython)

But I find this python Kdtree scripts too:



http://code.activestate.com/recipes/577497-kd-tree-for-nearest-neighbor-search-in-a-k-dimensi/
http://www.koders.com/python/fidEE2CD37AFBE880DA1D5C204C733970C352A9FDEE.aspx?s=self

I don’t know which scripts is the best , some of thoses offers great options but to have a example of cython conversion is great for the first script above. I need to check license of each script too. I try a fast implementation of the firt script in python in my script and I already see the gain for 4096particles self-collision: original script calculate at 1 frames per 38 seconds and with KDtree 1 frame for 6-7sec. Imagine the gain with Cython.

Coming soon with a full implementation of KDtree in my script with video test.

Oh yes, and I remove all code about friction. The ways I did it is really wrong. I finally find some docs about implementation of it. It’s not easy to find a clear one.

Other news:
I try to implement kdtree wednesday and I think is not the best algorithm for what I want to do.
Kdtree is very fast but for detecting collision between particles and static object. For self collision of
moving particles , it’s not great because I need to update the tree each time a particle move in the iteration. It’s take
too much time.

I ask on another forum of game dev about the question and somebody give me the tips about doing some search on google about grid hashing ! I think it’s the best and faster way to did it now.

I see alot of amazing video from Grant Kot , and I think he use grid hashing too … not sure : http://www.youtube.com/watch?v=WFHA7rQXkyU

Pyroevil, keep up the great work!

I wish that I could help you, but this is way above my current skill level in programming…

Pyroevil, it looks very promising!

the experimental architectural design would profit definitively from such destruction/deconstruction simulator.
Preferably driven by Python due to easiness to combine/extend with other Python modules - it would be perfect for rapid prototyping and experiments.

keep up the good work!

migius

I’m trying now to work with my girlfriend laptop in the bus.
And for that , I try to setup a github repo. All working fine and I read
a lot of tutorial and help file to try to understand how it’s work