interplane.Get Omega
Read in your language

yang

This project has not described itself yet.

Brought by drakestapleton

branch main Branches Tags History

yang / src / ufbi_w2_dyadic.om

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