load_existing_pvd Subroutine

public subroutine load_existing_pvd(params, flow, start_step)

Checks if an existing flow.pvd file exists and reads its entries to populate our steps/times.

Arguments

Type IntentOptional Attributes Name
type(case_params_t), intent(in) :: params
type(flow_mpi_t), intent(in) :: flow
integer, intent(in), optional :: start_step

Source Code

   subroutine load_existing_pvd(params, flow, start_step)
      type(case_params_t), intent(in) :: params
      type(flow_mpi_t), intent(in) :: flow
      integer, intent(in), optional :: start_step

      integer :: unit_id, ios, t_idx, f_idx, step_val, current_start_step
      real(rk) :: time_val
      character(len=path_len + 32) :: filename
      character(len=1024) :: line
      logical :: file_exists

      if (flow%rank /= 0 .or. .not. params%write_vtu) return

      current_start_step = 0
      if (present(start_step)) current_start_step = start_step

      ! Pre-allocate dynamic arrays
      if (.not. allocated(pvd_steps)) then
         allocate(pvd_steps(params%nsteps + 10))
         allocate(pvd_times(params%nsteps + 10))
         n_pvd_entries = 0
      end if

      filename = trim(params%output_dir)//'/VTK/flow.pvd'
      inquire(file=trim(filename), exist=file_exists)
      if (.not. file_exists) return

      open(newunit=unit_id, file=trim(filename), status='old', action='read', iostat=ios)
      if (ios /= 0) return

      n_pvd_entries = 0
      do
         read(unit_id, '(a)', iostat=ios) line
         if (ios /= 0) exit

         t_idx = index(line, 'timestep="')
         f_idx = index(line, 'file="flow_')

         if (t_idx > 0 .and. f_idx > 0) then
            read(line(t_idx + 10:), *, iostat=ios) time_val
            if (ios /= 0) cycle
            read(line(f_idx + 11:f_idx + 16), *, iostat=ios) step_val
            if (ios /= 0) cycle

            ! Truncate future PVD timeline on restart
            if (step_val > current_start_step .and. params%restart_from_file) cycle

            if (n_pvd_entries < params%nsteps + 10) then
               n_pvd_entries = n_pvd_entries + 1
               pvd_steps(n_pvd_entries) = step_val
               pvd_times(n_pvd_entries) = time_val
            end if
         end if
      end do
      close(unit_id)
   end subroutine load_existing_pvd