Problems with gfortran for compiling simple scripts [duplicate] - fortran

In the Cygwin terminal I enter
$ gfortran -o threed_euler_fluxes_v3.exe threed_euler_fluxes_v3.f90
and I get the compiler error
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../libcygwin.a(libcmain.o): In function `main':
/usr/src/debug/cygwin-1.7.17-1/winsup/cygwin/lib/libcmain.c:39: undefined reference to `_WinMain#16'
collect2: ld returned 1 exit status
I also tried compiling like this
$ gfortran -o threed_euler_fluxes_v3.exe threed_euler_fluxes_v3.f90 -shared
but when I tried running I got an error saying it wasn't a valid windows program?
Heres the complete fortran code. I removed some comments inorder to keep word limit below 30k Heres the original.
subroutine inviscid_roe(primL, primR, njk, num_flux)
implicit none
integer , parameter :: p2 = selected_real_kind(15) ! Double precision
!Input
real(p2), intent( in) :: primL(5), primR(5) ! Input: primitive variables
real(p2), intent( in) :: njk(3) ! Input: face normal vector
!Output
real(p2), intent(out) :: num_flux(5) ! Output: numerical flux
!Some constants
real(p2) :: zero = 0.0_p2
real(p2) :: one = 1.0_p2
real(p2) :: two = 2.0_p2
real(p2) :: half = 0.5_p2
real(p2) :: fifth = 0.2_p2
!Local variables
real(p2) :: nx, ny, nz ! Normal vector
real(p2) :: mx, my, mz ! Orthogonal tangent vector
real(p2) :: lx, ly, lz ! Another orthogonal tangent vector
real(p2) :: abs_n_cross_l ! Magnitude of n x l
real(p2) :: uL, uR, vL, vR, wL, wR ! Velocity components.
real(p2) :: rhoL, rhoR, pL, pR ! Primitive variables.
real(p2) :: qnL, qnR, qmL, qmR, qlL, qlR ! Normal and tangent velocities
real(p2) :: aL, aR, HL, HR ! Speed of sound, Total enthalpy
real(p2) :: RT,rho,u,v,w,H,a,qn, ql, qm ! Roe-averages
real(p2) :: drho,dqn,dql,dqm,dp,LdU(5) ! Wave strengths
real(p2) :: ws(5), R(5,5) ! Wave speeds and right-eigenvectors
real(p2) :: dws(5) ! Width of a parabolic fit for entropy fix
real(p2) :: fL(5), fR(5), diss(5) ! Fluxes ad dissipation term
real(p2) :: gamma = 1.4_p2 ! Ratio of specific heats
real(p2) :: temp, tempx, tempy, tempz ! Temoprary variables
! Face normal vector (unit vector)
nx = njk(1)
ny = njk(2)
nz = njk(3)
tempx = ny*ny + nz*nz
tempy = nz*nz + nx*nx
tempz = nx*nx + ny*ny
if ( tempx >= tempy .and. tempx >= tempz ) then
lx = zero
ly = -nz
lz = ny
elseif ( tempy >= tempx .and. tempy >= tempz ) then
lx = -nz
ly = zero
lz = nx
elseif ( tempz >= tempx .and. tempz >= tempy ) then
lx = -ny
ly = nx
lz = zero
else
! Impossible to happen
write(*,*) "subroutine inviscid_roe: Impossible to happen. Please report the problem."
stop
endif
! Make it the unit vector.
temp = sqrt( lx*lx + ly*ly + lz*lz )
lx = lx/temp
ly = ly/temp
lz = lz/temp
mx = ny*lz - nz*ly
my = nz*lx - nx*lz
mz = nx*ly - ny*lx
abs_n_cross_l = sqrt(mx**2 + my**2 + mz**2)
mx = mx / abs_n_cross_l
my = my / abs_n_cross_l
mz = mz / abs_n_cross_l
rhoL = primL(1)
uL = primL(2)
vL = primL(3)
wL = primL(4)
qnL = uL*nx + vL*ny + wL*nz
qlL = uL*lx + vL*ly + wL*lz
qmL = uL*mx + vL*my + wL*mz
pL = primL(5)
aL = sqrt(gamma*pL/rhoL)
HL = aL*aL/(gamma-one) + half*(uL*uL+vL*vL+wL*wL)
! Right state
rhoR = primR(1)
uR = primR(2)
vR = primR(3)
wR = primR(4)
qnR = uR*nx + vR*ny + wR*nz
qlR = uR*lx + vR*ly + wR*lz
qmR = uR*mx + vR*my + wR*mz
pR = primR(5)
aR = sqrt(gamma*pR/rhoR)
HR = aR*aR/(gamma-one) + half*(uR*uR+vR*vR+wR*wR)
RT = sqrt(rhoR/rhoL)
rho = RT*rhoL !Roe-averaged density
u = (uL + RT*uR)/(one + RT) !Roe-averaged x-velocity
v = (vL + RT*vR)/(one + RT) !Roe-averaged y-velocity
w = (wL + RT*wR)/(one + RT) !Roe-averaged z-velocity
H = (HL + RT*HR)/(one + RT) !Roe-averaged total enthalpy
a = sqrt( (gamma-one)*(H-half*(u*u + v*v + w*w)) ) !Roe-averaged speed of sound
qn = u*nx + v*ny + w*nz !Roe-averaged face-normal velocity
ql = u*lx + v*ly + w*lz !Roe-averaged face-tangent velocity
qm = u*mx + v*my + w*mz !Roe-averaged face-tangent velocity
!Wave Strengths
drho = rhoR - rhoL !Density difference
dp = pR - pL !Pressure difference
dqn = qnR - qnL !Normal velocity difference
dql = qlR - qlL !Tangent velocity difference in l
dqm = qmR - qmL !Tangent velocity difference in m
LdU(1) = (dp - rho*a*dqn )/(two*a*a) !Left-moving acoustic wave strength
LdU(2) = drho - dp/(a*a) !Entropy wave strength
LdU(3) = (dp + rho*a*dqn )/(two*a*a) !Right-moving acoustic wave strength
LdU(4) = rho*dql !Shear wave strength
LdU(5) = rho*dqm !Shear wave strength
!Absolute values of the wave speeds
ws(1) = abs(qn-a) !Left-moving acoustic wave speed
ws(2) = abs(qn) !Entropy wave speed
ws(3) = abs(qn+a) !Right-moving acoustic wave speed
ws(4) = abs(qn) !Shear wave speed
ws(5) = abs(qn) !Shear wave speed
!Harten's Entropy Fix JCP(1983), 49, pp357-393: only for the nonlinear fields.
!NOTE: It avoids vanishing wave speeds by making a parabolic fit near ws = 0.
dws(1) = fifth
if ( ws(1) < dws(1) ) ws(1) = half * ( ws(1)*ws(1)/dws(1)+dws(1) )
dws(3) = fifth
if ( ws(3) < dws(3) ) ws(3) = half * ( ws(3)*ws(3)/dws(3)+dws(3) )
!Right Eigenvectors
! Left-moving acoustic wave
R(1,1) = one
R(2,1) = u - a*nx
R(3,1) = v - a*ny
R(4,1) = w - a*nz
R(5,1) = H - a*qn
! Entropy wave
R(1,2) = one
R(2,2) = u
R(3,2) = v
R(4,2) = w
R(5,2) = half*(u*u + v*v + w*w)
! Right-moving acoustic wave
R(1,3) = one
R(2,3) = u + a*nx
R(3,3) = v + a*ny
R(4,3) = w + a*nz
R(5,3) = H + a*qn
! Shear wave
R(1,4) = zero
R(2,4) = lx
R(3,4) = ly
R(4,4) = lz
R(5,4) = ql
! Shear wave
R(1,5) = zero
R(2,5) = mx
R(3,5) = my
R(4,5) = mz
R(5,5) = qm
diss(:) = ws(1)*LdU(1)*R(:,1) + ws(2)*LdU(2)*R(:,2) + ws(3)*LdU(3)*R(:,3) &
+ ws(4)*LdU(4)*R(:,4) + ws(5)*LdU(5)*R(:,5)
fL(1) = rhoL*qnL
fL(2) = rhoL*qnL * uL + pL*nx
fL(3) = rhoL*qnL * vL + pL*ny
fL(4) = rhoL*qnL * wL + pL*nz
fL(5) = rhoL*qnL * HL
fR(1) = rhoR*qnR
fR(2) = rhoR*qnR * uR + pR*nx
fR(3) = rhoR*qnR * vR + pR*ny
fR(4) = rhoR*qnR * wR + pR*nz
fR(5) = rhoR*qnR * HR
num_flux = half * (fL + fR - diss)
subroutine inviscid_roe_n(primL, primR, njk, num_flux)
implicit none
integer , parameter :: p2 = selected_real_kind(15) ! Double precision
!Input
real(p2), intent( in) :: primL(5), primR(5) ! Input: primitive variables
real(p2), intent( in) :: njk(3) ! Input: face normal vector
!Output
real(p2), intent(out) :: num_flux(5) ! Output: numerical flux
!Some constants
real(p2) :: zero = 0.0_p2
real(p2) :: one = 1.0_p2
real(p2) :: two = 2.0_p2
real(p2) :: half = 0.5_p2
real(p2) :: fifth = 0.2_p2
!Local variables
real(p2) :: nx, ny, nz ! Normal vector
real(p2) :: uL, uR, vL, vR, wL, wR ! Velocity components.
real(p2) :: rhoL, rhoR, pL, pR ! Primitive variables.
real(p2) :: qnL, qnR ! Normal velocities
real(p2) :: aL, aR, HL, HR ! Speed of sound, Total enthalpy
real(p2) :: RT,rho,u,v,w,H,a,qn ! Roe-averages
real(p2) :: drho,dqn,dp,LdU(4) ! Wave strengths
real(p2) :: du, dv, dw ! Velocity differences
real(p2) :: ws(4), R(5,4) ! Wave speeds and right-eigenvectors
real(p2) :: dws(4) ! Width of a parabolic fit for entropy fix
real(p2) :: fL(5), fR(5), diss(5) ! Fluxes ad dissipation term
real(p2) :: gamma = 1.4_p2 ! Ratio of specific heats
! Face normal vector (unit vector)
nx = njk(1)
ny = njk(2)
nz = njk(3)
!Primitive and other variables.
! Left state
rhoL = primL(1)
uL = primL(2)
vL = primL(3)
wL = primL(4)
qnL = uL*nx + vL*ny + wL*nz
pL = primL(5)
aL = sqrt(gamma*pL/rhoL)
HL = aL*aL/(gamma-one) + half*(uL*uL+vL*vL+wL*wL)
! Right state
rhoR = primR(1)
uR = primR(2)
vR = primR(3)
wR = primR(4)
qnR = uR*nx + vR*ny + wR*nz
pR = primR(5)
aR = sqrt(gamma*pR/rhoR)
HR = aR*aR/(gamma-one) + half*(uR*uR+vR*vR+wR*wR)
!First compute the Roe-averaged quantities
! NOTE: See http://www.cfdnotes.com/cfdnotes_roe_averaged_density.html for
! the Roe-averaged density.
RT = sqrt(rhoR/rhoL)
rho = RT*rhoL !Roe-averaged density
u = (uL + RT*uR)/(one + RT) !Roe-averaged x-velocity
v = (vL + RT*vR)/(one + RT) !Roe-averaged y-velocity
w = (wL + RT*wR)/(one + RT) !Roe-averaged z-velocity
H = (HL + RT*HR)/(one + RT) !Roe-averaged total enthalpy
a = sqrt( (gamma-one)*(H-half*(u*u + v*v + w*w)) ) !Roe-averaged speed of sound
qn = u*nx + v*ny + w*nz !Roe-averaged face-normal velocity
!Wave Strengths
drho = rhoR - rhoL !Density difference
dp = pR - pL !Pressure difference
dqn = qnR - qnL !Normal velocity difference
LdU(1) = (dp - rho*a*dqn )/(two*a*a) !Left-moving acoustic wave strength
LdU(2) = drho - dp/(a*a) !Entropy wave strength
LdU(3) = (dp + rho*a*dqn )/(two*a*a) !Right-moving acoustic wave strength
LdU(4) = rho !Shear wave strength (not really, just a factor)
!Absolute values of the wave Speeds
ws(1) = abs(qn-a) !Left-moving acoustic wave
ws(2) = abs(qn) !Entropy wave
ws(3) = abs(qn+a) !Right-moving acoustic wave
ws(4) = abs(qn) !Shear waves
!Harten's Entropy Fix JCP(1983), 49, pp357-393: only for the nonlinear fields.
!NOTE: It avoids vanishing wave speeds by making a parabolic fit near ws = 0.
dws(1) = fifth
if ( ws(1) < dws(1) ) ws(1) = half * ( ws(1)*ws(1)/dws(1)+dws(1) )
dws(3) = fifth
if ( ws(3) < dws(3) ) ws(3) = half * ( ws(3)*ws(3)/dws(3)+dws(3) )
R(1,1) = one
R(2,1) = u - a*nx
R(3,1) = v - a*ny
R(4,1) = w - a*nz
R(5,1) = H - a*qn
R(1,2) = one
R(2,2) = u
R(3,2) = v
R(4,2) = w
R(5,2) = half*(u*u + v*v + w*w)
! Right-moving acoustic wave
R(1,3) = one
R(2,3) = u + a*nx
R(3,3) = v + a*ny
R(4,3) = w + a*nz
R(5,3) = H + a*qn
! Two shear wave components combined into one (wave strength incorporated).
du = uR - uL
dv = vR - vL
dw = wR - wL
R(1,4) = zero
R(2,4) = du - dqn*nx
R(3,4) = dv - dqn*ny
R(4,4) = dw - dqn*nz
R(5,4) = u*du + v*dv + w*dw - qn*dqn
!Dissipation Term: |An|(UR-UL) = R|Lambda|L*dU = sum_k of [ ws(k) * R(:,k) * L*dU(k) ]
diss(:) = ws(1)*LdU(1)*R(:,1) + ws(2)*LdU(2)*R(:,2) &
+ ws(3)*LdU(3)*R(:,3) + ws(4)*LdU(4)*R(:,4)
!Compute the physical flux: fL = Fn(UL) and fR = Fn(UR)
fL(1) = rhoL*qnL
fL(2) = rhoL*qnL * uL + pL*nx
fL(3) = rhoL*qnL * vL + pL*ny
fL(4) = rhoL*qnL * wL + pL*nz
fL(5) = rhoL*qnL * HL
fR(1) = rhoR*qnR
fR(2) = rhoR*qnR * uR + pR*nx
fR(3) = rhoR*qnR * vR + pR*ny
fR(4) = rhoR*qnR * wR + pR*nz
fR(5) = rhoR*qnR * HR
! This is the numerical flux: Roe flux = 1/2 *[ Fn(UL)+Fn(UR) - |An|(UR-UL) ]
num_flux = half * (fL + fR - diss)
!Normal max wave speed in the normal direction.
! wsn = abs(qn) + a
end subroutine inviscid_roe_n
subroutine inviscid_rotated_rhll(primL, primR, njk, num_flux)
implicit none
integer , parameter :: p2 = selected_real_kind(15) ! Double precision
!Input
real(p2), intent( in) :: primL(5), primR(5) ! Input: primitive variables
real(p2), intent( in) :: njk(3) ! Input: face normal vector
!Output
real(p2), intent(out) :: num_flux(5) ! Output: numerical flux
!Some constants
real(p2) :: zero = 0.0_p2
real(p2) :: one = 1.0_p2
real(p2) :: two = 2.0_p2
real(p2) :: half = 0.5_p2
real(p2) :: fifth = 0.2_p2
!Local variables
real(p2) :: nx, ny, nz ! Face normal vector
real(p2) :: uL, uR, vL, vR, wL, wR ! Velocity components.
real(p2) :: rhoL, rhoR, pL, pR ! Primitive variables.
real(p2) :: qnL, qnR ! Normal velocity
real(p2) :: aL, aR, HL, HR ! Speed of sound, Total enthalpy
real(p2) :: RT,rho,u,v,w,H,a,qn ! Roe-averages
real(p2) :: drho,dqn,dp,LdU(4) ! Wave strengths
real(p2) :: du, dv, dw ! Velocity conponent differences
real(p2) :: eig(4) ! Eigenvalues
real(p2) :: ws(4), R(5,4) ! Absolute Wave speeds and right-eigenvectors
real(p2) :: dws(4) ! Width of a parabolic fit for entropy fix
real(p2) :: fL(5), fR(5), diss(5) ! Fluxes ad dissipation term
real(p2) :: gamma = 1.4_p2 ! Ratio of specific heats
real(p2) :: SRp,SLm ! Wave speeds for the HLL part
real(p2) :: nx1, ny1, nz1 ! Vector along which HLL is applied
real(p2) :: nx2, ny2, nz2 ! Vector along which Roe is applied
real(p2) :: alpha1, alpha2 ! Projections of the new normals
real(p2) :: abs_dq ! Magnitude of the velocity difference
real(p2) :: temp, tempx, tempy, tempz ! Temporary variables
! Face normal vector (unit vector)
nx = njk(1)
ny = njk(2)
nz = njk(3)
!Primitive and other variables.
! Left state
rhoL = primL(1)
uL = primL(2)
vL = primL(3)
wL = primL(4)
qnL = uL*nx + vL*ny + wL*nz
pL = primL(5)
aL = sqrt(gamma*pL/rhoL)
HL = aL*aL/(gamma-one) + half*(uL*uL+vL*vL+wL*wL)
! Right state
rhoR = primR(1)
uR = primR(2)
vR = primR(3)
wR = primR(4)
qnR = uR*nx + vR*ny + wR*nz
pR = primR(5)
aR = sqrt(gamma*pR/rhoR)
HR = aR*aR/(gamma-one) + half*(uR*uR+vR*vR+wR*wR)
!Compute the physical flux: fL = Fn(UL) and fR = Fn(UR)
fL(1) = rhoL*qnL
fL(2) = rhoL*qnL * uL + pL*nx
fL(3) = rhoL*qnL * vL + pL*ny
fL(4) = rhoL*qnL * wL + pL*nz
fL(5) = rhoL*qnL * HL
fR(1) = rhoR*qnR
fR(2) = rhoR*qnR * uR + pR*nx
fR(3) = rhoR*qnR * vR + pR*ny
fR(4) = rhoR*qnR * wR + pR*nz
fR(5) = rhoR*qnR * HR
abs_dq = sqrt( (uR-uL)**2 + (vR-vL)**2 + (wR-wL)**2 )
if ( abs_dq > 1.0e-12_p2) then
nx1 = (uR-uL)/abs_dq
ny1 = (vR-vL)/abs_dq
nz1 = (wR-wL)/abs_dq
tempx = ny*ny + nz*nz
tempy = nz*nz + nx*nx
tempz = nx*nx + ny*ny
if ( tempx >= tempy .and. tempx >= tempz ) then
nx1 = zero
ny1 = -nz
nz1 = ny
elseif ( tempy >= tempx .and. tempy >= tempz ) then
nx1 = -nz
ny1 = zero
nz1 = nx
elseif ( tempz >= tempx .and. tempz >= tempy ) then
nx1 = -ny
ny1 = nx
nz1 = zero
else
! Impossible to happen
write(*,*) "inviscid_rotated_rhll: Impossible to happen. Please report the problem."
stop
endif
! Make it the unit vector.
temp = sqrt( nx1*nx1 + ny1*ny1 + nz1*nz1 )
nx1 = nx1/temp
ny1 = ny1/temp
nz1 = nz1/temp
endif
alpha1 = nx*nx1 + ny*ny1 + nz*nz1
! Make alpha1 always positive.
temp = sign(one,alpha1)
nx1 = temp * nx1
ny1 = temp * ny1
nz1 = temp * nz1
alpha1 = temp * alpha1
!n2 = direction perpendicular to n1.
! Note: There are infinitely many choices for this vector.
! The best choice may be discovered in future.
! Here, we employ the formula (4.4) in the paper:
! (nx2,ny2,nz2) = (n1xn)xn1 / |(n1xn)xn1| ('x' is the vector product.)
! (tempx,tempy,tempz) = n1xn
tempx = ny1*nz - nz1*ny
tempy = nz1*nx - nx1*nz
tempz = nx1*ny - ny1*nx
! (nx2,ny2,nz2) = (n1xn)xn1
nx2 = tempy*nz1 - tempz*ny1
ny2 = tempz*nx1 - tempx*nz1
nz2 = tempx*ny1 - tempy*nx1
! Make n2 the unit vector
temp = sqrt( nx2*nx2 + ny2*ny2 + nz2*nz2 )
nx2 = nx2/temp
ny2 = ny2/temp
nz2 = nz2/temp
alpha2 = nx*nx2 + ny*ny2 + nz*nz2
! Make alpha2 always positive.
temp = sign(one,alpha2)
nx2 = temp * nx2
ny2 = temp * ny2
nz2 = temp * nz2
alpha2 = temp * alpha2
!--------------------------------------------------------------------------------
!Now we are going to compute the Roe flux with n2 as the normal with modified
!wave speeds (5.12). NOTE: the Roe flux here is computed without tangent vectors.
!See "I do like CFD, VOL.1" for details: page 57, Equation (3.6.31).
!First compute the Roe-averaged quantities
! NOTE: See http://www.cfdnotes.com/cfdnotes_roe_averaged_density.html for
! the Roe-averaged density.
RT = sqrt(rhoR/rhoL)
rho = RT*rhoL !Roe-averaged density.
u = (uL + RT*uR)/(one + RT) !Roe-averaged x-velocity
v = (vL + RT*vR)/(one + RT) !Roe-averaged y-velocity
w = (wL + RT*wR)/(one + RT) !Roe-averaged z-velocity
H = (HL + RT*HR)/(one + RT) !Roe-averaged total enthalpy
a = sqrt( (gamma-one)*(H-half*(u*u + v*v + w*w)) ) !Roe-averaged speed of sound
!----------------------------------------------------
!Compute the wave speed estimates for the HLL part,
!following Einfeldt:
!
! B. Einfeldt, On Godunov-type methods for gas dynamics,
! SIAM Journal on Numerical Analysis 25 (2) (1988) 294–318.
!
! Note: HLL is actually applied to n1, but this is
! all we need to incorporate HLL. See JCP2008 paper.
qn = u *nx1 + v *ny1 + w *nz1
qnL = uL*nx1 + vL*ny1 + wL*nz1
qnR = uR*nx1 + vR*ny1 + wR*nz1
SLm = min( zero, qn - a, qnL - aL ) !Minimum wave speed estimate
SRp = max( zero, qn + a, qnR + aR ) !Maximum wave speed estimate
! This is the only place where n1=(nx1,ny1,nz1) is used.
! n1=(nx1,ny1,nz1) is never used below.
!----------------------------------------------------
!Wave Strengths
qn = u *nx2 + v *ny2 + w *nz2
qnL = uL*nx2 + vL*ny2 + wL*nz2
qnR = uR*nx2 + vR*ny2 + wR*nz2
drho = rhoR - rhoL !Density difference
dp = pR - pL !Pressure difference
dqn = qnR - qnL !Normal velocity difference
LdU(1) = (dp - rho*a*dqn )/(two*a*a) !Left-moving acoustic wave strength
LdU(2) = drho - dp/(a*a) !Entropy wave strength
LdU(3) = (dp + rho*a*dqn )/(two*a*a) !Right-moving acoustic wave strength
LdU(4) = rho !Shear wave strength (not really, just a factor)
!Wave Speed (Eigenvalues)
eig(1) = qn-a !Left-moving acoustic wave velocity
eig(2) = qn !Entropy wave velocity
eig(3) = qn+a !Right-moving acoustic wave velocity
eig(4) = qn !Shear wave velocity
!Absolute values of the wave speeds (Eigenvalues)
ws(1) = abs(qn-a) !Left-moving acoustic wave speed
ws(2) = abs(qn) !Entropy wave speed
ws(3) = abs(qn+a) !Right-moving acoustic wave speed
ws(4) = abs(qn) !Shear wave speed
!Harten's Entropy Fix JCP(1983), 49, pp357-393: only for the nonlinear fields.
!NOTE: It avoids vanishing wave speeds by making a parabolic fit near ws = 0.
dws(1) = fifth
if ( ws(1) < dws(1) ) ws(1) = half * ( ws(1)*ws(1)/dws(1)+dws(1) )
dws(3) = fifth
if ( ws(3) < dws(3) ) ws(3) = half * ( ws(3)*ws(3)/dws(3)+dws(3) )
!Combine the wave speeds for Rotated-RHLL: Eq.(5.12) in the original JCP2008 paper.
ws = alpha2*ws - (alpha1*two*SRp*SLm + alpha2*(SRp+SLm)*eig)/(SRp-SLm)
!Below, we compute the Roe dissipation term in the direction n2
!with the above modified wave speeds. HLL wave speeds act something like
!the entropy fix or eigenvalue limiting; they contribute only by the amount
!given by the fraction, alpha1 (less than or equal to 1.0). See JCP2008 paper.
!Right Eigenvectors:
!Note: Two shear wave components are combined into one, so that tangent vectors
! are not required. And that's why there are only 4 vectors here.
! Left-moving acoustic wave
R(1,1) = one
R(2,1) = u - a*nx2
R(3,1) = v - a*ny2
R(4,1) = w - a*nz2
R(5,1) = H - a*qn
! Entropy wave
R(1,2) = one
R(2,2) = u
R(3,2) = v
R(4,2) = w
R(5,2) = half*(u*u + v*v + w*w)
! Right-moving acoustic wave
R(1,3) = one
R(2,3) = u + a*nx2
R(3,3) = v + a*ny2
R(4,3) = w + a*nz2
R(5,3) = H + a*qn
! Two shear wave components combined into one (wave strength incorporated).
du = uR - uL
dv = vR - vL
dw = wR - wL
R(1,4) = zero
R(2,4) = du - dqn*nx2
R(3,4) = dv - dqn*ny2
R(4,4) = dw - dqn*nz2
R(5,4) = u*du + v*dv + w*dw - qn*dqn
!Dissipation Term: Roe dissipation with the modified wave speeds.
! |An|dU = R|Lambda|L*dU = sum_k of [ ws(k) * R(:,k) * L*dU(k) ], where n=n2.
diss(:) = ws(1)*LdU(1)*R(:,1) + ws(2)*LdU(2)*R(:,2) &
+ ws(3)*LdU(3)*R(:,3) + ws(4)*LdU(4)*R(:,4)
!Compute the Rotated-RHLL flux. (It looks like the HLL flux with Roe dissipation.)
num_flux = (SRp*fL - SLm*fR)/(SRp-SLm) - half*diss
!Normal max wave speed in the normal direction.
! wsn = abs(qn) + a
end subroutine inviscid_rotated_rhll
!--------------------------------------------------------------------------------

