-
Notifications
You must be signed in to change notification settings - Fork 6
/
Advection_diffusion.c
1415 lines (1109 loc) · 41.4 KB
/
Advection_diffusion.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
/*
Copyright (C) 2003 The GeoFramework Consortium
This file is part of Ellipsis3D.
Ellipsis3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2,
as published by the Free Software Foundation.
Ellipsis3D 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.
Authors:
Louis Moresi <[email protected]>
Richard Albert <[email protected]>
*/
/* Functions which solve the heat transport equations using Petrov-Galerkin
streamline-upwind methods. The process is basically as described in Alex
Brooks PhD thesis (Caltech) which refers back to Hughes, Liu and Brooks.
*/
#include "config.h"
#include <math.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include "element_definitions.h"
#include "global_defs.h"
#include "function_prototypes.h"
extern int Emergency_stop;
/*struct el { higher_precision gpt[9]; };*/ /*RAA: structure is not used*/
/* ============================================
Generic adv-diffusion for temperature field.
============================================ */
void advection_diffusion_parameters (
struct All_variables *E
)
{
int i;
/* Set intial values, defaults & read parameters*/
E->advection.timesteps = 0;
E->advection.temp_iterations = 2; /* petrov-galerkin iterations: minimum value. */
E->advection.total_timesteps = 1;
E->advection.sub_iterations = 1;
E->advection.last_sub_iterations = 1;
E->advection.gamma = 0.5;
input_boolean("ADV",&(E->advection.ADVECTION),"on");
input_int("minstep",&(E->advection.min_timesteps),"1");
input_int("maxstep",&(E->advection.max_timesteps),"1000");
input_int("maxtotstep",&(E->advection.max_total_timesteps),"1000000");
input_std_precision("finetunedt",&(E->advection.fine_tune_dt),"0.9");
input_std_precision("fixed_timestep",&(E->advection.fixed_timestep),"0.0");
input_std_precision("elastic_timestep",&(E->advection.elastic_timestep),"0.0");
input_int("adv_sub_iterations",&(E->advection.temp_iterations),"2,2,nomax");
input_std_precision("maxadvtime",&(E->advection.max_elapsed_time),"1.0e18");
/* Set an initial timestep for use in elasticity
calculations */
/*E->advection.timestep=1.0e-16 + E->advection.fixed_timestep;*/
if(E->advection.fixed_timestep == 0.0)
E->advection.timestep = 1.e-16 ;
else
E->advection.timestep = E->advection.fixed_timestep ;
if(E->control.ELASTICITY && E->advection.elastic_timestep <= 0.0) {
fprintf(stderr,"Warning, no elastic timestep specified, turning off elasticity option\n");
fprintf(E->fp,"Warning, no elastic timestep specified, turning off elasticity option\n");
E->control.ELASTICITY = 0;
}
return;
}
void advection_diffusion_allocate_memory
(
struct All_variables *E
)
{
int i;
E->Tdot = (standard_precision *)Malloc0((E->mesh.nno+1)*sizeof(standard_precision));
for(i=1;i<=E->mesh.nno;i++)
E->Tdot[i]=0.0;
return;
}
void PG_timestep(
struct All_variables *E
)
{
void predictor();
void corrector();
void std_timestep();
void temperatures_conform_bcs();
int get_eq_phase();
int get_v_estimate();
int i,j,psc_pass,count,steps,m;
int keep_going,previous_phase,metastable;
struct SOURCES Qnone;
standard_precision *DTdot,*Fold,*dF,vsselfdot();
standard_precision *mem1,*mem2;
standard_precision deltaF,Fmag,Fmag2;
standard_precision *DSTNdot;
standard_precision CPU_time(),time;
standard_precision ph_b_distance;
static int loops_since_new_eta = 0;
static int been_here = 0;
const int dims = E->mesh.nsd;
Qnone.number=0;
DTdot = (standard_precision *)Malloc0(E->mesh.fnodal_malloc_size);
DSTNdot = (standard_precision *)Malloc0(E->mesh.fnodal_malloc_size);
if(E->control.verbose)
fprintf(stderr,"Advection\n");
if (been_here++ ==0)
E->advection.timesteps=0;
/* place for stress updating in explicit scheme */
/* fprintf(stderr,"HERE IS TRACER.PT (13) %g \n",E->tracer.Pt[436]) ; */
if(E->control.ELASTICITY || E->control.SHEAR_HEATING) {
if(E->control.verbose)
fprintf(stderr,"Update stress history \n");
stress_update(E);
}
/* fprintf(stderr,"HERE IS TRACER.PT (14) %g \n",E->tracer.Pt[436]) ; */
/* The positions to be calculated actually reflect the beginning of the next timestep. */
E->advection.timesteps++;
fprintf(stderr,"CONE: Going into std_timestep %g \n",E->advection.timestep);
E->advection.previous_timestep_2 = E->advection.previous_timestep + E->advection.timestep;
E->advection.previous_timestep = E->advection.timestep;
std_timestep(E);
fprintf(stderr,"CONE: Out of std_timestep %g \n",E->advection.timestep);
time=CPU_time();
/* keep_going = get_v_estimate(E); */
/* Diffusion (thermal) */
#if 1
for(count=1;count<=E->advection.diff_ratio;count++) {
predictor(E,E->T,E->Tdot,E->node,OFFSIDE | TBD);
for(psc_pass=0;psc_pass<E->advection.temp_iterations;psc_pass++) {
pg_solver(E,E->T,E->Tdot,DTdot,E->V,E->convection.heat_sources,1.0,1,E->TB,
E->node,OFFSIDE | TBD, FBZ);
corrector(E,E->T,E->Tdot,DTdot,E->node,OFFSIDE | TBD);
}
if(E->control.verbose)
fprintf(stderr,"Sub timestep %d - %g / %g\n",count,E->advection.timestep_diff,E->advection.timestep_adv);
}
#endif
fprintf(stderr,"TIME UPDATED HERE: monitor_time: %g adv_time: %g \n",E->monitor.elapsed_time,E->advection.timestep);
E->advection.total_timesteps++; /* i.e. the computed location will apply to the next timestep */
E->monitor.elapsed_time += E->advection.timestep;
count++;
fprintf(stderr,"TIME UPDATE DONE: monitor_time: %g adv_time: %g \n",E->monitor.elapsed_time,E->advection.timestep);
if(E->mesh.periodic_x || E->mesh.periodic_y) {
flogical_mesh_to_real(E,E->T,E->mesh.levmax);
flogical_mesh_to_real(E,E->Tdot,E->mesh.levmax);
}
/* TRACER ADVECTION ... */
/* Update volumetric strain at tracers etc.*/
/*fprintf(stderr,"HERE IS TRACER.PT (0) %g \n",E->tracer.Pt[436]) ; */
if(E->control.verbose)
fprintf(stderr,"Particle based pressure\n");
tracer_time_dep_terms(E);
for(i=1;i<=E->tracer.NUM_TRACERS;i++) {
E->tracer.UU1[i] = E->tracer.U1[i];
E->tracer.UU2[i] = E->tracer.U2[i];
if(3==dims) /*RAA: 04/04/02, added this part*/
E->tracer.UU3[i] = E->tracer.U3[i];
}
/*RAA:5/4/01, nodes_to_tracers call for 3D added below*/
nodes_to_tracers(E,E->V[1],E->tracer.U1,E->mesh.levmax);
nodes_to_tracers(E,E->V[2],E->tracer.U2,E->mesh.levmax);
if(3==dims)
nodes_to_tracers(E,E->V[3],E->tracer.U3,E->mesh.levmax);
#if defined (CHEM_TRANS_MODULE_INSTALLED)
if(E->control.CHEM_TRANS) {
if(2==dims) { /*RAA: added this distinction*/
update_sigma_n(E);
update_porosity(E);
}
else if(3==dims) { /*RAA: added this bit*/
fprintf(stderr,"3D chem transport not properly coded. Adios, muchacho.\n");
exit(1);
}
}
#endif
/* Advection of tracers */
if(E->control.verbose)
fprintf(stderr,"Advection of tracers \n");
tracer_advection(E,E->advection.timestep);
passive_tracer_advection(E,E->advection.timestep);
if(E->control.verbose)
fprintf(stderr,"Particle phase stuff\n");
/* Compute which PHASE each tracer is in */
for(m=1;m<=E->tracer.NUM_TRACERS;m++) {
if(E->tracer.Phases[E->tracer.property_group[m]] <= 1)
continue;
/* 1. Before updating, is the tracer in metastable state ? */
if(E->tracer.Current_phase[m] != E->tracer.Equilibrium_phase[m])
metastable = 1;
else
metastable = 0;
/* 2. Now find new equilibrium phase, and store previous phase */
E->tracer.Equilibrium_phase[m] = get_eq_phase(E,m);
if(E->monitor.solution_cycles == 1)
previous_phase = E->tracer.Equilibrium_phase[m];
else
previous_phase = E->tracer.Current_phase[m];
/* 3. If particle can change phase, allow it to do so */
if(((E->monitor.solution_cycles == 1) || /* No knowledge of previous phase if first step */
(E->tracer.T[m] >
E->tracer.TBlock[E->tracer.property_group[m]*MAX_MATERIAL_PHASES+E->tracer.Current_phase[m]]))) {
E->tracer.Current_phase[m] = E->tracer.Equilibrium_phase[m];
}
/* 4. If particle changes, reset this clock */
if(previous_phase != E->tracer.Current_phase[m])
E->tracer.time_since_phase_change[m] = 0.0;
else
E->tracer.time_since_phase_change[m] += E->advection.timestep;
/* 5. Update grainsize for particles crossing boundary ... depends
on whether they cross equilibrium or metastable boundary */
if(E->viscosity.GRAINSIZE)
phase_boundary_grain_size(E,m,metastable);
}
if(E->viscosity.GRAINSIZE)
grow_grains(E);
if(E->control.ORTHOTROPY)
rotate_director(E);
/* Estimate new velocity solution */
/* get_v_estimate(E); */
temperatures_conform_bcs(E,E->T);
nodes_to_tracers(E,E->T,E->tracer.T,E->mesh.levmax);
/* Adjust mesh if required and resize elements */
mesh_update(E,E->advection.timestep,E->monitor.elapsed_time);
/* Update tracer element information */
get_tracer_elts_and_weights(E,1,E->tracer.NUM_TRACERS);
E->advection.last_sub_iterations = count;
if(((E->advection.total_timesteps < E->advection.max_total_timesteps) &&
(E->advection.timesteps < E->advection.max_timesteps) &&
(E->monitor.elapsed_time < E->advection.max_elapsed_time) ) ||
(E->advection.total_timesteps < E->advection.min_timesteps) )
E->control.keep_going = 1;
else {
E->control.keep_going = 0;
if(E->control.verbose) {
if(E->advection.total_timesteps >= E->advection.max_total_timesteps)
fprintf(stderr, "DAS: ** maxtotstep reached, stopping.\n");
else if(E->advection.timesteps >= E->advection.max_timesteps)
fprintf(stderr, "DAS: ** maxstep reached, stopping.\n");
else if(E->monitor.elapsed_time >= E->advection.max_elapsed_time)
fprintf(stderr, "DAS: ** maxadvtime reached, stopping.\n");
else
fprintf(stderr, "DAS: ** ARGH! Something unusual just happened!\n");
}
}
if(E->control.verbose)
fprintf(stderr,"Advection ... done\n");
free((void *) DTdot);
free((void *) DSTNdot); /* free memory for vel solver */
return;
}
/* Update nodal values of temperature using
the earlier positions of the nodes */
void nodal_semi_lagrange_advection(
struct All_variables *E,
standard_precision *T,
standard_precision *T1,
standard_precision timestep
) /*RAA: function argument syntax corrected on 4/4/01 */
{
int node,el;
int i,j,k;
standard_precision nodal_interpolated_value();
standard_precision vx1,vz1,vy1;
standard_precision vx2,vz2,vy2;
standard_precision xx1,zz1,yy1;
standard_precision xx2,zz2,yy2;
standard_precision xx3,zz3,yy3;
standard_precision kx1,kz1,ky1;
standard_precision kx2,kz2,ky2;
/* For each nodal point, find the advected position */
for(node=1;node<=E->mesh.nno;node++) {
vx1 = E->V[1][node];
vz1 = E->V[2][node];
xx1 = E->x[1][node];
zz1 = E->x[2][node];
kx1 = timestep * vx1;
kz1 = timestep * vz1;
/* Midpt ... now get its velocity */
xx2 = xx1 + kx1 * 0.5;
zz2 = zz1 + kz1 * 0.5;
vx2 = nodal_interpolated_value(E,E->V[1],xx2,zz2,NULL);
vz2 = nodal_interpolated_value(E,E->V[2],xx2,zz2,NULL);
kx2 = timestep * vx2;
kz2 = timestep * vz2;
/* Endpt ... now get its temperature */
xx3 = xx1 + kx2;
zz3 = zz1 + kz2;
T1[node] = nodal_interpolated_value(E,T,xx3,zz3,NULL);
/* fprintf(stderr,"Node %d (%g,%g) used to be at %g,%g where T = %g\n",
node,xx1,zz1,xx3,zz3,T1[node]); */
}
}
/* ==============================
predictor and corrector steps.
============================== */
void predictor(
struct All_variables *E,
standard_precision *field,
standard_precision *fielddot,
int *INFO,
int MASK
)
{
int node;
standard_precision multiplier;
multiplier = (1.0-E->advection.gamma) * E->advection.timestep_diff;
for(node=1;node<=E->mesh.nno;node++) {
if(INFO != (int *) NULL) {
if( !(INFO[node] & MASK))
field[node] += multiplier * fielddot[node] ;
}
else
field[node] += multiplier * fielddot[node] ;
fielddot[node] = 0.0;
}
return;
}
void corrector(
struct All_variables *E,
standard_precision *field,
standard_precision *fielddot,
standard_precision *Dfielddot,
int * INFO,
int MASK
)
{
int node;
standard_precision multiplier;
multiplier = E->advection.gamma * E->advection.timestep_diff;
for(node=1;node<=E->mesh.nno;node++) {
if(INFO != (int *) NULL) {
if( !(INFO[node] & MASK))
field[node] += multiplier * Dfielddot[node];
}
else
field[node] += multiplier * Dfielddot[node];
fielddot[node] += Dfielddot[node];
}
return;
}
/* ===================================================
The solution step -- determine residual vector from
advective-diffusive terms and solve for delta Tdot
Two versions are available -- one for Cray-style
vector optimizations etc and one optimized for
workstations.
=================================================== */
void pg_solver(
struct All_variables *E,
standard_precision *T,
standard_precision *Tdot,
standard_precision *DTdot,
standard_precision **V,
struct SOURCES Q0,
standard_precision diff,
int bc,
standard_precision *TBC,
unsigned int *FLAGS,
unsigned int OFFSIDE_MASK,
unsigned int FLUX_MASK
)
{
void get_global_shape_fn();
void pg_element_residual_t();
int el,a,i,a1;
higher_precision Eres[9]; /* correction to the (scalar) Tdot field */
/*RAA: 12/07/02, 3 new variables here from C. Wijns advection fix*/
standard_precision dist,nodeV;
standard_precision *realV[4];
standard_precision vel0,vel1; /*RAA: 23/9/02, 2 new variables here*/
standard_precision find_section_of_vel_fn(); /* RAA: 20/09/02, new function*/
struct Shape_function GN;
struct Shape_function_dA dOmega;
struct Shape_function_dx GNx;
const int dims=E->mesh.nsd;
const int ends=enodes[dims];
/*RAA - as per C.W. - we don't need to worry about rotational components*/
for(i=1;i<=dims;i++)
realV[i] = (standard_precision *)Malloc0((E->mesh.nno+1)*sizeof(standard_precision));
for(i=1;i<=E->mesh.nno;i++)
DTdot[i] = 0.0;
/*RAA (C.W.) Calculate a true nodal advection velocity subtracting the (constant) mesh velocity,
w/ time_interval=off & w/ stationary walls, nodeVs will be 0, so this should be fine*/
if(!E->mesh.BCvelocity_time_interval) {
for(i=1;i<=E->mesh.nno;i++) {
/* X-velocity*/
dist = (E->x[1][i] - E->mesh.layer0[1]) / (E->mesh.layer1[1] - E->mesh.layer0[1]);
nodeV = E->mesh.BCvelocityX0 + (E->mesh.BCvelocityX1 - E->mesh.BCvelocityX0) * dist;
realV[1][i] = V[1][i] - nodeV;
/* Z-velocity*/
dist = (E->x[2][i] - E->mesh.layer0[2]) / (E->mesh.layer1[2] - E->mesh.layer0[2]);
nodeV = E->mesh.BCvelocityZ0 + (E->mesh.BCvelocityZ1 - E->mesh.BCvelocityZ0) * dist;
realV[2][i] = V[2][i] - nodeV;
/* Y-velocity*/
if (dims==3) {
dist = (E->x[3][i] - E->mesh.layer0[3]) / (E->mesh.layer1[3] - E->mesh.layer0[3]);
nodeV = E->mesh.BCvelocityY0 + (E->mesh.BCvelocityY1 - E->mesh.BCvelocityY0) * dist;
realV[3][i] = V[3][i] - nodeV;
}
}
}
#if 1
/*--------------------------------------*/
else if(E->mesh.BCvelocity_time_interval) { /*RAA: vel bcs depend linearly on time*/
for(i=1;i<=E->mesh.nno;i++) {
vel0 = 0.0;
vel1 = 0.0;
/* X-velocity*/
dist = (E->x[1][i] - E->mesh.layer0[1]) / (E->mesh.layer1[1] - E->mesh.layer0[1]);
find_section_of_vel_fn(E,1,E->monitor.elapsed_time,&vel0,&vel1);
/*RAA: has the info below already been updated??? apparently not*/
/* nodeV = E->mesh.BCvelocityX0 + (E->mesh.BCvelocityX1 - E->mesh.BCvelocityX0) * dist; */
nodeV = vel0 + (vel1 - vel0) * dist;
realV[1][i] = V[1][i] - nodeV;
/* Z-velocity*/
dist = (E->x[2][i] - E->mesh.layer0[2]) / (E->mesh.layer1[2] - E->mesh.layer0[2]);
find_section_of_vel_fn(E,2,E->monitor.elapsed_time,&vel0,&vel1);
nodeV = vel0 + (vel1 - vel0) * dist;
realV[2][i] = V[2][i] - nodeV;
/* Y-velocity*/
if (dims==3) {
dist = (E->x[3][i] - E->mesh.layer0[3]) / (E->mesh.layer1[3] - E->mesh.layer0[3]);
find_section_of_vel_fn(E,3,E->monitor.elapsed_time,&vel0,&vel1);
nodeV = vel0 + (vel1 - vel0) * dist;
/* fprintf(E->fp1,"nodeV vel0, vel1, dist %g %g %g %g\n",nodeV,vel0,vel1,dist); */
realV[3][i] = V[3][i] - nodeV;
}
}
}
/*--------------------------------------*/
#endif
for(el=1;el<=E->mesh.nel;el++) {
get_global_shape_fn(E,el,&GN,&GNx,&dOmega,0,E->mesh.levmax);
/*RAA - here's the proper call to pg_element_residual w/ realV, as per C.W.'s bug fix */
pg_element_residual(E,el,GNx,dOmega,realV,T,Tdot,Q0,Eres,1.0,TBC,FLAGS,OFFSIDE_MASK,FLUX_MASK);
for(a=1;a<=ends;a++) {
a1 = E->ien[el].node[a];
DTdot[a1] += Eres[a];
}
} /* next element */
for(i=1;i<=E->mesh.nno;i++) {
if(E->node[i] & ( OFFSIDE ))
continue;
DTdot[i] /= E->mass[i]; /* lumped mass matrix */
}
for(i=1;i<=dims;i++) {
free((void *) realV[i]);
}
printf("Finished pg_solver, free'd up realV \n");
return;
}
/* ==========================================
Residual force vector from heat-transport.
Used to correct the Tdot term. Calculated
using Petrov-Galerkin Shape functions.
========================================= */
void pg_element_residual(
struct All_variables *E,
int el,
struct Shape_function_dx GNx,
struct Shape_function_dA dOmega,
standard_precision **vel,
standard_precision *TT1,
standard_precision *TT1dot,
struct SOURCES QQ1,
higher_precision Eres1[9],
standard_precision diff1,
standard_precision *BCC,
unsigned int *FLAGS1,
int OFFSIDE_MASK1,
int FLUX_MASK1
)
{
int i,j,a,k,m,node,nodes[5],d,aid,back_front,onedfns;
higher_precision v1[9],v2[9],v3[9];
higher_precision uc1,uc2,uc3;
higher_precision size1,size2,size3;
higher_precision T1xsi1,T1xsi2,T1xsi3;
higher_precision T1ah1,T1ah2,T1ah3;
higher_precision Q1,Qi[9];
higher_precision dT1[9];
higher_precision T1x1[9],T1x2[9],T1x3[9];
higher_precision T1,DT1,Ti;
higher_precision Vsum;
higher_precision T1xsisum,vT1sum;
higher_precision unorm;
higher_precision sfn,pgsfn;
struct Shape_function1 GM;
struct Shape_function1_dA dGamma;
standard_precision grain_growth_source_term();
standard_precision diff1_1,QtE,vol;
void get_global_1d_shape_fn();
const int dims=E->mesh.nsd;
const int ends=enodes[dims];
const int vpts=vpoints[dims];
const higher_precision recipends=1.0/(higher_precision)ends;
/* Since we're only using this for Temperature
these days, not chemistry or anything non-diffusive */
diff1=0.0;
QtE=0.0;
vol = 0.0;
for(j=0;j<E->tracer.tr_in_element_number[E->mesh.levmax][el];j++) {
m = E->tracer.tr_in_element[E->mesh.levmax][j+E->tracer.tr_in_element_offset[E->mesh.levmax][el]];
diff1 += E->tracer.Therm_diff[E->tracer.property_group[m]] *
E->tracer.tracer_weight_fn[E->mesh.levmax][m];
QtE += E->tracer.Qt[E->tracer.property_group[m]] *
E->tracer.tracer_weight_fn[E->mesh.levmax][m] / /* If Q is supplied as W/kg */
(E->tracer.Cp[E->tracer.property_group[m]]);
if(E->control.SHEAR_HEATING) {
/* fprintf(stderr,"Adding shear heating of %g for tracer %d\n",E->tracer.Hvisc[m],m); */
QtE += E->tracer.Hvisc[m] * E->tracer.tracer_weight_fn[E->mesh.levmax][m];
}
/*RAA: 09/07/03 - Latent heat of melting stuff - next 2 lines from Craig's 2D routine*/
QtE -= ((E->tracer.Latent_heat[E->tracer.property_group[m]] *E->tracer.dFdot[m]* E->tracer.T[m] *
E->tracer.tracer_weight_fn[E->mesh.levmax][m]) / (E->tracer.Cp[E->tracer.property_group[m]]));
vol += E->tracer.tracer_weight_fn[E->mesh.levmax][m];
}
if(vol != 0.0) {
diff1 /= vol;
QtE /= vol;
}
diff1_1 = 1.0 / (diff1 + 1.0e-32);
/* And this bit is unchanged */
for(i=1;i<=vpts;i++) {
dT1[i]=0.0;
v1[i] = T1x1[i]= 0.0;
v2[i] = T1x2[i]= 0.0;
v3[i] = T1x3[i]= 0.0;
}
uc1 = uc2 = uc3 = 0.0;
size1 = (higher_precision) E->eco[el].ntl_size[1];
size2 = (higher_precision) E->eco[el].ntl_size[2];
size3 = (higher_precision) E->eco[el].ntl_size[3];
Q1=0.0;
for(i=0;i<QQ1.number;i++)
Q1 += QQ1.Q[i] * exp(-QQ1.lambda[i] * (E->monitor.elapsed_time+QQ1.t_offset));
for(i=1;i<=vpts;i++)
Qi[i] = Q1;
/* Track to see if nothing to advect */
for(j=1;j<=ends;j++) {
Eres1[j] = 0.0;
node = E->ien[el].node[j];
T1 = TT1[node];
if(fabs(T1) < 1.0e-16)
T1 = 0.0;
if(FLAGS1[node] & (OFFSIDE_MASK1))
DT1=0.0;
else
DT1 = TT1dot[node];
if(3==dims) { /* Central velocity is expressed in natural coordinates and
used to construct the PG shape functions. Elsewhere the
cartesian velocity field is used */
uc1 += (E->eco[el].ntl_dirns[1][1] * vel[1][node] +
E->eco[el].ntl_dirns[1][2] * vel[2][node] +
E->eco[el].ntl_dirns[1][3] * vel[3][node] ) ;
uc2 += (E->eco[el].ntl_dirns[2][1] * vel[1][node] +
E->eco[el].ntl_dirns[2][2] * vel[2][node] +
E->eco[el].ntl_dirns[2][3] * vel[3][node] ) ;
uc3 += (E->eco[el].ntl_dirns[3][1] * vel[1][node] +
E->eco[el].ntl_dirns[3][2] * vel[2][node] +
E->eco[el].ntl_dirns[3][3] * vel[3][node] ) ;
Ti=0.0;
for(i=1;i<=vpts;i++) {
sfn = E->N.vpt[GNVINDEX(j,i)];
dT1[i] += DT1 * sfn;
v1[i] += vel[1][node] * sfn;
v2[i] += vel[2][node] * sfn;
v3[i] += vel[3][node] * sfn;
T1x1[i] += GNx.vpt[GNVXINDEX(0,j,i)] * T1;
T1x2[i] += GNx.vpt[GNVXINDEX(1,j,i)] * T1;
T1x3[i] += GNx.vpt[GNVXINDEX(2,j,i)] * T1;
Ti += sfn * T1;
if(E->advection_source_term != NULL)
Qi[i]=(E->advection_source_term)(E,el,i,TT1,v1[i],v2[i],v3[i],Ti);
}
}
else /* 2==dims */ {
uc1 += (E->eco[el].ntl_dirns[1][1] * vel[1][node] +
E->eco[el].ntl_dirns[1][2] * vel[2][node] ) ;
uc2 += (E->eco[el].ntl_dirns[2][1] * vel[1][node] +
E->eco[el].ntl_dirns[2][2] * vel[2][node] ) ;
Ti=0.0;
for(i=1;i<=vpts;i++) {
sfn = E->N.vpt[GNVINDEX(j,i)];
dT1[i] += DT1 * sfn;
v1[i] += vel[1][node] * sfn;
v2[i] += vel[2][node] * sfn;
Ti += sfn * T1;
T1x1[i] += GNx.vpt[GNVXINDEX(0,j,i)] * T1;
T1x2[i] += GNx.vpt[GNVXINDEX(1,j,i)] * T1;
if(E->advection_source_term != NULL)
Qi[i]=(E->advection_source_term)(E,el,i,TT1,v1[i],v2[i],0.0,Ti);
}
}
}
/* Construct PG shape functions */
if(3==dims) {
if (diff1 != 0.0) {
T1ah1 = fabs(uc1*recipends) * size1 * diff1_1;
T1ah2 = fabs(uc2*recipends) * size2 * diff1_1;
T1ah3 = fabs(uc3*recipends) * size3 * diff1_1;
T1xsi1 = (T1ah1 > 1.94) ? T1ah1 - 1.0 : 0.3333333*T1ah1*T1ah1*(1.0-T1ah1*T1ah1*0.06666667);
T1xsi2 = (T1ah2 > 1.94) ? T1ah2 - 1.0 : 0.3333333*T1ah2*T1ah2*(1.0-T1ah2*T1ah2*0.06666667);
T1xsi3 = (T1ah3 > 1.94) ? T1ah3 - 1.0 : 0.3333333*T1ah3*T1ah3*(1.0-T1ah3*T1ah3*0.06666667);
T1xsisum = diff1 * (T1xsi1 + T1xsi2 + T1xsi3) * 0.518;
}
else {
T1ah1 = fabs(uc1*recipends) * size1;
T1ah2 = fabs(uc2*recipends) * size2;
T1ah3 = fabs(uc3*recipends) * size3;
T1xsisum = (T1ah1 + T1ah2 + T1ah3) * 0.518;
}
for(i=1;i<=VPOINTS3D;i++) {
unorm = 1.0 / (v1[i] * v1[i] + v2[i] * v2[i] + v3[i] * v3[i] + 1.0e-32);
vT1sum = dT1[i] /*- Q1*/ + v1[i]*T1x1[i] + v2[i]*T1x2[i] + v3[i]*T1x3[i];
for(j=1;j<=ENODES3D;j++) {
Vsum = (v1[i] * GNx.vpt[GNVXINDEX(0,j,i)] +
v2[i] * GNx.vpt[GNVXINDEX(1,j,i)] +
v3[i] * GNx.vpt[GNVXINDEX(2,j,i)] ) ;
pgsfn = E->N.vpt[GNVINDEX(j,i)] + T1xsisum * Vsum * unorm;
Eres1[j] -= dOmega.vpt[i] * (pgsfn * vT1sum - E->N.vpt[GNVINDEX(j,i)] * QtE +
diff1 * (GNx.vpt[GNVXINDEX(0,j,i)]*T1x1[i] +
GNx.vpt[GNVXINDEX(1,j,i)]*T1x2[i] +
GNx.vpt[GNVXINDEX(2,j,i)]*T1x3[i] ) );
}
}
}
else /* 2==dims */ {
if (diff1 != 0.0) {
T1ah1 = fabs(uc1*recipends) * size1 * diff1_1;
T1ah2 = fabs(uc2*recipends) * size2 * diff1_1;
T1xsi1 = (T1ah1 > 1.94) ? T1ah1 - 1.0 : 0.3333333*T1ah1*T1ah1*(1.0-T1ah1*T1ah1*0.06666667);
T1xsi2 = (T1ah2 > 1.94) ? T1ah2 - 1.0 : 0.3333333*T1ah2*T1ah2*(1.0-T1ah2*T1ah2*0.06666667);
T1xsisum = diff1 * (T1xsi1 + T1xsi2) * 0.518;
}
else /* diff == 0.0 */ {
T1ah1 = fabs(uc1*recipends) * size1;
T1ah2 = fabs(uc2*recipends) * size2;
T1xsisum = (T1ah1 + T1ah2) * 0.518;
}
for(i=1;i<=VPOINTS2D;i++) {
unorm = 1.0/(v1[i] * v1[i] + v2[i] * v2[i] + 1.0e-32);
vT1sum = dT1[i] /* - Q1 */ + v1[i] * T1x1[i] + v2[i] * T1x2[i];
for(j=1;j<=ENODES2D;j++) {
Vsum = (v1[i] * GNx.vpt[GNVXINDEX(0,j,i)] +
v2[i] * GNx.vpt[GNVXINDEX(1,j,i)] ) ;
pgsfn = E->N.vpt[GNVINDEX(j,i)] + T1xsisum * Vsum * unorm;
/* Add to residual */
Eres1[j] -= dOmega.vpt[i] * (1.0 * pgsfn * vT1sum - E->N.vpt[GNVINDEX(j,i)] * QtE /*Qi[i]*/ +
diff1 * (GNx.vpt[GNVXINDEX(0,j,i)] * T1x1[i] + /* extra term for axi */
GNx.vpt[GNVXINDEX(1,j,i)] * T1x2[i] ));
}
}
}
/* include BC's for fluxes at (nominally horizontal) edges (X-Y plane) */
if(FLAGS1!=NULL) {
onedfns=0;
for(a=1;a<=ends;a++)
if (FLAGS1[E->ien[el].node[a]] & FLUX_MASK1) {
if (!onedfns++)
get_global_1d_shape_fn(E,el,&GM,&dGamma,E->mesh.levmax);
nodes[1] = loc[loc[a].node_nebrs[0][0]].node_nebrs[2][0];
nodes[2] = loc[loc[a].node_nebrs[0][1]].node_nebrs[2][0];
nodes[4] = loc[loc[a].node_nebrs[0][0]].node_nebrs[2][1];
nodes[3] = loc[loc[a].node_nebrs[0][1]].node_nebrs[2][1];
for(aid=0,j=1;j<=onedvpoints[E->mesh.nsd];j++)
if (a==nodes[j])
aid = j;
if(aid==0)
printf("%d: mixed up in pg-flux int: looking for %d\n",el,a);
if (loc[a].plus[1] != 0)
back_front = 0;
else back_front = dims;
for(j=1;j<=onedvpoints[dims];j++)
for(k=1;k<=onedvpoints[dims];k++)
Eres1[a] += dGamma.vpt[GMVGAMMA(1+back_front,j)] *
E->M.vpt[GMVINDEX(aid,j)] * g_1d[j].weight[dims-1] *
BCC[E->ien[el].node[a]] * E->M.vpt[GMVINDEX(k,j)];
}
}
/* fprintf(stderr,"El %d: %g, elt res time %g\n",el,diff1,CPU_time()-time); */
return;
}
/* ==========================================
Residual force vector from heat-transport.
Used to correct the Tdot term. Calculated
using Petrov-Galerking Shape functions.
========================================= */
void diff_element_residual(
struct All_variables *E,
int el,
struct Shape_function_dx GNx,
struct Shape_function_dA dOmega,
standard_precision *TT1,
standard_precision *TT1dot,
struct SOURCES QQ1,
higher_precision Eres1[9],
standard_precision diff1,
standard_precision *BCC,
unsigned int *FLAGS1,
int OFFSIDE_MASK1,
int FLUX_MASK1
)
{
int i,j,a,k,node,nodes[5],d,aid,back_front,onedfns;
higher_precision Q1,Qi[9];
higher_precision dT1[9];
higher_precision T1x1[9],T1x2[9],T1x3[9];
higher_precision T1,DT1,Ti;
higher_precision Vsum;
higher_precision T1xsisum,vT1sum;
higher_precision unorm;
higher_precision sfn,pgsfn;
struct Shape_function1 GM;
struct Shape_function1_dA dGamma;
standard_precision grain_growth_source_term();
void get_global_1d_shape_fn();
const int dims=E->mesh.nsd;
const int ends=enodes[dims];
const int vpts=vpoints[dims];
const higher_precision recipends=1.0/(higher_precision)ends;
const higher_precision diff1_1 = 1.0/(diff1+1.0e-32);
for(i=1;i<=vpts;i++) {
dT1[i]=0.0;
T1x1[i]= 0.0;
T1x2[i]= 0.0;
T1x3[i]= 0.0;
}
Q1=0.0;
for(i=0;i<QQ1.number;i++)
Q1 += QQ1.Q[i] * exp(-QQ1.lambda[i] * (E->monitor.elapsed_time+QQ1.t_offset));
for(i=1;i<=vpts;i++)
Qi[i] = Q1;
for(j=1;j<=ends;j++) {
Eres1[j] = 0.0;
node = E->ien[el].node[j];
T1 = TT1[node];
if(FLAGS1[node] & (OFFSIDE_MASK1))
DT1=0.0;
else
DT1 = TT1dot[node];
if(3==dims) {
Ti=0.0;
for(i=1;i<=vpts;i++) {
sfn = E->N.vpt[GNVINDEX(j,i)];
dT1[i] += DT1 * sfn;
T1x1[i] += GNx.vpt[GNVXINDEX(0,j,i)] * T1;
T1x2[i] += GNx.vpt[GNVXINDEX(1,j,i)] * T1;
T1x3[i] += GNx.vpt[GNVXINDEX(2,j,i)] * T1;
Ti += sfn * T1;
if(E->advection_source_term != NULL)
Qi[i]=(E->advection_source_term)(E,el,i,TT1,0.0,0.0,0.0,Ti);
}
}
else /* 2==dims */ {
Ti=0.0;
for(i=1;i<=vpts;i++) {
sfn = E->N.vpt[GNVINDEX(j,i)];
dT1[i] += DT1 * sfn;
Ti += sfn * T1;
T1x1[i] += GNx.vpt[GNVXINDEX(0,j,i)] * T1;
T1x2[i] += GNx.vpt[GNVXINDEX(1,j,i)] * T1;
if(E->advection_source_term != NULL) {
Qi[i]=(E->advection_source_term)(E,el,i,TT1,0.0,0.0,0.0,Ti);
}
}
}
}
/* Construct PG shape functions */
if(3==dims) {
for(i=1;i<=VPOINTS3D;i++) {
for(j=1;j<=ENODES3D;j++) {
Eres1[j] -= dOmega.vpt[i] * ( E->N.vpt[GNVINDEX(j,i)] * dT1[i] - Qi[i] +
diff1 * (GNx.vpt[GNVXINDEX(0,j,i)]*T1x1[i] +
GNx.vpt[GNVXINDEX(1,j,i)]*T1x2[i] +
GNx.vpt[GNVXINDEX(2,j,i)]*T1x3[i] ) );
}
}
}
else /* 2==dims */ {
for(i=1;i<=VPOINTS2D;i++) {
for(j=1;j<=ENODES2D;j++) {
/* Add to residual */
Eres1[j] -= dOmega.vpt[i] * ( E->N.vpt[GNVINDEX(j,i)] * dT1[i] - Qi[i] +
diff1 * (GNx.vpt[GNVXINDEX(0,j,i)] * T1x1[i] +
GNx.vpt[GNVXINDEX(1,j,i)] * T1x2[i]
/* + extra term for axi */));