-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathSinglePhaseBase.cpp
More file actions
1443 lines (1216 loc) · 69.6 KB
/
SinglePhaseBase.cpp
File metadata and controls
1443 lines (1216 loc) · 69.6 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
/*
* ------------------------------------------------------------------------------------------------------------
* SPDX-License-Identifier: LGPL-2.1-only
*
* Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
* Copyright (c) 2018-2024 TotalEnergies
* Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
* Copyright (c) 2023-2024 Chevron
* Copyright (c) 2019- GEOS/GEOSX Contributors
* All rights reserved
*
* See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
* ------------------------------------------------------------------------------------------------------------
*/
/**
* @file SinglePhaseBase.cpp
*/
#include "SinglePhaseBase.hpp"
#include "common/DataTypes.hpp"
#include "common/TimingMacros.hpp"
#include "constitutive/fluid/singlefluid/SingleFluidBase.hpp"
#include "constitutive/fluid/singlefluid/SingleFluidFields.hpp"
#include "constitutive/fluid/singlefluid/SingleFluidSelector.hpp"
#include "constitutive/solid/SolidInternalEnergy.hpp"
#include "constitutive/thermalConductivity/SinglePhaseThermalConductivitySelector.hpp"
#include "fieldSpecification/AquiferBoundaryCondition.hpp"
#include "fieldSpecification/EquilibriumInitialCondition.hpp"
#include "fieldSpecification/FieldSpecificationManager.hpp"
#include "fieldSpecification/SourceFluxBoundaryCondition.hpp"
#include "physicsSolvers/fluidFlow/SourceFluxStatistics.hpp"
#include "functions/TableFunction.hpp"
#include "mainInterface/ProblemManager.hpp"
#include "mesh/DomainPartition.hpp"
#include "physicsSolvers/LogLevelsInfo.hpp"
#include "physicsSolvers/KernelLaunchSelectors.hpp"
#include "physicsSolvers/fluidFlow/FlowSolverBaseFields.hpp"
#include "physicsSolvers/fluidFlow/SinglePhaseBaseFields.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/MobilityKernel.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/SolutionCheckKernel.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/SolutionScalingKernel.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/ThermalSolutionScalingKernel.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/StatisticsKernel.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/HydrostaticPressureKernel.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/ThermalHydrostaticPressureKernel.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/FluidUpdateKernel.hpp"
#include "physicsSolvers/fluidFlow/kernels/singlePhase/SolidInternalEnergyUpdateKernel.hpp"
namespace geos
{
using namespace dataRepository;
using namespace constitutive;
using namespace fields;
using namespace singlePhaseBaseKernels;
SinglePhaseBase::SinglePhaseBase( const string & name,
Group * const parent ):
FlowSolverBase( name, parent )
{
this->registerWrapper( viewKeyStruct::inputTemperatureString(), &m_inputTemperature ).
setApplyDefaultValue( 0.0 ).
setInputFlag( InputFlags::OPTIONAL ).
setDescription( "Temperature" );
this->getWrapper< integer >( string( viewKeyStruct::isThermalString() ) ).
appendDescription( GEOS_FMT( "\nSourceFluxes application if {} is enabled :\n"
"- negative value (injection): the mass balance equation is modified to considered the additional source term,\n"
"- positive value (production): both the mass balance and the energy balance equations are modified to considered the additional source term.\n"
"For the energy balance equation, the mass flux is multiplied by the enthalpy in the cell from which the fluid is being produced.",
viewKeyStruct::isThermalString() ) );
addLogLevel< logInfo::BoundaryConditions >();
}
void SinglePhaseBase::registerDataOnMesh( Group & meshBodies )
{
FlowSolverBase::registerDataOnMesh( meshBodies );
m_numDofPerCell = m_isThermal ? 2 : 1;
forDiscretizationOnMeshTargets( meshBodies, [&] ( string const &,
MeshLevel & mesh,
string_array const & regionNames )
{
ElementRegionManager & elemManager = mesh.getElemManager();
elemManager.forElementSubRegions< ElementSubRegionBase >( regionNames,
[&]( localIndex const,
ElementSubRegionBase & subRegion )
{
subRegion.registerField< flow::mobility >( getName() );
subRegion.registerField< flow::dMobility >( getName()).reference().resizeDimension< 1 >( m_numDofPerCell );
subRegion.registerField< flow::mass >( getName() );
subRegion.registerField< flow::mass_n >( getName() );
subRegion.registerField< flow::dMass >( getName() ).reference().resizeDimension< 1 >( m_numDofPerCell );
subRegion.registerField< flow::pressureScalingFactor >( getName() );
if( m_isThermal )
{
subRegion.registerField< flow::dEnergy >( getName() ).reference().resizeDimension< 1 >( m_numDofPerCell );
subRegion.registerField< flow::temperatureScalingFactor >( getName() );
}
} );
elemManager.forElementSubRegions< SurfaceElementSubRegion >( regionNames,
[&]( localIndex const,
SurfaceElementSubRegion & subRegion )
{
subRegion.registerField< flow::massCreated >( getName() );
} );
FaceManager & faceManager = mesh.getFaceManager();
{
if( m_isThermal )
{
faceManager.registerField< flow::faceTemperature >( getName() );
}
}
} );
}
void SinglePhaseBase::setConstitutiveNames( ElementSubRegionBase & subRegion ) const
{
setConstitutiveName< SingleFluidBase >( subRegion, viewKeyStruct::fluidNamesString(), "singlephase fluid" );
if( m_isThermal )
{
setConstitutiveName< SinglePhaseThermalConductivityBase >( subRegion, viewKeyStruct::thermalConductivityNamesString(), "singlephase thermal conductivity" );
}
}
void SinglePhaseBase::initializeAquiferBC() const
{
FieldSpecificationManager & fsManager = FieldSpecificationManager::getInstance();
fsManager.forSubGroups< AquiferBoundaryCondition >( [&] ( AquiferBoundaryCondition & bc )
{
// set the gravity vector (needed later for the potential diff calculations)
bc.setGravityVector( gravityVector() );
} );
}
void SinglePhaseBase::validateConstitutiveModels( DomainPartition & domain ) const
{
GEOS_MARK_FUNCTION;
forDiscretizationOnMeshTargets( domain.getMeshBodies(), [&]( string const &,
MeshLevel & mesh,
string_array const & regionNames )
{
mesh.getElemManager().forElementSubRegions( regionNames, [&]( localIndex const,
ElementSubRegionBase & subRegion )
{
string & fluidName = subRegion.getReference< string >( viewKeyStruct::fluidNamesString() );
SingleFluidBase const & fluid = getConstitutiveModel< SingleFluidBase >( subRegion, fluidName );
constitutiveUpdatePassThru( fluid, [&] ( auto & castedFluid )
{
string const fluidModelName = castedFluid.getCatalogName();
GEOS_THROW_IF( m_isThermal && ((fluidModelName != "ThermalCompressibleSinglePhaseFluid") && (fluidModelName != "ReactiveThermalCompressibleSinglePhaseFluid")),
GEOS_FMT( "SingleFluidBase {}: the thermal option is enabled in the solver, but the fluid model {} is not for thermal fluid",
fluid.getName() ),
InputError, getDataContext(), fluid.getDataContext() );
GEOS_THROW_IF( !m_isThermal && ((fluidModelName == "ThermalCompressibleSinglePhaseFluid") || (fluidModelName == "ReactiveThermalCompressibleSinglePhaseFluid")),
GEOS_FMT( "SingleFluidBase {}: the fluid model is for thermal fluid {}, but the solver option is incompatible with the fluid model",
fluid.getName() ),
InputError, getDataContext(), fluid.getDataContext() );
} );
} );
} );
}
void SinglePhaseBase::initializePreSubGroups()
{
FlowSolverBase::initializePreSubGroups();
DomainPartition & domain = this->getGroupByPath< DomainPartition >( "/Problem/domain" );
// 1. Validate various models against each other (must have same phases and components)
validateConstitutiveModels( domain );
// 2. Set the value of temperature
forDiscretizationOnMeshTargets( domain.getMeshBodies(), [&]( string const &,
MeshLevel & mesh,
string_array const & regionNames )
{
mesh.getElemManager().forElementSubRegions( regionNames,
[&]( localIndex const,
ElementSubRegionBase & subRegion )
{
arrayView1d< real64 > const temp = subRegion.getField< flow::temperature >();
temp.setValues< parallelHostPolicy >( m_inputTemperature );
} );
} );
// 3. Initialize the aquifer boundary condition
initializeAquiferBC();
}
void SinglePhaseBase::updateFluidModel( ObjectManagerBase & dataGroup ) const
{
GEOS_MARK_FUNCTION;
arrayView1d< real64 const > const pres = dataGroup.getField< flow::pressure >();
arrayView1d< real64 const > const temp = dataGroup.getField< flow::temperature >();
SingleFluidBase & fluid =
getConstitutiveModel< SingleFluidBase >( dataGroup, dataGroup.getReference< string >( viewKeyStruct::fluidNamesString() ) );
constitutiveUpdatePassThru( fluid, [&]( auto & castedFluid )
{
typename TYPEOFREF( castedFluid ) ::KernelWrapper fluidWrapper = castedFluid.createKernelWrapper();
singlePhaseBaseKernels::FluidUpdateKernel::launch( fluidWrapper, pres, temp );
} );
}
void SinglePhaseBase::updateMass( ElementSubRegionBase & subRegion ) const
{
GEOS_MARK_FUNCTION;
geos::internal::kernelLaunchSelectorThermalSwitch ( m_isThermal, [&] ( auto ISTHERMAL )
{
integer constexpr IS_THERMAL = ISTHERMAL();
updateMass< IS_THERMAL >( subRegion );
} );
}
template< integer IS_THERMAL >
void SinglePhaseBase::updateMass( ElementSubRegionBase & subRegion ) const
{
using DerivOffset = constitutive::singlefluid::DerivativeOffsetC< IS_THERMAL >;
arrayView1d< real64 > const mass = subRegion.getField< flow::mass >();
arrayView1d< real64 > const mass_n = subRegion.getField< flow::mass_n >();
arrayView2d< real64, constitutive::singlefluid::USD_FLUID > const dMass = subRegion.getField< flow::dMass >();
//START_SPHINX_INCLUDE_COUPLEDSOLID
CoupledSolidBase const & porousSolid =
getConstitutiveModel< CoupledSolidBase >( subRegion, subRegion.template getReference< string >( viewKeyStruct::solidNamesString()));
//END_SPHINX_INCLUDE_COUPLEDSOLID
arrayView2d< real64 const > const porosity = porousSolid.getPorosity();
arrayView2d< real64 const > const dPorosity_dP = porousSolid.getDporosity_dPressure();
arrayView2d< real64 const > const porosity_n = porousSolid.getPorosity_n();
arrayView1d< real64 const > const volume = subRegion.getElementVolume();
arrayView1d< real64 const > const deltaVolume = subRegion.getField< fields::flow::deltaVolume >();
SingleFluidBase & fluid =
getConstitutiveModel< SingleFluidBase >( subRegion, subRegion.getReference< string >( viewKeyStruct::fluidNamesString()));
arrayView2d< real64 const, constitutive::singlefluid::USD_FLUID > const density = fluid.density();
arrayView2d< real64 const, constitutive::singlefluid::USD_FLUID > const density_n = fluid.density_n();
arrayView3d< real64 const, constitutive::singlefluid::USD_FLUID_DER > const dDensity = fluid.dDensity();
forAll< parallelDevicePolicy<> >( subRegion.size(), [=] GEOS_HOST_DEVICE ( localIndex const ei )
{
real64 const vol = volume[ei] + deltaVolume[ei];
mass[ei] = porosity[ei][0] * density[ei][0] * vol;
dMass[ei][DerivOffset::dP] = ( dPorosity_dP[ei][0] * density[ei][0] + porosity[ei][0] * dDensity[ei][0][DerivOffset::dP] ) * vol;
if( isZero( mass_n[ei] ) ) // this is a hack for hydrofrac cases
{
mass_n[ei] = porosity_n[ei][0] * volume[ei] * density_n[ei][0]; // initialize newly created element mass
}
} );
if constexpr (IS_THERMAL)
{
arrayView2d< real64 const > const dPorosity_dT = porousSolid.getDporosity_dTemperature();
forAll< parallelDevicePolicy<> >( subRegion.size(), [=] GEOS_HOST_DEVICE ( localIndex const ei )
{
real64 const vol = volume[ei] + deltaVolume[ei];
dMass[ei][DerivOffset::dT] = ( dPorosity_dT[ei][0] * density[ei][0] + porosity[ei][0] * dDensity[ei][0][DerivOffset::dT] ) * vol;
} );
}
}
void SinglePhaseBase::updateEnergy( ElementSubRegionBase & subRegion ) const
{
GEOS_MARK_FUNCTION;
using DerivOffset = constitutive::singlefluid::DerivativeOffsetC< 1 >;
arrayView1d< real64 > const energy = subRegion.getField< flow::energy >();
arrayView2d< real64, constitutive::singlefluid::USD_FLUID > const dEnergy = subRegion.getField< flow::dEnergy >();
arrayView1d< real64 const > const volume = subRegion.getElementVolume();
arrayView1d< real64 const > const deltaVolume = subRegion.getField< flow::deltaVolume >();
CoupledSolidBase const & porousSolid =
getConstitutiveModel< CoupledSolidBase >( subRegion, subRegion.template getReference< string >( viewKeyStruct::solidNamesString() ) );
arrayView2d< real64 const > const porosity = porousSolid.getPorosity();
arrayView2d< real64 const > const dPorosity_dP = porousSolid.getDporosity_dPressure();
arrayView2d< real64 const > const dPorosity_dT = porousSolid.getDporosity_dTemperature();
arrayView2d< real64 const > const rockInternalEnergy = porousSolid.getInternalEnergy();
arrayView2d< real64 const > const dRockInternalEnergy_dT = porousSolid.getDinternalEnergy_dTemperature();
SingleFluidBase & fluid =
getConstitutiveModel< SingleFluidBase >( subRegion, subRegion.getReference< string >( viewKeyStruct::fluidNamesString() ) );
arrayView2d< real64 const, constitutive::singlefluid::USD_FLUID > const density = fluid.density();
arrayView2d< real64 const, constitutive::singlefluid::USD_FLUID > const fluidInternalEnergy = fluid.internalEnergy();
arrayView3d< real64 const, constitutive::singlefluid::USD_FLUID_DER > const dDensity = fluid.dDensity();
arrayView3d< real64 const, constitutive::singlefluid::USD_FLUID_DER > const dFluidInternalEnergy = fluid.dInternalEnergy();
forAll< parallelDevicePolicy<> >( subRegion.size(), [=] GEOS_HOST_DEVICE ( localIndex const ei )
{
real64 const vol = volume[ei] + deltaVolume[ei];
energy[ei] = vol *
( porosity[ei][0] * density[ei][0] * fluidInternalEnergy[ei][0] +
( 1.0 - porosity[ei][0] ) * rockInternalEnergy[ei][0] );
dEnergy[ei][DerivOffset::dP] = vol *
( dPorosity_dP[ei][0] * density[ei][0] * fluidInternalEnergy[ei][0] +
porosity[ei][0] * dDensity[ei][0][DerivOffset::dP] * fluidInternalEnergy[ei][0] +
porosity[ei][0] * density[ei][0] * dFluidInternalEnergy[ei][0][DerivOffset::dP] -
dPorosity_dP[ei][0] * rockInternalEnergy[ei][0] );
dEnergy[ei][DerivOffset::dT] = vol *
( dPorosity_dT[ei][0] * density[ei][0] * fluidInternalEnergy[ei][0] +
porosity[ei][0] * dDensity[ei][0][DerivOffset::dT] * fluidInternalEnergy[ei][0] +
porosity[ei][0] * density[ei][0] * dFluidInternalEnergy[ei][0][DerivOffset::dT] -
dPorosity_dT[ei][0] * rockInternalEnergy[ei][0] +
( 1.0 - porosity[ei][0] ) * dRockInternalEnergy_dT[ei][0] );
} );
}
void SinglePhaseBase::updateSolidInternalEnergyModel( ObjectManagerBase & dataGroup ) const
{
arrayView1d< real64 const > const temperature = dataGroup.getField< flow::temperature >();
string const & solidInternalEnergyName = dataGroup.getReference< string >( viewKeyStruct::solidInternalEnergyNamesString() );
SolidInternalEnergy & solidInternalEnergy = getConstitutiveModel< SolidInternalEnergy >( dataGroup, solidInternalEnergyName );
SolidInternalEnergy::KernelWrapper solidInternalEnergyWrapper = solidInternalEnergy.createKernelUpdates();
thermalSinglePhaseBaseKernels::SolidInternalEnergyUpdateKernel::launch< parallelDevicePolicy<> >( dataGroup.size(), solidInternalEnergyWrapper, temperature );
}
void SinglePhaseBase::updateThermalConductivity( ElementSubRegionBase & subRegion ) const
{
string const & thermalConductivityName = subRegion.template getReference< string >( viewKeyStruct::thermalConductivityNamesString() );
SinglePhaseThermalConductivityBase const & conductivityMaterial =
getConstitutiveModel< SinglePhaseThermalConductivityBase >( subRegion, thermalConductivityName );
arrayView1d< real64 const > const & temperature = subRegion.template getField< flow::temperature >();
conductivityMaterial.updateFromTemperature( temperature );
}
real64 SinglePhaseBase::updateFluidState( ElementSubRegionBase & subRegion ) const
{
updateFluidModel( subRegion );
updateMass( subRegion );
updateMobility( subRegion );
return 0.0;
}
void SinglePhaseBase::updateMobility( ObjectManagerBase & dataGroup ) const
{
GEOS_MARK_FUNCTION;
// output
arrayView1d< real64 > const mob = dataGroup.getField< flow::mobility >();
arrayView2d< real64, constitutive::singlefluid::USD_FLUID > const dMobility = dataGroup.getField< flow::dMobility >();
// input
SingleFluidBase & fluid =
getConstitutiveModel< SingleFluidBase >( dataGroup, dataGroup.getReference< string >( viewKeyStruct::fluidNamesString() ) );
geos::internal::kernelLaunchSelectorThermalSwitch( m_isThermal, [&] ( auto ISTHERMAL )
{
integer constexpr NUMDOF = ISTHERMAL() + 1;
singlePhaseBaseKernels::MobilityKernel::compute_value_and_derivatives< parallelDevicePolicy<>, NUMDOF >( dataGroup.size(),
fluid.density(),
fluid.dDensity(),
fluid.viscosity(),
fluid.dViscosity(),
mob,
dMobility );
} );
}
void SinglePhaseBase::initializePostInitialConditionsPreSubGroups()
{
GEOS_MARK_FUNCTION;
allowNegativePressure();
FlowSolverBase::initializePostInitialConditionsPreSubGroups();
DomainPartition & domain = this->getGroupByPath< DomainPartition >( "/Problem/domain" );
initializeState( domain );
}
void SinglePhaseBase::computeHydrostaticEquilibrium( DomainPartition & domain )
{
FieldSpecificationManager & fsManager = FieldSpecificationManager::getInstance();
real64 const gravVector[3] = LVARRAY_TENSOROPS_INIT_LOCAL_3( gravityVector() );
// Step 1: count individual equilibriums (there may be multiple ones)
stdMap< string, localIndex > equilNameToEquilId;
localIndex equilCounter = 0;
fsManager.forSubGroups< EquilibriumInitialCondition >( [&] ( EquilibriumInitialCondition const & bc )
{
// collect all the equil name to idx
equilNameToEquilId.insert( {bc.getName(), equilCounter} );
equilCounter++;
// check that the gravity vector is aligned with the z-axis
GEOS_THROW_IF( !isZero( gravVector[0] ) || !isZero( gravVector[1] ),
"The gravity vector specified in this simulation (" << gravVector[0] << " " << gravVector[1] << " " << gravVector[2] <<
") is not aligned with the z-axis. \n"
"This is incompatible with the " << bc.getCatalogName() << " " << bc.getName() <<
"used in this simulation. To proceed, you can either: \n" <<
" - Use a gravityVector aligned with the z-axis, such as (0.0,0.0,-9.81)\n" <<
" - Remove the hydrostatic equilibrium initial condition from the XML file",
InputError, getDataContext(), bc.getDataContext() );
// ensure that the temperature tables are defined for thermal simulations
GEOS_THROW_IF( m_isThermal && bc.getTemperatureVsElevationTableName().empty(),
EquilibriumInitialCondition::viewKeyStruct::temperatureVsElevationTableNameString()
<< " must be provided for a thermal simulation",
InputError, getDataContext(), bc.getDataContext() );
//ensure that compositions are empty
GEOS_THROW_IF( !bc.getComponentFractionVsElevationTableNames().empty(),
EquilibriumInitialCondition::viewKeyStruct::componentFractionVsElevationTableNamesString()
<< " must not be provided for a single phase simulation.",
InputError, getDataContext(), bc.getDataContext() );
GEOS_THROW_IF( !bc.getComponentNames().empty(),
EquilibriumInitialCondition::viewKeyStruct::componentNamesString()
<< " must not be provided for a single phase simulation.",
InputError, getDataContext(), bc.getDataContext() );
} );
if( equilCounter == 0 )
{
return;
}
// Step 2: find the min elevation and the max elevation in the targetSets
array1d< real64 > globalMaxElevation( equilNameToEquilId.size() );
array1d< real64 > globalMinElevation( equilNameToEquilId.size() );
findMinMaxElevationInEquilibriumTarget( domain,
equilNameToEquilId,
globalMaxElevation,
globalMinElevation );
// Step 3: for each equil, compute a fine table with hydrostatic pressure vs elevation if the region is a target region
// first compute the region filter
std::set< string > regionFilter;
forDiscretizationOnMeshTargets( domain.getMeshBodies(), [&] ( string const &,
MeshLevel &,
string_array const & regionNames )
{
for( string const & regionName : regionNames )
{
regionFilter.insert( regionName );
}
} );
// then start the actual table construction
fsManager.apply< ElementSubRegionBase,
EquilibriumInitialCondition >( 0.0,
domain.getMeshBody( 0 ).getBaseDiscretization(),
EquilibriumInitialCondition::catalogName(),
[&] ( EquilibriumInitialCondition const & fs,
string const &,
SortedArrayView< localIndex const > const & targetSet,
ElementSubRegionBase & subRegion,
string const & )
{
// Step 3.1: retrieve the data necessary to construct the pressure table in this subregion
integer const maxNumEquilIterations = fs.getMaxNumEquilibrationIterations();
real64 const equilTolerance = fs.getEquilibrationTolerance();
real64 const datumElevation = fs.getDatumElevation();
real64 const datumPressure = fs.getDatumPressure();
localIndex const equilIndex = equilNameToEquilId.at( fs.getName() );
real64 const minElevation = LvArray::math::min( globalMinElevation[equilIndex], datumElevation );
real64 const maxElevation = LvArray::math::max( globalMaxElevation[equilIndex], datumElevation );
real64 const elevationIncrement = LvArray::math::min( fs.getElevationIncrement(), maxElevation - minElevation );
localIndex const numPointsInTable = ( elevationIncrement > 0 ) ? std::ceil( (maxElevation - minElevation) / elevationIncrement ) + 1 : 1;
real64 const eps = 0.1 * (maxElevation - minElevation); // we add a small buffer to only log in the pathological cases
GEOS_LOG_RANK_0_IF( ( (datumElevation > globalMaxElevation[equilIndex]+eps) || (datumElevation < globalMinElevation[equilIndex]-eps) ),
getCatalogName() << " " << getDataContext() <<
"By looking at the elevation of the cell centers in this model, GEOS found that " <<
"the min elevation is " << globalMinElevation[equilIndex] << " and the max elevation is " <<
globalMaxElevation[equilIndex] << "\nBut, a datum elevation of " << datumElevation <<
" was specified in the input file to equilibrate the model.\n " <<
"The simulation is going to proceed with this out-of-bound datum elevation," <<
" but the initial condition may be inaccurate." );
array1d< array1d< real64 > > elevationValues;
array1d< real64 > pressureValues;
elevationValues.resize( 1 );
elevationValues[0].resize( numPointsInTable );
pressureValues.resize( numPointsInTable );
// Step 3.2: retrieve the fluid model to compute densities
// we end up with the same issue as in applyDirichletBC: there is not a clean way to retrieve the fluid info
FunctionManager & functionManager = FunctionManager::getInstance();
TableFunction::KernelWrapper tempTableWrapper;
// Creation of Wrapper in case of TemperatureVsElevationTableName
if( m_isThermal )
{
string const tempTableName = fs.getTemperatureVsElevationTableName();
TableFunction const & tempTable = functionManager.getGroup< TableFunction >( tempTableName );
tempTableWrapper = tempTable.createKernelWrapper();
}
// filter out region not in target
Group const & region = subRegion.getParent().getParent();
auto it = regionFilter.find( region.getName() );
if( it == regionFilter.end() )
{
return; // the region is not in target, there is nothing to do
}
string const & fluidName = subRegion.getReference< string >( viewKeyStruct::fluidNamesString());
// filter out the proppant fluid constitutive models
ConstitutiveBase & fluid = getConstitutiveModel( subRegion, fluidName );
if( !dynamicCast< SingleFluidBase * >( &fluid ) )
{
return;
}
SingleFluidBase & singleFluid = dynamicCast< SingleFluidBase & >( fluid );
// Step 3.3: compute the hydrostatic pressure values
constitutiveUpdatePassThru( singleFluid, [&] ( auto & castedFluid )
{
using FluidType = TYPEOFREF( castedFluid );
typename FluidType::KernelWrapper fluidWrapper = castedFluid.createKernelWrapper();
// note: inside this kernel, serialPolicy is used, and elevation/pressure values don't go to the GPU
bool const equilHasConverged = m_isThermal ?
thermalSinglePhaseBaseKernels::
HydrostaticPressureKernel::launch( numPointsInTable,
maxNumEquilIterations,
equilTolerance,
gravVector,
minElevation,
elevationIncrement,
datumElevation,
datumPressure,
fluidWrapper,
tempTableWrapper,
elevationValues.toNestedView(),
pressureValues.toView() ) :
singlePhaseBaseKernels::
HydrostaticPressureKernel::launch( numPointsInTable,
maxNumEquilIterations,
equilTolerance,
gravVector,
minElevation,
elevationIncrement,
datumElevation,
datumPressure,
fluidWrapper,
elevationValues.toNestedView(),
pressureValues.toView() );
GEOS_THROW_IF( !equilHasConverged,
"Hydrostatic pressure initialization failed to converge in region " << region.getName() << "!",
geos::RuntimeError, getDataContext() );
} );
// Step 3.4: create hydrostatic pressure table
string const tableName = fs.getName() + "_" + subRegion.getName() + "_table";
TableFunction * const presTable = dynamicCast< TableFunction * >( functionManager.createChild( TableFunction::catalogName(), tableName ) );
presTable->setTableCoordinates( elevationValues, { units::Distance } );
presTable->setTableValues( pressureValues, units::Pressure );
presTable->setInterpolationMethod( TableFunction::InterpolationType::Linear );
TableFunction::KernelWrapper presTableWrapper = presTable->createKernelWrapper();
// Step 4: assign pressure as a function of elevation
// TODO: this last step should probably be delayed to wait for the creation of FaceElements
arrayView2d< real64 const > const elemCenter = subRegion.getElementCenter();
arrayView1d< real64 > const pres = subRegion.getField< flow::pressure >();
arrayView1d< real64 > const temp = subRegion.getField< flow::temperature >();
RAJA::ReduceMin< parallelHostReduce, real64 > minPressure( LvArray::NumericLimits< real64 >::max );
forAll< parallelHostPolicy >( targetSet.size(), [=] GEOS_HOST_DEVICE ( localIndex const i )
{
localIndex const k = targetSet[i];
real64 const elevation = elemCenter[k][2];
pres[k] = presTableWrapper.compute( &elevation );
if( m_isThermal )
{
temp[k] = tempTableWrapper.compute( &elevation );
}
minPressure.min( pres[k] );
} );
// For single phase flow, just issue a warning, because the simulation can proceed with a negative pressure
GEOS_WARNING_IF( minPressure.get() <= 0.0,
GEOS_FMT( "A negative pressure of {} Pa was found during hydrostatic initialization in region/subRegion {}/{}",
minPressure.get(), region.getName(), subRegion.getName() ) );
} );
}
void SinglePhaseBase::initializeFluidState( MeshLevel & mesh, string_array const & regionNames )
{
mesh.getElemManager().forElementSubRegions< CellElementSubRegion, SurfaceElementSubRegion >( regionNames, [&]( localIndex const,
auto & subRegion )
{
SingleFluidBase const & fluid =
getConstitutiveModel< SingleFluidBase >( subRegion, subRegion.template getReference< string >( viewKeyStruct::fluidNamesString()));
updateFluidState( subRegion );
// 2. save the initial density (for use in the single-phase poromechanics solver to compute the deltaBodyForce)
fluid.initializeState();
} );
}
void SinglePhaseBase::initializeThermalState( MeshLevel & mesh, string_array const & regionNames )
{
mesh.getElemManager().forElementSubRegions< CellElementSubRegion, SurfaceElementSubRegion >( regionNames, [&]( localIndex const,
auto & subRegion )
{
// initialized porosity
CoupledSolidBase const & porousSolid =
getConstitutiveModel< CoupledSolidBase >( subRegion, subRegion.template getReference< string >( viewKeyStruct::solidNamesString() ) );
arrayView2d< real64 const > const porosity = porousSolid.getPorosity();
string const & thermalConductivityName = subRegion.template getReference< string >( viewKeyStruct::thermalConductivityNamesString());
SinglePhaseThermalConductivityBase const & conductivityMaterial =
getConstitutiveModel< SinglePhaseThermalConductivityBase >( subRegion, thermalConductivityName );
conductivityMaterial.initializeRockFluidState( porosity );
// note that there is nothing to update here because thermal conductivity is explicit for now
updateSolidInternalEnergyModel( subRegion );
string const & solidInternalEnergyName = subRegion.template getReference< string >( viewKeyStruct::solidInternalEnergyNamesString());
SolidInternalEnergy const & solidInternalEnergyMaterial =
getConstitutiveModel< SolidInternalEnergy >( subRegion, solidInternalEnergyName );
solidInternalEnergyMaterial.saveConvergedState();
updateEnergy( subRegion );
} );
}
void SinglePhaseBase::implicitStepSetup( real64 const & GEOS_UNUSED_PARAM( time_n ),
real64 const & GEOS_UNUSED_PARAM( dt ),
DomainPartition & domain )
{
forDiscretizationOnMeshTargets( domain.getMeshBodies(), [&]( string const &,
MeshLevel & mesh,
string_array const & regionNames )
{
mesh.getElemManager().forElementSubRegions< CellElementSubRegion, SurfaceElementSubRegion >( regionNames, [&]( localIndex const,
auto & subRegion )
{
saveConvergedState( subRegion );
applyDeltaVolume( subRegion );
// This should fix NaN density in newly created fracture elements
updatePorosityAndPermeability( subRegion );
updateFluidState( subRegion );
// for thermal simulations, update solid internal energy
if( m_isThermal )
{
updateSolidInternalEnergyModel( subRegion );
updateThermalConductivity( subRegion );
updateEnergy( subRegion );
}
} );
mesh.getElemManager().forElementSubRegions< SurfaceElementSubRegion >( regionNames, [&]( localIndex const,
SurfaceElementSubRegion & subRegion )
{
arrayView1d< real64 const > const aper = subRegion.getField< flow::hydraulicAperture >();
arrayView1d< real64 > const aper0 = subRegion.getField< flow::aperture0 >();
aper0.setValues< parallelDevicePolicy<> >( aper );
// Needed coz faceElems don't exist when initializing.
CoupledSolidBase const & porousSolid =
getConstitutiveModel< CoupledSolidBase >( subRegion, subRegion.getReference< string >( viewKeyStruct::solidNamesString() ) );
porousSolid.saveConvergedState();
saveConvergedState( subRegion ); // necessary for a meaningful porosity update in sequential schemes
updatePorosityAndPermeability( subRegion );
updateFluidState( subRegion );
// This call is required by the proppant solver, but should not be here
SingleFluidBase const & fluid =
getConstitutiveModel< SingleFluidBase >( subRegion, subRegion.getReference< string >( viewKeyStruct::fluidNamesString() ) );
fluid.saveConvergedState();
} );
} );
}
void SinglePhaseBase::implicitStepComplete( real64 const & time,
real64 const & dt,
DomainPartition & domain )
{
GEOS_MARK_FUNCTION;
// note: we have to save the aquifer state **before** updating the pressure,
// otherwise the aquifer flux is saved with the wrong pressure time level
saveAquiferConvergedState( time, dt, domain );
forDiscretizationOnMeshTargets( domain.getMeshBodies(), [&]( string const &,
MeshLevel & mesh,
string_array const & regionNames )
{
mesh.getElemManager().forElementSubRegions( regionNames, [&]( localIndex const,
ElementSubRegionBase & subRegion )
{
// update deltaPressure
arrayView1d< real64 const > const pres = subRegion.getField< flow::pressure >();
arrayView1d< real64 const > const initPres = subRegion.getField< flow::initialPressure >();
arrayView1d< real64 > const deltaPres = subRegion.getField< flow::deltaPressure >();
singlePhaseBaseKernels::StatisticsKernel::
saveDeltaPressure( subRegion.size(), pres, initPres, deltaPres );
applyDeltaVolume( subRegion );
SingleFluidBase const & fluid =
getConstitutiveModel< SingleFluidBase >( subRegion, subRegion.template getReference< string >( viewKeyStruct::fluidNamesString() ) );
fluid.saveConvergedState();
CoupledSolidBase const & porousSolid =
getConstitutiveModel< CoupledSolidBase >( subRegion, subRegion.template getReference< string >( viewKeyStruct::solidNamesString() ) );
if( m_keepVariablesConstantDuringInitStep )
{
porousSolid.ignoreConvergedState(); // newPorosity <- porosity_n
}
else
{
porousSolid.saveConvergedState(); // porosity_n <- porosity
}
if( m_isThermal )
{
arrayView2d< real64 const > const porosity = porousSolid.getPorosity();
SinglePhaseThermalConductivityBase const & conductivityMaterial =
getConstitutiveModel< SinglePhaseThermalConductivityBase >( subRegion, subRegion.template getReference< string >( viewKeyStruct::thermalConductivityNamesString() ) );
conductivityMaterial.saveConvergedRockFluidState( porosity );
}
} );
mesh.getElemManager().forElementSubRegions< SurfaceElementSubRegion >( regionNames, [&]( localIndex const,
SurfaceElementSubRegion & subRegion )
{
arrayView1d< integer const > const elemGhostRank = subRegion.ghostRank();
arrayView1d< real64 const > const volume = subRegion.getElementVolume();
arrayView1d< real64 > const creationMass = subRegion.getField< flow::massCreated >();
SingleFluidBase const & fluid =
getConstitutiveModel< SingleFluidBase >( subRegion, subRegion.template getReference< string >( viewKeyStruct::fluidNamesString() ) );
arrayView2d< real64 const, constitutive::singlefluid::USD_FLUID > const density_n = fluid.density_n();
forAll< parallelDevicePolicy<> >( subRegion.size(), [=] GEOS_HOST_DEVICE ( localIndex const ei )
{
if( elemGhostRank[ei] < 0 )
{
if( volume[ei] * density_n[ei][0] > 1.1 * creationMass[ei] )
{
creationMass[ei] *= 0.75;
if( creationMass[ei] < 1.0e-20 )
{
creationMass[ei] = 0.0;
}
}
}
} );
} );
} );
}
void SinglePhaseBase::assembleSystem( real64 const GEOS_UNUSED_PARAM( time_n ),
real64 const dt,
DomainPartition & domain,
DofManager const & dofManager,
CRSMatrixView< real64, globalIndex const > const & localMatrix,
arrayView1d< real64 > const & localRhs )
{
GEOS_MARK_FUNCTION;
assembleAccumulationTerms( domain,
dofManager,
localMatrix,
localRhs );
if( m_isJumpStabilized )
{
assembleStabilizedFluxTerms( dt,
domain,
dofManager,
localMatrix,
localRhs );
}
else
{
assembleFluxTerms( dt,
domain,
dofManager,
localMatrix,
localRhs );
}
}
void SinglePhaseBase::assembleAccumulationTerms( DomainPartition & domain,
DofManager const & dofManager,
CRSMatrixView< real64, globalIndex const > const & localMatrix,
arrayView1d< real64 > const & localRhs )
{
GEOS_MARK_FUNCTION;
forDiscretizationOnMeshTargets( domain.getMeshBodies(), [&]( string const &,
MeshLevel & mesh,
string_array const & regionNames )
{
mesh.getElemManager().forElementSubRegions< CellElementSubRegion,
SurfaceElementSubRegion >( regionNames,
[&]( localIndex const,
auto & subRegion )
{
accumulationAssemblyLaunch( dofManager, subRegion, localMatrix, localRhs );
} );
} );
}
void SinglePhaseBase::applyBoundaryConditions( real64 time_n,
real64 dt,
DomainPartition & domain,
DofManager const & dofManager,
CRSMatrixView< real64, globalIndex const > const & localMatrix,
arrayView1d< real64 > const & localRhs )
{
GEOS_MARK_FUNCTION;
if( m_keepVariablesConstantDuringInitStep )
{
// this function is going to force the current flow state to be constant during the time step
// this is used when the poromechanics solver is performing the stress initialization
// TODO: in the future, a dedicated poromechanics kernel should eliminate the flow vars to construct a reduced system
// which will remove the need for this brittle passing aroung of flag
keepVariablesConstantDuringInitStep( time_n, dt, dofManager, domain, localMatrix.toViewConstSizes(), localRhs.toView() );
}
else
{
applySourceFluxBC( time_n, dt, domain, dofManager, localMatrix, localRhs );
applyDirichletBC( time_n, dt, domain, dofManager, localMatrix, localRhs );
applyAquiferBC( time_n, dt, domain, dofManager, localMatrix, localRhs );
}
}
namespace
{
void applyAndSpecifyFieldValue( real64 const & time_n,
real64 const & dt,
MeshLevel & mesh,
globalIndex const rankOffset,
string const dofKey,
bool const,
integer const idof,
string const fieldKey,
string const boundaryFieldKey,
CRSMatrixView< real64, globalIndex const > const & localMatrix,
arrayView1d< real64 > const & localRhs )
{
FieldSpecificationManager & fsManager = FieldSpecificationManager::getInstance();
fsManager.apply< ElementSubRegionBase >( time_n + dt,
mesh,
fieldKey,
[&]( FieldSpecificationBase const & fs,
string const &,
SortedArrayView< localIndex const > const & lset,
ElementSubRegionBase & subRegion,
string const & )
{
// Specify the bc value of the field
fs.applyFieldValue< FieldSpecificationEqual,
parallelDevicePolicy<> >( lset,
time_n + dt,
subRegion,
boundaryFieldKey );
arrayView1d< integer const > const ghostRank = subRegion.ghostRank();
arrayView1d< globalIndex const > const dofNumber =
subRegion.getReference< array1d< globalIndex > >( dofKey );
arrayView1d< real64 const > const bcField =
subRegion.getReference< array1d< real64 > >( boundaryFieldKey );
arrayView1d< real64 const > const field =
subRegion.getReference< array1d< real64 > >( fieldKey );
forAll< parallelDevicePolicy<> >( lset.size(), [=] GEOS_HOST_DEVICE ( localIndex const a )
{
localIndex const ei = lset[a];
if( ghostRank[ei] >= 0 )
{
return;
}
globalIndex const dofIndex = dofNumber[ei];
localIndex const localRow = dofIndex - rankOffset;
real64 rhsValue;
// Apply field value to the matrix/rhs
FieldSpecificationEqual::SpecifyFieldValue( dofIndex + idof,
rankOffset,
localMatrix,
rhsValue,
bcField[ei],
field[ei] );
localRhs[localRow + idof] = rhsValue;
} );
} );
}
}
void SinglePhaseBase::applyDirichletBC( real64 const time_n,
real64 const dt,
DomainPartition & domain,
DofManager const & dofManager,
CRSMatrixView< real64, globalIndex const > const & localMatrix,
arrayView1d< real64 > const & localRhs ) const
{
GEOS_MARK_FUNCTION;
string const dofKey = dofManager.getKey( viewKeyStruct::elemDofFieldString() );
globalIndex const rankOffset = dofManager.rankOffset();
bool const isFirstNonlinearIteration = ( m_nonlinearSolverParameters.m_numNewtonIterations == 0 );
forDiscretizationOnMeshTargets( domain.getMeshBodies(), [&] ( string const &,
MeshLevel & mesh,
string_array const & )
{
applyAndSpecifyFieldValue( time_n, dt, mesh, rankOffset, dofKey, isFirstNonlinearIteration,
0, flow::pressure::key(), flow::bcPressure::key(),
localMatrix, localRhs );
if( m_isThermal )
{
applyAndSpecifyFieldValue( time_n, dt, mesh, rankOffset, dofKey, isFirstNonlinearIteration,
1, flow::temperature::key(), flow::bcTemperature::key(),
localMatrix, localRhs );
}
} );
}
void SinglePhaseBase::applySourceFluxBC( real64 const time_n,
real64 const dt,
DomainPartition & domain,
DofManager const & dofManager,
CRSMatrixView< real64, globalIndex const > const & localMatrix,
arrayView1d< real64 > const & localRhs ) const
{
GEOS_MARK_FUNCTION;
FieldSpecificationManager & fsManager = FieldSpecificationManager::getInstance();
string const dofKey = dofManager.getKey( viewKeyStruct::elemDofFieldString() );
// Step 1: count individual source flux boundary conditions
stdMap< string, localIndex > bcNameToBcId;
localIndex bcCounter = 0;
fsManager.forSubGroups< SourceFluxBoundaryCondition >( [&] ( SourceFluxBoundaryCondition const & bc )
{
// collect all the bc names to idx
bcNameToBcId.insert( {bc.getName(), bcCounter} );
bcCounter++;
} );
if( bcCounter == 0 )
{
return;
}