From 93349206eddc809910c3463f2d29727789ae2ad5 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sun, 7 Jun 2020 15:13:31 +0800 Subject: [PATCH] add Mandelbrot example --- examples/mandelbrot.py | 50 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/mandelbrot.py diff --git a/examples/mandelbrot.py b/examples/mandelbrot.py new file mode 100644 index 0000000..75697fa --- /dev/null +++ b/examples/mandelbrot.py @@ -0,0 +1,50 @@ +import sys + +from artiq.experiment import * + + +class Mandelbrot(EnvExperiment): + """Mandelbrot set demo""" + + def build(self): + self.setattr_device("core") + + @rpc(flags={"async"}) + def col(self, i): + sys.stdout.write(" .,-:;i+hHM$*#@ "[i]) + + @rpc(flags={"async"}) + def row(self): + print("") + + @rpc(flags={"async"}) + def prt(self, x): + print(x) + + + # based on: http://warp.povusers.org/MandScripts/python.html + @kernel + def run(self): + minX = -2.0 + maxX = 1.0 + width = 78 + height = 36 + aspectRatio = 2 + + yScale = (maxX-minX)*(height/width)*aspectRatio + + for y in range(height): + for x in range(width): + c_r = minX+x*(maxX-minX)/width + c_i = y*yScale/height-yScale/2 + z_r = c_r + z_i = c_i + i = 0 + for i in range(16): + if z_r*z_r + z_i*z_i > 4: + break + new_z_r = (z_r*z_r)-(z_i*z_i) + c_r + z_i = 2*z_r*z_i + c_i + z_r = new_z_r + self.col(i) + self.row()