read_profile Subroutine

private subroutine read_profile(dirname, pnum, cnv, fld_col, nf, rf)

Parse profile<pnum>.data into one record.

Header needs model_number, num_zones, star_age, star_mass; body needs mass, logR, mixing_type, eps_nuc (drives nuc layer) and eps_nuc_neu_total (drives neu layer). Raw colour-field columns go into rf, centre to surface, for caller to bin.

Arguments

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

Path of MESA run directory.

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

Profile number.

type(convtype), intent(out) :: cnv

Record to build.

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

Body column of each registered field.

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

Number of registered fields.

type(rawset), intent(out) :: rf

Colour-field values of profile.


Source Code

   subroutine read_profile(dirname, pnum, cnv, fld_col, nf, rf)
      !! Parse `profile<pnum>.data` into one record.
      !!
      !! Header needs `model_number`, `num_zones`, `star_age`, `star_mass`; body needs `mass`,
      !! `logR`, `mixing_type`, `eps_nuc` (drives `nuc` layer) and `eps_nuc_neu_total` (drives `neu`
      !! layer). Raw colour-field columns go into `rf`, centre to surface, for caller to bin.
      ! Arguments
      character(len=*), intent(in) :: dirname
         !! Path of MESA run directory.
      integer(int32), intent(in) :: pnum
         !! Profile number.
      integer(int32), intent(in) :: nf
         !! Number of registered fields.
      integer(int32), intent(in) :: fld_col(:)
         !! Body column of each registered field.
      type(convtype), intent(out) :: cnv
         !! Record to build.
      type(rawset), intent(out) :: rf
         !! Colour-field values of profile.

      integer(int32) :: iunit, iostat, r, src, nz, k, f
      integer(int32) :: maxh, maxb
      integer(int32) :: c_model, c_age, c_mass_h, c_nz
      integer(int32) :: c_mass, c_logr, c_mix, c_eps, c_neu
      character(len=256) :: fname
      character(len=8192) :: hnames, bnames, hvals
      real(real64), allocatable :: hv(:), row(:)
      real(real64), allocatable :: massf(:), logrf(:), epsf(:), enuf(:)
      real(real64), allocatable :: fldf(:, :)
      integer(int32), allocatable :: mtf(:)
      real(real64), allocatable :: xm(:), rn(:), eps(:), enu(:)
      integer(int32), allocatable :: mt(:)

      fname = trim(dirname)//'/profile'//trim(itoa(pnum))//'.data'
      open (NEWUNIT=iunit, FILE=trim(fname), STATUS='OLD', &
            ACTION='READ', IOSTAT=iostat)
      if (iostat /= 0) &
         error stop '[read_profile] cannot open '//trim(fname)

      ! header block: skip col-index line, read names + values
      read (iunit, '(a)') hnames        ! line 1 (col indices), reused as scratch
      read (iunit, '(a)') hnames        ! line 2 header names
      read (iunit, '(a)') hvals         ! line 3 header values
      read (iunit, '(a)') bnames        ! line 4 blank, reused as scratch
      read (iunit, '(a)') bnames        ! line 5 body col indices, reused
      read (iunit, '(a)') bnames        ! line 6 body names

      c_model = col_index(hnames, 'model_number')
      c_nz = col_index(hnames, 'num_zones')
      c_age = col_index(hnames, 'star_age')
      c_mass_h = col_index(hnames, 'star_mass')
      if (min(c_model, c_nz, c_age, c_mass_h) < 1) &
         error stop '[read_profile] missing header column'

      c_mass = col_index(bnames, 'mass')
      c_logr = col_index(bnames, 'logR')
      c_mix = col_index(bnames, 'mixing_type')
      c_eps = col_index(bnames, 'eps_nuc')
      c_neu = col_index(bnames, 'eps_nuc_neu_total')
      if (min(c_mass, c_logr, c_mix, c_eps, c_neu) < 1) &
         error stop '[read_profile] missing body column'

      ! read only up to the highest needed column; the header value line has
      ! trailing string columns (version, compiler) a numeric read would choke
      ! on, but they sit past every field we want
      maxh = max(c_model, c_nz, c_age, c_mass_h)
      allocate (hv(maxh))
      read (hvals, *) hv(1:maxh)

      nz = nint(hv(c_nz))
      cnv%ncyc = nint(hv(c_model))
      cnv%timesec = hv(c_age)*YR

      ! body rows run surface -> center; read them then reverse to the
      ! center -> surface ascending order the renderer expects
      maxb = max(c_mass, c_logr, c_mix, c_eps, c_neu)
      do f = 1, nf
         maxb = max(maxb, fld_col(f))
      end do
      allocate (row(maxb))
      allocate (massf(nz), logrf(nz), mtf(nz), epsf(nz), enuf(nz))
      allocate (fldf(nz, nf))
      do r = 1, nz
         read (iunit, *, IOSTAT=iostat) row(1:maxb)
         if (iostat /= 0) &
            error stop '[read_profile] short profile body'
         massf(r) = row(c_mass)
         logrf(r) = row(c_logr)
         mtf(r) = nint(row(c_mix))
         epsf(r) = row(c_eps)
         enuf(r) = row(c_neu)
         do f = 1, nf
            fldf(r, f) = row(fld_col(f))
         end do
      end do
      close (iunit)

      allocate (xm(nz), rn(nz), mt(nz), eps(nz), enu(nz))
      allocate (rf%v(nz, nf))
      do k = 1, nz
         src = nz - k + 1
         xm(k) = massf(src)*SOLMASS
         rn(k) = 10.d0**logrf(src)*SOLRAD
         mt(k) = mtf(src)
         eps(k) = epsf(src)
         enu(k) = enuf(src)
         do f = 1, nf
            rf%v(k, f) = fldf(src, f)
         end do
      end do
      deallocate (massf, logrf, mtf, epsf, enuf, hv, row, fldf)

      cnv%nvers = REC_NVERS
      cnv%ncoord = nz
      cnv%dt = 0.d0
      cnv%toffset = 0.d0
      cnv%idx_kind_len = idx_kind_len
      cnv%nuc_kind_len = nuc_kind_len
      cnv%ladv = 0
      cnv%nadv = 0
      cnv%levcnv = 0
      cnv%abun = 0.d0
      cnv%aw = 0.d0
      cnv%angltv = 0.d0

      cnv%xmcoord = xm
      cnv%rncoord = rn

      call build_mesa_zones(mt, nz, cnv)
      call build_energy(eps, nz, cnv%nnuc, cnv%nuc, cnv%inuc)
      call build_energy(enu, nz, cnv%nneu, cnv%neu, cnv%ineu)

      ! layer minima drive the colorbar labels (kipp level_mins): layer 0 nuc
      ! uses minloss as the gain-min log value / mingain as the loss-min;
      ! layer 2 neu uses minneul / minneug
      cnv%minloss = EPS_BASE
      cnv%mingain = EPS_BASE
      cnv%minnucl = EPS_BASE
      cnv%minnucg = EPS_BASE
      cnv%minneul = EPS_BASE
      cnv%minneug = EPS_BASE
      cnv%minlossd = 0; cnv%mingaind = 0
      cnv%minnucld = 0; cnv%minnucgd = 0
      cnv%minneuld = 0; cnv%minneugd = 0

      ! layer 1 (nuk) and all derivative / advection arrays stay empty
      call alloc_empty_layers(cnv)

      deallocate (xm, rn, mt, eps, enu)
   end subroutine read_profile