Skip to content

Commit bfeb2ec

Browse files
committed
added PROGRAM1 - translated from Fortran.
1 parent d154b53 commit bfeb2ec

File tree

5 files changed

+329
-0
lines changed

5 files changed

+329
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#***********************************************************************
2+
# Copyright 2006 John A. Trangenstein
3+
#
4+
# This software is made available for research and instructional use
5+
# only.
6+
# You may copy and use this software without charge for these
7+
# non-commercial purposes, provided that the copyright notice and
8+
# associated text is reproduced on all copies.
9+
# For all other uses (including distribution of modified versions),
10+
# please contact the author at
11+
# John A. Trangenstein
12+
# Department of Mathematics
13+
# Duke University
14+
# Durham, NC 27708-0320
15+
# USA
16+
# or
17+
18+
#
19+
# This software is made available "as is" without any assurance that it
20+
# is completely correct, or that it will work for your purposes.
21+
# Use the software at your own risk.
22+
#***********************************************************************
23+
24+
function consdiff(fic::Int,lac::Int,fif::Int,laf::Int,fix::Int,lax::Int,
25+
ifirst::Int,ilast::Int,
26+
x::OffsetArray{Float64,1},flux::OffsetArray{Float64,1},
27+
conservd::OffsetArray{Float64,1})
28+
# ******************************************************************
29+
# update conservd to new time
30+
# ******************************************************************
31+
@unsafe for ic=ifirst:ilast
32+
conservd[ic] -= (flux[ic+1]-flux[ic]) / (x[ic+1]-x[ic])
33+
end
34+
end
35+
36+
function stabledt(fc::Int,lc::Int,fm::Int,lm::Int,ifirst::Int,ilast::Int,
37+
conserved::OffsetArray{Float64,1},
38+
lambda_cell::OffsetArray{Float64,1},
39+
x::OffsetArray{Float64,1})
40+
# ******************************************************************
41+
# compute stable timestep
42+
# ******************************************************************
43+
sdt=huge
44+
@unsafe for ic=ifirst:ilast
45+
abs_lambda=abs(lambda_cell[ic])
46+
dx = x[ic+1]-x[ic]
47+
if abs_lambda > roundoff*dx
48+
sdt=min(sdt,dx/abs_lambda)
49+
end
50+
end
51+
@assert sdt > 0.0
52+
sdt
53+
end
54+
55+
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#***********************************************************************
2+
# Copyright 2006 John A. Trangenstein
3+
#
4+
# This software is made available for research and instructional use
5+
# only.
6+
# You may copy and use this software without charge for these
7+
# non-commercial purposes, provided that the copyright notice and
8+
# associated text is reproduced on all copies.
9+
# For all other uses (including distribution of modified versions),
10+
# please contact the author at
11+
# John A. Trangenstein
12+
# Department of Mathematics
13+
# Duke University
14+
# Durham, NC 27708-0320
15+
# USA
16+
# or
17+
18+
#
19+
# This software is made available "as is" without any assurance that it
20+
# is completely correct, or that it will work for your purposes.
21+
# Use the software at your own risk.
22+
#***********************************************************************
23+
24+
using OffsetArrays
25+
26+
const roundoff = 1.0-14
27+
const small = 1.0-20
28+
const huge = 1.0e300
29+
const undefind = 1.0e300
30+
const lnrndoff = 14.0e0
31+
const lnsmall = 20.0e0
32+
33+
# problem-specific parameters:
34+
const jump = 0.0
35+
const x_left = -0.2
36+
const x_right = 1.0
37+
const statelft = 2.0
38+
const statergt = 0.0
39+
const velocity = 1.0
40+
41+
include("riemprob.jl")
42+
include("linearad.jl")
43+
include("consdiff.jl")
44+
include("upwind.jl")
45+
46+
function main()
47+
const ncells = 10000
48+
49+
u = OffsetArray(Float64, -2:ncells+1)
50+
x = OffsetArray(Float64, 0:ncells)
51+
flux = OffsetArray(Float64, 0:ncells)
52+
dfdu = OffsetArray(Float64, -2:ncells+1)
53+
54+
const nsteps = 100
55+
#const nsteps = 10000
56+
const tmax = 0.8
57+
const cfl = 0.9
58+
59+
# array bounds:
60+
const fc=-2
61+
const lc=ncells+1
62+
const fm=0
63+
const lm=ncells-1
64+
const fs=0
65+
const ls=ncells-1
66+
const ifirst=0
67+
const ilast=ncells-1
68+
69+
initsl(ncells,fc,lc,fm,lm,ifirst,ilast, u,x)
70+
71+
#println("x : $x u : $u")
72+
bcmesh(fm,lm,ncells, x)
73+
bccells(fc,lc,ncells, u)
74+
fluxderv(fc,lc,fc,lc, u, dfdu)
75+
76+
istep=0
77+
t=0.0
78+
while istep < nsteps && t < tmax
79+
bccells(fc,lc,ncells, u)
80+
fluxderv(fc,lc,fc,lc, u, dfdu)
81+
dt=cfl*stabledt(fc,lc,fm,lm,ifirst,ilast, u, dfdu,x)
82+
method(dt,fc,lc,fm,lm,fs,ls,ifirst,ilast, u, flux)
83+
consdiff(fc,lc,fs,ls,fm,lm,ifirst,ilast, x,flux, u)
84+
85+
t=t+dt
86+
istep=istep+1
87+
end
88+
89+
# write final results (plot later)
90+
#@unsafe for ic=0:ncells-1
91+
for ic=0:ncells-1
92+
xc = (x[ic]+x[ic+1])*0.5
93+
uc = u[ic]
94+
@printf("%e %e\n",xc,uc)
95+
end
96+
97+
98+
end # main
99+
100+
main()
101+
102+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#***********************************************************************
2+
# Copyright 2006 John A. Trangenstein
3+
#
4+
# This software is made available for research and instructional use
5+
# only.
6+
# You may copy and use this software without charge for these
7+
# non-commercial purposes, provided that the copyright notice and
8+
# associated text is reproduced on all copies.
9+
# For all other uses (including distribution of modified versions),
10+
# please contact the author at
11+
# John A. Trangenstein
12+
# Department of Mathematics
13+
# Duke University
14+
# Durham, NC 27708-0320
15+
# USA
16+
# or
17+
18+
#
19+
# This software is made available "as is" without any assurance that it
20+
# is completely correct, or that it will work for your purposes.
21+
# Use the software at your own risk.
22+
#***********************************************************************
23+
24+
function fluxderv(fc::Int,lc::Int,ifirst::Int,ilast::Int,
25+
conservd::OffsetArray{Float64,1}, dfdu::OffsetArray{Float64,1})
26+
# ******************************************************************
27+
# compute derivative of flux with respect to conserved variable
28+
# ******************************************************************
29+
@unsafe for ic=ifirst:ilast
30+
dfdu[ic]=velocity
31+
end
32+
end
33+
34+
function riemann(fs::Int,ls::Int,ifirst::Int,ilast::Int,
35+
left::OffsetArray{Float64,1},right::OffsetArray{Float64,1},
36+
flux::OffsetArray{Float64,1})
37+
# ******************************************************************
38+
# the following riemann solver is hard-wired for linear advection
39+
# with velocity > 0
40+
# ******************************************************************
41+
@unsafe for ie=ifirst:ilast+1
42+
flux[ie]=velocity*left[ie]
43+
end
44+
end
45+
46+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#***********************************************************************
2+
# Copyright 2006 John A. Trangenstein
3+
#
4+
# This software is made available for research and instructional use
5+
# only.
6+
# You may copy and use this software without charge for these
7+
# non-commercial purposes, provided that the copyright notice and
8+
# associated text is reproduced on all copies.
9+
# For all other uses (including distribution of modified versions),
10+
# please contact the author at
11+
# John A. Trangenstein
12+
# Department of Mathematics
13+
# Duke University
14+
# Durham, NC 27708-0320
15+
# USA
16+
# or
17+
18+
#
19+
# This software is made available "as is" without any assurance that it
20+
# is completely correct, or that it will work for your purposes.
21+
# Use the software at your own risk.
22+
#***********************************************************************
23+
24+
function initsl(ncells::Int, fc::Int, lc::Int, fx::Int, lx::Int, ifirst::Int, ilast::Int,
25+
conservd::OffsetArray{Float64,1}, x::OffsetArray{Float64,1})
26+
# ******************************************************************
27+
# interior values only; others defined by boundary conditions
28+
# ******************************************************************
29+
dx=(x_right-x_left)/ncells
30+
@unsafe for ie=ifirst:ilast+1
31+
x[ie]=x_left+ie*dx
32+
end
33+
ijump=max(ifirst-1,min(convert(Int,round(ncells*(jump-x_left)/(x_right-x_left))),ilast+1))
34+
@unsafe for ic=ifirst:ijump-1
35+
conservd[ic]=statelft
36+
end
37+
frac=(jump-x_left-ijump*dx)/(x_right-x_left)
38+
conservd[ijump]=statelft*frac+statergt*(1.0-frac)
39+
@unsafe for ic=ijump+1:ilast
40+
conservd[ic]=statergt
41+
end
42+
end
43+
44+
function bcmesh(fim::Int,lam::Int,ncells::Int, x::OffsetArray{Float64,1})
45+
# ******************************************************************
46+
# right side
47+
# ******************************************************************
48+
if lam >= ncells
49+
it=ncells
50+
dx=x(it)-x(it-1)
51+
@unsafe for ie=it+1:lam+1
52+
x[ie]=x[it]+dx*(ie-it)
53+
end
54+
end
55+
# ******************************************************************
56+
# left side
57+
# ******************************************************************
58+
if fim < 0
59+
dx=x(1)-x(0)
60+
@unsafe for ie=fim:-1
61+
x[ie]=x[0]+dx*(ie)
62+
end
63+
end
64+
end
65+
66+
function bccells(fic::Int,lac::Int,ncells::Int, conservd::OffsetArray{Float64,1})
67+
# ******************************************************************
68+
# right side
69+
# ******************************************************************
70+
if lac >= ncells
71+
# println("treating right side")
72+
@unsafe for ic=ncells:lac
73+
# outgoing wave:
74+
conservd[ic]=conservd[ncells-1]
75+
end
76+
end
77+
# ******************************************************************
78+
# left side
79+
# ******************************************************************
80+
if fic < 0
81+
# println("treating left side")
82+
@unsafe for ic=fic:-1
83+
# outgoing wave:
84+
# conservd[ic]=conservd[0]
85+
86+
# specified value
87+
conservd[ic]=statelft
88+
end
89+
end
90+
end
91+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#***********************************************************************
2+
# Copyright 2006 John A. Trangenstein
3+
#
4+
# This software is made available for research and instructional use
5+
# only.
6+
# You may copy and use this software without charge for these
7+
# non-commercial purposes, provided that the copyright notice and
8+
# associated text is reproduced on all copies.
9+
# For all other uses (including distribution of modified versions),
10+
# please contact the author at
11+
# John A. Trangenstein
12+
# Department of Mathematics
13+
# Duke University
14+
# Durham, NC 27708-0320
15+
# USA
16+
# or
17+
18+
#
19+
# This software is made available "as is" without any assurance that it
20+
# is completely correct, or that it will work for your purposes.
21+
# Use the software at your own risk.
22+
#***********************************************************************
23+
24+
function method(dt::Float64,fc::Int,lc::Int,fm::Int,lm::Int,fs::Int,ls::Int,ifirst::Int,ilast::Int,
25+
conserved::OffsetArray{Float64,1},
26+
flux::OffsetArray{Float64,1})
27+
# ******************************************************************
28+
# multiply fluxes by dt (ie, compute time integral over cell side)
29+
# ******************************************************************
30+
vdt=velocity*dt
31+
@unsafe for ie=ifirst:ilast+1
32+
flux[ie]=vdt*conserved[ie-1]
33+
end
34+
end
35+

0 commit comments

Comments
 (0)