Skip to content

Commit 668744b

Browse files
committed
propagate seq.minlen everywhere
1 parent c397568 commit 668744b

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/madl_sequence.mad

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ local is_nil, is_boolean, is_number, is_integer, is_decimal,
3939
local not_mappable, not_extendable, not_mutable in concept
4040
local implicit, observed in flags
4141

42-
local abs, modf in math
42+
local abs, min, modf in math
4343

4444
local type, getmetatable, setmetatable, assert, error, rawequal =
4545
type, getmetatable, setmetatable, assert, error, rawequal
@@ -125,7 +125,7 @@ end
125125

126126
-- check sequence spos and elements overlapp, [clear 'at' and 'from']
127127
local function check_drift (seq, idx)
128-
local data, ds = seq.__dat
128+
local data, minlen, ds = seq.__dat, seq.minlen or minlen
129129
assert(idx > 1 and idx < data.ne, "unexpected corrupted element index")
130130

131131
ds = data.spos[idx] - (data.spos[idx-1] + data.ds[idx-1])
@@ -155,7 +155,7 @@ end
155155

156156
-- check sequence spos and elements overlapp
157157
local function check_pos (seq)
158-
local data = seq.__dat
158+
local data, minlen = seq.__dat, seq.minlen or minlen
159159
for i=1,data.ne-1 do
160160
local ds = data.spos[i+1] - (data.spos[i] + data.ds[i])
161161
if ds < -minlen then
@@ -295,7 +295,7 @@ end
295295

296296
-- get index from s-position (binary)
297297
local function index_of_num (seq, num, ref_, dir_)
298-
local data = seq.__dat
298+
local data, minlen = seq.__dat, seq.minlen or minlen
299299
if ref_ then num = num + data.upos[index_of_ref(seq,ref_)] end
300300
if num < 0 or num > seq.l then return nil end
301301
local idx, dir = bsearch(data.spos, num), dir_ or 1 -- num <= spos[idx]
@@ -544,7 +544,7 @@ end
544544

545545
-- compute elements positions
546546
local function build_pos (seq, sref)
547-
local data = seq.__dat
547+
local data, minlen = seq.__dat, seq.minlen or minlen
548548
local n, idx, pos = data.ne, 1, 0
549549
for i=1,n do
550550
if data[i].from == 'selected' then
@@ -556,8 +556,8 @@ local function build_pos (seq, sref)
556556
local sseq = sref[idx]
557557
if is_nil(sseq) then -- element
558558
elem_pos(seq, idx) -- set element positions
559-
if data.spos[idx]-pos < -minlen then -- see check_pos
560-
-- if pos-minlen > data.spos[idx] then
559+
if pos-minlen > data.spos[idx] then
560+
-- if data.spos[idx]-pos < -minlen then -- see check_pos
561561
seq_poserr('element', seq, idx, pos)
562562
elseif data[idx].is_thin and data.ds[idx] ~= 0 then
563563
seq_lenerr("thin element", seq, idx, data[idx].l)
@@ -730,12 +730,6 @@ local function finish_sequ (seq)
730730
-- sanity checks
731731
check_mark(seq)
732732
if option.debug >= 2 then check_sequ(seq) end
733-
734-
-- publish sequence in MADX environment
735-
if option.madxenv then
736-
seq.minlen = 1e-6 -- [m] for MAD-X compatibility
737-
seq:publish(MADX, true)
738-
end
739733
end
740734

741735
-- sequence init (build)
@@ -757,10 +751,17 @@ local function init_mm (seq)
757751
data.algn = table.new(ne,0)
758752
seq.__dat, data.ne = data, ne
759753

754+
-- set minlen to 1e-6 [m] for MAD-X compatibility
755+
if option.madxenv then seq.minlen = 1e-6 end
756+
760757
build_idx(data)
761758
build_pos(seq, sref)
762759
clear_sub(seq, sref)
763760
finish_sequ(seq)
761+
762+
-- publish sequence elements in MADX environment
763+
if option.madxenv then seq:publish(MADX, true) end
764+
764765
return seq
765766
end
766767

@@ -1426,6 +1427,7 @@ local function share (seq1, seq2)
14261427
assert(not (data1.ro or seq1:is_readonly()), "invalid write access to readonly sequence")
14271428
assert(not (data2.ro or seq2:is_readonly()), "invalid write access to readonly sequence")
14281429

1430+
local minlen = min(seq1.minlen or minlen, seq2.minlen or minlen)
14291431
local elm_r, idx_r, nr = {}, {}, 1
14301432
for i=2,data1.ne-1 do
14311433
local nam = data1[i].name -- share by (unique) name

0 commit comments

Comments
 (0)