subroutine map_constraint_to_lat_element (con, lat, co, lat_init) use bmad use constraints_mod implicit none type (lat_struct), target :: lat, lat_init type (coord_struct) co(0:lat%n_ele_max) type (constraint_struct), target :: con type (con_struct), pointer :: conc type (ele_struct), pointer :: ele_init, slave real(rp) value, det integer i, j, ix1, ix2 integer variable, plane ! find where constraint is. ! if location is a controller element then the location is at the first ! slave element except where2 and max/min then location is the last ! slave element con_loop: do i = 1, con%n_constraint conc => con%c(i) conc%where2_ix = -1 ! default if not used if (conc%where1 == 'FULL_TURN' )then conc%where1_ix = 0 conc%where1_ix_lord = 0 if (conc%where2 == null_name$) then conc%where2_ix = 0 conc%where2_ix_lord = 0 cycle con_loop ! next constraint endif endif do j = 0, lat%n_ele_max if (j <= lat%n_ele_track) then ! regular element ix1 = j ix2 = j else ! controller element slave => pointer_to_slave (lat%ele(j), 1) if (slave%ix_ele > lat%n_ele_track) slave => pointer_to_slave (slave, 1) ix1 = slave%ix_ele ix2 = ix1 if (conc%constraint == maxz$ .or. conc%constraint == minz$) then slave => pointer_to_slave (lat%ele(j), lat%ele(j)%n_slave) if (slave%ix_ele > lat%n_ele_track) slave => pointer_to_slave (slave, slave%n_slave) ix2 = slave%ix_ele endif endif if (conc%where1 == lat%ele(j)%name) then conc%where1_ix = ix1 conc%where1_ix_lord = j if (conc%where2 == null_name$) then conc%where2_ix = ix2 conc%where2_ix_lord = j cycle con_loop ! next constraint endif endif if (conc%where2==lat%ele(j)%name) then conc%where2_ix = ix2 conc%where2_ix_lord = j endif end do if(conc%where2 /= null_name$ .and. conc%where2_ix == -1)then print '(3x,a44,i3,a18,a16)',' MAP_CONSTRAINT_TO_LAT_ELEMENT: Constraint ', & i,' No element named ',conc%where2 call err_exit endif end do con_loop ! fill in target value from initial values do i = 1, con%n_constraint conc => con%c(i) if (conc%set_target_to_initial_value) then variable = conc%variable plane = conc%plane if(conc%where1(1:6) /= 'GLOBAL')then do j = 1, lat_init%n_ele_max if (conc%where1 == lat_init%ele(j)%name) then ele_init => lat_init%ele(j) exit endif if (j == lat_init%n_ele_max) then print *, 'ERROR IN MAP_CONSTRAINT_TO_LAT_ELEMENT: CANNOT FIND LOCATION' print *, ' IN init_lat: ', conc%where1 call err_exit endif enddo endif if (variable == beta$) then if (plane == x_plane$) conc%target_value = ele_init%a%beta if (plane == y_plane$) conc%target_value = ele_init%b%beta elseif (variable == alpha$) then if (plane == x_plane$) conc%target_value = ele_init%a%alpha if (plane == y_plane$) conc%target_value = ele_init%b%alpha elseif (variable == eta$) then if (plane == x_plane$) conc%target_value = ele_init%a%eta if (plane == y_plane$) conc%target_value = ele_init%b%eta elseif (variable == etap$) then if (plane == x_plane$) conc%target_value = ele_init%a%etap if (plane == y_plane$) conc%target_value = ele_init%b%etap elseif (variable == c11$) then conc%target_value = ele_init%c_mat(1,1) elseif (variable == c12$) then conc%target_value = ele_init%c_mat(1,2) elseif (variable == c21$) then conc%target_value = ele_init%c_mat(2,1) elseif (variable == c22$) then conc%target_value = ele_init%c_mat(2,2) elseif (variable == displacement_$) then if (plane == x_plane$) conc%target_value = co(0)%vec(1) if (plane == y_plane$) conc%target_value = co(0)%vec(3) elseif (variable == co_angle$) then if (plane == x_plane$) conc%target_value = co(0)%vec(2) if (plane == y_plane$) conc%target_value = co(0)%vec(4) elseif (variable == q$) then if (plane == x_plane$) conc%target_value = lat_init%a%tune / twopi if (plane == y_plane$) conc%target_value = lat_init%b%tune / twopi if (plane == z_plane$) conc%target_value = lat_init%z%tune / twopi else print *, 'ERROR IN MAP_CONSTRAINTS_TO_LAT_ELEMENT: ERROR SETTING TARGET' print *, ' CONSTRAINT #', i print *, ' FOR: ', var_name(conc%variable), plane_name(conc%plane) call err_exit endif endif enddo end