<commit_before>module mod_maps
  use mod_constants
  use mod_read_parameters
  use mod_variables

  implicit none

  integer                  :: nb_S  ! The number of points along Sigma axis
  integer                  :: nb_T  ! The number of points along T axis
  real(kind = x_precision) :: T_min ! Lowest value of Temperature range
  real(kind = x_precision) :: T_max ! Highest value of Temperature range
  real(kind = x_precision) :: S_min ! Lowest value of Sigma range
  real(kind = x_precision) :: S_max ! Highest value of Sigma range


contains

  subroutine set_conditions(nbS, nbT, Tmin, Tmax, Smin, Smax)
    implicit none

    integer, intent(in) :: nbS
    integer, intent(in) :: nbT
    real(kind = x_precision), intent(in) :: Tmin ! Lowest value of Temperature range
    real(kind = x_precision), intent(in) :: Tmax ! Highest value of Temperature range
    real(kind = x_precision), intent(in) :: Smin ! Lowest value of Sigma range
    real(kind = x_precision), intent(in) :: Smax ! Highest value of Sigma range

    nb_S = nbS
    nb_T = nbT
    T_min = Tmin
    T_max = Tmax
    S_min = Smin
    S_max = Smax

  end subroutine set_conditions

  ! Build the T, Sigma grid given the number of points along each axis and the ranges
  subroutine build_grid()
    implicit none

    type(state),              dimension(nb_T,nb_S)        :: grid    ! The states grid
    real(kind = x_precision), dimension(n_cell,nb_T,nb_S) :: Q_res   ! The Q+-Q- grids, one for each position in the disk
    real(kind = x_precision), dimension(n_cell,nb_T,nb_S) :: tau_res ! The tau grids, one for each position in the disk
    real(kind = x_precision) :: dT     ! Temperature steps in the grid
    real(kind = x_precision) :: dS     ! Sigma steps in the grid
    real(kind = x_precision) :: T_temp ! Temporary variable to store Temperature
    real(kind = x_precision) :: S_temp ! Temporary variable to store Sigma
    integer                  :: i,j,k  ! Loop counters

    ! Compute the Temperature and Sigma steps
    dT = (T_max - T_min) / (nb_T - 1)
    dS = (S_max - S_min) / (nb_S - 1)

    ! Loop over first dimension
    do i = 1, nb_T

      ! Compute the i-th values
      T_temp = dT * (i-1) + T_min
      S_temp = dS * (i-1) + S_min

      ! Store them along first/second dimension
      do k = 1, n_cell
        grid(i,1:nb_S)%T(k) = T_temp ! Does not work without (k), so put it there
        grid(1:nb_T,i)%S(k) = S_temp * x_state%x(k) ! Actually the state use S, not Sigma
      end do
    end do

    do i = 1, nb_T
      do j = 1, nb_S
        call compute_variables(grid(i,j)) ! Compute the variables in each position of the state
        do k = 1, n_cell ! For each position on the disk
          Q_res(k,i,j) = grid(i,j)%Qplus(k) - grid(i,j)%Qminus(k) ! Q+ - Q-
          tau_res(k,i,j) = grid(i,j)%tau(k) ! tau_eff
        end do
      end do
    end do

    call save_data(Q_res, tau_res)

  end subroutine build_grid

  subroutine save_data(Q_res, tau_res)
    implicit none

    real(kind = x_precision), dimension(n_cell,nb_T,nb_S), intent(in) :: Q_res   ! The Q+-Q- grids, one for each position in the disk
    real(kind = x_precision), dimension(n_cell,nb_T,nb_S), intent(in) :: tau_res ! The tau grids, one for each position in the disk

    character(len = 64) :: fname       ! Name of the output file
    character(len = 16) :: line_fmt    ! Format of lines in the output file
    character(len = 8)  :: cell_number ! Cell number, to use in filename
    integer             :: fid         ! File descriptor<commit_msg>mod_maps: reduce memory consumption (from ~4GiB to ~600MiB)<commit_after>module mod_maps
  use mod_constants
  use mod_read_parameters
  use mod_variables

  implicit none

  integer                  :: nb_S  ! The number of points along Sigma axis
  integer                  :: nb_T  ! The number of points along T axis
  real(kind = x_precision) :: T_min ! Lowest value of Temperature range
  real(kind = x_precision) :: T_max ! Highest value of Temperature range
  real(kind = x_precision) :: S_min ! Lowest value of Sigma range
  real(kind = x_precision) :: S_max ! Highest value of Sigma range


contains

  subroutine set_conditions(nbS, nbT, Tmin, Tmax, Smin, Smax)
    implicit none

    integer, intent(in) :: nbS
    integer, intent(in) :: nbT
    real(kind = x_precision), intent(in) :: Tmin ! Lowest value of Temperature range
    real(kind = x_precision), intent(in) :: Tmax ! Highest value of Temperature range
    real(kind = x_precision), intent(in) :: Smin ! Lowest value of Sigma range
    real(kind = x_precision), intent(in) :: Smax ! Highest value of Sigma range

    nb_S = nbS
    nb_T = nbT
    T_min = Tmin
    T_max = Tmax
    S_min = Smin
    S_max = Smax

  end subroutine set_conditions

  ! Build the T, Sigma grid given the number of points along each axis and the ranges
  subroutine build_grid()
    implicit none

    type(state)                                           :: s       ! The states grid
    real(kind = x_precision), dimension(n_cell,nb_T,nb_S) :: Q_res   ! The Q+-Q- grids, one for each position in the disk
    real(kind = x_precision), dimension(n_cell,nb_T,nb_S) :: tau_res ! The tau grids, one for each position in the disk
    real(kind = x_precision) :: dT     ! Temperature steps in the grid
    real(kind = x_precision) :: dS     ! Sigma steps in the grid
    real(kind = x_precision) :: T_temp ! Temporary variable to store Temperature
    real(kind = x_precision) :: S_temp ! Temporary variable to store Sigma
    integer                  :: i,j    ! Loop counters

    ! Compute the Temperature and Sigma steps
    dT = (T_max - T_min) / (nb_T - 1)
    dS = (S_max - S_min) / (nb_S - 1)

    do i = 1, nb_T
       do j = 1, nb_S
          T_temp = dT * (i-1) + T_min
          S_temp = dS * (j-1) + S_min

          s%T = T_temp
          S%S = S_temp
          
          call compute_variables(s) ! Compute the variables in each position of the state
          Q_res(:,i,j) = s%Qplus - s%Qminus
          tau_res(:,i,j) = s%tau
       end do
    end do

    call save_data(Q_res, tau_res)

  end subroutine build_grid

  subroutine save_data(Q_res, tau_res)
    implicit none

    real(kind = x_precision), dimension(n_cell,nb_T,nb_S), intent(in) :: Q_res   ! The Q+-Q- grids, one for each position in the disk
    real(kind = x_precision), dimension(n_cell,nb_T,nb_S), intent(in) :: tau_res ! The tau grids, one for each position in the disk

    character(len = 64) :: fname       ! Name of the output file
    character(len = 16) :: line_fmt    ! Format of lines in the output file
    character(len = 8)  :: cell_number ! Cell number, to use in filename
    integer             :: fid         ! File descriptor
{'fraction_non_alphanumeric': 0.09089556679559654, 'fraction_numerical': 0.0046117227015769115, 'mean_word_length': 2.624258760107817, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4685913', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2201, 'n_words': 752}