Your file is not a program at all! It is a collection of subprograms. You cannot compile it for running as a program, only as an object file or a library (try -c or -shared). You must add the main program body to be able to compile it as a program and run it!

Related

Unhandled exception at 0x001D6653 in Lid.exe: 0xc0000094: integer division by zero

I am trying to write the code for Lid-Driven Cavity in Fortran.
When I want to run the code, suddenly the integer division by zero errors appears.
I know what is the problem but I don't know how I can solve it. I even changed some numbers in order to avoid this issue but again happened.
I uploaded the photo of the error
I searched about it and there are some answers for C++ but I could not find anything for Fortran.
Program Lid
implicit none
Integer :: I,J,nx, ny, dx, dy, L, W, Iteration, Max_Iteration , Re, M, N, dt
Real :: Delta
Real, allocatable :: u(:,:), v(:,:), p(:,:), u_old(:,:), v_old(:,:), p_old(:,:), X(:), Y(:)
!***************************************************!
PRINT *, "ENTER THE DESIRED POINTS ..."
PRINT *, "... IN X DIRECTION: SUGGESTED RANGE (20-200)"
READ*, M
PRINT *, "... IN Y DIRECTION: SUGGESTED RANGE (10-100)"
READ*, N
! Define Geometry
dt = 0.001
Delta = 2
Re = 100
L = 10
W = 10
dx = L /Real(M-1)
dy = W /Real(N-1)
ALLOCATE (X(M),Y(N),u(M,N),u_old(M,N),v(M,N),v_old(M,N),p(M,N),p_old(M,N))
! Grid Generation
Do I = 1, M
x(I) = (I-1)* dx
End Do
Do J=1 , N
y(J) = (J-1) * dy
End Do
! Boundray Condition
Do I=1 , M
u(I,1) = 0
u(1,I) = 0
u(M,I) = 0
u(I,M) = 1 ! Lid Velocity
End Do
Do J=1, N
v(J,1) = 0
v(1,J) = 0
v(J,N) = 0
v(M,J) = 0
End Do
! Initialization
Do I=2, M-1
Do J=2, N-1
u(I,J) = 0
v(I,J) = 0
p(I,J) = 0
End Do
End Do
! Solver
Do I=2, M-1
Do J=2, N-1
u_old(I,J) = u(I,J)
v_old(I,J) = v(I,J)
p_old(I,J) = p(I,J)
u(I,J) = - dt / 4* dx * (( u(I, J+1)+ u_old(I,J))**2 - (u_old(I, J)+u(I,J-1))**2) - dt / 4* dy * ((u_old(I,J)+ u(I-1,J)) &
* (v(I-1,J) + v(I-1, J+1)) - (u_old(I,J) + u(I+1,J)) * (v_old(I,J) + v(I,J+1))) - dt / dx *(p(I, J+1) - p(I,J)) &
+ dt / Re * ((u(I+1,J) - 2 * u_old(I,J) + u(I-1,J)) / dx**2 + (u(I,J+1) - 2 * u_old(I,J) + u(I,J+1)) / dy**2) + u_old(I,J)
v(I,J) = - dt / 4* dy * (( v(I-1, J)+ v(I-1,J+1))**2 - (v_old(I, J)+v(I,J+1))**2) - dt / 4* dx * ((u_old(I,J)+ u(I,J+1)) &
* (v(I,J+1) + v(I-1, J+1)) - (u_old(I,J) + u(I,J-1)) * (v_old(I,J) + v(I-1,J))) - dt / dy *(p(I, J+1) - p(I,J)) &
+ dt / Re * ((v(I+1,J) - 2 * v_old(I,J) + v(I-1,J)) / dx**2 + (v(I,J+1) - 2 * v_old(I,J) + v(I,J+1)) / dy**2) + v_old(I,J)
p(I,J) = - Delta * dt / 2 * ((u(I,J+1)+ u_old(I,J)) - (u_old(I,J) + u(I,J-1))) - Delta * dt / 2 &
* ((v(I-1,J)+ v(I-1,J+1)) - (v_old(I,J) + v(I,J+1)))
End Do
End Do
!-----------------------OUTPUTS GENERATION-----------------------------
OPEN (1,FILE='FIELD.PLT')
WRITE (1,*) 'VARIABLES=X,Y,u,v,p'
WRITE (1,*) 'ZONE I=',M,' J=',N
DO J=1,N
DO I=1,M
WRITE (1,*) X(I),Y(J),u(I,J),v(I,J),p(I,J)
END DO
END DO
End Program Lid
Assuming that the yellow arrow shown on the image indicates the line (72) where the exception occurred:
Apparently dx is or becomes zero, because that's the only devision done on that line.
You must know what it means when dx is zero or what causes it, unless it's a programming or data input issue that causes it.
In any case you must make sure that you don't execute that part of your code that divides by zero.
What you need to do to prevent this, fully depends on what your code is supposed to do. I can't help you with that.

