{ "cells": [ { "cell_type": "markdown", "id": "6f40a60b", "metadata": {}, "source": [ "# SITypes: Unit Aware Calculations\n", "\n", "## Ideal Gas Law Example\n", "\n", "Calculate the pressure exerted by 0.078 moles of hydrogen gas in a 42.0 mL container at 25.0 °C.\n", "\n", "This problem requires the ideal gas equation: **pV = nRT**\n", "\n", "### Conventional Approach\n", "The traditional method involves multiple steps:\n", "- n = 0.078 mol \n", "- T = 273.15 K + 25.0 K = 298.15 K\n", "- V = 42.0 mL\n", "- R = 8.314510 J/(K·mol)\n", "\n", "Numerical calculation: p = nRT/V = `0.078 × 8.314510 × 298.15 ÷ 42.0 = 4.604...`\n", "\n", "However, an additional dimensional analysis needs to be performed:\n", "```\n", "mol × J/(K·mol) × K ÷ mL = J/mL\n", "```\n", "And then this result requires additional conversion to obtain pressure in atmospheres.\n", "\n", "### SITypes Approach\n", "**SITypes enables direct calculation with automatic unit management**" ] }, { "cell_type": "markdown", "id": "1535bd3b", "metadata": {}, "source": [ "First, import SITypes and demonstrate the calculation:" ] }, { "cell_type": "code", "execution_count": null, "id": "19a1329a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Pressure in atmospheres = 45.4357 atm\n" ] } ], "source": [ "# Import SITypes\n", "from rmnpy.wrappers.sitypes import Scalar\n", "\n", "# Complete calculation with automatic unit conversion\n", "pressure = Scalar(\"0.078 mol * R * (273.15 + 25.0) K / 42.0 mL\")\n", "\n", "# Convert to atmospheres as requested\n", "pressure_atm = pressure.to(\"atm\")\n", "print(f\"Pressure in atmospheres = {pressure_atm}\")" ] }, { "cell_type": "markdown", "id": "75e483d6", "metadata": {}, "source": [ "The same calculation using discrete variables demonstrates SITypes' flexibility with Python arithmetic operations:" ] }, { "cell_type": "code", "execution_count": null, "id": "9d4b5cc6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Variables:\n", " n (amount) = 0.078 mol\n", " R (gas constant) = 1 R\n", " T (temperature) = 298.15 K\n", " V (volume) = 42 mL\n", "\n", "Calculation: p = nRT/V\n", "p = 4.60378e+06 mJ/L\n", "p = 45.4357 atm (in atmospheres)\n", "\n", "Comparison:\n", " String expression: 45.4357 atm\n", " Python variables: 45.4357 atm\n" ] } ], "source": [ "# Create separate Scalar objects for each variable\n", "n = Scalar(\"0.078 mol\") # amount of substance\n", "R = Scalar(\"R\") # gas constant (built-in physical constant)\n", "T = Scalar(\"(273.15 + 25.0) K\") # temperature \n", "V = Scalar(\"42.0 mL\") # volume\n", "\n", "print(f\"Variables:\")\n", "print(f\" n (amount) = {n}\")\n", "print(f\" R (gas constant) = {R}\")\n", "print(f\" T (temperature) = {T}\")\n", "print(f\" V (volume) = {V}\")\n", "\n", "# Calculate pressure using Python arithmetic: p = nRT/V\n", "p = n * R * T / V\n", "print(f\"\\nCalculation: p = nRT/V\")\n", "print(f\"p = {p}\")\n", "print(f\"p = {p.to('atm')} (in atmospheres)\")\n", "\n", "print(f\"\\nComparison:\")\n", "print(f\" String expression: {pressure_atm}\")\n", "print(f\" Python variables: {p.to('atm')}\")" ] }, { "cell_type": "markdown", "id": "31c8dc56", "metadata": {}, "source": [ "**SITypes includes fundamental physical constants in its database.** \n", "\n", "No manual entry is required for values like R = 8.314510 J/(K·mol). These constants can be used directly in expressions by their standard symbols." ] }, { "cell_type": "code", "execution_count": 3, "id": "3819527b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Selected Physical Constants:\n", " R = 1 R (Gas constant)\n", " c_0 = 1 c_0 (Speed of light)\n", " h_P = 1 h_P (Planck constant)\n", " k_B = 1 k_B (Boltzmann constant)\n", " N_A = 1 N_A (Avogadro constant)\n", " q_e = 1 q_e (Elementary charge)\n", " m_e = 1 m_e (Electron mass)\n", " m_p = 1 m_p (Proton mass)\n", " G = 1 G (Newton gravitational constant)\n", " π = 1 π (Pi)\n" ] } ], "source": [ "# Built-in Physical Constants Database\n", "working_constants = [\n", " (\"R\", \"Gas constant\"),\n", " (\"c_0\", \"Speed of light\"), \n", " (\"h_P\", \"Planck constant\"),\n", " (\"k_B\", \"Boltzmann constant\"),\n", " (\"N_A\", \"Avogadro constant\"),\n", " (\"q_e\", \"Elementary charge\"),\n", " (\"m_e\", \"Electron mass\"),\n", " (\"m_p\", \"Proton mass\"),\n", " (\"G\", \"Newton gravitational constant\"),\n", " (\"π\", \"Pi\"),\n", " (\"e\", \"Euler constant\")\n", "]\n", "\n", "print(\"Selected Physical Constants:\")\n", "for symbol, description in working_constants:\n", " try:\n", " constant = Scalar(symbol)\n", " print(f\" {symbol:8} = {constant} ({description})\")\n", " except:\n", " pass" ] }, { "cell_type": "markdown", "id": "e48ef7b8", "metadata": {}, "source": [ "The example below uses the relationship **c = λν** to relate wavelength and frequency, followed by photon energy calculation using **E = hν**." ] }, { "cell_type": "code", "execution_count": null, "id": "2f89906b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "He-Ne laser wavelength: 632.8 nm\n", "Frequency: ν = c/λ = 4.73755e+14 (1/s)\n", "Frequency in THz: 473.755 THz\n", "\n", "Photon energy: E = hν = 4.73755e+14 h_P•(1/s)\n", "Energy in eV: 1.9593 eV\n", "\n", "UV comparison (λ = 280 nm):\n", "UV photon energy: 4.42801 eV\n", "Energy ratio (UV/visible): 2.3\n" ] } ], "source": [ "# Calculate frequency from wavelength using c = λν\n", "laser_wavelength = Scalar(\"632.8 nm\") # He-Ne laser\n", "frequency = Scalar(\"c_0\") / laser_wavelength\n", "print(f\"He-Ne laser wavelength: {laser_wavelength}\")\n", "print(f\"Frequency: ν = c/λ = {frequency}\")\n", "print(f\"Frequency in THz: {frequency.to('THz')}\")\n", "\n", "# Calculate photon energy\n", "photon_energy = Scalar(\"h_P\") * frequency\n", "print(f\"\\nPhoton energy: E = hν = {photon_energy}\")\n", "print(f\"Energy in eV: {photon_energy.to('eV')}\")\n", "\n", "# Compare different regions of electromagnetic spectrum\n", "uv_wavelength = Scalar(\"280 nm\")\n", "uv_energy = Scalar(\"h_P * c_0\") / uv_wavelength\n", "print(f\"\\nUV comparison (λ = {uv_wavelength}):\")\n", "print(f\"UV photon energy: {uv_energy.to('eV')}\")\n", "print(f\"Energy ratio (UV/visible): {(uv_energy/photon_energy).value:.1f}\")" ] }, { "cell_type": "markdown", "id": "65565612", "metadata": {}, "source": [ "**SITypes supports mathematical functions within unit-aware expressions**" ] }, { "cell_type": "code", "execution_count": 5, "id": "c839c031", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AC voltage at 45°: 84.8528 V\n", "RMS voltage: 169.706 V\n" ] } ], "source": [ "# Trigonometric functions\n", "ac_voltage = Scalar(\"sin(45°) * 120 V\")\n", "print(f\"AC voltage at 45°: {ac_voltage}\")\n", "\n", "# Square root preserves units correctly\n", "rms_voltage = Scalar(\"sqrt(2) * 120 V\")\n", "print(f\"RMS voltage: {rms_voltage}\")" ] }, { "cell_type": "code", "execution_count": 6, "id": "ddeb80d1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exponential decay after 2s (half-life 5s): 75.7858\n", "Circle area (radius 2.5m): 6.25 π•m^2\n" ] } ], "source": [ "# Exponential and logarithmic functions\n", "decay = Scalar(\"100 * exp(-ln(2) * 2 s / 5 s)\")\n", "print(f\"Exponential decay after 2s (half-life 5s): {decay}\")\n", "\n", "# Circle area using π\n", "circle_area = Scalar(\"π * (2.5 m)^2\")\n", "print(f\"Circle area (radius 2.5m): {circle_area}\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "86a81e45", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LC resonance frequency: 5032.92 m^2•s•A/(m^2•s^2•A)\n", "AC power with 30° phase: 519.615 m^2•kg•A/(s^3•A)\n" ] } ], "source": [ "# Complex engineering calculations\n", "resonance_freq = Scalar(\"1 / (2 * π * sqrt(1e-3 H * 1e-6 F))\")\n", "print(f\"LC resonance frequency: {resonance_freq}\")\n", "\n", "ac_power = Scalar(\"120 V * 5 A * cos(30°)\")\n", "print(f\"AC power with 30° phase: {ac_power}\")" ] }, { "cell_type": "markdown", "id": "b554d5a6", "metadata": {}, "source": [ "**Calculation of electron rest mass energy using Einstein's relation:**\n", "\n", "$$E = m_e c_0^2$$\n", "\n", "where $m_e$ is the electron rest mass and $c_0$ is the speed of light in vacuum. SITypes includes these fundamental constants in its database." ] }, { "cell_type": "code", "execution_count": null, "id": "e96ddc38", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "E = m_e × c₀² = 1 m_e•c_0^2\n", "E = 510999 eV\n" ] } ], "source": [ "# Einstein's mass-energy equivalence: E = mc²\n", "electron_energy = Scalar(\"m_e * c_0^2\")\n", "print(f\"E = m_e × c₀² = {electron_energy}\")\n", "\n", "# Convert to common energy unit\n", "electron_eV = electron_energy.to('eV')\n", "print(f\"E = {electron_eV}\")" ] }, { "cell_type": "markdown", "id": "6af87aa7", "metadata": {}, "source": [ "**Parallel plate capacitor with dielectric medium:**\n", "\n", "Given: plate area = 4 cm², separation = 0.15 mm, dielectric constant k = 3.0.\n", "\n", "$$C = \\frac{k \\epsilon_0 A}{d} (n-1)$$\n", "\n", "where $k$ is the dielectric constant, $\\epsilon_0$ is the electric constant (permittivity of free space), $A$ is the plate area, $d$ is the separation distance, and $n$ is the number of plates." ] }, { "cell_type": "code", "execution_count": null, "id": "db12bea1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C = 7.08335e-16 s^4•A^2/(dm^2•g)\n", "C = 70.8335 pF (picofarads)\n" ] } ], "source": [ "# Parallel plate capacitor calculation\n", "k = Scalar(\"3.0\") # dielectric constant\n", "area = Scalar(\"4 cm^2\") # plate area\n", "separation = Scalar(\"0.15 mm\") # plate separation\n", "n_plates = Scalar(\"2\") # number of plates\n", "\n", "capacitor_C = k * Scalar(\"ε_0\") * area / separation * (n_plates - 1)\n", "print(f\"C = {capacitor_C}\")\n", "print(f\"C = {capacitor_C.to('pF')} (picofarads)\")" ] }, { "cell_type": "markdown", "id": "03549425", "metadata": {}, "source": [ "**SITypes includes databases of atomic weights and molecular properties for chemical calculations**" ] }, { "cell_type": "code", "execution_count": 10, "id": "fb484c40", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Atomic weights from built-in database:\n", "Carbon: 12.0107 g/mol\n", "Hydrogen: 1.00794 g/mol\n", "Oxygen: 15.9994 g/mol\n", "Iron: 55.845 g/mol\n", "Gold: 196.967 g/mol\n" ] } ], "source": [ "# Built-in atomic weights\n", "carbon_weight = Scalar(\"aw[C]\")\n", "hydrogen_weight = Scalar(\"aw[H]\")\n", "oxygen_weight = Scalar(\"aw[O]\")\n", "fe_weight = Scalar(\"aw[Fe]\")\n", "au_weight = Scalar(\"aw[Au]\")\n", "\n", "print(\"Atomic weights from built-in database:\")\n", "print(f\"Carbon: {carbon_weight}\")\n", "print(f\"Hydrogen: {hydrogen_weight}\")\n", "print(f\"Oxygen: {oxygen_weight}\")\n", "print(f\"Iron: {fe_weight}\")\n", "print(f\"Gold: {au_weight}\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "aeb440e0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Water molecular weight: 18.0153 g/mol\n", "Methane molecular weight: 16.0425 g/mol\n", "Water MW (calculated): 18.0153 g/mol\n", "Verification: 18.015280 ≈ 18.015280\n" ] } ], "source": [ "# Formula weights and molecular weight calculations\n", "water_mw = Scalar(\"fw[H2O]\")\n", "methane_mw = Scalar(\"fw[CH4]\")\n", "print(f\"Water molecular weight: {water_mw}\")\n", "print(f\"Methane molecular weight: {methane_mw}\")\n", "\n", "# Calculate molecular weight manually and compare\n", "water_calc = Scalar(\"2 * aw[H] + aw[O]\")\n", "print(f\"Water MW (calculated): {water_calc}\")\n", "print(f\"Verification: {water_calc.value:.6f} ≈ {water_mw.value:.6f}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "5e5765bd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Moles in 36 g of water: 1.9983 mol\n", "Molarity: 7.99321 mol/L\n" ] } ], "source": [ "# Moles and concentration calculations\n", "sample_mass = Scalar(\"36 g\")\n", "moles_water = sample_mass / water_mw\n", "print(f\"Moles in {sample_mass} of water: {moles_water}\")\n", "\n", "# Concentration calculations\n", "volume_solution = Scalar(\"250 mL\")\n", "molarity = moles_water / volume_solution.to(\"L\")\n", "print(f\"Molarity: {molarity.to('mol/L')}\")" ] }, { "cell_type": "markdown", "id": "52b21cb2", "metadata": {}, "source": [ "**SITypes provides native support for complex numbers with units**" ] }, { "cell_type": "code", "execution_count": 13, "id": "ddfe4741", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Complex voltage: (120) V\n", "Magnitude: 150 V\n", "Resistance: 50 Ω\n", "Reactance: 30 Ω\n" ] } ], "source": [ "# Complex numbers with units for AC circuit analysis\n", "complex_voltage = Scalar(\"(120+90*I) V\")\n", "print(f\"Complex voltage: {complex_voltage}\")\n", "print(f\"Magnitude: {complex_voltage.magnitude}\")\n", "\n", "# Basic impedance calculation\n", "resistance = Scalar(\"50 Ω\")\n", "reactance = Scalar(\"30 Ω\")\n", "print(f\"Resistance: {resistance}\")\n", "print(f\"Reactance: {reactance}\")" ] }, { "cell_type": "markdown", "id": "d6542a7e", "metadata": {}, "source": [ "**SITypes enforces dimensional consistency and prevents common calculation errors**" ] }, { "cell_type": "code", "execution_count": null, "id": "9aec2a31", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Length addition: 5 m + 0.9144 m = 5.9144 m\n", "Force × Distance = Energy: 10 N × 2 m = 20 J\n", "✓ Prevents length + time: RMNError\n", "✓ Prevents invalid conversion: ValueError\n" ] } ], "source": [ "# Compatible operations\n", "length1 = Scalar(\"5 m\")\n", "length2 = Scalar(\"3 ft\")\n", "total_length = length1 + length2.to(\"m\")\n", "print(f\"Length addition: {length1} + {length2.to('m')} = {total_length}\")\n", "print(f\"Force × Distance = Energy: {Scalar('10 N')} × {Scalar('2 m')} = {Scalar('10 N') * Scalar('2 m')}\")\n", "\n", "# Error prevention\n", "try:\n", " invalid_sum = Scalar(\"5 m\") + Scalar(\"3 s\") # Can't add length and time\n", "except Exception as e:\n", " print(f\"✓ Prevents length + time: {type(e).__name__}\")\n", "\n", "try:\n", " invalid_conversion = Scalar(\"10 m\").to(\"s\") # Can't convert length to time\n", "except Exception as e:\n", " print(f\"✓ Prevents invalid conversion: {type(e).__name__}\")" ] }, { "cell_type": "markdown", "id": "f3d119b0", "metadata": {}, "source": [ "## Summary\n", "\n", "This notebook has demonstrated the core capabilities of SITypes for scientific computing:\n", "\n", "### Key Features:\n", "- **Automatic unit tracking** - Eliminates unit-related calculation errors\n", "- **Seamless unit conversions** - Supports conversion between compatible unit systems\n", "- **Dimensional consistency** - Enforces proper dimensional analysis\n", "- **Mathematical function library** - Complete mathematical operations with unit preservation\n", "- **Multi-domain applications** - Physics, chemistry, and engineering examples\n", "- **Complex number support** - Advanced calculations for AC analysis and other applications\n", "\n", "### Implementation Benefits:\n", "The examples in this notebook demonstrate actual computational results with proper units automatically derived and verified. This approach reduces the potential for dimensional errors while maintaining computational efficiency.\n", "\n", "### Further Development:\n", "- Extend calculations using the demonstrated principles\n", "- Implement domain-specific algorithms leveraging SITypes\n", "- Explore the complete mathematical function library\n", "- Integrate SITypes into larger scientific computing workflows\n", "\n", "SITypes provides a robust foundation for unit-aware scientific computation." ] } ], "metadata": { "kernelspec": { "display_name": "rmnpy", "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.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }