-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSv_problem.hh
More file actions
1549 lines (1258 loc) · 52.2 KB
/
Sv_problem.hh
File metadata and controls
1549 lines (1258 loc) · 52.2 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
#ifndef SV_PROBLEM_HH
#define SV_PROBLEM_HH
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/lac/sparse_direct.h>
#include <deal.II/distributed/shared_tria.h>
#include <deal.II/grid/tria.h>
#include <deal.II/grid/tria_accessor.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/numerics/vector_tools.h>
#include <deal.II/dofs/dof_accessor.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/numerics/data_out.h>
#include <deal.II/fe/mapping_q1.h>
#include <deal.II/fe/fe_dgq.h>
#include <deal.II/fe/fe_dg_vector.h>
#include <deal.II/fe/fe_interface_values.h>
#include <deal.II/lac/solver_richardson.h>
#include <deal.II/lac/solver_gmres.h>
#include <deal.II/lac/solver_cg.h>
#include <deal.II/lac/precondition.h>
#include <deal.II/lac/precondition_block.h>
#include <deal.II/lac/affine_constraints.h>
#include <deal.II/numerics/matrix_tools.h>
#include <deal.II/base/tensor_product_polynomials.h>
#include <deal.II/base/polynomial.h>
#include <deal.II/fe/fe_face.h>
#include <deal.II/base/conditional_ostream.h>
#include <deal.II/base/mpi.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/dofs/dof_renumbering.h>
#include <deal.II/meshworker/mesh_loop.h>
#include <deal.II/meshworker/scratch_data.h>
#include <deal.II/base/parameter_handler.h>
#include "utilities/AverageGradientOperators.hh"
#include "aux_primary.hh"
// PETSc stuff
#include <deal.II/lac/petsc_vector.h>
#include <deal.II/lac/petsc_sparse_matrix.h>
#include <deal.II/lac/petsc_solver.h>
#include <deal.II/lac/petsc_precondition.h>
#include <iostream>
#include <fstream>
#include <algorithm>
namespace VaporSaturation
{
using namespace dealii;
struct CopyDataFace
{
FullMatrix<double> cell_matrix;
Vector<double> cell_rhs;
std::vector<types::global_dof_index> joint_dof_indices;
std::array<unsigned int, 2> cell_indices;
std::array<double, 2> values;
};
struct CopyData
{
FullMatrix<double> cell_matrix;
Vector<double> cell_rhs;
std::vector<types::global_dof_index> local_dof_indices;
std::vector<CopyDataFace> face_data;
double value;
unsigned int cell_index;
template <class Iterator>
void reinit_matrix(const Iterator &cell, unsigned int dofs_per_cell)
{
cell_matrix.reinit(dofs_per_cell, dofs_per_cell);
local_dof_indices.resize(dofs_per_cell);
cell->get_dof_indices(local_dof_indices);
}
template <class Iterator>
void reinit_rhs(const Iterator &cell, unsigned int dofs_per_cell)
{
cell_rhs.reinit(dofs_per_cell);
local_dof_indices.resize(dofs_per_cell);
cell->get_dof_indices(local_dof_indices);
}
};
template <int dim>
class VaporSaturationProblem
{
public:
VaporSaturationProblem(Triangulation<dim, dim> &triangulation_,
const unsigned int degree_,
double theta_Sv_, double penalty_Sv_,
double penalty_Sv_bdry_, std::vector<unsigned int> dirichlet_id_sv_, bool use_exact_pl_in_Sv_,
bool use_exact_Sa_in_Sv_,
bool second_order_time_derivative_, bool second_order_extrapolation_,
bool use_direct_solver_, bool Stab_v_, bool incompressible_, bool project_Darcy_with_gravity_,
PETScWrappers::MPI::Vector kappa_abs_vec_,
const unsigned int degreeRT_, bool project_only_kappa_,
MPI_Comm mpi_communicator_, const unsigned int n_mpi_processes_, const unsigned int this_mpi_process_);
void setup_system();
void assemble_system_matrix_vapor_saturation(double time_step_, double time_, unsigned int timestep_number_,bool rebuild_matrix_,
PETScWrappers::MPI::Vector pl_solution_,
PETScWrappers::MPI::Vector pl_solution_n_,
PETScWrappers::MPI::Vector pl_solution_nminus1_,
PETScWrappers::MPI::Vector Sa_solution_,
PETScWrappers::MPI::Vector Sa_solution_n_, PETScWrappers::MPI::Vector Sa_solution_nminus1_,
PETScWrappers::MPI::Vector Sv_solution_n_, PETScWrappers::MPI::Vector Sv_solution_nminus1_,
PETScWrappers::MPI::Vector totalDarcyvelocity_RT_);
void solve_vapor_saturation(PETScWrappers::MPI::Vector pl_solution_);
PETScWrappers::MPI::Vector Sv_solution;
private:
parallel::shared::Triangulation<dim> triangulation;
const MappingQ1<dim> mapping;
const QGauss<dim> quadrature;
const QGauss<dim - 1> face_quadrature;
using ScratchData = MeshWorker::ScratchData<dim>;
FE_DGQ<dim> fe;
DoFHandler<dim> dof_handler;
const unsigned int degree;
MPI_Comm mpi_communicator;
const unsigned int n_mpi_processes;
const unsigned int this_mpi_process;
ConditionalOStream pcout;
IndexSet locally_owned_dofs;
IndexSet locally_relevant_dofs;
IndexSet locally_owned_dofs_RT;
IndexSet locally_relevant_dofs_RT;
SparsityPattern sparsity_pattern;
PETScWrappers::MPI::SparseMatrix system_matrix_vapor_saturation;
PETScWrappers::MPI::Vector right_hand_side_vapor_saturation;
PETScWrappers::MPI::Vector pl_solution;
PETScWrappers::MPI::Vector pl_solution_n;
PETScWrappers::MPI::Vector pl_solution_nminus1;
PETScWrappers::MPI::Vector Sv_solution_n;
PETScWrappers::MPI::Vector Sv_solution_nminus1;
PETScWrappers::MPI::Vector Sa_solution;
PETScWrappers::MPI::Vector Sa_solution_n;
PETScWrappers::MPI::Vector Sa_solution_nminus1;
FE_DGQ<dim> fe_dg0;
DoFHandler<dim> dof_handler_dg0;
IndexSet locally_owned_dofs_dg0;
IndexSet locally_relevant_dofs_dg0;
PETScWrappers::MPI::Vector kappa_abs_vec;
double time_step;
double time;
unsigned int timestep_number;
bool rebuild_matrix;
double penalty_Sv;
double penalty_Sv_bdry;
std::vector<unsigned int> dirichlet_id_sv;
double theta_Sv;
bool Stab_v;
bool incompressible;
bool second_order_time_derivative;
bool second_order_extrapolation;
bool use_direct_solver;
bool project_Darcy_with_gravity;
bool project_only_kappa;
bool use_exact_pl_in_Sv;
bool use_exact_Sa_in_Sv;
PETScWrappers::MPI::Vector totalDarcyvelocity_RT;
const unsigned int degreeRT;
FE_RaviartThomas<dim> fe_RT;
DoFHandler<dim> dof_handler_RT;
AffineConstraints<double> constraints;
};
template <int dim>
VaporSaturationProblem<dim>::VaporSaturationProblem(Triangulation<dim, dim> &triangulation_,
const unsigned int degree_,
double theta_Sv_, double penalty_Sv_,
double penalty_Sv_bdry_, std::vector<unsigned int> dirichlet_id_sv_, bool use_exact_pl_in_Sv_,
bool use_exact_Sa_in_Sv_,
bool second_order_time_derivative_, bool second_order_extrapolation_,
bool use_direct_solver_,bool Stab_v_, bool incompressible_, bool project_Darcy_with_gravity_,
PETScWrappers::MPI::Vector kappa_abs_vec_,
const unsigned int degreeRT_, bool project_only_kappa_,
MPI_Comm mpi_communicator_, const unsigned int n_mpi_processes_, const unsigned int this_mpi_process_)
: triangulation(MPI_COMM_WORLD)
, mapping()
, degree(degree_)
, fe(degree_)
, quadrature(degree_ + 1)
, face_quadrature(degree_ + 1)
, degreeRT(degreeRT_)
, fe_RT(degreeRT_)
, theta_Sv(theta_Sv_)
, penalty_Sv(penalty_Sv_)
, penalty_Sv_bdry(penalty_Sv_bdry_)
, dirichlet_id_sv(dirichlet_id_sv_)
, use_exact_pl_in_Sv(use_exact_pl_in_Sv_)
, use_exact_Sa_in_Sv(use_exact_Sa_in_Sv_)
, second_order_time_derivative(second_order_time_derivative_)
, second_order_extrapolation(second_order_extrapolation_)
, Stab_v(Stab_v_)
, incompressible(incompressible_)
, use_direct_solver(use_direct_solver_)
, project_Darcy_with_gravity(project_Darcy_with_gravity_)
, project_only_kappa(project_only_kappa_)
, kappa_abs_vec(kappa_abs_vec_)
, dof_handler(triangulation)
, dof_handler_RT(triangulation)
, fe_dg0(0)
, dof_handler_dg0(triangulation)
, mpi_communicator(mpi_communicator_)
, n_mpi_processes(n_mpi_processes_)
, this_mpi_process(this_mpi_process_)
, pcout(std::cout, (this_mpi_process == 0))
{
triangulation.copy_triangulation(triangulation_);
}
template <int dim>
void VaporSaturationProblem<dim>::setup_system()
{
dof_handler.distribute_dofs(fe);
dof_handler_RT.distribute_dofs(fe_RT);
constraints.clear();
constraints.close();
DynamicSparsityPattern dsp(dof_handler.n_dofs());
DoFTools::make_flux_sparsity_pattern(dof_handler, dsp);
sparsity_pattern.copy_from(dsp);
const std::vector<IndexSet> locally_owned_dofs_per_proc =
DoFTools::locally_owned_dofs_per_subdomain(dof_handler);
locally_owned_dofs = locally_owned_dofs_per_proc[this_mpi_process];
DoFTools::extract_locally_relevant_dofs(dof_handler, locally_relevant_dofs);
const std::vector<IndexSet> locally_owned_dofs_per_proc_RT =
DoFTools::locally_owned_dofs_per_subdomain(dof_handler_RT);
locally_owned_dofs_RT = locally_owned_dofs_per_proc_RT[this_mpi_process];
DoFTools::extract_locally_relevant_dofs(dof_handler_RT, locally_relevant_dofs_RT);
dof_handler_dg0.distribute_dofs(fe_dg0);
const std::vector<IndexSet> locally_owned_dofs_per_proc_dg0 =
DoFTools::locally_owned_dofs_per_subdomain(dof_handler_dg0);
locally_owned_dofs_dg0 = locally_owned_dofs_per_proc_dg0[this_mpi_process];
DoFTools::extract_locally_relevant_dofs(dof_handler_dg0, locally_relevant_dofs_dg0);
}
template <int dim>
void VaporSaturationProblem<dim>::assemble_system_matrix_vapor_saturation(double time_step_, double time_, unsigned int timestep_number_, bool rebuild_matrix_,
PETScWrappers::MPI::Vector pl_solution_,
PETScWrappers::MPI::Vector pl_solution_n_,
PETScWrappers::MPI::Vector pl_solution_nminus1_,
PETScWrappers::MPI::Vector Sa_solution_,
PETScWrappers::MPI::Vector Sa_solution_n_, PETScWrappers::MPI::Vector Sa_solution_nminus1_,
PETScWrappers::MPI::Vector Sv_solution_n_, PETScWrappers::MPI::Vector Sv_solution_nminus1_,
PETScWrappers::MPI::Vector totalDarcyvelocity_RT_)
{
rebuild_matrix = rebuild_matrix_;
if (rebuild_matrix){
system_matrix_vapor_saturation.reinit(locally_owned_dofs,
locally_owned_dofs,
sparsity_pattern,
mpi_communicator);
}
right_hand_side_vapor_saturation.reinit(locally_owned_dofs, mpi_communicator);
//time terms
time_step = time_step_;
time = time_;
timestep_number = timestep_number_;
const FEValuesExtractors::Vector velocities(0);
using Iterator = typename DoFHandler<dim>::active_cell_iterator;
BoundaryValuesVaporSaturation<dim> boundary_function;
RightHandSideVaporSaturation<dim> right_hand_side_fcn;
GravitySourceTerm<dim> gravity_fcn;
// Liquid Pressure
ExactLiquidPressure<dim> pl_fcn;
// Saturations
ExactAqueousSaturation<dim> Sa_fcn;
// Vapor saturation
ExactVaporSaturation<dim> Sv_fcn;
// Porosity
porosity<dim> porosity_fcn;
// Densities
rho_l<dim> rho_l_fcn;
rho_v<dim> rho_v_fcn;
rho_a<dim> rho_a_fcn;
// Mobilities
lambda_l<dim> lambda_l_fcn;
lambda_v<dim> lambda_v_fcn;
lambda_a<dim> lambda_a_fcn;
// Stabilization term
StabVaporSaturation<dim> Kappa_tilde_v_fcn;
double Kappa_tilde_v = Kappa_tilde_v_fcn.value();
// Capillary pressures
CapillaryPressurePcv<dim> cap_p_pcv_fcn;
// Neumann term
NeumannTermVaporSaturation<dim> neumann_fcn;
PETScWrappers::MPI::Vector temp_pl_solution;
PETScWrappers::MPI::Vector temp_pl_solution_n;
PETScWrappers::MPI::Vector temp_pl_solution_nminus1;
PETScWrappers::MPI::Vector temp_Sa_solution;
PETScWrappers::MPI::Vector temp_Sa_solution_n;
PETScWrappers::MPI::Vector temp_Sa_solution_nminus1;
PETScWrappers::MPI::Vector temp_Sv_solution_n;
PETScWrappers::MPI::Vector temp_Sv_solution_nminus1;
PETScWrappers::MPI::Vector temp_totalDarcyVelocity_RT;
PETScWrappers::MPI::Vector temp_kappa;
temp_pl_solution.reinit(locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
temp_pl_solution_n.reinit(locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
temp_pl_solution_nminus1.reinit(locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
temp_Sa_solution.reinit(locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
temp_Sa_solution_n.reinit(locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
temp_Sa_solution_nminus1.reinit(locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
temp_Sv_solution_n.reinit(locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
temp_Sv_solution_nminus1.reinit(locally_owned_dofs,
locally_relevant_dofs,
mpi_communicator);
temp_totalDarcyVelocity_RT.reinit(locally_owned_dofs_RT,
locally_relevant_dofs_RT,
mpi_communicator);
temp_kappa.reinit(locally_owned_dofs_dg0,
locally_relevant_dofs_dg0,
mpi_communicator);
temp_pl_solution = pl_solution_;
temp_pl_solution_n = pl_solution_n_;
temp_pl_solution_nminus1 = pl_solution_nminus1_;
temp_Sa_solution = Sa_solution_;
temp_Sa_solution_n = Sa_solution_n_;
temp_Sa_solution_nminus1 = Sa_solution_nminus1_;
temp_Sv_solution_n = Sv_solution_n_;
temp_Sv_solution_nminus1 = Sv_solution_nminus1_;
temp_totalDarcyVelocity_RT = totalDarcyvelocity_RT_;
temp_kappa = kappa_abs_vec;
// Volume integrals
const auto cell_worker = [&](const auto &cell,
auto &scratch_data,
auto & copy_data)
{
const FEValues<dim> &fe_v = scratch_data.reinit(cell);
const unsigned int n_dofs = fe_v.dofs_per_cell;
if (rebuild_matrix)
{
copy_data.reinit_matrix(cell, n_dofs);
}
copy_data.reinit_rhs(cell, n_dofs);
const auto &q_points = fe_v.get_quadrature_points();
const int n_qpoints = q_points.size();
const std::vector<double> &JxW = fe_v.get_JxW_values();
FEValues<dim> fe_values_RT(fe_RT,
quadrature,
update_values);
typename DoFHandler<dim>::cell_iterator cell_RT(&triangulation,
cell->level(), cell->index(), &dof_handler_RT);
fe_values_RT.reinit(cell_RT);
std::vector<double> rhs_values(n_qpoints);
right_hand_side_fcn.set_time(time);
right_hand_side_fcn.value_list(q_points, rhs_values);
gravity_fcn.set_time(time);
std::vector<double> pl_vals(n_qpoints);
std::vector<double> old_pl_vals(n_qpoints);
std::vector<double> old_pl_vals_nminus1(n_qpoints);
std::vector<Tensor<1, dim>> pl_grads(n_qpoints);
std::vector<double> Sa_vals(n_qpoints);
std::vector<double> old_Sa_vals(n_qpoints);
std::vector<double> old_Sa_vals_nminus1(n_qpoints);
std::vector<double> old_Sv_vals(n_qpoints);
std::vector<double> old_Sv_vals_nminus1(n_qpoints);
std::vector<Tensor<1, dim>> old_Sv_grads(n_qpoints);
std::vector<Tensor<1, dim>> old_Sv_grads_nminus1(n_qpoints);
fe_v.get_function_values(temp_pl_solution, pl_vals);
fe_v.get_function_values(temp_pl_solution_n, old_pl_vals);
fe_v.get_function_values(temp_pl_solution_nminus1, old_pl_vals_nminus1);
fe_v.get_function_gradients(temp_pl_solution, pl_grads);
fe_v.get_function_values(temp_Sa_solution, Sa_vals);
fe_v.get_function_values(temp_Sa_solution_n, old_Sa_vals);
fe_v.get_function_values(temp_Sa_solution_nminus1, old_Sa_vals_nminus1);
fe_v.get_function_values(temp_Sv_solution_n, old_Sv_vals);
fe_v.get_function_values(temp_Sv_solution_nminus1, old_Sv_vals_nminus1);
fe_v.get_function_gradients(temp_Sv_solution_n, old_Sv_grads);
fe_v.get_function_gradients(temp_Sv_solution_nminus1, old_Sv_grads_nminus1);
std::vector<Tensor<1, dim>> DarcyVelocities(n_qpoints);
fe_values_RT[velocities].get_function_values(temp_totalDarcyVelocity_RT, DarcyVelocities);
double kappa = temp_kappa[cell->global_active_cell_index()];
for (unsigned int point = 0; point < n_qpoints; ++point)
{
double pl_value = pl_vals[point];
double pl_value_n = old_pl_vals[point];
double pl_value_nminus1 = old_pl_vals_nminus1[point];
Tensor<1,dim> pl_grad = pl_grads[point];
if(use_exact_pl_in_Sv)
{
pl_fcn.set_time(time);
pl_value = pl_fcn.value(q_points[point]);
pl_grad = pl_fcn.gradient(q_points[point]);
pl_fcn.set_time(time - time_step);
pl_value_n = pl_fcn.value(q_points[point]);
pl_fcn.set_time(time - 2.0*time_step);
pl_value_nminus1 = pl_fcn.value(q_points[point]);
}
double Sa_value = Sa_vals[point];
double Sa_value_n = old_Sa_vals[point];
double Sa_value_nminus1 = old_Sa_vals_nminus1[point];
if(use_exact_Sa_in_Sv)
{
Sa_fcn.set_time(time);
Sa_value = Sa_fcn.value(q_points[point]);
Sa_fcn.set_time(time - time_step);
Sa_value_n = Sa_fcn.value(q_points[point]);
Sa_fcn.set_time(time - 2.0*time_step);
Sa_value_nminus1 = Sa_fcn.value(q_points[point]);
}
double Sv_value_n = old_Sv_vals[point];
double Sv_value_nminus1 = old_Sv_vals_nminus1[point];
Tensor<1,dim> Sv_grad_n = old_Sv_grads[point];
Tensor<1,dim> Sv_grad_nminus1 = old_Sv_grads_nminus1[point];
Tensor<1,dim> totalDarcyVelo = DarcyVelocities[point];
double Sv_nplus1_extrapolation = Sv_value_n;
Tensor<1,dim> Sv_grad_nplus1_extrapolation = Sv_grad_n;
Tensor<1,dim> totalDarcyVelo_extrapolation = totalDarcyVelo;
if(second_order_extrapolation)
{
Sv_nplus1_extrapolation *= 2.0;
Sv_nplus1_extrapolation -= Sv_value_nminus1;
Sv_grad_nplus1_extrapolation *=2.0;
Sv_grad_nplus1_extrapolation -= Sv_grad_nminus1;
}
double phi_nplus1 = porosity_fcn.value(pl_value);
double phi_n = porosity_fcn.value(pl_value_n);
double phi_nminus1 = porosity_fcn.value(pl_value_nminus1);
double rho_l = rho_l_fcn.value(pl_value);
double rho_v = rho_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double rho_v_extr = rho_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double rho_a = rho_a_fcn.value(pl_value);
double rho_v_n = rho_v_fcn.value(pl_value_n, Sa_value_n, Sv_value_n);
double rho_v_nminus1 = rho_v_fcn.value(pl_value_nminus1, Sa_value_nminus1, Sv_value_nminus1);
if(incompressible)
{
rho_l = rho_v = rho_a = 1.0;
rho_v_n = 1.0;
rho_v_nminus1 = 1.0;
}
double lambda_l = lambda_l_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_v = lambda_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_a = lambda_a_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_l_extr = lambda_l_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_v_extr = lambda_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_a_extr = lambda_a_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double rholambda_t = rho_l*lambda_l + rho_v*lambda_v + rho_a*lambda_a;
double rholambda_t_extr = rho_l*lambda_l_extr + rho_v_extr*lambda_v_extr + rho_a*lambda_a_extr;
double dpcv_dSv = cap_p_pcv_fcn.derivative_wrt_Sv(Sv_nplus1_extrapolation);
for (unsigned int i = 0; i < n_dofs; ++i)
{
if (rebuild_matrix)
{
for (unsigned int j = 0; j < n_dofs; ++j)
{
// Time term
if(!second_order_time_derivative)
{
// Time term
copy_data.cell_matrix(i,j) +=
(1.0/time_step)
* phi_nplus1
* rho_v
* fe_v.shape_value(i, point)
* fe_v.shape_value(j, point)
* JxW[point];
}
else
{
copy_data.cell_matrix(i,j) +=
(1.0/time_step)
* 1.5
* phi_nplus1
* rho_v
* fe_v.shape_value(i, point)
* fe_v.shape_value(j, point)
* JxW[point];
}
if(Stab_v)
{
copy_data.cell_matrix(i,j) +=
Kappa_tilde_v
* kappa
* fe_v.shape_grad(i, point)
* fe_v.shape_grad(j, point)
* JxW[point];
}
else
{
copy_data.cell_matrix(i,j) +=
rho_v
* lambda_v
* dpcv_dSv
* kappa
* fe_v.shape_grad(i, point)
* fe_v.shape_grad(j, point)
* JxW[point];
}
}
}
// Source term
copy_data.cell_rhs(i) += right_hand_side_fcn.value(q_points[point])
* fe_v.shape_value(i, point)
* JxW[point];
// Time term
if(!second_order_time_derivative)
{
copy_data.cell_rhs(i) += (1.0/time_step) * phi_n * rho_v_n * Sv_value_n
* fe_v.shape_value(i, point) * JxW[point];
}
else
{
copy_data.cell_rhs(i) += (1.0/time_step)
* (2.0 * phi_n * rho_v_n * Sv_value_n
- 0.5 * phi_nminus1 * rho_v_nminus1 * Sv_value_nminus1)
* fe_v.shape_value(i, point) * JxW[point];
}
// Diffusion term moved to RHS - stab method
if (Stab_v)
{
copy_data.cell_rhs(i) += (-rho_v * lambda_v * dpcv_dSv + Kappa_tilde_v)
* kappa * Sv_grad_nplus1_extrapolation
* fe_v.shape_grad(i, point) * JxW[point];
}
// Darcy term. Coefficient depends on what was projected
if(project_only_kappa)
copy_data.cell_rhs(i) += (rho_v*lambda_v) * totalDarcyVelo_extrapolation
* fe_v.shape_grad(i, point) * JxW[point];
else
copy_data.cell_rhs(i) += (rho_v*lambda_v/rholambda_t_extr) * totalDarcyVelo_extrapolation
* fe_v.shape_grad(i, point) * JxW[point];
// Gravity term
if(!project_Darcy_with_gravity)
copy_data.cell_rhs(i) += kappa*rho_v_fcn.value(pl_value_n, Sa_value_n, Sv_value_n)*rho_v*lambda_v
* gravity_fcn.vector_value(q_points[point])
* fe_v.shape_grad(i, point)
* JxW[point];
}
}
};
// Boundary face integrals
const auto boundary_worker = [&](const auto &cell,
const unsigned int &face_no,
auto &scratch_data,
auto & copy_data)
{
const FEFaceValuesBase<dim> &fe_face = scratch_data.reinit(cell, face_no);
const auto &q_points = scratch_data.get_quadrature_points();
const int n_qpoints = q_points.size();
const unsigned int n_facet_dofs = fe_face.dofs_per_cell;
const std::vector<double> & JxW = scratch_data.get_JxW_values();
const std::vector<Tensor<1, dim>> &normals = scratch_data.get_normal_vectors();
FEFaceValues<dim> fe_face_values_RT(fe_RT,
face_quadrature,
update_values);
typename DoFHandler<dim>::cell_iterator cell_RT(&triangulation,
cell->level(), cell->index(), &dof_handler_RT);
fe_face_values_RT.reinit(cell_RT, face_no);
std::vector<double> g(n_qpoints);
boundary_function.set_time(time);
boundary_function.value_list(q_points, g);
gravity_fcn.set_time(time);
neumann_fcn.set_time(time);
std::vector<double> pl_vals(n_qpoints);
std::vector<Tensor<1, dim>> pl_grads(n_qpoints);
std::vector<double> Sa_vals(n_qpoints);
std::vector<double> old_Sa_vals(n_qpoints);
std::vector<double> old_Sa_vals_nminus1(n_qpoints);
std::vector<double> old_Sv_vals(n_qpoints);
std::vector<double> old_Sv_vals_nminus1(n_qpoints);
std::vector<Tensor<1, dim>> old_Sv_grads(n_qpoints);
std::vector<Tensor<1, dim>> old_Sv_grads_nminus1(n_qpoints);
fe_face.get_function_values(temp_pl_solution, pl_vals);
fe_face.get_function_gradients(temp_pl_solution, pl_grads);
fe_face.get_function_values(temp_Sa_solution, Sa_vals);
fe_face.get_function_values(temp_Sa_solution_n, old_Sa_vals);
fe_face.get_function_values(temp_Sa_solution_nminus1, old_Sa_vals_nminus1);
fe_face.get_function_values(temp_Sv_solution_n, old_Sv_vals);
fe_face.get_function_values(temp_Sv_solution_nminus1, old_Sv_vals_nminus1);
fe_face.get_function_gradients(temp_Sv_solution_n, old_Sv_grads);
fe_face.get_function_gradients(temp_Sv_solution_nminus1, old_Sv_grads_nminus1);
std::vector<Tensor<1, dim>> DarcyVelocities(n_qpoints);
fe_face_values_RT[velocities].get_function_values(temp_totalDarcyVelocity_RT, DarcyVelocities);
double kappa = temp_kappa[cell->global_active_cell_index()];
// Figure out if this face is Dirichlet or Neumann
bool dirichlet = false;
for(unsigned int i = 0; i < dirichlet_id_sv.size(); i++)
{
if(cell->face(face_no)->boundary_id() == dirichlet_id_sv[i])
{
dirichlet = true;
break;
}
}
// Dirichlet part
if(dirichlet)
{
for (unsigned int point = 0; point < n_qpoints; ++point)
{
double pl_value = pl_vals[point];
Tensor<1,dim> pl_grad = pl_grads[point];
if(use_exact_pl_in_Sv)
{
pl_fcn.set_time(time);
pl_value = pl_fcn.value(q_points[point]);
pl_grad = pl_fcn.gradient(q_points[point]);
}
double Sa_value = Sa_vals[point];
double Sa_value_n = old_Sa_vals[point];
double Sa_value_nminus1 = old_Sa_vals_nminus1[point];
if(use_exact_Sa_in_Sv)
{
Sa_fcn.set_time(time);
Sa_value = Sa_fcn.value(q_points[point]);
Sa_fcn.set_time(time - time_step);
Sa_value_n = Sa_fcn.value(q_points[point]);
Sa_fcn.set_time(time - 2.0*time_step);
Sa_value_nminus1 = Sa_fcn.value(q_points[point]);
}
double Sv_value_n = old_Sv_vals[point];
double Sv_value_nminus1 = old_Sv_vals_nminus1[point];
Tensor<1,dim> Sv_grad_n = old_Sv_grads[point];
Tensor<1,dim> Sv_grad_nminus1 = old_Sv_grads_nminus1[point];
Tensor<1,dim> totalDarcyVelo = DarcyVelocities[point];
double Sv_nplus1_extrapolation = Sv_value_n;
Tensor<1,dim> Sv_grad_nplus1_extrapolation = Sv_grad_n;
Tensor<1,dim> totalDarcyVelo_extrapolation = totalDarcyVelo;
if(second_order_extrapolation)
{
Sv_nplus1_extrapolation *= 2.0;
Sv_nplus1_extrapolation -= Sv_value_nminus1;
Sv_grad_nplus1_extrapolation *= 2.0;
Sv_grad_nplus1_extrapolation -= Sv_grad_nminus1;
}
double rho_l = rho_l_fcn.value(pl_value);
double rho_v = rho_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double rho_v_extr = rho_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double rho_a = rho_a_fcn.value(pl_value);
if(incompressible)
{
rho_l = rho_v = rho_a = 1.0;
}
double lambda_l = lambda_l_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_v = lambda_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_a = lambda_a_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_l_extr = lambda_l_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_v_extr = lambda_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double lambda_a_extr = lambda_a_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double rholambda_t = rho_l*lambda_l + rho_v*lambda_v + rho_a*lambda_a;
double rholambda_t_extr = rho_l*lambda_l_extr + rho_v_extr*lambda_v_extr + rho_a*lambda_a_extr;
double dpcv_dSv = cap_p_pcv_fcn.derivative_wrt_Sv(Sv_nplus1_extrapolation);
double gamma_Sv_e;
if (Stab_v)
{
gamma_Sv_e = fabs(Kappa_tilde_v*kappa);
}
else
{
gamma_Sv_e = fabs(rho_v*lambda_v*kappa*dpcv_dSv);
}
if(!Stab_v){
gamma_Sv_e += sqrt(totalDarcyVelo_extrapolation*totalDarcyVelo_extrapolation);
}
double h_e = cell->face(face_no)->measure();
double penalty_factor = (penalty_Sv_bdry/h_e) * gamma_Sv_e * degree*(degree + dim - 1);
for (unsigned int i = 0; i < n_facet_dofs; ++i)
{
if (rebuild_matrix)
{
for (unsigned int j = 0; j < n_facet_dofs; ++j)
{
if(Stab_v)
{
// Diffusion term
copy_data.cell_matrix(i, j) -=
Kappa_tilde_v
* kappa
* fe_face.shape_value(i, point)
* fe_face.shape_grad(j, point)
* normals[point]
* JxW[point];
//Theta term
copy_data.cell_matrix(i, j) +=
theta_Sv
* Kappa_tilde_v
* kappa
* fe_face.shape_grad(i, point)
* normals[point]
* fe_face.shape_value(j, point)
* JxW[point];
}
else
{
// Diffusion term
copy_data.cell_matrix(i, j) -=
rho_v
* lambda_v
* dpcv_dSv
* kappa
* fe_face.shape_value(i, point)
* fe_face.shape_grad(j, point)
* normals[point]
* JxW[point];
//theta term
copy_data.cell_matrix(i, j) +=
theta_Sv
* rho_v
* lambda_v
* dpcv_dSv
* kappa
* fe_face.shape_grad(i, point)
* normals[point]
* fe_face.shape_value(j, point)
* JxW[point];
}
// Boundary condition
copy_data.cell_matrix(i, j) +=
penalty_factor
* fe_face.shape_value(i, point)
* fe_face.shape_value(j, point)
* JxW[point];
}
}
// Boundary condition
copy_data.cell_rhs(i) += penalty_factor
* fe_face.shape_value(i, point)
* g[point]
* JxW[point];
// added to RHS - stab method
if (Stab_v)
{
copy_data.cell_rhs(i) -= ( Kappa_tilde_v - rho_v*lambda_v*dpcv_dSv)
* kappa
* Sv_grad_nplus1_extrapolation
* normals[point]
* fe_face.shape_value(i, point)
* JxW[point];
copy_data.cell_rhs(i) += theta_Sv
* Kappa_tilde_v
* kappa
* fe_face.shape_grad(i, point)
* normals[point]
* g[point]
* JxW[point];
}
else
{
// No term added to RHS
copy_data.cell_rhs(i) += theta_Sv
* rho_v
* lambda_v
* dpcv_dSv
* kappa
* fe_face.shape_grad(i, point)
* normals[point]
* g[point]
* JxW[point];
}
// Darcy vel
if(project_only_kappa)
copy_data.cell_rhs(i) -= (rho_v*lambda_v)
* totalDarcyVelo_extrapolation
* normals[point]
* fe_face.shape_value(i, point)
* JxW[point];
else
copy_data.cell_rhs(i) -= (rho_v*lambda_v/rholambda_t_extr)
* totalDarcyVelo_extrapolation
* normals[point]
* fe_face.shape_value(i, point)
* JxW[point];
// Gravity
if(!project_Darcy_with_gravity)
copy_data.cell_rhs(i) -= kappa*rho_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation)*rho_v*lambda_v
* gravity_fcn.vector_value(q_points[point])
* normals[point]
* fe_face.shape_value(i, point)
* JxW[point];
}
}
}
else // Neumann boundary
{
for (unsigned int point = 0; point < q_points.size(); ++point)
{
double pl_value = pl_vals[point];
Tensor<1,dim> pl_grad = pl_grads[point];
if(use_exact_pl_in_Sv)
{
pl_fcn.set_time(time);
pl_value = pl_fcn.value(q_points[point]);
pl_grad = pl_fcn.gradient(q_points[point]);
}
double Sa_value = Sa_vals[point];
if(use_exact_Sa_in_Sv)
{
Sa_fcn.set_time(time);
Sa_value = Sa_fcn.value(q_points[point]);
}
double Sv_value_n = old_Sv_vals[point];
double Sv_value_nminus1 = old_Sv_vals_nminus1[point];
double Sv_nplus1_extrapolation = Sv_value_n;
if(second_order_extrapolation)
{
Sv_nplus1_extrapolation *= 2.0;
Sv_nplus1_extrapolation -= Sv_value_nminus1;
}
Tensor<1,dim> neumann_term = neumann_fcn.vector_value(q_points[point]);
Tensor<1,dim> totalDarcyVelo = DarcyVelocities[point];
double rho_l = rho_l_fcn.value(pl_value);
double rho_v = rho_v_fcn.value(pl_value, Sa_value, Sv_nplus1_extrapolation);
double rho_a = rho_a_fcn.value(pl_value);
if(incompressible)
{
rho_l = rho_v = rho_a = 1.0;
}