|
| 1 | +# Dimensional analysis |
| 2 | + |
| 3 | +`MathS.Dimensions.Of` reads a formula against the physical dimension of each of its variables and |
| 4 | +says whether it means anything: the dimension of the result, or the subexpression where two |
| 5 | +dimensions are added, compared or passed to a function that only takes a pure number. |
| 6 | +`MathS.Dimensions.DimensionlessGroups` gives the Buckingham π groups of a set of variables. |
| 7 | + |
| 8 | +A dimension is a product of powers of the seven SI base dimensions, `L`, `M`, `T`, `I`, `Θ`, `N` |
| 9 | +and `J`, with rational exponents, and is written as `Dimension.Length / Dimension.Time.Pow(2)` or |
| 10 | +`Dimension.Create(1, 0, -2)`. It says what kind of quantity something is and nothing about its |
| 11 | +unit: checking values in metres and feet is what a units library is for. What a units library |
| 12 | +cannot do is look at `sqrt(2 g h)` and say whether it is a speed. |
| 13 | + |
| 14 | +## Checking a formula |
| 15 | + |
| 16 | +The dimensions are passed in a map from each variable to its dimension, not attached to the |
| 17 | +variables, so the same expression can be read under different assignments and nothing about how |
| 18 | +it prints, parses or simplifies changes. A body falling from a height `h`: |
| 19 | + |
| 20 | +```cs |
| 21 | +using System.Collections.Generic; |
| 22 | +using AngouriMath.Core; |
| 23 | + |
| 24 | +var dims = new Dictionary<Variable, Dimension> |
| 25 | +{ |
| 26 | + ["g"] = Dimension.Length / Dimension.Time.Pow(2), |
| 27 | + ["h"] = Dimension.Length, |
| 28 | + ["t"] = Dimension.Time, |
| 29 | +}; |
| 30 | +Console.WriteLine(MathS.Dimensions.Of("sqrt(2 g h)", dims)); |
| 31 | +Console.WriteLine(MathS.Dimensions.Of("h - g t^2 / 2 = 0", dims)); |
| 32 | +Console.WriteLine(MathS.Dimensions.Of("h + g t", dims)); |
| 33 | +``` |
| 34 | + |
| 35 | +Output: |
| 36 | + |
| 37 | +``` |
| 38 | +Consistent: L T^-1 |
| 39 | +Consistent: 1 |
| 40 | +Inconsistent at h + g * t: h has dimension L and g * t has dimension L T^-1, so they cannot be added |
| 41 | +``` |
| 42 | + |
| 43 | +The answer is a `DimensionCheck`: its `Status` is one of three, its `Dimension` is the dimension of |
| 44 | +the result where it is `Consistent`, and `At` and `Reason` say where and why it is not. A statement |
| 45 | +such as an equation is a truth value, a pure number written `1`, once the sides of every |
| 46 | +comparison in it agree. |
| 47 | + |
| 48 | +**`Undetermined` is not a verdict.** A variable with no declared dimension, a dimensional quantity |
| 49 | +raised to a symbolic power, and a node the check does not read are each reported as undetermined, |
| 50 | +with the subexpression, rather than guessed at: |
| 51 | + |
| 52 | +```cs |
| 53 | +using System.Collections.Generic; |
| 54 | +using AngouriMath.Core; |
| 55 | + |
| 56 | +var dims = new Dictionary<Variable, Dimension> |
| 57 | +{ |
| 58 | + ["h"] = Dimension.Length, |
| 59 | + ["n"] = Dimension.Dimensionless, |
| 60 | +}; |
| 61 | +Console.WriteLine(MathS.Dimensions.Of("h + w", dims)); |
| 62 | +Console.WriteLine(MathS.Dimensions.Of("h^n", dims)); |
| 63 | +``` |
| 64 | + |
| 65 | +Output: |
| 66 | + |
| 67 | +``` |
| 68 | +Undetermined at w: no dimension is declared for w |
| 69 | +Undetermined at h ^ n: h has dimension L and is raised to n, whose value is not known |
| 70 | +``` |
| 71 | + |
| 72 | +## The rules |
| 73 | + |
| 74 | +**Nothing is simplified first.** The check is about the formula as written: `sin(x) - sin(x)` with |
| 75 | +`x` a length is inconsistent, although it simplifies to `0`. |
| 76 | + |
| 77 | +| | | |
| 78 | +|---|---| |
| 79 | +| `a + b`, `a - b`, `a mod b`, `max(a, b)`, `min(a, b)`, `a = b`, `a < b` | `a` and `b` have one dimension. A literal `0` or `oo` fits any, so `v = 0` is meaningful | |
| 80 | +| `a * b`, `a / b`, `dot(a, b)`, `cross(a, b)` | the dimensions multiply or divide | |
| 81 | +| `a ^ p` | `p` is a pure number; where `a` has a dimension, `p` is rational: `L^pi` is no dimension | |
| 82 | +| `abs`, `conj`, `re`, `im`, `norm(v, p)`, `trace`, `transpose`, an entry of a matrix | the dimension of the argument | |
| 83 | +| `sign`, `arg` | a pure number, whatever the argument measures | |
| 84 | +| `sin` and the other trigonometric functions, `exp` and `ln`, the hyperbolic functions, `erf`, `Ei`, `Si`, `Ci`, `li`, `gamma` and `n!`, `beta`, `zeta`, `polygamma`, `lambertw`, `floor`, `ceil`, `round`, the functions of number theory | each argument is a pure number, and so is the value | |
| 85 | +| `and`, `or`, `not`, `iverson(P)`, divisibility and congruence | each operand is a truth value or a pure number, and so is the result | |
| 86 | +| a matrix or vector | its entries share a dimension, which is the matrix's; a state vector `[x, v]` of mixed dimensions is undetermined | |
| 87 | +| `det(A)` of an `n` by `n` matrix | the entries' dimension to the `n`th power, undetermined where `n` is not written | |
| 88 | +| `derivative(f, x, n)`, `partialderivative(f, x, n)` | `f`'s dimension over `x`'s to the `n`th power | |
| 89 | +| `integral(f, x, a, b)` | `f`'s dimension times `x`'s, with `a` and `b` of `x`'s dimension | |
| 90 | +| `limit(f, x, a)` | `f`'s dimension, with `a` of `x`'s | |
| 91 | +| `sum(f, k, a, b)`, `product(f, k, a, b)` | the index counts terms and is a pure number unless declared otherwise; a product of `b - a + 1` terms of dimension `D` is `D^(b - a + 1)`, undetermined where that count is not a number | |
| 92 | +| `sum(f, x in S)`, `product(f, x in S)`, `max(f, x in S)`, `forall`, `exists`, `x in S` | a variable ranging over `S` takes the dimension `S`'s members share: `x in [0; l]` makes `x` a length. A number set, `RR` or `ZZ`, fits a declared variable of any dimension, since `l in RR` says a length is real, and makes an undeclared one a pure number | |
| 93 | +| `f provided P`, a piecewise | `P` is a consistent statement, and the cases share a dimension | |
| 94 | +| a set on its own, `lambda`, a function applied | undetermined: none of these is a quantity | |
| 95 | + |
| 96 | +A node type the check has not been taught is undetermined too, never read as a pure number by |
| 97 | +default; a test in the suite lists the verdict on every node type there is and fails on a new one |
| 98 | +until someone decides what its dimension is. |
| 99 | + |
| 100 | +## Buckingham π groups |
| 101 | + |
| 102 | +A relation between `n` quantities whose dimension matrix has rank `r` can be written as a relation |
| 103 | +between `n - r` dimensionless products of them. `DimensionlessGroups` gives a basis of those |
| 104 | +products, each with the smallest whole powers, and checks each is a pure number before returning |
| 105 | +it. Flow in a pipe, with density, speed, diameter and viscosity, has one, the reciprocal of the |
| 106 | +Reynolds number: |
| 107 | + |
| 108 | +```cs |
| 109 | +using System.Collections.Generic; |
| 110 | +using AngouriMath.Core; |
| 111 | + |
| 112 | +var (L, M, T) = (Dimension.Length, Dimension.Mass, Dimension.Time); |
| 113 | +var pipe = new Dictionary<Variable, Dimension> |
| 114 | +{ |
| 115 | + ["rho"] = M / L.Pow(3), ["v"] = L / T, ["d"] = L, ["mu"] = M / (L * T), |
| 116 | +}; |
| 117 | +foreach (var group in MathS.Dimensions.DimensionlessGroups(new Variable[] { "rho", "v", "d", "mu" }, pipe)) |
| 118 | + Console.WriteLine(group); |
| 119 | +``` |
| 120 | + |
| 121 | +Output: |
| 122 | + |
| 123 | +``` |
| 124 | +mu / (rho * v * d) |
| 125 | +``` |
| 126 | + |
| 127 | +The order of the variables chooses the basis. The ones listed first recur across the groups, as a |
| 128 | +physicist's "repeating variables" do, and each later one appears to the first power in exactly |
| 129 | +one group. |
| 130 | + |
| 131 | +## A worked example: water waves |
| 132 | + |
| 133 | +The dispersion relation of waves on water of depth `h` is `ω² = g k tanh(k h)`, with `ω` the |
| 134 | +angular frequency and `k` the wave number. In deep water `tanh(k h)` is close to `1`, and in |
| 135 | +shallow water close to `k h`, which gives the shallow-water speed `ω / k = sqrt(g h)`. Writing |
| 136 | +`ω = sqrt(g k h)` for that, a factor of `k` short, is a typical slip, and the check finds it inside |
| 137 | +the phase of the wave it is used in: |
| 138 | + |
| 139 | +```cs |
| 140 | +using System.Collections.Generic; |
| 141 | +using AngouriMath.Core; |
| 142 | + |
| 143 | +var (L, T) = (Dimension.Length, Dimension.Time); |
| 144 | +var dims = new Dictionary<Variable, Dimension> |
| 145 | +{ |
| 146 | + ["omega"] = Dimension.Dimensionless / T, ["g"] = L / T.Pow(2), ["k"] = Dimension.Dimensionless / L, |
| 147 | + ["h"] = L, ["A"] = L, ["x"] = L, ["t"] = T, |
| 148 | +}; |
| 149 | +Console.WriteLine(MathS.Dimensions.Of("omega^2 = g k tanh(k h)", dims)); |
| 150 | +Console.WriteLine(MathS.Dimensions.Of("A sin(k x - omega t)", dims)); |
| 151 | +Console.WriteLine(MathS.Dimensions.Of("A sin(k x - sqrt(g k h) t)", dims)); |
| 152 | +foreach (var group in MathS.Dimensions.DimensionlessGroups(new Variable[] { "k", "g", "A", "h", "t" }, dims)) |
| 153 | + Console.WriteLine(group); |
| 154 | +``` |
| 155 | + |
| 156 | +Output: |
| 157 | + |
| 158 | +``` |
| 159 | +Consistent: 1 |
| 160 | +Consistent: L |
| 161 | +Inconsistent at k * x - sqrt(g * k * h) * t: k * x has dimension 1 and sqrt(g * k * h) * t has dimension L^(1/2), so they cannot be subtracted |
| 162 | +k * A |
| 163 | +k * h |
| 164 | +k * g * t ^ 2 |
| 165 | +``` |
| 166 | + |
| 167 | +The three groups say that the surface of a wave started from rest depends on the amplitude, the |
| 168 | +depth and the time only through `k A`, the steepness, `k h`, the depth in wavelengths, and |
| 169 | +`g k t²`, the time in units of the deep-water period. A simulation tuned over those three is tuned |
| 170 | +over every wave with the same three values. |
0 commit comments