forked from dschwoerer/hermes-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhermes-2.cxx
3775 lines (3172 loc) · 126 KB
/
hermes-2.cxx
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright B.Dudson, J.Leddy, University of York, 2016-2019
email: benjamin.dudson@york.ac.uk
This file is part of Hermes-2 (Hot ion version)
Hermes is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Hermes is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hermes. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hermes-2.hxx"
#include <derivs.hxx>
#include <field_factory.hxx>
#include <initialprofiles.hxx>
#include <invert_parderiv.hxx>
#include "parallel_boundary_region.hxx"
#include "boundary_region.hxx"
#include "div_ops.hxx"
#include "loadmetric.hxx"
#include <bout/constants.hxx>
#include <bout/assert.hxx>
#include <bout/fv_ops.hxx>
// OpenADAS interface Atomicpp by T.Body
#include "atomicpp/ImpuritySpecies.hxx"
#include "atomicpp/Prad.hxx"
#define SETNAME(expr) setName(expr, #expr)
std::string parbc{"parallel_neumann_o1"};
namespace FV {
template<typename CellEdges = MC>
const Field3D Div_par_fvv(const Field3D &f_in, const Field3D &v_in,
const Field3D &wave_speed_in, bool fixflux=true) {
ASSERT1(areFieldsCompatible(f_in, v_in));
ASSERT1(areFieldsCompatible(f_in, wave_speed_in));
bool use_parallel_slices = (f_in.hasParallelSlices() && v_in.hasParallelSlices()
&& wave_speed_in.hasParallelSlices());
Mesh* mesh = f_in.getMesh();
CellEdges cellboundary;
/// Ensure that f, v and wave_speed are field aligned
Field3D f = use_parallel_slices ? f_in : toFieldAligned(f_in, "RGN_NOX");
Field3D v = use_parallel_slices ? v_in : toFieldAligned(v_in, "RGN_NOX");
Field3D wave_speed = use_parallel_slices ?
wave_speed_in : toFieldAligned(wave_speed_in, "RGN_NOX");
Coordinates *coord = f_in.getCoordinates();
Field3D result{zeroFrom(f)};
// Only need one guard cell, so no need to communicate fluxes
// Instead calculate in guard cells to preserve fluxes
int ys = mesh->ystart-1;
int ye = mesh->yend+1;
for (int i = mesh->xstart; i <= mesh->xend; i++) {
if (!mesh->firstY(i) || mesh->periodicY(i)) {
// Calculate in guard cell to get fluxes consistent between processors
ys = mesh->ystart - 1;
} else {
// Don't include the boundary cell. Note that this implies special
// handling of boundaries later
ys = mesh->ystart;
}
if (!mesh->lastY(i) || mesh->periodicY(i)) {
// Calculate in guard cells
ye = mesh->yend + 1;
} else {
// Not in boundary cells
ye = mesh->yend;
}
for (int j = ys; j <= ye; j++) {
for (int k = 0; k < mesh->LocalNz; k++) {
// For right cell boundaries
BoutReal common_factor = (coord->J(i, j, k) + coord->J(i, j + 1, k)) /
(sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j + 1, k)));
BoutReal flux_factor_rc = common_factor / (coord->dy(i, j, k) * coord->J(i, j, k));
BoutReal flux_factor_rp = common_factor / (coord->dy(i, j + 1, k) * coord->J(i, j + 1, k));
// For left cell boundaries
common_factor = (coord->J(i, j, k) + coord->J(i, j - 1, k)) /
(sqrt(coord->g_22(i, j, k)) + sqrt(coord->g_22(i, j - 1, k)));
BoutReal flux_factor_lc = common_factor / (coord->dy(i, j, k) * coord->J(i, j, k));
BoutReal flux_factor_lm = common_factor / (coord->dy(i, j - 1, k) * coord->J(i, j - 1, k));
////////////////////////////////////////////
// Reconstruct f at the cell faces
// This calculates s.R and s.L for the Right and Left
// face values on this cell
// Reconstruct f at the cell faces
Stencil1D s;
s.c = f(i, j, k);
s.m = f(i, j - 1, k);
s.p = f(i, j + 1, k);
cellboundary(s); // Calculate s.R and s.L
// Reconstruct v at the cell faces
Stencil1D sv;
sv.c = v(i, j, k);
sv.m = v(i, j - 1, k);
sv.p = v(i, j + 1, k);
cellboundary(sv);
////////////////////////////////////////////
// Right boundary
// Calculate velocity at right boundary (y+1/2)
BoutReal vpar = 0.5 * (v(i, j, k) + v(i, j + 1, k));
BoutReal flux;
if (mesh->lastY(i) && (j == mesh->yend) && !mesh->periodicY(i)) {
// Last point in domain
BoutReal bndryval = 0.5 * (s.c + s.p);
if (fixflux) {
// Use mid-point to be consistent with boundary conditions
flux = bndryval * vpar * vpar;
} else {
// Add flux due to difference in boundary values
flux = s.R * vpar * sv.R + wave_speed(i, j, k) * (s.R * sv.R - bndryval * vpar);
}
} else {
// Maximum wave speed in the two cells
BoutReal amax = BOUTMAX(wave_speed(i, j, k), wave_speed(i, j + 1, k));
if (vpar > amax) {
// Supersonic flow out of this cell
flux = s.R * vpar * sv.R;
} else if (vpar < -amax) {
// Supersonic flow into this cell
flux = 0.0;
} else {
// Subsonic flow, so a mix of right and left fluxes
flux = s.R * 0.5 * (vpar + amax) * sv.R;
}
}
result(i, j, k) += flux * flux_factor_rc;
result(i, j + 1, k) -= flux * flux_factor_rp;
////////////////////////////////////////////
// Calculate at left boundary
vpar = 0.5 * (v(i, j, k) + v(i, j - 1, k));
if (mesh->firstY(i) && (j == mesh->ystart) && !mesh->periodicY(i)) {
// First point in domain
BoutReal bndryval = 0.5 * (s.c + s.m);
if (fixflux) {
// Use mid-point to be consistent with boundary conditions
flux = bndryval * vpar * vpar;
} else {
// Add flux due to difference in boundary values
flux = s.L * vpar * sv.L - wave_speed(i, j, k) * (s.L * sv.L - bndryval * vpar);
}
} else {
// Maximum wave speed in the two cells
BoutReal amax = BOUTMAX(wave_speed(i, j, k), wave_speed(i, j - 1, k));
if (vpar < -amax) {
// Supersonic out of this cell
flux = s.L * vpar * sv.L;
} else if (vpar > amax) {
// Supersonic into this cell
flux = 0.0;
} else {
flux = s.L * 0.5 * (vpar - amax) * sv.L;
}
}
result(i, j, k) -= flux * flux_factor_lc;
result(i, j - 1, k) += flux * flux_factor_lm;
}
}
}
return fromFieldAligned(result, "RGN_NOBNDRY");
}
}
BoutReal floor(BoutReal var, BoutReal f) {
if (var < f)
return f;
return var;
}
/// Returns a copy of input \p var with all values greater than \p f replaced by
/// \p f.
const Field3D ceil(const Field3D &var, BoutReal f, REGION rgn = RGN_ALL) {
checkData(var);
Field3D result = copy(var);
BOUT_FOR(d, var.getRegion(rgn)) {
if (result[d] > f) {
result[d] = f;
}
}
return result;
}
// Square function for vectors
Field3D SQ(const Vector3D &v) { return v * v; }
void setRegions(Field3D &f) {
f.yup().setRegion("RGN_YPAR_+1");
f.ydown().setRegion("RGN_YPAR_-1");
}
const Field3D &yup(const Field3D &f) { return f.yup(); }
BoutReal yup(BoutReal f) { return f; };
const Field3D &ydown(const Field3D &f) { return f.ydown(); }
BoutReal ydown(BoutReal f) { return f; };
const BoutReal yup(BoutReal f, Ind3D i) { return f; };
const BoutReal ydown(BoutReal f, Ind3D i) { return f; };
// const BoutReal& yup(const Field3D &f, Ind3D i) { return f.yup()[i.yp()]; }
// const BoutReal& ydown(const Field3D &f, Ind3D i) { return f.ydown()[i.ym()];
// } BoutReal& yup(Field3D &f, Ind3D i) { return f.yup()[i.yp()]; } BoutReal&
// ydown(Field3D &f, Ind3D i) { return f.ydown()[i.ym()]; }
const BoutReal &yup(const Field3D &f, Ind3D i) { return f.yup()[i]; }
const BoutReal &ydown(const Field3D &f, Ind3D i) { return f.ydown()[i]; }
BoutReal &yup(Field3D &f, Ind3D i) { return f.yup()[i]; }
BoutReal &ydown(Field3D &f, Ind3D i) { return f.ydown()[i]; }
const BoutReal &_get(const Field3D &f, Ind3D i) { return f[i]; }
BoutReal &_get(Field3D &f, Ind3D i) { return f[i]; }
BoutReal _get(BoutReal f, Ind3D i) { return f; };
BoutReal copy(BoutReal f) { return f; };
void alloc_all(Field3D &f) {
f.allocate();
f.splitParallelSlices();
f.yup().allocate();
f.ydown().allocate();
setRegions(f);
}
#define GET_ALL(name) \
auto *name##a = &name[Ind3D(0)]; \
auto *name##b = &name.yup()[Ind3D(0)]; \
auto *name##c = &name.ydown()[Ind3D(0)];
#define DO_ALL(op, name) \
template <class A, class B> Field3D name##_all(const A &a, const B &b) { \
Field3D result; \
alloc_all(result); \
BOUT_FOR(i, result.getRegion("RGN_ALL")) { name##_all(result, a, b, i); } \
setRegions(result); \
return result; \
} \
template <class A, class B> \
void name##_all(Field3D &result, const A &a, const B &b, Ind3D i) { \
result[i] = op(_get(a, i), _get(b, i)); \
yup(result, i) = op(yup(a, i), yup(b, i)); \
ydown(result, i) = op(ydown(a, i), ydown(b, i)); \
} \
template <class B> void name##_all(Field3D &result, const B &b, Ind3D i) { \
result[i] = op(result[i], _get(b, i)); \
yup(result, i) = op(yup(result, i), yup(b, i)); \
ydown(result, i) = op(ydown(result, i), ydown(b, i)); \
}
DO_ALL(floor, floor)
DO_ALL(pow, pow)
#undef DO_ALL
#define DO_ALL(op, name) \
template <class A, class B> Field3D name##_all(const A &a, const B &b) { \
Field3D result; \
alloc_all(result); \
BOUT_FOR(i, result.getRegion("RGN_ALL")) { name##_all(result, a, b, i); } \
checkData(result, "RGN_ALL"); \
setRegions(result); \
return result; \
} \
Field3D name##_all(const Field3D &a, const Field3D &b) { \
Field3D result; \
alloc_all(result); \
const int n = result.getNx() * result.getNy() * result.getNz(); \
GET_ALL(result); \
GET_ALL(a); \
GET_ALL(b); \
BOUT_OMP(omp parallel for simd) \
for (int i = 0; i < n; ++i) { \
resulta[i] = aa[i] op ba[i]; \
resultb[i] = ab[i] op bb[i]; \
resultc[i] = ac[i] op bc[i]; \
} \
setRegions(result); \
return result; \
} \
Field3D name##_all(const Field3D &a, BoutReal b) { \
Field3D result; \
alloc_all(result); \
const int n = result.getNx() * result.getNy() * result.getNz(); \
GET_ALL(result); \
GET_ALL(a); \
BOUT_OMP(omp parallel for simd) \
for (int i = 0; i < n; ++i) { \
resulta[i] = aa[i] op b; \
resultb[i] = ab[i] op b; \
resultc[i] = ac[i] op b; \
} \
setRegions(result); \
return result; \
} \
template <class A, class B> \
void name##_all(Field3D &result, const A &a, const B &b, Ind3D i) { \
result[i] = _get(a, i) op _get(b, i); \
yup(result, i) = yup(a, i) op yup(b, i); \
ydown(result, i) = ydown(a, i) op ydown(b, i); \
}
// void div_all(Field3D & result, const Field3D & a, const Field3D & b, Ind3D i)
// {
// result[i] = a[i] / b[i];
// result.yup()[i.yp()] = a.yup()[i.yp()] / b.yup()[i.yp()];
// result.ydown()[i.ym()] = a.ydown()[i.ym()] / b.ydown()[i.ym()];
// }
//#include "mul_all.cxx"
DO_ALL(*, mul)
DO_ALL(/, div)
DO_ALL(+, add)
DO_ALL(-, sub)
#undef DO_ALL
#define DO_ALL(op, name) \
template <class A, class B> Field3D &name##_all_inp(A &a, const B &b) { \
BOUT_FOR(i, a.getRegion("RGN_ALL")) { name##_all(a, b, i); } \
checkData(a, "RGN_ALL"); \
return a; \
} \
Field3D &name##_all_inp(Field3D &a, const Field3D &b) { \
const int n = a.getNx() * a.getNy() * a.getNz(); \
GET_ALL(a); \
GET_ALL(b); \
BOUT_OMP(omp parallel for simd) \
for (int i = 0; i < n; ++i) { \
aa[i] op ba[i]; \
ab[i] op bb[i]; \
ac[i] op bc[i]; \
} \
return a; \
} \
Field3D &name##_all_inp(Field3D &a, BoutReal b) { \
const int n = a.getNx() * a.getNy() * a.getNz(); \
GET_ALL(a); \
BOUT_OMP(omp parallel for simd) \
for (int i = 0; i < n; ++i) { \
aa[i] op b; \
ab[i] op b; \
ac[i] op b; \
} \
return a; \
} \
template <class A, class B> void name##_all_inp(A &a, const B &b, Ind3D i) { \
a[i] op _get(b, i); \
yup(a, i) op yup(b, i); \
ydown(a, i) op ydown(b, i); \
}
// #include "mul_all.cxx"
DO_ALL(*=, mul)
DO_ALL(/=, div)
DO_ALL(+=, add)
DO_ALL(-=, sub)
#undef DO_ALL
#define DO_ALL(op) \
inline void op##_all(Field3D &result, const Field3D &a, Ind3D i) { \
result[i] = op(a[i]); \
yup(result, i) = op(yup(a, i)); \
ydown(result, i) = op(ydown(a, i)); \
} \
inline Field3D op##_all(const Field3D &a) { \
Field3D result; \
alloc_all(result); \
BOUT_FOR(i, result.getRegion("RGN_ALL")) { op##_all(result, a, i); } \
checkData(result, "RGN_ALL"); \
setRegions(result); \
return result; \
}
DO_ALL(sqrt)
DO_ALL(SQ)
DO_ALL(copy)
DO_ALL(exp)
DO_ALL(log)
#undef DO_ALL
void set_all(Field3D &f, BoutReal val) {
alloc_all(f);
BOUT_FOR(i, f.getRegion("RGN_ALL")) {
f[i] = val;
f.yup()[i] = val;
f.ydown()[i] = val;
}
}
void zero_all(Field3D &f) { set_all(f, 0); }
void check_all(Field3D &f) {
checkData(f);
checkData(f.yup());
checkData(f.ydown());
}
void ASSERT_CLOSE_ALL(const Field3D &a, const Field3D &b) {
BOUT_FOR(i, a.getRegion("RGN_NOY")) {
ASSERT0(std::abs(a[i] - b[i]) < 1e-10);
ASSERT0(std::abs(yup(a, i) - yup(b, i)) < 1e-10);
ASSERT0(std::abs(ydown(a, i) - ydown(b, i)) < 1e-10);
}
}
/// Modifies and returns the first argument, taking the boundary from second argument
/// This is used because unfortunately Field3D::setBoundary returns void
Field3D withBoundary(Field3D &&f, const Field3D &bndry) {
f.setBoundaryTo(bndry);
return f;
}
int Hermes::init(bool restarting) {
auto& opt = Options::root();
// Switches in model section
auto& optsc = opt["Hermes"];
OPTION(optsc, evolve_plasma, true);
OPTION(optsc, show_timesteps, false);
if (BoutComm::rank() != 0) {
show_timesteps = false;
}
electromagnetic = optsc["electromagnetic"]
.doc("Include vector potential psi in Ohm's law?")
.withDefault<bool>(true);
FiniteElMass = optsc["FiniteElMass"]
.doc("Include electron inertia in Ohm's law?")
.withDefault<bool>(true);
j_diamag = optsc["j_diamag"]
.doc("Diamagnetic current: Vort <-> Pe")
.withDefault<bool>(true);
j_par = optsc["j_par"]
.doc("Parallel current: Vort <-> Psi")
.withDefault<bool>(true);
j_pol_pi = optsc["j_pol_pi"]
.doc("Polarisation current with explicit Pi dependence")
.withDefault<bool>(true);
j_pol_simplified = optsc["j_pol_simplified"]
.doc("Polarisation current without explicit Pi dependence")
.withDefault<bool>(false);
relaxation = optsc["relaxation"]
.doc("Relaxation method for potential solvers")
.withDefault<bool>(false);
OPTION(optsc, lambda_0, 1e3);
OPTION(optsc, lambda_2, 1e5);
OPTION(optsc, parallel_flow, true);
OPTION(optsc, parallel_flow_p_term, parallel_flow);
OPTION(optsc, pe_par, true);
OPTION(optsc, pe_par_p_term, pe_par);
OPTION(optsc, resistivity, true);
OPTION(optsc, thermal_flux, true);
thermal_force = optsc["thermal_force"]
.doc("Force on electrons due to temperature gradients")
.withDefault<bool>(true);
OPTION(optsc, electron_viscosity, true);
ion_viscosity = optsc["ion_viscosity"].doc("Include ion viscosity?").withDefault<bool>(true);
ion_viscosity_par = optsc["ion_viscosity_par"].doc("Include parallel diffusion of ion momentum?").withDefault<bool>(ion_viscosity);
electron_neutral = optsc["electron_neutral"]
.doc("Include electron-neutral collisions in resistivity?")
.withDefault<bool>(true);
ion_neutral = optsc["ion_neutral"]
.doc("Include ion-neutral collisions in tau_i?")
.withDefault<bool>(false);
poloidal_flows = optsc["poloidal_flows"]
.doc("Include ExB flows in X-Y plane")
.withDefault(true);
OPTION(optsc, ion_velocity, true);
OPTION(optsc, thermal_conduction, true);
OPTION(optsc, electron_ion_transfer, true);
OPTION(optsc, neutral_friction, false);
OPTION(optsc, frecycle, 0.9);
OPTION(optsc, phi3d, false);
OPTION(optsc, ne_bndry_flux, true);
OPTION(optsc, pe_bndry_flux, true);
OPTION(optsc, vort_bndry_flux, false);
OPTION(optsc, ramp_mesh, true);
OPTION(optsc, ramp_timescale, 1e4);
OPTION(optsc, energy_source, false);
ion_neutral_rate = optsc["ion_neutral_rate"]
.doc("A fixed ion-neutral collision rate, normalised to ion cyclotron frequency.")
.withDefault(0.0);
OPTION(optsc, staggered, false);
OPTION(optsc, boussinesq, false);
OPTION(optsc, sinks, false);
OPTION(optsc, sheath_closure, true);
OPTION(optsc, drift_wave, false);
// Cross-field transport
classical_diffusion = optsc["classical_diffusion"]
.doc("Collisional cross-field diffusion, including viscosity")
.withDefault<bool>(false);
OPTION(optsc, anomalous_D, -1);
OPTION(optsc, anomalous_chi, -1);
OPTION(optsc, anomalous_nu, -1);
OPTION(optsc, anomalous_D_nvi, true);
OPTION(optsc, anomalous_D_pepi, true);
// Flux limiters
OPTION(optsc, kappa_limit_alpha, -1);
OPTION(optsc, eta_limit_alpha, -1);
// Numerical dissipation terms
OPTION(optsc, numdiff, -1.0);
OPTION(optsc, hyper, -1);
OPTION(optsc, hyperpar, -1);
OPTION(optsc, low_pass_z, -1);
OPTION(optsc, x_hyper_viscos, -1.0);
OPTION(optsc, y_hyper_viscos, -1.0);
OPTION(optsc, z_hyper_viscos, -1.0);
OPTION(optsc, scale_num_cs, 1.0);
OPTION(optsc, floor_num_cs, -1.0);
OPTION(optsc, vepsi_dissipation, false);
OPTION(optsc, vort_dissipation, false);
phi_dissipation = optsc["phi_dissipation"]
.doc("Add a dissipation term to vorticity, depending on reconstruction of potential?")
.withDefault<bool>(false);
ne_num_diff = optsc["ne_num_diff"]
.doc("Numerical Ne diffusion in X-Z plane. < 0 => off.")
.withDefault(-1.0);
ne_num_hyper = optsc["ne_num_hyper"]
.doc("Numerical Ne hyper-diffusion in X-Z plane. < 0 => off.")
.withDefault(-1.0);
vi_num_diff = optsc["vi_num_diff"]
.doc("Numerical Vi diffusion in X-Z plane. < 0 => off.")
.withDefault(-1.0);
ve_num_diff = optsc["ve_num_diff"]
.doc("Numerical Ve diffusion in X-Z plane. < 0 => off.")
.withDefault(-1.0);
ve_num_hyper = optsc["ve_num_hyper"]
.doc("Numerical Ve hyper-diffusion in X-Z plane. < 0 => off.")
.withDefault(-1.0);
OPTION(optsc, ne_hyper_z, -1.0);
OPTION(optsc, pe_hyper_z, -1.0);
OPTION(optsc, low_n_diffuse, false);
OPTION(optsc, low_n_diffuse_perp, false);
OPTION(optsc, resistivity_multiply, 1.0);
OPTION(optsc, sheath_model, 0);
OPTION(optsc, sheath_gamma_e, 5.5);
OPTION(optsc, sheath_gamma_i, 1.0);
OPTION(optsc, neutral_vwall, 1. / 3); // 1/3rd Franck-Condon energy at wall
OPTION(optsc, sheath_yup, true); // Apply sheath at yup?
OPTION(optsc, sheath_ydown, true); // Apply sheath at ydown?
OPTION(optsc, test_boundaries, false); // Test boundary conditions
OPTION(optsc, parallel_sheaths, false); // Apply parallel sheath conditions?
OPTION(optsc, par_sheath_model, 0);
OPTION(optsc, par_sheath_ve, true);
OPTION(optsc, electron_weight, 1.0);
OPTION(optsc, Div_parP_n_sheath_extra, Div_parP_n_sheath_extra);
sheath_allow_supersonic =
optsc["sheath_allow_supersonic"]
.doc("If plasma is faster than sound speed, go to plasma velocity")
.withDefault<bool>(true);
radial_buffers = optsc["radial_buffers"]
.doc("Turn on radial buffer regions?").withDefault<bool>(false);
OPTION(optsc, radial_inner_width, 4);
OPTION(optsc, radial_outer_width, 1);
OPTION(optsc, radial_buffer_D, 1.0);
OPTION(optsc, phi_smoothing, false);
OPTION(optsc, phi_sf, 0.0);
resistivity_boundary = optsc["resistivity_boundary"]
.doc("Normalised resistivity in radial boundary region")
.withDefault(1.0);
resistivity_boundary_width = optsc["resistivity_boundary_width"]
.doc("Number of grid cells in radial (x) direction")
.withDefault(0);
// Output additional information
OPTION(optsc, verbose, false); // Save additional fields
OPTION(optsc, output_ddt, false); // Save time derivatives
// Normalisation
OPTION(optsc, Tnorm, 100); // Reference temperature [eV]
OPTION(optsc, Nnorm, 1e19); // Reference density [m^-3]
OPTION(optsc, Bnorm, 1.0); // Reference magnetic field [T]
OPTION(optsc, AA, 2.0); // Ion mass (2 = Deuterium)
output.write("Normalisation Te={:e}, Ne={:e}, B={:e}\n", Tnorm, Nnorm, Bnorm);
SAVE_ONCE(Tnorm, Nnorm, Bnorm, AA); // Save
Cs0 = sqrt(qe * Tnorm / (AA * Mp)); // Reference sound speed [m/s]
Omega_ci = qe * Bnorm / (AA * Mp); // Ion cyclotron frequency [1/s]
rho_s0 = Cs0 / Omega_ci;
mi_me = AA * Mp / (electron_weight * Me);
me_mi = (electron_weight * Me) / (AA * Mp);
beta_e = qe * Tnorm * Nnorm / (SQ(Bnorm) / (2. * SI::mu0));
output.write("\tmi_me={}, beta_e={}\n", mi_me, beta_e);
SAVE_ONCE(mi_me, beta_e, me_mi);
output.write("\t Cs={:e}, rho_s={:e}, Omega_ci={:e}\n", Cs0, rho_s0, Omega_ci);
SAVE_ONCE(Cs0, rho_s0, Omega_ci);
// Collision times
BoutReal lambda_ei = 24. - log(sqrt(Nnorm / 1e6) / Tnorm);
BoutReal lambda_ii = 23. - log(sqrt(2. * Nnorm / 1e6) / pow(Tnorm, 1.5));
tau_e0 = 1. / (2.91e-6 * (Nnorm / 1e6) * lambda_ei * pow(Tnorm, -3. / 2));
tau_i0 =
sqrt(AA) / (4.78e-8 * (Nnorm / 1e6) * lambda_ii * pow(Tnorm, -3. / 2));
output.write("\ttau_e0={:e}, tau_i0={:e}\n", tau_e0, tau_i0);
if (anomalous_D > 0.0) {
// Normalise
anomalous_D /= rho_s0 * rho_s0 * Omega_ci; // m^2/s
output.write("\tnormalised anomalous D_perp = {:e}\n", anomalous_D);
a_d3d = anomalous_D;
mesh->communicate(a_d3d);
a_d3d.yup() = anomalous_D;
a_d3d.ydown() = anomalous_D;
}
if (anomalous_chi > 0.0) {
// Normalise
anomalous_chi /= rho_s0 * rho_s0 * Omega_ci; // m^2/s
output.write("\tnormalised anomalous chi_perp = {:e}\n", anomalous_chi);
a_chi3d = anomalous_chi;
mesh->communicate(a_chi3d);
a_chi3d.yup() = anomalous_D;
a_chi3d.ydown() = anomalous_D;
}
if (anomalous_nu > 0.0) {
// Normalise
anomalous_nu /= rho_s0 * rho_s0 * Omega_ci; // m^2/s
output.write("\tnormalised anomalous nu_perp = {:e}\n", anomalous_nu);
a_nu3d = anomalous_nu;
mesh->communicate(a_nu3d);
a_nu3d.yup() = anomalous_D;
a_nu3d.ydown() = anomalous_D;
}
if (ramp_mesh) {
Jpar0 = 0.0;
} else {
// Read equilibrium current density
// GRID_LOAD(Jpar0);
// Jpar0 /= qe*Nnorm*Cs0;
Jpar0 = 0.0;
}
FieldFactory fact(mesh);
if (sinks) {
std::string source = optsc["sink_invlpar"].withDefault<std::string>("0.05"); // 20 m
sink_invlpar = fact.create3D(source);
sink_invlpar *= rho_s0; // Normalise
SAVE_ONCE(sink_invlpar);
if (drift_wave) {
alpha_dw = fact.create2D("Hermes:alpha_dw");
SAVE_ONCE(alpha_dw);
}
} else {
optsc["sink_invlpar"].setConditionallyUsed();
}
// Get switches from each variable section
auto& optne = opt["Ne"];
NeSource = optne["source"].doc("Source term in ddt(Ne)").withDefault(Field3D{0.0});
NeSource /= Omega_ci;
Sn = NeSource;
// Inflowing density carries momentum
OPTION(optne, density_inflow, false);
auto& optpe = opt["Pe"];
PeSource = optpe["source"].withDefault(Field3D{0.0});
PeSource /= Omega_ci;
Spe = PeSource;
auto& optpi = opt["Pi"];
PiSource = optpi["source"].withDefault(Field3D{0.0});
PiSource /= Omega_ci;
Spi = PiSource;
OPTION(optsc, core_sources, false);
if (core_sources) {
for (int x = mesh->xstart; x <= mesh->xend; x++) {
if (!mesh->periodicY(x)) {
// Not periodic, so not in core
for (int y = mesh->ystart; y <= mesh->yend; y++) {
for (int z = 0; z <= mesh->LocalNz; z++) {
Sn(x, y, z) = 0.0;
Spe(x, y, z) = 0.0;
Spi(x, y, z) = 0.0;
}
}
}
}
}
// Mid-plane power flux q_||
// Midplane power specified in Watts per m^2
Field2D qfact;
GRID_LOAD(qfact); // Factor to multiply to get volume source
Field2D qmid = optpe["midplane_power"].withDefault(Field2D{0.0}) * qfact;
// Normalise from W/m^3
qmid /= qe * Tnorm * Nnorm * Omega_ci;
Spe += (2. / 3) * qmid;
// Add variables to solver
SOLVE_FOR(Ne);
EvolvingVars.add(Ne);
if (output_ddt) {
SAVE_REPEAT(ddt(Ne));
}
// Evolving n_i instead of n_e
evolve_ni = optsc["evolve_ni"].doc("Evolve ion density instead?")
.withDefault<bool>(true);
// Temperature evolution can be turned off
// so that Pe = Ne and/or Pi = Ne
evolve_te = optsc["evolve_te"].doc("Evolve electron temperature?")
.withDefault<bool>(true);
if (evolve_te) {
SOLVE_FOR(Pe);
EvolvingVars.add(Pe);
if (output_ddt) {
SAVE_REPEAT(ddt(Pe));
}
} else {
Pe = Ne;
}
evolve_ti = optsc["evolve_ti"].doc("Evolve ion temperature?")
.withDefault<bool>(true);
if (evolve_ti) {
SOLVE_FOR(Pi);
EvolvingVars.add(Pi);
if (output_ddt) {
SAVE_REPEAT(ddt(Pi));
}
} else {
Pi = Ne;
}
fall_off_Ne = optsc["fall_off_ne"].doc("outer radial fall off length BC for density [m]")
.withDefault<BoutReal>(-1);
fall_off_Pe = optsc["fall_off_pe"].doc("outer radial fall off length BC for electron pressure [m]")
.withDefault<BoutReal>(-1);
fall_off_Pi = optsc["fall_off_pi"].doc("outer radial fall off length BC for ion pressure [m]")
.withDefault<BoutReal>(-1);
fall_off = fall_off_Ne > 0 || fall_off_Pe > 0 || fall_off_Pi > 0;
if (fall_off) {
xdist = BoutNaN;
Field3D R, Z;
mesh->get(R, "R");
mesh->get(Z, "Z");
const int x0 = mesh->xend;
for (int y = mesh->ystart; y <= mesh->yend; ++y) {
for (int z = mesh->zstart; z <= mesh->zend; ++z) {
for (int x = mesh->xend + 1; x < mesh->LocalNx; ++x) {
xdist(x, y, z) =
sqrt(SQ(R(x0, y, z) - R(x, y, z)) + SQ(Z(x0, y, z) - Z(x, y, z)));
// printf("%d %d %d %e\n", x,y,z,xdist(x,y,z));
}
}
}
}
evolve_vort = optsc["evolve_vort"].doc("Evolve Vorticity?")
.withDefault<bool>(true);
if (relaxation) {
SOLVE_FOR(phi_1);
EvolvingVars.add(phi_1);
if (output_ddt) {
SAVE_REPEAT(ddt(phi_1));
}
}
if ((j_par || j_diamag || relaxation) && evolve_vort) {
// Have a source of vorticity
solver->add(Vort, "Vort");
EvolvingVars.add(Vort);
if (output_ddt) {
SAVE_REPEAT(ddt(Vort));
}
} else {
zero_all(Vort);
}
if (electromagnetic || FiniteElMass) {
solver->add(VePsi, "VePsi");
EvolvingVars.add(VePsi);
if (output_ddt) {
SAVE_REPEAT(ddt(VePsi));
}
} else {
// If both electrostatic and zero electron mass,
// then Ohm's law has no time-derivative terms,
// but is calculated from other evolving quantities
zero_all(VePsi);
}
if (ion_velocity) {
solver->add(NVi, "NVi");
EvolvingVars.add(NVi);
if (output_ddt) {
SAVE_REPEAT(ddt(NVi));
}
} else {
zero_all(NVi);
}
if (verbose) {
SAVE_REPEAT(Ti);
if (electron_ion_transfer && evolve_ti) {
SAVE_REPEAT(Wi);
}
if (ion_velocity) {
SAVE_REPEAT(Vi);
}
}
OPTION(optsc, adapt_source, false);
if (adapt_source) {
// Adaptive sources to match profiles
// PI controller, including an integrated difference term
OPTION(optsc, source_p, 1e-2);
OPTION(optsc, source_i, 1e-6);
Coordinates::FieldMetric Snsave = copy(Sn);
Coordinates::FieldMetric Spesave = copy(Spe);
Coordinates::FieldMetric Spisave = copy(Spi);
SOLVE_FOR(Sn, Spe, Spi);
Sn = Snsave;
Spe = Spesave;
Spi = Spisave;
} else {
SAVE_ONCE(Sn, Spe, Spi);
}
/////////////////////////////////////////////////////////
// Load metric tensor from the mesh, passing length and B
// field normalisations
Coordinates *coord = mesh->getCoordinates();
// To use non-orthogonal metric
// Normalise
coord->Bxy /= Bnorm;
// Metric is in grid file - just need to normalise
// coord->dx /= rho_s0;
// coord->dy /= rho_s0;
// coord->dz /= rho_s0;
// CONTRAVARIANT
mul_all_inp(coord->g11, rho_s0 * rho_s0);
mul_all_inp(coord->g22, rho_s0 * rho_s0);
mul_all_inp(coord->g33, rho_s0 * rho_s0);
mul_all_inp(coord->g12, rho_s0 * rho_s0);
mul_all_inp(coord->g13, rho_s0 * rho_s0);
mul_all_inp(coord->g23, rho_s0 * rho_s0);
// Jacobi matrix
div_all_inp(coord->J, rho_s0 * rho_s0 * rho_s0);
// LIKE IN D'haeseleer
// subscripts = ()_i -> covariant
// superscripts = ()^j -> contravariant
// COVARIANT
div_all_inp(coord->g_11, rho_s0 * rho_s0);
div_all_inp(coord->g_22, rho_s0 * rho_s0); // In m^2
div_all_inp(coord->g_33, rho_s0 * rho_s0);
div_all_inp(coord->g_12, rho_s0 * rho_s0);
div_all_inp(coord->g_13, rho_s0 * rho_s0);
div_all_inp(coord->g_23, rho_s0 * rho_s0);
coord->geometry(); // Calculate other metrics
_FCIDiv_a_Grad_perp = std::make_unique<FCI::dagp_fv>(*mesh);
*_FCIDiv_a_Grad_perp *= rho_s0;
if (Options::root()["mesh:paralleltransform"]["type"].as<std::string>() == "fci") {
fci_transform = true;
}else{
fci_transform = false;
}
ASSERT0(fci_transform);
if(fci_transform){
poloidal_flows = false;
mesh->get(Bxyz, "B",1.0);
mesh->get(coord->Bxy, "Bxy", 1.0);
Bxyz /= Bnorm;
coord->Bxy /= Bnorm;
// mesh->communicate(Bxyz, coord->Bxy); // To get yup/ydown fields
// Note: A Neumann condition simplifies boundary conditions on fluxes
// where the condition e.g. on J should be on flux (J/B)
auto logBxy = log(coord->Bxy);
auto logBxyz = log(Bxyz);
logBxy.applyBoundary("neumann");
logBxyz.applyBoundary("neumann");
mesh->communicate(logBxy, logBxyz);
logBxy.applyParallelBoundary(parbc);
logBxyz.applyParallelBoundary(parbc);
output_info.write("Setting from log");
coord->Bxy = exp_all(logBxy);
Bxyz = exp_all(logBxyz);
SAVE_ONCE(Bxyz);
ASSERT1(min(Bxyz) > 0.0);
fwd_bndry_mask = BoutMask(mesh, false);
bwd_bndry_mask = BoutMask(mesh, false);
for (const auto &bndry_par : mesh->getBoundariesPar(BoundaryParType::fwd)) {
for (bndry_par->first(); !bndry_par->isDone(); bndry_par->next()) {
fwd_bndry_mask[bndry_par->ind()] = true;