Physic Simulation / python

Hello guys ,
i am currently working on a physic simulation and i ran to some problems :

1 - when i connect a timer to python script which create a point it change the point id every 1ms and it doesnt even matter if toggle is set true or false …
will this cause memory problem or speed problem in future !?

import rhinoscriptsyntax as rs



a = rs.AddPoint(10,10,10)

print a

if reset or 'counter' not  in globals():
    counter = 0
else:
    counter += 1

2 - i did a simple test simulation which ill post below and it works fine first several seconds but then it start to slow dawn , how can i counter this slowing down and please give some advises on writing simulation scripts so i can manage the speed of the script cause its the most important part for me.

Simulation Test :

import rhinoscriptsyntax as rs



agent = points



mass = 1
velo = (0,0,0)

# timer

if reset or 'counter' not  in globals():
    counter = 0
else:
    counter += 1





for i in range (counter+1):
    
    a = rs.AddPoint(agent)
    agent = rs.VectorAdd(agent,velo)
    
    distance = rs.Distance(anchor , agent)
    distance2 = rs.Distance(anchor2 , agent)
    
    if(distance < distance2):
        
        stiffness = distance
        
        force = rs.VectorSubtract(agent , anchor)
        force = rs.VectorUnitize(force)
        force = rs.VectorDivide(force ,stiffness)
        acc = rs.VectorDivide(force , mass)
        velo = rs.VectorAdd(velo , acc )
        
    elif(distance2 < distance):
        
        stiffness = distance2
        force = rs.VectorSubtract(agent , anchor2)
        force = rs.VectorUnitize(force)
        force = rs.VectorDivide(force ,stiffness)
        acc = rs.VectorDivide(force , mass)
        velo = rs.VectorAdd(velo , acc )

thanks

Arash

Is it something wrong with my questions !? If so plz let me know .
No one answering is kinda more confusing :frowning:

I don’t believe you’ve posted enough code for us to be helpful. I cannot run your code here…

Hello @dale , thank you for your comment , i will just upload the files so you can help me with them :slight_smile:

first question :

1.gh (3.0 KB)

as you can see in the file above i just connected a timer to python script , in the script i just created a point with rs.addPoint() and i printed the name of the point but based on timers setting the name of the point change , is this gonna be some kinda speed problem (( memory or process)) in future for me !?

second question (( THE MORE IMPORTANT ONE )) :

forceSimulation.gh (6.9 KB)

