forked from dcfy/dusty-plasma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dust-gem-55.lua
719 lines (632 loc) · 19.5 KB
/
dust-gem-55.lua
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
-- GEM-challenge problem
Pi = Lucee.Pi
log = Lucee.logInfo
--needed
--Va/c,
--\bata=2*v_th^2/v_A^2,
-- physical parameters
gasGamma = 5./3.
elcCharge = -1.0
ionCharge = 1.0
ionMass = 1.0
elcMass = 100*ionMass --/100.0
lightSpeed = 1.0
epsilon0 = 1.0
mu0 = 1.0
mgnErrorSpeedFactor = 1.0
Lx = 256
Ly = 128
n0 = 1.0
VAe = 0.05 --? electron Alfven velocity
plasmaBeta = 1.0
lambda = 5.0 --?
TiOverTe = 1.0 --?
nbOverN0 = 0.2 --?
pert = 0.1 --? perturbation
Valf = VAe*math.sqrt(elcMass/ionMass)
B0 = Valf*math.sqrt(n0*ionMass) --? need to discussion
OmegaCi0 = ionCharge*B0/ionMass
OmegaCe0 = -elcCharge*B0/elcMass
psi0 = pert*B0 --?
-- resolution and time-stepping
NX = 128
NY = 64
cfl = 0.9
tStart = 0.0
tEnd = 40/OmegaCe0
nFrames = 4
log(string.format("elcMass/ionMass=1/%g",ionMass/elcMass)) --? log(string.format("ionMass/elcMass=%g",ionMass/elcMass))
log(string.format("Lx=%gdi=%gde", Lx,Lx*math.sqrt(ionMass/elcMass))) --? di = math.sqrt(ionMass/elcMass)*de
log(string.format("plasmaBeta=%g", plasmaBeta))
log(string.format("Valf/c=%g", Valf))
log(string.format("lambda/di=%g", lambda))
log(string.format("TiOverTe=%g", TiOverTe))
log(string.format("nbOverN0=%g", nbOverN0))
log(string.format("pert=%g", pert))
log(string.format("tEnd=%g, nFrames=%d",tEnd,nFrames))
log(string.format("NX=%d, dx=%g, di=%gde, cfl=%g\n", NX,Lx/NX,Lx/NX*math.sqrt(ionMass/elcMass),cfl))
------------------------------------------------
-- COMPUTATIONAL DOMAIN, DATA STRUCTURE, ETC. --
------------------------------------------------
-- decomposition object
decomp = DecompRegionCalc2D.CartGeneral {}
-- computational domain
grid = Grid.RectCart2D {
lower = {-Lx/2, -Ly/2},
upper = {Lx/2, Ly/2},
cells = {NX, NY},
decomposition = decomp,
periodicDirs = {0},
}
-- solution
q = DataStruct.Field2D {
onGrid = grid,
numComponents = 18,
ghost = {2, 2},
}
-- solution after update along X (ds algorithm)
qX = DataStruct.Field2D {
onGrid = grid,
numComponents = 18,
ghost = {2, 2},
}
-- final updated solution
qNew = DataStruct.Field2D {
onGrid = grid,
numComponents = 18,
ghost = {2, 2},
}
-- duplicate copy in case we need to take the step again
qDup = DataStruct.Field2D {
onGrid = grid,
numComponents = 18,
ghost = {2, 2},
}
qNewDup = DataStruct.Field2D {
onGrid = grid,
numComponents = 18,
ghost = {2, 2},
}
-- aliases to various sub-systems
elcFluid = q:alias(0, 5)
ionFluid = q:alias(5, 10)
emField = q:alias(10, 18)
elcFluidX = qX:alias(0, 5)
ionFluidX = qX:alias(5, 10)
emFieldX = qX:alias(10, 18)
elcFluidNew = qNew:alias(0, 5)
ionFluidNew = qNew:alias(5, 10)
emFieldNew = qNew:alias(10, 18)
-----------------------
-- INITIAL CONDITION --
-----------------------
-- initial conditions
function init(x,y,z)
local tanh = math.tanh
local cosh = math.cosh
local cos = math.cos
local sin = math.sin
local me = elcMass
local mi = ionMass
local qe = elcCharge
local qi = ionCharge
local g1 = gasGamma-1.0
local l = lambda
local TeFrac = 1.0 / (1.0 + TiOverTe)
local TiFrac = 1.0 - TeFrac
local sech2 = (1.0/cosh(y/l))^2
local _2pi = 2.0*Pi --? why _
local Bxb = B0*tanh(y/l)
local Bx = Bxb - psi0*(Pi/Ly)*cos(_2pi*x/Lx)*sin(Pi*y/Ly)
local By = psi0*(_2pi/Lx)*sin(_2pi*x/Lx)*cos(Pi*y/Ly)
local Bz = 0.0
local n = n0*(sech2 + nbOverN0)
local Jz = -(B0/l)*sech2
local Ttotal = plasmaBeta*(B0*B0)/2.0/n0
local rhoe = n*me
local ezmom = (me/qe)*Jz*TeFrac --? eletron momentum in z
local ere = n*Ttotal*TeFrac/g1 + 0.5*ezmom*ezmom/rhoe --? electron energy, why B^2/2
local rhoi = n*mi
local izmom = (mi/qi)*Jz*TiFrac
local eri = n*Ttotal*TiFrac/g1 + 0.5*izmom*izmom/rhoi
return rhoe, 0.0, 0.0, ezmom, ere, rhoi, 0.0, 0.0, izmom, eri, 0.0, 0.0, 0.0, Bx, By, Bz, 0.0, 0.0
end
------------------------
-- Boundary Condition --
------------------------
-- boundary applicator objects for fluids and fields
bcElcCopy = BoundaryCondition.Copy { components = {0, 4} } --? 0 is density and 4 is pressure?
bcElcWall = BoundaryCondition.ZeroNormal { components = {1, 2, 3} }
bcIonCopy = BoundaryCondition.Copy { components = {5, 9} }
bcIonWall = BoundaryCondition.ZeroNormal { components = {6, 7, 8} }
bcElcFld = BoundaryCondition.ZeroTangent { components = {10, 11, 12} } --? tangential component of ElcFld is continuous across boundary
bcMgnFld = BoundaryCondition.ZeroNormal { components = {13, 14, 15} } --? normal component of MgnFld is continuous across the boundary
bcPot = BoundaryCondition.Copy { components = {16, 17}, fact = {-1, +1} } --?
--FIXME: fact in bcPot
-- create boundary condition object
function createBc(myDir, myEdge)
local bc = Updater.Bc2D {
onGrid = grid,
-- boundary conditions to apply
boundaryConditions = {
bcElcCopy, bcElcWall,
bcIonCopy, bcIonWall,
bcElcFld, bcMgnFld, bcPot,
},
-- direction to apply
dir = myDir,
-- edge to apply on
edge = myEdge,
}
return bc
end
-- create updaters to apply boundary conditions
bcBottom = createBc(1, "lower")
bcTop = createBc(1, "upper")
-- function to apply boundary conditions to specified field
function applyBc(fld, tCurr, myDt)
for i,bc in ipairs({bcBottom, bcTop}) do
bc:setOut( {fld} )
bc:advance(tCurr+myDt)
end
-- sync ghost cells
fld:sync()
end
----------------------
-- EQUATION SOLVERS --
----------------------
-- regular Euler equations
elcEulerEqn = HyperEquation.Euler {
gasGamma = gasGamma,
}
ionEulerEqn = HyperEquation.Euler {
gasGamma = gasGamma,
}
-- (Lax equations are used to fix negative pressure/density)
elcEulerLaxEqn = HyperEquation.Euler {
gasGamma = gasGamma,
numericalFlux = "lax",
}
ionEulerLaxEqn = HyperEquation.Euler {
gasGamma = gasGamma,
numericalFlux = "lax",
}
maxwellEqn = HyperEquation.PhMaxwell {
lightSpeed = lightSpeed,
elcErrorSpeedFactor = 0.0,
mgnErrorSpeedFactor = mgnErrorSpeedFactor --?
}
-- ds solvers for regular Euler equations along X
elcFluidSlvrDir0 = Updater.WavePropagation2D {
onGrid = grid,
equation = elcEulerEqn,
-- one of no-limiter, min-mod, superbee,
-- van-leer, monotonized-centered, beam-warming
limiter = "monotonized-centered",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {0} -- directions to update
}
ionFluidSlvrDir0 = Updater.WavePropagation2D {
onGrid = grid,
equation = ionEulerEqn,
limiter = "monotonized-centered",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {0}
}
maxSlvrDir0 = Updater.WavePropagation2D {
onGrid = grid,
equation = maxwellEqn,
limiter = "monotonized-centered",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {0}
}
-- ds solvers for regular Euler equations along Y
elcFluidSlvrDir1 = Updater.WavePropagation2D {
onGrid = grid,
equation = elcEulerEqn,
limiter = "monotonized-centered",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {1}
}
ionFluidSlvrDir1 = Updater.WavePropagation2D {
onGrid = grid,
equation = ionEulerEqn,
limiter = "monotonized-centered",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {1}
}
maxSlvrDir1 = Updater.WavePropagation2D {
onGrid = grid,
equation = maxwellEqn,
limiter = "monotonized-centered",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {1}
}
-- ds solvers for Lax Euler equations along X
elcLaxSlvrDir0 = Updater.WavePropagation2D {
onGrid = grid,
equation = elcEulerLaxEqn,
limiter = "zero",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {0}
}
ionLaxSlvrDir0 = Updater.WavePropagation2D {
onGrid = grid,
equation = ionEulerLaxEqn,
limiter = "zero",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {0}
}
maxLaxSlvrDir0 = Updater.WavePropagation2D {
onGrid = grid,
equation = maxwellEqn,
limiter = "zero",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {0}
}
-- ds solvers for Lax Euler equations along Y
elcLaxSlvrDir1 = Updater.WavePropagation2D {
onGrid = grid,
equation = elcEulerLaxEqn,
limiter = "zero",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {1}
}
ionLaxSlvrDir1 = Updater.WavePropagation2D {
onGrid = grid,
equation = ionEulerLaxEqn,
limiter = "zero",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {1}
}
maxLaxSlvrDir1 = Updater.WavePropagation2D {
onGrid = grid,
equation = maxwellEqn,
limiter = "zero",
cfl = cfl,
cflm = 1.1*cfl,
updateDirections = {1}
}
-- updater for source terms
sourceSlvr = Updater.ImplicitFiveMomentSrc2D {
onGrid = grid,
numFluids = 2,
charge = {elcCharge, ionCharge},
mass = {elcMass, ionMass},
epsilon0 = epsilon0,
-- linear solver to use: one of partialPivLu or colPivHouseholderQr
linearSolver = "partialPivLu",
hasStaticField = false,
}
elcIonMomRelax = Updater.TwoFluidMomentumRelaxation2D {
onGrid = grid,
electronIonCollisionFrequency = 0.0,
frictionFactor = 0.5
}
-- function to update source terms
function updateSource(elcIn, ionIn, emIn, tCurr, t)
sourceSlvr:setOut( {elcIn, ionIn, emIn} )
sourceSlvr:setCurrTime(tCurr)
sourceSlvr:advance(t)
end
-- function to update the fluid and field using dimensional splitting
function updateFluidsAndField(tCurr, t)
local myStatus = true
local myDtSuggested = 1e3*math.abs(t-tCurr)
local useLaxSolver = False
-- X-direction updates
for i,slvr in ipairs({elcFluidSlvrDir0, ionFluidSlvrDir0, maxSlvrDir0}) do
slvr:setCurrTime(tCurr)
local status, dtSuggested = slvr:advance(t)
myStatus = status and myStatus
myDtSuggested = math.min(myDtSuggested, dtSuggested)
end
if ((elcEulerEqn:checkInvariantDomain(elcFluidX) == false)
or (ionEulerEqn:checkInvariantDomain(ionFluidX) == false)) then
useLaxSolver = true
end
if ((myStatus == false) or (useLaxSolver == true)) then
return myStatus, myDtSuggested, useLaxSolver
end
-- apply BCs to intermediate update after X sweep
applyBc(qX, tCurr, t-tCurr)
-- Y-direction updates
for i,slvr in ipairs({elcFluidSlvrDir1, ionFluidSlvrDir1, maxSlvrDir1}) do
slvr:setCurrTime(tCurr)
local status, dtSuggested = slvr:advance(t)
myStatus = status and myStatus
myDtSuggested = math.min(myDtSuggested, dtSuggested)
end
if ((elcEulerEqn:checkInvariantDomain(elcFluidNew) == false)
or (ionEulerEqn:checkInvariantDomain(ionFluidNew) == false)) then
useLaxSolver = true
end
return myStatus, myDtSuggested, useLaxSolver
end
-- function to take one time-step with Euler solver
function solveTwoFluidSystem(tCurr, t)
local dthalf = 0.5*(t-tCurr)
-- update source terms
updateSource(elcFluid, ionFluid, emField, tCurr, tCurr+dthalf)
applyBc(q, tCurr, t-tCurr)
-- update fluids and fields
local status, dtSuggested, useLaxSolver = updateFluidsAndField(tCurr, t)
-- update source terms
updateSource(elcFluidNew, ionFluidNew, emFieldNew, tCurr, tCurr+dthalf)
applyBc(qNew, tCurr, t-tCurr)
return status, dtSuggested,useLaxSolver
end
-- function to update the fluid and field using dimensional splitting Lax scheme
function updateFluidsAndFieldLax(tCurr, t)
local myStatus = true
local myDtSuggested = 1e3*math.abs(t-tCurr)
for i,slvr in ipairs({elcLaxSlvrDir0, ionLaxSlvrDir0, maxLaxSlvrDir0}) do
slvr:setCurrTime(tCurr)
local status, dtSuggested = slvr:advance(t)
myStatus = status and myStatus
myDtSuggested = math.min(myDtSuggested, dtSuggested)
end
applyBc(qX, tCurr, t-tCurr)
-- Y-direction updates
for i,slvr in ipairs({elcLaxSlvrDir1, ionLaxSlvrDir1, maxLaxSlvrDir1}) do
slvr:setCurrTime(tCurr)
local status, dtSuggested = slvr:advance(t)
myStatus = status and myStatus
myDtSuggested = math.min(myDtSuggested, dtSuggested)
end
return myStatus, myDtSuggested
end
-- function to take one time-step with Lax Euler solver
function solveTwoFluidLaxSystem(tCurr, t)
local dthalf = 0.5*(t-tCurr)
-- update source terms
updateSource(elcFluid, ionFluid, emField, tCurr, tCurr+dthalf)
applyBc(q, tCurr, t-tCurr)
-- update fluids and fields
local status, dtSuggested = updateFluidsAndFieldLax(tCurr, t)
-- update source terms
updateSource(elcFluidNew, ionFluidNew, emFieldNew, tCurr, tCurr+dthalf)
applyBc(qNew, tCurr, t-tCurr)
return status, dtSuggested
end
----------------------------
-- DIAGNOSIS AND DATA I/O --
----------------------------
-- dynvector to store integrated flux
byAlias = qNew:alias(14, 15)
byFlux = DataStruct.DynVector { numComponents = 1 }
byFluxCalc = Updater.IntegrateFieldAlongLine2D {
onGrid = grid,
-- start cell
startCell = {0, NY/2},
-- direction to integrate in
dir = 0,
-- number of cells to integrate
numCells = NX,
-- integrand
integrand = function (by)
return math.abs(by)
end,
}
byFluxCalc:setIn( {byAlias} )
byFluxCalc:setOut( {byFlux} )
-- dynvector to store Ez at X-point
ezAlias = qNew:alias(12, 13)
xpointEz = DataStruct.DynVector { numComponents = 1 }
xpointEzRec = Updater.RecordFieldInCell2D {
onGrid = grid,
-- index of cell to record
cellIndex = {(NX-1)/2, (NY-1)/2},
}
xpointEzRec:setIn( {ezAlias} )
xpointEzRec:setOut( {xpointEz} )
-- dynvector to store number density at X-point
neAlias = qNew:alias(0, 1)
xpointNe = DataStruct.DynVector { numComponents = 1 }
xpointNeRec = Updater.RecordFieldInCell2D {
onGrid = grid,
-- index of cell to record
cellIndex = {(NX-1)/2, (NY-1)/2},
}
xpointNeRec:setIn( {neAlias} )
xpointNeRec:setOut( {xpointNe} )
-- dynvector to store electron uz at X-point
uzeAlias = qNew:alias(3, 4)
xpointUze = DataStruct.DynVector { numComponents = 1 }
xpointUzeRec = Updater.RecordFieldInCell2D {
onGrid = grid,
-- index of cell to record
cellIndex = {(NX-1)/2, (NY-1)/2},
}
xpointUzeRec:setIn( {uzeAlias} )
xpointUzeRec:setOut( {xpointUze} )
-- dynvector to store ion uz at X-point
uziAlias = qNew:alias(8, 9)
xpointUzi = DataStruct.DynVector { numComponents = 1 }
xpointUziRec = Updater.RecordFieldInCell2D {
onGrid = grid,
-- index of cell to record
cellIndex = {(NX-1)/2, (NY-1)/2},
}
xpointUziRec:setIn( {uziAlias} )
xpointUziRec:setOut( {xpointUzi} )
-- dynvector to store ion uz at X-point
uziAlias = qNew:alias(8, 9)
xpointUzi = DataStruct.DynVector { numComponents = 1 }
xpointUziRec = Updater.RecordFieldInCell2D {
onGrid = grid,
-- index of cell to record
cellIndex = {(NX-1)/2, (NY-1)/2},
}
xpointUziRec:setIn( {uziAlias} )
xpointUziRec:setOut( {xpointUzi} )
-- dynvector to store electron fluid energy
elcEnergy = DataStruct.DynVector { numComponents = 1 }
elcEnergyCalc = Updater.IntegrateField2D {
onGrid = grid,
-- index of cell to record
integrand = function (rho, rhou, rhov, rhow, er)
return er
end,
}
elcEnergyCalc:setIn( {elcFluid} )
elcEnergyCalc:setOut( {elcEnergy} )
-- dynvector to store ion fluid energy
ionEnergy = DataStruct.DynVector { numComponents = 1 }
ionEnergyCalc = Updater.IntegrateField2D {
onGrid = grid,
-- index of cell to record
integrand = function (rho, rhou, rhov, rhow, er)
return er
end,
}
ionEnergyCalc:setIn( {ionFluid} )
ionEnergyCalc:setOut( {ionEnergy} )
-- dynvector to EM energy
emEnergy = DataStruct.DynVector { numComponents = 1 }
emEnergyCalc = Updater.IntegrateField2D {
onGrid = grid,
-- index of cell to record
integrand = function (ex, ey, ez, bx, by, bz, e1, e2)
return 0.5*epsilon0*(ex^2+ey^2+ez^2) + 0.5/mu0*(bx^2+by^2+bz^2)
end,
}
emEnergyCalc:setIn( {emField} )
emEnergyCalc:setOut( {emEnergy} )
-- compute diagnostic
function calcDiagnostics(tCurr, myDt)
for i,diag in ipairs({byFluxCalc, elcEnergyCalc, ionEnergyCalc, emEnergyCalc}) do
diag:setCurrTime(tCurr)
diag:advance(tCurr+myDt)
end
end
-- write data to H5 files
function writeFields(frame, t)
qNew:write( string.format("q_%d.h5", frame), t )
byFlux:write( string.format("byFlux_%d.h5", frame) )
elcEnergy:write( string.format("elcEnergy_%d.h5", frame) )
ionEnergy:write( string.format("ionEnergy_%d.h5", frame) )
emEnergy:write( string.format("emEnergy_%d.h5", frame) )
end
----------------------------
-- TIME-STEPPING FUNCTION --
----------------------------
function runSimulation(tStart, tEnd, nFrames, initDt)
local frame = 1
local tFrame = (tEnd-tStart)/nFrames
local nextIOt = tFrame
local step = 1
local tCurr = tStart
local myDt = initDt
local status, dtSuggested
local useLaxSolver = false
-- the grand loop
while true do
-- copy q and qNew in case we need to take this step again
qDup:copy(q)
qNewDup:copy(qNew)
-- if needed adjust dt to hit tEnd exactly
if (tCurr+myDt > tEnd) then
myDt = tEnd-tCurr
end
-- advance fluids and fields
if (useLaxSolver) then
-- call Lax solver if positivity violated
log (string.format(" Taking step %5d at time %6g with dt %g (using Lax solvers)", step, tCurr, myDt))
status, dtSuggested = solveTwoFluidLaxSystem(tCurr, tCurr+myDt)
useLaxSolver = false
else
log (string.format(" Taking step %5d at time %6g with dt %g", step, tCurr, myDt))
status, dtSuggested, useLaxSolver = solveTwoFluidSystem(tCurr, tCurr+myDt)
end
if (status == false) then
-- time-step too large
log (string.format(" ** Time step %g too large! Will retake with dt %g", myDt, dtSuggested))
myDt = dtSuggested
qNew:copy(qNewDup)
q:copy(qDup)
elseif (useLaxSolver == true) then
-- negative density/pressure occured
log (string.format(" ** Negative pressure or density at %8g! Will retake step with Lax fluxes", tCurr+myDt))
q:copy(qDup)
qNew:copy(qNewDup)
else
-- check if a nan occured
if (qNew:hasNan()) then
log (string.format(" ** NaN occured at %g! Stopping simulation", tCurr))
break
end
-- compute diagnostics
calcDiagnostics(tCurr, myDt)
-- copy updated solution back
q:copy(qNew)
-- write out data
if (tCurr+myDt > nextIOt or tCurr+myDt >= tEnd) then
log (string.format(" Writing data at time %g (frame %d) ...\n", tCurr+myDt, frame))
writeFields(frame, tCurr+myDt)
frame = frame + 1
nextIOt = nextIOt + tFrame
step = 0
end
tCurr = tCurr + myDt
myDt = dtSuggested
step = step + 1
-- check if done
if (tCurr >= tEnd) then
break
end
end
end -- end of time-step loop
return dtSuggested
end
----------------------------
-- RUNNING THE SIMULATION --
----------------------------
-- setup initial condition
q:set(init)
q:sync()
qNew:copy(q)
-- set input/output arrays for various solvers
elcFluidSlvrDir0:setIn( {elcFluid} )
elcFluidSlvrDir0:setOut( {elcFluidX} )
ionFluidSlvrDir0:setIn( {ionFluid} )
ionFluidSlvrDir0:setOut( {ionFluidX} )
maxSlvrDir0:setIn( {emField} )
maxSlvrDir0:setOut( {emFieldX} )
elcFluidSlvrDir1:setIn( {elcFluidX} )
elcFluidSlvrDir1:setOut( {elcFluidNew} )
ionFluidSlvrDir1:setIn( {ionFluidX} )
ionFluidSlvrDir1:setOut( {ionFluidNew} )
maxSlvrDir1:setIn( {emFieldX} )
maxSlvrDir1:setOut( {emFieldNew} )
elcLaxSlvrDir0:setIn( {elcFluid} )
elcLaxSlvrDir0:setOut( {elcFluidX} )
ionLaxSlvrDir0:setIn( {ionFluid} )
ionLaxSlvrDir0:setOut( {ionFluidX} )
maxLaxSlvrDir0:setIn( {emField} )
maxLaxSlvrDir0:setOut( {emFieldX} )
elcLaxSlvrDir1:setIn( {elcFluidX} )
elcLaxSlvrDir1:setOut( {elcFluidNew} )
ionLaxSlvrDir1:setIn( {ionFluidX} )
ionLaxSlvrDir1:setOut( {ionFluidNew} )
maxLaxSlvrDir1:setIn( {emFieldX} )
maxLaxSlvrDir1:setOut( {emFieldNew} )
-- apply BCs on initial conditions
applyBc(q, 0.0, 0.0)
applyBc(qNew, 0.0, 0.0)
-- write initial conditions
calcDiagnostics(0.0, 0.0)
writeFields(0, 0.0)
initDt = 100.0
runSimulation(tStart, tEnd, nFrames, initDt)