-
Notifications
You must be signed in to change notification settings - Fork 0
/
shape.jl
785 lines (661 loc) · 23 KB
/
shape.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
abstract type PlasmaShape{T} end
Base.broadcastable(S::PlasmaShape) = (S,)
Base.eltype(::PlasmaShape{T}) where T = T
function Base.copy(S::T) where T <: PlasmaShape
SS = T((getfield(S,s) for s in fieldnames(T))...)
return SS
end
function (S::PlasmaShape)(ρ::T,θ::T,ζ::T) where T<:Number
ρ > minor_radius(S) && throw(DomainError("ρ is larger than the minor radius"))
r, z = S(ρ,θ)
x = r*cos(ζ)
y = r*sin(ζ)
return x,y,z
end
function (S::PlasmaShape)(x::T) where T <:AbstractVector
N = length(x)
if N == 2
return collect(S(x[1],x[2]))
elseif N == 3
return collect(S(x[1],x[2],x[3]))
else
throw(ArgumentError("Length of array can only be 2 or 3"))
end
end
"""
3 element u: Volume element
2 element u: Internal Area element
"""
function dVA(S::PlasmaShape,u::T) where T<:AbstractVector
@assert length(u) in (2,3)
abs(det(ForwardDiff.jacobian(S,u)))
end
"""
Surface area element of a plasma shape at a given position
"""
function dS(S::PlasmaShape,u::T) where T<:AbstractVector{R} where R
N = length(u)
@assert N == 2
d1 = zeros(R,3)
d2 = zeros(R,3)
r = minor_radius(S)
for i=1:3
d1[i] = ForwardDiff.derivative(t->S([r,t,u[2]])[i], u[1])
d2[i] = ForwardDiff.derivative(t->S([r,u[1],t])[i], u[2])
end
return norm(cross(d1,d2))
end
"""
Line element of a a plasma shape at a given position
"""
function dL(S::PlasmaShape,u::T) where T<:Number
d = zeros(2)
r = minor_radius(S)
for i=1:2
d[i] = ForwardDiff.derivative(t->S([r,t])[i], u)
end
return norm(d)
end
"""
Integrate a function F which takes a scalar or vector over the given plasma shape
Example:
```
I = integrate(x->1,S,:line,(0,2pi)) # circumference
I = integrate(x->1,S,:area,(0,minor_radius(S)),(0,2pi)) # area
I = integrate(x->1,S,:surface,(0,2pi),(0,2pi)) # surface area
I = integrate(x->1,S,:volume,(0,minor_radius(S)),(0,2pi),(0,2pi))
```
"""
function integrate(f::Function, S::PlasmaShape, type::Symbol, bds::Vararg{NTuple{2,Number},N}; kwargs...) where N
kws = pairs((rtol=1e-3, atol=1e-3, kwargs...))
if N == 1 && type == :line
θ_min, θ_max = bds[1]
(F,err) = hquadrature(x->f(x)*dL(S,x),θ_min,θ_max; kws...)
elseif N == 2 && type == :surface
θ_min, θ_max = bds[1]
ζ_min, ζ_max = bds[2]
(F,err) = hcubature(x->f(x)*dS(S,x),(θ_min,ζ_min),(θ_max,ζ_max); kws...)
elseif N == 2 && type == :area
r_min, r_max = bds[1]
θ_min, θ_max = bds[2]
(F,err) = hcubature(x->f(x)*dVA(S,x),(r_min, θ_min),(r_max, θ_max); kws...)
elseif N == 3 && type == :volume
r_min, r_max = bds[1]
θ_min, θ_max = bds[2]
ζ_min, ζ_max = bds[3]
(F,err) = hcubature(x->f(x)*dVA(S,x),(r_min,θ_min,ζ_min),(r_max,θ_max,ζ_max); kws...)
else
throw(ArgumentError("Unsupported Integral Type: $type : Supported types: :line ∫dθ, :area ∬drdθ, :surface ∬dθdζ, :volume ∭drdθdζ"))
end
return F
end
@memoize LRU(maxsize=cache_size) function integrate(f::Function, S::PlasmaShape, type::Symbol; kwargs...)
if type == :line
F_bar = integrate(f, S, :line, (0.0,2pi); kwargs...)
elseif type == :surface
F_bar = integrate(f, S, :surface, (0.0,2pi),(0.0,2pi); kwargs...)
elseif type == :area
F_bar = integrate(f, S, :area, (0.0, minor_radius(S)), (0.0, 2pi); kwargs...)
elseif type == :volume
F_bar = integrate(f, S, :volume, (0.0, minor_radius(S)), (0.0, 2pi), (0.0, 2pi); kwargs...)
else
throw(ArgumentError("Unsupported Integral Type: $type : Supported types: :line ∫dθ, :area ∬drdθ, :surface ∬dθdζ, :volume ∭drdθdζ"))
end
end
@memoize LRU(maxsize=cache_size) function circumference(S::PlasmaShape; kwargs...)
integrate(x->1, S, :line, (0.0,2pi); kwargs...)
end
@memoize LRU(maxsize=cache_size) function area(S::PlasmaShape; kwargs...)
integrate(x->1, S, :area, (0.0,minor_radius(S)), (0.0,2pi); kwargs...)
end
@memoize LRU(maxsize=cache_size) function surface_area(S::PlasmaShape; kwargs...)
integrate(x->1,S,:surface, (0.0,2pi), (0.0,2pi); kwargs...)
end
@memoize LRU(maxsize=cache_size) function volume(S::PlasmaShape; kwargs...)
integrate(x->1, S, :volume, (0.0,minor_radius(S)), (0.0,2pi), (0.0,2pi); kwargs...)
end
@memoize LRU(maxsize=cache_size) function average(f::Function, S::PlasmaShape, type::Symbol; kwargs...)
if type == :line
F_bar = integrate(f, S, :line, (0.0,2pi); kwargs...)/circumference(S; kwargs...)
elseif type == :surface
F_bar = integrate(f, S, :surface, (0.0,2pi),(0.0,2pi); kwargs...)/surface_area(S; kwargs...)
elseif type == :area
F_bar = integrate(f, S, :area, (0.0, minor_radius(S)), (0.0, 2pi); kwargs...)/area(S; kwargs...)
elseif type == :volume
F_bar = integrate(f, S, :volume, (0.0, minor_radius(S)), (0.0, 2pi), (0.0, 2pi); kwargs...)/volume(S; kwargs...)
else
throw(ArgumentError("Unsupported Average Type: $type : Supported types: :line ∫dθ, :area ∬drdθ, :surface ∬dθdζ, :volume ∭drdθdζ"))
end
end
function limits(s::T, x_point=nothing; pad=0.2) where {T<:PlasmaShape}
xlims = (s(pi)[1], s(0.0)[1])
ylims = (s(3pi / 2)[2], s(pi / 2)[2])
xpad = xlims[2] - xlims[1]
ypad = ylims[2] - ylims[1]
xlims = (max(xlims[1] - xpad * pad, 0.0), xlims[2] + xpad * pad)
ylims = (ylims[1] - ypad * pad, ylims[2] + ypad * pad)
if x_point !== nothing
xlims = (min(xlims[1],x_point[1]), max(xlims[2],x_point[1]))
ylims = (min(ylims[1],x_point[2]), max(ylims[2],x_point[2]))
end
return xlims, ylims
end
function plasma_boundary(S::PlasmaShape; kwargs...)
x,y = shape(S; kwargs...)
return PlasmaBoundary(collect(zip(x[1:end-1],y[1:end-1])))
end
function scale_aspect(S::T,s) where T<:PlasmaShape
return T((f == :ϵ ? s*getfield(S,f) : getfield(S,f) for f in fieldnames(T))...)
end
function Base.getproperty(S::PlasmaShape,s::Symbol)
if s == :_get_x
return x -> S(x)[1]
elseif s == :_get_y
return x -> S(x)[2]
end
return getfield(S,s)
end
"""
MillerShape Structure
Defines the Miller Plasma Shape Parameterization
Fields:\\
`R0` - Major Radius [m]\\
`Z0` - Elevation [m]\\
`ϵ` - Inverse Aspect Ratio a/R0 where a = minor radius\\
`κ` - Elongation\\
`δ` - Triangularity
"""
struct MillerShape{T} <: PlasmaShape{T}
R0::T # Major Radius [m]
Z0::T # Elevation
ϵ::T # Inverse Aspect Ratio a/R0 (a = minor radius)
κ::T # Elongation
δ::T # Triangularity
end
const MShape = MillerShape
MillerShape() = MillerShape(0.0, 0.0, 0.0, 0.0, 0.0)
function MillerShape(R0,Z0,ϵ,κ,δ)
MillerShape(promote(R0,Z0,ϵ,κ,δ)...)
end
# Miller Shape API
aspect_ratio(S::MillerShape) = inv(S.ϵ)
elongation(S::MillerShape) = S.κ
major_radius(S::MillerShape) = S.R0
minor_radius(S::MillerShape) = S.R0*S.ϵ
elevation(S::MillerShape) = S.Z0
triangularity(S::MillerShape) = S.δ
function Base.show(io::IO, S::MillerShape)
print(io, "$(typeof(S))\n")
print(io, " R0 = $(round(major_radius(S),digits=3)) [m]\n")
print(io, " Z0 = $(round(elevation(S),digits=3)) [m]\n")
print(io, " ϵ = $(round(inv(aspect_ratio(S)),digits=3))\n")
print(io, " κ = $(round(elongation(S),digits=3))\n")
print(io, " δ = $(round(triangularity(S),digits=3))")
end
function m_rz(r, θ, R0, Z0, κ, δ)
δ₀ = asin(δ)
x = R0 + r * cos(θ + δ₀ * sin(θ))
y = Z0 + r * κ * sin(θ)
return x, y
end
function shape(S::MillerShape; N=100, normed=false)
r = S.ϵ*S.R0
x = zeros(N)
y = zeros(N)
τ = range(0,2pi,length=N)
@inbounds for i=1:N
x[i], y[i] = m_rz(r, τ[i], S.R0, S.Z0, S.κ, S.δ)
end
if !normed
return x, y
else
return x/S.R0, (y .- S.Z0)/S.R0
end
end
function (S::MillerShape)(θ::T) where T<:Number
r = S.ϵ*S.R0
x, y = m_rz(r, θ, S.R0, S.Z0, S.κ, S.δ)
return x,y
end
function (S::MillerShape)(r::T,θ::T) where T<:Number
x, y = m_rz(r, θ, S.R0, S.Z0, S.κ, S.δ)
return x,y
end
"""
AsymmetricMillerShape Structure
Defines the Asymmetric Miller Plasma Shape Parameterization
Fields:\\
`R0` - Major Radius [m]\\
`Z0` - Elevation [m]\\
`ϵ` - Inverse Aspect Ratio a/R0 where a = minor radius\\
`κ` - Elongation\\
`δl` - Lower Triangularity\\
`δu` - Upper Triangularity
"""
struct AsymmetricMillerShape{T} <: PlasmaShape{T}
R0::T # Major Radius [m]
Z0::T # Elevation
ϵ::T # Inverse Aspect Ratio a/R0 (a = minor radius)
κ::T # Elongation
δl::T # Lower Triangularity
δu::T # Lower Triangularity
end
const AMShape = AsymmetricMillerShape
AsymmetricMillerShape() = AsymmetricMillerShape(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
function AsymmetricMillerShape(R0,Z0,ϵ,κ,δl,δu)
MillerShape(promote(R0,Z0,ϵ,κ,δl,δu)...)
end
aspect_ratio(S::AMShape) = inv(S.ϵ)
elongation(S::AMShape) = S.κ
major_radius(S::AMShape) = S.R0
minor_radius(S::AMShape) = S.R0*S.ϵ
elevation(S::AMShape) = S.Z0
triangularity(S::AMShape) = (S.δl,S.δu)
function Base.show(io::IO, S::AMShape)
print(io, "$(typeof(S))\n")
print(io, " R0 = $(round(major_radius(S),digits=3)) [m]\n")
print(io, " Z0 = $(round(elevation(S),digits=3)) [m]\n")
print(io, " ϵ = $(round(inv(aspect_ratio(S)),digits=3))\n")
print(io, " κ = $(round(elongation(S),digits=3))\n")
print(io, " δ = $(round.(triangularity(S),digits=3))")
end
function am_rz(r, θ, R0, Z0, κ, δl, δu)
δ₀l = asin(δl)
δ₀u = asin(δu)
y = Z0 + r * κ * sin(θ)
δ₀ = y < Z0 ? δ₀l : δ₀u
x = R0 + r * cos(θ + δ₀ * sin(θ))
return x, y
end
function shape(S::AMShape; N=100, normed=false)
r = S.ϵ*S.R0
x = zeros(N)
y = zeros(N)
τ = range(0,2pi,length=N)
@inbounds for i=1:N
x[i], y[i] = am_rz(r, τ[i], S.R0, S.Z0, S.κ, S.δl, S.δu)
end
if !normed
return x, y
else
return x/S.R0, (y .- S.Z0)/S.R0
end
end
function (S::AMShape)(θ::T) where T<:Number
r = S.ϵ*S.R0
x, y = am_rz(r, θ, S.R0, S.Z0, S.κ, S.δl, S.δu)
return x,y
end
function (S::AMShape)(r::T,θ::T) where T<:Number
x, y = am_rz(r, θ, S.R0, S.Z0, S.κ, S.δl, S.δu)
return x,y
end
"""
TurnbullMillerShape Structure
Defines the Turnbull-Miller Plasma Shape Parameterization\\
> Turnbull, A. D., et al. "Improved magnetohydrodynamic stability through optimization of higher order moments in cross-section shape of tokamaks." Physics of Plasmas 6.4 (1999): 1113-1116.
Fields:\\
`R0` - Major Radius [m]\\
`Z0` - Elevation [m]\\
`ϵ` - Inverse Aspect Ratio a/R0 where a = minor radius\\
`κ` - Elongation\\
`δ` - Triangularity\\
`ζ` - Squareness
"""
struct TurnbullMillerShape{T} <: PlasmaShape{T}
R0::T # Major Radius [m]
Z0::T # Elevation
ϵ::T # Inverse Aspect Ratio a/R0 (a = minor radius)
κ::T # Elongation
δ::T # Triangularity
ζ::T # Squareness
end
const TMShape = TurnbullMillerShape
TurnbullMillerShape() = TurnbullMillerShape(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
function TurnbullMillerShape(R0,Z0,ϵ,κ,δ,ζ)
TurnbullMillerShape(promote(R0,Z0,ϵ,κ,δ,ζ)...)
end
aspect_ratio(S::TMShape) = inv(S.ϵ)
elongation(S::TMShape) = S.κ
major_radius(S::TMShape) = S.R0
minor_radius(S::TMShape) = S.R0*S.ϵ
elevation(S::TMShape) = S.Z0
triangularity(S::TMShape) = S.δ
squareness(S::TMShape) = S.ζ
function Base.show(io::IO, S::TMShape)
print(io, "$(typeof(S))\n")
print(io, " R0 = $(round(major_radius(S),digits=3)) [m]\n")
print(io, " Z0 = $(round(elevation(S),digits=3)) [m]\n")
print(io, " ϵ = $(round(inv(aspect_ratio(S)),digits=3))\n")
print(io, " κ = $(round(elongation(S),digits=3))\n")
print(io, " δ = $(round(triangularity(S),digits=3))\n")
print(io, " ζ = $(round(squareness(S),digits=3))")
end
function tm_rz(r, θ, R0, Z0, κ, δ, ζ)
δ₀ = asin(δ)
x = R0 + r * cos(θ + δ₀*sin(θ))
y = Z0 + r * κ * sin(θ + ζ*sin(2*θ))
return x, y
end
function shape(S::TMShape; N=100, normed=false)
r = S.ϵ*S.R0
x = zeros(N)
y = zeros(N)
τ = range(0,2pi,length=N)
@inbounds for i=1:N
x[i], y[i] = tm_rz(r, τ[i], S.R0, S.Z0, S.κ, S.δ, S.ζ)
end
if !normed
return x, y
else
return x/S.R0, (y .- S.Z0)/S.R0
end
end
function (S::TMShape)(θ::T) where T<:Number
r = S.ϵ*S.R0
x, y = tm_rz(r, θ, S.R0, S.Z0, S.κ, S.δ, S.ζ)
return x,y
end
function (S::TMShape)(r::T,θ::T) where T<:Number
x, y = tm_rz(r, θ, S.R0, S.Z0, S.κ, S.δ, S.ζ)
return x,y
end
function TurnbullMillerShape(S::MillerShape)
TurnbullMillerShape(S.R0, S.Z0, S.ϵ, S.κ, S.δ, zero(S.δ))
end
"""
MillerExtendedHarmonicShape Structure
Defines the Miller Extended Harmonic Plasma Shape Parameterization\\
> Arbon, Ryan, Jeff Candy, and Emily A. Belli. "Rapidly-convergent flux-surface shape parameterization." Plasma Physics and Controlled Fusion 63.1 (2020): 012001.
Fields:\\
`R0` - Major Radius [m]\\
`Z0` - Elevation [m]\\
`ϵ` - Inverse Aspect Ratio a/R0 where a = minor radius\\
`κ` - Elongation\\
`c0` - Tilt\\
`c` - Cosine coefficients i.e. [ovality,...]\\
`s` - Sine coefficients i.e. [asin(triangularity), squareness,...])
"""
struct MillerExtendedHarmonicShape{N, T} <: PlasmaShape{T}
R0::T # Major Radius
Z0::T # Elevation
ϵ::T # inverse aspect ratio a/R0
κ::T # Elongation
c0::T # Tilt
c::SVector{N,T} # Cosine coefficients acos.([ovality,...])
s::SVector{N,T} # Sine coefficients asin.([triangularity,-squareness,...]
end
const MXHShape = MillerExtendedHarmonicShape
function MillerExtendedHarmonicShape(N)
if N == -2
return MillerShape()
elseif N == -1
return AsymmetricMillerShape()
end
return MXHShape(0.0, 0.0, 0.0, 0.0, 0.0, zeros(SVector{N}), zeros(SVector{N}))
end
function MillerExtendedHarmonicShape(R0, Z0, ϵ, κ, c0, c::Vector, s::Vector)
@assert length(c) == length(s)
R0, Z0, ϵ, κ, c0 = promote(R0,Z0,ϵ,κ,c0, one(eltype(s)), one(eltype(c)))
N = length(c)
if N == 0 && c0 == zero(R0)
return MillerShape(R0,Z0,ϵ,κ,c0)
end
c = convert(SVector{N, typeof(R0)}, c)
s = convert(SVector{N, typeof(R0)}, s)
MillerExtendedHarmonicShape(R0, Z0, ϵ, κ, c0, c, s)
end
function MillerExtendedHarmonicShape(R0, Z0, ϵ, κ, δ; tilt=zero(κ), c0=tilt, ovality=one(R0), squareness=zero(R0))
R0, Z0, ϵ, κ, c0, ovality, squareness = promote(R0,Z0,ϵ,κ,c0,ovality,squareness)
Z = zero(R0)
if δ == Z && c0 == Z && ovality == Z && squareness == Z
return MillerShape(R0,Z0,ϵ,κ,δ)
end
c = SVector(ovality, zero(ovality))
s = SVector(asin(δ), -squareness)
MillerExtendedHarmonicShape(R0,Z0,ϵ,κ,c0,c,s)
end
function MillerExtendedHarmonicShape(S::MillerShape)
MXHShape(S.R0,S.Z0,S.ϵ,S.κ,zero(S.δ), SVector(zero(S.δ)), SVector(asin.(S.δ)))
end
function MillerExtendedHarmonicShape(S::TurnbullMillerShape)
MXHShape(S.R0, S.Z0, S.ϵ, S.κ, zero(S.δ), SVector(zero(S.δ),zero(S.δ)), SVector(asin.(S.δ),-S.ζ))
end
aspect_ratio(S::MXHShape) = inv(S.ϵ)
elongation(S::MXHShape) = S.κ
major_radius(S::MXHShape) = S.R0
minor_radius(S::MXHShape) = S.R0*S.ϵ
elevation(S::MXHShape) = S.Z0
tilt(S::MXHShape) = S.c0
ovality(S::MXHShape) = S.c[1]
triangularity(S::MXHShape) = sin(S.s[1])
# MXH squareness differs from TurnbullMiller, using MXH paper's definition
squareness(S::MXHShape) = -S.s[2]
function Base.show(io::IO, S::MXHShape)
print(io, "$(typeof(S))\n")
print(io, " R0 = $(round(major_radius(S),digits=3)) [m]\n")
print(io, " Z0 = $(round(elevation(S),digits=3)) [m]\n")
print(io, " ϵ = $(round(inv(aspect_ratio(S)),digits=3))\n")
print(io, " κ = $(round(elongation(S),digits=3))\n")
print(io, " c₀ = $(round(tilt(S),digits=3))\n")
print(io, " c = $(round.(S.c,digits=3))\n")
print(io, " s = $(round.(S.s,digits=3))")
end
function mxh_rz(r, θ, R0, Z0, κ, c0, c::SVector{N}, s::SVector{N}) where N
c_sum = 0.0
@inbounds for n=1:N
c_sum += c[n]*cos(n*θ)
end
s_sum = 0.0
@inbounds for n=1:N
s_sum += s[n]*sin(n*θ)
end
θ_R = θ + c0 + c_sum + s_sum
x = R0 + r*cos(θ_R)
y = Z0 + κ*r*sin(θ)
return x, y
end
function shape(S::MXHShape; N=100, normed=false)
r = S.ϵ*S.R0
x = zeros(N)
y = zeros(N)
θ = range(0,2pi,length=N)
@inbounds for i=1:N
x[i], y[i] = mxh_rz(r, θ[i], S.R0, S.Z0, S.κ, S.c0, S.c, S.s)
end
if !normed
return x, y
else
return x/S.R0, (y .- S.Z0)/S.R0
end
end
function (S::MXHShape)(θ::T) where T<:Number
r = S.ϵ*S.R0
return mxh_rz(r, θ, S.R0, S.Z0, S.κ, S.c0, S.c, S.s)
end
function (S::MXHShape)(r::T,θ::T) where T<:Number
return mxh_rz(r, θ, S.R0, S.Z0, S.κ, S.c0, S.c, S.s)
end
"""
Luce Plasma Shape as described in:
> "An analytic functional form for characterization and generation of axisymmetric plasma boundaries",\\
TC Luce, Plasma Phys. Control. Fusion 55 (2013) http://dx.doi.org/10.1088/0741-3335/55/9/095009
Fields:\\
`R0` - Major Radius [m]\\
`Z0` - Elevation [m]\\
`r` - Minor Radius [m]\\
`Zᵣₘ` - Z(Rₘₐₓ) [m]\\
`κ` - Lower and Upper Elongation\\
`δ` - Lower and Upper Triangulation\\
`ζ` - Squareness for the I,II,III,IV quadrants
"""
struct LuceShape{T} <: PlasmaShape{T}
R0::T # Major Radius
Z0::T # Elevation
r::T # Minor Radius
Zᵣₘ::T # Zoff = Z(Rₘₐₓ)
κ::NTuple{2,T} # Lower and Upper Elongation
δ::NTuple{2,T} # Lower and Upper Triangularity
ζ::NTuple{4,T} # Squareness for the 4 quadrants ζ_(uo,ui,li,lo)
end
const LShape = LuceShape
LuceShape() = LuceShape(0.0, 0.0, 0.0, 0.0, (0.0,0.0), (0.0,0.0), (0.0,0.0,0.0,0.0))
function LuceShape(R0,Z0,r,Zrm,κ::NTuple{2},δ::NTuple{2},ζ::NTuple{4})
R0,Z0,r,Zrm = promote(R0,Z0,r,Zrm)
κ = convert.(typeof(R0), κ)
δ = convert.(typeof(R0), δ)
ζ = convert.(typeof(R0), ζ)
LuceShape(R0,Z0,r,Zrm,κ,δ,ζ)
end
function LuceShape(G::PlasmaGeometricParameters)
LuceShape(getfield.(G,fieldnames(typeof(G)))...)
end
function LuceShape(r::Vector,z::Vector)
G = plasma_geometry(r,z)
return LuceShape(G)
end
function Base.show(io::IO, G::LuceShape)
print(io, "$(typeof(G))\n")
print(io, " R0 = $(round(G.R0,digits=3)) [m]\n")
print(io, " Z0 = $(round(G.Z0,digits=3)) [m]\n")
print(io, " r = $(round(G.r,digits=3)) [m]\n")
print(io, " Zᵣₘ= $(round.(G.Zᵣₘ,digits=3)) [m]\n")
print(io, " κ = $(round.(G.κ,digits=3))\n")
print(io, " δ = $(round.(G.δ,digits=3))\n")
print(io, " ζ = $(round.(G.ζ,digits=3))")
end
aspect_ratio(S::LShape) = S.r/S.R0
elongation(S::LShape) = S.κ
major_radius(S::LShape) = S.R0
minor_radius(S::LShape) = S.r
elevation(S::LShape) = S.Z0
triangularity(S::LShape) = S.δ
squareness(S::LShape) = S.ζ
function superellipse(t,A,B,n)
st, ct = sincos(t)
x = abs(ct)^(2/n) * A*sign(ct)
y = abs(st)^(2/n) * B*sign(st)
return x, y
end
function luce_rz(r, θ, R0, Z0, Zrm, κ::NTuple{2}, δ::NTuple{2}, ζ::NTuple{4})
θ = mod2pi(θ)
if 0 <= θ < pi/2
t = θ
A = r*(1 + δ[2])
B = κ[2]*r
n = -log(2)/log(inv(sqrt(2)) + ζ[1]*(1 - inv(sqrt(2))))
x, y = superellipse(t,A,B,n)
R = x + r*(inv(r/R0) - δ[2])
Z = y + Zrm
elseif pi/2 <= θ < pi
t = pi - θ
A = r*(1 - δ[2])
B = κ[2]*r
n = -log(2)/log(inv(sqrt(2)) + ζ[2]*(1 - inv(sqrt(2))))
x, y = superellipse(t,A,B,n)
R = r*(inv(r/R0) - δ[2]) - x
Z = y + Zrm
elseif pi <= θ < 3pi/2
t = θ - pi
A = r*(1 - δ[1])
B = κ[1]*r
n = -log(2)/log(inv(sqrt(2)) + ζ[3]*(1 - inv(sqrt(2))))
x, y = superellipse(t,A,B,n)
R = r*(inv(r/R0) - δ[1]) - x
Z = Zrm - y
else
t = 2pi - θ
A = r*(1 + δ[1])
B = κ[1]*r
n = -log(2)/log(inv(sqrt(2)) + ζ[4]*(1 - inv(sqrt(2))))
x, y = superellipse(t,A,B,n)
R = x + r*(inv(r/R0) - δ[1])
Z = Zrm - y
end
return R, Z
end
function shape(S::LuceShape; N=100, normed=false)
x = zeros(N)
y = zeros(N)
θ = range(0,2pi,length=N)
@inbounds for i=1:N
x[i], y[i] = luce_rz(S.r, θ[i], S.R0, S.Z0, S.Zᵣₘ, S.κ, S.δ, S.ζ)
end
if !normed
return x, y
else
return x/S.R0, (y .- S.Z0)/S.R0
end
end
function (S::LuceShape)(θ::T) where T<:Number
return luce_rz(S.r, θ, S.R0, S.Z0, S.Zᵣₘ, S.κ, S.δ, S.ζ)
end
function (S::LuceShape)(r::T,θ::T) where T<:Number
return luce_rz(r, θ, S.R0, S.Z0, S.Zᵣₘ, S.κ, S.δ, S.ζ)
end
# --- Conversion and Promotions ---
(::Type{T})(S::PlasmaShape) where T<:PlasmaShape = convert(T,S)
Base.convert(::Type{<:PlasmaShape{T}}, x) where T = T(x)
for S in (MShape,AMShape,TMShape,LShape)
@eval begin
function Base.convert(SS::Type{<:PlasmaShape{T}},W::$S{R}) where {T,R}
return convert($S{T},W)
end
function Base.convert(::Type{$S{T}},W::$S) where T
return $S{T}((getfield(W,s) for s in fieldnames($S))...)
end
function Base.promote_rule(::Type{$S{T}},::Type{$S{R}}) where {T,R}
return $S{promote_type(T,R)}
end
function Base.promote_rule(::Type{$S{T}},::Type{R}) where {T,R}
return $S{promote_type(T,R)}
end
function Base.promote_rule(::Type{R},::Type{$S{T}}) where {T,R}
return $S{promote_type(T,R)}
end
end
end
# --- Promotion/Conversion for MXHShape ---
function Base.convert(SS::Type{<:PlasmaShape{T}},W::MXHShape{N,R}) where {N,T,R}
return convert(MXHShape{N,T},W)
end
function Base.convert(::Type{MXHShape{N,T}},W::MXHShape) where {N,T}
return MXHShape{N,T}((getfield(W,s) for s in fieldnames(MXHShape))...)
end
function Base.promote_rule(::Type{MXHShape{N,T}},::Type{MXHShape{N,R}}) where {N,T,R}
return MXHShape{N,promote_type(T,R)}
end
function Base.promote_rule(::Type{MXHShape{N,T}},::Type{R}) where {N,T,R}
return MXHShape{N,promote_type(T,R)}
end
function Base.promote_rule(::Type{R},::Type{MXHShape{N,T}}) where {N,T,R}
return MXHShape{N,promote_type(T,R)}
end
function convert_eltype(x::S, ::Type{R}) where {S<:PlasmaShape, R}
convert(S.name.wrapper{R},x)
end
# --- special cases ----
function plasma_geometry(S::PlasmaShape)
return plasma_geometry(plasma_boundary(S))
end
function plasma_geometry(S::Union{MShape,TMShape})
Z = zero(S.δ)
return PlasmaGeometricParameters(S.R0,S.Z0,S.R0*S.ϵ,S.Z0,(S.κ,S.κ), (S.δ, S.δ),(Z,Z,Z,Z))
end
function plasma_geometry(S::AMShape)
Z = zero(S.δ)
return PlasmaGeometricParameters(S.R0,S.Z0,S.R0*S.ϵ, S.Z0, (S.κ,S.κ), (S.δl, S.δu),(Z,Z,Z,Z))
end
#--- Curvature calculation via AutoDiff ---
_d1x(S) = (S._get_x)'
_d1y(S) = (S._get_y)'
_d2x(S) = (S._get_x)''
_d2y(S) = (S._get_y)''
function curvature(S::PlasmaShape,θ)
xp = _d1x(S)(θ)
yp = _d1y(S)(θ)
xpp = _d2x(S)(θ)
ypp = _d2y(S)(θ)
κ = abs(yp*xpp - ypp*xp)/(xp^2 + yp^2)^1.5
return κ
end