Mandelbrot set with python

Hi. Cannot get my head around making a python script that runs the mandelbrot function z=z*z +c

new to python and have tried this script with no luck.

would be so thankful for help!

Can you show what you’re inputting and what’s outputting.

OK… that looks a little chaotic…
What’s going into the python node and what’s coming out.

Can you paste the graph maybe?

Sorry :slight_smile:
Maby this could help?

Python:
Line 7 : You’ve not defined b, it’s just a totally blank list.
Line 12: C is never used
Line 14 you’re trying to multiply a blank by a blank then by some points

Have this visiual basic script in grasshopper that does what im aiming for…

Just made a couple of notes.
Doesn’t solve your issue but it may give you some pointers.

Top tip is… Use notes so it’s clear and group things logically.
What you’ve got so far is incredibly hard to decipher.

Think will help a lot! Thanks!

Final notes from me:

Be cautious of squaring squares… Think of the parable of the man who invented chess and for his reward asked for 1 grain of rice on the first square, double on the next, double on the next etc…
(the numbers are going to get HUGE)

Also read: https://primer.dynamobim.org/

And look at the sample programs you get with Dynamo.

There’s an example with a coloured ‘worm’ and spiral somewhere in those that you may find useful.

1 Like

and set the maximum number of iterations
:rofl:

3 Likes

Hello,
Where does the difference between the 2 versions come from?

Python script:

# Charger les bibliothèques DesignScript et Standard Python
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Les entrées effectuées dans ce noeud sont stockées sous forme de liste dans les variables IN.
zz=[]
# Placer votre code au-dessous de cette ligne
def z(n, c):
	if n == 0:
		return 0
	else:
		return z(n - 1, c) * z(n - 1, c) + c

for i in IN[0]:
	zz.append(z(i,IN[1]))
# Affectez la sortie à la variable OUT.
OUT = zz

code block:

n;
z=[];
c;
[Imperative]
{
z[0]=0;
for (i in 1..(DSCore.List.Count(n)-1))
{
z[i]=z[i-1]*z[i-1]+c;
}
return z;
};

edit:
oops, this is from my incoming (No double)

cordially
christian.stan

1 Like

Mandelbrot with Design Script.


Code available here

13 Likes

<3 Wow:)

Your posts are always on another level!
Love it!

2 Likes

Hi,

to infinity and beyond with DynamoRevit :upside_down_face: :microscope:

Mandelbrot test3

Python Code
import sys
import clr
import System

clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)

clr.AddReference('DynamoRevitDS')
import Dynamo 
#reference dynamo core to update node values
clr.AddReference('DynamoCore')
from Dynamo.Graph import UpdateValueParams

#access to the current Dynamo instance and workspace
dynamoRevit = Dynamo.Applications.DynamoRevit()
engine = dynamoRevit.RevitDynamoModel.EngineController
currentWorkspace = dynamoRevit.RevitDynamoModel.CurrentWorkspace
model = dynamoRevit.RevitDynamoModel

clr.AddReference('System.Drawing')
from System.Drawing import Color, Bitmap

pf_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
sys.path.append(pf_path + '\\IronPython 2.7\\Lib')
import colorsys
import math
import time

px, py = -0.7746806106269039, -0.1374168856037867 #Tante Renate
R = 3 
max_iteration = 170 #2500
w, h = 400, 400 #1024,1024
mfactor = 0.5

def Mandelbrot2(*args):
	"""
	get Mandelbrot fractal 
	"""
	x, y, max_iteration, minx, maxx, miny, maxy = args
	zx = 0
	zy = 0
	RX1, RX2, RY1, RY2 = px-R/2.0, px+R/2.0, py-R/2.0, py+R/2.0
	cx = (x-minx)/float(maxx-minx)*(RX2-RX1)+RX1
	cy = (y-miny)/float(maxy-miny)*(RY2-RY1)+RY1
	i=0
	while zx**2 + zy**2 <= 4 and i < max_iteration:
		temp = zx**2 - zy**2
		zy = 2*zx*zy + cy
		zx = temp + cx
		i += 1
	return x, y, i

def gen_Mandelbrot_image(sequence):
	bmpOut = Bitmap(w, h)
	byteArr = []
	ziplst = []
	for x in range(w):
		for y in range(h):
			ziplst.append([x, y, max_iteration, 0, w-1, 0, h-1])
			
	# apply multiprocessing parrlalelisme
	threadResult = ziplst.AsParallel()\
		.WithDegreeOfParallelism(System.Environment.ProcessorCount)\
		.WithExecutionMode(System.Linq.ParallelExecutionMode.ForceParallelism)\
		.Select(lambda e : Mandelbrot2(*e))#.ToList()
		
	# color pixels
	for x, y, c in threadResult:
		v = c**mfactor/float(max_iteration**mfactor)
		hv = 0.67-v
		if hv<0: 
			hv+=1
		r,g,b = colorsys.hsv_to_rgb(hv,1,1-(v-0.1)**2/0.9**2)
		r = min(255,round(r*255))
		g = min(255,round(g*255))
		b = min(255,round(b*255))
		bmpOut.SetPixel(x, y, Color.FromArgb(r, g, b))
	#
	return bmpOut

debug = []
nodeK = next((i for i in currentWorkspace.Nodes if i.Name.Equals("K_R")), None)
if nodeK is not None:
	# get values
	k = int(nodeK.GetValue(0,engine).Data) 
	R = float(nodeK.GetValue(1,engine).Data) 

f = 0.80
RZF = 1/1000000000000

if k < 30 :
	mfactor = 0.5 + (1/float(1000000000000))**0.1/float(R**0.1)
	print(k,mfactor)
	t1 = time.time()
	imgM = gen_Mandelbrot_image(k)
	print("elasped {}s".format(time.time() - t1))
	R *= f
	k+=1
	params = UpdateValueParams("Code", "{0};\n{1};".format(k, R))
	nodeK.UpdateValue(params)

	OUT = imgM

work only with IronPython2 and Revit
mandelbrot zoom_v3.dyn (10.2 KB)

10 Likes

:face_with_spiral_eyes: Woow

1 Like

So here is the development of the Mandelbrot fractal. -thought only way to continue after being given such great help was to make a mandelbulb instead. :blush: -Looks promising but takes really long time to calculate.
-Any idea of how to speed up the calculation?
-And what would be a good way to make it into a solid/surface. -A voxel/cuboid approach?

2 Likes