Related
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
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.
Thank you guys, all your advices are relevant, but I gave up of Fortran and translated my code to Python. There, with just 1 or 2 little bugs my code worked perfect
I wrote a program to solve a linear system using the Gauss method. I wrote all the algorithms, the forward elimination and the back substitution and I made a lot of others subroutines and I don't know anymore what's wrong, I don't if is something wrong with my code or if some problem programming in Fortran because I'm new in this language. I'll put my code below and the linear system that I should find a solution
PROGRAM metodo_Gauss
IMPLICIT NONE
REAL :: det_a_piv
INTEGER :: n, i, j
REAL, DIMENSION(:,:), ALLOCATABLE :: a, a_piv
INTEGER, DIMENSION(:), ALLOCATABLE :: p
REAL, DIMENSION(:), ALLOCATABLE :: b, x
PRINT*, "Entre com a dimensão n do sistema a ser resolvido"
READ*, n
! allocate memory
ALLOCATE(a(n, n))
ALLOCATE(a_piv(n, n))
ALLOCATE(p(n))
ALLOCATE(b(n))
ALLOCATE(x(n))
CALL matriz_a(n, a)
CALL vetor_b(n, b)
a_piv(1:n, 1:n) = a(1:n, 1:n)
DO i = 1, n
x(i) = 0
END DO
CALL eliminacao(n, a, a_piv, p)
det_a_piv = (-1) ** n
DO j = 1, n
det_a_piv = det_a_piv * a_piv(j, j)
END DO
IF (det_a_piv == 0) THEN
PRINT*, "O sistema linear é indeterminado"
ELSE IF (abs(det_a_piv) <= 1) THEN
PRINT*, "O sistema linear é mal-condicionado"
ELSE
CALL substituicao(n, a_piv, p, b, x)
PRINT*, "A solução do sistema é:"
PRINT*, x
END IF
END PROGRAM metodo_Gauss
SUBROUTINE matriz_a(n, a)
IMPLICIT NONE
INTEGER, INTENT(in) :: n
REAL, DIMENSION(n,n), INTENT(inout) :: a
INTEGER :: i, j !Indícios usados em loops para percorrer os arrays
PRINT*, "Por favor digite os valores do elementos da matriz sistema linear seguindo pela ordem das linhas até o final:"
DO i = 1, n
DO j = 1, n
READ*, a(i,j)
END DO
END DO
END SUBROUTINE matriz_a
SUBROUTINE vetor_b(n, b)
IMPLICIT NONE
INTEGER, INTENT(in) :: n
REAL, DIMENSION(n), INTENT(inout) :: b
INTEGER :: i
PRINT*, "Por favor entre com os elementos do vetor b:"
DO i = 1, n
READ*, b(i)
END DO
END SUBROUTINE vetor_b
SUBROUTINE eliminacao(n, a, a_piv, p)
IMPLICIT NONE
INTEGER, INTENT(in) :: n
REAL, DIMENSION(n, n), INTENT(in) :: a
REAL, DIMENSION(n, n), INTENT(out) :: a_piv
INTEGER, DIMENSION(n), INTENT(out) :: p
INTEGER :: i, j, local, dim
REAL :: mult
DO i = 1, (n - 1)
dim = n - 1
CALL local_pivo(dim, a(i:n, i), local)
a_piv(i, i:n) = a(local, i:n)
a_piv(local, i:n) = a(i, i:n)
p(i) = local
DO j = (i + 1), n
mult = (-1) * (a_piv(j,i) / a_piv(local,i))
a_piv(j,i) = mult
a_piv(j, j:n) = a_piv(j, j:n) + mult * a_piv(i, j:n)
END DO
END DO
END SUBROUTINE eliminacao
SUBROUTINE local_pivo(n, a, local)
IMPLICIT NONE
INTEGER, INTENT(in) :: n
REAL, DIMENSION(n), INTENT(in) :: a
INTEGER, INTENT(inout) :: local
INTEGER :: i
local = 1
DO i = 2, n
IF ((ABS(a(i))) > ABS(a(local))) THEN
local = i
END IF
END DO
END SUBROUTINE local_pivo
SUBROUTINE substituicao(n, a_piv, p, b, x)
IMPLICIT NONE
INTEGER, INTENT(in) :: n
REAL, DIMENSION(n, n), INTENT(in) :: a_piv
REAL, DIMENSION(n), INTENT(out) :: b, x
INTEGER, DIMENSION(n), INTENT(in) :: p
INTEGER :: i, j, k, l, pivo
REAL :: aux
DO i = 1, (n - 1)
pivo = p(i)
IF (pivo /= i) THEN
aux = b(i)
b(i) = b(pivo)
b(pivo) = aux
END IF
DO j = (i + 1), n
b(j) = a_piv(j, i) * b(j) + b(i)
END DO
END DO
DO k = n, 1, -1
IF (k == n) THEN
x(n) = b(n) / a_piv(n, n)
ELSE
x(k) = (b(k) + a_piv(k, n) * x(n)) / a_piv(k, k)
DO l = n, k, -1
x(l) = x(l) + (a_piv(k, l) * x(l)) / a_piv(k, k)
END DO
END IF
END DO
END SUBROUTINE substituicao
Here it is the system that I'm trying to solve
My input is:
4
4
3
2
2
2
1
1
2
2
2
2
4
6
1
1
2
5
8
3
1
My output is:
-40.5000000 -40.2500000 -3.75000000 -37.5000000
But the output should be:
6.500000
-44.000000
72.000000
-16.500000
Blow is a main file
PROGRAM SPHEROID
USE nrtype
USE SUB_INFO
INCLUDE "/usr/local/include/fftw3.f"
INTEGER(I8B) :: plan_forward, plan_backward
INTEGER(I4B) :: i, t, int_N
REAL(DP) :: cth_i, sth_i, real_i, perturbation
REAL(DP) :: PolarEffect, dummy, x1, x2, x3
REAL(DP), DIMENSION(4096) :: dummy1, dummy2, gam, th, ph
REAL(DP), DIMENSION(4096) :: k1, k2, k3, k4, l1, l2, l3, l4, f_in
COMPLEX(DPC), DIMENSION(2049) :: output1, output2, f_out
CHARACTER(1024) :: baseOutputFilename
CHARACTER(1024) :: outputFile, format_string
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int_N = 4096
! File Open Section
format_string = '(I5.5)'
! Write the coodinates at t = 0
do i = 1, N
real_i = real(i)
gam(i) = 2d0*pi/real_N
perturbation = 0.01d0*dsin(2d0*pi*real_i/real_N)
ph(i) = 2d0*pi*real_i/real_N + perturbation
th(i) = pi/3d0 + perturbation
end do
! Initialization Section for FFTW PLANS
call dfftw_plan_dft_r2c_1d(plan_forward, int_N, f_in, f_out, FFTW_ESTIMATE)
call dfftw_plan_dft_c2r_1d(plan_backward, int_N, f_out, f_in, FFTW_ESTIMATE)
! Runge-Kutta 4th Order Method Section
do t = 1, Iter_N
call integration(th, ph, gam, k1, l1)
do i = 1, N
dummy1(i) = th(i) + 0.5d0*dt*k1(i)
end do
do i = 1, N
dummy2(i) = ph(i) + 0.5d0*dt*l1(i)
end do
call integration(dummy1, dummy2, gam, k2, l2)
do i = 1, N
dummy1(i) = th(i) + 0.5d0*dt*k2(i)
end do
do i = 1, N
dummy2(i) = ph(i) + 0.5d0*dt*l2(i)
end do
call integration(dummy1, dummy2, gam, k3, l3)
do i = 1, N
dummy1(i) = th(i) + dt*k3(i)
end do
do i = 1, N
dummy2(i) = ph(i) + dt*l3(i)
end do
call integration(dummy1, dummy2, gam, k4, l4)
do i = 1, N
cth_i = dcos(th(i))
sth_i = dsin(th(i))
PolarEffect = (nv-sv)*dsqrt(1d0+a*sth_i**2) + (nv+sv)*cth_i
PolarEffect = PolarEffect/(sth_i**2)
th(i) = th(i) + dt*(k1(i) + 2d0*k2(i) + 2d0*k3(i) + k4(i))/6d0
ph(i) = ph(i) + dt*(l1(i) + 2d0*l2(i) + 2d0*l3(i) + l4(i))/6d0
ph(i) = ph(i) + dt*0.25d0*PolarEffect/pi
end do
!! Fourier Filtering Section
call dfftw_execute_dft_r2c(plan_forward, th, output1)
do i = 1, N/2+1
dummy = abs(output1(i))
if (dummy.lt.threshhold) then
output1(i) = dcmplx(0.0d0)
end if
end do
call dfftw_execute_dft_c2r(plan_backward, output1, th)
do i = 1, N
th(i) = th(i)/real_N
end do
call dfftw_execute_dft_r2c(plan_forward, ph, output2)
do i = 1, N/2+1
dummy = abs(output2(i))
if (dummy.lt.threshhold) then
output2(i) = dcmplx(0.0d0)
end if
end do
call dfftw_execute_dft_c2r(plan_backward, output2, ph)
do i = 1, N
ph(i) = ph(i)/real_N
end do
!! Data Writing Section
write(baseOutputFilename, format_string) t
outputFile = "xyz" // baseOutputFilename
open(unit=7, file=outputFile)
outputFile = "Fsptrm" // baseOutputFilename
open(unit=8, file=outputFile)
do i = 1, N
x1 = dsin(th(i))*dcos(ph(i))
x2 = dsin(th(i))*dsin(ph(i))
x3 = dsqrt(1d0+a)*dcos(th(i))
write(7,*) x1, x2, x3
end do
do i = 1, N/2+1
write(8,*) abs(output1(i)), abs(output2(i))
end do
close(7)
close(8)
do i = 1, N/2+1
output1(i) = dcmplx(0.0d0)
end do
do i = 1, N/2+1
output2(i) = dcmplx(0.0d0)
end do
end do
! Destroying Process for FFTW PLANS
call dfftw_destroy_plan(plan_forward)
call dfftw_destroy_plan(plan_backward)
END PROGRAM
Below is a subroutine file for integration
! We implemented Shelly's spectrally accurate convergence method
SUBROUTINE integration(in1,in2,in3,out1,out2)
USE nrtype
USE SUB_INFO
INTEGER(I4B) :: i, j
REAL(DP) :: th_i, th_j, gi, ph_i, ph_j, gam_j, v1, v2
REAL(DP), DIMENSION(N), INTENT(INOUT) :: in1, in2, in3, out1, out2
REAL(DP) :: ui, uj, part1, part2, gj, cph, sph
REAL(DP) :: denom, numer, temp
do i = 1, N
out1(i) = 0d0
end do
do i = 1, N
out2(i) = 0d0
end do
do i = 1, N
th_i = in1(i)
ph_i = in2(i)
ui = dcos(th_i)
part1 = dsqrt(1d0+a)/(dsqrt(-a)*ui+dsqrt(1d0+a-a*ui*ui))
part1 = part1**(dsqrt(-a))
part2 = (dsqrt(1d0+a-a*ui*ui)+ui)/(dsqrt(1d0+a-a*ui*ui)-ui)
part2 = dsqrt(part2)
gi = dsqrt(1d0-ui*ui)*part1*part2
do j = 1, N
if (mod(i+j,2).eq.1) then
th_j = in1(j)
ph_j = in2(j)
gam_j = in3(j)
uj = dcos(th_j)
part1 = dsqrt(1d0+a)/(dsqrt(-a)*uj+dsqrt(1d0+a-a*uj*uj))
part1 = part1**(dsqrt(-a))
part2 = (dsqrt(1d0+a-a*uj*uj)+uj)/(dsqrt(1d0+a-a*uj*uj)-uj)
part2 = dsqrt(part2)
gj = dsqrt(1d0-ui*ui)*part1*part2
cph = dcos(ph_i-ph_j)
sph = dsin(ph_i-ph_j)
numer = dsqrt(1d0-uj*uj)*sph
denom = (gj/gi*(1d0-ui*ui) + gi/gj*(1d0-uj*uj))*0.5d0
denom = denom - dsqrt((1d0-ui*ui)*(1d0-uj*uj))*cph
denom = denom + krasny_delta
v1 = -0.25d0*gam_j*numer/denom/pi
temp = dsqrt(1d0+(1d0-ui*ui)*a)
numer = -(gj/gi)*(temp+ui)
numer = numer + (gi/gj)*((1d0-uj*uj)/(1d0-ui*ui))*(temp-ui)
numer = numer + 2d0*ui*dsqrt((1d0-uj*uj)/(1d0-ui*ui))*cph
numer = 0.5d0*numer
v2 = -0.25d0*gam_j*numer/denom/pi
out1(i) = out1(i) + 2d0*v1
out2(i) = out2(i) + 2d0*v2
end if
end do
end do
END
Below is a module file
module nrtype
Implicit none
!integer
INTEGER, PARAMETER :: I8B = SELECTED_INT_KIND(20)
INTEGER, PARAMETER :: I4B = SELECTED_INT_KIND(9)
INTEGER, PARAMETER :: I2B = SELECTED_INT_KIND(4)
INTEGER, PARAMETER :: I1B = SELECTED_INT_KIND(2)
!real
INTEGER, PARAMETER :: SP = KIND(1.0)
INTEGER, PARAMETER :: DP = KIND(1.0D0)
!complex
INTEGER, PARAMETER :: SPC = KIND((1.0,1.0))
INTEGER, PARAMETER :: DPC = KIND((1.0D0,1.0D0))
!defualt logical
INTEGER, PARAMETER :: LGT = KIND(.true.)
!mathematical constants
REAL(DP), PARAMETER :: pi = 3.141592653589793238462643383279502884197_dp
!derived data type s for sparse matrices,single and double precision
!User-Defined Constants
INTEGER(I4B), PARAMETER :: N = 4096, Iter_N = 20000
REAL(DP), PARAMETER :: real_N = 4096d0
REAL(DP), PARAMETER :: a = -0.1d0, dt = 0.001d0, krasny_delta = 0.01d0
REAL(DP), PARAMETER :: nv = 0d0, sv = 0d0, threshhold = 0.00000000001d0
!N : The Number of Point Vortices, Iter_N * dt = Total time, dt : Time Step
!krasny_delta : Smoothing Parameter introduced by R.Krasny
!nv : Northern Vortex Strength, sv : Southern Vortex Strength
!a : The Eccentricity in the direction of z , threshhold : Filtering Threshhold
end module nrtype
Below is a subroutine info file
MODULE SUB_INFO
INTERFACE
SUBROUTINE integration(in1,in2,in3,out1,out2)
USE nrtype
INTEGER(I4B) :: i, j
REAL(DP) :: th_i, th_j, gi, ph_i, ph_j, gam_j, v1, v2
REAL(DP), DIMENSION(N), INTENT(INOUT) :: in1, in2, in3, out1, out2
REAL(DP) :: ui, uj, part1, part2, gj, cph, sph
REAL(DP) :: denom, numer, temp
END SUBROUTINE
END INTERFACE
END MODULE
I compiled them using the below command
gfortran -o p0 -fbounds-check nrtype.f90 spheroid_sub_info.f90 spheroid_sub_integration.f90 spheroid_main.f90 -lfftw3 -lm -Wall -pedantic -pg
nohup ./p0 &
Note that 2049 = 4096 / 2 + 1
When making plan_backward, isn't it correct that we use 2049 instead of 4096 since the dimension of output is 2049?
But when I do that, it blows up. (Blowing up means NAN error)
If I use 4096 in making plan_backward, Everything is fine except that some Fourier coefficients are abnormally big which should not happen.
Please help me use FFTW in Fortran correctly. This issue has discouraged me for a long time.
First, although you claim your example is minimal, it is still pretty large, I have no time to study it.
But I updated my gist code https://gist.github.com/LadaF/73eb430682ef527eea9972ceb96116c5 to show also the backward transform and to answer the title question about the transform dimensions.
The logical size of the transform is the size of the real array (Real-data DFT Array Format) but the complex part is smaller due to inherent symmetries.
But when you make first r2c transform from real array of size n to complex array of size n/2+1. and then an opposite transform back, the real array should be again of size n.
This is my minimal example from the gist:
module FFTW3
use, intrinsic :: iso_c_binding
include "fftw3.f03"
end module
use FFTW3
implicit none
integer, parameter :: n = 100
real(c_double), allocatable :: data_in(:)
complex(c_double_complex), allocatable :: data_out(:)
type(c_ptr) :: planf, planb
allocate(data_in(n))
allocate(data_out(n/2+1))
call random_number(data_in)
planf = fftw_plan_dft_r2c_1d(size(data_in), data_in, data_out, FFTW_ESTIMATE+FFTW_UNALIGNED)
planb = fftw_plan_dft_c2r_1d(size(data_in), data_out, data_in, FFTW_ESTIMATE+FFTW_UNALIGNED)
print *, "real input:", real(data_in)
call fftw_execute_dft_r2c(planf, data_in, data_out)
print *, "result real part:", real(data_out)
print *, "result imaginary part:", aimag(data_out)
call fftw_execute_dft_c2r(planb, data_out, data_in)
print *, "real output:", real(data_in)/n
call fftw_destroy_plan(planf)
call fftw_destroy_plan(planb)
end
Note that I am using the modern Fortran interface. I don't like using the old one.
One issue may be that dfftw_execute_dft_c2r can destroy the content of the input array, as described in this page. The key excerpt is
FFTW_PRESERVE_INPUT specifies that an out-of-place transform must not change its input array. This is ordinarily the default, except for c2r and hc2r (i.e. complex-to-real) transforms for which FFTW_DESTROY_INPUTis the default...
We can verify this, for example, by modifying the sample code by #VladimirF such that it saves data_out to data_save right after the first FFT(r2c) call, and then calculating their difference after the second FFT (c2r) call. So, in the case of OP's code, it seems safer to save output1 and output2 to different arrays before entering the second FFT (c2r).
With the following program I experience errors.
Program COM
!Input
!No of Atoms
!No of Iterations
!Respective Positions.
!As of now for homogeneous clusters.
Implicit None
Real, Parameter :: R8B=selected_real_kind(10)
Real, Parameter :: R4B=selected_real_kind(4)
Integer, Parameter :: I1B=selected_int_kind(2)
Integer, Parameter :: I2B=selected_int_kind(4)
Integer, Parameter :: I4B=selected_int_kind(9)
Integer, Parameter :: I8B=selected_int_kind(18)
Real (R8B), Dimension (:,:), Allocatable :: Posx, Posy, Posz
Real (R8B), Dimension (:), Allocatable :: Posx_n, Posy_n, Posz_n
Real (R8B), Dimension (:), Allocatable :: dist_com, avj_dist_com
Integer (I4B), Dimension (:), Allocatable :: bin_array
Real (R8B) :: comx, comy, comz
Integer (I8B) :: nIter, nAtom, dist
Integer (I8B) :: I,J,ii,k
Integer (I1B) :: xyz_format, FlagR, FlagM, Flag_com
Integer (I8B) :: bin
Integer (R8B) :: max_dist
Character (50) POS_file, COM_file,Bin_file
Character (2) jj
Read (*,*) POS_file
Read (*,*) COM_file
Read (*,*) Bin_file
Read (*,*) nAtom
Read (*,*) nIter
Read (*,*) xyz_format
Read (*,*) max_dist, bin
! if Flag_com == 1 then compute dist from COM
! if its 0 then specify the atom no and g(r) will be computed..
! i.e. no of atoms from that atom between dist r and r + dr
Allocate (Posx(nAtom,nIter))
Allocate (Posy(nAtom,nIter))
Allocate (Posz(nAtom,nIter))
! xyz_format = 0 ==> old_ks
! xyz_format = 1 ==> xmakemol
! xyz_format = 2 ==> Envision
write(*,*)POS_file
Open (unit=99, file=POS_file)
if (xyz_format == 0 ) then
do i = 1,nIter
read(99,*)
do j = 1,nAtom
read(99,*)ii,Posx(j,i),Posy(j,i),Posz(j,i),ii
enddo
enddo
elseif (xyz_format == 1 ) then
do i = 1,nIter
read(99,*)ii
read(99,*)
do j = 1,nAtom
read(99,*)jj,Posx(j,i),Posy(j,i),Posz(j,i)
enddo
enddo
elseif (xyz_format == 2 ) then
read(99,*)
read(99,*)
read(99,*)
read(99,*)
do i = 1,nIter
do j = 1,nAtom
read(99,*)
read(99,*)Posx(j,i),Posy(j,i),Posz(j,i)
enddo
enddo
endif
Close (99)
Write (*,'(\1x,"Reading Complete")')
allocate (avj_dist_com (nIter))
allocate (dist_com (nAtom))
avj_dist_com = 0.0d0
dist_com = 0.0d0
Allocate (Posx_n(nAtom))
Allocate (Posy_n(nAtom))
Allocate (Posz_n(nAtom))
Allocate (Bin_Array(bin))
Posx_n = 0.0d0
Posy_n = 0.0d0
Posz_n = 0.0d0
bin_array = 0.0d0
Open (unit=2, file=COM_file)
Do I = 1, nIter
comx = 0.0d0
comy = 0.0d0
comz = 0.0d0
Do J = 1, nAtom
comx = comx + Posx(j,i)
comy = comy + Posy(j,i)
comz = comz + Posz(j,i)
Enddo
comx = comx/nAtom
comy = comy/nAtom
comz = comz/nAtom
Write (*,*) i, comx, comy, comz
Do J = 1, nAtom
Posx_n (j) = Posx(j,i) - comx
Posy_n (j) = Posy(j,i) - comy
Posz_n (j) = Posz(j,i) - comz
dist_com (j) = dsqrt ( Posx_n(j)*Posx_n(j) &
+ Posy_n(j)*Posy_n(j) &
+ Posz_n(j)*Posz_n(j) )
avj_dist_com (i) = avj_dist_com(i) + dist_com(j)
Enddo
avj_dist_com(i) = avj_dist_com(i)/nAtom
Do j = 1, nAtom
dist = dist_com (j) * dfloat((bin/max_dist))
bin_array(dist) = bin_array(dist) + 1
Enddo
write (2,'(2x,i6,143(2x,f10.7))') I, avj_dist_com(i),(dist_com(k),k=1,nAtom)
write(*,*) i
Enddo
close (2)
Open (unit=3, file=Bin_file)
do i = 1, bin
write (3,'(2x,i6,4x,i8)') i , bin_array(i)
enddo
close (3)
deAllocate (Posx)
deAllocate (Posy)
deAllocate (Posz)
deAllocate (Posx_n)
deAllocate (Posy_n)
deAllocate (Posz_n)
deallocate (avj_dist_com)
deallocate (dist_com)
deallocate (bin_array)
Stop
End Program COM
The errors look like
Real(KIND=r8b), Dimension (:), Allocatable :: Posx, Posy, Posz
1
Error: Integer expression required at (1)
and there are many more
How can I rectify these?
The kind parameter for a type must be an integer constant expression. You have the latter part down, as you are using named constants R8B and R4B.
However, and this is what the error message says, you have not used an integer constant expression. You should notice that selected_real_kind returns an integer value even as the kind for a selected real type. So, you can correct your code with
Integer, Parameter :: R8B=selected_real_kind(10)
Integer, Parameter :: R4B=selected_real_kind(4)