-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_vorticity.py
450 lines (326 loc) · 16.9 KB
/
run_vorticity.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
'''
Created on Mar 23, 2012
@author: Michael Kraus ([email protected])
'''
import sys, petsc4py
petsc4py.init(sys.argv)
from petsc4py import PETSc
import argparse
import time
import numpy as np
from vorticity.config import Config
from vorticity.solvers.PETScDerivatives import PETScDerivatives
from vorticity.solvers.PETScPoisson import PETScPoisson
from vorticity.solvers.PETScVorticity import PETScVorticity
class petscMHD2D(object):
'''
PETSc/Python Vlasov Poisson Solver in 1D.
'''
def __init__(self, cfgfile):
'''
Constructor
'''
# load run config file
cfg = Config(cfgfile)
# run in linear or nonlinear mode
self.nonlinear = cfg['solver']['nonlinear']
# timestep setup
self.ht = cfg['grid']['ht'] # timestep size
self.nt = cfg['grid']['nt'] # number of timesteps
self.nsave = cfg['io']['nsave'] # save only every nsave'th timestep
# grid setup
self.nx = cfg['grid']['nx'] # number of points in x
self.ny = cfg['grid']['ny'] # number of points in y
Lx = cfg['grid']['Lx'] # spatial domain in x
x1 = cfg['grid']['x1'] #
x2 = cfg['grid']['x2'] #
Ly = cfg['grid']['Ly'] # spatial domain in y
y1 = cfg['grid']['y1'] #
y2 = cfg['grid']['y2'] #
if x1 != x2:
Lx = x2-x1
else:
x1 = 0.0
x2 = Lx
if y1 != y2:
Ly = y2-y1
else:
y1 = 0.0
y2 = Ly
self.hx = Lx / self.nx # gridstep size in x
self.hy = Ly / self.ny # gridstep size in y
self.time = PETSc.Vec().createMPI(1, PETSc.DECIDE, comm=PETSc.COMM_WORLD)
self.time.setName('t')
if PETSc.COMM_WORLD.getRank() == 0:
self.time.setValue(0, 0.0)
# set some PETSc options
OptDB = PETSc.Options()
OptDB.setValue('snes_rtol', cfg['solver']['petsc_snes_rtol'])
OptDB.setValue('snes_atol', cfg['solver']['petsc_snes_atol'])
OptDB.setValue('snes_stol', cfg['solver']['petsc_snes_stol'])
OptDB.setValue('snes_max_it', cfg['solver']['petsc_snes_max_iter'])
OptDB.setValue('ksp_rtol', cfg['solver']['petsc_ksp_rtol'])
OptDB.setValue('ksp_atol', cfg['solver']['petsc_ksp_atol'])
OptDB.setValue('ksp_max_it', cfg['solver']['petsc_ksp_max_iter'])
OptDB.setValue('ksp_monitor', '')
# OptDB.setValue('log_info', '')
# OptDB.setValue('log_summary', '')
self.snes_rtol = cfg['solver']['petsc_snes_rtol']
self.snes_atol = cfg['solver']['petsc_snes_atol']
self.snes_max_iter = cfg['solver']['petsc_snes_max_iter']
if PETSc.COMM_WORLD.getRank() == 0:
print("")
print("Config File: %s" % cfgfile)
print("Output File: %s" % cfg['io']['hdf5_output'])
print("")
if self.nonlinear:
print("nonlinear mode")
else:
print("linear mode")
print("")
print("nt = %i" % (self.nt))
print("nx = %i" % (self.nx))
print("ny = %i" % (self.ny))
print("")
print("ht = %e" % (self.ht))
print("hx = %e" % (self.hx))
print("hy = %e" % (self.hy))
print("")
print("xMin = %+12.6e" % (x1))
print("xMax = %+12.6e" % (x2))
print("yMin = %+12.6e" % (y1))
print("yMax = %+12.6e" % (y2))
print("")
print("")
# create DA with single dof
self.da1 = PETSc.DA().create(dim=2, dof=1,
sizes=[self.nx, self.ny],
proc_sizes=[PETSc.DECIDE, PETSc.DECIDE],
boundary_type=('periodic', 'periodic'),
stencil_width=1,
stencil_type='box')
# create DA for x grid
self.dax = PETSc.DA().create(dim=1, dof=1,
sizes=[self.nx],
proc_sizes=[PETSc.DECIDE],
boundary_type=('periodic'))
# create DA for y grid
self.day = PETSc.DA().create(dim=1, dof=1,
sizes=[self.ny],
proc_sizes=[PETSc.DECIDE],
boundary_type=('periodic'))
# initialise grid
self.da1.setUniformCoordinates(xmin=x1, xmax=x2,
ymin=y1, ymax=y2)
self.dax.setUniformCoordinates(xmin=x1, xmax=x2)
self.day.setUniformCoordinates(xmin=y1, xmax=y2)
# create solution, RHS and function vectors
self.Pb = self.da1.createGlobalVec()
self.Ob = self.da1.createGlobalVec()
self.Pf = self.da1.createGlobalVec()
self.Of = self.da1.createGlobalVec()
# create nullspace vector for Poisson equation
self.Pn = self.da1.createGlobalVec()
# create vectors for vorticity, streaming function and velocity
self.P = self.da1.createGlobalVec() # streaming function phi
self.O = self.da1.createGlobalVec() # vorticity omega
self.Vx = self.da1.createGlobalVec() # velocity, x-component
self.Vy = self.da1.createGlobalVec() # velocity, y-component
# set variable names
self.P.setName('P')
self.O.setName('O')
self.Vx.setName('Vx')
self.Vy.setName('Vy')
# create linear solver for vorticity
self.vorticity = PETScVorticity(self.da1, self.nx, self.ny, self.ht, self.hx, self.hy)
# self.vorticity_mat = self.da1.createMat()
# self.vorticity_mat.setOption(PETSc.Mat().Option.NEW_NONZERO_ALLOCATION_ERR, False)
# self.vorticity_mat.setUp()
self.vorticity_mf = PETSc.Mat().createPython([self.O.getSizes(), self.Ob.getSizes()], comm=PETSc.COMM_WORLD)
self.vorticity_mf.setPythonContext(self.vorticity)
self.vorticity_mf.setUp()
self.vorticity_snes = PETSc.SNES().create()
self.vorticity_snes.setType('ksponly')
self.vorticity_snes.setFunction(self.vorticity.function_snes_mult, self.Ob)
self.vorticity_snes.setJacobian(self.updateVorticityJacobian, self.vorticity_mf)
# self.vorticity_snes.setJacobian(self.updateVorticityJacobian, self.vorticity_mat)
# self.vorticity_snes.setJacobian(self.updateVorticityJacobian, self.vorticity_mf, self.vorticity_mat)
self.vorticity_snes.setFromOptions()
self.vorticity_snes.getKSP().setType('gmres')
self.vorticity_snes.getKSP().getPC().setType('none')
# self.vorticity_snes.getKSP().getPC().setType('asm')
# self.vorticity_snes.getKSP().setType('preonly')
# self.vorticity_snes.getKSP().getPC().setType('lu')
# self.vorticity_snes.getKSP().getPC().setFactorSolverPackage('superlu_dist')
# create Poisson matrix and solver
if self.nonlinear:
self.poisson = PETScPoisson(self.da1, self.nx, self.ny, self.hx, self.hy)
self.poisson_mf = PETSc.Mat().createPython([self.P.getSizes(), self.Pb.getSizes()], comm=PETSc.COMM_WORLD)
self.poisson_mf.setPythonContext(self.poisson)
self.poisson_mf.setUp()
self.poisson_mat = self.da1.createMat()
self.poisson_mat.setOption(PETSc.Mat().Option.NEW_NONZERO_ALLOCATION_ERR, False)
self.poisson_mat.setUp()
# self.Pn.set(1.)
# self.Pn.normalize()
# self.poisson_nullspace = PETSc.NullSpace().create(constant=False, vectors=(self.Pn,))
self.poisson_nullspace = PETSc.NullSpace().create(constant=True)
self.poisson_mat.setNullSpace(self.poisson_nullspace)
self.poisson_ksp = PETSc.KSP().create()
self.poisson_ksp.setFromOptions()
self.poisson_ksp.setTolerances(rtol=1E-13)
# self.poisson_ksp.setOperators(self.poisson_mf, self.poisson_mat)
self.poisson_ksp.setOperators(self.poisson_mat)
self.poisson_ksp.setType('preonly')
# self.poisson_ksp.setType('cg')
# self.poisson_ksp.getPC().setType('none')
# self.poisson_ksp.getPC().setType('hypre')
self.poisson_ksp.getPC().setType('lu')
self.poisson_ksp.getPC().setFactorSolverPackage('superlu_dist')
# self.poisson_ksp.setNullSpace(self.poisson_nullspace)
self.poisson.formMat(self.poisson_mat)
# create derivatives object
self.derivatives = PETScDerivatives(self.da1, self.nx, self.ny, self.ht, self.hx, self.hy)
# get coordinate vectors
coords_x = self.dax.getCoordinates()
coords_y = self.day.getCoordinates()
# save x coordinate arrays
scatter, xVec = PETSc.Scatter.toAll(coords_x)
scatter.begin(coords_x, xVec, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD)
scatter.end (coords_x, xVec, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD)
xGrid = xVec.getValues(range(0, self.nx)).copy()
scatter.destroy()
xVec.destroy()
# save y coordinate arrays
scatter, yVec = PETSc.Scatter.toAll(coords_y)
scatter.begin(coords_y, yVec, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD)
scatter.end (coords_y, yVec, PETSc.InsertMode.INSERT, PETSc.ScatterMode.FORWARD)
yGrid = yVec.getValues(range(0, self.ny)).copy()
scatter.destroy()
yVec.destroy()
# set initial data
(xs, xe), (ys, ye) = self.da1.getRanges()
O_arr = self.da1.getVecArray(self.O)
init_data = __import__("examples." + cfg['initial_data']['python'], globals(), locals(), ['vorticity'], 0)
txGrid, tyGrid = np.meshgrid(xGrid, yGrid)
txGrid = txGrid.T
tyGrid = tyGrid.T
O_arr[xs:xe, ys:ye] = init_data.vorticity(txGrid[xs:xe, ys:ye], tyGrid[xs:xe, ys:ye], Lx, Ly)
# for i in range(xs, xe):
# for j in range(ys, ye):
# O_arr[i,j] = init_data.vorticity(xGrid[i], yGrid[j], Lx, Ly)
if self.nonlinear:
# compute initial streaming function
self.poisson.updateVorticity(self.O)
self.poisson.formRHS(self.Pb)
self.poisson_nullspace.remove(self.Pb)
self.poisson_ksp.solve(self.Pb, self.P)
self.poisson.function(self.P, self.Pf)
pnorm = self.Pf.norm()
psum = self.P.sum()
if PETSc.COMM_WORLD.getRank() == 0:
# print(" Poisson Solver: %5i iterations, residual = %24.16E " % (self.poisson_snes.getLinearSolveIterations(), pnorm) )
print(" Poisson Solver: %5i iterations, residual = %24.16E, sum = %24.16E " % (self.poisson_ksp.getIterationNumber(), pnorm, psum) )
else:
# compute streaming function from input
P_arr = self.da1.getVecArray(self.P)
init_data = __import__("examples." + cfg['initial_data']['python'], globals(), locals(), ['streaming_function'], 0)
P_arr[xs:xe, ys:ye] = init_data.streaming_function(txGrid[xs:xe, ys:ye], tyGrid[xs:xe, ys:ye], Lx, Ly)
# for i in range(xs, xe):
# for j in range(ys, ye):
# P_arr[i,j] = init_data.streaming_function(xGrid[i], yGrid[j], Lx, Ly)
# set streaming function in vorticity solver
self.vorticity.updateStreamingFunction(self.P)
# create HDF5 output file
self.hdf5_viewer = PETSc.ViewerHDF5().create(cfg['io']['hdf5_output'],
mode=PETSc.Viewer.Mode.WRITE,
comm=PETSc.COMM_WORLD)
self.hdf5_viewer.pushGroup("/")
# write grid data to hdf5 file
coords_x = self.dax.getCoordinates()
coords_y = self.day.getCoordinates()
coords_x.setName('x')
coords_y.setName('y')
self.hdf5_viewer(coords_x)
self.hdf5_viewer(coords_y)
# write initial data to hdf5 file
self.save_to_hdf5(0)
def __del__(self):
self.hdf5_viewer.destroy()
self.poisson_ksp.destroy()
self.poisson_mat.destroy()
self.vorticity_mf.destroy()
def run(self):
# loop in time
for itime in range(1, self.nt+1):
if PETSc.COMM_WORLD.getRank() == 0:
localtime = time.asctime( time.localtime(time.time()) )
print("\nit = %4d, t = %10.4f, %s" % (itime, self.ht*itime, localtime) )
self.time.setValue(0, self.ht*itime)
# update history
self.vorticity.updateHistory(self.O, self.P)
# compute norm
self.vorticity.function(self.O, self.Of)
# self.poisson.function (self.P, self.Pf)
norm = self.Of.norm() #+ self.Pf.norm()
# start nonlinear loop
i = 0
if PETSc.COMM_WORLD.getRank() == 0:
print(" Newton Solver: %5i iterations residual = %24.16E " % (i, norm) )
normh = norm
while True:
i += 1
# solve vorticity equation
self.vorticity_snes.solve(None, self.O)
if self.nonlinear:
# build RHS and solve Poisson equation
self.poisson.updateVorticity(self.O)
self.poisson.formRHS(self.Pb)
self.poisson_nullspace.remove(self.Pb)
self.poisson_ksp.solve(self.Pb, self.P)
self.poisson.function(self.P, self.Pf)
pnorm = self.Pf.norm()
# compute norm
self.vorticity.updateStreamingFunction(self.P)
self.vorticity.function(self.O, self.Of)
onorm = self.Of.norm()
norm = onorm# + pnorm
# display some solver information
if PETSc.COMM_WORLD.getRank() == 0:
print(" Newton Solver: %5i iterations residual = %24.16E " % (i, norm) )
print(" Vorticity Solver: %5i iterations, residual = %24.16E " % (self.vorticity_snes.getLinearSolveIterations(), onorm) )
if self.nonlinear:
print(" Poisson Solver: %5i iterations, residual = %24.16E " % (self.poisson_ksp.getIterationNumber(), pnorm) )
if norm < self.snes_atol or i >= self.snes_max_iter or np.abs(normh - norm) < self.snes_rtol:
break
if norm > 10.:
if PETSc.COMM_WORLD.getRank() == 0:
print("ERROR: Residual of nonlinear solver too large.")
sys.exit(1)
# save to hdf5 file
self.save_to_hdf5(itime)
def save_to_hdf5(self, timestep):
if timestep % self.nsave == 0 or timestep == self.nt + 1:
# calculate V field
self.derivatives.dy(self.P, self.Vx)
self.derivatives.dx(self.P, self.Vy)
# save timestep
self.hdf5_viewer.setTimestep(timestep // self.nsave)
self.hdf5_viewer(self.time)
self.hdf5_viewer(self.P)
self.hdf5_viewer(self.O)
self.hdf5_viewer(self.Vx)
self.hdf5_viewer(self.Vy)
def updateVorticityJacobian(self, snes, X, J, P):
# self.vorticity.formMat(J)
if J != P:
self.vorticity.formMat(P)
def updatePoissonJacobian(self, snes, X, J, P):
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='PETSc Vorticity Solver in 2D')
parser.add_argument('runfile', metavar='runconfig', type=str,
help='Run Configuration File')
args = parser.parse_args()
petscvp = petscMHD2D(args.runfile)
petscvp.run()