Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 0 additions & 82 deletions src/Vacuum/VacuumStructs.jl → src/Vacuum/DataTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -340,85 +340,3 @@ function initialize_wall(inputs::VacuumInput, plasma_surf::PlasmaGeometry, wall_
dz_dtheta
)
end

"""
distribute_to_equal_arc_grid(xin, zin, mw1)

Perform arc length re-parameterization of a 2D curve.

Takes an input curve defined by `(xin, zin)` coordinates and re-samples it such that
the new points `(xout, zout)` are equally spaced in arc length along the curve.

# Arguments

- `xin::Vector{Float64}`: Array of x-coordinates of the input curve
- `zin::Vector{Float64}`: Array of z-coordinates of the input curve
- `mw1::Int`: Number of points in the input and output curves

# Returns

- `xout::Vector{Float64}`: Array of x-coordinates of the arc-length re-parameterized curve
- `zout::Vector{Float64}`: Array of z-coordinates of the arc-length re-parameterized curve
- `ell::Vector{Float64}`: Array of cumulative arc lengths for the input curve
- `thgr::Vector{Float64}`: Array of re-parameterized 'theta' values corresponding to equal arc lengths
- `thlag::Vector{Float64}`: Array of normalized 'theta' values for the input curve (0 to 1)

# Notes

- Uses Lagrange interpolation for calculating arc length and resampling
- Ensures uniform spacing in arc length for improved numerical stability
"""
function distribute_to_equal_arc_grid(xin::Vector{Float64}, zin::Vector{Float64}, mtheta::Int)

# Temporary arrays for interpolation and arc-length calculation
theta_in = zeros(Float64, mtheta) # Normalized input parameter [0, 1)
theta_out = zeros(Float64, mtheta) # New parameter distribution for equal spacing
xout = zeros(Float64, mtheta) # Uniformly spaced R-coordinates
zout = zeros(Float64, mtheta) # Uniformly spaced Z-coordinates

# Define initial normalized parameter theta_in
dt = 1.0 / mtheta
theta_in .= range(; start=0, length=mtheta, step=dt) # θ ∈ [0, 1)
# we need a closed loop for arc length calculation
mtheta1 = mtheta + 1
xin1 = vcat(xin, xin[1])
zin1 = vcat(zin, zin[1])
theta_in1 = vcat(theta_in, [1.0])
ell = zeros(Float64, mtheta1) # Cumulative arc length of closed loop

# Calculate cumulative arc length using numerical integration
# We use a mid-point derivative approximation to find the path length
for iw in 2:mtheta1
# Evaluate derivative at the midpoint of the interval
theta = (theta_in1[iw] + theta_in1[iw-1]) / 2.0

# Calculate dx/dt and dz/dt using Lagrange interpolation (order 3)
_, d_xin = lagrange1d(theta_in1, xin1, mtheta1, 3, theta, 1)
_, d_zin = lagrange1d(theta_in1, zin1, mtheta1, 3, theta, 1)

# Instantaneous speed (ds/dt)
ds_dt = sqrt(d_xin^2 + d_zin^2)

# Accumulate length: ds = (ds/dt) * dt
ell[iw] = ell[iw-1] + ds_dt * dt
end

# Re-parameterize based on equal arc-length segments
ell_targets = collect(range(0; step=ell[end]/mtheta, length=mtheta)) # [0, Length) for open loop result
for i in 2:mtheta
# Find the value of 'theta_in' that corresponds to the target arc length 's'
f_th, _ = lagrange1d(ell, theta_in1, mtheta1, 3, ell_targets[i], 0)
theta_out[i] = f_th
end

# Interpolate the original (x,z) data at the new parameter points to get (xout, zout)
# Chance interpolates in theta_out to get xin, zin but this introduces small errors in arc lengths
for i in 1:mtheta
f_x, _ = lagrange1d(ell, xin1, mtheta1, 3, ell_targets[i], 0)
f_z, _ = lagrange1d(ell, zin1, mtheta1, 3, ell_targets[i], 0)
xout[i] = f_x
zout[i] = f_z
end

return xout, zout, ell, theta_out, theta_in
end
Loading