-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #683 from bmad-sim/devel/69
Corrected code for using natural emit for initial beam emit.
- Loading branch information
Showing
13 changed files
with
970 additions
and
909 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
!+ | ||
! Function set_emit_from_beam_init (beam_init_in, ele, species, modes, err_flag) result (beam_init_set) | ||
! | ||
! Routine to put in the beam_init_set structure values that should be used to initialize a beam. | ||
! For example, if beam_init_in%sig_z is set negative, beam_init_set%sig_z will be set to the value in modes. | ||
! Also, if emit is set, norm_emit will be computed and vice versa. | ||
! | ||
! Input: | ||
! beam_init_in -- beam_init_struct: Input parameters | ||
! ele -- ele_struct: | ||
! species -- integer: Beam particle species. | ||
! modes -- normal_modes_struct, optional: Normal mode parameters. Used if stuff like beam_init_in%a_emit set negative. | ||
! | ||
! Ouput: | ||
! err_flag -- logical, optional: Set true if there is an error. False otherwise. | ||
! beam_init_set -- beam_init_struct: See above. | ||
!- | ||
|
||
function set_emit_from_beam_init (beam_init_in, ele, species, modes, err_flag) result (beam_init_set) | ||
|
||
use bmad_routine_interface, dummy => set_emit_from_beam_init | ||
|
||
implicit none | ||
|
||
type (beam_init_struct), target :: beam_init_in, beam_init_set | ||
type (beam_init_struct), pointer :: bis | ||
type (ele_struct) ele | ||
type (normal_modes_struct), optional :: modes | ||
|
||
real(rp) ran_g(2), beta_gamma | ||
integer species | ||
logical, optional :: err_flag | ||
|
||
character(*), parameter :: r_name = 'set_emit_from_beam_init' | ||
|
||
! Check | ||
|
||
if (present(err_flag)) err_flag = .true. | ||
bis => beam_init_set | ||
bis = beam_init_in | ||
if (ele%value(p0c$) == 0) return | ||
|
||
! Sanity checks. | ||
|
||
if (bis%a_emit /= 0 .and. bis%a_norm_emit /= 0 .and. .not. (bis%a_emit < 0 .and. bis%a_norm_emit < 0)) then | ||
call out_io (s_error$, r_name, 'I am confused! Both a_emit and a_norm_emit are set non-zero in the beam_init_struct structure.', & | ||
'Please set one or the other to zero.') | ||
return | ||
endif | ||
|
||
if (bis%b_emit /= 0 .and. bis%b_norm_emit /= 0 .and. .not. (bis%b_emit < 0 .and. bis%b_norm_emit < 0)) then | ||
call out_io (s_error$, r_name, 'I am confused! Both b_emit and b_norm_emit are set non-zero in the beam_init_struct structure.', & | ||
'Please set one or the other to zero.') | ||
return | ||
endif | ||
|
||
! a-emit | ||
|
||
beta_gamma = ele%value(p0c$) / mass_of(species) | ||
|
||
if (bis%a_emit < 0 .or. bis%a_norm_emit < 0) then | ||
if (present(modes)) then | ||
bis%a_emit = modes%a%emittance | ||
bis%a_norm_emit = modes%a%emittance * beta_gamma | ||
else | ||
call out_io (s_warn$, r_name, & | ||
'a_emit and/or a_norm_emit is set negative but the calling program has not provided lattice emittance numbers!', & | ||
'Will use a value of zero...') | ||
bis%a_emit = 0 | ||
bis%a_norm_emit = 0 | ||
endif | ||
elseif (bis%a_norm_emit /= 0) then | ||
bis%a_emit = bis%a_norm_emit / beta_gamma | ||
else | ||
bis%a_norm_emit = bis%a_emit * beta_gamma | ||
endif | ||
|
||
! b-emit | ||
|
||
if (bis%b_emit < 0 .or. bis%b_norm_emit < 0) then | ||
if (present(modes)) then | ||
bis%b_emit = modes%b%emittance | ||
bis%b_norm_emit = modes%b%emittance * beta_gamma | ||
else | ||
call out_io (s_warn$, r_name, & | ||
'b_emit and/or b_norm_emit is set negative but the calling program has not provided lattice emittance numbers!', & | ||
'Will use a value of zero...') | ||
bis%b_emit = 0 | ||
bis%b_norm_emit = 0 | ||
endif | ||
elseif (bis%b_norm_emit /= 0) then | ||
bis%b_emit = bis%b_norm_emit / beta_gamma | ||
else | ||
bis%b_norm_emit = bis%b_emit * beta_gamma | ||
endif | ||
|
||
! sig_z | ||
|
||
if (bis%sig_z < 0) then | ||
if (present(modes)) then | ||
bis%sig_z = modes%sig_z | ||
else | ||
call out_io (s_warn$, r_name, & | ||
'sig_z is set negative but the calling program has not provided lattice emittance numbers!', & | ||
'Will use a value of zero...') | ||
bis%sig_z = 0 | ||
endif | ||
endif | ||
|
||
! sig_pz | ||
|
||
if (bis%sig_pz < 0) then | ||
if (present(modes)) then | ||
bis%sig_pz = modes%sigE_E | ||
else | ||
call out_io (s_warn$, r_name, & | ||
'sig_pz is set negative but the calling program has not provided lattice emittance numbers!', & | ||
'Will use a value of zero...') | ||
bis%sig_pz = 0 | ||
endif | ||
endif | ||
|
||
! Checking that |bis%dpz_dz| < mode%sigE_E / mode%sig_z | ||
|
||
if (abs(bis%dPz_dz * bis%sig_z) > bis%sig_pz) then | ||
call out_io (s_error$, r_name, "|dpz_dz| MUST be < mode%sigE_E / mode%sig_z") | ||
return | ||
endif | ||
|
||
! Add jitter if needed | ||
|
||
if (any(bis%emit_jitter /= 0)) then | ||
call ran_gauss(ran_g) ! ran(3:4) for z and e jitter used below | ||
bis%a_emit = bis%a_emit * (1 + bis%emit_jitter(1) * ran_g(1)) | ||
bis%b_emit = bis%b_emit * (1 + bis%emit_jitter(2) * ran_g(2)) | ||
bis%a_norm_emit = bis%a_norm_emit * (1 + bis%emit_jitter(1) * ran_g(1)) | ||
bis%b_norm_emit = bis%b_norm_emit * (1 + bis%emit_jitter(2) * ran_g(2)) | ||
endif | ||
|
||
if (present(err_flag)) err_flag = .false. | ||
|
||
end function set_emit_from_beam_init | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.