module mod_allocation
    use mod_parameters
    use mod_variables
    use mod_plib
    implicit none
contains
    
subroutine alloc_NM
    implicit none

    call delloc_real2(S_U)
    call delloc_real2(S_L)
    call delloc_real2(T_U)
    call delloc_real2(T_L)
    call delloc_int1(S_I)
    call delloc_int1(T_I)

    call alloc_real2(S_U, smtot, sph_band   )
    call alloc_real2(S_L, smtot, sph_lo_diag)
    call alloc_real2(T_U, tmtot, tor_band   )
    call alloc_real2(T_L, tmtot, tor_lo_diag)
    call alloc_int1(S_I, smtot)
    call alloc_int1(T_I, tmtot)
end subroutine alloc_NM

subroutine alloc_RHS
    implicit none
    call delloc_complex1(RHS%spheriodal)
    call delloc_complex1(RHS%torodial  )

    call alloc_complex1(RHS%spheriodal, smtot)
    call alloc_complex1(RHS%torodial  , tmtot)
end subroutine alloc_RHS


subroutine alloc_vars
    implicit none
    integer :: il, nn

    do il = 1, numoflayers
        nn = layer(il)%n
        call delloc_complex2(layer(il)%rhoe    )
        call delloc_complex2(layer(il)%vel     )
        call delloc_complex2(layer(il)%pvel    )
        call delloc_complex2(layer(il)%str     )
        call delloc_complex2(layer(il)%dis     )
        call delloc_complex2(layer(il)%disl    )
        call delloc_complex2(layer(il)%ur_i    )
        call delloc_complex2(layer(il)%pot     )
        call delloc_complex2(layer(il)%grad_pot)
        call delloc_complex2(layer(il)%fc      )
        call delloc_complex2(layer(il)%fc0     )
        call delloc_complex2(layer(il)%fc1     )
        call delloc_real1   (layer(il)%spectrum)
        call delloc_real1   (layer(il)%spectrum_v)
        call delloc_complex2(layer(il)%div_tensor)

        call alloc_complex2(layer(il)%rhoe    , nn  ,   total_scalar_harm)
        call alloc_complex2(layer(il)%vel     , nn+1,   total_vector_harm)
        call alloc_complex2(layer(il)%pvel    , nn+1,   total_vector_harm)
        call alloc_complex2(layer(il)%str     , nn  , 6*total_scalar_harm)
        call alloc_complex2(layer(il)%dis     , nn  ,   total_vector_harm)
        call alloc_complex2(layer(il)%disl    , nn+1,   total_vector_harm)
        call alloc_complex2(layer(il)%ur_i    , nn  ,   total_scalar_harm)
        call alloc_complex2(layer(il)%pot     , nn  ,   total_scalar_harm)
        call alloc_complex2(layer(il)%grad_pot, nn-1,   total_vector_harm)
        call alloc_complex2(layer(il)%fc      , nn-1,   total_vector_harm)
        call alloc_complex2(layer(il)%fc0     , nn-1,   total_vector_harm)
        call alloc_complex2(layer(il)%fc1     , nn-1,   total_vector_harm)
        call alloc_complex2(layer(il)%div_tensor, nn-1, total_vector_harm)
        call alloc_real1   (layer(il)%spectrum  , Max_jmax)
        call alloc_real1   (layer(il)%spectrum_v, Max_jmax)
    end do
    call alloc_real1(kh,3*10)
    call alloc_real2(khp, 3*10, num_time_steps)
    call alloc_real2(kh_per, 3*10, Max_num_period)
    call alloc_real1(gp20 , num_time_steps)
    call alloc_real1(rgp22, num_time_steps)
    call alloc_real1(igp22, num_time_steps)
    call alloc_real1(ur20 , num_time_steps)
    call alloc_real1(rur22, num_time_steps)
    call alloc_real1(iur22, num_time_steps)
    call alloc_real1(ut20 , num_time_steps)
    call alloc_real1(rut22, num_time_steps)
    call alloc_real1(iut22, num_time_steps)
end subroutine alloc_vars

subroutine alloc_rads
    implicit none
    integer :: il, nn

    do il = 1, numoflayers
        nn = layer(il)%n

        call delloc_real1(layer(il)%r_i    ); call delloc_real1(layer(il)%r_l    );
        call delloc_real1(layer(il)%rho_i  ); call delloc_real1(layer(il)%rho_l  );
        call delloc_real1(layer(il)%rho_r_i); call delloc_real1(layer(il)%rho_r_l);
        call delloc_real1(layer(il)%g_i    ); call delloc_real1(layer(il)%g_l    );
        call delloc_real1(layer(il)%mass_i ); call delloc_real1(layer(il)%mass_l );
        call delloc_real1(layer(il)%mu_i   );
        call delloc_real1(layer(il)%eta_i  );
        call delloc_real1(layer(il)%K_i    );

        call alloc_real1(layer(il)%r_i    , nn); call alloc_real1(layer(il)%r_l    , nn+1);
        call alloc_real1(layer(il)%rho_i  , nn); call alloc_real1(layer(il)%rho_l  , nn+1);
        call alloc_real1(layer(il)%rho_r_i, nn); call alloc_real1(layer(il)%rho_r_l, nn+1);
        call alloc_real1(layer(il)%g_i    , nn); call alloc_real1(layer(il)%g_l    , nn+1);
        call alloc_real1(layer(il)%mass_i , nn); call alloc_real1(layer(il)%mass_l , nn+1);
        call alloc_real1(layer(il)%mu_i   , nn);
        call alloc_real1(layer(il)%eta_i  , nn);
        call alloc_real1(layer(il)%K_i    , nn);
    end do
end subroutine alloc_rads


subroutine alloc_grid
    implicit none
    
    call delloc_real2(rho_phi)
    call delloc_real1(theta)
    call delloc_real1(phi)

    call  alloc_real2(rho_phi, layer(numoflayers)%n-1, 360)
    call  alloc_real1(theta, 360)
    call  alloc_real1(phi,   360)
end subroutine alloc_grid


end module mod_allocation
