|
| 1 | +!> @brief Allocate memory and read initial condition data for IC extrusion. |
| 2 | +!> |
| 3 | +!> @details |
| 4 | +!> This macro handles the complete initialization process for IC extrusion by: |
| 5 | +!> |
| 6 | +!> **Memory Allocation:** |
| 7 | +!> - stored_values(xRows, yRows, sys_size) - stores primitive variable data from files |
| 8 | +!> - x_coords(nrows) - stores x-coordinates from input files |
| 9 | +!> - y_coords(nrows) - stores y-coordinates from input files (3D case only) |
| 10 | +!> |
| 11 | +!> **File Reading Operations:** |
| 12 | +!> - Reads primitive variable data from multiple files with pattern: |
| 13 | +!> `prim.<file_number>.00.<timestep>.dat` where timestep uses `zeros_default` padding |
| 14 | +!> - Files are read from directory specified by `init_dir` parameter |
| 15 | +!> - Supports 1D, 2D, and 3D computational domains |
| 16 | +!> |
| 17 | +!> **Grid Structure Detection:** |
| 18 | +!> - 1D/2D: Counts lines in first file to determine xRows |
| 19 | +!> - 3D: Analyzes coordinate patterns to determine xRows and yRows structure |
| 20 | +!> |
| 21 | +!> **MPI Domain Mapping:** |
| 22 | +!> - Calculates global_offset_x and global_offset_y for MPI subdomain positioning |
| 23 | +!> - Maps file coordinates to local computational grid coordinates |
| 24 | +!> |
| 25 | +!> **Data Assignment:** |
| 26 | +!> - Populates q_prim_vf primitive variable arrays with file data |
| 27 | +!> - Handles momentum component indexing with special treatment for momxe |
| 28 | +!> - Sets momxe component to zero for 2D/3D cases |
| 29 | +!> |
| 30 | +!> **State Management:** |
| 31 | +!> - Uses files_loaded flag to prevent redundant file operations |
| 32 | +!> - Preserves data across multiple macro calls within same simulation |
| 33 | +!> |
| 34 | +!> @note File pattern uses `zeros_default` parameter (default: "000000") for timestep padding |
| 35 | +!> @note Directory path is hardcoded in `init_dir` parameter - modify as needed |
| 36 | +!> @warning Aborts execution if file reading errors occur. |
| 37 | + |
| 38 | +#:def HardcodedDimensionsExtrusion() |
| 39 | + integer :: xRows, yRows, nRows, iix, iiy, max_files |
| 40 | + integer :: f, iter, ios, ios2, unit, unit2, idx, idy, index_x, index_y, jump, line_count, ycount |
| 41 | + real(wp) :: x_len, x_step, y_len, y_step |
| 42 | + real(wp) :: dummy_x, dummy_y, dummy_z, x0, y0 |
| 43 | + integer :: global_offset_x, global_offset_y ! MPI subdomain offset |
| 44 | + real(wp) :: delta_x, delta_y |
| 45 | + character(len=100), dimension(sys_size) :: fileNames ! Arrays to store all data from files |
| 46 | + character(len=200) :: errmsg |
| 47 | + real(wp), allocatable :: stored_values(:, :, :) |
| 48 | + real(wp), allocatable :: x_coords(:), y_coords(:) |
| 49 | + logical :: files_loaded = .false. |
| 50 | + real(wp) :: domain_xstart, domain_xend, domain_ystart, domain_yend |
| 51 | + character(len=*), parameter :: init_dir = "/home/MFC/FilesDirectory" ! For example /home/MFC/examples/1D_Shock/D/ |
| 52 | + character(len=20) :: file_num_str ! For storing the file number as a string |
| 53 | + character(len=20) :: zeros_part ! For the trailing zeros part |
| 54 | + character(len=6), parameter :: zeros_default = "000000" ! Default zeros (can be changed) |
| 55 | +#:enddef |
| 56 | + |
| 57 | +#:def HardcodedReadValues() |
| 58 | + |
| 59 | + if (.not. files_loaded) then |
| 60 | + max_files = merge(sys_size, sys_size - 1, num_dims == 1) |
| 61 | + do f = 1, max_files |
| 62 | + write (file_num_str, '(I0)') f |
| 63 | + fileNames(f) = trim(init_dir)//"prim."//trim(file_num_str)//".00."//zeros_default//".dat" |
| 64 | + end do |
| 65 | + |
| 66 | + ! Common file reading setup |
| 67 | + open (newunit=unit2, file=trim(fileNames(1)), status='old', action='read', iostat=ios2) |
| 68 | + if (ios2 /= 0) call s_mpi_abort("Error opening file: "//trim(fileNames(1))) |
| 69 | + |
| 70 | + select case (num_dims) |
| 71 | + case (1, 2) ! 1D and 2D cases are similar |
| 72 | + ! Count lines |
| 73 | + line_count = 0 |
| 74 | + do |
| 75 | + read (unit2, *, iostat=ios2) dummy_x, dummy_y |
| 76 | + if (ios2 /= 0) exit |
| 77 | + line_count = line_count + 1 |
| 78 | + end do |
| 79 | + close (unit2) |
| 80 | + |
| 81 | + xRows = line_count |
| 82 | + yRows = 1 |
| 83 | + index_x = 0 |
| 84 | + if (num_dims == 2) index_x = i |
| 85 | + @:ALLOCATE (x_coords(xRows), stored_values(xRows, 1, sys_size)) |
| 86 | + |
| 87 | + ! Read data from all files |
| 88 | + do f = 1, max_files |
| 89 | + open (newunit=unit, file=trim(fileNames(f)), status='old', action='read', iostat=ios) |
| 90 | + if (ios /= 0) call s_mpi_abort("Error opening file: "//trim(fileNames(f))) |
| 91 | + |
| 92 | + do iter = 1, xRows |
| 93 | + read (unit, *, iostat=ios) x_coords(iter), stored_values(iter, 1, f) |
| 94 | + if (ios /= 0) call s_mpi_abort("Error reading file: "//trim(fileNames(f))) |
| 95 | + end do |
| 96 | + close (unit) |
| 97 | + end do |
| 98 | + |
| 99 | + ! Calculate offsets |
| 100 | + domain_xstart = x_coords(1) |
| 101 | + x_step = x_cc(1) - x_cc(0) |
| 102 | + delta_x = merge(x_cc(0) - domain_xstart + x_step/2.0, & |
| 103 | + x_cc(index_x) - domain_xstart + x_step/2.0, num_dims == 1) |
| 104 | + global_offset_x = nint(abs(delta_x)/x_step) |
| 105 | + |
| 106 | + case (3) ! 3D case - determine grid structure |
| 107 | + ! Find yRows by counting rows with same x |
| 108 | + read (unit2, *, iostat=ios2) x0, y0, dummy_z |
| 109 | + if (ios2 /= 0) call s_mpi_abort("Error reading first line") |
| 110 | + |
| 111 | + yRows = 1 |
| 112 | + do |
| 113 | + read (unit2, *, iostat=ios2) dummy_x, dummy_y, dummy_z |
| 114 | + if (ios2 /= 0) exit |
| 115 | + if (dummy_x == x0 .and. dummy_y /= y0) then |
| 116 | + yRows = yRows + 1 |
| 117 | + else |
| 118 | + exit |
| 119 | + end if |
| 120 | + end do |
| 121 | + close (unit2) |
| 122 | + |
| 123 | + ! Count total rows |
| 124 | + open (newunit=unit2, file=trim(fileNames(1)), status='old', action='read', iostat=ios2) |
| 125 | + nrows = 0 |
| 126 | + do |
| 127 | + read (unit2, *, iostat=ios2) dummy_x, dummy_y, dummy_z |
| 128 | + if (ios2 /= 0) exit |
| 129 | + nrows = nrows + 1 |
| 130 | + end do |
| 131 | + close (unit2) |
| 132 | + |
| 133 | + xRows = nrows/yRows |
| 134 | + @:ALLOCATE (x_coords(nrows), y_coords(nrows), stored_values(xRows, yRows, sys_size)) |
| 135 | + index_x = i |
| 136 | + index_y = j |
| 137 | + |
| 138 | + ! Read all files |
| 139 | + do f = 1, max_files |
| 140 | + open (newunit=unit, file=trim(fileNames(f)), status='old', action='read', iostat=ios) |
| 141 | + if (ios /= 0) then |
| 142 | + if (f == 1) call s_mpi_abort("Error opening file: "//trim(fileNames(f))) |
| 143 | + cycle |
| 144 | + end if |
| 145 | + |
| 146 | + iter = 0 |
| 147 | + do iix = 1, xRows |
| 148 | + do iiy = 1, yRows |
| 149 | + iter = iter + 1 |
| 150 | + if (f == 1) then |
| 151 | + read (unit, *, iostat=ios) x_coords(iter), y_coords(iter), stored_values(iix, iiy, f) |
| 152 | + else |
| 153 | + read (unit, *, iostat=ios) dummy_x, dummy_y, stored_values(iix, iiy, f) |
| 154 | + end if |
| 155 | + if (ios /= 0) call s_mpi_abort("Error reading data") |
| 156 | + end do |
| 157 | + end do |
| 158 | + close (unit) |
| 159 | + end do |
| 160 | + |
| 161 | + ! Calculate offsets |
| 162 | + x_step = x_cc(1) - x_cc(0) |
| 163 | + y_step = y_cc(1) - y_cc(0) |
| 164 | + delta_x = x_cc(index_x) - x_coords(1) + x_step/2.0_wp |
| 165 | + delta_y = y_cc(index_y) - y_coords(1) + y_step/2.0_wp |
| 166 | + global_offset_x = nint(abs(delta_x)/x_step) |
| 167 | + global_offset_y = nint(abs(delta_y)/y_step) |
| 168 | + end select |
| 169 | + |
| 170 | + files_loaded = .true. |
| 171 | + end if |
| 172 | + |
| 173 | + ! Data assignment |
| 174 | + select case (num_dims) |
| 175 | + case (1) |
| 176 | + idx = i + 1 + global_offset_x |
| 177 | + do f = 1, sys_size |
| 178 | + q_prim_vf(f)%sf(i, 0, 0) = stored_values(idx, 1, f) |
| 179 | + end do |
| 180 | + |
| 181 | + case (2) |
| 182 | + idx = i + 1 + global_offset_x - index_x |
| 183 | + do f = 1, sys_size - 1 |
| 184 | + jump = merge(1, 0, f >= momxe) |
| 185 | + q_prim_vf(f + jump)%sf(i, j, 0) = stored_values(idx, 1, f) |
| 186 | + end do |
| 187 | + q_prim_vf(momxe)%sf(i, j, 0) = 0.0_wp |
| 188 | + |
| 189 | + case (3) |
| 190 | + idx = i + 1 + global_offset_x - index_x |
| 191 | + idy = j + 1 + global_offset_y - index_y |
| 192 | + do f = 1, sys_size - 1 |
| 193 | + jump = merge(1, 0, f >= momxe) |
| 194 | + q_prim_vf(f + jump)%sf(i, j, k) = stored_values(idx, idy, f) |
| 195 | + end do |
| 196 | + q_prim_vf(momxe)%sf(i, j, k) = 0.0_wp |
| 197 | + end select |
| 198 | +#:enddef |
| 199 | + |
| 200 | +#:def HardcodedDellacation() |
| 201 | + if (allocated(stored_values)) then |
| 202 | + @:DEALLOCATE (stored_values) |
| 203 | + @:DEALLOCATE (x_coords) |
| 204 | + end if |
| 205 | + |
| 206 | + if (allocated(y_coords)) then |
| 207 | + @:DEALLOCATE (y_coords) |
| 208 | + end if |
| 209 | +#:enddef |
0 commit comments