Incorrect Order of Error of Verlet Algorithm in Fortran

I am trying to simulate harmonic oscillator by using Verlet Method(Original Verlet) in Fortran.
My research tells that the order of error should be 2 but my calculation showed the order of 1.
I couldn't find my mistake in my source code. What should I do?
Edit:
The algorithm I am using is below:
x(t+Δt) = 2x(t) - x(t-Δt) + Δt² F(t)/m
v(t) = {x(t+Δt) -x(t-Δt)}/2Δt
Where x(t) represents the position, v(t) represents velocity and F(t) represents Force. I recognize this is the Original Verlet described here
According to this site, the order of error should be at least O(Δt²) but the error of the order of my program plotted in gnuplot (below) does not have a order of O(Δt²).
program newton_verlet
implicit none
real*16, parameter :: DT = 3.0
real*16, parameter :: T0 = 0.0
real*16, parameter :: TEND = 2.0
integer, parameter :: NT = int(TEND/DT + 0.5)
real*16, parameter :: M = 1.0
real*16, parameter :: X0 = 1.0
real*16, parameter :: V0 = 0.0
real*16 x,v,t,xold,xnew,vnew,ek,ep,et,f,h
integer it,n
do n=0,20
h = DT/2**n
x = X0
v = V0
ek = 0.5*M*v*v
ep = x*x/2
et = ek + ep
xold = x - v*h
do it = 1,2**n
! f = -x
f = -x
xnew = 2.0*x - xold + f*h*h/M
v = (xnew-xold)/(2.0*h)
ek = 0.5*M*v*v
ep = 0.5*xnew*xnew
et = ek + ep
xold = x
x = xnew
end do
write(*,*) h,abs(x-cos(DT))+abs(v+sin(DT))
end do
end program
Above program calculates the error of calculation for the time step h.
According to the Wiki page for Verlet integrators, it seems that we need to use a more accurate way of setting the initial value of xold (i.e., include terms up to the force) to get the global error of order 2. Indeed, if we modify xold as
program newton_verlet
implicit none
real*16, parameter :: DT = 3.0
real*16, parameter :: M = 1.0
real*16, parameter :: X0 = 1.0
real*16, parameter :: V0 = 0.0
real*16 x,v,xold,xnew,f,h
integer it,n
do n = 0, 20
h = DT / 2**n
x = X0
v = V0
f = -x
! xold = x - v * h !! original
xold = x - v * h + f * h**2 / (2 * M) !! modified
do it = 1, 2**n
f = -x
xnew = 2 * x - xold + f * h * h / M
xold = x
x = xnew
end do
write(*,*) log10( h ), log10( abs(x - cos(DT)) )
end do
end program
the global error becomes of order 2 (please see the log-log plot below).

