Skip to content
Draft
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
5 changes: 5 additions & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ path = ["docs/src/*.png", "docs/src/*.svg", "docs/src/*.md", "docs/src/*.pdf", "
SPDX-FileCopyrightText = "2025 Uwe Fechner, Bart van de Lint"
SPDX-License-Identifier = "CC-BY-4.0"

[[annotations]]
path = "changelog.d/*.md"
SPDX-FileCopyrightText = "2026 Uwe Fechner, Bart van de Lint"
SPDX-License-Identifier = "MIT"

[[annotations]]
path = ["docs/.gitignore", "docs/Project.toml", "examples/Project.toml", "test/Project.toml", "CITATION.cff"]
SPDX-FileCopyrightText = "2025 Uwe Fechner"
Expand Down
4 changes: 4 additions & 0 deletions changelog.d/359-kps3-and-kps4-read-positions-and-velocit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Changed
- BREAKING: KPS3 and KPS4 read the positions and velocities of the particles from the state of the integrator. After `next_step!`, `s.pos`, `s.vel`, `s.vel_kite` and every getter built on them (`pos_kite`, `calc_elevation`, `tether_length`, `unstretched_length`, `reel_out_speed`, `SysState`, …) give the accepted state of the time step instead of the last state the solver tried. Before `init!` and after `find_steady_state!` they give the state of `init`, `find_steady_state!` or the last call of `residual!`. `s.pos`, `s.vel` and `s.vel_kite` are read-only now; KPS3 gains `s.vel`.
### Fixed
- KPS3: the elevation of the kite used for the correction of the steering by gravity is that of the state the residual is evaluated for, not that of the previous call of the residual.
2 changes: 1 addition & 1 deletion docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Print the vector of the positions of the particles:

```julia
julia> kps.pos
7-element StaticArrays.SVector{7, StaticArrays.MVector{3, Float64}} with indices SOneTo(7):
7-element StaticArrays.SVector{7, StaticArrays.SVector{3, Float64}} with indices SOneTo(7):
[0.0, 0.0, 0.0]
[26.95751778658999, 0.0, 59.59749511924355]
[51.97088814144287, 0.0, 120.03746888266994]
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples_4p.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Print the vector of the positions of the particles:

```julia
julia> kps.pos
11-element StaticArrays.SVector{11, StaticArrays.MVector{3, Float64}} with indices SOneTo(11):
11-element StaticArrays.SVector{11, StaticArrays.SVector{3, Float64}} with indices SOneTo(11):
[0.0, 0.0, 0.0]
[9.179237622840555, 0.0, 23.842235605018427]
[18.04995989861643, 0.0, 47.800963572571995]
Expand Down
1 change: 1 addition & 0 deletions docs/src/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ calc_spring_force
calc_drag_forces
calc_drag_force
tether_drag_forces
particle_positions
particle_velocities
UNUSED_FIELDS
UNUSED_FIELDS_KPS3
Expand Down
8 changes: 2 additions & 6 deletions examples/test_heading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ function create_kite_model(x, y, z, pos)
s.y = y
s.z = z

s.pos[end-2][begin] = pos[begin]
s.pos[end-2][begin+1] = pos[begin+1]
s.pos[end-2][begin+2] = pos[begin+2]
KiteModels.set_pos_kite!(s, pos)
s
end

Expand All @@ -68,9 +66,7 @@ function create_kite_model(x, y, z, pos, upwind_dir_deg)

KiteModels.set_v_wind_ground!(s, pos[begin+2], deg2rad(upwind_dir_deg))

s.pos[end-2][begin] = pos[begin]
s.pos[end-2][begin+1] = pos[begin+1]
s.pos[end-2][begin+2] = pos[begin+2]
KiteModels.set_pos_kite!(s, pos)
s
end

Expand Down
8 changes: 2 additions & 6 deletions mwes/mwe_25.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ function create_kite_model(x, y, z, pos)
s.y = y
s.z = z

s.pos[end-2][begin] = pos[begin]
s.pos[end-2][begin+1] = pos[begin+1]
s.pos[end-2][begin+2] = pos[begin+2]
KiteModels.set_pos_kite!(s, pos)
s
end

Expand All @@ -37,9 +35,7 @@ function create_kite_model(x, y, z, pos, wind)

set_v_wind_ground!(s, pos[begin+2], wind_dir=deg2rad(wind))

s.pos[end-2][begin] = pos[begin]
s.pos[end-2][begin+1] = pos[begin+1]
s.pos[end-2][begin+2] = pos[begin+2]
KiteModels.set_pos_kite!(s, pos)

s
end
Expand Down
102 changes: 24 additions & 78 deletions src/KPS3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ $(TYPEDFIELDS)
res1::SVector{P, KVec3} = zeros(SVector{P, KVec3})
"part two of the residual, difference between vel' and acc, non-flat, mainly for unit testing"
res2::SVector{P, KVec3} = zeros(SVector{P, KVec3})
"vector of the positions of the particles"
pos::SVector{P, KVec3} = zeros(SVector{P, KVec3})
"velocity vector of the kite"
vel_kite::T = zeros(S, 3)
"state vector of the last call of `init` or `residual!` without an integrator"
trial_y::Vector{S} = zeros(S, 6 * (P - 1) + 2)
"acceleration vector of the kite, updated once per time step by next_step!"
acc_kite::T = zeros(S, 3)
"time of the last update of the rates by update_sys_state! [s]; -Inf before the first one"
Expand Down Expand Up @@ -174,6 +172,7 @@ function clear!(s::KPS3)
s.v_wind_vert = 0.0
s.v_apparent .= [Float64(s.set.v_wind), 0, 0]
s.acc_kite .= [0.0, 0, 0]
s.integrator = nothing
s.t_last = -Inf
s.orient_last .= 0.0
s.azimuth_rate = 0.0
Expand Down Expand Up @@ -260,51 +259,19 @@ and its component perpendicular to `unit_vector`.
drag, v_apparent, v_app_perp
end

"""
particle_velocities(s::KPS3)

Velocities of the particles in ENU from the state vector of the integrator; zero before
`init!`.

Returns an `SVector` with one `SVec3` per particle in [m/s].
"""
function particle_velocities(s::KPS3{S, T, P}) where {S, T, P}
integrator = s.integrator
isnothing(integrator) && return zeros(SVector{P, SVec3})
u = integrator.u
# the state vector holds the positions of the particles 2:P, then their velocities
vel(i) = i == 1 ? zero(SVec3) : SVec3(u[3*(P-1)+3*(i-2)+1], u[3*(P-1)+3*(i-2)+2],
u[3*(P-1)+3*(i-2)+3])
SVector{P, SVec3}(ntuple(vel, Val(P)))
end

# Set the positions of the particles and the velocity of the kite from the state vector `u`
# of the integrator. Between two calls of residual! they belong to the last trial state of
# the solver.
function update_kite_state!(s::KPS3{S, T, P}, u) where {S, T, P}
# the state vector holds the positions of the particles 2:P, then their velocities
for i in 2:P
s.pos[i] .= (u[3*(i-2)+1], u[3*(i-2)+2], u[3*(i-2)+3])
end
i_vel = 3*(P-1) + 3*(P-2)
s.vel_kite .= (u[i_vel+1], u[i_vel+2], u[i_vel+3])
nothing
end

"""
tether_drag_forces(s::KPS3)

Drag forces of the tether and the bridle on each particle in ENU, for the positions `s.pos`
and the velocities of `particle_velocities`, distributed on the particles as in
`residual!`.
Drag forces of the tether and the bridle on each particle in ENU, for the positions and
velocities of the state of the model, distributed on the particles as in `residual!`.

Returns an `SVector` with one `SVec3` per particle in [N].
"""
function tether_drag_forces(s::KPS3{S, T, P}) where {S, T, P}
forces = zeros(MVector{P, SVec3})
vel = particle_velocities(s)
pos, vel = s.pos, s.vel
for i in P:-1:2
pos1, pos2 = SVec3(s.pos[i]), SVec3(s.pos[i-1])
pos1, pos2 = pos[i], pos[i-1]
segment = pos1 - pos2
norm1 = norm(segment)
unit_vector = segment / norm1
Expand Down Expand Up @@ -349,9 +316,8 @@ end
Returns a tuple of the x, y, and z vectors of the kite reference frame.
"""
function kite_ref_frame(s::KPS3; one_point=true)
pos_kite = s.pos[end]
delta = pos_kite - s.pos[end - 1]
c = -delta
pos = s.pos
c = pos[end - 1] - pos[end]
z = normalize(c)
y = normalize(s.v_apparent × c)
x = normalize(y × c)
Expand Down Expand Up @@ -452,13 +418,6 @@ Return the absolute value of the force at the winch as calculated during the las
"""
function winch_force(s::KPS3) norm(s.last_force) end

"""
reel_out_speed(s::KPS3)

Return the current reel-out speed of the winch in m/s.
"""
function reel_out_speed(s::KPS3) s.v_reel_out end

"""
calc_set_cl_cd!(s::KPS3, vec_c, v_app)

Expand Down Expand Up @@ -524,20 +483,19 @@ function residual!(res, yd, y::MVector{S, SimFloat}, s::KPS3, _=nothing) where S
veld = SVector{div(T,6)+1}(if i==1 SVector(0.0,0,0) else SVector(veld1_[:,i-1]) end for i in 1:div(T,6)+1)

# update parameters
pos_kite = pos[segments+1]
s.vel_kite .= vel[segments+1]
pos_kite, vel_kite = pos[segments+1], vel[segments+1]
s.l_tether = length
s.segment_length = length / segments

s.axial_stiffness = s.set.axial_stiffness / s.segment_length
s.axial_damping = s.set.axial_damping / s.segment_length
s.beta = calc_elevation(s)
s.beta = KiteUtils.calc_elevation(pos_kite)
Comment thread
1-Bort-1 marked this conversation as resolved.
Resolved

# call core calculation routines
vec_c = SVector{3, SimFloat}(pos[s.set.segments] - pos_kite) # convert to SVector to avoid allocations
v_app = SVector{3, SimFloat}(s.v_wind - s.vel_kite)
v_app = SVector{3, SimFloat}(s.v_wind - vel_kite)
calc_set_cl_cd!(s, vec_c, v_app)
calc_aero_forces(s, pos_kite, s.vel_kite, s.rho, s.steering) # force at the kite
calc_aero_forces(s, pos_kite, vel_kite, s.rho, s.steering) # force at the kite
loop(s, pos, vel, posd, veld, s.res1, s.res2)

# copy and flatten result
Expand All @@ -547,12 +505,7 @@ function residual!(res, yd, y::MVector{S, SimFloat}, s::KPS3, _=nothing) where S
@inbounds res[3*(div(S,6))+3*(i-2)+j] = s.res2[i][j]
end
end
if norm(res) < 1e5
# println(norm(res))
for i in eachindex(pos)
@inbounds s.pos[i] .= pos[i]
end
end
isnothing(s.integrator) && copyto!(s.trial_y, y)
# winch calculations
res[end-1] = lengthd - v_reel_out
# the state of the winch (brake, rate limited set speed) is updated once per time step in next_step!
Expand Down Expand Up @@ -610,10 +563,6 @@ function init_inner(s::KPS3, X=zeros(2 * s.set.segments); delta=0.0, upwind_dir=
for i in 1:s.set.segments
vel[i+1] .+= normalize(pos[i+1] - pos[i]) * (s.set.v_reel_out::Float64 * i / s.set.segments)
end
for i in eachindex(pos)
@inbounds s.pos[i] .= pos[i]
end

for i in 2:s.set.segments+1
state_y0[i-1] .= pos[i] # Initial state vector
yd0[i-1] .= vel[i] # Initial state vector derivative
Expand All @@ -623,6 +572,7 @@ function init_inner(s::KPS3, X=zeros(2 * s.set.segments); delta=0.0, upwind_dir=
state_y0[s.set.segments+i-1] .= vel[i] # Initial state vector
yd0[s.set.segments+i-1] .= acc[i] # Initial state vector derivative
end
s.trial_y[1:6*s.set.segments] .= reduce(vcat, state_y0)
set_v_wind_ground!(s, pos[s.set.segments+1][3]; upwind_dir)
s.l_tether = s.set.l_tether
s.sync_speed = s.set.v_reel_out
Expand All @@ -642,17 +592,12 @@ function init(s::KPS3, X=zeros(2 * (s.set.segments)); delta = 0.0, upwind_dir=no
turn_dir = upwind_dir - deg2rad(s.set.azimuth)
res1 = vcat(turn(res1[1:n], turn_dir), res1[n+1:end])
res2 = vcat(turn(res2[1:n], turn_dir), res2[n+1:end])
# Keep s.pos consistent with the rotated state vectors so callers that
# read s.pos immediately after init/init! see the correct positions.
for i in 2:s.set.segments+1
j = i - 1
s.pos[i] .= res1[3*(j-1)+1:3*j]
end
# the turbulent wind depends on the horizontal position of the kite, so sample it again
# at the turned position
if s.set.use_turbulence != 0
set_v_wind_ground!(s, s.pos[end][3]; upwind_dir)
end
end
s.trial_y .= res1
# the turbulent wind depends on the horizontal position of the kite, so sample it again
# at the turned position
if !isnothing(upwind_dir) && s.set.use_turbulence != 0
set_v_wind_ground!(s, calc_height(s); upwind_dir)
end
MVector{6*(s.set.segments)+2, SimFloat}(res1), MVector{6*(s.set.segments)+2, SimFloat}(res2)
end
Expand All @@ -668,10 +613,10 @@ and for KPS4 also of the springs of the kite. They are the forces of the residua
current positions and velocities, including the damping force, unless `damping=false`.
"""
function spring_forces(s::KPS3; damping=true)
vel = particle_velocities(s)
pos, vel = s.pos, s.vel
forces = zeros(SimFloat, s.set.segments)
for i in 1:s.set.segments
segment = s.pos[i+1] - s.pos[i]
segment = pos[i+1] - pos[i]
norm1 = norm(segment)
spring_vel = (segment / norm1) ⋅ (vel[i+1] - vel[i])
forces[i] = calc_spring_force(s, norm1, spring_vel; damping)
Expand Down Expand Up @@ -807,6 +752,7 @@ function find_steady_state!(s::KPS3; prn=false, delta=nothing, stiffness_factor=
trim=false)
warn_delta_deprecated(delta, :find_steady_state!)
warn_stiffness_factor_deprecated(stiffness_factor, :find_steady_state!)
s.integrator = nothing
el_set = s.set.elevation
alpha_stall = s.set.alpha_cl[argmax(s.set.cl_list)] # angle of attack of maximum lift [°]
# Find the steady state, starting at s.set.elevation. Returns the state, whether the solver
Expand Down
Loading
Loading