ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
! 1d scalar wave equation with fd method and stability condition = 1
SUBROUTINE fd1d
implicit none
real,dimension(1) :: x
real :: x_delta
real :: t_delta
real :: c
real :: xlast
real :: xinit
real :: tinit
real :: tlast
real :: k
real :: x0
real,dimension(:,,allocatable :: u
real :: alpha
integer :: iinit
integer :: ilast
integer :: ninit
integer :: nlast
integer :: i
integer :: n
real, parameter :: pi = 3.141592654
!initial profile
k = 1000.0
x0 = 0.3
do i = iinit,ilast
u(i,0) = 0.0
u(i,-1) = 0.0
end do
open (unit=100,file='n0.dat',action='write')
do i = iinit, ilast
x(i) = x(iinit) + i*x_delta
u(i,0) = exp (-k*(x(i)-x0)**2)
u(i,-1) = exp (-k*(x(i)-x0)**2)
Print *, u(i,0)
write (*,*) u(i,0)
end do
close (unit = 100)
open (unit=110,file='n1.dat',action='write')
do n = 0,nlast
do i = iinit,ilast
u(i,n+1) = 2*(1-(alpha**2))*u(i,n)-u(i,n-1)+(alpha**2)*(u(i+1,n)+u(i-1,n))
end do !i
!boundary condition
u(0,n+1) = 0
u(ilast,n+1) = 0
!end of boundary condition
!export data
print *, u(i,n+1)
write (110,*) u(i,n+1)
end do !n
close (unit = 110)
return
end SUBROUTINE fd1d
Click here to see the post LQ members have rated as the most helpful post in this thread.
It will be a lot easier to learn from an intro online than for me to try to explain it. It's very simple if you're just trying to find a segfault, as long as you compile with the -g flag (I'm assuming you're running some sort of *nix with gcc for this; if not, this probably doesn't help.)
Kevin Barry
i use makefile to compile this(main.f90). the online version of gdb just show how to run it with .out, but my output are main.o and subroutines.o. Have no idea how to use gdb with this kind of file. pls advice.
To use gdb, compile your program with the `-g' option, like this
Code:
$ gfortran -g wave.f90
or add the option to makefile. To learn GDB read any online tutorial, this for example. Here is a sample gdb session with your program:
Code:
$ gfortran -g wave.f90
$ gdb ./a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /tmp/a.out...done.
(gdb) run
Starting program: /tmp/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0804897c in fd1d () at wave.f90:58
58 u(i,-1) = 0.0
(gdb) l # list -- type the text of the program in the vicinity of where it is presently stopped.
53 !initial profile
54 k = 1000.0
55 x0 = 0.3
56 do i = iinit,ilast
57 u(i,0) = 0.0
58 u(i,-1) = 0.0
59 end do
60
61 open (unit=100,file='n0.dat',action='write')
62 do i = iinit, ilast
(gdb) p u # p = print
$1 = (( 0) )
(gdb) p i
$2 = 0
(gdb) p u(0,-1)
no such vector element
(gdb) quit
A debugging session is active.
Inferior 1 [process 20884] will be killed.
Quit anyway? (y or n) y
We see that the bug reveal itself in the line where you are trying to assign 'u(0,-1)'. You should either rethink this line or change the definition of u:
Code:
allocate (u(0:200,-1:600))
Next, when we made this change, we similarly reveal next bug in the line
Code:
(gdb) run
Starting program: /tmp/a.out
8.19395264E-40
8.19395264E-40
1.60516147E-38
1.60516147E-38
Program received signal SIGSEGV, Segmentation fault.
0x08048a46 in fd1d () at wave.f90:64
64 u(i,0) = exp (-k*(x(i)-x0)**2)
(gdb) p i
$1 = 2
(gdb)
This time we access element 2 of array x, which has only one element... So we change definition of x to
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.