-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAMRLevelMushyLayer.cpp
More file actions
3880 lines (3210 loc) · 113 KB
/
AMRLevelMushyLayer.cpp
File metadata and controls
3880 lines (3210 loc) · 113 KB
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
#ifdef CH_LANG_CC
/*
* _______ __
* / ___/ / ___ __ _ / / ___
* / /__/ _ \/ _ \/ V \/ _ \/ _ \
* \___/_//_/\___/_/_/_/_.__/\___/
* Please refer to Copyright.txt, in Chombo's root directory.
*/
#endif
#include <iomanip>
#include "parstream.H"
#include "AMRFASMultiGrid.H"
#include "AMRIO.H"
#include "AMRMultiGrid.H"
#include "BoxIterator.H"
#include "CH_HDF5.H"
#include "LayoutIterator.H"
#include "LoadBalance.H"
#include "SPMD.H"
#include "computeNorm.H"
#include "computeSum.H"
#include "BackwardEuler.H"
#include "Divergence.H"
#include "ExtrapFillPatch.H"
#include "PhysIBC.H"
#include "SetValLevel.H"
#include "VCAMRPoissonOp2.H"
#include "EnthalpyVariablesF_F.H"
#include "utils_F.H"
#include "AMRLevelMushyLayer.H"
#include "Channel.h"
#include "MushyLayerUtils.H"
#include "NamespaceHeader.H"
BiCGStabSolver<LevelData<FArrayBox>> AMRLevelMushyLayer::s_botSolverUStar;
RelaxSolver<LevelData<FArrayBox>> AMRLevelMushyLayer::s_botSolverHC;
/*******/
AMRLevelMushyLayer::~AMRLevelMushyLayer() {}
/*******/
void AMRLevelMushyLayer::getHierarchyAndGrids(
Vector<AMRLevelMushyLayer *> &a_hierarchy,
Vector<DisjointBoxLayout> &a_grids, Vector<int> &a_refRat,
ProblemDomain &a_lev0Dom, Real &a_lev0Dx)
{
Vector<AMRLevel *> hierarchy = AMRLevel::getAMRLevelHierarchy();
int nlevels = hierarchy.size();
a_hierarchy.resize(nlevels);
a_refRat.resize(nlevels);
a_grids.resize(nlevels);
AMRLevelMushyLayer *coarsestLevel =
static_cast<AMRLevelMushyLayer *>(hierarchy[0]);
a_lev0Dx = coarsestLevel->m_dx;
a_lev0Dom = coarsestLevel->m_problem_domain;
for (int ilev = 0; ilev < nlevels; ilev++)
{
AMRLevelMushyLayer *adLevel =
static_cast<AMRLevelMushyLayer *>(hierarchy[ilev]);
a_hierarchy[ilev] = adLevel;
a_grids[ilev] = adLevel->m_grids;
a_refRat[ilev] = adLevel->m_ref_ratio;
}
}
void AMRLevelMushyLayer::getCoarseScalarDataPointers(
const int a_scalarVar, LevelData<FArrayBox> **a_coarserDataOldPtr,
LevelData<FArrayBox> **a_coarserDataNewPtr,
LevelFluxRegister **a_coarserFRPtr, LevelFluxRegister **a_finerFRPtr,
Real &a_tCoarserOld, Real &a_tCoarserNew)
{
*a_coarserDataOldPtr = nullptr;
*a_coarserDataNewPtr = nullptr;
*a_coarserFRPtr = nullptr;
*a_finerFRPtr = nullptr;
a_tCoarserOld = 0.0;
a_tCoarserNew = 0.0;
// A coarser level exists
if (m_hasCoarser)
{
AMRLevelMushyLayer *coarserPtr = getCoarserLevel();
// Recall that my flux register goes between my level and the next
// finer level
*a_coarserFRPtr = &(*coarserPtr->m_fluxRegisters[a_scalarVar]);
*a_coarserDataOldPtr = &(*coarserPtr->m_scalarOld[a_scalarVar]);
*a_coarserDataNewPtr = &(*coarserPtr->m_scalarNew[a_scalarVar]);
a_tCoarserNew = coarserPtr->m_time;
a_tCoarserOld = a_tCoarserNew - coarserPtr->m_dt;
}
// A finer level exists
if (m_hasFiner)
{
// Recall that my flux register goes between my level and the next
// finer level
// Check flux register is defined - if we are restarting,
// then it might not be defined yet on finer (currently unused) levels
if (m_fluxRegisters[a_scalarVar])
{
*a_finerFRPtr = &(*m_fluxRegisters[a_scalarVar]);
}
}
}
bool AMRLevelMushyLayer::convergedToSteadyState()
{
// Check to see if we've crashed
if (crashed())
{
LOG_INFO("==============================");
LOG_INFO("Simulation crashed, stopping gracefully.");
LOG_INFO("==============================");
return true;
}
if (m_timestepFailed)
{
return false;
}
// Don't consider levels other than base level
// Reflux corrections etc. can prevent us ever reaching a 'steady' state on
// these levels, even when d/dt on the coarsest level is ~ 10^{-10}
if (m_level > 0)
{
return true;
}
Real Tnorm = convergedToSteadyState(ScalarVars::m_enthalpy);
Real Cnorm = convergedToSteadyState(ScalarVars::m_bulkConcentration);
Real Unorm = convergedToSteadyState(m_fluidVel, true);
bool metricConverged = false;
// For some simulations, steady state can be defined by some other metric
// Only test for this on level 0
if (m_opt.computeDiagnostics && !getCoarserLevel())
{
if ((m_parameters.physicalProblem == PhysicalProblems::m_HRL ||
m_parameters.physicalProblem == PhysicalProblems::m_rayleighBenard))
{
metricConverged =
metricConverged || m_diagnostics.movingAverageHasConverged(
DiagnosticNames::diag_Nu, m_time, m_dt);
}
}
// Two ways we can 'converge':
// 1) All fields have converged
// 2) All fields have stalled (d/dt = constant) but metrics have converged
bool hasConverged = false;
bool velConverged =
m_opt.ignoreVelocitySteadyState || Unorm < m_opt.steadyStateCondition;
bool velStalled = m_opt.ignoreVelocitySteadyState;
bool Tconverged = Tnorm < m_opt.steadyStateCondition;
bool Cconverged = Cnorm < m_opt.steadyStateCondition;
// Override to let us converge without salinity having actually converged
if (m_opt.ignoreBulkConcSteadyState)
{
Cconverged = true;
}
if (Tconverged && Cconverged && velConverged)
{
LOG_INFO("Steady state reached (all fields converged)");
hasConverged = true; // converged
}
else if (velStalled && metricConverged)
{
LOG_INFO("Steady state reached (all fields nearly converged and metric "
"converged)");
hasConverged = true; // converged
}
else if (metricConverged)
{
LOG_INFO("Steady state reached (metric converged)");
hasConverged = true; // converged
}
else
{
hasConverged = false; // not converged
}
// One final thing to do
// If we've converged, it may be that we just haven't kicked off the
// instability yet Trying adding a small perturbation and keep going Only do
// this once though
// Real maxVel = ::computeNorm(*m_vectorNew[VectorVars::m_advectionVel],
// nullptr, -1, m_dx, Interval(0, SpaceDim-1), 0); if (hasConverged &&
// m_doAutomaticRestart && abs(maxVel) < 1e-3)
// {
// LOG_INFO("Max Vel = " << maxVel << ". Trying to restart with a small
// perturbation to kick off instability");
// addPerturbation(ScalarVars::m_enthalpy, 1e-3);
// hasConverged = false;
// m_doAutomaticRestart = false;
// }
if (hasConverged && m_dt < 1e-10)
{
LOG_INFO("All fields converged but dt < 1e-10 so keep solving");
return false;
}
if (hasConverged && m_time < m_opt.min_time)
{
LOG_INFO("Converged but time < min_time (" << m_time << " < "
<< m_opt.min_time << ")");
return false;
}
return hasConverged;
}
bool AMRLevelMushyLayer::solvingFullDarcyBrinkman()
{
return m_parameters.isDarcyBrinkman();
}
void AMRLevelMushyLayer::compute_d_dt(const int a_var,
LevelData<FArrayBox> &diff, bool vector)
{
if (vector)
{
diff.define(m_grids, SpaceDim);
m_vectorNew[a_var]->copyTo(diff);
}
else
{
diff.define(m_grids, 1);
m_scalarNew[a_var]->copyTo(diff);
}
DataIterator dit(m_grids);
for (dit.reset(); dit.ok(); ++dit)
{
if (vector)
{
diff[dit] -= (*m_vectorOld[a_var])[dit];
}
else
{
diff[dit] -= (*m_scalarOld[a_var])[dit];
}
diff[dit] /= m_dt;
// diff[dit] /= max;
}
// Output some stuff maybe
if (vector && a_var == m_fluidVel)
{
diff.copyTo(*m_vectorNew[VectorVars::m_dUdt]);
}
else if (!vector && a_var == ScalarVars::m_enthalpy)
{
diff.copyTo(*m_scalarNew[ScalarVars::m_dHdt]);
}
else if (!vector && a_var == ScalarVars::m_bulkConcentration)
{
// horizontallyAverage(*m_scalarNew[ScalarVars::m_dSdt], diff);
diff.copyTo(*m_scalarNew[ScalarVars::m_dSdt]);
}
}
Real AMRLevelMushyLayer::convergedToSteadyState(const int a_var, bool vector)
{
LevelData<FArrayBox> diff;
compute_d_dt(a_var, diff, vector);
Real max;
// Need this to ensure we only calculate sum over valid regions
DisjointBoxLayout *fineGridsPtr = nullptr;
if (hasFinerLevel())
{
fineGridsPtr = &(getFinerLevel()->m_grids);
}
if (vector)
{
max = ::computeNorm(*m_vectorNew[a_var], fineGridsPtr, m_ref_ratio, m_dx,
Interval(0, SpaceDim - 1), 0);
}
else
{
max = ::computeNorm(*m_scalarNew[a_var], fineGridsPtr, m_ref_ratio, m_dx,
Interval(0, 0), 0);
}
Real minAllowed = 1e-10;
max = Max(max, minAllowed);
// Don't consider sponge region in steady state calculations.
DataIterator dit(m_grids);
if (m_opt.spongeHeight > 0)
{
for (dit.reset(); dit.ok(); ++dit)
{
Box spongeBox;
IntVect topCorner = IntVect::Zero;
for (int dir = 0; dir < SpaceDim; dir++)
{
if (dir == 0)
{
topCorner += m_numCells[0] - 1 * BASISV(dir);
}
else
{
topCorner += round(m_numCells[1] * m_opt.spongeHeight) * BASISV(dir);
}
}
spongeBox.define(IntVect::Zero, topCorner);
spongeBox &= diff[dit].box();
diff[dit].setVal(0.0, spongeBox, 0);
}
}
int largestDim = 0;
if (vector)
{
largestDim = SpaceDim - 1;
}
Real norm = ::computeNorm(diff, nullptr, -1, m_dx, Interval(0, largestDim),
m_opt.steadyStateNormType);
norm = norm / max;
pout() << setiosflags(ios::scientific) << setprecision(5);
char outString[100];
string varName = vector ? m_vectorVarNames[a_var] : m_scalarVarNames[a_var];
sprintf(outString, "d/dt (%-20s) = %e ", varName.c_str(), norm);
LOG_DEBUG(outString);
return norm;
}
// void AMRLevelMushyLayer::horizontallySmooth(LevelData<FluxBox>& a_flux)
//{
// CH_TIME("AMRLevelMushyLayer::horizontallySmooth");
//
// DataIterator dit = a_flux.dataIterator();
//
// int dir = 0; // horizontally averaging
//
//
//
// for (dit.reset(); dit.ok(); ++dit)
// {
// for (int velDir=0; velDir < SpaceDim; velDir++)
// {
// FArrayBox& vel = a_flux[dit][velDir];
// Box b = vel.box();
//
// for (BoxIterator bit(b); bit.ok(); ++bit)
// {
// IntVect iv = bit();
// IntVect ivUp = iv+BASISV(dir);
//
// if (b.contains(ivUp))
// {
//
// Real neighbour = vel(ivUp);
//
// // Make sure we don't change the velocity if the neighboroung value
// is much larger
// // this will prevent changing zero velocity cells, or including NaN
// type values if( abs(neighbour) < 1e100 ) //abs(vel(iv)) > 1e-15 &&
// {
// vel(iv) =
// (1-m_opt.postTraceSmoothing)*vel(iv)+m_opt.postTraceSmoothing*neighbour;
// }
//
// }
// }
// }
//
// }
//}
bool AMRLevelMushyLayer::doVelocityAdvection()
{
if (m_parameters.darcy == 0)
{
return false;
}
if (m_opt.doEulerPart)
{
return true;
}
else
{
return false;
}
}
void AMRLevelMushyLayer::copyNewToOldStates()
{
LOG_DEBUG("copyNewToOldStates(" << m_time << " to " << m_time - m_dt << ") ");
// Copy the new to the old
// Old now contains values at n, new will contain values at n+1 eventually
for (int a_scalarVar = 0; a_scalarVar < m_numScalarVars; a_scalarVar++)
{
m_scalarNew[a_scalarVar]->copyTo(m_scalarNew[a_scalarVar]->interval(),
*m_scalarOld[a_scalarVar],
m_scalarOld[a_scalarVar]->interval());
}
for (int vectorVar = 0; vectorVar < m_numVectorVars; vectorVar++)
{
m_vectorNew[vectorVar]->copyTo(m_vectorNew[vectorVar]->interval(),
*m_vectorOld[vectorVar],
m_vectorOld[vectorVar]->interval());
}
m_advVelNew.copyTo(m_advVelOld);
}
/*******/
Real AMRLevelMushyLayer::advance()
{
///////////////////////////////////////////////////////////////
// This method advances the solution by one timestep on a level.
// Before solving the equations, we need to set some things up...
///////////////////////////////////////////////////////////////
// If a timestep fails, we sometimes try reducing the timestep by setting
// m_dtReduction to some factor > 0 If we've just reduced the timestep, set
// this < 0 to ensure we don't do it again
if (m_dtReduction > 0)
{
m_dtReduction = -1;
}
// Get the coarser level, so we can work out later if this is in fact the
// coarsest level
AMRLevelMushyLayer *amrMLcrse = nullptr;
if (m_level > 0)
{
amrMLcrse = getCoarserLevel();
}
// Do some setup operations on only the coarsest level
// If the coarser level pointer is null, it means that this is the coarsest
// level
if (amrMLcrse == nullptr)
{
// 1) set all levels reflux registers to zero
AMRLevelMushyLayer *amrMLptr = this;
// Loop over all levels, starting from 0
Real oldLev0Dt = m_dt;
Real oldTime = m_time;
while (amrMLptr)
{
if (amrMLptr->hasFinerLevel())
{
amrMLptr->setFluxRegistersZero();
}
// 2) Set timestep failed flags to false, after halving dts
if (amrMLptr->m_timestepFailed)
{
// LOG_INFO("PREVIOUS TIMESTEP FAILED - HALVING DT");
Real prevDt = amrMLptr->dt();
Real prevTime = amrMLptr->time();
amrMLptr->dt(prevDt / 2); // halve the timestep
amrMLptr->time(oldTime - oldLev0Dt);
pout() << "Reset on level " << amrMLptr->level()
<< " from (time=" << prevTime << ", dt = " << prevDt << ")";
LOG_INFO(" to (time=" << amrMLptr->time() << ", dt = " << amrMLptr->dt()
<< ")");
}
amrMLptr->m_timestepFailed = false;
// Check if we want to add a perturbation
if (m_time < m_opt.perturbationTime &&
(m_time + m_dt) > m_opt.perturbationTime)
{
addPerturbation(ScalarVars::m_enthalpy, m_opt.delayedPerturbation,
m_opt.perturbationWavenumber,
m_opt.perturbationPhaseShift);
}
amrMLptr = amrMLptr->getFinerLevel();
}
}
Real new_time = m_time + m_dt;
// Let's determine the CFL number we're running at
Real maxAdvU = getMaxVelocityForCFL();
m_computedCFL = m_dt * maxAdvU / m_dx;
LOG_INFO(" AMRLevelMushyLayer::advance (level = "
<< m_level << ", old_time=" << m_time << ", new_time=" << new_time
<< ", dt = " << m_dt << ", CFL=" << m_computedCFL << ")");
// Reset BCs in case they change with time
m_parameters.setTime(m_time); // BCs are stored in m_parameters
this->m_physBCPtr->Time(m_time);
setAdvectionBCs(); // Reset BCs on advection physics objects
// Elliptic operators get the BC from m_parameters when they're defined
// (later)
if (m_opt.rampBuoyancy > 0)
{
if (m_parameters.rayleighComposition == 0)
{
m_parameters.rayleighComposition = m_opt.initRaC;
}
if (m_parameters.rayleighTemp == 0)
{
m_parameters.rayleighTemp = m_opt.initRaT;
}
m_parameters.rayleighComposition =
m_parameters.rayleighComposition * m_opt.rampBuoyancy;
m_parameters.rayleighTemp = m_parameters.rayleighTemp * m_opt.rampBuoyancy;
m_parameters.rayleighComposition =
min(m_opt.maxRaC, m_parameters.rayleighComposition);
m_parameters.rayleighTemp = min(m_opt.maxRaT, m_parameters.rayleighTemp);
LOG_INFO("RaC = " << m_parameters.rayleighComposition
<< ", RaT = " << m_parameters.rayleighTemp);
}
// Compute temperature, porosity, liquid concentration and sold concentration
// from the enthalpy and bulk concentration to ensure they're consistent with
// each other
updateEnthalpyVariables();
#ifdef CH_MPI
{
CH_TIME("MPI_Barrier exchange");
MPI_Barrier(Chombo_MPI::comm);
}
#endif
// Increment time by dt, so m_time represent the time at the end of this
// timestep, and m_time-m_dt is the time at the start of this timestep
// Real old_time = m_time;
// Real half_time = new_time - m_dt / 2;
m_time = new_time;
// Compute d(porosity)/dt from the old timestep in case we need it at some
// point First create data structures
LevelData<FArrayBox> oldPorosity(m_dPorosity_dt.disjointBoxLayout(), 1,
m_dPorosity_dt.ghostVect());
LevelData<FArrayBox> newPorosity(m_dPorosity_dt.disjointBoxLayout(), 1,
m_dPorosity_dt.ghostVect());
// Now fill them with data, making sure we fill ghost cells
fillScalars(oldPorosity, m_time - m_dt, m_porosity, true, true);
fillScalars(newPorosity, m_time, m_porosity, true, true);
for (DataIterator dit = m_dPorosity_dt.dataIterator(); dit.ok(); ++dit)
{
m_dPorosity_dt[dit].copy(newPorosity[dit]);
m_dPorosity_dt[dit].minus(oldPorosity[dit]);
m_dPorosity_dt[dit].divide(m_dt);
}
// The 'new' variables have been calculated at the end of the previous
// timestep, i.e. at the start of this timestep. So move them from new->old.
// do this after we've incremented m_time for consistency with coarser levels
copyNewToOldStates();
// Gradually ramp up temperature forcing
// if (m_parameters.physicalProblem == PhysicalProblems::m_poiseuilleFlow)
// {
// stokesDarcyForcing(*m_scalarNew[ScalarVars::m_temperature], new_time);
// stokesDarcyForcing(*m_scalarOld[ScalarVars::m_temperature], old_time);
// }
// Some useful things to have around
DataIterator dit(m_grids);
IntVect ivGhost = m_numGhost * IntVect::Unit;
IntVect advectionGhost = m_numGhostAdvection * IntVect::Unit;
/*
* First step for momentum equation: predict face centred (u/chi)^{n+1/2}
* using (u/chi)^n
*/
LevelData<FArrayBox> advectionSourceTerm(m_grids, SpaceDim, advectionGhost);
calculatePermeability(); // make sure this is up to date
LevelData<FArrayBox> zeroSrc(m_grids, 1, ivGhost);
setValLevel(zeroSrc, 0.0);
// Need to redefine solvers if variables have changed
defineSolvers(m_time - m_dt); // define at old time
// if (solvingFullDarcyBrinkman())
if (doVelocityAdvection())
{
// This fills all the ghost cells of advectionSourceTerm
computeAdvectionVelSourceTerm(advectionSourceTerm);
computeAdvectionVelocities(advectionSourceTerm, m_adv_vel_centering);
}
else
{
// Sanity checks
Real maxEnthalpy = ::computeNorm(*m_scalarNew[ScalarVars::m_enthalpy],
nullptr, 1, m_dx, Interval(0, 0), 0);
if (maxEnthalpy > 1e200)
{
LOG_WARNING("Max enthalpy = " << maxEnthalpy);
}
// this->finestLevel()
// Calculate time centred advection velocity
calculateTimeIndAdvectionVel(m_time - m_dt, m_advVel);
}
// Check we're still satisfying CFL condition
// If not, skip scalar advection for this step
bool doAdvectiveSrc = true;
if (!this->isCurrentCFLSafe(true))
{
doAdvectiveSrc = false;
}
// Another sanity check
// Divergence::levelDivergenceMAC(*m_scalarNew[ScalarVars::m_divUadv],
// m_advVel, m_dx); Real maxDivU =
// ::computeNorm(*m_scalarNew[ScalarVars::m_divUadv], nullptr, 1, m_dx,
// Interval(0,0)); LOG_INFO(" Sanity check: max(div u) = " << maxDivU);
// always* advect lambda (and update flux registers)
// do this as soon as we have advection velocities, in case we want to
// correct them prior to HC advection
// * skip if we're danger of violating the CFL condition
if (doAdvectiveSrc)
{
advectLambda(true);
}
if (m_opt.includeTracers)
{
this->computeRadianceIntensity();
this->advectPassiveTracer();
this->advectActiveTracer();
}
if (!(m_parameters.physicalProblem == PhysicalProblems::m_poiseuilleFlow ||
m_parameters.physicalProblem == PhysicalProblems::m_soluteFluxTest ||
m_parameters.physicalProblem == PhysicalProblems::m_zeroPorosityTest) &&
m_opt.doScalarAdvectionDiffusion)
{
// Computation of advection velocity is done
// Now do advection and diffusion of scalar fields
CH_TIME("AMRLevelMushyLayer::advection-diffusion");
LOG_DEBUG("Advect and diffuse scalars");
int exitStatus = 0;
// Need to construct multi component object
LevelData<FArrayBox> HC_new(m_grids, 2, IntVect::Unit);
LevelData<FArrayBox> HC_old(m_grids, 2, IntVect::Unit);
LevelData<FArrayBox> srcMultiComp(m_grids, 2, IntVect::Zero);
fillHC(HC_new, m_time);
fillHC(HC_old, m_time - m_dt);
setValLevel(srcMultiComp, 0.0);
addHeatSource(srcMultiComp);
bool doFRUpdates = true;
exitStatus = multiCompAdvectDiffuse(HC_old, HC_new, srcMultiComp,
doFRUpdates, doAdvectiveSrc);
bool solverFailed = (exitStatus == 2 || exitStatus == 4 || exitStatus == 6);
Real maxHCNew = ::computeNorm(HC_new, nullptr, 1, m_dx, Interval(0, 1), 0);
if (maxHCNew > 1e200)
{
LOG_WARNING("Max (enthalpy, bulk concentration) = " << maxHCNew);
}
// Get back the answer if solver was a success
HC_new.copyTo(Interval(0, 0), *m_scalarNew[ScalarVars::m_enthalpy],
Interval(0, 0));
HC_new.copyTo(Interval(1, 1), *m_scalarNew[ScalarVars::m_bulkConcentration],
Interval(0, 0));
updateEnthalpyVariables();
if (solverFailed)
{
if (m_opt.ignoreSolveFails)
{
LOG_INFO("Ignoring all solver fails.");
}
else
{
// Alternative way of restarting - set this to true to just do this
// timestep again with half the dt. The trouble is we still output a bad
// file from this timestep which is annoying
m_timestepFailed = true;
Vector<string> failedReasons(99, "Unknown error");
failedReasons[4] = "Solver hang";
failedReasons[8] = "Norm not reduced enough";
failedReasons[2] = "Reached iter max";
failedReasons[1] = "Initial norm not reduced enough";
LOG_INFO("Solver failed. Exit status: "
<< exitStatus << "(" << failedReasons[exitStatus] << ")");
}
} // end if scalar diffusion solver failed
} // end if doing scalar advection/diffusion
// compute cell centered velocities
// for problems where the momentum equation has time dependence
// if (solvingFullDarcyBrinkman())
if (isVelocityTimeDependent())
{
// If we're skipping advective srcs for this timestep, skip this too
if (!doAdvectiveSrc)
{
DataIterator dit = m_vectorNew[VectorVars::m_UdelU]->dataIterator();
for (dit.reset(); dit.ok(); ++dit)
{
(*m_vectorNew[VectorVars::m_UdelU])[dit].setVal(0.0);
}
}
bool doFRupdates = true;
bool compute_uDelU = doAdvectiveSrc;
bool doProjection = true;
computeCCvelocity(advectionSourceTerm, m_time - m_dt, m_dt, doFRupdates,
doProjection, compute_uDelU);
}
getExtraPlotFields();
// supposed to return dt but the return value is never used, so don't.
// if the return value is ever used we will know because this will break it
return -1;
}
void AMRLevelMushyLayer::addHeatSource(LevelData<FArrayBox>& src)
{
// This is where we add in a heat source, if required
Real gaussian_heat_source_size = 0.0;
Real gaussian_heat_source_width = 0.0;
Real gaussian_heat_source_depth = 0.0;
Real gaussian_heat_source_xpos = m_domainWidth/2;
ParmParse ppHeatSource("heatSource");
ppHeatSource.query("size", gaussian_heat_source_size);
ppHeatSource.query("width", gaussian_heat_source_width);
ppHeatSource.query("depth", gaussian_heat_source_depth);
// ppHeatSource.query("depth", gaussian_heat_source_depth);
ppHeatSource.query("xpos", gaussian_heat_source_xpos);
// enthalpy is in the first component of the source term
int Hcomp = 0;
if (gaussian_heat_source_size != 0.0)
{
for (DataIterator dit = m_grids.dataIterator(); dit.ok(); ++dit)
{
for (BoxIterator bit = BoxIterator(m_grids[dit]); bit.ok(); ++bit)
{
IntVect iv = bit();
RealVect loc;
::getLocation(iv, loc, m_dx);
Real porosity = (*m_scalarNew[ScalarVars::m_porosity])[dit](iv);
src[dit](iv, Hcomp) = (1-porosity)*gaussian_heat_source_size/(gaussian_heat_source_width*sqrt(2*M_PI))
* exp(-0.5*pow((loc[0]-gaussian_heat_source_xpos)/gaussian_heat_source_width, 2))
* 0.5*(1 + tanh(10*(loc[1]-(m_domainHeight-gaussian_heat_source_depth) ) ));
}
}
}
}
void AMRLevelMushyLayer::setVelZero(FArrayBox &a_vel,
const FArrayBox &a_porosity,
const Real a_limit, const int a_radius)
{
if (a_radius > 0)
{
IntVectSet porousCells;
for (BoxIterator bit(a_porosity.box()); bit.ok(); ++bit)
{
IntVect iv = bit();
if (a_porosity(iv) <= a_limit)
{
porousCells |= iv;
}
}
porousCells.grow(a_radius);
IntVectSet setZeroCells = porousCells;
setZeroCells &= a_vel.box();
for (IVSIterator its(setZeroCells); its.ok(); ++its)
{
for (int comp = 0; comp < a_vel.nComp(); comp++)
{
a_vel(its(), comp) = 0;
}
}
}
else
{
Box region = a_vel.box();
region &= a_porosity.box();
FORT_SETZEROVELOCITY(CHF_FRA(a_vel), CHF_CONST_FRA(a_porosity),
CHF_BOX(region), CHF_CONST_REAL(a_limit));
}
}
void AMRLevelMushyLayer::setCCVelZero(Real a_limit)
{
setVelZero(*m_scalarNew[VectorVars::m_fluidVel], a_limit);
}
void AMRLevelMushyLayer::setVelZero(LevelData<FArrayBox> &a_vel, Real a_limit,
int a_radius)
{
if (a_limit < 0)
{
a_limit = m_opt.solidPorosity;
}
for (DataIterator dit = a_vel.dataIterator(); dit.ok(); ++dit)
{
setVelZero(a_vel[dit], (*m_scalarNew[ScalarVars::m_porosity])[dit], a_limit,
a_radius);
}
}
void AMRLevelMushyLayer::setVelZero(LevelData<FluxBox> &a_vel, Real a_limit)
{
if (a_limit < 0)
{
a_limit = m_opt.solidPorosity;
}
// Get the porosity
LevelData<FluxBox> porosity(a_vel.disjointBoxLayout(), 1, a_vel.ghostVect());
fillScalarFace(porosity, m_time, m_porosity, true);
for (DataIterator dit = a_vel.dataIterator(); dit.ok(); ++dit)
{
for (int dir = 0; dir < SpaceDim; dir++)
{
setVelZero(a_vel[dit][dir], porosity[dit][dir], a_limit);
}
}
}
void AMRLevelMushyLayer::computeScalDiffusion(LevelData<FArrayBox> &a_src,
int a_var)
{
BCHolder bc;
getScalarBCs(bc, a_var, false);
AMRPoissonOpFactory *op = new AMRPoissonOpFactory();
op->define(m_problem_domain, m_grids, m_dx, bc);
RefCountedPtr<AMRPoissonOpFactory> OpFact =
RefCountedPtr<AMRLevelOpFactory<LevelData<FArrayBox>>>(op);
RefCountedPtr<AMRPoissonOp> amrpop = RefCountedPtr<AMRPoissonOp>(
(AMRNonLinearMultiCompOp *)OpFact->AMRnewOp(m_problem_domain));
LevelData<FArrayBox> *crseVar = nullptr;
AMRLevelMushyLayer *crseML = getCoarserLevel();
if (crseML)
{
crseVar = &(*crseML->m_scalarNew[a_var]);
}
amrpop->setAlphaAndBeta(0, 1);
// This just calls applyOpI if crseHC = nullptr, else does CF interpolation
amrpop->applyOpMg(a_src, *m_scalarNew[a_var], crseVar, false);
}
void AMRLevelMushyLayer::advectLambda(bool doFRupdates)
{
CH_TIME("AMRLevelMushyLayer::advectLambda");
LOG_FUNCTION_ENTRY();
m_advVel.exchange();
// Do lambda advection
m_scalarNew[ScalarVars::m_lambda]->copyTo(
Interval(0, 0), *m_scalarOld[ScalarVars::m_lambda], Interval(0, 0));
// Want to get the lambda flux back so we can remove it later
LevelData<FluxBox> lambdaFlux(m_grids, 1);
advectScalar(m_lambda, m_lambda, m_advVel, true,
lambdaFlux); // advect without a diffusive source term
setValLevel(*m_vectorNew[VectorVars::m_advVelCorr], 0.0);
}
void AMRLevelMushyLayer::updateEnthalpyVariables()
{
CH_TIME("AMRLevelMushyLayer::updateEnthalpyVariables");
LevelData<FArrayBox> HC(m_grids, 2, IntVect::Unit);
fillHC(HC, m_time);
::updateEnthalpyVariables(HC, *m_scalarNew[ScalarVars::m_temperature],
*m_scalarNew[ScalarVars::m_liquidConcentration],
*m_scalarNew[ScalarVars::m_solidConcentration],
*m_scalarNew[ScalarVars::m_porosity],
*m_scalarNew[ScalarVars::m_enthalpySolidus],
*m_scalarNew[ScalarVars::m_enthalpyLiquidus],
*m_scalarNew[ScalarVars::m_enthalpyEutectic],
m_parameters);
doRegularisationOps(*m_scalarNew[ScalarVars::m_liquidConcentration],
m_liquidConcentration);
doRegularisationOps(*m_scalarNew[ScalarVars::m_porosity], m_porosity);
// A few alterations for test problems
if (m_parameters.physicalProblem == PhysicalProblems::m_poiseuilleFlow)
{
initialDataPoiseuille();
}
else if (m_parameters.physicalProblem ==
PhysicalProblems::m_convectionMixedPorous ||
m_parameters.physicalProblem ==
PhysicalProblems::m_zeroPorosityTest)
{
fillFixedPorosity(*m_scalarNew[ScalarVars::m_porosity]);
m_scalarNew[ScalarVars::m_porosity]->copyTo(
*m_scalarOld[ScalarVars::m_porosity]);
}
Real maxEnthalpy = ::computeNorm(*m_scalarNew[ScalarVars::m_enthalpy],
nullptr, 1, m_dx, Interval(0, 0), 0);
Real maxBulkC = ::computeNorm(*m_scalarNew[ScalarVars::m_bulkConcentration],
nullptr, 1, m_dx, Interval(0, 0), 0);