!+ ! Module bmad_common_mod ! ! Module to hold the bmad lattice and other data shared between ! the routines on the Fortran side of things. !- module bmad_common_mod use bmad_struct use bmad_interface use bmad_cpp_convert_mod use fortran_cpp_utils ! integer, parameter :: bad$ = -1, no_init$ = 0, in_init$ = 1, init_done$ = 2 type bmad_doocs_common_struct type (lat_struct) :: lat type (coord_struct), allocatable :: orb(:) integer :: track_state = 0 integer :: status = no_init$ end type ! Used with bmad_return_twiss_and_track type bmad_doocs_saved_twiss_struct type (ele_struct) ele type (coord_struct) orb integer :: status = no_init$ end type type (bmad_doocs_common_struct), target, save, allocatable :: bd_com(:) type (bmad_doocs_saved_twiss_struct), target, save, allocatable :: bd_twiss(:) logical, save :: print_debug = .true. contains !------------------------------------------------------------------------------------- !------------------------------------------------------------------------------------- !------------------------------------------------------------------------------------- function lat_status (ix_lat) result (this_status) integer ix_lat, this_status character(20), parameter :: r_name = 'lat_is_initialized' ! this_status = bad$ if (.not. allocated(bd_com)) return if (ix_lat < 0 .or. ix_lat > size(bd_com)) return this_status = bd_com(ix_lat)%status end function lat_status !------------------------------------------------------------------------------------- !------------------------------------------------------------------------------------- !------------------------------------------------------------------------------------- !+ ! Subroutine bmad_set_lat_pointer (err_flag, r_name, ix_lat, lat, ix_ele, ele, can_be_in_init) ! ! Input: ! r_name -- character(*): Name of calling routine. ! ix_lat -- Integer: Index of lattice. ! ix_ele -- Integer, optional: Index of element. ! can_be_in_init -- Logical: If True then allow lattice status to be in_init$. ! ! Output: ! err_flag -- logical: Set True if error. False otherwise. ! lat -- lat_struct, pointer: Pointer to the lattice. ! ele -- ele_struct, optional, pointer: Pointer to the element. !- Subroutine bmad_set_lat_pointer (err_flag, r_name, ix_lat, lat, ix_ele, ele, can_be_in_init) implicit none type (lat_struct), pointer :: lat type (ele_struct), pointer, optional :: ele integer ix_lat integer, optional :: ix_ele character(*) r_name logical err_flag logical, optional :: can_be_in_init ! err_flag = .true. if (lat_status(ix_lat) /= init_done$) then if (.not. (logic_option(.false., can_be_in_init) .and. lat_status(ix_lat) == in_init$)) then if (lat_status(ix_lat) == in_init$) then call out_io (s_error$, r_name, "Lattice \i0\ not yet fully initialized!", ix_lat) else call out_io (s_error$, r_name, "Lattice \i0\ not yet begun initialization!", ix_lat) endif return endif endif lat => bd_com(ix_lat)%lat if (present (ix_ele)) then if (ix_ele < 0 .or. ix_ele > lat%n_ele_max) then call out_io (s_error$, r_name, 'ELEMENT INDEX OUT OF BOUNDS! \i0\ ', ix_ele) return endif ele => lat%ele(ix_ele) endif err_flag = .false. end subroutine bmad_set_lat_pointer end module