loadmesa Subroutine

public subroutine loadmesa(dirname, ifirst, ilast)

Load profiles of MESA run directory into data.

profiles.index maps model numbers ifirst to ilast to profiles, at least 2 in range. Each profile becomes one record (model number, age, mass and radius grid, zones from mixing_type, energy layers from eps_nuc and eps_nuc_neu_total), parsed concurrently w/ OpenMP by at most MESA_MAX_THREADS threads; field ranges and layers then built serially, then sort_models.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: dirname

Path of MESA run directory.

integer(kind=int32), intent(in) :: ifirst

First model number to load.

integer(kind=int32), intent(in) :: ilast

Last model number to load.


Source Code

   subroutine loadmesa(dirname, ifirst, ilast)
      !! Load profiles of MESA run directory into `data`.
      !!
      !! `profiles.index` maps model numbers `ifirst` to `ilast` to profiles, at least 2 in range.
      !! Each profile becomes one record (model number, age, mass and radius grid, zones from
      !! `mixing_type`, energy layers from `eps_nuc` and `eps_nuc_neu_total`), parsed concurrently
      !! w/ OpenMP by at most `MESA_MAX_THREADS` threads; field ranges and layers then built
      !! serially, then `sort_models`.
      ! Arguments
      character(len=*), intent(in) :: dirname
         !! Path of MESA run directory.
      integer(int32), intent(in) :: ifirst
         !! First model number to load.
      integer(int32), intent(in) :: ilast
         !! Last model number to load.

      integer(int32) :: iunit, iostat, nidx, i, nkeep, m, prio, pf, nthreads
      integer(int32) :: nf, f, k
      integer(int32), allocatable :: mdl(:), prof(:), fld_col(:)
      character(len=256) :: idxfile
      type(rawset), allocatable :: rf(:)

      idxfile = trim(dirname)//'/profiles.index'
      open (NEWUNIT=iunit, FILE=trim(idxfile), STATUS='OLD', &
            ACTION='READ', IOSTAT=iostat)
      if (iostat /= 0) &
         error stop '[loadmesa] cannot open profiles.index'

      ! line 1 holds the model count; the rest are model / priority / profile
      read (iunit, *, IOSTAT=iostat) nidx
      if (iostat /= 0) &
         error stop '[loadmesa] cannot read profiles.index header'

      allocate (mdl(nidx), prof(nidx))
      nkeep = 0
      do i = 1, nidx
         read (iunit, *, IOSTAT=iostat) m, prio, pf
         if (iostat /= 0) exit
         if (m < ifirst .or. m > ilast) cycle
         nkeep = nkeep + 1
         mdl(nkeep) = m
         prof(nkeep) = pf
      end do
      close (iunit)

      if (nkeep < 2) &
         error stop '[loadmesa] need at least 2 profiles in range'

      if (allocated(data)) deallocate (data)
      allocate (data(nkeep))
      allocate (rf(nkeep))

      ! register the colour fields present in this run from the first profile's
      ! body header, so every read_profile call fills the same field slots
      call register_mesa_fields(dirname, prof(1), fld_col, nf)

      ! each profile parses into its own data(i) slot with no shared state, so
      ! read them concurrently. schedule(dynamic) balances the uneven per-file
      ! cost (profiles differ in zone count). read_profile uses NEWUNIT units
      ! and only local storage, so it is thread-safe as written
      nthreads = 1
!$    nthreads = max(1, min(omp_get_max_threads(), MESA_MAX_THREADS))
      !$omp parallel do default(shared) private(i) schedule(dynamic) &
      !$omp num_threads(nthreads)
      do i = 1, nkeep
         call read_profile(dirname, prof(i), data(i), fld_col, nf, rf(i))
         data(i)%ncyc = mdl(i)     ! index mapping is authoritative
      end do
      !$omp end parallel do
      deallocate (mdl, prof)

      ! range over the whole sequence, then quantize each profile's raw field
      ! columns into its record's fld layers (serial: the reduction and the
      ! shared registry are not thread-safe)
      do i = 1, nkeep
         do f = 1, nf
            do k = 1, size(rf(i)%v, 1)
               call update_field_range(f, rf(i)%v(k, f))
            end do
         end do
      end do
      do f = 1, nf
         call finalize_field_log(f)
      end do
      do i = 1, nkeep
         data(i)%nfld = nf
         if (nf > 0) allocate (data(i)%fld(2, nf))
         do f = 1, nf
            call build_field_layer(rf(i)%v(:, f), size(rf(i)%v, 1), f, &
                                   data(i)%fld(:, f))
         end do
      end do
      deallocate (rf, fld_col)

      call sort_models()
      print *, '[loadmesa] Loaded models', data(1)%ncyc, ' - ', &
         data(size(data))%ncyc
   end subroutine loadmesa