Segmentation fault when passing a function as argument in a subroutine - fortran

I try to illustrate how to pass a function to a Newton Raphson procedure. I succeed with a very simple function (called unefonction see below) but it does not work with a function which has got parameters. This second fonction is called gaussienne and it takes one argument, x, and two optional arguments mu and sig. In my newton raphson procedure I called the fonction in this way : f(x). What is strange for me is that during the execution, the program does as if the optional parameters sig and mu were present but they don't... Thus I do not understand ...
Here is the module which contains the functions
module fonction
implicit none
! parametre pour la gaussienne
double precision :: f_sigma = 1.d0, f_mu = 0.d0
! pi accessible uniquement en interne
double precision, parameter :: pi = 3.14159265359d0
contains
double precision function unefonction(x)
! fonction : unefonction
! renvoie
! $\frac{e^x - 10}{x + 2}$
implicit none
! arguments
double precision, intent(in) :: x
unefonction = (exp(x) - 10.) / (x + 2.)
end function unefonction
! * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
double precision function gaussienne(x, mu, sig)
! fonction gaussienne
! utilise les parametres definis dans le module si
! mu et sig ne sont pas passes en argument
implicit none
! arguments
double precision, intent(in) :: x
double precision, intent(in), optional :: mu, sig
! variables locales
double precision :: norme, moy, sigma
! sigma
if (present(sig)) then
write(*,*)"sig present"
sigma = sig
else
sigma = f_sigma
end if
! mu
if (present(mu)) then
write(*,*)"mu present"
moy = mu
else
moy = f_mu
end if
! calcul de la gaussienne
norme = 1.d0 / (sigma * sqrt(2.d0 * pi))
gaussienne = norme * exp(-(x - moy)**2 / (2.d0 * sigma**2))
end function gaussienne
end module fonction
Here is the module which contains the newton raphson procedure
module rechercheRacine
implicit none
contains
subroutine newtonRaphson(racine, f, eps, cible)
! recherche l'antecedant de cible
implicit none
! arguments
double precision, intent(inout) :: racine
double precision, intent(in), optional :: cible, eps
! fonction dont on cherche la racine
double precision, external :: f
! variables locales
integer :: compteur
double precision :: xold, xnew, delta, valcible
double precision :: threshold, fprim, fdex
! precision
if (present(eps)) then
threshold = eps
else
threshold = 1.d-10
end if
! valeur cible
if (present(cible)) then
valcible = cible
else
valcible = 0.d0
end if
write(*,*) "--------------------------------------------------------"
write(*,*) " NEWTON RAPHSON"
write(*,*) "--------------------------------------------------------"
write(*,"('x0 = ',e16.6)") racine
write(*,"('seuil = ',e16.6)") threshold
write(*,"('cible = ',e16.6)") valcible
write(*,*) "--------------------------------------------------------"
write(*,*) " ITERATIONS"
write(*,*) "--------------------------------------------------------"
! initialisation
compteur = 0
delta = 1.d0
xold = racine
write(*, '(i4,4e16.6)') compteur, f(xold), xold, 0., threshold
! iterations
do while (delta > threshold .and. compteur <= 100)
! calcul de la fonction en xold
fdex = f(xold) - valcible
! calcul de la derivee numerique
fprim = (f(xold + threshold) - f(xold - threshold)) / (2.d0 * threshold)
! application de l'iteration de Newton Raphson
xnew = xold - fdex / fprim
delta = abs(xnew - xold)
compteur = compteur + 1
! affichage de la convergence
write(*, '(i4,4e16.6)') compteur, fdex, xnew, delta, threshold
! mise a jour de xstart
xold = xnew
end do
if (delta < threshold) then
racine = xnew
write(*, *) '--------------------------------------------------------'
write(*, *) ' CONVERGE'
write(*, *) '--------------------------------------------------------'
write(*, *) 'A la convergence demandee, une solution est:'
write(*, "('x = ',e20.10,' f(x) = ', e20.10)") racine, f(racine)
write(*, *)
else
write(*, *) '--------------------------------------------------------'
write(*, *) ' NON CONVERGE'
write(*, *) '--------------------------------------------------------'
end if
end subroutine newtonRaphson
end module rechercheRacine
Here is the main program :
program main
! contient la subroutine newtonRaphson
use rechercheRacine
! contient la fonction
use fonction
implicit none
double precision :: racine, eps, cible
! appel de la subroutine newtonRaphson
! sans la valeur cible : cible (defaut = 0)
! sans la precision : eps (defaut 1d-10)
racine = 1.d0
call newtonRaphson(racine, unefonction)
! --------------------------------------------------------
! appel de la subroutine newtonRaphson
! avec pour cible 10
racine = 1.d0
eps = 1.d-14
cible = 10.d0
call newtonRaphson(racine, unefonction, eps, cible)
! --------------------------------------------------------
! parametre de la gaussienne
f_sigma = 2.d0
f_mu = 5.d0
! appel de la subroutine newtonRaphson
! passage des arguments sous la forme clef = valeur
cible = 0.1d0
racine = 2.d0
call newtonRaphson(cible = cible, f = gaussienne, racine = racine)
end program main
The main program works for the function called unefonction but it doesn't work for the gaussienne function.
Here is the error message :
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x7F1B6F5890F7
#1 0x7F1B6F5896D4
#2 0x7F1B6EEEB49F
#3 0x4009D2 in __fonction_MOD_gaussienne at mod_fonction.f90:54
#4 0x40104D in __rechercheracine_MOD_newtonraphson at mod_racine.f90:59
#5 0x4016BA in MAIN__ at main.f90:40
Erreur de segmentation (core dumped)
I think that the invalid memory reference is due to the fact that the program does as if optional parameters sig and mu were present and thus looks for them while they are not.

Yes, the problem is that the function you pass indeed expects three arguments instead of one only. If you change the external declaration of f in the subroutine newtonRaphson
double precision, external :: f
to an explicit interface (which describes, how you really use it):
interface
double precision function f(x)
double precision, intent(in) :: x
end function f
end interface
your code won't even compile due to the mismatch in the number of parameters.
They are different ways to pass "parameters" to the function f which is called from the routine newtonRaphson:
You could expect f to have two intent(in) arguments instead of one: Additional to the value x, it could take also a real array, which may be of arbitrary size and may contain the necessary parameters. That would require following interface:
interface
double precision function f(x, params)
double precision, intent(in) :: x
double precision, intent(in) :: params(:)
end function f
end interface
Those functions, which do not need parameters (like unefonction) would just not use the content of the second parameter, while others (like gaussienne) would take their parameters from it.
You could make newtonRaphson to expect a given extensible type (class) with a type bound procedure returning the value for a given x-value. You can then create abritrary extensions of this type, which may calculate the value for the given x-value based on some parameters stored as fields in the extended type. Then the program could look like below (I stripped several parts), but it would require Fortran 2003 compiler:
module rechercheRacine
implicit none
type, abstract :: calculator
contains
procedure(getvalue_iface), deferred :: getvalue
end type calculator
interface
double precision function getvalue_iface(self, x)
import :: calculator
class(calculator), intent(in) :: self
double precision, intent(in) :: x
end function getvalue_iface
end interface
contains
subroutine newtonRaphson(racine, f, eps, cible)
double precision, intent(inout) :: racine
class(calculator), intent(in) :: f
double precision, intent(in), optional :: cible, eps
do while (delta > threshold .and. compteur <= 100)
fdex = f%getvalue(xold) - valcible
:
end do
end subroutine newtonRaphson
end module rechercheRacine
module fonction
use rechercheRacine
implicit none
type, extends(calculator) :: unefonction
contains
procedure :: getvalue => unefonction_getvalue
end type unefonction
type, extends(calculator) :: gaussienne
double precision :: mu = 0.0d0, sigma = 1.0d0
contains
procedure :: getvalue => gaussienne_getvalue
end type gaussienne
contains
double precision function unefonction_getvalue(self, x)
class(unefonction), intent(in) :: self
double precision, intent(in) :: x
unefonction_getvalue = (exp(x) - 10.) / (x + 2.)
end function unefonction_getvalue
double precision function gaussienne_getvalue(self, x)
class(gaussienne), intent(in) :: self
double precision, intent(in) :: x
:
gaussienne_getvalue = norme * exp(-(x - moy)**2 / (2.d0 * self%sigma**2))
end function gaussienne_getvalue
end module fonction
program main
use rechercheRacine
use fonction
implicit none
type(unefonction) :: fone
type(gaussienne) :: fgauss
:
call newtonRaphson(racine, fone)
call newtonRaphson(cible = cible, f = fgauss, racine = racine)
end program main

Related

Differential Equation in Fortran

