-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadi_solvers.c
1829 lines (1672 loc) · 68.1 KB
/
adi_solvers.c
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
/*Functions and utilities for integration of parabolic (currently resistive
and thermal conduction) terms with the Alternating Direction Implicit algorithm*/
// Remarkable comments:
// [Opt] = it can be optimized (in terms of performance)
// [Err] = it is and error (usually introduced on purpose)
// [Rob] = it can/should be made more robust
#include "pluto.h"
#include "adi.h"
#include "capillary_wall.h"
#include "debug_utilities.h"
#include <time.h>
#include <stdlib.h>
/*Relative tollerance for checking that at each call of an ADI scheme, the algorithm advances for all the reqired total time*/
#define DT_REL_TOLL 1e-8
/****************************************************************************
Performs an implicit update of a diffusive problem (either for B or for T).
It also applies the bcs on the ghost cells of the output matrix (**v) (useful later
for instance for ResEnergyIncrease())
*****************************************************************************/
void ImplicitUpdate (double **v, double **b, double **source,
double **Hp, double **Hm, double **C,
Lines *lines, Bcs *lbound, Bcs *rbound,
int compute_inflow, double *inflow, Grid *grid,
double dt, int dir) {
/*[Opt] Maybe I could pass to this func. an integer which tells which bc has to be
used inside the structure *lines, instead of passing separately the bcs (which are
still also contained inside *lines)*/
/*[Opt] Maybe I could use g_dir instead of passing dir, but I am afraid of
doing caos modifiying the value of g_dir for the rest of PLUTO*/
// int *m, *n, *i, *j; // m and n play the role of i and j (not necessarly respectively)
// int s, dom_line_idx;
// const int zero=0;
int i,j;
static int first_call = 1;
int Nlines = lines->N;
int ridx, lidx, l;
/* I allocate these as big as if I had to cover the whole domain, so that I
don't need to reallocate at every domain line that I update */
static double *diagonal, *upper, *lower, *rhs, *x;
double *dz, *rR, *rL;
double vol_lidx, vol_ridx;
if (first_call) {
diagonal = ARRAY_1D(MAX(NX1_TOT, NX2_TOT), double);
rhs = ARRAY_1D(MAX(NX1_TOT, NX2_TOT), double);
upper = ARRAY_1D(MAX(NX1_TOT, NX2_TOT), double);
lower = ARRAY_1D(MAX(NX1_TOT, NX2_TOT), double);
x = ARRAY_1D(MAX(NX1_TOT, NX2_TOT), double);
}
rR = grid[IDIR].xr_glob;
rL = grid[IDIR].xl_glob;
dz = grid[JDIR].dx_glob;
if (dir == IDIR) {
/********************
* Case direction IDIR
*********************/
for (l = 0; l < Nlines; l++) {
j = lines->dom_line_idx[l];
lidx = lines->lidx[l];
ridx = lines->ridx[l];
upper[lidx] = -dt/C[j][lidx]*Hp[j][lidx];
lower[ridx] = -dt/C[j][ridx]*Hm[j][ridx];
rhs[lidx] = b[j][lidx];
rhs[ridx] = b[j][ridx];
for (i = lidx+1; i < ridx; i++) {
diagonal[i] = 1 + dt/C[j][i] * (Hp[j][i]+Hm[j][i]);
rhs[i] = b[j][i];
upper[i] = -dt/C[j][i]*Hp[j][i];
lower[i] = -dt/C[j][i]*Hm[j][i];
}
/* I include the effect of the source */
if (source != NULL) {
for (i = lidx; i <= ridx; i++)
rhs[i] += source[j][i]*dt;
}
// I set the Bcs for left boundary
if (lbound[l].kind == DIRICHLET){
diagonal[lidx] = 1 + dt/C[j][lidx]*(Hp[j][lidx]+2*Hm[j][lidx]);
rhs[lidx] += dt/C[j][lidx]*Hm[j][lidx]*2*lbound[l].values[0];
} else if (lbound[l].kind == NEUMANN_HOM) {
diagonal[lidx] = 1 + dt/C[j][lidx]*Hp[j][lidx];
} else {
print1("\n[ImplicitUpdate]Error setting left bc (in dir i), not known bc kind!");
QUIT_PLUTO(1);
}
// I set the Bcs for right boundary
if (rbound[l].kind == DIRICHLET){
diagonal[ridx] = 1 + dt/C[j][ridx]*(2*Hp[j][ridx]+Hm[j][ridx]);
rhs[ridx] += dt/C[j][ridx]*Hp[j][ridx]*2*rbound[l].values[0];
} else if (rbound[l].kind == NEUMANN_HOM) {
diagonal[ridx] = 1 + dt/C[j][ridx]*Hm[j][ridx];
} else {
print1("\n[ImplicitUpdate]Error setting right bc (in dir i), not known bc kind!");
QUIT_PLUTO(1);
}
/*---------------------------------------------------------------------*/
/* --- Now I solve the system --- */
tdm_solver( x+lidx, diagonal+lidx, upper+lidx, lower+lidx+1, rhs+lidx, ridx-lidx+1);
/*[Opt] Is this for a waste of time? maybe I could engineer better the use of tdm_solver function(or the way it is written)*/
for (i=lidx; i<=ridx; i++)
v[j][i] = x[i];
/*---------------------------------------------------------------------*/
/*--- I set the boundary values (ghost cells) in the solution
[I do it now as I for the NEUMANN conditions I could't do it before solving the tridiag. system] ---*/
// Cells near left boundary
if (lbound[l].kind == DIRICHLET){
// I assign the ghost value (needed by ResEnergyIncrease and maybe others..)
v[j][lidx-1] = 2*lbound[l].values[0] - b[j][lidx];
if (compute_inflow) {
/*--- I compute the inflow ---*/
// I am not sure this "2" in front of pi is ok
*inflow += (v[j][lidx-1]-v[j][lidx]) * Hm[j][lidx] * 2*CONST_PI*dz[j] * dt;
}
} else if (lbound[l].kind == NEUMANN_HOM) {
/* I assign the ghost value (needed by ResEnergyIncrease and maybe others..).*/
v[j][lidx-1] = v[j][lidx];
if (compute_inflow) {
/*--- I compute the inflow (0!!!)---*/
*inflow += 0;
}
} else {
print1("\n[ImplcitUpdate]Error setting left ghost in solution (in dir i), not known bc kind!");
QUIT_PLUTO(1);
}
// Cells near right boundary
if (rbound[l].kind == DIRICHLET){
v[j][ridx+1] = 2*rbound[l].values[0] - v[j][ridx];
if (compute_inflow) {
/*--- I compute the inflow ---*/
*inflow += (v[j][ridx+1]-v[j][ridx]) * Hp[j][ridx] * 2*CONST_PI*dz[j] * dt;
}
} else if (rbound[l].kind == NEUMANN_HOM) {
v[j][ridx+1] = v[j][ridx];
if (compute_inflow) {
/*--- I compute the inflow (0!!!)---*/
*inflow += 0;
}
} else {
print1("\n[ImplcitUpdate]Error setting right ghost in solution (in dir i), not known bc kind!");
QUIT_PLUTO(1);
}
}
} else if (dir == JDIR) {
/********************
* Case direction JDIR
*********************/
for (l = 0; l < Nlines; l++) {
i = lines->dom_line_idx[l];
lidx = lines->lidx[l];
ridx = lines->ridx[l];
upper[lidx] = -dt/C[lidx][i]*Hp[lidx][i];
lower[ridx] = -dt/C[ridx][i]*Hm[ridx][i];
rhs[lidx] = b[lidx][i];
rhs[ridx] = b[ridx][i];
for (j = lidx+1; j < ridx; j++) {
diagonal[j] = 1 + dt/C[j][i] * (Hp[j][i]+Hm[j][i]);
rhs[j] = b[j][i];
upper[j] = -dt/C[j][i]*Hp[j][i];
lower[j] = -dt/C[j][i]*Hm[j][i];
}
/* I include the effect of the source */
if (source != NULL) {
for (j = lidx; j <= ridx; j++)
rhs[j] += source[j][i]*dt;
}
// I set the Bcs for left boundary
if (lbound[l].kind == DIRICHLET){
diagonal[lidx] = 1 + dt/C[lidx][i]*(Hp[lidx][i]+2*Hm[lidx][i]);
rhs[lidx] += dt/C[lidx][i]*Hm[lidx][i]*2*lbound[l].values[0];
} else if (lbound[l].kind == NEUMANN_HOM) {
diagonal[lidx] = 1 + dt/C[lidx][i]*Hp[lidx][i];
} else {
print1("\n[ImplicitUpdate]Error setting left bc (in dir j), not known bc kind!");
QUIT_PLUTO(1);
}
// I set the Bcs for right boundary
if (rbound[l].kind == DIRICHLET){
diagonal[ridx] = 1 + dt/C[ridx][i]*(2*Hp[ridx][i]+Hm[ridx][i]);
rhs[ridx] += dt/C[ridx][i]*Hp[ridx][i]*2*rbound[l].values[0];
} else if (rbound[l].kind == NEUMANN_HOM) {
diagonal[ridx] = 1 + dt/C[ridx][i]*Hm[ridx][i];
} else {
print1("\n[ImplicitUpdate]Error setting right bcs (in dir j), not known bc kind!");
QUIT_PLUTO(1);
}
/*---------------------------------------------------------------------*/
/* --- Now I solve the system --- */
tdm_solver( x+lidx, diagonal+lidx, upper+lidx, lower+lidx+1, rhs+lidx, ridx-lidx+1);
for (j=lidx; j<=ridx; j++)
v[j][i] = x[j];
/*---------------------------------------------------------------------*/
/*--- I set the boundary values (ghost cells) in the solution
[I do it now as I for the NEUMANN conditions I could't do it before solving the tridiag. system] ---*/
/*--- I set the boundary values (ghost cells) ---*/
// Cells near left boundary
if (lbound[l].kind == DIRICHLET){
v[lidx-1][i] = 2*lbound[l].values[0] - v[lidx][i];
if (compute_inflow) {
/*--- I compute the inflow ---*/
*inflow += (v[lidx-1][i]-v[lidx][i]) * Hm[lidx][i] * CONST_PI*(rR[i]*rR[i]-rL[i]*rL[i]) * dt;
}
} else if (lbound[l].kind == NEUMANN_HOM) {
v[lidx-1][i] = v[lidx][i];
if (compute_inflow) {
/*--- I compute the inflow (0!!!)---*/
*inflow += 0;
}
} else {
print1("\n[ImplcitUpdate]Error setting left ghost in solution (in dir j), not known bc kind!");
QUIT_PLUTO(1);
}
// Cells near right boundary
if (rbound[l].kind == DIRICHLET) {
v[ridx+1][i] = 2*rbound[l].values[0] - v[ridx][i];
if (compute_inflow) {
/*--- I compute the inflow ---*/
*inflow += (v[ridx+1][i]-v[ridx][i]) * Hp[ridx][i] * CONST_PI*(rR[i]*rR[i]-rL[i]*rL[i]) * dt;
}
} else if (rbound[l].kind == NEUMANN_HOM) {
v[ridx+1][i] = v[ridx][i];
if (compute_inflow) {
/*--- I compute the inflow (0!!!)---*/
*inflow += 0;
}
} else {
print1("\n[ImplcitUpdate]Error setting right ghost in solution (in dir j), not known bc kind!");
QUIT_PLUTO(1);
}
}
} else {
print1("[ImplicitUpdate] Unimplemented choice for 'dir'!");
QUIT_PLUTO(1);
}
first_call = 0;
}
//
/****************************************************************************
Performs an explicit update of a diffusive problem (either for B or for T).
It also applies the bcs on the ghost cells of the input matrix (**b) (useful later
for instance for ResEnergyIncrease())
*****************************************************************************/
void ExplicitUpdate (double **v, double **b, double **source,
double **Hp, double **Hm, double **C,
Lines *lines, Bcs *lbound, Bcs *rbound,
int compute_inflow, double *inflow, Grid *grid,
double dt, int dir) {
int i,j,l;
int ridx, lidx;
int Nlines = lines->N;
double *rR, *rL;
double *dz;
double vol_lidx, vol_ridx;
static double **rhs;
static int first_call = 1;
if (first_call) {
rhs = ARRAY_2D(NX2_TOT, NX1_TOT, double);
first_call = 0;
}
rR = grid[IDIR].xr_glob;
rL = grid[IDIR].xl_glob;
dz = grid[JDIR].dx_glob;
if (dir == IDIR) {
/********************
* Case direction IDIR
*********************/
for (l = 0; l < Nlines; l++) {
j = lines->dom_line_idx[l];
lidx = lines->lidx[l];
ridx = lines->ridx[l];
if (source != NULL) {
for (i = lidx; i <= ridx; i++)
rhs[j][i] = b[j][i] + source[j][i]*dt;
} else {
/*[Opt] Maybe I could assign directly the address. BE CAREFUL:
if I assign the address, then I have to recover the old address of rhs (by saving temporarly the old rhs address
inside another variable), otherwise
at the next call of this function I will write over the memory of the old b*/
for (i = lidx; i <= ridx; i++)
rhs[j][i] = b[j][i];
}
/*--- I set the boundary values (ghost cells) ---*/
// Cells near left boundary
if (lbound[l].kind == DIRICHLET){
// I assign the ghost value (needed by ResEnergyIncrease and maybe others..)
// [Err] decomment next line
b[j][lidx-1] = 2*lbound[l].values[0] - b[j][lidx];
//[Err] Experimental: 2nd order accurate bc for cell centered FD (as explained in L.Chen draft on FDM)
// b[j][lidx-1] = 1/3*b[j][lidx+1] + 8/3*lbound[l].values[0] - 2*b[j][lidx];
if (compute_inflow) {
/*--- I compute the inflow ---*/
// I am not sure this "2" in front of pi is ok
*inflow += (b[j][lidx-1]-b[j][lidx]) * Hm[j][lidx] * 2*CONST_PI*dz[j] * dt;
}
} else if (lbound[l].kind == NEUMANN_HOM) {
/* I assign the ghost value (needed by ResEnergyIncrease and maybe others..).*/
b[j][lidx-1] = b[j][lidx];
if (compute_inflow) {
/*--- I compute the inflow (0!!!)---*/
*inflow += 0;
}
} else {
print1("\n[ExplicitUpdate]Error setting left bc (in dir i), not known bc kind!");
QUIT_PLUTO(1);
}
// Cells near right boundary
if (rbound[l].kind == DIRICHLET){
// [Err] decomment next line
b[j][ridx+1] = 2*rbound[l].values[0] - b[j][ridx];
//[Err] Experimental: 2nd order accurate bc for cell centered FD (as explained in L.Chen draft on FDM)
// b[j][ridx+1] = 1/3*b[j][ridx-1] + 8/3*rbound[l].values[0] - 2*b[j][ridx];
if (compute_inflow) {
/*--- I compute the inflow ---*/
*inflow += (b[j][ridx+1]-b[j][ridx]) * Hp[j][ridx] * 2*CONST_PI*dz[j] * dt;
}
} else if (rbound[l].kind == NEUMANN_HOM) {
b[j][ridx+1] = b[j][ridx];
if (compute_inflow) {
/*--- I compute the inflow (0!!!)---*/
*inflow += 0;
}
} else {
print1("\n[ExplicitUpdate]Error setting right bc (in dir i), not known bc kind!");
QUIT_PLUTO(1);
}
/*--- Actual update ---*/
for (i = lidx; i <= ridx; i++)
v[j][i] = rhs[j][i] + dt/C[j][i] * (b[j][i+1]*Hp[j][i] - b[j][i]*(Hp[j][i]+Hm[j][i]) + b[j][i-1]*Hm[j][i]);
}
} else if (dir == JDIR) {
/********************
* Case direction JDIR
*********************/
rR = grid[IDIR].xr_glob;
rL = grid[IDIR].xl_glob;
for (l = 0; l < Nlines; l++) {
i = lines->dom_line_idx[l];
lidx = lines->lidx[l];
ridx = lines->ridx[l];
if (source != NULL) {
for (j = lidx; j <= ridx; j++)
rhs[j][i] = b[j][i] + source[j][i]*dt;
} else {
/*[Opt] Maybe I could assign directly the address. BE CAREFUL:
if I assign the address, then I have to recover the old address of rhs (by saving temporarly the old rhs address
inside another variable), otherwise
at the next call of this function I will write over the memory of the old b*/
for (j = lidx; j <= ridx; j++)
rhs[j][i] = b[j][i];
}
/*--- I set the boundary values (ghost cells) ---*/
// Cells near left boundary
if (lbound[l].kind == DIRICHLET){
// [Err] decomment next line
b[lidx-1][i] = 2*lbound[l].values[0] - b[lidx][i];
//[Err] Experimental: 2nd order accurate bc for cell centered FD (as explained in L.Chen draft on FDM)
// b[lidx-1][i] = 1/3*b[lidx+1][i] + 8/3*lbound[l].values[0] - 2*b[lidx][i];
if (compute_inflow) {
/*--- I compute the inflow ---*/
*inflow += (b[lidx-1][i]-b[lidx][i]) * Hm[lidx][i] * CONST_PI*(rR[i]*rR[i]-rL[i]*rL[i]) * dt;
}
} else if (lbound[l].kind == NEUMANN_HOM) {
b[lidx-1][i] = b[lidx][i];
if (compute_inflow) {
/*--- I compute the inflow (0!!!)---*/
*inflow += 0;
}
} else {
print1("\n[ExplicitUpdate]Error setting left bc (in dir j), not known bc kind!");
QUIT_PLUTO(1);
}
// Cells near right boundary
if (rbound[l].kind == DIRICHLET){
// [Err] decomment next line
b[ridx+1][i] = 2*rbound[l].values[0] - b[ridx][i];
//[Err] Experimental: 2nd order accurate bc for cell centered FD (as explained in L.Chen draft on FDM)
// b[ridx+1][i] = 1/3*b[ridx-1][i] + 8/3*rbound[l].values[0] - 2*b[ridx][i];
if (compute_inflow) {
/*--- I compute the inflow ---*/
*inflow += (b[ridx+1][i]-b[ridx][i]) * Hp[ridx][i] * CONST_PI*(rR[i]*rR[i]-rL[i]*rL[i]) * dt;
}
} else if (rbound[l].kind == NEUMANN_HOM) {
b[ridx+1][i] = b[ridx][i];
if (compute_inflow) {
/*--- I compute the inflow (0!!!)---*/
*inflow += 0;
}
} else {
print1("\n[ExplicitUpdate]Error setting right bc (in dir j), not known bc kind!");
QUIT_PLUTO(1);
}
/*--- Actual update ---*/
for (j = lidx; j <= ridx; j++){
v[j][i] = rhs[j][i] + dt/C[j][i] * (b[j+1][i]*Hp[j][i] - b[j][i]*(Hp[j][i]+Hm[j][i]) + b[j-1][i]*Hm[j][i]);
// print1("v[%d][%d]=%e\n", j,i,v[j][i]);
}
}
} else {
print1("[ExplicitUpdate] Unimplemented choice for 'dir'!");
QUIT_PLUTO(1);
}
}
/****************************************************************************
Performs an explicit update of a diffusive problem (either for B or for T).
This function allows the user to provide separate variables for the computation of
the right hand side(**b) and the discrete derivative part(**b_der) (reason: this function is
meant for application inside a Douglas-Rachford scheme).
Be careful! The bcs are not required, since they are supposed to be already set in the
ghost cells of **b_der.
It also applies the bcs on the ghost cells of the input matrix (**b) (useful later
for instance for ResEnergyIncrease())
*****************************************************************************/
void ExplicitUpdateDR (double **v, double **b, double **b_der, double **source,
double **Hp, double **Hm, double **C,
Lines *lines,
int compute_inflow, double *inflow, Grid *grid,
double dt, int dir) {
int i,j,l;
int ridx, lidx;
int Nlines = lines->N;
double *rR, *rL;
double *dz;
static double **rhs;
static int first_call = 1;
double vol_lidx, vol_ridx;
if (first_call) {
rhs = ARRAY_2D(NX2_TOT, NX1_TOT, double);
first_call = 0;
}
rR = grid[IDIR].xr_glob;
rL = grid[IDIR].xl_glob;
dz = grid[JDIR].dx_glob; // it is equal to grid[JDIR].xr_glob - grid[JDIR].xl_glob
if (dir == IDIR) {
/********************
* Case direction IDIR
*********************/
for (l = 0; l < Nlines; l++) {
j = lines->dom_line_idx[l];
lidx = lines->lidx[l];
ridx = lines->ridx[l];
if (source != NULL) {
for (i = lidx; i <= ridx; i++)
rhs[j][i] = b[j][i] + source[j][i]*dt;
} else {
/*[Opt] Maybe I could assign directly the address. BE CAREFUL:
if I assign the address, then I have to recover the old address of rhs (by saving temporarly the old rhs address
inside another variable), otherwise
at the next call of this function I will write over the memory of the old b*/
for (i = lidx; i <= ridx; i++)
rhs[j][i] = b[j][i];
}
if (compute_inflow) {
/*--- I compute the inflow ---*/
// I am not sure this "2" in front of pi is ok
*inflow += (b_der[j][lidx-1]-b_der[j][lidx]) * Hm[j][lidx] * 2*CONST_PI*dz[j] * dt;
// Here the "2" in front of pi is ok
*inflow += (b_der[j][ridx+1]-b_der[j][ridx]) * Hp[j][ridx] * 2*CONST_PI*dz[j] * dt;
}
/*--- Actual update ---*/
for (i = lidx; i <= ridx; i++)
v[j][i] = rhs[j][i] + dt/C[j][i] * (b_der[j][i+1]*Hp[j][i] - b_der[j][i]*(Hp[j][i]+Hm[j][i]) + b_der[j][i-1]*Hm[j][i]);
}
} else if (dir == JDIR) {
/********************
* Case direction JDIR
*********************/
for (l = 0; l < Nlines; l++) {
i = lines->dom_line_idx[l];
lidx = lines->lidx[l];
ridx = lines->ridx[l];
if (source != NULL) {
for (j = lidx; j <= ridx; j++)
rhs[j][i] = b[j][i] + source[j][i]*dt;
} else {
/*[Opt] Maybe I could assign directly the address. BE CAREFUL:
if I assign the address, then I have to recover the old address of rhs (by saving temporarly the old rhs address
inside another variable), otherwise
at the next call of this function I will write over the memory of the old b*/
for (j = lidx; j <= ridx; j++)
rhs[j][i] = b[j][i];
}
if (compute_inflow) {
/*--- I compute the inflow ---*/
// Modified again 4/12/2018
*inflow += (b_der[lidx-1][i]-b_der[lidx][i]) * Hm[lidx][i] * CONST_PI*(rR[i]*rR[i]-rL[i]*rL[i]) * dt;
*inflow += (b_der[ridx+1][i]-b_der[ridx][i]) * Hp[ridx][i] * CONST_PI*(rR[i]*rR[i]-rL[i]*rL[i]) * dt;
}
/*--- Actual update ---*/
for (j = lidx; j <= ridx; j++){
v[j][i] = rhs[j][i] + dt/C[j][i] * (b_der[j+1][i]*Hp[j][i] - b_der[j][i]*(Hp[j][i]+Hm[j][i]) + b_der[j-1][i]*Hm[j][i]);
// print1("v[%d][%d]=%e\n", j,i,v[j][i]);
}
}
} else {
print1("[ExplicitUpdateDR] Unimplemented choice for 'dir'!");
QUIT_PLUTO(1);
}
}
/************************************************************
* ApplyBCsonGhosts(): This func. applies the BCs to the "solution"
* vector/matrix as prescribed by rbound and lbound.
* *********************************************************/
void ApplyBCsonGhosts(double **v, Lines *lines,
Bcs *lbound, Bcs *rbound,
int dir) {
int i,j,l;
int ridx, lidx;
int Nlines = lines->N;
if (dir == IDIR) {
/********************
* Case direction IDIR
*********************/
for (l = 0; l < Nlines; l++) {
j = lines->dom_line_idx[l];
lidx = lines->lidx[l];
ridx = lines->ridx[l];
/*--- I set the boundary values (ghost cells) ---*/
// Cells near left boundary
if (lbound[l].kind == DIRICHLET){
// I assign the ghost value (needed by ResEnergyIncrease and maybe others..)
// [Err] decomment next line
v[j][lidx-1] = 2*lbound[l].values[0] - v[j][lidx];
//[Err] Experimental: 2nd order accurate bc for cell centered FD (as explained in L.Chen draft on FDM)
// v[j][lidx-1] = 1/3*v[j][lidx+1] + 8/3*lbound[l].values[0] - 2*v[j][lidx];
} else if (lbound[l].kind == NEUMANN_HOM) {
/* I assign the ghost value (needed by ResEnergyIncrease and maybe others..).*/
v[j][lidx-1] = v[j][lidx];
} else {
print1("\n[ApplyBCsonGhosts]Error setting left bc (in dir i), not known bc kind!");
QUIT_PLUTO(1);
}
// Cells near right boundary
if (rbound[l].kind == DIRICHLET){
// [Err] decomment next line
v[j][ridx+1] = 2*rbound[l].values[0] - v[j][ridx];
//[Err] Experimental: 2nd order accurate bc for cell centered FD (as explained in L.Chen draft on FDM)
// v[j][ridx+1] = 1/3*v[j][ridx-1] + 8/3*rbound[l].values[0] - 2*v[j][ridx];
} else if (rbound[l].kind == NEUMANN_HOM) {
v[j][ridx+1] = v[j][ridx];
} else {
print1("\n[ApplyBCsonGhosts]Error setting right bc (in dir i), not known bc kind!");
QUIT_PLUTO(1);
}
}
} else if (dir == JDIR) {
/********************
* Case direction JDIR
*********************/
for (l = 0; l < Nlines; l++) {
i = lines->dom_line_idx[l];
lidx = lines->lidx[l];
ridx = lines->ridx[l];
/*--- I set the boundary values (ghost cells) ---*/
// Cells near left boundary
if (lbound[l].kind == DIRICHLET){
// [Err] decomment next line
v[lidx-1][i] = 2*lbound[l].values[0] - v[lidx][i];
//[Err] Experimental: 2nd order accurate bc for cell centered FD (as explained in L.Chen draft on FDM)
// v[lidx-1][i] = 1/3*v[lidx+1][i] + 8/3*lbound[l].values[0] - 2*v[lidx][i];
} else if (lbound[l].kind == NEUMANN_HOM) {
v[lidx-1][i] = v[lidx][i];
} else {
print1("\n[ApplyBCsonGhosts]Error setting left bc (in dir j), not known bc kind!");
QUIT_PLUTO(1);
}
// Cells near right boundary
if (rbound[l].kind == DIRICHLET){
// [Err] decomment next line
v[ridx+1][i] = 2*rbound[l].values[0] - v[ridx][i];
//[Err] Experimental: 2nd order accurate bc for cell centered FD (as explained in L.Chen draft on FDM)
// v[ridx+1][i] = 1/3*v[ridx-1][i] + 8/3*rbound[l].values[0] - 2*v[ridx][i];
} else if (rbound[l].kind == NEUMANN_HOM) {
v[ridx+1][i] = v[ridx][i];
} else {
print1("\n[ApplyBCsonGhosts]Error setting right bc (in dir j), not known bc kind!");
QUIT_PLUTO(1);
}
}
} else {
print1("[ApplyBCsonGhosts] Unimplemented choice for 'dir'!");
QUIT_PLUTO(1);
}
}
/************************************************************
* Solve a linear system made by a tridiagonal matrix.
* See https://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
* for info (accessed on 24/3/2017)
* BE CAREFUL: THIS FUNC. MODIFIES ITS INPUT (NOT ONLY X!)
*
* N: the size of the arrays x, diagonal, right_hand_side.
* x: solution
* *********************************************************/
void tdm_solver(double *x, double const *diagonal, double *up,
double const *lower, double *rhs, int const N) {
/*[Opt] Is it really needed to define two new arrays?
Maybe I can make that it uses directly the diagonal, upper,
lower arrays, modifying them, the only problem is that I
don't like the idea as a principle, that the function
modifies its actual input with no apparent reason*/
int i;
up[0] = up[0]/diagonal[0];
for (i=1; i<N-1; i++)
up[i] = up[i] / (diagonal[i]-lower[i-1]*up[i-1]);
rhs[0] = rhs[0]/diagonal[0];
for (i=1; i<N; i++)
rhs[i] = (rhs[i] - lower[i-1]*rhs[i-1]) / (diagonal[i] - lower[i-1]*up[i-1]);
x[N-1] = rhs[N-1];
for (i=N-2; i>-1; i--)
x[i] = rhs[i] - up[i]*x[i+1];
}
/* ***********************************************************
* Modified Peachman-Rachford ADI method (I have no clue whether this
* is docuemnted in literature and how accurate it is. I hope it is fine
* actually I use it since I have seen that in improves
* the stability properties of the diffusion of the magnetic field
* and computation of dUres).
*
* input: diff = BDIFF or TDIFF
* int order = FIRST_IDIR or FIRST_JDIR: tells whether the order of the directions
* must be IDIR, JDIR (FIRST_IDIR) or JDIR, IDIR (FIRST_JDIR).
* **dEdT: may point to NULL in case diff == BDIFF
* **dUres: will not be updated if diff != BDIFF
* (it is my opinion that for the sake of clarity it is better not to
* "merge" in one single variable the quantities **dEdT and **dUres,
* even though I never need both of them at the same time)
* ***********************************************************/
void PeacemanRachfordMod(double **v_new, double **v_old,
double **dUres, double **dEdT,
const Data *d, Grid *grid,
Lines *lines, int diff, int order,
double dt, double t0, double fract, int M) {
static double **v_aux, **v_old_aux;; // auxiliary solution vector
double **v_new_save; // To save the actual address of v_new
static double **Ip, **Im, **CI, **Jp, **Jm, **CJ;
static int first_call = 1;
double **H1p, **H1m, **H2p, **H2m, **C1, **C2;
// void (*BoundaryADI) (Lines, const Data, Grid, double);
BoundaryADI *ApplyBCs;
BuildIJ *MakeIJ;
int dir1, dir2;
double dts;
double t_now;
int l,i,j, s;
#if (JOULE_EFFECT_AND_MAG_ENG)
static double **dUres_aux; // auxiliary vector containing a contribution to ohmic heating
#endif
#if (JOULE_EFFECT_AND_MAG_ENG && !POW_INSIDE_ADI)
static double **Br_avg, **dUres_aux1;
#endif
if (first_call) {
v_aux = ARRAY_2D(NX2_TOT, NX1_TOT, double);
v_old_aux = ARRAY_2D(NX2_TOT, NX1_TOT, double);
#if (JOULE_EFFECT_AND_MAG_ENG)
dUres_aux = ARRAY_2D(NX2_TOT, NX1_TOT, double);
#endif
#if (JOULE_EFFECT_AND_MAG_ENG && !POW_INSIDE_ADI)
Br_avg = ARRAY_2D(NX2_TOT, NX1_TOT, double);
dUres_aux1 = ARRAY_2D(NX2_TOT, NX1_TOT, double);
#endif
Ip = ARRAY_2D(NX2_TOT, NX1_TOT, double);
Im = ARRAY_2D(NX2_TOT, NX1_TOT, double);
Jp = ARRAY_2D(NX2_TOT, NX1_TOT, double);
Jm = ARRAY_2D(NX2_TOT, NX1_TOT, double);
CI = ARRAY_2D(NX2_TOT, NX1_TOT, double);
CJ = ARRAY_2D(NX2_TOT, NX1_TOT, double);
first_call = 0;
}
/* Set the direction order*/
if (order == FIRST_IDIR) {
H1p = Ip; H1m = Im;
H2p = Jp; H2m = Jm;
C1 = CI; C2 = CJ;
dir1 = IDIR; dir2 = JDIR;
} else if (order == FIRST_JDIR) {
H1p = Jp; H1m = Jm;
H2p = Ip; H2m = Im;
C1 = CJ; C2 = CI;
dir1 = JDIR; dir2 = IDIR;
}
switch (diff) {
#if RESISTIVITY==ALTERNATING_DIRECTION_IMPLICIT
case BDIFF:
ApplyBCs = BoundaryADI_Res;
MakeIJ = BuildIJ_Res;
break;
#endif
#if THERMAL_CONDUCTION==ALTERNATING_DIRECTION_IMPLICIT
case TDIFF:
ApplyBCs = BoundaryADI_TC;
MakeIJ = BuildIJ_TC;
break;
#endif
default:
print1("\n[PeachmanRachford]Wrong setting for diffusion (diff) problem");
QUIT_PLUTO(1);
break;
}
// I copy v_old inside v_old_aux, as I cannot use directly v_old in the cycle, it will be modified!
LINES_LOOP(lines[IDIR], l, j, i)
v_new[j][i] = v_old[j][i];
v_new_save = v_new;
print1("\nI apply a Peaceman-Rachford scheme for diff=%d (BDIFF=%d,TDIFF=%d)\n", diff, BDIFF, TDIFF);
print1(" -> I do %d calls to ImplicitUpdate() and %d calls to ExplicitUpdate()\n", 2*M,2*M);
/*****************************************
* ---------------------------------------
* I perform the actual cycle
* ---------------------------------------
* ****************************************/
dts = dt/M;
t_now = t0;
ApplyBCs(lines, d, grid, t_now, dir1);
ApplyBCs(lines, d, grid, t_now, dir2);
MakeIJ(d, grid, lines, Ip, Im, Jp, Jm, CI, CJ, dEdT);
for (s=0; s<M; s++) {
/* ---- Swap pointers to be ready for next cycle ---*/
SwapDoublePointers (&v_old_aux, &v_new);
ApplyBCs(lines, d, grid, t_now, dir1);
/**********************************
(a.1) Explicit update sweeping DIR1
**********************************/
ExplicitUpdate (v_aux, v_old_aux, NULL, H1p, H1m, C1, &lines[dir1],
lines[dir1].lbound[diff], lines[dir1].rbound[diff],
(diff == TDIFF) && EN_CONS_CHECK, &en_tc_in, grid,
fract*dts, dir1);
#if (JOULE_EFFECT_AND_MAG_ENG && POW_INSIDE_ADI)
if (diff == BDIFF) {
// [Err] Decomment next line
// [Opt] You could modify and make that the ResEnergyEncrease automatically updates a Ures variable,
// instead of doing it a line later
ResEnergyIncrease(dUres_aux, H1p, H1m, v_old_aux, grid, &lines[dir1],
EN_CONS_CHECK, &en_res_in,
fract*dts, dir1);
LINES_LOOP(lines[IDIR], l, j, i)
dUres[j][i] = dUres_aux[j][i];
}
#endif
#ifdef DEBUG_EMA
printf("\nafter expl dir1:\n");
printf("\nv_old_aux\n");
printmat(v_old_aux, NX2_TOT, NX1_TOT);
printf("\nv_aux\n");
printmat(v_aux, NX2_TOT, NX1_TOT);
#if (JOULE_EFFECT_AND_MAG_ENG && POW_INSIDE_ADI)
printf("\ndUres_aux\n");
printmat(dUres_aux, NX2_TOT, NX1_TOT);
#endif
#endif
/**********************************
(a.2) Implicit update sweeping DIR2
**********************************/
ApplyBCs(lines, d, grid, t_now + dts*(1-fract), dir2);
ImplicitUpdate (v_new, v_aux, NULL, H2p, H2m, C2, &lines[dir2],
lines[dir2].lbound[diff], lines[dir2].rbound[diff],
(diff == TDIFF) && EN_CONS_CHECK, &en_tc_in, grid,
(1-fract)*dts, dir2);
#if (JOULE_EFFECT_AND_MAG_ENG && POW_INSIDE_ADI)
if (diff == BDIFF) {
// [Err] Decomment next line
ResEnergyIncrease(dUres_aux, H2p, H2m, v_new, grid, &lines[dir2],
EN_CONS_CHECK, &en_res_in,
(1-fract)*dts, dir2);
LINES_LOOP(lines[IDIR], l, j, i)
dUres[j][i] += dUres_aux[j][i];
}
#endif
#ifdef DEBUG_EMA
printf("\nafter impl dir2:\n");
printf("\nv_new\n");
printmat(v_new, NX2_TOT, NX1_TOT);
#if (JOULE_EFFECT_AND_MAG_ENG && POW_INSIDE_ADI)
printf("\ndUres_aux\n");
printmat(dUres_aux, NX2_TOT, NX1_TOT);
#endif
#endif
/**********************************
(b.1) Explicit update sweeping DIR2
**********************************/
ExplicitUpdate (v_aux, v_new, NULL, H2p, H2m, C2, &lines[dir2],
lines[dir2].lbound[diff], lines[dir2].rbound[diff],
(diff == TDIFF) && EN_CONS_CHECK, &en_tc_in, grid,
fract*dts, dir2);
#if (JOULE_EFFECT_AND_MAG_ENG && POW_INSIDE_ADI)
if (diff == BDIFF) {
/* [Opt]: I could inglobate this call to ResEnergyIncrease in the previous one by using dt_res_reduced instead of 0.5*dt_res_reduced
(but in this way it is more readable)*/
// [Err] Decomment next line
ResEnergyIncrease(dUres_aux, H2p, H2m, v_new, grid, &lines[dir2],
EN_CONS_CHECK, &en_res_in,
fract*dts, dir2);
LINES_LOOP(lines[IDIR], l, j, i)
dUres[j][i] += dUres_aux[j][i];
}
#endif
#ifdef DEBUG_EMA
printf("\nafter expl dir2:\n");
printf("\nv_new\n");
printmat(v_new, NX2_TOT, NX1_TOT);
printf("\nv_aux\n");
printmat(v_aux, NX2_TOT, NX1_TOT);
#if (JOULE_EFFECT_AND_MAG_ENG && POW_INSIDE_ADI)
printf("\ndUres_aux\n");
printmat(dUres_aux, NX2_TOT, NX1_TOT);
#endif
#endif
/**********************************
(b.2) Implicit update sweeping DIR1
**********************************/
ApplyBCs(lines, d, grid, t_now + dts, dir1);
ImplicitUpdate (v_new, v_aux, NULL, H1p, H1m, C1, &lines[dir1],
lines[dir1].lbound[diff], lines[dir1].rbound[diff],
(diff == TDIFF) && EN_CONS_CHECK, &en_tc_in, grid,
(1-fract)*dts, dir1);
#if (JOULE_EFFECT_AND_MAG_ENG && POW_INSIDE_ADI)
if (diff == BDIFF) {
// [Err] Decomment next line
ResEnergyIncrease(dUres_aux, H1p, H1m, v_new, grid, &lines[dir1],
EN_CONS_CHECK, &en_res_in,
(1-fract)*dts, dir1);
LINES_LOOP(lines[IDIR], l, j, i)
dUres[j][i] += dUres_aux[j][i];
}
#endif
#if (JOULE_EFFECT_AND_MAG_ENG && !POW_INSIDE_ADI)
if (diff == BDIFF) {
// I average Br
LINES_LOOP_EXTENDED(lines[IDIR], l, j, i) {
Br_avg[j][i] = sqrt((v_new[j][i]*v_new[j][i] + v_new[j][i]*v_old[j][i] + v_old[j][i]*v_old[j][i])/3);
}
// I compute the fluxes of poynting vector
ResEnergyIncrease(dUres_aux, H2p, H2m, Br_avg, grid, &lines[dir2],
EN_CONS_CHECK, &en_res_in,
dts, dir2);
ResEnergyIncrease(dUres_aux1, H1p, H1m, Br_avg, grid, &lines[dir1],
EN_CONS_CHECK, &en_res_in,
dts, dir1);
// I update the increase in energy
LINES_LOOP(lines[IDIR], l, j, i) {
dUres[j][i] = dUres_aux[j][i];
dUres[j][i] += dUres_aux1[j][i];
}
}
#endif
#ifdef DEBUG_EMA
printf("\nafter impl dir1:\n");
printf("\nv_new\n");
printmat(v_new, NX2_TOT, NX1_TOT);
#if (JOULE_EFFECT_AND_MAG_ENG && POW_INSIDE_ADI)
printf("\ndUres_aux\n");
printmat(dUres_aux, NX2_TOT, NX1_TOT);
#endif
#endif
t_now += dts;
}
/* If necessary, I copy the final result to the correct memory address,
to get it available outside.
THe reason why I have to do this is that I mixed up the addresses calling:
"SwapDoublePointers (&v_old_aux, &v_new);", but this exchange of addresses
cannot be seen from outside the functin, as in C parameters are passed
to functinos by value!*/
if (v_new_save != v_new) {
LINES_LOOP(lines[IDIR], l, j, i)
v_new_save[j][i] = v_new[j][i];
}
if (fabs((t_now-t0) - dt)/dt > DT_REL_TOLL) {
print1("\nInaccurate dt, actual dt performed: %le, desired: %le\n", t_now-t0, dt);
}
}
/* ***********************************************************
* Douglas-Rachford ADI method
*
* input: diff = BDIFF or TDIFF
* int order = FIRST_IDIR or FIRST_JDIR: tells whether the order of the directions
* must be IDIR, JDIR (FIRST_IDIR) or JDIR, IDIR (FIRST_JDIR).
* **dEdT: may point to NULL in case diff == BDIFF
* **dUres: will not be updated if diff != BDIFF
* (it is my opinion that for the sake of clarity it is better not to
* "merge" in one single variable the quantities **dEdT and **dUres,
* even though I never need both of them at the same time)
* ***********************************************************/
void DouglasRachford (double **v_new, double **v_old,
double **dUres, double **dEdT,
const Data *d, Grid *grid,
Lines *lines, int diff, int order,
double dt, double t0, int M, int recompute_operators) {
static double **v_aux, **v_hat, **v_old_aux; // auxiliary solution vectors
#if RESISTIVITY==ALTERNATING_DIRECTION_IMPLICIT
static double **IpB, **ImB, **CIB, **JpB, **JmB, **CJB;
#endif
#if THERMAL_CONDUCTION==ALTERNATING_DIRECTION_IMPLICIT
static double **IpT, **ImT, **CIT, **JpT, **JmT, **CJT;
#endif
static int first_call = 1;
double **H1p, **H1m, **H2p, **H2m, **C1, **C2;
// void (*BoundaryADI) (Lines, const Data, Grid, double);
BoundaryADI *ApplyBCs;
int dir1, dir2;
int l,i,j,s;
double dts;
double t_now;
#if (JOULE_EFFECT_AND_MAG_ENG)
static double **dUres_aux; // auxiliary vector containing a contribution to ohmic heating
#endif
/*
print1("\nAttenzione al calcolo dell'energia che entra dai bordi per conduzione/elettromagnetica:\n");
print1("\npotrebbe essere che sia sbagliata per come ho implmentato lo schema D-R (e per l'uso di variabili globali)\n");
*/
if (first_call) {
v_aux = ARRAY_2D(NX2_TOT, NX1_TOT, double);
v_hat = ARRAY_2D(NX2_TOT, NX1_TOT, double);
v_old_aux = ARRAY_2D(NX2_TOT, NX1_TOT, double);
#if (JOULE_EFFECT_AND_MAG_ENG)
dUres_aux = ARRAY_2D(NX2_TOT, NX1_TOT, double);
#endif