Calculating mean square displacement using fortran

I want to calculate the mean square displacements (MSDs) for some particles in 2D space. From what I understand, the MSD is the measure of the displacements for each particle over the trajectory: I'm using the definition that <(∆r(∆t))^2> = 1/N ∑r_i^2 (∆t) where N is the number of particles.
A displacement is calculated as
x_1 = x(t_1), x_2 = x(t_1 + ∆t), ∆x_1(∆t) = x_2 - x_1
y_1 = y(t_1), y_2 = y(t_1 + ∆t), ∆y_1(∆t) = y_2 - y_1
...
x_i = x(t_i), x_i+1 = x(t_i + ∆t), ∆x_i(∆t) = x_i+1 - x_i
y_i = y(t_i), y_i+1 = y(t_i + ∆t), ∆y_i(∆t) = y_i+1 - y_i
The square displacement (∆r)^2 is the sum of the displacements in each dimension. Then the mean is taken.
How do I implement this? I tried the following, but as others have pointed out here it's not correct.
PROGRAM CALC
IMPLICIT NONE
INTEGER :: J,N,T,NPARTICLES,NSTEPS
REAL(8) :: SUM,DX,DY
REAL(8),ALLOCATABLE :: X(:,:),Y(:,:)
REAL(8),ALLOCATABLE :: MSD(:)
! INPUT
NSTEPS = 101
NPARTICLES = 500
ALLOCATE ( X(NPARTICLES,0:NSTEPS-1) )
ALLOCATE ( Y(NPARTICLES,0:NSTEPS-1) )
ALLOCATE ( MSD(0:NSTEPS-1) )
X = 0.0D0
Y = 0.0D0
DX = 0.0D0
DY = 0.0D0
OPEN(UNIT=50,FILE='TRAJECTORY',STATUS='UNKNOWN',ACTION='READ')
DO T = 0,NSTEPS-1
DO J = 1,NPARTICLES
READ(50,*) X(J,T), Y(J,T)
END DO
SUM = 0.0D0
MSD = 0.0D0
DO WHILE (NSTEPS < T)
DO N = 1,NPARTICLES
DX = X(N,T+1) - X(N,T)
DY = Y(N,T+1) - Y(N,T)
SUM = SUM + (DX**2 + DY**2)
END DO
END DO
MSD(T) = SUM / NPARTICLES
END DO
CLOSE(5)
DEALLOCATE(X)
DEALLOCATE(Y)
OPEN(UNIT=60,FILE='msd.dat',STATUS='UNKNOWN')
DO T = 0,NSTEPS-1
WRITE(60,*) T,MSD(T)
END DO
CLOSE(60)
DEALLOCATE(MSD)
END PROGRAM CALC

