|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Unit-Aware Parameters with `Param()`\n", |
| 8 | + "\n", |
| 9 | + "Demonstrates `Param()`, `to_example()`, `with_units()`, and automatic pint Quantity stripping using a single comprehensive model." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "from meshly import Packable, Param, InlineArray, Array\n", |
| 19 | + "from pint import UnitRegistry\n", |
| 20 | + "import numpy as np\n", |
| 21 | + "\n", |
| 22 | + "ureg = UnitRegistry()\n", |
| 23 | + "\n", |
| 24 | + "class Simulation(Packable):\n", |
| 25 | + " \"\"\"Covers all field types: float, InlineArray, Array, nested Packable.\"\"\"\n", |
| 26 | + " velocity: InlineArray = Param(units=\"m/s\", example=[30.0, 0, 0], shape=(3,), description=\"Inlet velocity\")\n", |
| 27 | + " temperature: float = Param(300.0, units=\"K\", description=\"Fluid temperature\")\n", |
| 28 | + " pressure: float = Param(101325.0, units=\"Pa\", description=\"Static pressure\")\n", |
| 29 | + " displacements: Array = Param(units=\"m\", example=np.zeros((4, 3), dtype=np.float32), shape=(None, 3), description=\"Nodal displacements\")\n", |
| 30 | + " label: str = Param(\"default\", units=\"dimensionless\", description=\"Run label\")" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": 2, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [ |
| 38 | + { |
| 39 | + "name": "stdout", |
| 40 | + "output_type": "stream", |
| 41 | + "text": [ |
| 42 | + "velocity: units=m/s\n", |
| 43 | + "temperature: units=K\n", |
| 44 | + "pressure: units=Pa\n", |
| 45 | + "displacements: units=m\n" |
| 46 | + ] |
| 47 | + } |
| 48 | + ], |
| 49 | + "source": [ |
| 50 | + "import json\n", |
| 51 | + "\n", |
| 52 | + "# Units appear in the JSON schema (this is what MCP tools / UIs see)\n", |
| 53 | + "schema = Simulation.model_json_schema()\n", |
| 54 | + "for name in [\"velocity\", \"temperature\", \"pressure\", \"displacements\"]:\n", |
| 55 | + " props = schema[\"properties\"][name]\n", |
| 56 | + " print(f\"{name}: units={props.get('units')}\")" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 3, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [ |
| 64 | + { |
| 65 | + "name": "stdout", |
| 66 | + "output_type": "stream", |
| 67 | + "text": [ |
| 68 | + "velocity: [30. 0. 0.]\n", |
| 69 | + "temperature: 300.0\n", |
| 70 | + "displacements: (4, 3)\n" |
| 71 | + ] |
| 72 | + } |
| 73 | + ], |
| 74 | + "source": [ |
| 75 | + "# to_example() builds an instance from Param example/default values\n", |
| 76 | + "sim = Simulation.to_example()\n", |
| 77 | + "print(f\"velocity: {sim.velocity}\")\n", |
| 78 | + "print(f\"temperature: {sim.temperature}\")\n", |
| 79 | + "print(f\"displacements: {sim.displacements.shape}\")" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": 4, |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [ |
| 87 | + { |
| 88 | + "name": "stdout", |
| 89 | + "output_type": "stream", |
| 90 | + "text": [ |
| 91 | + "velocity: [30.0 0.0 0.0] meter / second\n", |
| 92 | + "to km/h: [108.0 0.0 0.0] kilometer / hour\n", |
| 93 | + "temperature: 300.0 kelvin\n", |
| 94 | + "to Celsius: 26.850000000000023 degree_Celsius\n", |
| 95 | + "pressure: 1.0 standard_atmosphere\n", |
| 96 | + "displace mm: [[0.0 0.0 0.0] [0.0 0.0 0.0] [0.0 0.0 0.0] [0.0 0.0 0.0]] millimeter\n" |
| 97 | + ] |
| 98 | + } |
| 99 | + ], |
| 100 | + "source": [ |
| 101 | + "# with_units() converts fields to pint Quantities\n", |
| 102 | + "su = sim.with_units()\n", |
| 103 | + "print(f\"velocity: {su.velocity}\")\n", |
| 104 | + "print(f\"to km/h: {su.velocity.to('km/h')}\")\n", |
| 105 | + "print(f\"temperature: {su.temperature}\")\n", |
| 106 | + "print(f\"to Celsius: {su.temperature.to('degC')}\")\n", |
| 107 | + "print(f\"pressure: {su.pressure.to('atm')}\")\n", |
| 108 | + "print(f\"displace mm: {su.displacements.to('mm')}\")" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": 5, |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [ |
| 116 | + { |
| 117 | + "name": "stdout", |
| 118 | + "output_type": "stream", |
| 119 | + "text": [ |
| 120 | + "velocity (m/s): [30. 0. 0.] (ndarray)\n", |
| 121 | + "temperature (K): 300.0 (float)\n", |
| 122 | + "pressure (Pa): 101325.0 (float)\n", |
| 123 | + "displacements (m): [[0.001 0. 0. ]] (ndarray)\n" |
| 124 | + ] |
| 125 | + } |
| 126 | + ], |
| 127 | + "source": [ |
| 128 | + "# Pass pint Quantities directly — auto-converted to declared units, stored as plain values\n", |
| 129 | + "sim2 = Simulation(\n", |
| 130 | + " velocity=ureg.Quantity([108.0, 0, 0], \"km/h\"),\n", |
| 131 | + " temperature=ureg.Quantity(26.85, \"degC\"),\n", |
| 132 | + " pressure=ureg.Quantity(1.0, \"atm\"),\n", |
| 133 | + " displacements=ureg.Quantity(np.array([[1, 0, 0]], dtype=np.float32), \"mm\"),\n", |
| 134 | + ")\n", |
| 135 | + "print(f\"velocity (m/s): {sim2.velocity} ({type(sim2.velocity).__name__})\")\n", |
| 136 | + "print(f\"temperature (K): {sim2.temperature} ({type(sim2.temperature).__name__})\")\n", |
| 137 | + "print(f\"pressure (Pa): {sim2.pressure} ({type(sim2.pressure).__name__})\")\n", |
| 138 | + "print(f\"displacements (m): {sim2.displacements} ({type(sim2.displacements).__name__})\")" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 6, |
| 144 | + "metadata": {}, |
| 145 | + "outputs": [ |
| 146 | + { |
| 147 | + "name": "stdout", |
| 148 | + "output_type": "stream", |
| 149 | + "text": [ |
| 150 | + "Expected error: Field 'value' on NoUnits received a pint Quantity with units 'meter / second', but no Param(units=...) is defined for this field. Either add Param(units=...) or pass a plain value.\n", |
| 151 | + "dimensionless OK: 42.0\n" |
| 152 | + ] |
| 153 | + } |
| 154 | + ], |
| 155 | + "source": [ |
| 156 | + "# Error: passing units to a field without Param raises TypeError\n", |
| 157 | + "class NoUnits(Packable):\n", |
| 158 | + " value: float = 0.0\n", |
| 159 | + "\n", |
| 160 | + "try:\n", |
| 161 | + " NoUnits(value=ureg.Quantity(42.0, \"m/s\"))\n", |
| 162 | + "except TypeError as e:\n", |
| 163 | + " print(f\"Expected error: {e}\")\n", |
| 164 | + "\n", |
| 165 | + "# Dimensionless quantities on non-Param fields are fine\n", |
| 166 | + "m = NoUnits(value=ureg.Quantity(42.0, \"dimensionless\"))\n", |
| 167 | + "print(f\"dimensionless OK: {m.value}\")" |
| 168 | + ] |
| 169 | + } |
| 170 | + ], |
| 171 | + "metadata": { |
| 172 | + "kernelspec": { |
| 173 | + "display_name": "Python 3", |
| 174 | + "language": "python", |
| 175 | + "name": "python3" |
| 176 | + }, |
| 177 | + "language_info": { |
| 178 | + "name": "python", |
| 179 | + "version": "3.12.2" |
| 180 | + } |
| 181 | + }, |
| 182 | + "nbformat": 4, |
| 183 | + "nbformat_minor": 4 |
| 184 | +} |
0 commit comments