in this file i created a moving point between two anchor point , and the problem is with script continue running the simulation (( vis

Hey Arash,

Sorry, but I just don’t know enough about how GhPython works. You might want to move this post over the the Grasshopper developer category or to the Grasshopper community site.

hello @DavidRutten , sorry to bother you but can you please take a look at this topic and help me please !?

I don’t know enough about how Python is implemented to comment. @piac or @stevebaer may be able to help… maybe.

Though I will note that if speed is of paramount importance you do not want to use either the Rhino document or the Grasshopper document for storage.

High performance code will have to be written in such a way that it is not slaved to another platform which has things like redraw, and mouse event handling and undo to worry about. Do all the simulation in pure python or pure C# or pure C++, then inject your (partial) solutions into GH or Rhino whenever the time seems right.

Hi Arash

thanks for posting this question. I have to say it looks slightly introductory and it seems to be asking a tutorial rather than really an attempt to solve a problem. This is likely the reason it did not attract many replies. In the future, you could try to tackle the investigation on your own, and mention what you did and why. Only when you do not succeed, you could request that somebody takes the time to understand the purpose and the code and makes a better version of a part of it (btw, better: it really depends on the needs).

This being said; the main issue I see with the ‘slowness’ of the sample is that it will recompute the whole simulation from the beginning of time, every time. As the counter increases, it will take longer and longer for it to go through all the steps, all the way from the start to the moment where you want it to be. In fact, if the purpose is to visualize just the current position of the simulation, you could very well store the current state, for example in the sticky dictionary or in a variable that is treated like the ‘counter’ one, and elaborate the simulation from there.

Computing a single step will take about the same timespan every time.

If you are used to rhinoscript, as it seems, you could still use the RhinoCommon Point3d structures to keep data between iterations. You can use rs.coerceXXX to make sure you have a specific RhinoCommon object to keep between iterations. The nice thing is, that Point3d and Vector3d work with most point and vector methods in rhinoscriptsyntax.

Please let me know if you have more questions, or if I should help with the code itself.
Thanks,

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Thank you for your time and help @DavidRutten , i probably need to start learning pure C# or python as you said sometimes they are needed .

Hello @Giulio , thank you very much for your time , i guess it is as you said , i really need a tutorial for this kinda codes , but its not like i did not investigate , i really did a lot of search (( although it does not seems like it )) but i really did not know the source of the problem , i just did this simulation exactly the way i done in processing before .

at first i thought the problem is from grasshopper timer so i spent a lot of time creating a chronometer in python so i dont have to use timer but it was not successful either , the confusing thing was the second part of my question , the moment i connect timer to python component it start to change Point id based on timer setting :

import rhinoscriptsyntax as rs



a = rs.AddPoint(10,10,10)

print a

if reset or 'counter' not  in globals():
    counter = 0
else:
    counter += 1

so i thought maybe the problem come from memory and storing data.

now i know what is the problem but i did not get how to fix it so i really appreciate if you please help me with the code itself .

This should provide a sample for what you are trying to do. Here one timestep takes 2ms or less.

import rhinoscriptsyntax as rs

#initialize
if reset or 'agent' not in globals():
    agent = points
    mass = 1
    velo = (0,0,0)


#compute one step
agent = rs.VectorAdd(agent,velo)

distance = rs.Distance(anchor , agent)
distance2 = rs.Distance(anchor2 , agent)

if(distance < distance2):
    
    stiffness = distance
    
    force = rs.VectorSubtract(agent , anchor)
    force = rs.VectorUnitize(force)
    force = rs.VectorDivide(force ,stiffness)
    acc = rs.VectorDivide(force , mass)
    velo = rs.VectorAdd(velo , acc )
    
elif(distance2 < distance):
    
    stiffness = distance2
    force = rs.VectorSubtract(agent , anchor2)
    force = rs.VectorUnitize(force)
    force = rs.VectorDivide(force ,stiffness)
    acc = rs.VectorDivide(force , mass)
    velo = rs.VectorAdd(velo , acc )

#visualize
a = rs.AddPoint(agent) 

This works because each component is one instance of a script scope. If you do not override variables, they will be the ones from the previous iteration, unless you push the “Test”/play (in Rhino WIP) button. You could also, alternatively, save data into the scriptcontext.stricky dictionary. In this case it would be available also from the _EditPythonScript editor.

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

simulation-from-last-timestep.gh (8.3 KB)

Thanks @Giulio , it works a lot better now :smile:
i just have one more question if you please help me with that i promise i wont bother you for a long time :blush:

what if i want to Disable the timer after some iteration , do something then Enable it again ?

think of it like , when the point is at specific position , freeze the point , move one of anchor points then start the simulation again with point start to moving from the position it froze at .

See if this fits better the description… the main thing I did was to put the two forces of the two anchors together.

simulation-from-last-timestep-2.gh (7.6 KB)

Giulio

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Wow , nice …it doesnt get slow at all in this one , but the thing is that i want to have only one force on a agent at the time , but in this one i think we have both forces on the agent at all times right !?

@Giulio , i have some questions , when i connect a timer to the component it execute all the script every time , what if i want just one part of the script to loop base on timer ?

and how can i pause and reset the timer from inside the script ?

@piac , please take a look at this attachment , i tried to make it generate only one random point and at the same time move the agent , but it doesnt work .

forceSimulation&randomPoint.gh (9.0 KB)