-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.jl
More file actions
186 lines (153 loc) · 5.69 KB
/
main.jl
File metadata and controls
186 lines (153 loc) · 5.69 KB
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
##==================================================================================================================
## Main file for parallel job execution
##==================================================================================================================
#### minimal imports
using Distributed
using SharedArrays
#### initialize workers
n_cores = parse(Int64, ARGS[2])
if length(workers()) > 1
rmprocs(workers())
end
addprocs(n_cores)
proc = workers()
println("Workers added: $n_cores")
#### run set up script
@everywhere input_deck = $(ARGS[1])
@everywhere include("setup.jl")
#### kludge fix for now
dir = pwd()
file_id = ""
##==================================================================================================================
## Parallel engines for jobs
##==================================================================================================================
##--------------------------
## Neutral Beam MC
##--------------------------
#### axes: beam number, energy component [1, 1/2, 1/3], x axis, y axis, z axis
fastn = SharedArray{Float64}(length(beams), 3, length(RSgrid.x), length(RSgrid.y), length(RSgrid.z))
nb_src = SharedArray{Float64}(OSgrid.N)
function MCbeams!(n::Integer)
@sync @distributed for i=1:n
for j=eachindex(beams)
ni,nj,nk,w = throw_neutral(beams[j], state, RSgrid)
fastn[j,:,ni,nj,nk] += w / n
end
end
end
function MCsource!(n::Integer)
@sync @distributed for _=1:n
for beam in beams
i,w = throw_neutral(beam, state, OSgrid)
nb_src[i] += w / n
end
end
end
##--------------------------
## Weight functions
##--------------------------
results = SharedArray{Float64}(N + 1,3) # +1 to ensure dimensionality
function engine(n::Integer)
#### CX method
function MCweights!(view::CXView, view_index::Integer, n::Integer)
start_index = (view_index - 1) * n
if view.beam_index == -1
@sync @distributed for j=1:n
I,J,V = throw_particle(view, state, OSgrid, RSgrid) # COO format for sparse matrices
results[start_index + j, :] .= [I, J, V * OSgrid.V / N]
#put!(results, (i,j,w*OSgrid.V/N))
end
else
@sync @distributed for j=1:n
I,J,V = throw_particle(view, state, beams, fn_itp, OSgrid, RSgrid)
results[start_index + j, :] .= [I, J, V * OSgrid.V / N]
end
end
end
#### fusion product method
function MCweights!(view::FusionProductView, view_index::Integer, n::Integer)
start_index = (view_index - 1) * n
@sync @distributed for j=1:n
I,J,V = throw_particle(view, state, RSgrid, OSgrid)
results[start_index + j, :] .= [I, J, V * OSgrid.V / N]
end
end
for (i,view) in enumerate(views)
MCweights!(view, i, n)
pdone = round(i / length(views) * 100, digits=1)
println("$(pdone)% completed")
end
results[end,:] .= [length(signals),OSgrid.N,0.0]
W = sparse(Integer.(results[:,1]), Integer.(results[:,2]), results[:,3]) ## SparseMatrixCSC
save_weights(dir, file_id, W)
return W
end
##==================================================================================================================
## Do the computation
##==================================================================================================================
## timing
t = time()
## Initialize output file
initialize_output_files(dir, file_id, OSgrid, signals)
## if there are no states, exit nicely
if OSgrid.N == 0
println("Exiting: provided orbit-space axes don't span a non-trivial space.")
println("\nFinished!")
exit()
end
##--------------------------
## Compute or load fast neutrals
##--------------------------
if have_fastn
println("\nLoading fast neutral density from file...")
load_fastn(run_params["files"]["fastn_file"])
else
println("\nNo fast neutral file provided. Computing fast neutral distribution...")
MCbeams!(Nfastn)
println("\nSaving fast neutrals...")
#save_fastn(dir, file_id, beams, fastn)
end
@everywhere _fn_itp = interpolate_fast_neutrals(fastn, beams, RSgrid)
@everywhere const fn_itp = _fn_itp
tdone = round((time() - t) / 60, digits=2)
stat = have_fastn ? "loaded" : "computed"
println("\nFast neutral density $(stat), t = $(tdone) minutes.")
if build_source
println("\nBuilding source distribution...")
MCsource!(Nfastn)
F = sparse(nb_src)
end
tdone = round((time() - t) / 60, digits=2)
println("\nSource distribution computed, t = $(tdone) minutes.")
##--------------------------
## Compute + output weight functions + signals
##--------------------------
println("\nComputing weights...")
W = engine(Nion)
tdone = round((time() - t) / 60, digits=2)
println("\nWeight computation done, t = $(tdone) minutes.")
## signals!
S = Vector(W * F) # project to dense vector, doesn't need to be sparse anymore
save_signals(dir, file_id, S)
tdone = round((time() - t) / 60, digits=2)
println("\nSignals computed, t = $(tdone) minutes.")
### no more parallelism
rmprocs(workers())
##--------------------------
## Make plots of weight functions & compute signals
##--------------------------
if make_plots
#println("\nCannot make plots, because I haven't built this yet!")
include("plots.jl")
# load
println("\nMaking plots...")
plot_view_weights!(W, dir, views, OSgrid)
plot_fast_ions!(F, dir, OSgrid)
plot_npa_spectra!(S, dir, views)
tdone = round((time() - t) / 60, digits=2)
println("\nPlots made, t = $(tdone) minutes.")
else
println("\nNot making plots...")
end
tdone = round((time() - t) / 60, digits=2)
println("\nFinished! Total time : $(tdone) minutes.")