Today I'm trying to evaluate this differential equation for internal energy in a gas in Fortran 90:
du / dt = dT / dt = - λ / ρ
Where u is the internal energy and λ is the cooling function (and they are both functions of temperature T only). ρ is the mass density and we can assume it's constant.
I'm using a Runge-Kutta 2nd order method (heun), and I'm sure I wrote the actual solving algorithm correctly, but I'm pretty sure I'm messing up the implementation. I'm also not sure how to efficiently choose an arbitrary energy scale.
I'm implementing the Right Hand Side with this subroutine:
MODULE RHS
! right hand side
IMPLICIT NONE
CONTAINS
SUBROUTINE dydx(neq, y, f)
INTEGER, INTENT(IN) :: neq
REAL*8, DIMENSION(neq), INTENT(IN) :: y
REAL*8, DIMENSION(neq), INTENT(OUT) :: f
f(1) = -y(1)
END SUBROUTINE dydx
END MODULE RHS
And this is the Heun algorithm I'm using:
SUBROUTINE heun(neq, h, yold, ynew)
INTEGER, INTENT(IN) :: neq
REAL*8, INTENT(IN) :: h
REAL*8, DIMENSION(neq), INTENT(IN) ::yold
REAL*8, DIMENSION(neq), INTENT(OUT) :: ynew
REAL*8, DIMENSION(neq) :: f, ftilde
INTEGER :: i
CALL dydx(neq, yold, f)
DO i=1, neq
ynew(i) = yold(i) + h*f(i)
END DO
CALL dydx(neq, ynew, ftilde)
DO i=1, neq
ynew(i) = yold(i) + 0.5d0*h*(f(i) + ftilde(i))
END DO
END SUBROUTINE heun
Considering both lambda and rho are n-dimensional arrays, i'm saving the results in an array called u_tilde, selecting a starting condition at T = 1,000,000 K
h = 1.d0/n
u_tilde(1) = lambda(n)/density(n) ! lambda(3) is at about T=one million
DO i = 2, n
CALL heun(1, h*i, u_tilde(i-1), u_tilde(i))
ENDDO
This gives me this weird plot for temperature over time.
I would like to have a starting temperature of one million kelvin, and then have it cool down to 10.000 K and see how long it takes. How do I implement these boundary conditions?
What am I doing wrong in RHS and in setting up the calculation loop in the program?
Your implementation of dydx only assigns the first element.
Also, there is no need to define loops for each step, as Fortran90 can do vector operations.
For a modular design, I suggest implementing a custom type that holds your model data, like the mass density and the cooling coefficient.
Here is an example simple implementation, that only holds one scalar value, such that y' = -c y
module mod_diffeq
use, intrinsic :: iso_fortran_env, wp => real64
implicit none
type :: model
real(wp) :: coefficient
end type
contains
pure function dxdy(arg, x, y) result(yp)
type(model), intent(in) :: arg
real(wp), intent(in) :: x, y(:)
real(wp) :: yp(size(y))
yp = -arg%coefficient*y
end function
pure function heun(arg, x0, y0, h) result(y)
type(model), intent(in) :: arg
real(wp), intent(in) :: x0, y0(:), h
real(wp) :: y(size(y0)), k0(size(y0)), k1(size(y0))
k0 = dxdy(arg, x0, y0)
k1 = dxdy(arg, x0+h, y0 + h*k0)
y = y0 + h*(k0+k1)/2
end function
end module
and the above module is used for some cooling simulations with
program FortranCoolingConsole1
use mod_diffeq
implicit none
integer, parameter :: neq = 100
integer, parameter :: nsteps = 256
! Variables
type(model):: gas
real(wp) :: x, y(neq), x_end, h
integer :: i
! Body of Console1
gas%coefficient = 1.0_wp
x = 0.0_wp
x_end = 10.0_wp
do i=1, neq
if(i==1) then
y(i) = 1000.0_wp
else
y(i) = 0.0_wp
end if
end do
print '(1x," ",a22," ",a22)', 'x', 'y(1)'
print '(1x," ",g22.15," ",g22.15)', x, y(1)
! Initial Conditions
h = (x_end - x)/nsteps
! Simulation
do while(x<x_end)
x = x + h
y = heun(gas, x, y, h)
print '(1x," ",g22.15," ",g22.15)', x, y(1)
end do
end program
Note that I am only tracking the 1st element of neq components of y.
The sample output shows exponential decay starting from 1000
x y(1)
0.00000000000000 1000.00000000000
0.390625000000000E-01 961.700439453125
0.781250000000000E-01 924.867735244334
0.117187500000000 889.445707420492
0.156250000000000 855.380327695983
0.195312500000000 822.619637044785
0.234375000000000 791.113666448740
0.273437500000000 760.814360681126
0.312500000000000 731.675505009287
0.351562500000000 703.652654704519
0.390625000000000 676.703067251694
0.429687500000000 650.785637155231
0.468750000000000 625.860833241968
0.507812500000000 601.890638365300
0.546875000000000 578.838491418631
0.585937500000000 556.669231569681
...
Also, if you wanted the above to implement runge-kutta 4th order you can include the following in the mod_diffeq module
pure function rk4(arg, x0, y0, h) result(y)
type(model), intent(in) :: arg
real(wp), intent(in) :: x0, y0(:), h
real(wp) :: y(size(y0)), k0(size(y0)), k1(size(y0)), k2(size(y0)), k3(size(y0))
k0 = dxdy(arg, x0, y0)
k1 = dxdy(arg, x0+h/2, y0 + (h/2)*k0)
k2 = dxdy(arg, x0+h/2, y0 + (h/2)*k1)
k3 = dxdy(arg, x0+h, y0 + h*k2)
y = y0 + (h/6)*(k0+2*k1+2*k2+k3)
end function

