{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89f66dde-ee06-4c33-924c-893265d305a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solve poisson on unit square\n",
    "from ngsolve import *\n",
    "from ngsolve_webgpu.jupyter import Draw\n",
    "\n",
    "mesh_ = Mesh(unit_square.GenerateMesh(maxh=0.2))\n",
    "fes = H1(mesh_, order=3, dirichlet=[1,2,3,4])\n",
    "\n",
    "u = fes.TrialFunction()\n",
    "v = fes.TestFunction()\n",
    "\n",
    "gfu = Solve(grad(u)*grad(v)*dx == 32 * (y*(1-y)+x*(1-x)) * v * dx)\n",
    "print(type(gfu))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e6cc612-3d80-4b9a-9e3e-81c79df082d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(Draw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e72d5e5-d958-4431-a375-996593f0df8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "scene = Draw(gfu, mesh_, order=2, subdivision=3, isolines=10, deformation=CF((0,0,gfu)), width=800, height=400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bbbc83e2-8914-4c0f-ba96-4a5d79d5d169",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ngsolve_webgpu import *\n",
    "from webgpu import Labels\n",
    "#help(SurfaceVectors)\n",
    "mesh_data = scene.render_objects[0].data\n",
    "gradient_data = FunctionData(mesh_data, grad(gfu), order=3)\n",
    "vectors = SurfaceVectors(gradient_data)\n",
    "scene.render_objects.append(vectors)\n",
    "\n",
    "label = Labels([\"\"], [(-0.9, -0.9)])\n",
    "scene.render_objects.append(label)\n",
    "#Draw(scene.render_objects)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec90b4e9-3230-4fad-92ca-a58c661d159f",
   "metadata": {},
   "outputs": [],
   "source": [
    "iso = scene.render_objects[1]\n",
    "camera = scene.options.camera\n",
    "camera.set_orthographic(True)\n",
    "import time\n",
    "for i in range(10):\n",
    "    iso.isolines.n_lines = i\n",
    "    scene.render()\n",
    "    scene.save_screenshot(f\"screenshot_{i}.png\")\n",
    "    camera.transform.rotate(10, 10)\n",
    "    camera._notify_observers()\n",
    "    label.labels[0] = f\"time = {i}\"\n",
    "    label.set_needs_update()\n",
    "    time.sleep(0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2270b95-9f4a-40b1-af5f-b6ec4bee5838",
   "metadata": {},
   "outputs": [],
   "source": [
    "Draw(grad(gfu), mesh_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96a63f2e-9043-495f-b2e1-099422e02590",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(Labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0155a42f-af0e-4fb8-889c-cb82ee168e1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "help(vectors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "497b18dd-85b9-4c3c-800c-8cb6fa595910",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.14.6"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
