forked from WarwickDumas/FocusFusion2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.cpp
2392 lines (2067 loc) · 79.2 KB
/
simulation.cpp
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
#include "mesh.h"
#include "globals.h"
#include "headers.h"
#include "FFxtubes.h"
#include "cppconst.h"
real GlobalIzElasticity;
bool inline in_domain(Vector2 u)
{
return (u.x*u.x+u.y*u.y > DEVICE_RADIUS_INSULATOR_OUTER*DEVICE_RADIUS_INSULATOR_OUTER);
}
void TriMesh::Advance(TriMesh * pDestMesh)
{
printf("CreateMeshDisplacement_zero_future_pressure();\n");
CreateMeshDisplacement_zero_future_pressure(); // creates default displacement
// and sets up nT = pTri->temp_f64
// For now, in a cheap and dirty way.
printf("SolveForAdvectedPositions(pDestMesh);\n");
SolveForAdvectedPositions(pDestMesh);
// creates data on dest mesh inc. comp htg
// Idea for a sequence:
// 1. Advect to new vertex positions
// 2. Copy over triangle lists.
// . (De-tangle overlaps... how to then act?)
// . Transfer data and apply compressive heating
// (requires a call to pDestMesh->RecalculateVertexCellAreas()
// how does that play into wrapping over PBC?)
// it works as long as we are either unwrapped, or wrapped with updated periodic flag.
// . Initially, pTriDest->periodic = pTri->periodic
// . Wrap around PBC. (Rotate Nv if already placed. pTri->DecrementPeriodic)
// That is to be all included in the above routine.
// . Interpolate to get A : AFTER wrap.
// It seems more natural to interpolate within domain only.
// Small chance: we might be outside tranche and not be within a tri that is considered periodic
// if we do it unwrapped?
printf("pDestMesh->InterpolateA And Adot From(this);\n");
pDestMesh->InterpolateAFrom(this); // hmm
printf("pDestMesh->Redelaunerize(true,true);\n");
pDestMesh->Redelaunerize(true, // 'to exhaustion'
true // try to replace fluids
);
printf("pDestMesh->GetBFromA();\n");
pDestMesh->GetBFromA();
// Now we have to replace data:
// For each flip, we have to decide what amount has transferred between vertices.
// Stage II:
printf("pDestMesh->Set_nT_and_Get_Pressure(SPECIES_ION);\n");
pDestMesh->Set_nT_and_Get_Pressure(SPECIES_ION);
pDestMesh->Set_nT_and_Get_Pressure(SPECIES_ELEC);
pDestMesh->Set_nT_and_Get_Pressure(SPECIES_NEUT);
printf("pDestMesh->ComputeOhmsLaw();\n");
pDestMesh->ComputeOhmsLaw();
printf("pDestMesh->Solve_A_phi(false);\n");
printf("any key: \n"); getch();
pDestMesh->Solve_A_phi(false); // not initial solve
printf("done.stop.\n");
getch();
// Stage III:
//pDestMesh->Evolve();
// species relative advection,
// heat and momentum diffusion,
// ionisation and heating.
// ===
// We need to start from saying what planes apply on each shard.
// Should we do it as we go?
// Is there any advantage to doing otherwise?
// In 2D we should avoid cycle of flips.
// In a flip, 2 vertex-centered cells are strictly growing and
// 2 are strictly decreasing.
// Therefore we can
// . attribute from the giving cells to the taking cells
// . conclude the totals for both by adding / subtracting from original.
//
// The taking cells are the ones that are originally opposing.
// ie they are the unshared vertices of the tris that will flip.
// The shared vertices are going to lose mass, and the shards that are
// concerned are the 3 ones involving one of the tri centroids of the flip tris.
// 2. B. 3 shards, we wish to apportion into overlap with 3 known polygons,
// for each of 2 sides; and integrate planes in each case.
// 2. C. Subtract to see what's left over in the losing vertcells; add to give total in gaining vertcells.
// Be careful about flips of ins-crossing triangles!!
// (Consider afterwards)
// ---------------------
// ______________________________________________________________________________
// Try storing old-time pressure at vertices (the prev mesh ones?). Then we can
// assume the transition to newer pressure takes place
// gradually, when we are doing the evolution.
// Have to be careful: the old cell region was different before Delaunay flip.
}
real inline TriMesh::GetIzPrescribed(real const t)
{
real Iz = -PEAKCURRENT_STATCOULOMB * sin ((t + ZCURRENTBASETIME) * PIOVERPEAKTIME );
printf("\nGetIzPrescribed called with t+ZCURRENTBASETIME = %1.5E : %1.5E\n", t + ZCURRENTBASETIME, Iz);
getch();
return Iz;
}
//
//
//if (bScrewPinch) {
// return IZ_SCREW_PINCH;
// } else {
// real Iz = -PEAKCURRENT_STATCOULOMB * sin ((t + ZCURRENTBASETIME) * PIOVERPEAKTIME );
// return Iz;
// };
real inline Get_lnLambda(real n_e,real T_e)
{
real lnLambda, factor, lnLambda_sq, lnLambda1, lnLambda2;
static real const one_over_kB = 1.0/kB;
real Te_eV = T_e*one_over_kB;
real Te_eV2 = Te_eV*Te_eV;
real Te_eV3 = Te_eV*Te_eV2;
if (n_e*Te_eV3 > 0.0) {
lnLambda1 = 23.0 - 0.5*log(n_e/Te_eV3);
lnLambda2 = 24.0 - 0.5*log(n_e/Te_eV2);
// smooth between the two:
factor = 2.0*fabs(Te_eV-10.0)*(Te_eV-10.0)/(1.0+4.0*(Te_eV-10.0)*(Te_eV-10.0));
lnLambda = lnLambda1*(0.5-factor)+lnLambda2*(0.5+factor);
// floor at 2 just in case, but it should not get near:
lnLambda_sq = lnLambda*lnLambda;
factor = 1.0+0.5*lnLambda+0.25*lnLambda_sq+0.125*lnLambda*lnLambda_sq + 0.0625*lnLambda_sq*lnLambda_sq;
lnLambda += 2.0/factor;
// Golant p.40 warns that it becomes invalid when an electron gyroradius is less than a Debye radius. That is something to worry about if B/400 > n^1/2 , so looks not a big concern.
// There is also a quantum ceiling. It will not be anywhere near. At n=1e20, 0.5eV, the ceiling is only down to 29; it requires cold dense conditions to apply.
} else {
lnLambda = 20.0;
};
//if (GlobalDebugRecordIndicator)
// Globaldebugdata.lnLambda = lnLambda;
return lnLambda;
}
real inline Get_lnLambda_ion(real n_ion,real T_ion)
{
static real const one_over_kB = 1.0/kB; // multiply by this to convert to eV
static real const one_over_kB_cubed = 1.0/(kB*kB*kB); // multiply by this to convert to eV
real factor, lnLambda_sq;
real Tion_eV3 = T_ion*T_ion*T_ion*one_over_kB_cubed;
real lnLambda = 23.0 - 0.5*log(n_ion/Tion_eV3);
// floor at 2:
lnLambda_sq = lnLambda*lnLambda;
factor = 1.0+0.5*lnLambda+0.25*lnLambda_sq+0.125*lnLambda*lnLambda_sq + 0.0625*lnLambda_sq*lnLambda_sq;
lnLambda += 2.0/factor;
return lnLambda;
}
real Estimate_Neutral_Neutral_Viscosity_Cross_section(real T) // call with T in electronVolts
{
if (T > cross_T_vals[9]) return cross_s_vals_viscosity_nn[9];
if (T < cross_T_vals[0]) return cross_s_vals_viscosity_nn[0];
int i = 1;
while (T > cross_T_vals[i]) i++;
// T lies between i-1,i
real ppn = (T-cross_T_vals[i-1])/(cross_T_vals[i]-cross_T_vals[i-1]);
return ppn*cross_s_vals_viscosity_nn[i] + (1.0-ppn)*cross_s_vals_viscosity_nn[i-1];
}
void Estimate_Ion_Neutral_Cross_sections(real T, // call with T in electronVolts
real * p_sigma_in_MT,
real * p_sigma_in_visc)
{
if (T > cross_T_vals[9]) {
*p_sigma_in_MT = cross_s_vals_momtrans_ni[9];
*p_sigma_in_visc = cross_s_vals_viscosity_ni[9];
return;
}
if (T < cross_T_vals[0]){
*p_sigma_in_MT = cross_s_vals_momtrans_ni[0];
*p_sigma_in_visc = cross_s_vals_viscosity_ni[0];
return;
}
int i = 1;
while (T > cross_T_vals[i]) i++;
// T lies between i-1,i
real ppn = (T-cross_T_vals[i-1])/(cross_T_vals[i]-cross_T_vals[i-1]);
*p_sigma_in_MT = ppn*cross_s_vals_momtrans_ni[i] + (1.0-ppn)*cross_s_vals_momtrans_ni[i-1];
*p_sigma_in_visc = ppn*cross_s_vals_viscosity_ni[i] + (1.0-ppn)*cross_s_vals_viscosity_ni[i-1];
return;
}
real Estimate_Ion_Neutral_MomentumTransfer_Cross_section(real T) // call with T in electronVolts
{
if (T > cross_T_vals[9]) return cross_s_vals_momtrans_ni[9];
if (T < cross_T_vals[0]) return cross_s_vals_momtrans_ni[0];
int i = 1;
while (T > cross_T_vals[i]) i++;
// T lies between i-1,i
real ppn = (T-cross_T_vals[i-1])/(cross_T_vals[i]-cross_T_vals[i-1]);
return ppn*cross_s_vals_momtrans_ni[i] + (1.0-ppn)*cross_s_vals_momtrans_ni[i-1];
}
void TriMesh::InterpolateAFrom(TriMesh * pSrcMesh)
{
// We can readily do interpolation in triangles.
// A values live on vertices so we just make planes with 3 of them.
long iVertex;
Vertex * pVertex, * pVertSrc;
real beta[3];
Triangle * pTri;
Vector2 u[3];
long iWhich, iTri;
pVertex = X;
pVertSrc = pSrcMesh->X;
for (iVertex = 0; iVertex < numVertices; iVertex++)
{
if ((pVertex->flags == INNERMOST) ||
(pVertex->flags == OUTERMOST))
{
pVertex->A = pVertSrc->A;
pVertex->Adot = pVertSrc->Adot;
} else {
Triangle * pSeedTri = pSrcMesh->T + pVertSrc->GiveMeAnIndex();
if ((pSeedTri->u8domain_flag == INNER_FRILL) ||
(pSeedTri->u8domain_flag == OUTER_FRILL))
{
pSeedTri = pSeedTri->neighbours[0];
}
pTri = pSrcMesh->ReturnPointerToTriangleContainingPoint(
pSeedTri,
pVertex->pos.x,pVertex->pos.y
);
// presumably this works best when pos is actually
// inside the tranche. So we call InterpolateA for wrapped mesh.
if (pTri->periodic == 0) {
GetInterpolationCoefficients(beta,
pVertex->pos.x, pVertex->pos.y,
pTri->cornerptr[0]->pos,
pTri->cornerptr[1]->pos,
pTri->cornerptr[2]->pos);
pVertex->A = beta[0]*pTri->cornerptr[0]->A
+ beta[1]*pTri->cornerptr[1]->A
+ beta[2]*pTri->cornerptr[2]->A;
pVertex->Adot = beta[0]*pTri->cornerptr[0]->Adot
+ beta[1]*pTri->cornerptr[1]->Adot
+ beta[2]*pTri->cornerptr[2]->Adot;
} else {
// We apply this routine to WRAPPED MESH.
// So if it inhabits a periodic src tri,
// we can go by its own x-position to tell which side for A.
pTri->MapLeftIfNecessary(u[0],u[1],u[2]);
int par[3];
pTri->GetParity(par);
memset(&(pVertex->A),0,sizeof(Vector3));
memset(&(pVertex->Adot),0,sizeof(Vector3));
if (pVertex->pos.x > 0.0) {
GetInterpolationCoefficients(beta,
pVertex->pos.x, pVertex->pos.y,
Clockwise*u[0],
Clockwise*u[1],
Clockwise*u[2]);
// equivalently we could just rotate anticlockwise our pos to get
// at beta.
if (par[0] == 0){
pVertex->A += beta[0]*(Clockwise3*pTri->cornerptr[0]->A);
pVertex->Adot += beta[0]*(Clockwise3*pTri->cornerptr[0]->Adot);
} else {
pVertex->A += beta[0]*(pTri->cornerptr[0]->A);
pVertex->Adot += beta[0]*(pTri->cornerptr[0]->Adot);
};
if (par[1] == 0) {
pVertex->A += beta[1]*(Clockwise3*pTri->cornerptr[1]->A);
pVertex->Adot += beta[1]*(Clockwise3*pTri->cornerptr[1]->Adot);
} else {
pVertex->A += beta[1]*(pTri->cornerptr[1]->A);
pVertex->Adot += beta[1]*(pTri->cornerptr[1]->Adot);
};
if (par[2] == 0) {
pVertex->A += beta[2]*(Clockwise3*pTri->cornerptr[2]->A);
pVertex->Adot += beta[2]*(Clockwise3*pTri->cornerptr[2]->Adot);
} else {
pVertex->A += beta[2]*pTri->cornerptr[2]->A;
pVertex->Adot += beta[2]*pTri->cornerptr[2]->Adot;
};
} else {
GetInterpolationCoefficients(beta,
pVertex->pos.x, pVertex->pos.y,
u[0],u[1],u[2]);
if (par[0] == 0){
pVertex->A += beta[0]*pTri->cornerptr[0]->A;
pVertex->Adot += beta[0]*pTri->cornerptr[0]->Adot;
} else {
pVertex->A += beta[0]*(Anticlockwise3*pTri->cornerptr[0]->A);
pVertex->Adot += beta[0]*(Anticlockwise3*pTri->cornerptr[0]->Adot);
};
if (par[1] == 0) {
pVertex->A += beta[1]*pTri->cornerptr[1]->A;
pVertex->Adot += beta[1]*pTri->cornerptr[1]->Adot;
} else {
pVertex->A += beta[1]*(Anticlockwise3*pTri->cornerptr[1]->A);
pVertex->Adot += beta[1]*(Anticlockwise3*pTri->cornerptr[1]->Adot);
};
if (par[2] == 0) {
pVertex->A += beta[2]*pTri->cornerptr[2]->A;
pVertex->Adot += beta[2]*pTri->cornerptr[2]->Adot;
} else {
pVertex->A += beta[2]*(Anticlockwise3*pTri->cornerptr[2]->A);
pVertex->Adot += beta[2]*(Anticlockwise3*pTri->cornerptr[2]->Adot);
};
};
};
};
++pVertex;
++pVertSrc;
};
}
void TriMesh::GetBFromA()
{
// This routine will set B on vertices based on A on vertices.
// To set B at an edge we can use the quadrilateral of A values
// near that edge. That is not what we are about to do, but we
// could make a subroutine to get curl of A around a ConvexPolygon.
// And that seems very wise.
ConvexPolygon cp;
Triangle * pTri;
Vertex * pVertex;
long iVertex, iTri;
Vector2 u;
int i;
long izTri[128];
Vector3 A[128];
long tri_len;
// Reset triangle centroids:
pTri = T;
for (iTri = 0; iTri < numTriangles; iTri++)
{
pTri->RecalculateCentroid(this->InnermostFrillCentroidRadius,
this->OutermostFrillCentroidRadius);
++pTri;
};
// For each vertex create a ConvexPolygon of centroids
// and a list of A-values:
pVertex = X;
for (iVertex = 0; iVertex < numVertices; iVertex++)
{
tri_len = pVertex->GetTriIndexArray(izTri);
cp.Clear();
// Special allowance for outer edge is only to be that
// we add on this point itself as the last one.
for (i = 0; i < tri_len; i++)
{
pTri = T + izTri[i];
cp.add(pTri->GetContiguousCent_AssumingCentroidsSet(pVertex));
A[i] = pTri->GetAAvg(); // Note this assumes the data is in pVertex->A
if ((pTri->periodic) && (pVertex->pos.x > 0.0))
A[i] = Clockwise3*A[i];
};
if ((pVertex->flags == CONCAVE_EDGE_VERTEX) ||
(pVertex->flags == CONVEX_EDGE_VERTEX) )
{
cp.add(pVertex->pos);
A[i] = pVertex->A;
};
// Estimate the average by integration of curl A :
pVertex->B = cp.Get_curl2D_from_anticlockwise_array(A);
++pVertex;
};
}
void TriMesh::GetGradTeOnVertices()
{
long iTri;
Triangle * pTri;
real beta[3];
// Assign T to triangles: what is best fit?
// To do it properly:
// Do minmod then match up where they do not match.
pTri = T;
for (iTri = 0; iTri < numTriangles; iTri++)
{
if (pTri->u8domain_flag == DOMAIN_TRIANGLE) {
GetInterpolationCoefficients(beta, pTri->cent.x,pTri->cent.y,
pTri->cornerptr[0]->centroid,
pTri->cornerptr[1]->centroid,
pTri->cornerptr[2]->centroid);
pTri->temp_f64 = beta[0]*(pTri->cornerptr[0]->Elec.heat/pTri->cornerptr[0]->Elec.mass)
+ beta[1]*(pTri->cornerptr[1]->Elec.heat/pTri->cornerptr[1]->Elec.mass)
+ beta[2]*(pTri->cornerptr[2]->Elec.heat/pTri->cornerptr[2]->Elec.mass);
} else {
pTri->temp_f64 = 0.0;
// the value will not be used.
};
++pTri;
};
// On GPU how to do Grad Te: each tri collects info from 3 places, =>
// 3 x random access;
// contributes to Grad Te at 3 corners. Can have 1 thread per 1 tri.
// Same with vertex A -> vertex B.
ConvexPolygon cp;
long tri_len, i, iVertex;
long izTri[128];
real Te[128];
Vertex * pVertex = X;
bool bDone = false;
for (iVertex= 0; iVertex < numVertices; iVertex++)
{
if ((pVertex->flags == DOMAIN_VERTEX) ||
(pVertex->flags == CONVEX_EDGE_VERTEX))
{
tri_len = pVertex->GetTriIndexArray(izTri);
// Get centroid polygon:
cp.Clear();
for (i = 0; i < tri_len; i++)
{
pTri = T + izTri[i];
if (pTri->u8domain_flag == DOMAIN_TRIANGLE) {
cp.add(pTri->GetContiguousCent_AssumingCentroidsSet(pVertex));
Te[i] = pTri->temp_f64; // Note this assumes the data is in pVertex->A
} else {
// Insulator tri: do not include, but instead put in the centre point:
if (bDone == false)
{
bDone = true;
cp.add(pVertex->centroid);
Te[i] = pVertex->Elec.heat/pVertex->Elec.mass;
};
};
};
// As in code for A->B :
// Special allowance for outer edge is only to be that
// we add on this point itself as the last one.
if (pVertex->flags == CONVEX_EDGE_VERTEX)
{
cp.add(pVertex->pos);
Te[i] = pVertex->Elec.heat/pVertex->Elec.mass;
};
// But remember heat lives in the whole "house-shaped" cell.
// We could choose otherwise but let's not.
// So, AreaCell routine contains house shape?
pVertex->GradTe = cp.Get_grad_from_anticlockwise_array(Te);
} else {
pVertex->GradTe.x = 0.0; pVertex->GradTe.y = 0.0;
};
++pVertex;
}
}
//
//class CalculateAccelsClass
//{
//public:
// // exists only to do a calculation repeatedly from some stored data
//
// Vector3 omega_ce, omega_ci;
// Tensor3 omega_ci_cross;
//
// real nu_eiBar, nu_eHeart, nu_ieBar,
// nu_en_MT, nu_in_MT,
// nu_ne_MT, nu_ni_MT,
// n_i, n_n, n_e;
//
// real heat_transfer_rate_in,heat_transfer_rate_ni,
// heat_transfer_rate_en,heat_transfer_rate_ne,
// heat_transfer_rate_ei,heat_transfer_rate_ie;
//
// Vector3 a_neut_pressure,
// a_ion_pressure,
// ROC_v_ion_due_to_Rie,
// ROC_v_ion_thermal_force;
//
// Tensor3 Upsilon_nu_eHeart;
// Tensor3 Rie_thermal_force_matrix;
// Tensor3 Rie_friction_force_matrix;
// Tensor3 Ratio_times_Upsilon_eHeart;
//
// real fric_dTe_by_dt_ei;
//
// real StoredEz;
//
// bool bNeutrals;
//
// // EASIER WAY:
// // Let's just stick simple Ohm's Law v_e(v_i,v_n)
// // in CalculateCoefficients.
//
// real SimpleOhms_vez0, SimpleOhms_beta_neutz, Ohms_vez0, Ohms_sigma;
// Vector3 SimpleOhms_beta_ion;
//
// CalculateAccelsClass(){};
//
// void CalculateCoefficients(Vertex * pVertex)
// {
// // NOTE: Uses GradTe so it better exist.
//
// static Tensor3 const ID3x3 (1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0);
// static real const TWOTHIRDSqsq = 2.0*q*q/3.0;
// static real const one_over_kB = 1.0/kB; // multiply by this to convert to eV
// static real const one_over_kB_cubed = 1.0/(kB*kB*kB); // multiply by this to convert to eV
// static real const kB_to_3halves = sqrt(kB)*kB;
// static real const over_sqrt_m_ion = 1.0/sqrt(m_ion);
// static real const over_sqrt_m_e = 1.0/sqrt(m_e);
// static real const qoverMc = q/(m_ion*c);
// static real const qovermc = q/(m_e*c);
// static real const NU_EI_FACTOR = 1.0/(3.44e5);
// static real const nu_eiBarconst = //(4.0/3.0)*sqrt(2.0*PI/m_e)*q*q*q*q;
// // don't know in what units but it IS exactly what we already had - see Formulary
// 1.0/(3.44e5);
//
// real area, det;
// real T_ion, T_n, T_e, sqrt_Te, ionneut_thermal, electron_thermal,
// lnLambda, s_in_MT, s_in_visc, s_en_MT,s_en_visc,
// nu_en_visc;
// //Vector3 const E, Vector3 const vrel_e, real * const scratch
// // The first thing we need to do is collect
//
// // nu_eibar, nu_in, nu_en
// // ======================
//
// // Get nu_eiBar
// // Get nu_en, nu_in, nu_ni, nu_ne, nu_eHeart
//
// StoredEz = pVertex->E.z;
//
// area = pVertex->area;
// n_i = pVertex->Ion.mass/area;
// n_e = pVertex->Elec.mass/area;
// n_n = pVertex->Neut.mass/area;
//
// if (pVertex->Ion.mass > 0.0) {
// T_Ion = pVertex->Ion.heat/pVertex->Ion.mass;
// } else {
// T_Ion = 0.0;
// };
// if (pVertex->Neut.mass > 0.0) {
// T_n = pVertex->Neut.heat/pVertex->Neut.mass;
// } else {
// T_n = 0.0;
// };
// if (pVertex->Elec.mass > 0.0) {
// T_e = pVertex->Elec.heat/pVertex->Elec.mass;
// sqrt_Te = sqrt(T_e);
// } else {
// T_e = 0.0;
// sqrt_Te = 0.0;
// };
//
// ionneut_thermal = sqrt(T_ion/m_ion+T_n/m_n); // hopefully not sqrt(0)
// electron_thermal = sqrt_Te*over_sqrt_m_e; // possibly == 0
//
// lnLambda = Get_lnLambda(n_i,T_e); // anything strange in there?
//
// Estimate_Ion_Neutral_Cross_sections(T_ion*one_over_kB, &s_in_MT, &s_in_visc);
// Estimate_Ion_Neutral_Cross_sections(T_e*one_over_kB,&s_en_MT,&s_en_visc);
// // To use combined temperature looks to be more intelligent -- rel temp GZSB(6.55) for ion, neutral at least.
//
// if (T_e != 0.0) {
// nu_eiBar = nu_eiBarconst*kB_to_3halves*n_i*lnLambda/(T_e*sqrt_Te);
// } else {
// nu_eiBar = 0.0;
// };
//
// nu_ieBar = nu_eiBar; // always same when n_e=n_i
//
// nu_en_MT = n_n*s_en_MT*electron_thermal;
// nu_in_MT = n_n*s_in_MT*ionneut_thermal;
// nu_ne_MT = n_e*s_en_MT*electron_thermal;
// nu_ni_MT = n_i*s_in_MT*ionneut_thermal;
//
// nu_en_visc = n_n*s_en_visc*electron_thermal;
//
// // those should all be fine though may == 0
//
// nu_eHeart = 1.87*nu_eiBar + nu_en_visc; // note, used visc
//
// heat_transfer_rate_in = (2.0*m_i*m_n/((m_i+m_n)*(m_i+m_n)))
// *nu_in_MT; // ratio nu_in/nu_ni = n_n/n_i
// heat_transfer_rate_ni = (2.0*m_i*m_n/((m_i+m_n)*(m_i+m_n)))
// *nu_ni_MT;
// heat_transfer_rate_ne = (2.0*m_e*m_n/((m_e+m_n)*(m_e+m_n)))
// *nu_ne_MT;
// heat_transfer_rate_en = (2.0*m_e*m_n/((m_e+m_n)*(m_e+m_n)))
// *nu_en_MT;
// heat_transfer_rate_ei = (2.0*m_e*m_i/((m_e+m_i)*(m_e+m_i)))
// *nu_eiBar;
// heat_transfer_rate_ie = (2.0*m_e*m_i/((m_e+m_i)*(m_e+m_i)))
// *nu_ieBar;
//
// // OK that bit is clear and as expected.
// // So what is the difference when we transfer NT ?
//
// // (n_n/n_i) transfer_rate_ni = transfer_rate_in
//
// omega_ce = qovermc*pTri->B;
// omega_ci = qoverMc*pTri->B; // note: if ion acceleration stage, we could if we wanted work out B at k+1 first.
// omega_ci_cross.MakeCross(omega_ci);
//
// // Populate Upsilon(nu_eHeart):
// real nu = nu_eHeart;
// Vector3 omega = omega_ce;
// det = nu*nu + omega.dot(omega);
//
// // (nu - omega x ) ^-1 :
// Upsilon_nu_eHeart.xx = nu*nu+omega.x*omega.x;
// Upsilon_nu_eHeart.xy = -nu*omega.z + omega.x*omega.y;
// Upsilon_nu_eHeart.xz = nu*omega.y + omega.x*omega.z;
// Upsilon_nu_eHeart.yx = nu*omega.z + omega.x*omega.y;
// Upsilon_nu_eHeart.yy = nu*nu + omega.y*omega.y;
// Upsilon_nu_eHeart.yz = -nu*omega.x + omega.y*omega.z;
// Upsilon_nu_eHeart.zx = -nu*omega.y + omega.z*omega.x;
// Upsilon_nu_eHeart.zy = nu*omega.x + omega.y*omega.z;
// Upsilon_nu_eHeart.zz = nu*nu + omega.z*omega.z;
//
// Upsilon_nu_eHeart = Upsilon_nu_eHeart/det;
//
// if (nu_eHeart > 0.0) {
// Ratio_times_Upsilon_eHeart = (nu_eiBar/nu_eHeart)*Upsilon_nu_eHeart;
// } else {
// ZeroMemory(&Ratio_times_Upsilon_eHeart,sizeof(Tensor3));
// };
//
// Rie_friction_force_matrix =
// nu_ieBar*(m_e/m_i)*(ID3x3-0.9*Ratio_times_Upsilon_eHeart);
// // multiply by (v_e-v_i) for ions
//
// Rie_thermal_force_matrix =
// ((1.5/m_i)*(nu_ieBar/nu_eHeart)*Upsilon_nu_eHeart);
// // We multiply by +GradTe for ions
//
// ZeroMemory(&vrel_e,sizeof(Vector3));
// if (pVertex->Elec.mass > 0.0)
// vrel_e = pVertex->Elec.mom/pVertex->Elec.mass
// - pVertex->Ion.mom/pVertex->Ion.mass;
//// !!!!!!!!!!!!!!!!!! Note bene.
//
// ROC_v_ion_thermal_force = Rie_thermal_force_matrix * Make3(pVertex->GradTe,0.0)
//
// ROC_v_ion_due_to_Rie = ROC_v_ion_thermal_force
// + R_ie_friction_force_matrix*vrel_e;
//
// // ===
//
// if (pTri->ion.mass != 0.0) {
//
// a_ion_pressure.x = pTri->dNv_pressure.ion.x/pTri->ion.mass;
// a_ion_pressure.y = pTri->dNv_pressure.ion.y/pTri->ion.mass;
// a_ion_pressure.z = 0.0;
//
// Vector3 a_elec_pressure;
//
// a_elec_pressure.x = pTri->dNv_pressure.elec.x/pTri->elec.mass;
// a_elec_pressure.y = pTri->dNv_pressure.elec.y/pTri->elec.mass;
// a_elec_pressure.z = 0.0;
//
// // MELD THE TWO ACCELS
// a_ion_pressure = (m_ion*a_ion_pressure + m_e*a_elec_pressure)/(m_ion + m_e);
//
// // ^ pTri->E will no longer exist.
// // It's pTri->Ez only, which will be from Az, chEz_ext.
//
// } else {
// //ZeroMemory(&a_ion_pressure_and_E_accel, sizeof(Vector3));
// ZeroMemory(&a_ion_pressure, sizeof(Vector3));
// };
//
// if (pTri->neut.mass > 0.0) {
// a_neut_pressure.x = pTri->dNv_pressure.neut.x/pTri->neut.mass;
// a_neut_pressure.y = pTri->dNv_pressure.neut.y/pTri->neut.mass;
// a_neut_pressure.z = 0.0;
// } else {
// ZeroMemory(&a_neut_pressure, sizeof(Vector3));
// };
// // All of these things, do not change, because we do not change E, vrel, pressure.
////
////
//// fric_dTe_by_dt_ei = 0.0;
//// // The fact is that if x-y current is dropped, you get just a scalar Ohm's Law.
////
//// // This illustrates why we need to go back to having current.
////
//// // Roll on the next version...
////
//// real chi = (m_n/(m_e+m_n))*this->nu_en_MT
//// + (1.0-0.9*this->Ratio_times_Upsilon_eHeart.zz)*this->nu_eiBar;
////
//// SimpleOhms_vez0 = (-qoverm*StoredEz
//// -(1.5/m_e)*((this->Ratio_times_Upsilon_eHeart*pTri->GradTe).z)
//// )/chi;
////
//// Ohms_vez0 = (
//// -(1.5/m_e)*((this->Ratio_times_Upsilon_eHeart*pTri->GradTe).z)
//// )/chi;
//// // Where is thermal pressure force? Doesn't exist in z dimension.
////
//// Ohms_sigma = -qoverm/chi;
////
//// SimpleOhms_beta_ion.x = -this->omega_ce.y/chi;
//// SimpleOhms_beta_ion.y = this->omega_ce.x/chi;
//// SimpleOhms_beta_ion.z = ((1.0-0.9*this->Ratio_times_Upsilon_eHeart.zz)*this->nu_eiBar)/chi;
////
//// SimpleOhms_beta_neutz = (m_n/(m_e+m_n))*this->nu_en_MT/chi;
////
//// // 03/04/16
//
// }
//
// void inline Populate_Acceleration_Coefficients_no_pressure(real H[6][6], real a0[6])
// {
// real factor;
//
// ZeroMemory(H,sizeof(real)*6*6);
// ZeroMemory(a0, sizeof(real)*6);
//
// // Pressure not applied.
//
// a0[2] += this->StoredEz*qoverM; // StoredEz: populated or not?
//
// // magnetic Lorentz for ions:
// // a_ion.z -= omega_ci.x*v_ion.y-omega_ci.y*v_ion.x;
// H[2][0] += omega_ci.y;
// H[2][1] -= omega_ci.x;
//
// // species combining gives:
// //a_ion.x -= (m_ion/(m_e+m_ion))*
// // omega_ci.y*(v_ion.z-v_e.z);
// // v_e is something we are passed.
// //a_ion.y += (m_ion/(m_e+m_ion))*
// // omega_ci.x*(v_ion.z-v_e.z);
// H[0][2] -= (m_i/(m_e+m_i))*omega_ci.y;
// H[1][2] += (m_i/(m_e+m_i))*omega_ci.x;
//
// //a0[0] += (m_i/(m_e+m_i))*omega_ci.y*v_e_z;
// factor = (m_i/(m_e+m_i))*omega_ci.y;
// a0[0] += factor*this->SimpleOhms_vez0;
// H[0][0] += factor*this->SimpleOhms_beta_ion.x;
// H[0][1] += factor*this->SimpleOhms_beta_ion.y;
// H[0][2] += factor*this->SimpleOhms_beta_ion.z;
// H[0][5] += factor*this->SimpleOhms_beta_neutz;
//
//
// //a0[1] -= (m_i/(m_e+m_i))*omega_ci.x*v_e_z;
// factor = -(m_i/(m_e+m_i))*omega_ci.x;
// a0[1] += factor*this->SimpleOhms_vez0;
// H[1][0] += factor*this->SimpleOhms_beta_ion.x;
// H[1][1] += factor*this->SimpleOhms_beta_ion.y;
// H[1][2] += factor*this->SimpleOhms_beta_ion.z;
// H[1][5] += factor*this->SimpleOhms_beta_neutz;
//
// // e-i Friction:
// a0[2] += ROC_v_ion_thermal_force.z;
// a0[2] += Rie_friction_force_matrix.zz*this->SimpleOhms_vez0;
// H[2][0] += Rie_friction_force_matrix.zz*this->SimpleOhms_beta_ion.x;
// H[2][1] += Rie_friction_force_matrix.zz*this->SimpleOhms_beta_ion.y;
// H[2][2] += Rie_friction_force_matrix.zz*this->SimpleOhms_beta_ion.z;
// H[2][5] += Rie_friction_force_matrix.zz*this->SimpleOhms_beta_neutz;
//
// H[2][2] -= Rie_friction_force_matrix.zz; // v_i term
//
// if (bNeutrals) {
//
// // i-n, e-n friction:
//
// real Combined = (m_e*m_n/((m_e+m_i)*(m_e+m_n)))*nu_en_MT +
// (m_i*m_n/((m_e+m_i)*(m_i+m_n)))*nu_in_MT ;
//
// //a_ion.x += Combined*(v_neut.x - v_ion.x);
// H[0][3] += Combined;
// H[0][0] -= Combined;
// H[1][4] += Combined;
// H[1][1] -= Combined;
//
// //a_ion.z += (m_n/(m_i+m_n))*nu_in_MT*(v_neut.z - v_ion.z);
// H[2][2] -= (m_n/(m_i+m_n))*nu_in_MT;
// H[2][5] += (m_n/(m_i+m_n))*nu_in_MT;
//
// factor = (m_i/(m_i+m_n))*nu_ni_MT;
// //a_neut += (m_i/(m_i+m_n))*nu_ni_MT*(v_ion - v_neut)
// // + (m_e/(m_n+m_e))*nu_ne_MT*(v_e - v_neut);
// H[3][0] += factor;
// H[3][3] -= factor;
// H[4][1] += factor;
// H[4][4] -= factor;
// H[5][2] += factor;
// H[5][5] -= factor;
// factor = (m_e/(m_n+m_e))*nu_ne_MT;
// H[3][0] += factor;
// H[3][3] -= factor;
// H[4][1] += factor;
// H[4][4] -= factor;
// H[5][5] -= factor;
//
// //a0[5] += factor*v_e_z;
// a0[5] += factor*SimpleOhms_vez0;
// H[5][0] += factor*this->SimpleOhms_beta_ion.x;
// H[5][1] += factor*this->SimpleOhms_beta_ion.y;
// H[5][2] += factor*this->SimpleOhms_beta_ion.z;
// H[5][5] += factor*this->SimpleOhms_beta_neutz;
// } else {
// H[3][3] = 1.0;
// H[4][4] = 1.0;
// H[5][5] = 1.0;
//// fric_heat_energy_rate_in = 0.0;
//// fric_heat_energy_rate_en_over_ne = 0.0;
// }
// } // almost exact same as above routine.
//
//
// void inline Populate_Acceleration_Coefficients_no_pressure(real H[6][6], real a0[6])
// {
// real factor;
//
// ZeroMemory(H,sizeof(real)*6*6);
// ZeroMemory(a0, sizeof(real)*6);
//
// // Pressure not applied.
//
// a0[2] += this->StoredEz*qoverM; // StoredEz: populated or not?
//
// // magnetic Lorentz for ions:
// // a_ion.z -= omega_ci.x*v_ion.y-omega_ci.y*v_ion.x;
// H[2][0] += omega_ci.y;
// H[2][1] -= omega_ci.x;
//
// // species combining gives:
// //a_ion.x -= (m_ion/(m_e+m_ion))*
// // omega_ci.y*(v_ion.z-v_e.z);
// // v_e is something we are passed.
// //a_ion.y += (m_ion/(m_e+m_ion))*
// // omega_ci.x*(v_ion.z-v_e.z);
// H[0][2] -= (m_i/(m_e+m_i))*omega_ci.y;
// H[1][2] += (m_i/(m_e+m_i))*omega_ci.x;
//
// //a0[0] += (m_i/(m_e+m_i))*omega_ci.y*v_e_z;
// factor = (m_i/(m_e+m_i))*omega_ci.y;
// a0[0] += factor*this->SimpleOhms_vez0;
// H[0][0] += factor*this->SimpleOhms_beta_ion.x;
// H[0][1] += factor*this->SimpleOhms_beta_ion.y;
// H[0][2] += factor*this->SimpleOhms_beta_ion.z;
// H[0][5] += factor*this->SimpleOhms_beta_neutz;
//
//
// //a0[1] -= (m_i/(m_e+m_i))*omega_ci.x*v_e_z;
// factor = -(m_i/(m_e+m_i))*omega_ci.x;
// a0[1] += factor*this->SimpleOhms_vez0;
// H[1][0] += factor*this->SimpleOhms_beta_ion.x;
// H[1][1] += factor*this->SimpleOhms_beta_ion.y;
// H[1][2] += factor*this->SimpleOhms_beta_ion.z;
// H[1][5] += factor*this->SimpleOhms_beta_neutz;
//
// // e-i Friction:
// a0[2] += ROC_v_ion_thermal_force.z;
// a0[2] += Rie_friction_force_matrix.zz*this->SimpleOhms_vez0;
// H[2][0] += Rie_friction_force_matrix.zz*this->SimpleOhms_beta_ion.x;
// H[2][1] += Rie_friction_force_matrix.zz*this->SimpleOhms_beta_ion.y;
// H[2][2] += Rie_friction_force_matrix.zz*this->SimpleOhms_beta_ion.z;
// H[2][5] += Rie_friction_force_matrix.zz*this->SimpleOhms_beta_neutz;
//
// H[2][2] -= Rie_friction_force_matrix.zz; // v_i term
//
// if (bNeutrals) {
//
// // i-n, e-n friction:
//
// real Combined = (m_e*m_n/((m_e+m_i)*(m_e+m_n)))*nu_en_MT +
// (m_i*m_n/((m_e+m_i)*(m_i+m_n)))*nu_in_MT ;
//
// //a_ion.x += Combined*(v_neut.x - v_ion.x);
// H[0][3] += Combined;
// H[0][0] -= Combined;
// H[1][4] += Combined;
// H[1][1] -= Combined;
//
// //a_ion.z += (m_n/(m_i+m_n))*nu_in_MT*(v_neut.z - v_ion.z);
// H[2][2] -= (m_n/(m_i+m_n))*nu_in_MT;
// H[2][5] += (m_n/(m_i+m_n))*nu_in_MT;
//
// factor = (m_i/(m_i+m_n))*nu_ni_MT;
// //a_neut += (m_i/(m_i+m_n))*nu_ni_MT*(v_ion - v_neut)
// // + (m_e/(m_n+m_e))*nu_ne_MT*(v_e - v_neut);
// H[3][0] += factor;
// H[3][3] -= factor;
// H[4][1] += factor;
// H[4][4] -= factor;
// H[5][2] += factor;
// H[5][5] -= factor;
// factor = (m_e/(m_n+m_e))*nu_ne_MT;
// H[3][0] += factor;
// H[3][3] -= factor;
// H[4][1] += factor;
// H[4][4] -= factor;
// H[5][5] -= factor;
//
// //a0[5] += factor*v_e_z;
// a0[5] += factor*SimpleOhms_vez0;
// H[5][0] += factor*this->SimpleOhms_beta_ion.x;
// H[5][1] += factor*this->SimpleOhms_beta_ion.y;
// H[5][2] += factor*this->SimpleOhms_beta_ion.z;
// H[5][5] += factor*this->SimpleOhms_beta_neutz;
// } else {
// H[3][3] = 1.0;
// H[4][4] = 1.0;
// H[5][5] = 1.0;
//// fric_heat_energy_rate_in = 0.0;
//// fric_heat_energy_rate_en_over_ne = 0.0;
// }
// } // almost exact same as with-pressure routine.
//
//};
void TriMesh::Set_nT_and_Get_Pressure(int species)
{
// Must have populated:
// Vertex::AreaCell
// anticlockwise tri & neigh index arrays
// Triangle::cent
Vector2 tri_cent, sum, u[3];
ConvexPolygon cp;
int par[3];
Triangle * pTri;
long iTri, iVertex;
Vertex * pVertex;
real beta[3];
// Input: AreaCell