Skip to content

Commit

Permalink
Merge pull request #627 from bmad-sim/devel/52
Browse files Browse the repository at this point in the history
Fix foil ref species bookkeeping.
  • Loading branch information
DavidSagan authored Nov 9, 2023
2 parents b3f0bf2 + 093950b commit 8b91468
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 64 deletions.
4 changes: 2 additions & 2 deletions bmad/code/lat_compute_reference_energy.f90
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ subroutine ele_compute_ref_energy_and_time (ele0, ele, param, err_flag)
if (key == converter$) then
ele%ref_species = ele%converter%species_out
elseif (key == foil$) then
if (ele0%value(final_charge$) == real_garbage$) ele0%value(final_charge$) = atomic_number(ele0%ref_species)
n = nint(ele0%value(final_charge$))
if (ele%value(final_charge$) == real_garbage$) ele%value(final_charge$) = atomic_number(ele0%ref_species)
n = nint(ele%value(final_charge$))
ele%ref_species = set_species_charge(ele0%ref_species, n)
else
ele%ref_species = ele0%ref_species
Expand Down
4 changes: 3 additions & 1 deletion bmad/doc/electromagnetic-fields.tex
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ \section{Map Decomposition of Magnetic and Electric Fields}

The \vn{generalized gradient map} start with the cylindrical map decomposition but then express the
field using coefficients derived from an expansion of the scalar potential in powers of the radius
(\sref{s:gen.grad.phys}.
(\sref{s:gen.grad.phys}).

%-----------------------------------------------------------------
\section{Cartesian Map Field Decomposition}
Expand Down Expand Up @@ -526,6 +526,8 @@ \section{Cartesian Map Field Decomposition}
zero), and $k_z$ must be non-zero for the \vn{hyper-xy} forms, and $k_x$ must be nonzero for the
\vn{hyper-x} forms. The magnetic field is always well defined even if one of the $k$'s is zero.

Note: The vector potential for these fields is given in \sref{s:wiggler.std}.

%-----------------------------------------------------------------
\section{Cylindrical Map Decomposition}
\label{s:cylind.map.phys}
Expand Down
4 changes: 2 additions & 2 deletions bmad/modules/attribute_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ subroutine init_attribute_name_array ()
call init_attribute_name1 (crab_cavity$, cylindrical_map$, 'CYLINDRICAL_MAP')
call init_attribute_name1 (crab_cavity$, gen_grad_map$, 'GEN_GRAD_MAP')
call init_attribute_name1 (crab_cavity$, grid_field$, 'GRID_FIELD')
call init_attribute_name1 (crab_cavity$, gradient$, 'GRADIENT')
call init_attribute_name1 (crab_cavity$, gradient$, 'GRADIENT', dependent$)
call init_attribute_name1 (crab_cavity$, rf_frequency$, 'RF_FREQUENCY')
call init_attribute_name1 (crab_cavity$, rf_wavelength$, 'RF_WAVELENGTH', dependent$)
call init_attribute_name1 (crab_cavity$, rf_clock_harmonic$, 'rf_clock_harminic', private$)
Expand Down Expand Up @@ -1901,7 +1901,7 @@ function attribute_type (attrib_name, ele) result (attrib_type)
case ('TAYLOR_ORDER', 'N_SLICE', 'DIRECTION', 'TIME_DIR', 'N_PARTICLE', 'VERTICAL_KICK', 'N_CELL', &
'IX_TO_BRANCH', 'IX_TO_ELEMENT', 'NUM_STEPS', 'INTEGRATOR_ORDER', 'N_SLAVE', 'N_LORD', &
'MAX_FRINGE_ORDER', 'UPSTREAM_ELE_DIR', 'DOWNSTREAM_ELE_DIR', 'RUNGE_KUTTA_ORDER', &
'SAD_N_DIV_MAX', 'LONGITUDINAL_MODE', 'MOSAIC_DIFFRACTION_NUM')
'SAD_N_DIV_MAX', 'LONGITUDINAL_MODE', 'MOSAIC_DIFFRACTION_NUM', 'FINAL_CHARGE')
attrib_type = is_integer$

case ('APERTURE_AT', 'APERTURE_TYPE', 'COUPLER_AT', 'FIELD_CALC', 'EXACT_MULTIPOLES', &
Expand Down
10 changes: 8 additions & 2 deletions sim_utils/interfaces/particle_species_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,12 @@ function set_species_charge(species_in, charge) result(species_charged)
return
endif

species_charged = species_in + int(z'1000000') * (charge - species_in / int(z'1000000'))
if (charge < -127 .or. charge > 127) then
call out_io (s_error$, r_name, 'CHARGE TO SET TO DOES NOT MAKE SENSE: ' // int_str(charge))
return
endif

species_charged = species_in + int(z'1000000') * (charge - species_in / int(z'1000000'))

end function set_species_charge

Expand Down Expand Up @@ -1588,7 +1593,8 @@ end function x0_radiation_length
!+
! Function atomic_number(species) result (atomic_num)
!
! Routine to return the True if species argument corresponds to an atomic particle or is a proton.
! Routine to return the atomic number Z if species argument corresponds to an atomic particle or is a proton.
! Set to zero otherwise.
!
! Input:
! species -- integer: Spicies ID.
Expand Down
17 changes: 11 additions & 6 deletions tao/code/tao_init_variables_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ subroutine tao_init_variables (var_file)
character(200) file_name
character(200) line, search_for_lat_eles

logical(4) default_key_bound, default_good_user
logical default_key_bound, default_good_user
logical err, free, gang
logical searching, limited
logical, allocatable :: dflt_good_unis(:), good_unis(:)
Expand Down Expand Up @@ -896,11 +896,16 @@ subroutine tao_pointer_to_var_in_lattice2 (var, ix_uni, err)

! allocate space for var%slave.

n_old = size(var%slave)
var_slave_saved = var%slave
deallocate (var%slave)
allocate (var%slave(n_old+size(a_ptr)))
var%slave(1:n_old) = var_slave_saved
if (allocated(var%slave)) then
n_old = size(var%slave)
var_slave_saved = var%slave
deallocate (var%slave)
allocate (var%slave(n_old+size(a_ptr)))
var%slave(1:n_old) = var_slave_saved
else
n_old = 0
allocate(var%slave(size(a_ptr)))
endif

! locate attribute

Expand Down
52 changes: 2 additions & 50 deletions tao/code/tao_python_cmd.f90
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ subroutine tao_python_cmd (input_str)
use wall3d_mod, only: calc_wall_radius
use tao_command_mod, only: tao_next_switch, tao_cmd_split
use tao_init_data_mod, only: tao_point_d1_to_data
use tao_init_variables_mod, only: tao_point_v1_to_var
use tao_init_variables_mod, only: tao_point_v1_to_var, tao_var_stuffit2
use tao_c_interface_mod, only: tao_c_interface_com, re_allocate_c_double
use tao_plot_mod, only: tao_set_floor_plan_axis_label
use tao_data_and_eval_mod, only: tao_evaluate_expression
Expand Down Expand Up @@ -7532,27 +7532,6 @@ subroutine tao_python_cmd (input_str)
ele_name = upcase(name_arr(2))
attrib_name = upcase(name_arr(3))

num_ele = 0
do iu = lbound(s%u, 1), ubound(s%u, 1)
if (.not. picked(iu)) cycle
call tao_init_find_elements (s%u(iu), ele_name, eles, attrib_name, found_one)
n = size(eles)
if (n == 0) cycle
call pointer_to_attribute(eles(1)%ele, attrib_name, .true., a_ptr, err, .true.)
if (err) return
if (.not. associated(a_ptr%r)) then
call out_io (s_error$, r_name, 'ELEMENT ATTRIBUTE MUST BE A REAL (NOT AN INTEGER, ETC.)')
return
endif
call re_allocate_eles (eles2, num_ele+n)
call re_allocate (index_arr, num_ele+n)
do i = num_ele+1, num_ele+n
index_arr(i) = iu
eles2(i)%ele => eles(i-num_ele)%ele
enddo
num_ele = num_ele + n
enddo

v_ptr%exists = .true.
v_ptr%merit_type = str_val(name_arr(9), 'limit')
v_ptr%good_var = .true.
Expand All @@ -7567,34 +7546,7 @@ subroutine tao_python_cmd (input_str)
v_ptr%key_bound = logic_val(name_arr(11), .false., err); if (err) return
v_ptr%key_delta = real_val(name_arr(12), 0.0_rp, err); if (err) return

if (num_ele == 0) then
v_ptr%model_value => v_ptr%old_value ! Just to point to somewhere
v_ptr%base_value => v_ptr%old_value
v_ptr%exists = .false.
v_ptr%key_bound = .false.
return
endif

v_ptr%model_value => a_ptr%r

if (allocated(v_ptr%slave)) deallocate (v_ptr%slave)
allocate (v_ptr%slave(num_ele))

do i = 1, num_ele
ele => eles2(i)%ele
iu = index_arr(i)
v_ptr%slave(i)%ix_uni = iu
v_ptr%slave(i)%ix_branch = ele%ix_branch
v_ptr%slave(i)%ix_ele = ele%ix_ele
call pointer_to_attribute(ele, attrib_name, .true., a_ptr, err, .true.)
v_ptr%slave(i)%model_value => a_ptr%r

ele => s%u(iu)%base%lat%branch(ele%ix_branch)%ele(ele%ix_ele)
call pointer_to_attribute(ele, attrib_name, .true., a_ptr, err, .true.)
v_ptr%base_value => a_ptr%r
v_ptr%slave(i)%base_value => a_ptr%r
enddo

call tao_var_stuffit2(picked, v_ptr, 'python var_create command')

!------------------------------------------------------------------------------------------------
!------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tao/version/tao_version_mod.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
!-

module tao_version_mod
character(*), parameter :: tao_version_date = "2023/11/04 03:26:16"
character(*), parameter :: tao_version_date = "2023/11/06 08:27:07"
end module

0 comments on commit 8b91468

Please sign in to comment.