In the following algorithm I set up a do loop in such a way to perform some operations for each value of the variable domega. The problem of my code is that it misses the last value of domega, that should be equal to tau2.
Can you tell me how to solve this issue?
In matlab I would write an array of domega like:
domega = first_value:step:last_value
and then I would perform a for loop on the values of domega array.
Can you show how to modify my code to get a do loop on domega, by starting from first value up to last value with the indicated step size?
domega = tau1 ! first value
if(tau2 < 0.d0) tau2 = tau2 + twopi ! tau2 is the last value
delta = 4.617207817725326D-003 ! Interval step size
n_domega = 1 + ((tau2 - tau1)/delta) ! numbers of values within interval
do i_om = 1,n_domega ! Loop
! ( COMPUTATIONS...)
domega = tau1 + i_om*delta
enddo ! end loop on Delta_omega
PROBLEM SOLVED! After reading your comments and some web searches, I managed to solve the problem by using the following linspace subroutine:
!***********************************************************************
subroutine linspace_real(from, to, array)
!***********************************************************************
! Generates evenly spaced numbers from `from` to `to` (inclusive).
!
! Inputs:
! -------
!
! from, to : the lower and upper boundaries of the numbers to generate
!
! Outputs:
! -------
!
! array : Array of evenly spaced numbers
!***********************************************************************
implicit none
!Arguments
real(pr), intent(in) :: from, to
real(pr), intent(out) :: array(:)
real(pr) :: range
integer :: n, i
n = size(array)
range = to - from
if (n == 0) return
if (n == 1) then
array(1) = from
return
end if
do i=1, n
array(i) = from + range * (i - 1) / (n - 1)
end do
return
end subroutine linspace_real
!***********************************************************************
In the main program I wrote:
!Declarations
real(pr), dimension(:), allocatable :: d_omega_vec
! Scan interval of Delta_omega (we know the interval step, so we have to compute the number of values within the interval)
domega = tau1
if(tau2 < 0.d0) tau2 = tau2 + twopi
delta = 4.617207817725326D-003 ! Interval step size
n_domega = 1 + ((tau2 - tau1)/delta) ! numbers of values within interval
n_domega_vec = n_domega + 1
allocate(d_omega_vec(n_domega_vec))
call linspace_real(tau1, tau2, d_omega_vec)
So, instead to increment domega during the loop, I created an array of domega from tau1 to tau2 with number of elements properly computed by exploiting my delta.