The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

PabloGarcia (2) [Avatar] Offline
#1
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
PabloGarcia (2) [Avatar] Offline
#2
I have found by myself the different mistakes I did and thanks also to the reply of the message "differential equations in chapter 3". I think one of the problems I faced was not use the allocatable feature inside the function.
Milan Curcic (44) [Avatar] Offline
#3
Hi Pablo,

Sorry for the late response, somehow I missed the notification about this post.

This code shouldn't work because of all the period symbols in it -- perhaps some formatting or copy/paste issue?

In either case, if you still have issues with this code, please post the compiler error message and we can take it from there.


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