Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/yang/blob/main/src/ufbi_w2_dyadic.om # yang This project has not described itself yet. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `yang / src / ufbi_w2_dyadic.om` 240 lines ยท 5895 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note W2 quadrature for 0 <= beta <= 0.288. Values in this file are signed integer 3 note multiples of 2^-28 unless explicitly named as totals, counts or basis points. 4 note Each input field box uses rational beta and exact integer angle-cell indices. 5 note No f64 operations enter a bound or a subunit decision. 6 7 fn wScale to long 8 ret long `268435456` 9 end 10 11 fn floorRatio n long d long to long 12 if lt n 0 13 ret neg div add neg n sub d 1 d 14 end 15 ret div n d 16 end 17 18 fn ceilRatio n long d long to long 19 ret neg floorRatio neg n d 20 end 21 22 fn lowerProduct x long y long to long 23 ret floorRatio mul x y wScale 24 end 25 26 fn upperProduct x long y long to long 27 ret ceilRatio mul x y wScale 28 end 29 30 fn smaller x long y long to long 31 if lt x y 32 ret x 33 end 34 ret y 35 end 36 37 fn larger x long y long to long 38 if gt x y 39 ret x 40 end 41 ret y 42 end 43 44 note exp(x) >= (1 + x/4096)^4096. Floor at each positive squaring. 45 note For |x| <= 1.75 the largest exact squaring product is < 6 * 2^56. 46 fn expLower x long to long 47 v = add wScale floorRatio x 4096 48 for k ulong 12 49 v = lowerProduct v v 50 end 51 ret v 52 end 53 54 fn expUpper x long to long 55 ret ceilRatio mul wScale wScale expLower neg x 56 end 57 58 global wCosLo long 2048 59 global wCosHi long 2048 60 global wSinLo long 2048 61 global wSinHi long 2048 62 global wDensityLo long 2048 63 global wDensityHi long 2048 64 note moments: sum density lower/upper, sum cosine moment lower/upper, sum transverse cost upper. 65 global wMoments long 5 66 global wMean long 2 67 global wFlux long 2 68 global wBoxEnergy long 2 69 global wMaxEnergy long 2 70 global wFailed long 1 71 72 fn initW2Angles to void 73 for i ulong 2048 74 put wCosLo i floorRatio get gridLo add i 1 4 75 put wCosHi i ceilRatio get gridHi i 4 76 put wSinLo i long shr gridSinSqLoUnits i 22 77 put wSinHi i long shr add gridSinSqHiUnits i `4194303` 22 78 end 79 end 80 81 note Multiply a signed interval by a nonnegative interval. 82 fn weightedLo x long lo long hi long to long 83 if lt x 0 84 ret lowerProduct x hi 85 end 86 ret lowerProduct x lo 87 end 88 89 fn weightedHi x long lo long hi long to long 90 if lt x 0 91 ret upperProduct x lo 92 end 93 ret upperProduct x hi 94 end 95 96 fn setDensity i ulong rLo long rHi long to void 97 xLo = weightedLo get wCosLo i rLo rHi 98 xHi = weightedHi get wCosHi i rLo rHi 99 wLo = lowerProduct get wSinLo i expLower xLo 100 wHi = upperProduct get wSinHi i expUpper xHi 101 put wDensityLo i wLo 102 put wDensityHi i wHi 103 put wMoments 0 add get wMoments 0 wLo 104 put wMoments 1 add get wMoments 1 wHi 105 put wMoments 2 add get wMoments 2 weightedLo get wCosLo i wLo wHi 106 put wMoments 3 add get wMoments 3 weightedHi get wCosHi i wLo wHi 107 end 108 109 fn transverseDrift i ulong rLo long rHi long to long 110 sLo = get wSinLo i 111 sHi = get wSinHi i 112 rxLo = weightedLo get wCosLo i rLo rHi 113 rxHi = weightedHi get wCosHi i rLo rHi 114 dLo = floorRatio sub mul 2 wScale weightedHi rxHi sLo sHi 6 115 dHi = ceilRatio sub mul 2 wScale weightedLo rxLo sLo sHi 6 116 ret larger neg dLo dHi 117 end 118 119 fn setTransverse i ulong rLo long rHi long to void 120 d = transverseDrift i rLo rHi 121 c = larger neg get wCosLo i get wCosHi i 122 cost = add ceilRatio upperProduct c c 27 ceilRatio mul 2 upperProduct d d 3 123 put wMoments 4 add get wMoments 4 upperProduct cost get wDensityHi i 124 end 125 126 fn meanLo num long zLo long zHi long to long 127 if lt num 0 128 ret floorRatio mul num wScale zLo 129 end 130 ret floorRatio mul num wScale zHi 131 end 132 133 fn meanHi num long zLo long zHi long to long 134 if lt num 0 135 ret ceilRatio mul num wScale zHi 136 end 137 ret ceilRatio mul num wScale zLo 138 end 139 140 note A partial-bin integral is between min(0,incrementLo) and max(0,incrementHi). 141 note Both flux sweeps start at a pole where the centered integral vanishes. 142 fn fluxCell i ulong backward long to long 143 cLo = sub get wCosLo i get wMean 1 144 cHi = sub get wCosHi i get wMean 0 145 if ne backward 0 146 tmp = neg cLo 147 cLo = neg cHi 148 cHi = tmp 149 end 150 incLo = weightedLo cLo get wDensityLo i get wDensityHi i 151 incHi = weightedHi cHi get wDensityLo i get wDensityHi i 152 lo = add get wFlux 0 smaller 0 incLo 153 hi = add get wFlux 1 larger 0 incHi 154 put wFlux 0 add get wFlux 0 incLo 155 put wFlux 1 add get wFlux 1 incHi 156 if or eq i 0 eq i 2047 157 note |cos-mean| <= 2. Moving inward from a pole, sin^2 increases and 158 note the exp factor varies by at most exp(1.75*pi/2048) < 2. 159 note Thus |flux|/density <= 4*pi/2048 throughout either end cell. 160 ret ceilRatio mul 16 get wDensityHi i `4194304` 161 end 162 den = get wDensityLo i 163 if le den 0 164 put wFailed 0 1 165 ret 0 166 end 167 a = ceilRatio larger neg lo hi 2048 168 ret ceilRatio mul a a den 169 end 170 171 fn fluxEnergy zLo long to long 172 total = long 0 173 for side ulong 2 174 put wFlux 0 0 175 put wFlux 1 0 176 for i ulong 1024 177 j = i 178 if eq side 1 179 j = sub 2047 i 180 end 181 total = add total fluxCell j long side 182 end 183 end 184 avg = ceilRatio total 2048 185 ratio = ceilRatio mul avg wScale zLo 186 note pi < 3.141593, hence pi^2 < 986961/100000. 187 ret ceilRatio mul ratio 986961 100000 188 end 189 190 fn w2FieldBox rLo long rHi long to void 191 for j ulong 5 192 put wMoments j 0 193 end 194 for i ulong 2048 195 setDensity i rLo rHi 196 setTransverse i rLo rHi 197 end 198 zLo = floorRatio get wMoments 0 2048 199 zHi = ceilRatio get wMoments 1 2048 200 if le zLo 0 201 put wFailed 0 1 202 ret 203 end 204 put wMean 0 meanLo floorRatio get wMoments 2 2048 zLo zHi 205 put wMean 1 meanHi ceilRatio get wMoments 3 2048 zLo zHi 206 par = fluxEnergy zLo 207 perp = ceilRatio mul ceilRatio get wMoments 4 2048 wScale zLo 208 put wBoxEnergy 0 par 209 put wBoxEnergy 1 perp 210 put wMaxEnergy 0 larger par get wMaxEnergy 0 211 put wMaxEnergy 1 larger perp get wMaxEnergy 1 212 end 213 214 fn scanDyadicW2 betaBps long to void 215 put wMaxEnergy 0 0 216 put wMaxEnergy 1 0 217 if or lt betaBps 0 gt betaBps 2880 218 put wFailed 0 1 219 ret 220 end 221 inc = mul mul 6 betaBps wScale 222 for k ulong 128 223 lo = floorRatio mul long k inc `1280000` 224 hi = ceilRatio mul long add k 1 inc `1280000` 225 w2FieldBox lo hi 226 end 227 end 228 229 note energy * (degree * betaBps)^2 < 2^28 * 10000^2; degree <= 18, betaBps <= 2880 and energy <= 2^28. 230 fn dyadicSubunit energy long degree long betaBps long to long 231 if or lt energy 0 gt energy wScale 232 put wFailed 0 1 233 ret 0 234 end 235 v = mul degree betaBps 236 if lt mul mul v v energy mul wScale `100000000` 237 ret 1 238 end 239 ret 0 240 end