# [Fortran90] how to write increment in dimension

• Fortran
• s_hy
In summary, the conversation revolves around a person seeking help with writing code for 2D FDTD polar coordinates in Fortran 90. They have encountered a problem with handling increments in real numbers, specifically for the variables r and p. The solution involves using do loops to initialize r and p with equally spaced values. The conversation also includes a discussion about the correct starting and ending values for the loops and how to properly use the implied do loop.
s_hy
hi all,

i am new with fortran 90. i have here codes for 2d fdtd polar coordinates. but i have problem handling with increment in dimension in real number (as higlighted below). I have googled how to write the increment in do loops, but do loops only applicable for integer. can anyone help me how to solve this.

thank you

Code:
!2d polar coordinates
!reference: Numerical electromagnetic: fdtd method (Umran S Inan, R Marshall)

subroutine test3
implicit none

double precision                   :: f0,miu0,eps0
double precision                   :: delta, dt,dr,dp
double precision                   :: vp,lam0,rph,rmh           !lam0 = lambda
integer                                 :: pp
integer                                 :: rr,m,n
integer                                 :: t
integer                                 :: i,j
double precision,dimension(202,202) :: x,y
double precision,dimension(202,202) :: Ez,Hp,Hr    !Ez,Hy,Hx
double precision                   :: Ein
double precision, parameter  :: pi = 3.14159265
double precision                   :: r
double precision                   :: p
character(len=20)                 :: filename

f0   = 30.e9
miu0 = 4.0*pi*1.0e-7
eps0 = 8.854e-12
vp   = 1/sqrt(eps0*miu0)
lam0 = vp/f0
dr   = 0.05*lam0
dt   = dr/vp

! space grid 201x201
rr = 201
[COLOR="Blue"] r = ,dr*(rr-1),dr[/COLOR]

!define deltaphi so that there are 200 points in 2pi rad

[COLOR="Blue"] p = 1,2*pi-dp,dp[/COLOR]
pp = size(p)

!initialize Ez,Hp,Hr to zero at t=0
do m = rr,pp
do n = rr,pp
Hp(m,n) = 0
Hr(m,n) = 0
Ez(m,n) = 0
end do
end do

! coodinate transformation
do m = rr,pp
do n = rr,pp
x(m,n) = 0.0
y(m,n) = 0.0
end do
end do

do m = 1,rr
do n = 1,pp
x(m,n) = r(m)*cos(p(n))
y(m,n) = r(m)*sin(p(n))
end do
end do

!timesteps
do t = 1,200

write (filename, "('data',I3.3,'.dat')") n
open (unit=130,file=filename)

!initiate sinusoidal wavepulse at center
!  Ein  = 0.0
!  Ein(t) = sin(2*pi*f0*t*dt)
Ez(1,1) = sin(2*pi*f0*t*dt)
!print *, 'Ein(t)=',Ein(t)

!update magnetic field equation for Hr
do m = 2,rr
do n = 1,pp-1
Hr(m,n) = Hr(m,n) - ((dt/miu0/r(m)/dp)*(Ez(m,n+1) - Ez(m,n)))
!print *,'i=',i,'j=',j,'Hr(i,j)=',Hr(i,j)
end do
end do

! correction for 2pi continuity
do m = 2,rr
Hr(m,pp) = Hr(m,pp) - ((dt/miu0/r(m)/dp)*(Ez(m,1) - Ez(m,pp)))
end do

!update magnetic field for Hp
do m = 1,rr-1
do n = 1,pp
Hp(m,n) = Hp(m,n) + ((dt/miu0/dr)*(Ez(m+1,n) - Ez(m,n)))
!print *,'m=',m,'n=',n,'Hp(m,n)=',Hp(m,n)
end do
end do

!fix end point where phi = 2pi
do m = 2,rr
Ez(m,1) = Ez(m,1)+(dt/eps0/r(m)/dr)*(rph*Hp(m,1)-rmh*Hp(m-1,1))&
-((dt/eps0/r(m)/dp)*(Hr(m,1)-Hr(m,pp)))
end do

