Professional Documents
Culture Documents
Computational Physics - 2007 - Landau - Fortran 95 Codes
Computational Physics - 2007 - Landau - Fortran 95 Codes
479
C
Fortran 95 Codes
I m p l i c i t none
Real * 8 : : r , ranDom , lambda
I n t e g e r : : i , j , h , n l e f t , nloop , s t a r t , seed
Program b e s s e l
I m p l i c i t none
Real * 8 : : step , x , xmin , xmax , up , Down, t1 , t 2
I n t e g e r : : order , s t a r t
xmin = 0 . 2 5
xmax = 4 0 . 0
step = 0.1
order = 10
s t a r t = 50
open ( 6 , F i l e = ’bessel.dat’ , S t a t u s = ’Unknown’ ) ! open output f i l e
Do x = xmin , xmax , s t e p
t 1 = Down( x , order , s t a r t )
t 2 = up ( x , order )
wr i t e ( 6 , 5 0 ) x , t1 , t 2
End Do
Close ( 6 )
50 Format ( f 1 5 . 1 0 , f 1 5 . 1 0 , f 1 5 . 1 0 )
Stop ’data saved in bessel.dat’
End Program b e s s e l
! c a l c u l a t e using Downward r e c u r s i o n
Function Down( x , order , s t a r t )
I m p l i c i t none
I n t e g e r : : k , order , s t a r t
Real * 8 : : Down, s c a l e , x , j ( 1 0 0 )
! the a r b i t r a r y s t a r t
j ( s t a r t + 1) = 1
j ( start ) = 1
Do k = s t a r t , 2 , − 1
j ( k − 1 ) = ( ( 2 * k − 1 . 0 ) /x ) * j ( k ) − j ( k + 1 )
End Do
! s c a l e so t h a t j ( 1 ) = s i n ( x ) /x
s c a l e = ( s i n ( x ) /x ) / j ( 1 )
Down = j ( order + 1 ) * s c a l e
Return
End
! c a l c u l a t e using upward r e c u r s i o n
Function up ( x , order )
I m p l i c i t none
I n t e g e r : : k , order
Real * 8 : : up , x , one , two , t h r
one = s i n ( x ) /x
two = ( s i n ( x ) − x * cos ( x ) ) /( x * x )
Do k = 1 , ( order − 1 )
t h r = ( ( 2 * k + 1 . 0 ) /x ) * two − one
one = two
two = t h r
End Do
up = t h r
Return
End
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
481
Program d i f f
I m p l i c i t none
Real * 8 : : f , h , r e s u l t ( 3 ) , x , xmin , xmax , x s t e p
Program h e a t
I m p l i c i t none
Double p r e c i s i o n : : cons , ro , sph , thk , u ( 1 0 1 , 2 )
I n t e g e r : : i , k , max
Program e q s t r i n g
I m p l i c i t none
Real * 8 : : x ( 1 0 1 , 3 )
I n t e g e r : : i , j , k , max
max = 100
open ( 9 , FILE = ’eqstring.dat’ , S t a t u s = ’Unknown’ )
Do i = 1 , 101
Do j = 1 , 3
x ( i , j ) = 0.0
End Do
End Do
! initialize
Do i = 1 , 80
x ( i , 1) = 0.00125* i
End Do
Do i = 8 1 , 101
x ( i , 1) = 0.1 − 0. 005* ( i− 81)
End Do
! f i r s t time s t e p
Do i = 2 , 100
x ( i , 2 ) = x ( i , 1 ) + 0 . 5 * ( x ( i +1 , 1 ) + x ( i − 1, 1 ) − 2 . * x ( i , 1 ) )
End Do
! o t h e r time s t e p s
Do k = 1 , max
Do i = 2 , 100
x ( i , 3 ) = 2 . * x ( i , 2 ) − x ( i , 1 ) + ( x ( i + 1 , 2 ) + x ( i − 1 ,2) − 2 . * x ( i , 2 ) )
End Do
Do i = 1 , 101
x ( i , 1) = x ( i , 2) ! new − > old
x ( i , 2) = x ( i , 3)
End Do
I f ( modulo ( k , 1 0 ) == 0 ) then
! output data every 10 s t e p s
Do i = 1 , 101
wr i t e ( 9 , 1 1 ) x ( i , 3 )
End Do
write ( 9 , * ) ’ ’
Endif
End Do
11 format ( e12 . 6 )
close (9)
Stop ’data saved in eqstring.dat (for gnuplot)’
End Program e q s t r i n g
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
484 C Fortran 95 Codes
Program expbad
I m p l i c i t none
! min = accuracy , x step , max i n x , up numer , down denomin .
Real * 8 : : down , min , max , step , sum , up , x
Integer : : i , j
min = 1E − 10
max = 1 0 .
step = 0.1
open ( 6 , F i l e = ’exp-bad.dat’ , S t a t u s = ’Unknown’ )
! summation
Do x = 0 , max , s t e p
sum = 1
i = 0
down = 1
up = 1
! while loop may never s t o p
Do while ( ( sum == 0 ) . or . ( abs ( ( up/down) /sum ) > min ) )
i = i + 1
down = 1
up = 1
Do j = 1 , i
up = − up * x
down = down * j
End Do
sum = sum + up/down
End Do
w r i t e ( 6 , * ) x , sum
End Do
close (6)
Stop ’data saved in exp-bad.dat’
End Program expbad
Program expgood
I m p l i c i t none
Real * 8 : : element , min , max , step , sum , x
Integer : : n
min = 1E − 10
max = 1 0 .
step = 0.1
open ( 6 , F i l e = ’exp-good.dat’ , S t a t u s = ’Unknown’ )
! summation
Do x = 0 , max , s t e p
sum = 1
element = 1
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
485
n = 0
! while loop may never s t o p
Do while ( ( abs ( element/sum ) > min ) . or . ( sum . eq . 0 ) )
n = n + 1
element = element * ( − x ) /n
sum = sum + element
End Do
w r i t e ( 6 , * ) x , sum
End Do
close (6)
Stop ’data saved in exp-good.dat’
End Program expgood
Program f i t
I m p l i c i t none
Integer : : i
Real * 8 : : s , sx , sy , sxx , sxy , d e l t a , i n t e r , s l o p e
Real * 8 : : x ( 1 2 ) , y ( 1 2 ) , d ( 1 2 )
Data y /328 , 1 8 7 , 8 2 1 , 7 8 , 8 8 , 6 , 5 , 8 2 , 2 , 0 . 1 , 8 4 , 1/ ! y v a l u e s
Do i = 1 , 12 ! values x
x ( i ) = i *10 − 5
End Do
! input d e lt a y
Do i = 1 , 12
d( i ) = 1.
End Do
s = 0.0; sx = 0 . ; sy = 0 .
sxx = 0 .
sxy = 0 .
! c a l c u l a t e sums
Do i = 1 , 12
s = s + 1 / ( d ( i ) *d ( i ) )
sx = sx + x ( i ) / (d( i ) *d( i ) )
sy = sy + y ( i ) / (d( i ) *d( i ) )
sxx = sxx + x ( i ) * x ( i ) / ( d ( i ) * d ( i ) )
sxy = sxy + x ( i ) * y ( i ) / ( d ( i ) * d ( i ) )
End Do
! calculate coefficients
d e l t a = s * sxx − sx * sx
slope = ( s * sxy − sx * sy ) / d e l t a
i n t e r = ( sxx * sy − sx * sxy ) / d e l t a
w r i t e ( * , * ) ’intercept = ’ , i n t e r
w r i t e ( * , * ) ’slope = ’ , s l o p e
w r i t e ( * , * ) ’correlation = ’ , − sx/ s q r t ( sxx * s )
Stop ’fit’
End Program f i t
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
486 C Fortran 95 Codes
I n t e g e r : : npts , job , m, i , j
Real * 8 : : x ( npts ) , w( npts ) , a , b , x i
Real * 8 : : t , t1 , pp , p1 , p2 , p3 , a j
Real * 8 : : eps , pi , zero , two , one , h a l f , q u a r t e r
parameter ( p i = 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 8 , eps = 3 . 0 E − 1 4 )
parameter ( zero = 0 . d0 , one = 1 . d0 , two = 2 . d0 )
parameter ( h a l f = 0 . 5 d0 , q u a r t e r = 0 . 2 5 d0 )
m = ( npts + 1 ) /2
Do i = 1 , m
t = cos ( p i * ( i − q u a r t e r ) /( npts + h a l f ) )
10 continue
p1 = one
p2 = zero
a j = zero
Do j = 1 , npts
p3 = p2
p2 = p1
a j = a j + one
p1 = ( ( two * a j − one ) * t * p2 − ( a j − one ) * p3 ) / a j
End Do
pp = npts * ( t * p1 − p2 ) /( t * t − one )
t1 = t
t = t 1 − p1/pp
I f ( abs ( t − t 1 ) > eps ) goto 10
x( i ) = − t
x ( npts + 1 − i ) = t
w( i ) = two / ( ( one − t * t ) * pp * pp )
w( npts + 1 − i ) = w( i )
End Do
! r e s c a l e grid points
s e l e c t case ( j o b )
! s c a l e t o ( a , b ) uniformly
case ( 0 )
Do i = 1 , npts
x ( i ) = x ( i ) * ( b − a ) /two + ( b + a ) /two
w( i ) = w( i ) * ( b − a ) /two
End Do
! s c a l e t o ( 0 , b ) with 50% p o i n t s i n s i d e ( 0 , ab /( a + b ) )
case ( 1 )
Do i = 1 , npts
xi = x ( i )
x ( i ) = a * b * ( one + x i ) /( b + a − ( b − a ) * x i )
w( i ) = w( i ) * two * a * b * b / ( ( b + a − (b−a ) * x i ) * ( b + a − ( b−a ) * x i ) )
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
487
End Do
! s c a l e t o ( a , i n f ) with 50% i n s i d e ( a , b + 2 a )
case ( 2 )
Do i = 1 , npts
xi = x ( i )
x ( i ) = ( b * x i + b + a + a ) /( one − x i )
w( i ) = w( i ) * two * ( a + b ) / ( ( one − x i ) * ( one − x i ) )
End Do
End s e l e c t
Return
End
Program i n t 1 0 d
I m p l i c i t none
In t e g e r : : m = 16 , k ! number o f t r i a l s
Real * 8 : : s , i n t e g ( 1 6 )
s = 0.
Do k = 1 , m
c a l l montecarlo ( i n t e g , k ) ;
s = s + integ (k )
End Do
w r i t e ( * , * ) s/m
End Program i n t 1 0 d
s u b r o u t i n e montecarlo ( i n t e g , k )
I m p l i c i t none
I n t e g e r : : i , j , k , max = 65536
Real * 8 : : x , y , sum , ranDom , i n t e g ( 1 6 )
x = 0.
y = 0.
sum = 0 .
Do i = 1 , max
x = 0 ! reset x
! sum 10 x v a l u e s
Do j = 1 , 10
x = x + ranDom ( )
End Do
! square and sum up
y = y + x*x
sum = sum + y/ i ;
End Do
i n t e g ( k ) = sum/max
write ( * , * ) k , integ ( k )
End
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
488 C Fortran 95 Codes
Program harmos
I m p l i c i t None
Real * 8 : : psr ( 7 5 0 , 2 ) , p s i ( 7 5 0 , 2 ) , v ( 7 5 0 ) , p2 ( 7 5 0 )
Real * 8 : : pi , dx , k0 , dt , x
Complex : : exc , z i
I n t e g e r : : max , i , j , n
End Do
11 Format ( E12 . 6 )
Close ( 9 )
Stop ’data saved in harmos.dat (for gnuplot)’
End
Program l a g r a n g e
I m p l i c i t none
Real * 8 : : i n t e r , x , x i n ( 9 ) , yin ( 9 )
Integer : : i , e
e = 9
open ( 6 , F i l e = ’lagrange.dat’ , S t a t u s = ’Unknown’ )
! Input data
data x i n /0 , 8 5 , 5 8 0 , 7 5 8 , 8 0 0 , 1 2 8 5 , 8 5 0 , 7 9 5 , 82/
data yin / 1 8 . 6 , 1 6 , 8 5 , 8 3 . 5 , 5 8 . 8 , 1 9 . 9 , 1 0 . 8 , 8 8 . 2 5 , 4 . 7 /
! Calculate f ( x )
Do i = 0 , 1000
x = i *0.2
w r i t e ( 6 , * ) x , i n t e r ( xin , yin , e , x )
End Do
Close ( 6 )
Stop ’data saved in lagrange.dat’
End Program l a g r a n g e
! Evaluate i n t e r p o l a t i o n function ( x )
Function i n t e r ( xin , yin , e , x )
I m p l i c i t none
Integer : : i , j , e
Real * 8 : : i n t e r , lambda ( 9 ) , x i n ( 9 ) , yin ( 9 ) , x
inter = 0
Do i = 1 , e
lambda ( i ) = 1
Do j = 1 , e
I f ( i . neqv . j ) then
lambda ( i ) = lambda ( i ) * ( ( x − x i n ( j ) ) /( x i n ( i ) − x i n ( j ) ) )
Endif
End Do
i n t e r = i n t e r + ( yin ( i ) * lambda ( i ) )
End Do
Return
End
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
490 C Fortran 95 Codes
Program i n t e g r a t e
I m p l i c i t none
Real * 8 : : t r a p e z , simpson , quad , r1 , r2 , r 3 ! declarations
Real * 8 : : theo , vmin , vmax
Integer : : i
theo = 0 . 6 3 2 1 2 0 5 5 8 8 2 9 ! t h e o r e t i c a l r e s u l t , i n t e g r a t i o n range
vmin = 0 .
vmax = 1 .
open ( 6 , F i l e = ’integ.dat’ , S t a t u s = ’Unknown’ )
! c a l c u l a t e i n t e g r a l using both methods f o r s t e p s = 3 . . 5 0 1
Do i = 3 , 501 , 2
r 1 = t r a p e z ( i , vmin , vmax )
r 1 = abs ( r 1 − theo )
r 2 = simpson ( i , vmin , vmax )
r 2 = abs ( r 2 − theo )
r 3 = quad ( i , vmin , vmax )
r 3 = abs ( r 3 − theo )
w r i t e ( 6 , * ) i , r1 , r2 , r 3
End Do
close (6)
Stop ’data saved in integ.dat’
End Program i n t e g r a t e
! Function we want t o i n t e g r a t e
Function f ( x )
I m p l i c i t none
Real * 8 : : f , x
f = exp ( − x )
Return
End
I m p l i c i t none
Integer : : I , N
Real * 8 : : eps , one
N = 60 ! number o f i t e r a t i o n s N
eps = 1 . ! s e t i n i t i a l values
one = 1 . 0
! add eps t o one and p r i n t r e s u l t
Do I = 1, N
eps = eps / 2
one = 1 + eps
w r i t e ( * , * ) I , one , eps
End Do
Stop ’limit’
End Program l i m i t
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
492 C Fortran 95 Codes
Program LaplaceSOR
I m p l i c i t none
I n t e g e r : : max = 4 0 , i , j , i t e r
Real * 8 : : t o l , omega , r , p ( 4 0 , 4 0 )
I m p l i c i t none
I n t e g e r : : i t , imax = 10 ! Maximum number o f i t e r a t i o n s p e r m i t t e d
Real * 8 : : x , dx = 1 e − 2 , eps = 1 e − 6 , f1 , df , F
! x guess , must be c l o s e t o r o o t
x = 2.
Do i t = 0 , imax
f1 = F(x) ! Compute Function value
write ( * , * ) i t , x , f1
! Central d iffe re n ce derivative
df = ( F ( x + dx /2) − F ( x − dx /2) ) /dx
dx = − f 1 /df
x = x + dx ! New guess
! Check f o r convergence
I f ( abs ( F ( x ) ) <= eps ) then
w r i t e ( * , * ) eps
Stop
Endif
End Do
End Program Newton_cd
Function F ( x )
I m p l i c i t none
Real * 8 : : x , F
F = 2 * cos ( x ) − x
End
I m p l i c i t none
I n t e g e r : : i t , imax = 10 ! Max number i t e r a t i o n s
Real * 8 : : x , dx = 1 e − 2 , eps = 1 e − 6 , df , F
! Guess must be c l o s e
x = 2.
Do i t = 1 , imax
! Forward d i f f e r e n c e d e r i v a t i v e
df = ( F ( x + dx ) − F ( x ) ) /dx
dx = − F ( x ) /df
x = x + dx ! New guess
write ( * , * ) i t , x , F ( x )
! Check f o r convergence
I f ( abs ( F ( x ) ) <= eps ) then
w r i t e ( * , * ) eps
Stop
Endif
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
494 C Fortran 95 Codes
End Do
End
! Find zero o f t h i s f u n c t i o n
function F ( x )
I m p l i c i t none
Real * 8 : : x , F
F = 2 * cos ( x ) − x
End
Program overflow
I m p l i c i t none
Integer : : I , N
Real * 8 : : under , over
N = 1024 ! number o f i t e r a t i o n s , may need b i g g e r
under = 1 . ! s e t i n i t i a l values
over = 1.
Do I = 1 , N ! c a l c underflow and overflow , output t o s c r e e n
under = under / 2
over = over * 2
w r i t e ( * , * ) I , over , under
End Do
Stop ’overflow’
End Program overflow
Program pond
I m p l i c i t none
Real * 8 : : area , x , y , ranDom
I n t e g e r : : i , max , p i
max = 2000
! open f i l e , s e t i n i t i a l value , seed g e n e r a t o r
Open ( 6 , F i l e = ’pond.dat’ , S t a t u s = ’Unknown’ )
pi = 0
! e x e cu t e
Do i = 1 , max
x = ranDom ( ) * 2 − 1
y = ranDom ( ) * 2 − 1
I f ( ( x * x + y * y ) <= 1 ) then
pi = pi + 1
Endif
a r e a = 4 . * p i /Real ( i )
write ( 6 , * ) i , area
End Do
close (6)
Stop ’data saved in pond.dat’
End Program pond
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
495
Program qmc
I m p l i c i t none
I n t e g e r : : i , j , max , element , prop ( 1 0 0 )
Real * 8 : : change , ranDom , energy , newE , oldE , out , path ( 1 0 0 )
max = 250000
open ( 9 , FILE = ’qmc.dat’ , S t a t u s = ’Unknown’ )
! i n i t i a l path and p r o b a b i l i t y
Do j = 1 , 100
path ( j ) = 0 . 0
prop ( j ) = 0
End Do
! f i n d energy o f i n i t i a l path
oldE = energy ( path , 1 0 0 )
! p i ck random element , change by random
Do i = 1 , max
element = ranDom ( ) * 1 0 0 + 1
change = ( ( ranDom ( ) − 0 . 5 ) * 2 )
path ( element ) = path ( element ) + change
newE = energy ( path , 1 0 0 ) ! f i n d new energy
! Metropolis algorithm
I f ( ( newE > oldE ) .AND. ( exp ( − newE + oldE ) < ranDom ( ) ) ) then
path ( element ) = path ( element ) − change
EndIf
! add up p r o b a b i l i t i e s
Do j = 1 , 100
element = path ( j ) * 1 0 + 50
prop ( element ) = prop ( element ) + 1
End Do
oldE = newE
End Do
! wr i t e output data t o f i l e
Do j = 1 , 100
out = prop ( j )
w r i t e ( 9 , * ) j − 5 0 , out/max
End Do
close (9)
Stop ’data saved in qmc.dat’
End Program qmc
! Function c a l c u l a t e s energy o f t h e system
Function energy ( array , max )
I m p l i c i t none
I n t e g e r : : i , max
Real * 8 : : energy , a r r a y ( max )
energy = 0
Do i = 1 , ( max − 1 )
energy = energy + ( a r r a y ( i + 1 ) − a r r a y ( i ) ) * * 2 + a r r a y ( i ) * * 2
End Do
Return
End
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
496 C Fortran 95 Codes
Program o s c i l l a t o r
I m p l i c i t none
! n : number o f e q u a t i o n s , min/max i n x , d i s t : l e n g t h o f x − s t e p s
! y ( 1 ) : i n i t i a l position , y ( 2 ) : i n i t i a l v e locit y
Real * 8 : : d i s t , min1 , max1 , x , y ( 5 )
Integer : : n
n = 2
min1 = 0 . 0 ; max1 = 1 0 . 0
dist = 0.1
y(1) = 1.0; y(2) = 0.
open ( 6 , F i l e = ’rk4.dat’ , S t a t u s = ’Unknown’ )
! Do n s t e p s rk a l g o r i t h m
Do x = min1 , max1 , d i s t
c a l l rk4 ( x , d i s t , y , n )
write ( 6 , * ) x , y ( 1 )
End Do
close (6)
Stop ’data saved in rk4.dat’
End Program o s c i l l a t o r ! End o f main Program
Program Rk45
I m p l i c i t none
Real * 8 : : h , t , s , hmin , hmax , Tol = 2 * 1 E − 7 , Tmin = 0 . , &
Tmax = 1 0 .
Real * 8 , dimension ( 2 ) : : y , FReturn , ydumb , k1 , k2 , k3 , k4 , &
k5 , k6 , e r r
I n t e g e r : : i , Ntimes = 10
Do i = 1 , 2
k6 ( i ) = h * FReturn ( i )
e r r ( i ) = abs ( k1 ( i ) /360 − 1 2 8 * k3 ( i ) /4275 − 2 1 9 7 * k4 ( i ) /75240 &
+ k5 ( i ) / 5 0 . + 2 * k6 ( i ) /55 )
End Do
I f ( ( e r r ( 1 ) < Tol ) . or . ( e r r ( 2 ) < Tol ) . or . ( h <= 2 * hmin ) ) then
! a c c e p t approximation
Do i = 1 , 2
y ( i ) = y ( i ) + 2 5 * k1 ( i ) / 2 1 6 . + 1 4 0 8 * k3 ( i ) / 2 5 6 5 . &
+ 2 1 9 7 * k4 ( i ) / 4 1 0 4 . − k5 ( i ) / 5 .
End Do
t = t + h
Endif
I f ( ( e r r ( 1 ) == 0 ) . or . ( e r r ( 2 ) == 0 ) ) then
s = 0 ! t r a p d i v i s i o n by 0
else
s = 0 . 8 4 * Tol * h/ e r r ( 1 ) * * 0 . 2 5 ! step size scalar
Endif
I f ( ( s < 0 . 7 5 ) . and . ( h > 2 * hmin ) ) then
h = h / 2 . ! reduce s t e p
e l s e I f ( ( s > 1 . 5 ) . and . ( 2 * h < hmax ) ) then
h = h*2. ! increase step
Endif
! End loop
End Do
close (6)
Stop’Data stored in rk45.dat’
End Program Rk45
! PLACE YOUR FUNCTION HERE
subroutine f ( t , y , FReturn )
I m p l i c i t none ; Real * 8 t , y ( 2 ) , FReturn ( 2 )
FReturn ( 1 ) = y ( 2 ) ! RHS o f f i r s t e q u a t i o n
FReturn ( 2 ) = − 1 0 0 * y ( 1 ) − 2 * y ( 2 ) + 1 0 * s i n ( 3 * t ) ! RHS o f 2nd e q u a t i o n
Return
End
Program random
I m p l i c i t none
I n t e g e r : : i , number , old , seed , x , y
y = modulo ( ( 5 7 * x + 1 ) , 2 5 6 )
write ( 6 , * ) x , y
old = y
End Do
close (6)
Stop ’data saved in ranDom.dat’
End Program random
Program s c a t t
I n t e g e r : : n , S i z e , i , j , Row, Column
Double P r e c i s i o n : : b , Pot
Parameter ( S i z e = 3 0 0 , p i = 3 . 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 , b = 1 0 . 0 )
Double P r e c i s i o n : : lambda , s c a l e , ko , Temp
Double P r e c i s i o n : : F ( S i z e , S i z e ) , k ( S i z e ) , w( S i z e ) ,D( S i z e ) , r ( S i z e )
Double P r e c i s i o n : : V( S i z e ) , L ( S i z e , S i z e ) ,U( S i z e , S i z e ) , P ( S i z e , S i z e )
Integer : : PivotInfo ( Size )
! E n t e r p o t e n t i a l s t r e n g t h lambda
Write ( * , * ) ’enter lambda’
Read ( * , * ) lambda
Write ( * , * ) ’enter scaling factor’
Read ( * , *) scale
Write ( * , * ) ’enter ko’
Read ( * , * ) ko
Write ( * , * ) ’enter grid size’
Read ( * , *) n
! S e t up Gaussian i n t e g r a t i o n p o i n t s and weights
! on i n t e r v a l [ 0 , i n f ] with t h e mid − p o i n t a t ’scale’
! S e t l a s t element i n k a r r a y t o ko
c a l l gauss ( n , 2 , 0d0 , s c a l e , k , w)
! S e t up D m a t r i x
Do i = 1 , n
D( i ) = 2 . 0 d0/ p i *w( i ) * k ( i ) * k ( i ) /( k ( i ) * k ( i ) − ko * ko )
End Do
D( n + 1 ) = 0 . 0
Do j = 1 , n
D( n + 1 ) = D( n + 1 ) + w( j ) * ko * ko /( k ( j ) * k ( j ) − ko * ko )
End Do
D( n + 1 ) = D( n + 1 ) * ( − 2 . 0 d0/ p i )
! S e t up F m a t r i x and V v e c t o r
Do i = 1 , n
Do j = 1 , n
Pot = − b * b * lambda * SIN ( b * k ( i ) ) * SIN ( b * k ( j ) )
Pot = Pot /( k ( i ) * b * k ( j ) * b )
F ( i , j ) = Pot *D( j )
I f ( i == j ) then
F ( i , j ) = F ( i , j ) + 1 . 0 d0
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
500 C Fortran 95 Codes
Endif
End Do
V( i ) = Pot
End Do
! LU f a c t o r i z a t i o n . Put LU f a c t o r s o f F i n corresponding m a t r i x
! ( not e f f i c i e n t but easy ) . S t o r e p a r t i a l p i v o t i n g i n f o
!
c a l l LUfactor ( F , n , S i z e , L , U, P i v o t I n f o )
! P i v o t and s o l v e
! Set P to i d e n t i t y matrix
Do Row = 1 , n + 1
Do Column = 1 , n + 1
P (Row, Column ) = 0
I f (Row . EQ . Column ) P (Row, Column ) = 1
End Do
End Do
! In t e r ch a n g e rows t o g e t t r u e P m a t r i x
Do Row = 1 , n
Do Column = 1 , n
Temp = P (Row, Column )
P (Row, Column ) = P ( P i v o t I n f o (Row) , Column )
P ( P i v o t I n f o (Row) , Column ) = Temp
End Do
End Do
c a l l LUSolve ( V, L , U, n , S i z e , P i v o t I n f o , r )
! output r e s u l t s
w r i t e ( * , * ) ko * ko , DATAN( − r ( n ) * ko )
End Program s c a t t
! LU f a c t o r i z a t i o n , p a r t i a l p i v o t i n g o f A i n Ax = b
s u b r o u t i n e LUfactor (A, n , S i z e , L , U, P i v o t I n f o )
I n t e g e r : : n , Column , CurrentPivotRow , CurrentRow , SwapCol , Row
I n t e g e r : : ElimCol , S i z e
Double P r e c i s i o n : : A( S i z e , S i z e ) , L ( S i z e , S i z e ) , U( S i z e , S i z e )
Integer : : PivotInfo ( Size )
Double P r e c i s i o n : : CurrentPivotValue , Swap
Do Column = 1 , n − 1
CurrentPivotRow = Column
C u r r e n t P i v o t V al u e = A( CurrentPivotRow , Column )
! Determine row f o r l a r g e s t p i v o t
Do CurrentRow = Column + 1 , n
I f ( DABS(A( CurrentRow , Column ) ) . GT . C u r r e n t P i v o t V a lu e ) Then
C u r r e n t P i v o t V a l ue = DABS(A( CurrentRow , Column ) )
CurrentPivotRow = CurrentRow
Endif
End Do
P i v o t I n f o ( Column ) = CurrentPivotRow
! Swap rows so l a r g e s t value a t p i v o t
Do SwapCol = Column , n
Swap = A( Column , SwapCol )
A( Column , SwapCol ) = A( P i v o t I n f o ( Column ) , SwapCol )
A( P i v o t I n f o ( Column ) , Swapcol ) = Swap
End Do
!
! Gauss Elimin , upper t r i a n g u l a r A, unpivoted lower t r i a n g u l a r L
!
Do Row = Column + 1 , n
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
501
! P a r t o f an LU decomposition + p a r t i a l p i v o t i n g t o s o l v e Ax = b
S u b r o u t i n e LUSolve ( b , L , U, n , S i z e , P i v o t I n f o , x )
I n t e g e r : : n , S i z e , Row, Column
Double P r e c i s i o n : : b ( S i z e ) , x ( S i z e )
Integer : : PivotInfo ( Size )
Double P r e c i s i o n : : L ( S i z e , S i z e ) , U( S i z e , S i z e )
Double P r e c i s i o n : : Temp
Do Row = 1 , n ! In t e r ch a n g e rows o f b f o r p i v o t i n g
Temp = b (Row)
b (Row) = b ( P i v o t I n f o (Row) )
b ( P i v o t I n f o (Row) ) = Temp
End Do
! S o l v e Ly = b , where y = Ux , by forward e l i m i n a t i o n
Do Row = 2 , n
DO Column = 1 , Row − 1
b (Row) = b (Row) − L (Row, Column ) * b ( Column )
End Do
b (Row) = b (Row) /L (Row, Row)
End Do
! S o l v e Ux = y by back s u b s t i t u t i o n
x ( n ) = b ( n ) /U( n , n )
Do Row = n − 1 , 1 , − 1
x (Row) = b (Row)
Do Column = Row + 1 , n
x (Row) = x (Row) − U(Row, Column ) * x ( Column )
End Do
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
502 C Fortran 95 Codes
Program s l i t
I m p l i c i t none
Real * 8 : : psr ( 9 1 , 9 1 , 2 ) , p s i ( 9 1 , 9 1 , 2 ) , v ( 9 1 , 9 1 ) , p2 ( 9 1 , 9 1 )
Real * 8 : : a1 , a2 , dt , dx , k0x , k0y , x0 , y0 , x , y
I n t e g e r i , j , k , max , n , time
Complex exc , z i
! i n p u t p o s i t i v e i n t p r o p o r t i o n a l t o time f o r p l o t
write ( * , * ) ’Enter a positive Integer from 1(initial time)’
write ( * , * ) ’to 800 to get wave packet position at that time’
read ( * , * ) time
write ( * , * ) ’processing data for time’ , time
open ( 9 , FILE = ’slit.dat’ , S t a t u s = ’Unknown’ )
! i n i t i a l i z e c o n s t a n t s and wave p a ck e t
zi = cmplx ( 0 . 0 , 1 . 0 )
dx = 0.2
dt = 0 . 0 0 2 5 / ( dx * dx )
! i n i t i a l momentum , p o s i t i o n
k0x = 0 . 0 ; k0y = 2 . 5
x0 = 0.0; y0 = − 7.0
max = 90
! cl e a r the arrays
Do i = 1 , 91
Do j = 1 , 91
Do k = 1 , 2
psi ( i , j , k) = 0.0
psr ( i , j , k ) = 0 . 0
End Do
End Do
End Do
! i n i t i a l wave f u n c t i o n
y = − 9.0
Do j = 1 , max + 1
x = − 9 . 0 d0
Do i = 1 , max + 1
exc = exp ( z i * ( k0x * x + k0y * y ) )
a1 = exp ( − 0 . 5 * ( ( ( x − x0 ) ) * * 2 + ( ( y − y0 ) ) * * 2 ) )
psr ( i , j , 1 ) = r e a l ( a1 * exc ) ! re al part
p s i ( i , j , 1 ) = aimag ( a1 * exc ) ! imaginay p a r t
x = x + dx
End Do
y = y + dx
End Do
! s e t p o t e n t i a l s l i t width : 50 − 40 = 10 u n i t s
Do j = 1 , max + 1
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
503
Do i = 1 , max + 1
I f ( ( j == 3 5 ) . and . ( ( i < 4 0 ) . or . ( i > 5 1 ) ) ) then
v( i , j ) = 0.5
else
v( i , j ) = 0.0
Endif
End Do
End Do
! propagate p s i through time
Do n = 1 , time
! compute r e a l wave p a ck e t and p r o b a b i l i t y
Do j = 2 , max
Do i = 2 , max
a2 = v ( i , j ) * p s i ( i , j , 1 ) + 2 . 0 d0 * dt * p s i ( i , j , 1 )
a1 = p s i ( i +1 , j , 1 ) + p s i ( i − 1, j , 1 ) + p s i ( i , j + 1 , 1 ) + p s i ( i , j − 1 ,1)
psr ( i , j , 2 ) = psr ( i , j , 1 ) − dt * a1 + 2 . 0 * a2
I f ( n == time ) then
p2 ( i , j ) = psr ( i , j , 1 ) * psr ( i , j , 1 ) + p s i ( i , j , 1 ) * p s i ( i , j , 1 )
Endif
End Do
psr ( 1 , j , 2 ) = psr ( 2 , j , 2 ) ! a t x edges d e r i v a t i v e =0
psr ( max + 1 , j , 2 ) = psr ( max , j , 2 )
End Do
! imaginary p a r t o f p s i
Do j = 2 , max
Do i = 2 , max
a2 = v ( i , j ) * psr ( i , j , 2 ) + 2 . 0 * dt * psr ( i , j , 2 )
a1 = psr ( i +1 , j , 2 ) + psr ( i − 1, j , 2 ) + psr ( i , j − 1 ,2) + psr ( i , j + 1 , 2 )
p s i ( i , j , 2 ) = p s i ( i , j , 1 ) + dt * a1 − 2 . 0 * a2
End Do
psi (1 , j , 2) = p s i ( 2 , j , 2 ) ! a t x edges d e r i v a t i v e =0
p s i ( max + 1 , j , 2 ) = p s i ( max , j , 2 )
End Do
! new −> old
Do j = 1 , max + 1
Do i = 1 , max + 1
psi ( i , j , 1) = psi ( i , j , 2)
psr ( i , j , 1 ) = psr ( i , j , 2 )
End Do
End Do
End Do
! wr i t e p r o b a b i l i t i e s & p o t e n t i a l s c a l e d by 0 . 0 2 5 ( t o f i t )
Do j = 2 , max , 3
Do i = 2 , max , 2
wr i t e ( 9 , 1 1 ) p2 ( i , j ) + v ( i , j )
End Do
write ( 9 , * ) ’ ’
End Do
11 format ( E12 . 6 )
close (9)
Stop ’data saved in slit.dat’
End
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
504 C Fortran 95 Codes
I m p l i c i t None
Real * 8 : : ds , dt , max , mu, eps , u ( 1 3 1 , 3 )
parameter ( ds = 0 . 4 , dt = 0 . 1 , max = 2 0 0 0 , mu = 0 . 1 , eps = 0 . 2 )
! d e l t a t , d e l t a x , time s t e p s , mu and eps from KdeV e q u a t i o n
Real * 8 : : a1 , a2 , a3 , f a c , time
Integer : : i , j , k
I f ( modulo ( j , 2 0 0 ) == 0 ) then
Do k = 1 , 131
wr i t e ( 9 , 2 2 ) u ( k , 3 )
End Do
wr i t e ( 9 , 2 2 )
EndIf
time = time + dt
End Do
22 format ( f 1 0 . 6 )
close (9)
Stop ’data saved in soliton.dat (for gnuplot)’
End Program s o l i t o n
Program s p l i n e
I m p l i c i t none
! i n p u t a r r a y x [ n ] , y [ n ] r e p r e s e n t s t a b u l a t i o n Function y ( x )
! with x0 < x1 . . . < x ( n − 1 ) . n = # o f t a b u l a t e d p o i n t s
! output yout f o r given xout ( here xout v i a loop a t End )
! yp1 and ypn : 1 s t d e r i v a t i v e s a t Endpoints , e v a l u a t e d i n t e r n a l l y
! y2 [ n ] i s a r r a y o f second d e r i v a t i v e s
! ( s e t t i n g yp1 or ypn > 0 . 9 9 e30 produces n a t u r a l s p l i n e )
Do i = 1 , n
write ( 1 0 , * ) x ( i ) , y ( i )
End Do
Nfit = 3000;
! e n t e r t h e d e s i r e d number o f p o i n t s t o f i t
yp1 = ( y ( 2 ) − y ( 1 ) ) /( x ( 2 ) − x ( 1 ) ) − ( y ( 3 ) − y ( 2 ) ) /( x ( 3 ) − x ( 2 ) ) &
+ ( y ( 3 ) − y ( 1 ) ) /( x ( 3 ) − x ( 1 ) ) ! 1 s t deriv
ypn = ( y ( n − 1) − y ( n − 2) ) /( x ( n − 1) − x ( n − 2) ) − ( y ( n − 2) &
− y ( n − 3) ) /( x ( n − 2)−x ( n − 3) ) + ( y ( n − 1)−y ( n − 3) ) /( x ( n − 1)−x ( n − 3) )
I f ( yp1 > 0 . 9 9 e30 ) then
y2 ( 1 ) = 0 . 0
u(1) = 0.0
else
y2 ( 1 ) = ( − 0 . 5 )
u ( 1 ) = ( 3 . 0 / ( x ( 2 ) − x ( 1 ) ) ) * ( ( y ( 2 ) − y ( 1 ) ) /( x ( 2 ) − x ( 1 ) ) − yp1 )
Endif
! decomposition loop ; y2 , u a r e temps
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
506 C Fortran 95 Codes
Do i = 2 , n − 1
s i g = ( x ( i ) − x ( i − 1 ) ) /( x ( i + 1 ) − x ( i − 1 ) ) ;
p = s i g * y2 ( i − 1 ) + 2 . 0
y2 ( i ) = ( s i g − 1 . 0 ) /p
u ( i ) = ( y ( i +1)−y ( i ) ) /( x ( i +1)−x ( i ) ) − ( y ( i )−y ( i − 1) ) /( x ( i )− x ( i − 1) )
u ( i ) = ( 6 . 0 * u ( i ) /( x ( i +1) − x ( i − 1) ) − s i g * u ( i − 1) ) /p ;
End Do
! t e s t for natural
! e l s e e v a l u a t e second d e r i v a t i v e
I f ( ypn > 0 . 9 9 e30 ) then
qn = 0 . 0
un = 0 .
else
qn = 0 . 5
un = ( 3 / ( x ( n − 1) − x ( n − 2) ) ) * ( ypn − ( y ( n − 1)−y ( n − 2) ) &
/( x ( n − 1) − x ( n − 2) ) )
y2 ( n − 1 ) = ( un − qn * u ( n − 2 ) ) /( qn * y2 ( n − 2 ) + 1 . 0 )
Endif
! back s u b s t i t u t i o n
Do k = n − 2 , 1 , − 1
y2 ( k ) = y2 ( k ) * y2 ( k + 1 ) + u ( k )
End Do ! s p l i n t ( i n i t i a l i z a t i o n ) Ends
Program s q we l l
I m p l i c i t None
Real * 8 : : psr ( 7 5 1 , 2 ) , p s i ( 7 5 1 , 2 ) , p2 ( 7 5 1 )
Real * 8 : : dx , k0 , dt , x , p i
I n t e g e r : : i , j , n , max
Complex exc , z i
Common / v a l u e s /dx , dt
Do
i = 1 , max + 1
psi ( i , 1) = psi ( i , 2)
psr ( i , 1 ) = psr ( i , 2 )
End Do
End Do
11 format ( E12 . 6 )
close (9)
Stop ’data saved in sqwell.dat’
End
Program tune
parameter ( ldim = 2 0 5 0 )
I m p l i c i t Double p r e c i s i o n ( a − h , o − z )
dimension ham( ldim , ldim ) , c o e f ( ldim ) , sigma ( ldim )
! s e t up H and s t a r t i n g v e c t o r
Do i = 1 , ldim
Do j = 1 , ldim
I f ( abs ( j − i ) > 1 0 ) then
ham( j , i ) = 0 .
else
ham( j , i ) = 0 . 3 * * Abs ( j − i )
EndIf
End Do
ham( i , i ) = i
coef ( i ) = 0.
End Do
coef ( 1 ) = 1.
! start iterating
err = 1.
iter = 0
20 I f ( i t e r < 15 . and . e r r >1. e − 6) then
iter = iter + 1
! compute c u r r e n t energy & normalize
ener = 0 .
ovlp = 0 .
Do i = 1 , ldim
ovlp = ovlp + c o e f ( i ) * c o e f ( i )
sigma ( i ) = 0 .
Do j = 1 , ldim
sigma ( i ) = sigma ( i ) + c o e f ( j ) * ham ( j , i )
End Do
ener = ener + c o e f ( i ) * sigma ( i )
End Do
ener = ener/ovlp
Do I = 1 , ldim
c o e f ( i ) = c o e f ( i ) / S q r t ( ovlp )
sigma ( i ) = sigma ( i ) / S q r t ( ovlp )
End Do
! compute update and e r r o r norm
err = 0.
Do i = 1 , ldim
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
509
I f ( i == 1 ) goto 23
s t e p = ( sigma ( i ) − ener * c o e f ( i ) ) /( ener − ham( i , i ) )
coef ( i ) = coef ( i ) + step
err = err + step **2
23 End Do
err = sqrt ( err )
w r i t e ( * , ’(1x, i2, 7f10.5)’ ) i t e r , ener , e r r , c o e f ( 1 )
goto 20
Endif
Stop
End Program tune
Program twodsol
I m p l i c i t none
Double p r e c i s i o n : : u ( 2 0 1 , 2 0 1 , 3 )
Integer : : nint
Common / v a l u e s / dx , dy , dt , time , d t s
Do i = 1 , 2 0 1 ! c l e a r a r r a y s
Do j = 1 , 201
Do k = 1 , 3
u( i , j , k) = 0.0
End Do
End Do
End Do
dx = 1 4 . 0 / 2 0 0 . ! i n i t i a l c o n d i t i o n
dy = dx
dt = dx/ s q r t ( 2 . 0 )
d t s = ( dt/dx ) * * 2
yy = − 7 . 0
time = 0 . 0
Do i = 1 , 201
xx = − 7 . 0
Do j = 1 , 201
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
510 C Fortran 95 Codes
u ( i , j , 1 ) = 4 . 0 * Datan ( 3 . − s q r t ( xx * xx + yy * yy ) )
xx = xx + dx
End Do
yy = yy + dy
End Do
Return
End
! s o l v e SGE , i n i t i a l c o n d i t i o n s i n i n i t i a l
Subroutine s o lu t io n ( u , nint )
I m p l i c i t none
Double p r e c i s i o n : : u ( 2 0 1 , 2 0 1 , 3 ) , dx , dy , dt , time , a2 , zz , dts , a1
I n t e g e r : : l , m, mm, k , j , i , n i n t
Common/ v a l u e s / dx , dy , dt , time , d t s
time = time + dt
! 2nd i t e r a t i o n uses d phi/dt ( t =0) = 0 (G( x , y , 0 ) = 0 )
! d U/dx = 0 a t − x0 , x0 , − y0 and y0
Do l = 2 , 200
Do m = 2 , 200
a2 = u (m+1 , l , 1 ) + u (m− 1, l , 1 ) + u (m, l +1 , 1 ) + u (m, l − 1, 1 )
u (m, l , 2 ) = 0 . 5 * ( d t s * a2 − dt * dt * DSIN ( 0 . 2 5 * a2 ) )
End Do
End Do
! t h e b o r d e r s i n 2nd i t e r a t i o n
Do mm = 2 , 200
u (mm, 1 , 2 ) = u (mm, 2 , 2 )
u (mm, 2 0 1 , 2 ) = u (mm, 2 0 0 , 2 )
u ( 1 , mm, 2 ) = u ( 2 , mm, 2 )
u ( 2 0 1 , mm, 2 ) = u ( 2 0 0 , mm, 2 )
End Do
! t h e s t i l l undefined terms
u(1 , 1 , 2) = u(2 , 1 , 2)
u(201 , 1 , 2) = u(200 , 1 , 2)
u ( 1 , 201 , 2) = u ( 2 , 201 , 2)
u ( 201 , 201 , 2) = u( 200 , 201 , 2)
! 3 rd and f o l l o w i n g i t e r a t i o n s use your input , loop up t o n i n t
Do k = 1 , n i n t
Do l = 2 , 200
Do m = 2 , 200
a1 = u (m+1 , l , 2 ) + u (m− 1, l , 2 ) + u (m, l + 1 , 2 ) + u (m, l − 1, 2 )
u (m, l , 3 ) = − u (m, l , 1 ) + d t s * a1 − dt * dt * DSIN ( 0 . 2 5 * a1 )
u (m, 1 , 3 ) = u (m, 2 , 3 )
u (m, 2 0 1 , 3 ) = u (m, 2 0 0 , 3 )
End Do
End Do
Do mm = 2 , 200
u (mm, 1 , 3 ) = u (mm, 2 , 3 )
u (mm, 2 0 1 , 3 ) = u (mm, 2 0 0 , 3 )
u ( 1 , mm, 3 ) = u ( 2 , mm, 3 )
u ( 2 0 1 , mm, 3 ) = u ( 2 0 0 , mm, 3 )
End Do
u(1 , 1 , 3) = u(2 , 1 , 3)
u(201 , 1 , 3) = u(200 , 1 , 3)
u ( 1 , 201 , 3) = u ( 2 , 201 , 3)
u ( 2 0 1 , 201 , 3) = u ( 2 0 0 , 201 , 3)
! new −> old
Do l = 1 , 201
Do m = 1 , 201
10.1002/9783527618835.app3, Downloaded from https://onlinelibrary.wiley.com/doi/10.1002/9783527618835.app3 by University Of The Philippines, Wiley Online Library on [18/10/2023]. See the Terms and Conditions (https://onlinelibrary.wiley.com/terms-and-conditions) on Wiley Online Library for rules of use; OA articles are governed by the applicable Creative Commons License
511
u ( l , m, 1 ) = u ( l , m, 2 )
u ( l , m, 2 ) = u ( l , m, 3 )
End Do
End Do
! Output s o l u t i o n a t time p r o p o r t i o n a l t o n i n t
I f ( k == n i n t ) then
Do i = 1 , 2 0 1 , 5
Do j = 1 , 2 0 1 , 5
zz = DSIN ( u ( i , j , 3 ) / 2 . 0 )
w r i t e ( 9 , * ) zz
End Do
! need blank l i n e s t o s e p a r a t e s p a t i a l rows f o r 3−D
write ( 9 , * ) ’ ’
End Do
Endif
time = time + dt
End Do
Return
End
Program walk
I m p l i c i t none
Real * 8 : : ranDom , r o o t 2 , x , y , r ( 1 : 1 0 0 0 0 )
I n t e g e r : : i , j , max