{ "cells": [ { "cell_type": "markdown", "id": "a1", "metadata": {}, "source": [ "# Coefficient Functions\n", "\n", "Rendering NGSolve `CoefficientFunction`s on meshes using `ngsolve_webgpu`.\n", "\n", "- `FunctionData` stores the GPU representation of a CoefficientFunction.\n", "- `CFRenderer` renders CoefficientFunctions on 2D surface elements.\n", "- `ClippingCF` adds a cross-section view for 3D meshes.\n", "- For vector-valued CFs, a component dropdown appears automatically." ] }, { "cell_type": "markdown", "id": "a2", "metadata": {}, "source": [ "## Rendering a Scalar Function on a Surface\n", "\n", "Create a `MeshData` from a mesh, wrap a CF in `FunctionData`, and render with `CFRenderer`. Add a `Colorbar` for reference." ] }, { "cell_type": "code", "execution_count": null, "id": "a3", "metadata": {}, "outputs": [], "source": [ "from netgen.occ import *\n", "from ngsolve import *\n", "from ngsolve_webgpu import *\n", "from webgpu.jupyter import Draw\n", "\n", "geo = OCCGeometry(Cylinder((0, 0, 0), X, r=0.3, h=0.5))\n", "mesh = Mesh(geo.GenerateMesh(maxh=6))\n", "mesh.Curve(5)\n", "\n", "mesh_data = MeshData(mesh)\n", "function_data = FunctionData(mesh_data, sin(20 * x), order=5)\n", "cfrenderer = CFRenderer(function_data)\n", "cfrenderer.colormap.set_min_max(-1, 1)\n", "Draw([cfrenderer, Colorbar(cfrenderer.colormap)])" ] }, { "cell_type": "markdown", "id": "a4", "metadata": {}, "source": [ "## Multi-Component Functions\n", "\n", "For vector-valued CFs on 3D meshes, use `ClippingCF` to visualize a cross-section. Connect `CFRenderer` and `ClippingCF` via a shared colormap and clipping plane. The component selector lets you switch between vector components." ] }, { "cell_type": "code", "execution_count": null, "id": "a5", "metadata": {}, "outputs": [], "source": [ "from ngsolve import *\n", "from ngsolve_webgpu import *\n", "from webgpu.clipping import Clipping\n", "from webgpu.jupyter import Draw\n", "\n", "mesh = Mesh(unit_cube.GenerateMesh(maxh=0.1))\n", "cf = CF((sin(10 * z) * cos(15 * x), (x-0.5)**2 + (y-0.5)**2 + (z-0.5)**2 - 2))\n", "\n", "mesh_data = MeshData(mesh)\n", "function_data = FunctionData(mesh_data, cf, order=5)\n", "\n", "clipping = Clipping()\n", "clipping.mode = clipping.Mode.PLANE\n", "clipping.center = [0.7, 0.5, 0.5]\n", "clipping.normal = [1, -1, 1]\n", "\n", "colormap = Colormap()\n", "clip = ClippingCF(function_data, clipping=clipping, colormap=colormap)\n", "cfr = CFRenderer(function_data, colormap=colormap, clipping=clipping)\n", "\n", "Draw([cfr, clip, Colorbar(colormap)])\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0e970c9a-5b0a-4618-8289-996205f78f40", "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }