subroutine choose_quads(lat, dk1, regex_mask_in) use bmad_struct use bmad_interface implicit none type (lat_struct) lat type (ele_struct), pointer :: slave type (control_struct), pointer :: ctl integer i, j, is character(40), optional :: regex_mask_in character(40) :: regex_mask = '' real(rp), allocatable, intent(inout) :: dk1(:) logical found ! find which quads to change if (.not. allocated(dk1)) allocate(dk1(lat%n_ele_max)) if (present(regex_mask_in)) then call upcase_string(regex_mask_in) regex_mask = regex_mask_in else regex_mask='' endif do i = 1, lat%n_ele_max dk1(i) = 0.0 if (lat%ele(i)%key == quadrupole$ .and. & attribute_free(lat%ele(i), 'K1',err_print_flag = .false.) .and. & abs(lat%ele(i)%value(tilt$)) < 0.01) then if (.not. match_reg(lat%ele(i)%name, regex_mask)) cycle ! if no mask provided, mask set to '', thereby always matching if (lat%ele(i)%a%beta > lat%ele(i)%b%beta) then dk1(i) = +1 else dk1(i) = -1 endif else dk1(i) = 0 endif if(lat%ele(i)%key == match$)then dk1(i) = 1 !If there is a match element we will use it to qtune cycle endif if (lat%ele(i)%lord_status == overlay_lord$) then found = .false. do is = 1, lat%ele(i)%n_slave slave => pointer_to_slave(lat%ele(i), is, ctl) if (ctl%ix_attrib == k1$ .and. slave%key == quadrupole$ .and. slave%value(tilt$) == 0) then if (.not. match_reg(slave%name, regex_mask)) cycle found = .true. exit endif enddo if (.not. found) cycle !write(*,*) 'overlay: ', trim(lat%ele(i)%name) if (slave%a%beta > slave%b%beta) then dk1(i) = +1 else dk1(i) = -1 endif endif enddo return end subroutine choose_quads