Create Rectangular Cavity

I need to create this model as shown below by using the FORTRAN compiler 1= layer one 2= layer tow 3= cavity (rectangular cavity)

program Make_2D_Model
	implicit none
	 integer :: width,length,Area
     integer:: i,j,nx,nz 
     integer :: idepth
 character*1,allocatable,dimension(:,:) :: icode
nx=1000  
nz=500
idepth=100
width = 30
length=20
Area = 600
	allocate(icode(1:nx,1:nz))	
	do i=1,nx
		do j=1,nz
			if(j<=idepth)then
				icode(i,j)='1'
			elseif(j>idepth)then
				icode(i,j)='3'		
            endif
		enddo
    enddo

    do i=1,nx
        do j=1,nz
            if  (width*length)<=Area then
                icode(i,j)='2'
            endif
        enddo
    enddo
open(2012,file='model.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 Make_2D_Model

Well, to specify the position of a rectangle in a plane you need 4 numbers, for example the coordinates of top left point and the coordinates of the right bottom point.
I don’t see 4 numbers to specify your rectangle in the program shown.
Try to write it again assuming that you know that coordinates. Write first in a mathematical notation.

implicit none
	 integer :: a,b,c,s,p, Area
     integer:: i,j,nx,nz 
     integer :: idepth
 character*1,allocatable,dimension(:,:) :: icode
nx=1000  
nz=500
idepth=100
a=3
b=4
c=5
p=a+b+c
s=p/2
	allocate(icode(1:nx,1:nz))	
	do i=1,nx
		do j=1,nz
			if(j<=idepth)then
				icode(i,j)='1'
			elseif(j>idepth)then
				icode(i,j)='3'		
            endif
		enddo
    enddo

    do i=1,nx
        do j=1,nz
            if (s*(s-a)*(s-b)*(s-c))**0.5)<=Area) then
                icode(i,j)='2'
            endif
        enddo
    enddo
open(2012,file='model.txt',status='unknown')
		do j=1,nz
			write(2012,1000)(icode(i,j),i=1,nx)
		enddo
	close(2012)
	1000 format(<nx>a1)

I tried to create the model, but it gave me one layer

Your expression in the loop (the logical expression inside the loop) doesn’t depend on the index i, j so it is not changed from one iteration to the next one. As I told you before coding write it in mathematical notation.

2 Likes

maybe,

1 Like

program Make_2D_Model
implicit none
! integer :: a,b,c,s,p, Area
integer :: l,w,Area
integer:: i,j,nx,nz
integer :: idepth
character*1,allocatable,dimension(:,:slight_smile: :: icode
nx=1000
nz=500
idepth=100
l=7
w=12
Area = 84

allocate(icode(1:nx,1:nz))	
do i=1,nx
	do j=1,nz
		if(j<=idepth)then
			icode(i,j)='1'
		elseif(j>idepth)then
			icode(i,j)='3'		
        endif
	enddo
enddo


do i=1,nx
    do j=1,nz
        if ((i-l)*(j-w)<=Area**0.5) then
            icode(i,j)='2'
       endif
    enddo
enddo

open(2012,file=‘model.txt’,status=‘unknown’)
do j=1,nz
write(2012,1000)(icode(i,j),i=1,nx)
enddo
close(2012)
1000 format(a1)

end program Make_2D_Model
program Make_2D_Model
 implicit none
 integer:: i,j,nx,nz
    integer :: radius, cx, cy
 character*1,allocatable,dimension(:,:) :: icode
 integer :: idepth
 nx=801
 nz=401
 idepth=100
    radius=20
    cx=401
    cy=51
 allocate(icode(1:nx,1:nz)) 
 do i=1,nx
  do j=1,nz
   if(j<=idepth)then
    icode(i,j)='1'
   elseif(j>idepth)then
    icode(i,j)='2'  
            endif
  enddo
    enddo
    
    do i=1,nx
        do j=1,nz
            if(i<=600&&i>=500&&j<=-50&&j>=-60)then
                icode(i,j)='3'
            endif
        enddo
    enddo
    
 open(2012,file='model.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 Make_2D_Model

it seems that this post is duplicated.
See:
Create Rectangular Cavity