Skip to content

Commit

Permalink
Add mode flip warning in Tao. (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSagan committed Feb 13, 2024
1 parent 32842d3 commit 763f354
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
37 changes: 17 additions & 20 deletions bmad/code/twiss_propagate_all.f90
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
!+
! Subroutine twiss_propagate_all (lat, ix_branch, err_flag, ie_start, ie_end, zero_uncalculated)
! Subroutine twiss_propagate_all (lat, ix_branch, err_flag, ie_start, ie_end)
!
! Subroutine to propagate the twiss, coupling, and dispersion parameters from
! the start to the end of a lattice branch.
!
! If there is a problem, Twiss parameters not calculated in range [ie_start, ie_end] are set to zero.
!
! Input:
! lat -- lat_struct: lattice.
Expand All @@ -12,15 +13,13 @@
! ie_start -- integer, optional: Starting element index. Default is 0.
! Note: The first element at which the Twiss parameters are calculated is ie_start+1.
! ie_end -- integer, optional: Ending element index, Default is branch%n_ele_track.
! zero_uncalculated -- logical, optional: Set to zero Twiss parameters not calculated in
! range [ie_start, ie_end]? Default is True.
!
! Output:
! lat -- lat_struct: Lattice with parameters computed for the branch.
! err_flag -- logical, optional: Set True if there is an error. False otherwise.
!-

subroutine twiss_propagate_all (lat, ix_branch, err_flag, ie_start, ie_end, zero_uncalculated)
subroutine twiss_propagate_all (lat, ix_branch, err_flag, ie_start, ie_end)

use bmad_interface, except_dummy => twiss_propagate_all

Expand All @@ -35,7 +34,7 @@ subroutine twiss_propagate_all (lat, ix_branch, err_flag, ie_start, ie_end, zero
integer n, n_track, i_start, i_end
integer, optional :: ix_branch, ie_start, ie_end

logical, optional :: err_flag, zero_uncalculated
logical, optional :: err_flag
logical err

character(*), parameter :: r_name = 'twiss_propagate_all'
Expand Down Expand Up @@ -81,21 +80,19 @@ subroutine twiss_propagate_all (lat, ix_branch, err_flag, ie_start, ie_end, zero
if (err) return
enddo

if (logic_option(.true., zero_uncalculated)) then
do n = 0, i_start-1
branch%ele(n)%a = twiss_struct()
branch%ele(n)%b = twiss_struct()
branch%ele(n)%x = xy_disp_struct()
branch%ele(n)%y = xy_disp_struct()
enddo

do n = i_end+1, n_track
branch%ele(n)%a = twiss_struct()
branch%ele(n)%b = twiss_struct()
branch%ele(n)%x = xy_disp_struct()
branch%ele(n)%y = xy_disp_struct()
enddo
endif
do n = 0, i_start-1
branch%ele(n)%a = twiss_struct()
branch%ele(n)%b = twiss_struct()
branch%ele(n)%x = xy_disp_struct()
branch%ele(n)%y = xy_disp_struct()
enddo

do n = i_end+1, n_track
branch%ele(n)%a = twiss_struct()
branch%ele(n)%b = twiss_struct()
branch%ele(n)%x = xy_disp_struct()
branch%ele(n)%y = xy_disp_struct()
enddo

! Make sure final mode is same as initial mode

Expand Down
4 changes: 2 additions & 2 deletions bmad/modules/bmad_routine_interface.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3089,12 +3089,12 @@ subroutine twiss_propagate1 (ele1, ele2, err)
logical, optional :: err
end subroutine

subroutine twiss_propagate_all (lat, ix_branch, err_flag, ie_start, ie_end, zero_uncalculated)
subroutine twiss_propagate_all (lat, ix_branch, err_flag, ie_start, ie_end)
import
implicit none
type (lat_struct), target :: lat
integer, optional :: ix_branch, ie_start, ie_end
logical, optional :: err_flag, zero_uncalculated
logical, optional :: err_flag
end subroutine

subroutine twiss_to_1_turn_mat (twiss, phi, mat2)
Expand Down
10 changes: 8 additions & 2 deletions tao/code/tao_lattice_calc_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ subroutine tao_single_track (tao_lat, calc_ok, ix_branch, print_err)
character(*), parameter :: r_name = "tao_single_track"

logical, optional :: print_err
logical calc_ok, err, radiation_fluctuations_on
logical calc_ok, err, radiation_fluctuations_on, mode_flip

!

Expand Down Expand Up @@ -161,7 +161,6 @@ subroutine tao_single_track (tao_lat, calc_ok, ix_branch, print_err)
endif

bmad_com%radiation_fluctuations_on = radiation_fluctuations_on

endif

! Twiss
Expand All @@ -188,6 +187,13 @@ subroutine tao_single_track (tao_lat, calc_ok, ix_branch, print_err)
call twiss_propagate_all (lat, ix_branch, err, 0, ix_lost - 1)
endif

mode_flip = any(branch%ele(1:branch%n_ele_track)%mode_flip)
if (mode_flip .and. .not. tao_branch%mode_flip_here) then
call out_io(s_warn$, r_name, '*Mode flip* detected! Care must be used in interpreting Twiss parameter!', &
'See the Bmad manual on linear optics for more information.')
endif
tao_branch%mode_flip_here = mode_flip

else
branch%param%stable = .false.
branch%param%unstable_factor = 0 ! Unknown
Expand Down
1 change: 1 addition & 0 deletions tao/code/tao_struct.f90
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ module tao_struct
logical has_open_match_element
logical :: plot_cache_valid = .false. ! Valid plotting data cache?
logical :: spin_map_valid = .false.
logical :: mode_flip_here = .false. ! Twiss parameter mode flip seen?
end type

! Structure to hold a single lat_struct (model, base, or design) in
Expand Down

0 comments on commit 763f354

Please sign in to comment.