# Create Rectangular Cavity

**URL:** <https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795>\
**Category:** Help\
**Created:** [June 19, 2022, 8:44am UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795 "2022-06-19T08:44:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![1988](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/1988/32/1103_2.png) [@1988](https://fortran-lang.discourse.group/u/1988)\
**Post date:** [June 19, 2022, 8:44am UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/1 "2022-06-19T08:44:39Z")

</div>

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

```fortran
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

```

---

<div class="post-metadata">

**Author:** ![1988](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/1988/32/1103_2.png) [@1988](https://fortran-lang.discourse.group/u/1988)\
**Post date:** [June 19, 2022, 8:46am UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/2 "2022-06-19T08:46:00Z")

</div>

![Snipaste_2022-06-19_15-09-13](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/d/dd82c75b4f4c7e4696488ee655509897fe2b9c4b.png)

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [June 19, 2022, 11:59am UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/3 "2022-06-19T11:59:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![1988](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/1988/32/1103_2.png) [@1988](https://fortran-lang.discourse.group/u/1988)\
**Post date:** [June 19, 2022, 12:09pm UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/4 "2022-06-19T12:09:14Z")

</div>

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![1988](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/1988/32/1103_2.png) [@1988](https://fortran-lang.discourse.group/u/1988)\
**Post date:** [June 19, 2022, 12:10pm UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/5 "2022-06-19T12:10:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [June 19, 2022, 12:15pm UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/6 "2022-06-19T12:15:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [June 19, 2022, 12:57pm UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/7 "2022-06-19T12:57:47Z")

</div>

> [@1988](#):
>
> I tried to create the model, but it gave me one layer

maybe,

 ![r1_layers](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/5/5603803cb8f9d7b2c30eaad4a484ceb8164d624d.png)

---

<div class="post-metadata">

**Author:** ![1988](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/1988/32/1103_2.png) [@1988](https://fortran-lang.discourse.group/u/1988)\
**Post date:** [June 19, 2022, 1:41pm UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/8 "2022-06-19T13:41:26Z")

</div>

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(:,🙂 :: 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
```

---

<div class="post-metadata">

**Author:** ![1988](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/1988/32/1103_2.png) [@1988](https://fortran-lang.discourse.group/u/1988)\
**Post date:** [June 29, 2022, 2:13am UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/9 "2022-06-29T02:13:14Z")

</div>

```auto
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

```

 ![Snipaste_2022-06-29_09-54-20](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/7/7f2cbade0374868de0fae703b56ae0d83672bf17.png)

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [June 29, 2022, 3:26am UTC](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795/10 "2022-06-29T03:26:18Z")

</div>

it seems that this post is duplicated.  
See:  
[Create Rectangular Cavity](https://fortran-lang.discourse.group/t/create-rectangular-cavity/3795)
