Skip to content

Commit

Permalink
Adding SOS state vector initialization in gas inj
Browse files Browse the repository at this point in the history
While computing gas injection, when low pass filter is applied to the
flow rate, it would show a transient if the flow rate starts at a
non-zero value. This could be bad for MPC which uses the computation
while optimization. To avoid this, we initialize the state vector of
the low pass filter to the initial value of the flow rate. This way,
there will never be an initial transient due to the low pass filter.
  • Loading branch information
anchal-physics committed Aug 14, 2024
1 parent 7b5dffa commit 6b969ac
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/gas_injection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function compute_gas_injection(
)
end
if !isnothing(LPF)
flow_rate = filt(LPF, flow_rate)
flow_rate = filt(LPF, flow_rate, create_si(LPF, flow_rate[1]))
end
flow_rate = map((x)::Float64 -> x < 0.0 ? 0.0 : x, flow_rate)
future_flow_rates = flow_rate[end-skip+2:end]
Expand Down Expand Up @@ -345,6 +345,36 @@ function get_lpf(
return convert(SecondOrderSections, bilinear(filter_s, fs))
end

"""
create_si(filter, last_val)
Create initial state for a filter based on a provided last value. This assumes that
upto this point, there was a constant command on (called prev_xi in this code) and the
filter had reached equilibrium to a constant output which is the last_val provided.
"""
function create_si(filter::SecondOrderSections, last_val::Float64)
n = length(filter.biquads)
g = filter.g
si = zeros(Float64, 2, n)
for fi n:-1:1
b0 = filter.biquads[fi].b0
b1 = filter.biquads[fi].b1
b2 = filter.biquads[fi].b2
a1 = filter.biquads[fi].a1
a2 = filter.biquads[fi].a2
if fi == n
prev_yi = last_val / g
else
prev_yi = last_val
end
prev_xi = prev_yi * (1 + a1 + a2) / (b0 + b1 + b2)
si[2, fi] = b2 * prev_xi - a2 * prev_yi
si[1, fi] = si[2, fi] + b1 * prev_xi - a1 * prev_yi
last_val = prev_xi
end
return si
end

"""
dribble(
data::Vector{Float64},
Expand Down

0 comments on commit 6b969ac

Please sign in to comment.