running recursive algorithm with kangaroo physic in GH python

running recursive algorithm with kangaroo physic in GH python

Hello World!

For mid-advanced level grasshopper user, writing looping algorithm is not a big problem. There are several plugins, such as anemone and hoopsnake can help us with the topic.  Moreover, combing looping function with kangaroo physic can create an exciting and sophisticated result. The approach has been frequently used for simulating differential growth or cellular growth.

However, the slow computational speed is a disadvantage for doing the recursive algorithm in grasshopper, especially if the mesh subdivision or smoothing are involved.

Therefore, running the recursive algorithm with the kangaroo in a programming language will be a better solution regarding speed-wise or emotional wise. For sure, all of us have this upset moment while your laptop keeps crashing while 3d modeling.

In the following screenshot, you may see a significant time difference between running a recursive algorithm with Kangaroo in anemone and Kangaroo in python.

 

 

 

With looping kangaroo and weaverbird subdivision in Anemone, It took 4.4 sec for the solver to compute. Besides, different goal-objects in kangaroo take another 4 sec approximately and around 8~9 seconds in total.

On the other hand, it only takes 1.5~1.9 seconds total for GHpython to run through the same recursive algorithm. Consequently, it is an intelligent choice if you want to write the recursive algorithm with Kangaroo in C++ or python.

 

 

 

Following paragraph, I will elaborate on the main idea behind the code. Unfortunately, the script is part of another project which I cannot disclose. However, you may refer to Anders Deleuran’s Github, which has the example files for how Kangaroo works with GHpython.


import clr
clr.AddReferenceToFile("KangarooSolver.dll")
import KangarooSolver as ks
from System.Collections.Generic import List

#creating the solver system and the empty goal list
ps = ks.PhysicalSystem()
goals = List[ks.IGoal]()

#adding particles in the system
for k in range(mesh.Vertices.Count):
    ps.AddParticle(pts[k],1)

#adding specific goals, such as anchor points, spring force...etc
for l in range(len(random_index)):
    goals.Add(ks.Goals.Anchor(random_index[l],pts[random_index[l]],10000))

# Solve zombie style i.e. max N iterations, break when system kinetic energy drops below threshold
counter = 0
N = 5000
while counter < N:
    ps.Step(goals,True,threshold)
    counter += 1
    if ps.GetvSum() < threshold:
        break

*the code will not work if you directly copy and paste since it is extracted from another python file.

line1~4

Several things need to be noticed; For calling the API from kangaroo solver, you need to import clr to load the .dll to the current file.

line 6~8

Here we create the solver system, which is similar to the kangaroo solver in grasshopper. We create the system, and an empty goal list allows later to append different goal-objects.

line 10~16
Basically, it is same as the workflow in grasshopper. We create different goals (by using ks.Goals method, you can access to almost all the physic simulation method in kangaroo) and collect them in the same Goal List. Then, feed the list to the solver system to compute the result.

Although most designers and architects do not have experience in coding, the code for kangaroo integrating into GHpython is not that complicated. The script is pretty similar to the grasshopper workflow.  Therefore, I will highly recommend using GHpython or C++ to write the script, if you are trying to play with Kangaroo in the recursive algorithm.
If you have more doubts or find the mistake in the article, feel free to leave the message or send me an email.
Best!

Back

This is a unique website which will require a more modern browser to work!

Please upgrade today!

Share