Rutherford scattering experiment error

I'm very new to Fortran and have to simulate the Rutherford Scattering experiment for an assignment. In the code, there is a gold nucleus assumed to be at the center (x,y) = (0,0). I'm trying to find the acceleration, velocity and then distance of the alpha particle based on the Coloumb's Law. r_lim is meant to be the boundary condition which, when the particle passes, the loop is meant to stop successfully. Please let me know if I can explain anything else.
PROGRAM rutherford_scatter
IMPLICIT NONE
TYPE particle
REAL:: x,y
END TYPE particle
TYPE(particle):: r_alpha, v_alpha, a_alpha
REAL:: x_0,y_0, v_0, q_gold, q_alpha, k_e, m_alpha, c, c_frac, time_step, r_lim
LOGICAL :: check = .FALSE.
k_e = 1.0
c = 137.053999
time_step = 1.0 *10.0**(-5.0)
q_alpha = 2.0
q_gold = 79.0
m_alpha = 7294.3
WRITE (*,*) 'Enter initial value for x distance.'
READ (*,*) x_0
y_0 = -0.005
WRITE (*,*) 'Enter initial value for velocity (fraction of speed of light).'
READ (*,*) c_frac
v_0 = c_frac * c
r_alpha%x = x_0
r_alpha%y = y_0
r_lim = 1.1 * SQRT(r_alpha%x**2+r_alpha%y**2)
v_alpha%x = 0
v_alpha%y = ABS(v_0)
OPEN (11, FILE = 'assign_11.out')
DO WHILE (check)
a_alpha = acceleration(k_e, m_alpha, q_gold, q_alpha, r_alpha)
v_alpha = velocity (v_alpha, a_alpha)
r_alpha = distance (r_alpha, v_alpha)
WRITE (11,*) r_alpha
IF (r_alpha%x .GT. r_lim .OR. r_alpha%y .GT. r_lim) THEN
check =.TRUE.
END IF
END DO
CLOSE(11)
CONTAINS
TYPE (particle) FUNCTION acceleration (k_e, m_alpha, q_gold, q_alpha,r_alpha)
TYPE (particle), INTENT(IN):: r_alpha
REAL, INTENT (IN)::k_e, m_alpha, q_gold, q_alpha
acceleration%x = (k_e/m_alpha) * ((q_gold * q_alpha)/(ABS(r_alpha%x))**2 )* (r_alpha%x/SQRT(r_alpha%x**2+r_alpha%y**2))
acceleration%y = (k_e/m_alpha) * ((q_gold * q_alpha)/(ABS(r_alpha%y))**2 )*(r_alpha%y/SQRT(r_alpha%x**2+r_alpha%y**2))
END FUNCTION
TYPE (particle) FUNCTION velocity (v_alpha, a_alpha)
TYPE (particle), INTENT (IN):: a_alpha, v_alpha
velocity%x = v_alpha%x + a_alpha%x*time_step
velocity%y = v_alpha%y + a_alpha%y* time_step
END FUNCTION
TYPE(particle) FUNCTION distance (r_alpha, v_alpha)
TYPE (particle), INTENT (IN):: r_alpha, v_alpha
distance%x = r_alpha%x + v_alpha%x * time_step
distance%y = r_alpha%y + v_alpha%y * time_step
END FUNCTION
END PROGRAM rutherford_scatter

