A subroutine to allocate the response matrix storage objects
subroutine alloc_response_objects
use gs2_layouts, only: jf_lo, f_lo, im_idx, in_idx, ig_idx, ifield_idx, ij_idx, idx_local
use theta_grid, only: ntgrid
implicit none
integer :: ic, idc, sc_len, ilo, dc, im, in, ig, ifld, ik, it, jlo
!Top level, one object for each class (length of supercell)
if(.not.allocated(aminv)) allocate(aminv(i_class))
!Loop over each class
do ic=1,i_class
!Get the supercell length
sc_len=(2*ntgrid+1)*nfield*N_class(ic)
!Count how many dcell we have locally and fill related data
dc=0
do ilo=f_lo(ic)%llim_world,f_lo(ic)%ulim_world
!i.e. what is my class of supercell and which cell am I looking at
im = im_idx(f_lo(ic), ilo)
in = in_idx(f_lo(ic), ilo)
! find standard coordinates
!Get theta, field, kx and ky indexes for current point
ig = ig_idx(f_lo(ic), ilo)
ifld = ifield_idx(f_lo(ic), ilo)
ik = f_lo(ic)%ik(im,in)
it = f_lo(ic)%it(im,in)
! translate to fast field coordinates
jlo = ij_idx(jf_lo, ig, ifld, ik, it)
! Locate this jlo, count it, and save address
!Is this point on this proc, if so increment counter
if (idx_local(jf_lo,jlo)) then
! count it
dc = dc + 1
! save dcell address
jf_lo%dj(ic,jlo) = dc
! save supercell address
jf_lo%mj(jlo) = im
endif
enddo
!Next level, one object for each point in the class
if(.not. allocated(aminv(ic)%dcell))then
allocate(aminv(ic)%dcell(dc))
endif
!Now loop over each point and allocate storage for the response data
do idc=1,dc
!Bottom level, this is actually where data is stored
if(.not. allocated(aminv(ic)%dcell(idc)%supercell)) then
allocate(aminv(ic)%dcell(idc)%supercell(sc_len))
endif
enddo
enddo
end subroutine alloc_response_objects