forked from mabarnes/moment_kinetics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_MPI_test.jl
203 lines (195 loc) · 6.67 KB
/
run_MPI_test.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
if abspath(PROGRAM_FILE) == @__FILE__
using Pkg
Pkg.activate(".")
import moment_kinetics
using moment_kinetics.input_structs: grid_input, advection_input
using moment_kinetics.coordinates: define_coordinate
using moment_kinetics.chebyshev: setup_chebyshev_pseudospectral
using moment_kinetics.calculus: derivative!, integral
import MPI
using Plots
MPI.Init()
comm = MPI.COMM_WORLD
nrank = MPI.Comm_size(comm) # number of ranks
irank = MPI.Comm_rank(comm) # rank of this process
#println("Hello world, I am $(irank) of $(nrank)")
MPI.Barrier(comm)
#println("comm: ",comm)
discretization = "chebyshev_pseudospectral"
etol = 1.0e-15
###################
## df/dx Nonperiodic (No) BC test
###################
# define inputs needed for the test
ngrid = 20 #number of points per element
nelement_local = 2 # number of elements per rank
nelement_global = nelement_local*nrank # total number of elements
if irank == 0
println("ngrid = ",ngrid," nelement_local = ",nelement_local,
" nelement_global = ",nelement_global," nrank = ",nrank)
end
L = 6.0 #physical box size in reference units
bc = "" #not required to take a particular value, not used
# fd_option and adv_input not actually used so given values unimportant
fd_option = ""
adv_input = advection_input("default", 1.0, 0.0, 0.0)
# create the 'input' struct containing input info needed to create a
# coordinate
input = grid_input("coord", ngrid, nelement_global, nelement_local,
nrank, irank, L, discretization, fd_option, bc, adv_input,comm)
# create the coordinate struct 'x'
#println("made inputs")
x = define_coordinate(input)
#println("made x")
# create arrays needed for Chebyshev pseudospectral treatment in x
# and create the plans for the forward and backward fast Chebyshev
# transforms
spectral = setup_chebyshev_pseudospectral(x)
#println("made spectral")
# create array for the function f(x) to be differentiated/integrated
f = Array{Float64,1}(undef, x.n)
g = Array{Float64,1}(undef, x.n)
x_for_plot = Array{Float64,2}(undef, x.n, nrank)
g_for_plot = Array{Float64,2}(undef, x.n, nrank)
df_for_plot = Array{Float64,2}(undef, x.n, nrank)
# create array for the derivative df/dx
df = Array{Float64,1}(undef, x.n)
# initialize f
for ix ∈ 1:x.n
f[ix] = ( (cospi(2.0*x.grid[ix]/x.L)+sinpi(2.0*x.grid[ix]/x.L))
* exp(-x.grid[ix]^2) )
g[ix] = (2.0*pi/x.L)*( (cospi(2.0*x.grid[ix]/x.L)-sinpi(2.0*x.grid[ix]/x.L))
* exp(-x.grid[ix]^2) ) - 2.0*x.grid[ix]*f[ix]
end
# differentiate f
derivative!(df, f, x, spectral)
# plot df and g per process
outprefix = "run_MPI_test.plot."
plot([x.grid,x.grid], [g,df], xlabel="x", ylabel="", label=["g" "df"],
shape =:circle, markersize = 5, linewidth=2)
outfile = outprefix*string(irank)*".pdf"
savefig(outfile)
# plot df and g on rank 0
x_for_plot .= 0.0
g_for_plot .= 0.0
df_for_plot .= 0.0
for ix ∈ 1:x.n
x_for_plot[ix,irank+1] = x.grid[ix]
g_for_plot[ix,irank+1] = g[ix]
df_for_plot[ix,irank+1] = df[ix]
end
MPI.Reduce!(x_for_plot,.+,comm)
MPI.Reduce!(g_for_plot,.+,comm)
MPI.Reduce!(df_for_plot,.+,comm)
if irank == 0
outprefix = "run_MPI_test.plot."
xlist = [x_for_plot[:,1]]
ylist = [g_for_plot[:,1]]
labels = Matrix{String}(undef, 1, 2*nrank)
labels[1] = "g"
for iproc in 2:nrank
push!(xlist,x_for_plot[:,iproc])
push!(ylist,g_for_plot[:,iproc])
labels[iproc] ="g"
end
push!(xlist,x_for_plot[:,1])
push!(ylist,df_for_plot[:,1])
labels[1+nrank]="df"
for iproc in 2:nrank
push!(xlist,x_for_plot[:,iproc])
push!(ylist,df_for_plot[:,iproc])
labels[iproc+nrank]="df"
end
#println(labels)
plot(xlist, ylist, xlabel="x", ylabel="", label=labels, markersize = 1, linewidth=1)
outfile = outprefix*"global.pdf"
savefig(outfile)
println(outfile)
end
# integrate df/dx
#println("x.grid",x.grid)
#println("x.wgts",x.wgts)
#println("df",df)
intdf = integral(df, x.wgts)
#println(intdf)
intdf_out = MPI.Reduce(intdf,+,comm)
# Test that error intdf is less than the specified error tolerance etol
#@test abs(intdf) < etol
if(irank == 0)
println( "abs(intdf_out) = ", abs(intdf_out), ": etol = ",etol)
end
###################
## df/dx Periodic BC test
###################
MPI.Barrier(comm)
bc = "periodic"
# create the 'input' struct containing input info needed to create a
# coordinate, other values taken from above
input = grid_input("coord", ngrid, nelement_global, nelement_local,
nrank, irank, L, discretization, fd_option, bc, adv_input,comm)
# create the coordinate struct 'x'
x = define_coordinate(input)
# create arrays needed for Chebyshev pseudospectral treatment in x
# and create the plans for the forward and backward fast Chebyshev
# transforms
spectral = setup_chebyshev_pseudospectral(x)
# initialize f
for ix ∈ 1:x.n
# sine wave test
f[ix] = - cospi(2.0*x.grid[ix]/x.L) +2.0*x.grid[ix]/x.L
g[ix] = (2.0*pi/x.L)*sinpi(2.0*x.grid[ix]/x.L) + 2.0/x.L
end
# differentiate f
derivative!(df, f, x, spectral)
# plot df and g per process
outprefix = "run_MPI_test.dfperiodic.plot."
plot([x.grid,x.grid], [g,df], xlabel="x", ylabel="", label=["g" "df"],
shape =:circle, markersize = 5, linewidth=2)
outfile = outprefix*string(irank)*".pdf"
savefig(outfile)
# plot df and g on rank 0
x_for_plot .= 0.0
g_for_plot .= 0.0
df_for_plot .= 0.0
for ix ∈ 1:x.n
x_for_plot[ix,irank+1] = x.grid[ix]
g_for_plot[ix,irank+1] = g[ix]
df_for_plot[ix,irank+1] = df[ix]
end
MPI.Reduce!(x_for_plot,.+,comm)
MPI.Reduce!(g_for_plot,.+,comm)
MPI.Reduce!(df_for_plot,.+,comm)
if irank == 0
outprefix = "run_MPI_test.dfperiodic.plot."
xlist = [x_for_plot[:,1]]
ylist = [g_for_plot[:,1]]
labels = Matrix{String}(undef, 1, 2*nrank)
labels[1] = "g"
for iproc in 2:nrank
push!(xlist,x_for_plot[:,iproc])
push!(ylist,g_for_plot[:,iproc])
labels[iproc] ="g"
end
push!(xlist,x_for_plot[:,1])
push!(ylist,df_for_plot[:,1])
labels[1+nrank]="df"
for iproc in 2:nrank
push!(xlist,x_for_plot[:,iproc])
push!(ylist,df_for_plot[:,iproc])
labels[iproc+nrank]="df"
end
plot(xlist, ylist, xlabel="x", ylabel="", label=labels, markersize = 1, linewidth=1)
outfile = outprefix*"global.pdf"
savefig(outfile)
println(outfile)
end
# integrate df/dx
intdf = integral(df, x.wgts)
intdf_out = MPI.Reduce(intdf,+,comm)
# Test that error intdf -2.0 is less than the specified error tolerance etol
#@test abs(intdf) < etol
if(irank == 0)
println( "abs(intdf_out-2.0) = ", abs(intdf_out-2.0), ": etol = ",etol)
end
MPI.Finalize()
end