!update electric field equation
do m = 2,rr
do n = 2,pp
rph = r(m)+dr/2
rmh = r(m)-dr/2
Ez(m,n) = Ez(m,n)+(dt/eps0/r(m)/dr)*(rph*Hp(m,n)-rmh*Hp(m-1,n))&
-(dt/eps0/r(m)/dp*(Hr(m,n)-Hr(m,n-1)))
!    write (130,*) m,n,Ez(m,n)
!    if (m == 200) write (130,*) ' '
end do
end do

close (unit=130)

end do !n

end SUBROUTINE test3

Your assignment statements in blue for r = and p = don't make sense in Fortran.

If you want to create a series of real values for r and p which are equally spaced,
then define r and p as real arrays. You already know the size of the array you will need given the
number of points you want to create in the interval. Arrays in F90 can be dimensioned in a flexible fashion

See: http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/F90-Array.pdf

You can also use the implied do loop to initialize the values of your arrays.

actually i have MATLAB code and trying to write in fortran. however i have problem with the interval part for r and p. however, for r i have write it id do loops
Code:
!increment r = 1,dr*(rr-1),dr
do ir = 1,rr-1
r(ir) = (ir-1)*dr
!print *, 'ir=',ir,'r=',r
end do

and it is work. but i have problem with p, which is for p the last value should be 2*pi-dp, with the intervalor increment is dp...and size for p is 200. ( p=0:2*pi-dp:dp). can anyone teach me how to write this increment using do loop.

thank you

Code:
n  = 200
dp = 2*pi/n
do ip = 0,n-1
p = ip*dp
.
.
.
end do

By the way, your loop in the previous post seems short one extra dr...by having ir go from 1 to (rr-1) and additionally multiplying dr times (ir-1)...you have one extra -1; also, you may not want to go from 1, but 0 as my example shows to go from 0 to 2*pi-dp

1 person
thank you gsal. it's work perfectly.

Last edited:

## 1. How do I increment a value in a Fortran90 array dimension?

To increment a value in a Fortran90 array dimension, you can use the "do" loop structure. This allows you to loop through the array and increment the desired value by a specified amount. For example, you can use the syntax "do i = 1, n, 1" to loop through the array from index 1 to n and increment the desired value by 1 each time.

## 2. Can I use the "do" loop structure to increment a value by a non-integer amount?

Yes, you can use the "do" loop structure to increment a value by a non-integer amount. You can specify the desired increment amount in the "do" loop syntax, such as "do i = 1, n, 0.5" to increment the value by 0.5 each time. However, keep in mind that this may affect the precision of your calculations.

## 3. Is there a built-in function for incrementing a value in a Fortran90 array dimension?

No, there is not a built-in function specifically for incrementing a value in a Fortran90 array dimension. However, you can use the "do" loop structure or other programming techniques to achieve the desired result.

## 4. How do I increment a value in a specific dimension of a multi-dimensional Fortran90 array?

To increment a value in a specific dimension of a multi-dimensional Fortran90 array, you can use nested "do" loops. This allows you to specify which dimension you want to increment while looping through the other dimensions.

## 5. Can I use the "do" loop structure to increment multiple values in a Fortran90 array dimension simultaneously?

Yes, you can use the "do" loop structure to increment multiple values in a Fortran90 array dimension simultaneously. You can use the same "do" loop to loop through multiple indices and increment the values at those indices by the desired amount.

• Programming and Computer Science
Replies
4
Views
3K
• Programming and Computer Science
Replies
4
Views
3K
• Programming and Computer Science
Replies
5
Views
2K
• Programming and Computer Science
Replies
2
Views
4K
• Programming and Computer Science
Replies
4
Views
1K
• Programming and Computer Science
Replies
8
Views
1K
• Programming and Computer Science
Replies
4
Views
2K
• Programming and Computer Science
Replies
26
Views
3K
• Programming and Computer Science
Replies
8
Views
3K
• Programming and Computer Science
Replies
1
Views
4K