Riyal
Riyal

Reputation: 37

Selecting one value from loop and transferring out of subroutine to main program (Fortran)

I am new to programming in Fortran. I would be very grateful for any help on this problem. I need to transfer a value from a do loop out of the subroutine where the looping takes place, into the main program.. Something like this:

program wet

implicit none

real(8) sulstar, u1, u2, theta1, theta2, q1, q2, tol
real(8) sblstar, surstar, sbrstar
open(3,file="theta1.txt",status="unknown")


tol=1

call ra1(sulstar,sblstar)

u1=sulstar

q1=(u1)**3

write(3,*) q1, u1

end 

subroutine ra1(ulstar,blstar)
! Purpose: To calculate variables across rarefaction1 before the shock

implicit none

real(8) h(51), u(51), b(51), x(51), l(51), inibl, inihl, iniul
real(8) delta, ulstar,blstar
integer i, itermax, t_start


t_start=1
itermax=51
delta=0.01
inihl=1
iniul=0
inibl=0

open(unit=1,file="rare1.txt",status="unknown")
  do 10 i=2, itermax

  h(i)=inihl-(i-1)*delta

  u(1)=iniul
  h(1)=inihl
  l(1)=u(1)-sqrt(h(1))

  u(i)=u(i-1)+((lambda1(i-1)-u(i-1))/h(i-1))*(h(i)-h(i-1))

  lambda1(i)=u(i)-sqrt(h(i))
  b(1)=inibl

  b(i)=b(i-1)+(((lambda1(i-1)-u(i-1))**2)/(h(i))-1)*(h(i)-h(i-1))


  x(1)=l(1)*t_start
  x(i)=l(i)*t_start

write(1,*) x(i), h(i), u(i), b(i) 


u(51)=ulstar
b(51)=blstar

10 continue
return

end

I found out that whenever I try to select the 51st value of U, the 51st value gets distorted, meaning that the value turns out to be zero or a very small number instead of the correct u value when u(51)=ulstar does not appear in the subroutine. Could someone advice me on how I can select the last value of u and pass it to main program? Thank you very much.

Upvotes: 1

Views: 93

Answers (1)

M. S. B.
M. S. B.

Reputation: 29401

Place your procedures into a module and use that module so that the compiler can check consistency of arguments in the call and in the procedure. It looks to me that you have some undeclared variables. e.g., where are lambda1 and h declared?

module MyStuff

contains

subroutine ra1(ulstar,blstar)
! Purpose: To calculate variables across rarefaction1 before the shock

implicit none

real(8) h(51), u(51), b(51), x(51), l(51), inibl, inihl, iniul
real(8) delta, ulstar,blstar
integer i, itermax, t_start


t_start=1
itermax=51
delta=0.01
inihl=1
iniul=0
inibl=0

open(unit=1,file="rare1.txt",status="unknown")
  do 10 i=2, itermax

  h(i)=inihl-(i-1)*delta

  u(1)=iniul
  h(1)=inihl
  l(1)=u(1)-sqrt(h(1))

  u(i)=u(i-1)+((lambda1(i-1)-u(i-1))/h(i-1))*(h(i)-h(i-1))

  lambda1(i)=u(i)-sqrt(h(i))
  b(1)=inibl

  b(i)=b(i-1)+(((lambda1(i-1)-u(i-1))**2)/(h(i))-1)*(h(i)-h(i-1))


  x(1)=l(1)*t_start
  x(i)=l(i)*t_start

write(1,*) x(i), h(i), u(i), b(i)


u(51)=ulstar
b(51)=blstar

10 continue
return

end subroutine ra1

end module MyStuff


program wet

use MyStuff

implicit none

real(8) sulstar, u1, u2, theta1, theta2, q1, q2, tol
real(8) sblstar, surstar, sbrstar
open(3,file="theta1.txt",status="unknown")


tol=1

call ra1(sulstar,sblstar)

u1=sulstar

q1=(u1)**3

write(3,*) q1, u1

end program wet

Upvotes: 0

Related Questions