-2

I have made a linear interpolation code, but when i try to get out this value in a main program or in another subroutine it displays the value of 0.0000, but when i run the program in the subroutine it gets the correct value.

write(,) ' Interpolverdi=', interpolverdi gets the correct value but when i run write(,)'main interpolverdi',interpolverdi i get the value of 0.0000

My input file is working perfect.

What am i doing wrong? How do you get a specific value from a subroutine and then use this value in another subroutine?

Thanks for all the help i can get :)

!main.f90

program mainn

use inputt
use interpoll
!use mongo

 implicit none

 integer :: n, itmax,k,bc
 real(8) :: epsilon, Re, tmax,dt,h,fux,interpolverdi
 real(8), dimension (:), allocatable :: oo,u,v,p
 integer, dimension (:), allocatable :: nn

 !real(8), dimension(:,:), allocatable :: Uvel, Vvel, pres, psi


 call lesinput(n,itmax,k,epsilon,Re,tmax,dt,oo,nn,bc)



 call interpol(nn,oo,n)

 write(*,*)'main interpolverdi',interpolverdi


 end program mainn

  !interpoll.f90
 module interpoll

 implicit none

 contains


 subroutine interpol(nn,oo,n)
  integer :: nn(:), n,s,i
  real(8) :: oo(:),interpolverdi




  s = size(nn)

  do i=1,s-1

  if (n==nn(i)) then
   interpolverdi= oo(i)
  exit


 else if ((n>nn(i)) .and. (n<nn(i+1))) then
 interpolverdi= oo(i)+((oo(i+1)-oo(i))*(n-nn(i)))/(nn(i+1)-nn(i))
 exit

 else
 interpolverdi=nn(s)

 end if

end do

write(*,*) ' Interpolverdi=', interpolverdi
write(*,*)'n=',n

end subroutine interpol

 end module interpoll
ursmooth
  • 311
  • 1
  • 4
  • 12

1 Answers1

1

You have two distinct variables called interpolverdi. One is defined in the main program and one is defined in the subroutine.

When you do write(*,*) interpolverdi in the main program and in the subroutine it prints a different variable each time.

If you wanted to just directly changed the main program variable from the internal subroutine, delete the declaration of interpolverdi from the subroutine.

A better approach however is to add another argument to your subroutine

 subroutine interpol(nn,oo,n,interpolverdi)
  integer :: nn(:), n,s,i
  real(8) :: oo(:),interpolverdi

end subroutine interpol

I recommend to rename the argument or the main program variable to something else so that they have different names.


BTW real(8) is very ugly and not completely portable. If you are learning Fortran, don't learn this bad habit. Fortran 90 kind parameter Confusing double precision real in Fortran

Community
  • 1
  • 1