Compile Fortran code with #:if defined('FOO')

A Fortran code has two definitions of a subroutine within an if defined block, as shown below. If I manually remove of the definitions, the code can be compiled, but that's not what the author intended. Compiling with gfortran -c -cpp does not work. What is the right way to compile it?
#:if defined('SLICOT')
subroutine dlyap(TT, RQR, P0, ns, info)
! Computes the solution to the discrete Lyapunov equation,
! P0 = TT*P0*TT' + RQR
! where (inputs) TT, RQR and (output) P0 are ns x ns (real) matrices.
!--------------------------------------------------------------------------------
integer, intent(in) :: ns
real(wp), intent(in) :: TT(ns,ns), RQR(ns,ns)
integer, intent(out) :: info
real(wp), intent(out) :: P0(ns,ns)
! for slicot
real(wp) :: scale, U(ns,ns), UH(ns, ns), rcond, ferr, wr(ns), wi(ns), dwork(14*ns*ns*ns), sepd
integer :: iwork(ns*ns), ldwork
integer :: t
UH = TT
P0 = -1.0_wp*RQR
!call sb03md('D','X', 'N', 'T', ns, UH, ns, U, ns, P0, ns, &
! scale, sepd, ferr, wr, wi, iwork, dwork, 14*ns*ns*ns, info)
!if (ferr > 0.000001_wp) call dlyap_symm(TT, RQR, P0, ns, info)
if (info .ne. 0) then
print*,'SB03MD failed. (info = ', info, ')'
P0 = 0.0_wp
info = 1
do t = 1,ns
P0(t,t)=1.0_wp
end do
return
else
! P0 = 0.5_wp*P0 + 0.5_wp*transpose(P0)
info = 0
end if
end subroutine dlyap
#:else
! from elmar
SUBROUTINE DLYAP(A, QQ, Sigma, nx, status)
! doubling, calling DSYMM and DGEMM
! Sigma = A * Sigma * A' + B * B'
! output Sigma is symmetric
IMPLICIT NONE
integer, intent(in) :: nx
integer, intent(out) :: status
real(wp), intent(in) :: QQ(nx,nx), A(nx,nx)
real(wp), intent(out) :: Sigma(nx,nx)
INTEGER, PARAMETER :: maxiter = 100
DOUBLE PRECISION, PARAMETER :: tol = 1.0d-8
INTEGER :: iter, i
LOGICAL :: converged
DOUBLE PRECISION, DIMENSION(Nx,Nx) :: AA, AAA, AASigma, Sigma0
Sigma0 = QQ
! Sigma0 = B B'
! Sigma0 = 0.0d0
! call DSYRK('U','N',Nx,Nw,1.0d0,B,Nx,0.0d0,Sigma0,Nx)
! ! fill up lower triangular -- necessary for DGEMM below
! FORALL (i=2:Nx) Sigma0(i,1:i-1) = Sigma0(1:i-1,i)
converged = .false.
iter = 0
AA = A
DO
iter = iter + 1
! call sandwichplus(Sigma, AA, Nx, Sigma0, Nx)
! MANUAL SANDWICHPLUS: Sigma = AA * Sigma0 * AA' + Sigma
call DSYMM('R','U',Nx,Nx,1.0d0,Sigma0,Nx,AA,Nx,0.0d0,AASigma,Nx)
Sigma = Sigma0 ! this line requires Sigma0 to
call DGEMM('N','T',Nx,Nx,Nx,1.0d0,AASigma,Nx,AA,Nx,1.0d0,Sigma,Nx)
! balance for symmetry
Sigma = 0.5d0 * (Sigma + transpose(Sigma))
IF (abs(maxval(Sigma - Sigma0)) < tol) converged = .true.
! print *, iter, abs(maxval(Sigma - Sigma0)), tol
! Sigma = (Sigma + transpose(Sigma)) / dble(2)
IF (converged .OR. (iter > maxiter)) EXIT
! AAA = AA * AA
call DGEMM('N','N',Nx,Nx,Nx,1.0d0,AA,Nx,AA,Nx,0.0d0,AAA,Nx)
AA = AAA
Sigma0 = Sigma
END DO
IF (converged) THEN
status = 0
ELSE
status = -1
END IF
END SUBROUTINE DLYAP
#:endif

I have a piece of code written in fortran 90 output is different when I compile it different times

