I’m working on a Fortran program to create a 2D model for near-surface topography, specifically simulating a sinusoidal topography on the second layer’s top surface. The model outputs a grid to a text file, with the first layer represented by `1`

and the second layer by `2`

. The spacing between grid points is `(nx - 1)`

. Here is the code I’ve written so far:

program Sin_Topo_Layer

```
implicit none
integer :: nx ! Number of grid points in the x direction
integer :: nz ! Number of grid points in the y direction
real :: x, z
integer :: i, j
!real :: z(ny, nx)
```

! integer :: topo(ny, nx)

!real :: x_min, x_max, z_min, z_max

!real :: dx, dz

```
real :: amplitude ! Amplitude of the sinusoidal wave
real :: wavelength ! Wavelength of the sinusoidal wave
```

character*1,allocatable,dimension(:, :: icode

! Values of parameters

```
nx = 100
nz = 100
amplitude = 5.0
wavelength = 20.0
x_min = 0.0
x_max = 10.0
z_min = 0.0
z_max = 10.0
! Compute the grid spacing
dx = (x_max - x_min) / (nx - 1)
dz = (z_max - z_min) / (nz - 1)
```

! Allocate the topography array

```
allocate(icode(1:nx,1:nz))
! Create topography
do j = 1, nz
do i = 1, nx
x = (i - 1) * wavelength / (nx - 1)
z = (j - 1) * wavelength / (nz - 1)
z(j, i) = amplitude * sin(2.0 * 3.141592653589793 * x / wavelength)
```

! Assign layers based on the sinusoidal topography

if (y > z(j, i)) then

topo(j, i) = 1 ! First layer

else

topo(j, i) = 2 ! Second layer

end if

end do

end do

```
! Write the topography to the output file
open(2012,file='Sin_Topo_Layer.txt',status='unknown')
do j=1,nz
write(2012,1000)(icode(i,j),i=1,nx)
enddo
close(2012)
1000 format(<nx>a1)
! Generate the topography using sin fuction
!do j = 1, nz
! z = z_min + (j - 1) * dz
! do i = 1, nx
! x = x_min + (i - 1) * dx
! topo(i, j) = sin(x) * cos(z)
!end do
!end do
! Output the data to a text file
! do j = 1, nz
! z = z_min + (j - 1) * dz
! do i = 1, nx
! x = x_min + (i - 1) * dx
! if (j <= nz / 2) then
! topo(i, j) = 1 ! First layer
! else
! topo(i, j) = 2 ! Second layer
! end if
! end do
```

! end do

! deallocate(topo)’

```
!open(2012,file='Sin_Topo_Layer.txt',status='unknown')
! do j=1,nz
! write(2012,1000)(icode(i,j),i=1,nx)
! enddo
!close(2012)
!1000 format(<nx>a1)
end program Sin_Topo_Layer
```