First of all, congratulations for your book! For me it is the first introduction to Fortran, and I am enjoying a lot. But sometimes I got stuck in a few problems that, usually I am able to cope with.
I have one problem at the end of chapter 3, and I donĀ“t know where is the mistake(s). I have put the function for calculate the diff inside the main program (see below), but it doesn't work and I don't know why.
Can you help me with this?
Thank you!
program tsunami
use iso_fortran_env, only: int32, real32, output_unit, error_unit
implicit none
integer(int32) :: i, n ! indices in space and time
integer(int32), parameter :: im = 100 ! grid size in space
integer(int32), parameter :: nm = 100 ! number of time steps
real(real32), parameter :: dt = 1. ! time step [s]
real(real32), parameter :: dx = 1. ! grid spacing [m]
real(real32), parameter :: c = 1. ! phase speed [m/s]
integer(int32), parameter :: ipos = 25
real(real32), parameter :: decay = 0.02
real(real32) :: du(im), u(im)
do i = 1, im
u(i) = exp(decay * (i  ipos)**2)
end do
! write initial state to screen
write(output_unit, *) 0, u
loop_time: do n = 1, nm
du = diff(u)
do concurrent (i = 1:im)
u(i) = u(i)  c * du(i) / dx * dt
end do
! write current state to screen
write(output_unit, *) n, u
end do loop_time
contains
real elemental function diff(x.) result(dx.)
integer :: i., im.
real :: x.(i.)
!real, intent(out) :: dx.(im.)
dx.(1) = x.(1)  x.(im.)
do concurrent(i. = 2:im.)
dx.(im.) = (x.(i.)  x.(i.1))
end do
end function diff
end program tsunami