definitely memory loss on valgrind

I have this part of the following Fortran code, where at allocate(temp), valgrind says about definitely memory loss. Is there any mistake in the way I am doing on allocating this data?
subroutine insert_linked_list_grids(l,ncell,nclust,corner,headlev)
implicit none
integer :: ig, jg, kg, k, l, m1, m2, m3, nx, ny, nz, ncell, &
nclust, bsaux, bnaux, bwaux, beaux, bbaux, btaux
double precision :: dx, dy, dz, dxaux, dyaux, dzaux
double precision, dimension(24,maxcl) :: corner
type(level_components1), pointer :: temp, curr
type(level_components1), dimension(ltop), target :: headlev
dx = headlev(l)%hx
dy = headlev(l)%hy
dz = headlev(l)%hz
dxaux = headlev(l-1)%hx
dyaux = headlev(l-1)%hy
dzaux = headlev(l-1)%hz
!nullify(headlev(l)%next)
curr => headlev(l)
headlev(l)%npatches = 0
!calculating ix, iy, iz, mx, my, mz
write(*,*) 'total number of cluster =', nclust
do k=1,nclust
headlev(l)%npatches = headlev(l)%npatches +1
ig = nint(r*(corner(1,k) - 0.5d0*dxaux - a1)/dxaux ) + 1
jg = nint(r*(corner(2,k) - 0.5d0*dyaux - a2)/dyaux ) + 1
kg = nint(r*(corner(3,k) - 0.5d0*dzaux - a3)/dzaux ) + 1
!write(*,*) "cluster = ", k,"ig = ", ig,"jg = ", jg,&
! "kg = ",kg
nx = nint(r*(corner(10,k)-corner(1,k) + dxaux)/dxaux)
ny = nint(r*(corner(5,k)-corner(2,k) + dyaux)/dyaux)
nz = nint(r*(corner(15,k)-corner(3,k) + dzaux)/dzaux)
!write(*,*) "cluster = ", k,"nx = ", nx,"ny = ", ny,&
! "nz = ",nz
call bc_linked_list(ig,jg,kg,nx,ny,nz,dx,dy,dz,bwaux,beaux,&
bsaux,bnaux,bbaux,btaux,headlev)
allocate(temp)
temp%grid%ix = ig
temp%grid%iy = jg
temp%grid%iz = kg
temp%grid%mx = nx
temp%grid%my = ny
temp%grid%mz = nz
temp%grid%iu = 1 + ncell
temp%grid%bw = bwaux
temp%grid%be = beaux
temp%grid%bs = bsaux
temp%grid%bn = bnaux
temp%grid%bb = bbaux
temp%grid%bt = btaux
m1 = temp%grid%mx + 1 + 2*nbc
m2 = temp%grid%my + 1 + 2*nbc
m3 = temp%grid%mz + 1 + 2*nbc
ncell = ncell + m1*m2*m3
nullify(temp%next)
curr%next => temp
curr => temp
end do
return
end subroutine insert_linked_list_grids
I expect that valgrind is warning you that when the subroutine returns temp goes out of scope and is, in effect, reaped, but the memory pointed to by temp is not reaped; this looks like a canonical memory leak to me.
You could deallocate temp before the subroutine ends.
Or you could make temp an allocatable array in which case it is the compiler's responsibility to generate code which reaps the memory allocated when the subroutine returns. In general, with a modern (Fortran 2003) compiler allocatable is a better route to managing memory dynamically than pointer because the compiler takes care of memory deallocation. Of course, there are some cases where only a pointer will do.