starcore-explorer-bad/content/pyodide/pyb2d/gauss_machine.ipynb
2023-06-24 01:19:43 +08:00

129 lines
4.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "39a80aae-a990-4ed8-b880-3db2e7f70f16",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"if \"pyodide\" in sys.modules:\n",
" import piplite\n",
" await piplite.install('pyb2d-jupyterlite-backend>=0.4.2')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d9f8d68-0f3b-49c1-9512-e3e8344e7342",
"metadata": {},
"outputs": [],
"source": [
"from b2d.testbed import TestbedBase\n",
"import random\n",
"import numpy\n",
"import b2d\n",
"\n",
"\n",
"class GaussMachine(TestbedBase):\n",
"\n",
" name = \"Gauss Machine\"\n",
"\n",
" def __init__(self, settings=None):\n",
" super(GaussMachine, self).__init__(settings=settings)\n",
"\n",
" self.box_shape = 30, 20\n",
" box_shape = self.box_shape\n",
"\n",
" # outer box\n",
" verts = numpy.array(\n",
" [(0, box_shape[1]), (0, 0), (box_shape[0], 0), (box_shape[0], box_shape[1])]\n",
" )\n",
" shape = b2d.chain_shape(vertices=numpy.flip(verts, axis=0))\n",
" box = self.world.create_static_body(position=(0, 0), shape=shape)\n",
"\n",
" # \"bins\"\n",
" bin_height = box_shape[1] / 3\n",
" bin_width = 1\n",
" for x in range(0, box_shape[0], bin_width):\n",
" box = self.world.create_static_body(\n",
" position=(0, 0), shape=b2d.two_sided_edge_shape((x, 0), (x, bin_height))\n",
" )\n",
"\n",
" # reflectors\n",
" ref_start_y = int(bin_height + box_shape[1] / 10.0)\n",
" ref_stop_y = int(box_shape[1] * 0.9)\n",
" for x in range(0, box_shape[0] + 1):\n",
"\n",
" for y in range(ref_start_y, ref_stop_y):\n",
" s = [0.5, 0][y % 2 == 0]\n",
" shape = b2d.circle_shape(radius=0.3)\n",
" box = self.world.create_static_body(position=(x + s, y), shape=shape)\n",
"\n",
" # particle system\n",
" pdef = b2d.particle_system_def(\n",
" viscous_strength=0.9,\n",
" spring_strength=0.0,\n",
" damping_strength=100.5,\n",
" pressure_strength=1.0,\n",
" color_mixing_strength=0.05,\n",
" density=2,\n",
" )\n",
"\n",
" psystem = self.world.create_particle_system(pdef)\n",
" psystem.radius = 0.1\n",
" psystem.damping = 0.5\n",
"\n",
" # linear emitter\n",
" emitter_pos = (self.box_shape[0] / 2, self.box_shape[1] + 10)\n",
" emitter_def = b2d.RandomizedLinearEmitterDef()\n",
" emitter_def.emite_rate = 400\n",
" emitter_def.lifetime = 25\n",
" emitter_def.size = (10, 1)\n",
" emitter_def.transform = b2d.Transform(emitter_pos, b2d.Rot(0))\n",
"\n",
" self.emitter = b2d.RandomizedLinearEmitter(psystem, emitter_def)\n",
"\n",
" def pre_step(self, dt):\n",
" self.emitter.step(dt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51e5df1b-f5e6-486a-a6c0-173597198e5d",
"metadata": {},
"outputs": [],
"source": [
"from pyb2d_jupyterlite_backend.async_jupyter_gui import JupyterAsyncGui\n",
"s = JupyterAsyncGui.Settings()\n",
"s.resolution = [350,400]\n",
"s.scale = 11\n",
"tb = b2d.testbed.run(GaussMachine, backend=JupyterAsyncGui, gui_settings=s);"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}