Related
I'm trying to run a simulation on the Monte Carlo problem. Whenever I try to run my version of the program (meant for up to 100/101 steps) it runs for a split second and then it closes. I can't even add any input whatsoever.
I've tried anything I can think of, including modifying the code of my 50 steps program (which is working accordingly and is exactly the same as my 101 steps program) to try and run it, but the issue remains.
The code as it follows should start by asking the size of the overall net (which should be 50) and, later on the amount of drunkards used (I tipically use 10,000 drunks). The output should be the position of each drunkard, the standard deviation of the amount of drunkards in each position, and an external output with the mount of drunkards per position.
implicit none
integer x, y, T, L, N, B, PL, PV, E, DP1
real *8 S(101, 101)
real *8 r
double precision M
real *8 U
real *8 Pit
real *8 Pitotal
real *8 DP2
real *8 DPF
real *8 PVT
real *8 PLT
read(*,*) L !Tamanho da rede
!read(*,*) U !Quantidade de passos
read(*,*) B !Quantidade de bêbados
!Zerar os vetores
!WRITE(*,*) PV, PL
do x = 1, L
do y = 1, L
S(x, y) = 0
!WRITE(*,*) x, y, S(x, y)
enddo
enddo
!Simular Caminhar e Escrever na Matriz
do T = 1, B
PV = (L/2)+1
PL = (L/2)+1
!Determinar a quantidade de passos dados até 101
!!! THIS IS WHERE BOTH THE 50 STEPS CODE AND THE 101 STEPS CODE DIFFER THIS ONE'S SUPPOSED TO MULTIPLY THE RANDOM NUMBER IN ORDER TO GET A NUMBER OF STEPS THAT DOESN'T EXCEED 101,. WHILE THE OTHER JUST RANDOMLY ASSIGNS 50 OR 51 STEPS.
call random_number(U)
U = U * 101 + 1
!Simular Caminhar
do N = 1, int(U)
call random_seed() !Gerar a seed aleatória
call random_number(r)
if (r < 0.5) then
PV = PV-1
else
PV = PV+1
!WRITE(*,*) PV, PL
endif
!Junção de contorno para determinar o ponto final mesmo se estourar a rede
if (PV > L) then
!PV = 1
PV = mod(PV,L)
elseif (PV < 1) then
!PV = L
PV = L - mod(PV,L)
else
endif
call random_seed()
call random_number(r)
if (r < 0.5) then
PL = PL-1
else
PL = PL+1
endif
!WRITE(*,*) PV, PL
!Junção de contorno para determinar o ponto final mesmo se estourar a rede
if (PL > L) then
!PL = 1
PL = mod(PV,L)
elseif (PL < 1) then
!PL = L
PL = L - mod(PV,L)
else
endif
enddo
WRITE(*,*) PV, PL
PVT = PV
PLT = PL
!Escrever na matriz
S(PV,PL) = S(PV,PL) + 1
!Calcular a distância total relativa ao ponto inicial (pitágoras)
Pit = sqrt((PVT*PVT)+(PLT*PLT))
Pitotal = Pitotal + Pit
enddo
WRITE(*,*) Pitotal
!Calcular a distância média
M = Pitotal/B
WRITE(*,*) M
!Calcular o Desvio Padrão
do DP1 = 1,B !Somatório
E = E + ((Pitotal-M) * (Pitotal-M))
enddo
!Dividir o somatório por B * (B-1)
DP2 = E / (B * (B-1))
!Extrair a raíz da divisão
DPF = sqrt(DP2)
WRITE(*,*) E, DP2, DPF
!Escrever os resultados
do x = 1, L
do y = 1, L
S(x,y) = S(x,y)/B
WRITE(10,*) x, y, S(x,y)
enddo
enddo
The culprit seems to be the number of steps desired, not even using the same method used in the 50/51 steps program seems to solve it (even though it would be incorrect given the fact that this program should generate steps up to 100/101, not just 100/101 steps). Any ideas on how I can solve this?
p.s. I'm using Visual Studio Code to compile the program and trying to run it on cmd, I already tried to use VSC to run it, but the issue persists.
Every time I compile this program I get these errors...
(This error occurs every time I try to call the subroutine.)
103 | call ColumnInsert(M(n,n), b, n, col, MatOut(n,n))
| 1
Error: Explicit interface required for ‘columninsert’ at (1): assumed-shape argument
(This error also occurs every time I run the function)
107 | detA = Determinant (MatOut(:,:), n)
| 1
Error: Type mismatch in argument ‘m’ at (1); passed INTEGER(4) to REAL(8)
Here is the main program:
program CramersRule
! System of equations. 2x2, 3x3
implicit none
! Declare varialble
integer :: n, row, col, i
real*8, allocatable :: Matrix1(:,:), b(:), x(:)
real*8 :: detA, detM, determinant
logical :: Success
! Open the input and output files.
open(42,file='Data2.txt')
open(43,file='Data2Out.txt')
! Solve each system in the input files.
do
! Read in size of first system.
read(42,*) n
if (n .eq. 0) exit ! Quit if zero.
! Allocate memory for system, right hand side, and solution vector.
allocate(Matrix1(n,n), b(n), x(n))
! Read in the system. Ask if you do not understand how this works!
do row = 1, n
read(42,*) (Matrix1(row, col), col = 1, n), b(row)
print*, Matrix1
enddo
! Use cramers rule to get solution.
call Cramer(Matrix1, b, n, x, Success)
if (Success) then
! Write solution to file
do row = 1, n
write(43,*) x(row)
enddo
write(43,*)
else ! This happens when there is no unique solution.
write(43,*) 'No Solution'
write(43,*)
endif
! clean up memory and go back up to top for next system.
deallocate(Matrix1, b, x)
enddo
! close files
close(42)
close(43)
end program CramersRule
subroutine Cramer(M, b, n, x, Success)
! This subroutine does Cramer's Rule
implicit none
! Declare and initialize your variables first.
real*8, allocatable :: M(:,:), b(:), x(:)
integer :: n, row, col, i
integer :: MatOut(n,n)
real*8 :: detA, detM, x1, x2, x3, Determinant, solution1, solution2, solution3
logical :: Success
! Find the determinant of M first. print it to screen.
detM = Determinant(M, n)
print*, "The determinant of this matrix is = ", detM
! If it is zero, set the Success logical variable and quit.
if (detM .eq. 0) then
Success = .false.
return
end if
! Allocate memory for a working matrix for column substituion. Then, for each
! column, i, substitute column i with vector b and get that determinant.
! Compute the ith solution.
if (n .eq. 2)then
col = 1
call ColumnInsert(M(n,n), b, n, col, MatOut(n,n))
print*, MatOut(:,:)
detA = Determinant (MatOut(:,:), n)
x1 = detA/detM
solution1 = x1
col = col + 1
call ColumnInsert(M, b, n, col, MatOut)
print*, MatOut(:,:)
detA = Determinant (MatOut(:,:), n)
x2 = detA/detM
solution2 = x2
success = .true.
return
else
col = 1
call ColumnInsert(M, b, n, col, MatOut)
print*, MatOut(:,:)
detA = Determinant (MatOut(:,:), n)
x1 = detA/detM
solution1 = x1
col = col + 1
call ColumnInsert(M, b, n, col, MatOut)
print*, MatOut(:,:)
detA = Determinant (MatOut(:,:), n)
x2 = detA/detM
solution2 = x2
col = col +1
call ColumnInsert(M, b, n, col, MatOut)
print*, MatOut(:,:)
detA = Determinant (MatOut(:,:), n)
x3 = detA/detM
solution3 = x3
success = .true.
return
end if
! deallocate memory for the working matrix.
deallocate(M, b, x)
end subroutine Cramer
subroutine ColumnInsert(M, b, n, col, MatOut)
! This subroutine takes vector b and inserts in into matrix M at column col.
implicit none
integer :: n
integer, intent(out) :: col, MatOut(:,:)
real :: a, b1, c, d, e, f, g, h, j, k, l, m1
double precision :: M(n,n), b(1,n)
if (n .eq. 2)then
a = M(1,1)
b1 = M(1,2)
c = M(2,1)
d = M(2,2)
e = b(1,1)
f = b(1,2)
!the next if statement substitutes based on which column the main program asks for
if (col .eq. 1)then
M(1,1) = e
M(1,2) = f
M(2,1) = c
M(2,2) = d
MatOut(:,:) = M(:,:)
print*, MatOut(:,:)
return
else
M(1,1) = a
M(1,2) = b1
M(2,1) = e
M(2,2) = f
MatOut(:,:) = M(:,:)
print*, MatOut(:,:)
return
endif
!this is for 3x3 matricies
else
a = M(1,1)
b1 = M(1,2)
c = M(1,3)
d = M(2,1)
e = M(2,2)
f = M(2,3)
g = M(3,1)
h = M(3,2)
j = M(3,3)
k = b(1,1)
l = b(1,2)
m1 = b(1,3)
if (col .eq. 1) then
M(1,1) = k
M(1,2) = l
M(1,3) = m1
M(2,1) = d
M(2,2) = e
M(2,3) = f
M(3,1) = g
M(3,2) = h
M(3,3) = j
MatOut(:,:) = M(:,:)
print*, MatOut(:,:)
return
else if (col .eq. 2)then
M(1,1) = a
M(1,2) = b1
M(1,3) = c
M(2,1) = k
M(2,2) = l
M(2,3) = m1
M(3,1) = g
M(3,2) = h
M(3,3) = j
MatOut(:,:) = M(:,:)
print*, MatOut(:,:)
return
else
M(1,1) = a
M(1,2) = b1
M(1,3) = c
M(2,1) = d
M(2,2) = e
M(2,3) = f
M(3,1) = k
M(3,2) = l
M(3,3) = m1
MatOut(:,:) = M(:,:)
print*, MatOut(:,:)
return
endif
endif
end subroutine ColumnInsert
function Determinant(M, n) result(Det)
!pulled straight from lab 2 in week 4
integer :: n
real*8 :: M(n,n), Det, a, b, c, d, e, f, g, h, j
if (n .eq. 2) then
a = M(1,1)
b = M(1,2)
c = M(2,1)
d = M(2,2)
Det = (a*d)-(b*c)
else
a = M(1,1)
b = M(1,2)
c = M(1,3)
d = M(2,1)
e = M(2,2)
f = M(2,3)
g = M(3,1)
h = M(3,2)
j = M(3,3)
Det = (a*e*j)+(b*f*g)+(c*d*h)-(c*e*g)-(b*d*j)-(a*f*h)
endif
end function Determinant
I know this might seem like a dumb question to be asking, but I cannot for the life of me find where I need to change something. Any help or guidance is greatly appreciated. Thanks!
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
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!
I need to calculate six dimensional integrals using Trapezoid in Fortran 90 in an efficient way. Here is an example of what I need to do:
Where F is a numerical (e.g. not analytical) function which is to be integrated over x1 to x6, variables. I have initially coded a one dimension subroutine:
SUBROUTINE trapzd(f,mass,x,nstep,deltam)
INTEGER nstep,i
DOUBLE PRECISION mass(nstep+1),f(nstep+1),x,deltam
x=0.d0
do i=1,nstep
x=x+deltam*(f(i)+f(i+1))/2.d0
end do
return
END
Which seems to work fine with one dimension, however, I don't know how to scale this up to six dimensions. Can I re-use this six times, once for every dimension or shall I write a new subroutine?
If you have a fully coded (no library/API use) version of this in another language like Python, MATLAB or Java, I'd be very glad to have a look and get some ideas.
P.S. This is not school homework. I am a PhD student in Biomedicine and this is part of my research in modeling stem cell activities. I do not have a deep background of coding and mathematics.
Thank you in advance.
You could look at the Monte Carlo Integration chapter of the GNU Scientific Library (GSL). Which is both a library, and, since it is open source, source code that you can study.
Look at section 4.6 of numerical recipes for C.
Step one is to reduce the problem using, symmetry and analytical dependencies.
Step two is to chain the solution like this:
f2(x2,x3,..,x6) = Integrate(f(x,x2,x3..,x6),x,1,x1end)
f3(x3,x4,..,x6) = Integrate(f2(x,x3,..,x6),x,1,x2end)
f4(x4,..,x6) = ...
f6(x6) = Integrate(I4(x,x6),x,1,x5end)
result = Integrate(f6(x),x,1,x6end)
Direct evaluation of multiple integrals is computationally challenging. It might be better to use Monte Carlo, perhaps using importance sampling. However brute force direct integration is sometimes of interest for validation of methods.
The integration routine I use is "QuadMo" written by Luke Mo about 1970. I made it recursive and put it in a module. QuadMo refines the mesh were needed to get the requested integration accuracy. Here is a program that does an n-dimensional integral using QuadMo.
Here is the validation of the program using a Gaussian centered at 0.5 with SD 0.1 in all dimensions for nDim up to 6, using a G95 compile. It runs in a couple of seconds.
nDim ans expected nlvl
1 0.249 0.251 2
2 6.185E-02 6.283E-02 2 2
3 1.538E-02 1.575E-02 2 2 2
4 3.826E-03 3.948E-03 2 2 2 2
5 9.514E-04 9.896E-04 2 2 2 2 2
6 2.366E-04 2.481E-04 2 2 2 2 2 2
Here is the code:
!=======================================================================
module QuadMo_MOD
implicit none
integer::QuadMo_MinLvl=6,QuadMo_MaxLvl=24
integer,dimension(:),allocatable::QuadMo_nlvlk
real*8::QuadMo_Tol=1d-5
real*8,save,dimension(:),allocatable::thet
integer,save::nDim
abstract interface
function QuadMoFunct_interface(thet,k)
real*8::QuadMoFunct_interface
real*8,intent(in)::thet
integer,intent(in),optional::k
end function
end interface
abstract interface
function MultIntFunc_interface(thet)
real*8::MultIntFunc_interface
real*8,dimension(:),intent(in)::thet
end function
end interface
procedure(MultIntFunc_interface),pointer :: stored_func => null()
contains
!----------------------------------------------------------------------
recursive function quadMoMult(funct,lower,upper,k) result(ans)
! very powerful integration routine written by Luke Mo
! then at the Stanford Linear Accelerator Center circa 1970
! QuadMo_Eps is error tolerance
! QuadMo_MinLvl determines initial grid of 2**(MinLvl+1) + 1 points
! to avoid missing a narrow peak, this may need to be increased.
! QuadMo_Nlvl returns number of subinterval refinements required beyond
! QuadMo_MaxLvl
! Modified by making recursive and adding argument k
! for multiple integrals (GuthrieMiller#gmail.com)
real*8::ans
procedure(QuadMoFunct_interface) :: funct
real*8,intent(in)::lower,upper
integer,intent(in),optional::k
real*8::Middle,Left,Right,eps,est,fLeft,fMiddle,fRight
& ,fml,fmr,rombrg,coef,estl,estr,estint,area,abarea
real*8::valint(50,2), Middlex(50), Rightx(50), fmx(50), frx(50)
& ,fmrx(50), estrx(50), epsx(50)
integer retrn(50),i,level
level = 0
QuadMo_nlvlk(k) = 0
abarea = 0
Left = lower
Right = upper
if(present(k))then
fLeft = funct(Left,k)
fMiddle = funct((Left+Right)/2,k)
fRight = funct(Right,k)
else
fLeft = funct(Left)
fMiddle = funct((Left+Right)/2)
fRight = funct(Right)
endif
est = 0
eps = QuadMo_Tol
100 level = level+1
Middle = (Left+Right)/2
coef = Right-Left
if(coef.ne.0) go to 150
rombrg = est
go to 300
150 continue
if(present(k))then
fml = funct((Left+Middle)/2,k)
fmr = funct((Middle+Right)/2,k)
else
fml = funct((Left+Middle)/2)
fmr = funct((Middle+Right)/2)
endif
estl = (fLeft+4*fml+fMiddle)*coef
estr = (fMiddle+4*fmr+fRight)*coef
estint = estl+estr
area= abs(estl)+ abs(estr)
abarea=area+abarea- abs(est)
if(level.ne.QuadMo_MaxLvl) go to 200
QuadMo_nlvlk(k) = QuadMo_nlvlk(k)+1
rombrg = estint
go to 300
200 if(( abs(est-estint).gt.(eps*abarea)).or.
1(level.lt.QuadMo_MinLvl)) go to 400
rombrg = (16*estint-est)/15
300 level = level-1
i = retrn(level)
valint(level, i) = rombrg
go to (500, 600), i
400 retrn(level) = 1
Middlex(level) = Middle
Rightx(level) = Right
fmx(level) = fMiddle
fmrx(level) = fmr
frx(level) = fRight
estrx(level) = estr
epsx(level) = eps
eps = eps/1.4d0
Right = Middle
fRight = fMiddle
fMiddle = fml
est = estl
go to 100
500 retrn(level) = 2
Left = Middlex(level)
Right = Rightx(level)
fLeft = fmx(level)
fMiddle = fmrx(level)
fRight = frx(level)
est = estrx(level)
eps = epsx(level)
go to 100
600 rombrg = valint(level,1)+valint(level,2)
if(level.gt.1) go to 300
ans = rombrg /12
end function quadMoMult
!-----------------------------------------------------------------------
recursive function MultInt(k,func) result(ans)
! MultInt(nDim,func) returns multi-dimensional integral from 0 to 1
! in all dimensions of function func
! variable QuadMo_Mod: nDim needs to be set initially to number of dimensions
procedure(MultIntFunc_interface) :: func
real*8::ans
integer,intent(in)::k
stored_func => func
if(k.eq.nDim)then
if(allocated(thet))deallocate(thet)
allocate (thet(nDim))
if(allocated(QuadMo_nlvlk))deallocate(QuadMo_nlvlk)
allocate(QuadMo_nlvlk(nDim))
endif
if(k.eq.0)then
ans=func(thet)
return
else
ans=QuadMoMult(MultIntegrand,0d0,1d0,k)
endif
end function MultInt
!-----------------------------------------------------------------------
recursive function MultIntegrand(thetARG,k) result(ans)
real*8::ans
real*8,intent(in)::thetARG
integer,optional,intent(in)::k
if(present(k))then
thet(k)=thetARG
else
write(*,*)'MultIntegrand: not expected, k not present!'
stop
endif
ans=MultInt(k-1,stored_func)
end function MultIntegrand
!-----------------------------------------------------------------------
end module QuadMo_MOD
!=======================================================================
module test_MOD
use QuadMo_MOD
implicit none
contains
!-----------------------------------------------------------------------
real*8 function func(thet) ! multidimensional function
! this is the function defined in nDim dimensions
! in this case a Gaussian centered at 0.5 with SD 0.1
real*8,intent(in),dimension(:)::thet
func=exp(-sum(((thet-5d-1)/1d-1)
& *((thet-5d-1)/1d-1))/2)
end function func
!-----------------------------------------------------------------------
end module test_MOD
!=======================================================================
! test program to evaluate multiple integrals
use test_MOD
implicit none
real*8::ans
! these values are set for speed, not accuracy
QuadMo_MinLvl=2
QuadMo_MaxLvl=3
QuadMo_Tol=1d-1
write(*,*)' nDim ans expected nlvl'
do nDim=1,6
! expected answer is (0.1 sqrt(2pi))**nDim
ans=MultInt(nDim,func)
write(*,'(i10,2(1pg10.3),999(i3))')nDim,ans,(0.250663)**nDim
& ,QuadMo_nlvlk
enddo
end
!-----------------------------------------------------------------------
double MultInt(int k);
double MultIntegrand(double thetARG, int k);
double quadMoMult(double(*funct)(double, int), double lower, double upper, int k);
double funkn(double *thet);
int QuadMo_MinLvl = 2;
int QuadMo_MaxLvl = 3;
double QuadMo_Tol = 0.1;
int *QuadMo_nlvlk;
double *thet;
int nDim;
//double MultInt(int k, double(*func)(double *))
double MultInt(int k)
{
//MultInt(nDim, func) returns multi - dimensional integral from 0 to 1
//in all dimensions of function func
double ans;
if (k == 0)
{
ans = funkn(thet);
}
else
{
ans = quadMoMult(MultIntegrand, 0.0, 1.0, k); //limits hardcoded here
}
return ans;
}
double MultIntegrand(double thetARG, int k)
{
double ans;
if (k > 0)
thet[k] = thetARG;
else
printf("\n***MultIntegrand: not expected, k not present!***\n");
//Recursive call
//ans = MultInt(k - 1, func);
ans = MultInt(k - 1);
return ans;
}
double quadMoMult(double(*funct)(double, int), double lower, double upper, int k)
{
//Integration routine written by Luke Mo
//Stanford Linear Accelerator Center circa 1970
//QuadMo_Eps is error tolerance
//QuadMo_MinLvl determines initial grid of 2 * *(MinLvl + 1) + 1 points
//to avoid missing a narrow peak, this may need to be increased.
//QuadMo_Nlvl returns number of subinterval refinements required beyond
//QuadMo_MaxLvl
//Modified by making recursive and adding argument k
//for multiple integrals(GuthrieMiller#gmail.com)
double ans;
double Middle, Left, Right, eps, est, fLeft, fMiddle, fRight;
double fml, fmr, rombrg, coef, estl, estr, estint, area, abarea;
double valint[51][3], Middlex[51], Rightx[51], fmx[51], frx[51]; //Jack up arrays
double fmrx[51], estrx[51], epsx[51];
int retrn[51];
int i, level;
level = 0;
QuadMo_nlvlk[k] = 0;
abarea = 0.0;
Left = lower;
Right = upper;
if (k > 0)
{
fLeft = funct(Left, k);
fMiddle = funct((Left + Right) / 2, k);
fRight = funct(Right, k);
}
else
{
fLeft = funct(Left,0);
fMiddle = funct((Left + Right) / 2,0);
fRight = funct(Right,0);
}
est = 0.0;
eps = QuadMo_Tol;
l100:
level = level + 1;
Middle = (Left + Right) / 2;
coef = Right - Left;
if (coef != 0.0)
goto l150;
rombrg = est;
goto l300;
l150:
if (k > 0)
{
fml = funct((Left + Middle) / 2.0, k);
fmr = funct((Middle + Right) / 2.0, k);
}
else
{
fml = funct((Left + Middle) / 2.0, 0);
fmr = funct((Middle + Right) / 2.0, 0);
}
estl = (fLeft + 4 * fml + fMiddle)*coef;
estr = (fMiddle + 4 * fmr + fRight)*coef;
estint = estl + estr;
area = abs(estl) + abs(estr);
abarea = area + abarea - abs(est);
if (level != QuadMo_MaxLvl)
goto l200;
QuadMo_nlvlk[k] = QuadMo_nlvlk[k] + 1;
rombrg = estint;
goto l300;
l200:
if ((abs(est - estint) > (eps*abarea)) || (level < QuadMo_MinLvl))
goto l400;
rombrg = (16 * estint - est) / 15;
l300:
level = level - 1;
i = retrn[level];
valint[level][i] = rombrg;
if (i == 1)
goto l500;
if (i == 2)
goto l600;
l400:
retrn[level] = 1;
Middlex[level] = Middle;
Rightx[level] = Right;
fmx[level] = fMiddle;
fmrx[level] = fmr;
frx[level] = fRight;
estrx[level] = estr;
epsx[level] = eps;
eps = eps / 1.4;
Right = Middle;
fRight = fMiddle;
fMiddle = fml;
est = estl;
goto l100;
l500:
retrn[level] = 2;
Left = Middlex[level];
Right = Rightx[level];
fLeft = fmx[level];
fMiddle = fmrx[level];
fRight = frx[level];
est = estrx[level];
eps = epsx[level];
goto l100;
l600:
rombrg = valint[level][1] + valint[level][2];
if (level > 1)
goto l300;
ans = rombrg / 12.0;
return ans;
}
double funkn(double *thet)
{
//in this case a Gaussian centered at 0.5 with SD 0.1
double *sm;
double sum;
sm = new double[nDim];
sum = 0.0;
for (int i = 1; i <= nDim; i++)
{
sm[i] = (thet[i] - 0.5) / 0.1;
sm[i] *= sm[i];
sum = sum + sm[i];
}
return exp(-sum / 2.0);
}
int main() {
double ans;
printf("\nnDim ans expected nlvl\n");
for (nDim = 1; nDim <= 6; nDim++)
{
//expected answer is(0.1 sqrt(2pi))**nDim
QuadMo_nlvlk = new int[nDim + 1]; //array for x values
thet = new double[nDim + 1]; //array for x values
ans = MultInt(nDim);
printf("\n %d %f %f ", nDim, ans, pow((0.250663),nDim));
for (int j=1; j<=nDim; j++)
printf(" %d ", QuadMo_nlvlk[nDim]);
printf("\n");
}
return 0;
}
Declare relevant parameters globally
int QuadMo_MinLvl = 2;
int QuadMo_MaxLvl = 3;
double QuadMo_Tol = 0.1;
int *QuadMo_nlvlk;
double *thet;
int nDim;
This coding is much clearer than the obfuscated antiquated fortran coding, with some tweaking the integral limits and tolerances could be parameterised!!
There are better algorithms to use with adaptive techniques and which handle singularities on the surfaces etc....