Browse Source

new messy stuff

master
m 1 year ago
parent
commit
37c3628afa
4 changed files with 347 additions and 0 deletions
  1. +97
    -0
      doom_fire_psx.py
  2. +101
    -0
      doom_fire_psx2.py
  3. +78
    -0
      fdtd.py
  4. +71
    -0
      rps.py

+ 97
- 0
doom_fire_psx.py View File

@ -0,0 +1,97 @@
#!/usr/bin/env python3
import sys
import os
import numpy as np
import pygame
Nx = int(sys.argv[1])
Ny = int(sys.argv[2])
try:
fps = float(sys.argv[3])
except (IndexError, ValueError):
fps = 17
#fps = 15
colors = np.array([
0x0, 0x0, 0x0,
0x07,0x07,0x07,
# 0x1F,0x07,0x07,
0x2F,0x0F,0x07,
0x47,0x0F,0x07,
0x57,0x17,0x07,
# 0x67,0x1F,0x07,
0x77,0x1F,0x07,
0x8F,0x27,0x07,
0x9F,0x2F,0x07,
# 0xAF,0x3F,0x07,
0xBF,0x47,0x07,
0xC7,0x47,0x07,
0xDF,0x4F,0x07,
# 0xDF,0x57,0x07,
0xDF,0x57,0x07,
0xD7,0x5F,0x07,
0xD7,0x5F,0x07,
# 0xD7,0x67,0x0F,
0xCF,0x6F,0x0F,
0xCF,0x77,0x0F,
0xCF,0x7F,0x0F,
# 0xCF,0x87,0x17,
0xC7,0x87,0x17,
0xC7,0x8F,0x17,
0xC7,0x97,0x1F,
# 0xBF,0x9F,0x1F,
0xBF,0x9F,0x1F,
0xBF,0xA7,0x27,
0xBF,0xA7,0x27,
# 0xBF,0xAF,0x2F,
0xB7,0xAF,0x2F,
# 0xB7,0xB7,0x2F,
0xB7,0xB7,0x37,
# 0xCF,0xCF,0x6F,
# 0xDF,0xDF,0x9F,
# 0xEF,0xEF,0xC7,
# 0xFF,0xFF,0xFF
], dtype=np.uint8)
colors = np.reshape(colors, (colors.size//3, 3))
grid = np.zeros((Ny,Nx), dtype=np.uint8)
grid[:,0].fill(colors.shape[0]-1)
clock = pygame.time.Clock()
def state2red(c):
return colors[c, 0]
def state2green(c):
return colors[c, 1]
def state2blue(c):
return colors[c, 2]
i = 0
while True:
i = i + 1
rnd = np.random.randint(3, size=(Ny, Nx))
for py in range(Ny):
for px in range(1, Nx):
#grid[py, px] = max(grid[py-1, px] - 1, 0) # v1
#grid[py, px] = max(grid[py-1, px] - (rnd[py, px] & 1), 0) # v2
grid[(py - rnd[py, px] + 1) % Ny, px] = max(grid[py, px-1] - (rnd[py, px] & 1), 0) # v3
if i % 1000 == 0:
grid[:,0].fill(0)
elif (i - (colors.shape[0] +10)) % 1000 == 0:
grid[:,0].fill(colors.shape[0]-1)
out = np.dstack((state2red(grid), state2green(grid), state2blue(grid))).astype(np.uint8).tobytes()
os.write(1, out)
clock.tick_busy_loop(fps)

+ 101
- 0
doom_fire_psx2.py View File

@ -0,0 +1,101 @@
#!/usr/bin/env python3
import sys
import os
import numpy as np
import pygame
Nx = int(sys.argv[1])
Ny = int(sys.argv[2])
try:
fps = float(sys.argv[3])
except (IndexError, ValueError):
fps = 15
#fps = 15
colors = np.array([
0x0, 0x0, 0x0,
# 0x07,0x07,0x07,
# 0x1F,0x07,0x07,
# 0x2F,0x0F,0x07,
# 0x47,0x0F,0x07,
0x57,0x17,0x07,
# 0x67,0x1F,0x07,
# 0x77,0x1F,0x07,
# 0x8F,0x27,0x07,
0x9F,0x2F,0x07,
# 0xAF,0x3F,0x07,
# 0xBF,0x47,0x07,
# 0xC7,0x47,0x07,
0xDF,0x4F,0x07,
# 0xDF,0x57,0x07,
# 0xDF,0x57,0x07,
# 0xD7,0x5F,0x07,
# 0xD7,0x5F,0x07,
# 0xD7,0x67,0x0F,
# 0xCF,0x6F,0x0F,
# 0xCF,0x77,0x0F,
0xCF,0x7F,0x0F,
# 0xCF,0x87,0x17,
# 0xC7,0x87,0x17,
# 0xC7,0x8F,0x17,
# 0xC7,0x97,0x1F,
# 0xBF,0x9F,0x1F,
# 0xBF,0x9F,0x1F,
# 0xBF,0xA7,0x27,
0xBF,0xA7,0x27,
# 0xBF,0xAF,0x2F,
# 0xB7,0xAF,0x2F,
# 0xB7,0xB7,0x2F,
# 0xB7,0xB7,0x37,
# 0xCF,0xCF,0x6F,
# 0xDF,0xDF,0x9F,
# 0xEF,0xEF,0xC7,
# 0xFF,0xFF,0xFF
], dtype=np.uint8)
colors = np.reshape(colors, (colors.size//3, 3))
grid = np.zeros((Ny,Nx), dtype=np.int)
gridout = np.zeros((Ny,Nx), dtype=np.int)
grid[0,:].fill(colors.shape[0]-1)
clock = pygame.time.Clock()
def state2red(c):
return colors[c, 0]
def state2green(c):
return colors[c, 1]
def state2blue(c):
return colors[c, 2]
i = 0
while True:
i = i + 1
rnd = np.random.randint(3, size=(Ny, Nx))
for px in range(Nx):
for py in range(1, Ny):
#grid[py, px] = max(grid[py-1, px] - 1, 0) # v1
#grid[py, px] = max(grid[py-1, px] - (rnd[py, px] & 1), 0) # v2
grid[py, (px - rnd[py, px] + 1) % Nx] = max(grid[py-1, px] - (rnd[py, px] & 1), 0) # v3
#if i % 100 == 0:
# grid[0,:].fill(0)
#elif (i - (colors.shape[0] +10)) % 100 == 0:
# grid[0,:].fill(colors.shape[0]-1)
gridup = np.flipud(np.fliplr(grid))
np.clip(grid + gridup, 0, colors.shape[0]-1, out=gridout)
#out = np.dstack((state2red(gridout), state2green(gridout), state2blue(gridout), np.full((Ny, Nx), 100, dtype=np.uint8))).astype(np.uint8).tobytes()
out = np.dstack((state2red(gridout), state2green(gridout), state2blue(gridout))).astype(np.uint8).tobytes()
os.write(1, out)
clock.tick_busy_loop(fps)

+ 78
- 0
fdtd.py View File

@ -0,0 +1,78 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# From the Meep tutorial: plotting permittivity and fields of a bent waveguide
from __future__ import division
import meep as mp
import matplotlib.pyplot as plt
import os
import sys
import numpy as np
from matplotlib import cm
import pygame
mp.quiet(True)
Nx = int(sys.argv[1])
Ny = int(sys.argv[2])
try:
fps = float(sys.argv[3])
except (IndexError, ValueError):
fps = 40
cell = mp.Vector3(8,4,0)
geometry = [mp.Block(mp.Vector3(12,1,mp.inf),
center=mp.Vector3(0,0),
material=mp.Medium(epsilon=24))
]#,
#mp.Block(mp.Vector3(1,5,mp.inf),
# center=mp.Vector3(1,1),
# material=mp.Medium(epsilon=12))]
#pml_layers = [mp.PML(2.0)]
resolution = 10
sources = [mp.Source(mp.ContinuousSource(frequency=0.15, width=20), # wavelength=2*(11**0.5)
component=mp.Ez,
center=mp.Vector3(0,0),
size=mp.Vector3(0,1))
]
sim = mp.Simulation(cell_size=cell,
geometry=geometry,
sources=sources,
resolution=resolution)
buffer = np.empty([80,40])
clock = pygame.time.Clock()
def get_slice(sim):
val = sim.get_array(center=mp.Vector3(), size=cell, component=mp.Ez, arr=buffer)
#plt.figure()
#plt.imshow(val, interpolation='none', cmap='RdBu')
#plt.axis('off')
#plt.show()
out = (val - np.amin(val)) / (np.amax(val) - np.amin(val))
out = np.transpose(out) # or switch x and y
out = out.ravel()
out = 250*cm.RdBu(out)
out = out[:,0:3]
out = out.ravel()
out = out.astype(np.uint8).tobytes()
os.write(1, out)
clock.tick_busy_loop(fps)
def plot_dielectric(sim):
val = sim.get_array(center=mp.Vector3(), size=cell, component=mp.Dielectric, arr=buffer)
plt.figure()
plt.imshow(val, interpolation='none', cmap='RdBu')
#plt.axis('off')
plt.show()
print()
#sim.run(mp.at_beginning(plot_dielectric), mp.at_every(0.6, get_slice), until=200)
sim.run(mp.at_every(0.00001, get_slice), until=1000)

+ 71
- 0
rps.py View File

@ -0,0 +1,71 @@
#!/usr/bin/env python3
import sys
import os
import numpy as np
import pygame
Nx = int(sys.argv[1])
Ny = int(sys.argv[2])
try:
frequency = float(sys.argv[3])
except (IndexError, ValueError):
frequency = 5
fps = 10
nstates = 5
colors = np.linspace(0, 255, nstates, dtype=np.uint8)
red = np.random.choice(colors, size=(Ny,Nx))
green = np.zeros((Ny, Nx))
blue = np.zeros((Ny, Nx))
grid = np.dstack((red, green, blue)).astype(np.uint8)
clock = pygame.time.Clock()
def clamp(value, min, max):
if value > max:
return max
elif value < min:
return min
return value
while True:
for px in range(Nx):
for py in range(Ny):
statecount = np.zeros(nstates, dtype=np.uint8)
for ix in [-1, 0, 1]:
for iy in [-1, 0, 1]:
if (px+ix) >= Nx or (px+ix) <= 0 or (py+iy) >= Ny or (py+iy) <= 0:
i = 0
else:
i = int(grid[py + iy, px + ix, 0] / colors[1])
#i = int(grid[(px + ix) % Nx, (py + iy) % Ny, 0] / colors[1])
#i = int(grid[max(min(px + ix, Nx-1), 0), max(min(py + iy, Ny-1), 0), 0] / colors[1])
#print(px, py, max(min(px + ix, Nx-1), 0), max(min(py + iy, Ny-1), 0), file=sys.stderr)
statecount[i] = statecount[i] + 1
i = int(grid[py, px, 0] / colors[1])
i_successor = (i+1) % colors.size
if statecount[i_successor] >= 2: #(2-np.random.randint(0, 2)):
grid[py, px, 0] = colors[i_successor]
# for i in range(Nx*Ny):
# c1 = clamp(i % Nx - 1, 0, Nx - 1)
# c2 = clamp(int(i / Nx) - 1, 0, Ny - 1)
# neighbourhood = grid[c1:(c1+3), c2:(c2+3), 0]
# #print(neighbourhood.size, file=sys.stderr)
out = grid.tobytes()
os.write(1, out)
clock.tick_busy_loop(fps)

Loading…
Cancel
Save