I use the same compiler (gfortran) and computer every time I compile it and I don't change the source code, but on different compilations it gives a completely different outcome. Sometimes a reasonable outcome and sometimes a wrong outcome.
For instance I compiled it and got the next very unreasonable outputs.
0.0000000000000000 , 3.0902604013843218E+049 , 3.0902604013843218E+049 , 3.0902604013843218E+049 , 5.3524880238158376E+049
2.0000000000000000 , -6.1610238730665058E+049 , -6.1610238730665058E+049 , -6.1610238730665058E+049 , 1.0671206374795975E+050
4.0000000000000000 , 5.5751160679618236E+049 , 5.5751160679618236E+049 , 5.5751160679618236E+049 , 9.6563842878035016E+049
6.0000000000000000 , -1.0179425493222214E+049 , -1.0179425493222214E+049 , -1.0179425493222214E+049 , 1.7631282146122754E+049
8.0000000000000000 , 2.4002992709421553E+049 , 2.4002992709421553E+049 , 2.4002992709421553E+049 , 4.1574402906423475E+049
10.000000000000000 , 3.5499818567499908E+049 , 3.5499818567499908E+049 , 3.5499818567499908E+049 , 6.1487489418386840E+049
12.000000000000000 , -3.5465339877133604E+049 , -3.5465339877133604E+049 , -3.5465339877133604E+049 , 6.1427770574893967E+049
14.000000000000000 , 3.7523505062483277E+049 , 3.7523505062483277E+049 , 3.7523505062483277E+049 , 6.4992617246289011E+049
Then without changing anything I recompiled the same code and ran it again and got the more reasonable first result:
0.0000000000000000 , -0.20075827532679802 , 5.7540609466025759E-003 , 0.33972754855979093 , 0.39465402770022856
Why does this happen? Is it a problem in my code or the compiler. I leave the code just in case its useful to answer the question. I'm just starting to learn fortran, and sorry for the comments in spanish.
EDIT: In this program I use fgsl, a fortran interface to the gnu scientific library, it can be found here: https://github.com/reinh-bader/fgsl
program trace_of_product
use fgsl
implicit none
real(fgsl_double) :: p1, p2, p3, t, r, omega, tmax, rad, zav, yav, xav, integ1, integ2, integ3
integer(fgsl_int) :: i, j, k, nin, nt, jmax
complex(fgsl_double) :: wigner_func, z_sym, y_sym, x_sym
real(fgsl_double), parameter :: pi = 3.14159265359
character(len=3) :: filenumber
omega = 0.01
nin = 500
nt = 50
jmax = 9
tmax = 100
r = sqrt(1.0d0*jmax)
write(filenumber, '(I0.3)') jmax
open(unit = 1, file = 'data'//trim(filenumber)//'.csv')
do k = 0,nt
t = k*tmax/nt
zav = 0.0d0
yav = 0.0d0
xav = 0.0d0
do j = 1,jmax,2
integ1 = 0.0d0
integ2 = 0.0d0
integ3 = 0.0d0
!
! Esta parte del código calcula la integral del producto de la
! j-función de Wigner y el j-simbolo de los operadores de posición en la región
! [0,2pi)x[0,pi)x[0,4pi) y dV = sin(theta) d(phi) d(theta) d(psi)
!
do i = 0,nin
p1 = 2*pi*rand()
p2 = pi*rand()
p3 = 4*pi*rand()
integ1 = integ1 + realpart(wigner_func(j,r,t,omega,p1,p2,p3))*realpart(z_sym(j,t,omega,p1,p2,p3))&
*sin(p2)
integ2 = integ2 + realpart(wigner_func(j,r,t,omega,p1,p2,p3))*realpart(y_sym(j,t,omega,p1,p2,p3))&
*sin(p2)
integ3 = integ3 + realpart(wigner_func(j,r,t,omega,p1,p2,p3))*realpart(x_sym(j,t,omega,p1,p2,p3))&
*sin(p2)
end do
integ1 = integ1*pi*(j+1)/nin
integ2 = integ2*pi*(j+1)/nin
integ3 = integ3*pi*(j+1)/nin
zav = zav +integ1
yav = yav +integ2
xav = xav +integ3
end do
rad = sqrt(xav**2+yav**2+zav**2)
write(1,*) t,',',xav,',',yav,',',zav,',',rad
write(*,*) t,',',xav,',',yav,',',zav,',',rad
end do
end program
!
! Esta función calcula la j-función de Wigner para un estado coherente de dos
! modos |ab>=|a>|b>, rho = |ab><ab|. N y J están restingidos a que N+J=j
!
function wigner_func(jc,r,t,omega,phi,theta,psi) result(Wp)
use fgsl
implicit none
real(fgsl_double), intent(in) :: r, t, omega, phi, theta, psi
integer(fgsl_int), intent(in) :: jc
complex(fgsl_double) :: wigner_D, Wp
real(fgsl_double) :: wigner_small_d, cg
integer(fgsl_int) :: m, mp, k, l, q, N, J
real(fgsl_double), parameter :: pi = 3.14159265359
do N = 0,jc
J = jc-N
do k = abs(N-J),jc
do m = -J,J
do mp = -N,N
do l = -k,k
do q = -k,k
Wp = Wp + exp(-r**2) * r**(2*jc) * (2*k+1) * sin(pi/8)**(jc-m-mp) * cos(pi/8)**(jc+m+mp)* &
cg(N,mp,J,m,k,l)*wigner_D(k,l,q,phi+omega*t,theta,psi)*wigner_small_d(k,q,N-J,pi/2) / &
sqrt(Gamma(N-mp+1.0d0)*Gamma(J-m+1.0d0)*Gamma(N+mp+1.0d0)*Gamma(J+m+1.0d0)*(jc+1)*(2*N+1))
end do
end do
end do
end do
end do
end do
end function wigner_func
!
! Esta función calcula el j-simbolo del operador z, j = 2l + 1
!
function z_sym(j,t,omega,phi,theta,psi) result(Wz)
use fgsl
implicit none
real(fgsl_double), intent(in) :: t, omega, phi, theta, psi
integer(fgsl_int), intent(in) :: j
complex(fgsl_double) :: Wigner_D, Wz
real(fgsl_double) :: wigner_small_d, cg
integer(fgsl_int) :: l,m,k,qp,q
real(fgsl_double), parameter :: pi = 3.14159265359
if ( mod(j,2) == 1 ) then
l = (j-1)/2
do m = -l,l
do k = 1,j
do qp = -k,k
do q = -k,k
Wz = Wz + sqrt(((l+1.0d0)**2-m**2)/((4*(l+1)**2-1)*(j+1)))*(2*k+1)*&
Wigner_D(k,qp,q,phi+omega*t,theta,psi)* &
(cg(l+1,m,l,m,k,qp)*wigner_small_d(k,q,1,pi/2)/sqrt(j+1.0d0)+&
cg(l,m,l+1,m,k,qp)*wigner_small_d(k,q,-1,pi/2)/sqrt(1.0d0*j))
end do
end do
end do
end do
end if
end function z_sym
!
! Esta función calcula el j-simbolo del operador y, j = 2l + 1
!
function y_sym(j,t,omega,phi,theta,psi) result(Wy)
use fgsl
implicit none
real(fgsl_double), intent(in) :: t, omega, phi, theta, psi
integer(fgsl_int), intent(in) :: j
complex(fgsl_double) :: Wigner_D, Wy
real(fgsl_double) :: wigner_small_d, cg
integer(fgsl_int) :: l, m, k, s, q
real(fgsl_double), parameter :: pi = 3.141596265359
if ( mod(j,2) == 1 ) then
l = (j-1)/2
do m = -l,l
do k = 1,j
do q = -k,k
do s = -k,k
Wy = Wy+cmplx(0,0.5)*sqrt((2*k+1)/((4*(l+1)**2-1)*(j+1.0d0)))*wigner_D(k,s,q,phi+omega*t,theta,psi)*&
(sqrt((l+m+1)*(l+m+2.0d0))*(wigner_small_d(k,q,-1,pi/2)*cg(l,m,l+1,m+1,k,s)/sqrt(1.0d0*j)- &
wigner_small_d(k,q,1,pi/2)*cg(l+1,m+1,l,m,k,s)/sqrt(j+2.0d0)) + sqrt((l-m)*(l-m+1.0d0))*(&
wigner_small_d(k,q,-1,pi/2)*cg(l,m+1,l+1,m,k,s)/sqrt(1.0d0*j)-wigner_small_d(k,q,1,pi/2)*&
cg(l+1,m,l,m+1,k,s)/sqrt(j+2.0d0)))
end do
end do
end do
end do
end if
end function
!
! Esta función calcula el j-simbolo del operador x, j = 2l + 1
!
function x_sym(j,t,omega,phi,theta,psi) result(Wx)
use fgsl
implicit none
real(fgsl_double), intent(in) :: t, omega, phi, theta, psi
integer(fgsl_int), intent(in) :: j
complex(fgsl_double) :: Wigner_D, Wx
real(fgsl_double) :: wigner_small_d, cg
integer(fgsl_int) :: l, m, k, s, q
real(fgsl_double), parameter :: pi = 3.141596265359
if ( mod(j,2) == 1 ) then
l = (j-1)/2
do m = -l,l
do k = 1,j
do q = -k,k
do s = -k,k
Wx = Wx+0.5*sqrt((2*k+1)/((4*(l+1)**2-1)*(j+1.0d0)))*wigner_D(k,s,q,phi+omega*t,theta,psi)*&
(-sqrt((l+m+1)*(l+m+2.0d0))*(wigner_small_d(k,q,-1,pi/2)*cg(l,m,l+1,m+1,k,s)/sqrt(1.0d0*j)+ &
wigner_small_d(k,q,1,pi/2)*cg(l+1,m+1,l,m,k,s)/sqrt(j+2.0d0)) + sqrt((l-m)*(l-m+1.0d0))*(&
wigner_small_d(k,q,-1,pi/2)*cg(l,m+1,l+1,m,k,s)/sqrt(1.0d0*j)+wigner_small_d(k,q,1,pi/2)*&
cg(l+1,m,l,m+1,k,s)/sqrt(j+2.0d0)))
end do
end do
end do
end do
end if
end function
!
! Esta función calcula los coeficientes de Clebsch-Gordan
!
function cg(j,m,j1,m1,j2,m2) result(cgn)
use fgsl
implicit none
integer(fgsl_int), intent(in) :: j, m, j1, m1, j2, m2
real(fgsl_double) :: cgn
cgn = (-1)**(-j1+j2-m)*sqrt(2.0*j+1)*fgsl_sf_coupling_3j(2*j1,2*j2,2*j,2*m1,2*m2,-2*m)
end function cg
!
! Esta función es la d-función de Wigner
!
function wigner_small_d(j,m,mp,theta) result(d)
use fgsl
implicit none
integer(fgsl_int), intent(in) :: j, m, mp
integer(fgsl_int) :: k
real(fgsl_double), intent(in) :: theta
real(fgsl_double) :: d
do k = max(0,m-mp),min(j-mp,j+m)
d = d + (-1)**k * cos(theta/2)**(2*j+m-mp-2*k) * sin(theta/2)**(mp-m+2*k) / &
(Gamma(j-mp-k+1.0d0)*Gamma(k+1.0d0)*Gamma(mp-m+k+1.0d0)*Gamma(j+m-k+1.0d0))
end do
d = sqrt(Gamma(j+m+1.0d0)*Gamma(j-m+1.0d0)*Gamma(j+mp+1.0d0)*Gamma(j-mp+1.0d0))*d
end function wigner_small_d
!
! Esta función es la D-función de Wigner
!
function wigner_D(j,m,mp,phi,theta,psi) result(D)
use fgsl
implicit none
integer(fgsl_int), intent(in) :: j, m, mp
real(fgsl_double), intent(in) :: phi, theta, psi
real(fgsl_double) :: wigner_small_d
complex(fgsl_double) :: D
D = exp(-cmplx(0,1)*phi*m)*wigner_small_d(j,m,mp,theta)*exp(-cmplx(0,1)*psi*mp)
end function wigner_D
Does it also happen when you run the program multiple times without recompiling it? If that is the case, you are probably reading a variable that has not been assigned yet.
When debugging a piece of fortran code it is useful to include some debug flags. I use:
gfortran myprog.f90 -g -O0 -Wall -fcheck=all -fbacktrace
You can look them up in the gnu fortran compiler documentation if your interested in what they are doing.

Fortran expected a right parenthesis in expression at (1) - Derived types in a subroutine

I get the error
"Fortran expected a right parenthesis in expression at (1)"
when specifying a component of a declared type in a subroutine. The (1) appears underneath the second % in the assignment from a, b, c in the subroutine. What am I doing wrong? Thanks in advance.
program prototipo
implicit none
!DECLARACIÓN DE TIPOS
type triangulo
integer :: vertices(3) !VECTOR DE VÉRTICES DE CADA TRIÁNGULO EN NUBE DE VÉRTICES
real (kind = 8) :: X(3) !VECTOR DE COORDENADAS X DE LOS TRES VÉRTICES DEL TRIÁNGULO
real (kind = 8) :: Y(3) !VECTOR DE COORDENADAS Y DE LOS TRES VÉRTICES DEL TRIÁNGULO
real (kind = 8) :: area_triang !ÁREA DEL TRIÁNGULO
end type triangulo
!irrelevant code removed
contains
subroutine Area (V)
implicit none
type(triangulo), intent(inout) :: V !VECTOR DE TRIÁNGULOS
integer :: i !ÍNDICE
real (kind = 8) :: a, b, c !LONGITUDES DEL LADO DE CADA TRIANGULO
real (kind = 8) :: t !TÉRMINO PARA CALCULAR LA ALTURA
real (kind = 8) :: h !ALTURA
do i = 1, 8042
a = sqrt(((V(i)%X(2) - V(i)%X(1))**2) + ((V(i)%Y(2) - V(i)%Y(1))**2))
b = sqrt(((V(i)%X(3) - V(i)%X(1))**2) + ((V(i)%Y(3) - V(i)%Y(1))**2))
c = sqrt(((V(i)%X(3) - V(i)%X(2))**2) + ((V(i)%Y(3) - V(i)%Y(2))**2))
t = (sqrt((a + b - c)*(a - b + c)*(-a + b + c)*(a + b + c))) / 2.D0
h = t / b
V(i)%area_triang = (b*h) / 2.D0
end do
end subroutine Area
end program
You have incorrectly declared V to be a single triangle in subroutine Area rather than an array of triangles. Change your declaration to
type(triangulo), intent(inout) :: V(:) !VECTOR DE TRIÁNGULOS
and it compiles.

Trouble using function name as argument in Fortran

I wanted to make it easier to change a certain function which will be used by a subroutine in a fortran project. However I can not get it to work. I have seen quite a few examples which use the external, however I am not sure if I have to use it, since I put all my function and subroutines in modules.
Here is a simplified example of the problem I am dealing with:
I have the program in a separate file:
program test
use Parameters
use circArrayConstructer
use velocity
use RungeKutta4
implicit none
integer(is) :: N, P, nsteps, i, j
real(fd) :: D, dt
real(fd), allocatable :: coor(:,:)
integer(is), allocatable :: topo(:,:)
integer(is) :: error
read (*,*) D, nsteps, N, P
dt = 1.0 / nsteps
call circArray ( 0.5_fd, 0.5_fd, 0.2_fd, 0.2_fd, N, coor, topo, error )
do i = 1, P
do j = 1, nsteps
if ( mod(P,2) > 0 ) then
call RK4 ( dt, coor, D, vel1, coor )
else
call RK4 ( dt, coor, D, vel2, coor )
end if
end do
end do
end program test
I put each subroutine and all the functions in a separate module and each module has its own file:
The module Parameters just defines constants and variable types:
module Parameters
implicit none
integer, parameter :: fs = selected_real_kind(6)
integer, parameter :: fd = selected_real_kind(15)
integer, parameter :: is = selected_int_kind(9)
integer, parameter :: id = selected_int_kind(18)
real(fd), parameter :: PI = 3.141592653589793
end module Parameters
Module circArrayConstructer contains the subroutine circArray which has the output error, coor and topo, the last two have dimensions N by 2 and since N in an input they have to be allocated.
Module RungeKutta4 contains the subroutine RK4 which is an implementation of the 4th order Runge Kutta method:
module RungeKutta4
use Parameters
use velocity
implicit none
contains
subroutine RK4 ( dt, coorOld, D, vel, coorNew )
implicit none
real(fd), intent(in ) :: dt
real(fd), intent(in ) :: D
real(fd), intent(in ) :: coorOld(:,:)
real(fd), intent(out) :: coorNew(:,:)
real(fd), dimension(size(coorOld,1), size(coorOld,2)) :: k1, k2, k3, k4
real(fd), external :: vel
k1 = vel ( coorOld , D )
k2 = vel ( coorOld + 0.5 * dt * k1, D )
k3 = vel ( coorOld + 0.5 * dt * k2, D )
k4 = vel ( coorOld + dt * k3, D )
coorNew = coorOld + dt / 6.0 * (k1 + 2 * (k2 + k3) + k4)
end subroutine RK4
end module RungeKutta4
And module velocity contains multiple functions:
module velocity
use Parameters
implicit none
contains
function vel1 ( coor, D )
implicit none
real(fd), intent(in) :: D
real(fd), intent(in) :: coor(:,:)
real(fd), dimension(size(coor,1), size(coor,2)) :: vel1
vel1(:,1) = -2.0 * D * coor(:,2) * sin(PI * coor(:,1)) * cos(PI * coor(:,2) ** 2)
vel1(:,2) = D * cos(PI * coor(:,1)) * sin(PI * coor(:,2) ** 2)
end function vel1
function vel2 ( coor, D )
implicit none
real(fd), intent(in) :: D
real(fd), intent(in) :: coor(:,:)
real(fd), dimension(size(coor,1), size(coor,2)) :: vel2
vel2(:,1) = 2.0 * D * (1 - coor(:,2)) * sin(PI * coor(:,1)) * cos(PI * (1 - coor(:,2)) ** 2)
vel2(:,2) = D * cos(PI * coor(:,1)) * sin(PI * (1 - coor(:,2)) ** 2)
end function vel2
end module velocity
Currently when I try to compile the program I get the error: Interface mismatch in dummy procedure 'vel': Type/rank mismatch in function result.
I have tried all kinds of things, such as changing the declaration of vel in RungeKutta4 to real(fd), external :: vel(:,:) but that gives conflicting attributes. But I have run out of ideas on how to make the code work.
With the function in a module, you don't want to use external. The problem is that RK4 doesn't know the properties of the function that it is receiving. You can declare the function with an interface block:
interface
function vel ( corr, D )
import fd
real (fd), intent (in) :: D
real (fd), intent (in) :: corr (:,:)
real(fd), dimension(size(corr,1), size(corr,2)) :: vel
end function vel
end interface