Skip to content

Commit 7bb8ed3

Browse files
Extend initial conditions from 1D to 2D and 2D to 3D (#844)
Co-authored-by: Spencer Bryngelson <sbryngelson@gmail.com>
1 parent 59d0c09 commit 7bb8ed3

File tree

7 files changed

+249
-15
lines changed

7 files changed

+249
-15
lines changed

docs/documentation/case.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ and use `patch_icpp(i)%%geometry = 7` and `patch_icpp(i)%%hcid = 200` in the inp
236236
Additional variables can be declared in `Hardcoded1[2,3]DVariables` and used in `hardcoded1[2,3]D`.
237237
As a convention, any hard coded patches that are part of the MFC master branch should be identified as 1[2,3]xx where the first digit indicates the number of dimensions.
238238

239+
The code provides three pre-built patches for dimensional extrusion of initial conditions:
240+
241+
- `case(170)`: Load 1D profile from data files
242+
- `case(270)`: Extrude 1D data to 2D domain
243+
- `case(370)`: Extrude 2D data to 3D domain
244+
245+
Setup: Only requires specifying `init_dir` and filename pattern via `zeros_default`. Grid dimensions are automatically detected from the data files.
246+
Implementation: All variables and file handling are managed in `src/pre_process/include/ExtrusionHardcodedIC.fpp` with no manual grid configuration needed.
247+
Usage: Ideal for initializing simulations from lower-dimensional solutions, enabling users to add perturbations or modifications to the base extruded fields for flow instability studies.
248+
239249
#### Parameter Descriptions
240250

241251
- `num_patches` defines the total number of patches defined in the domain.

src/pre_process/include/1dHardcodedIC.fpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#:def Hardcoded1DVariables()
22
! Place any declaration of intermediate variables here
3-
43
#:enddef
54

65
#:def Hardcoded1D()
76

87
select case (patch_icpp(patch_id)%hcid)
9-
case (100)
10-
! Put your variable assignments here
8+
case (170)
9+
! This hardcoded case can be used to start a simulation with initial conditions given from a known 1D profile (e.g. Cantera, SDtoolbox)
10+
@: HardcodedReadValues()
11+
1112
case default
1213
call s_int_to_str(patch_id, iStr)
1314
call s_mpi_abort("Invalid hcid specified for patch "//trim(iStr))

src/pre_process/include/2dHardcodedIC.fpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#:def Hardcoded2DVariables()
2-
2+
! Place any declaration of intermediate variables here
33
real(wp) :: eps
44
real(wp) :: r, rmax, gam, umax, p0
55
real(wp) :: rhoH, rhoL, pRef, pInt, h, lam, wl, amp, intH, intL, alph
66
real(wp) :: factor
7-
7+
88
eps = 1.e-9_wp
9-
109
#:enddef
1110

1211
#:def Hardcoded2D()
@@ -158,6 +157,10 @@
158157
q_prim_vf(E_idx)%sf(i, j, 0) = 3.e-5_wp
159158
end if
160159

160+
case (270)
161+
! This hardcoded case extrudes a 1D profile to initialize a 2D simulation domain
162+
@: HardcodedReadValues()
163+
161164
case default
162165
if (proc_rank == 0) then
163166
call s_int_to_str(patch_id, iStr)

src/pre_process/include/3dHardcodedIC.fpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#:def Hardcoded3DVariables()
22
! Place any declaration of intermediate variables here
3-
43
real(wp) :: rhoH, rhoL, pRef, pInt, h, lam, wl, amp, intH, alph
54

65
real(wp) :: eps
@@ -56,7 +55,10 @@
5655
q_prim_vf(advxe)%sf(i, j, k) = patch_icpp(1)%alpha(2)
5756
end if
5857

59-
! Put your variable assignments here
58+
case (370)
59+
! This hardcoded case extrudes a 2D profile to initialize a 3D simulation domain
60+
@: HardcodedReadValues()
61+
6062
case default
6163
call s_int_to_str(patch_id, iStr)
6264
call s_mpi_abort("Invalid hcid specified for patch "//trim(iStr))
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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

src/pre_process/m_patches.fpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
!! @brief Contains module m_patches
44

55
#:include 'case.fpp'
6+
#:include 'ExtrusionHardcodedIC.fpp'
67
#:include '1dHardcodedIC.fpp'
78
#:include '2dHardcodedIC.fpp'
89
#:include '3dHardcodedIC.fpp'
@@ -1296,15 +1297,18 @@ contains
12961297
type(scalar_field), dimension(1:sys_size), intent(inout) :: q_prim_vf
12971298

12981299
! Generic loop iterators
1299-
integer :: i
1300+
integer :: i, j, k
1301+
13001302
! Placeholders for the cell boundary values
13011303
real(wp) :: pi_inf, gamma, lit_gamma
1302-
1304+
@:HardcodedDimensionsExtrusion()
13031305
@:Hardcoded1DVariables()
13041306

13051307
pi_inf = fluid_pp(1)%pi_inf
13061308
gamma = fluid_pp(1)%gamma
13071309
lit_gamma = (1._wp + gamma)/gamma
1310+
j = 0.0_wp
1311+
k = 0.0_wp
13081312

13091313
! Transferring the patch's centroid and length information
13101314
x_centroid = patch_icpp(patch_id)%x_centroid
@@ -1341,6 +1345,8 @@ contains
13411345
end if
13421346
end do
13431347

1348+
@:HardcodedDellacation()
1349+
13441350
end subroutine s_1D_analytical
13451351

13461352
!! @param patch_id is the patch identifier
@@ -1409,17 +1415,19 @@ contains
14091415
integer, dimension(0:m, 0:n, 0:p), intent(inout) :: patch_id_fp
14101416
type(scalar_field), dimension(1:sys_size), intent(inout) :: q_prim_vf
14111417

1412-
integer :: i, j !< generic loop iterators
1418+
integer :: i, j, k !< generic loop iterators
14131419

14141420
real(wp) :: pi_inf, gamma, lit_gamma !< equation of state parameters
14151421
real(wp) :: l, U0 !< Taylor Green Vortex parameters
1416-
1422+
@:HardcodedDimensionsExtrusion()
14171423
@:Hardcoded2DVariables()
14181424

14191425
pi_inf = fluid_pp(1)%pi_inf
14201426
gamma = fluid_pp(1)%gamma
14211427
lit_gamma = (1._wp + gamma)/gamma
14221428

1429+
k = 0.0_wp
1430+
14231431
! Transferring the patch's centroid and length information
14241432
x_centroid = patch_icpp(patch_id)%x_centroid
14251433
y_centroid = patch_icpp(patch_id)%y_centroid
@@ -1465,6 +1473,8 @@ contains
14651473
end do
14661474
end do
14671475

1476+
@:HardcodedDellacation()
1477+
14681478
end subroutine s_2D_analytical
14691479

14701480
!> This patch assigns the primitive variables as analytical
@@ -1480,7 +1490,7 @@ contains
14801490

14811491
integer :: i, j, k !< generic loop iterators
14821492
real(wp) :: pi_inf, gamma, lit_gamma !< equation of state parameters
1483-
1493+
@:HardcodedDimensionsExtrusion()
14841494
@:Hardcoded3DVariables()
14851495

14861496
pi_inf = fluid_pp(1)%pi_inf
@@ -1549,6 +1559,7 @@ contains
15491559
end do
15501560
end do
15511561
1562+
@:HardcodedDellacation()
15521563
end subroutine s_3D_analytical
15531564
15541565
!> This patch generates the shape of the spherical harmonics

src/simulation/m_data_output.fpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,6 @@ contains
663663
if (((i >= cont_idx%beg) .and. (i <= cont_idx%end)) &
664664
.or. &
665665
((i >= adv_idx%beg) .and. (i <= adv_idx%end)) &
666-
.or. &
667-
((i >= chemxb) .and. (i <= chemxe)) &
668666
) then
669667
write (2, FMT) x_cb(j), y_cb(k), q_cons_vf(i)%sf(j, k, 0)
670668
else

0 commit comments

Comments
 (0)