/
solitary_wave.cc
1033 lines (875 loc) · 40 KB
/
solitary_wave.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2011 - 2023 by the authors of the ASPECT code.
This file is part of ASPECT.
ASPECT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
ASPECT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ASPECT; see the file LICENSE. If not see
<http://www.gnu.org/licenses/>.
*/
#include <aspect/melt.h>
#include <aspect/initial_composition/interface.h>
#include <aspect/postprocess/interface.h>
#include <aspect/gravity_model/interface.h>
#include <aspect/geometry_model/interface.h>
#include <aspect/simulator_access.h>
#include <aspect/global.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/numerics/data_out.h>
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/function_lib.h>
#include <deal.II/numerics/error_estimator.h>
#include <deal.II/numerics/vector_tools.h>
/**
* MPI operator used by MPI_max_and_data
*/
void myop_func(void *invec,
void *inoutvec,
int *len,
MPI_Datatype *datatype)
{
AssertThrow(*len > 1, dealii::ExcNotImplemented());
AssertThrow(*datatype == MPI_DOUBLE, dealii::ExcNotImplemented());
double *indata = static_cast<double *>(invec);
double *inoutdata = static_cast<double *>(inoutvec);
if (indata[0]>inoutdata[0])
{
for (int i=0; i<*len; ++i)
inoutdata[i] = indata[i];
}
}
/**
* Computes MPI_MAX of @p local_max like Allreduce, but also transmits the
* @p local_data from the rank with the largest @local_max to every rank
* (returned in @p global_data).
*/
void MPI_max_and_data(const double &local_max,
const double &local_data,
double &global_max,
double &global_data)
{
MPI_Op myop;
MPI_Op_create(&myop_func, /* commutes? */ 1 ,&myop);
double local[] = {local_max, local_data};
double global[2];
MPI_Allreduce(local, global, 2, MPI_DOUBLE, myop, MPI_COMM_WORLD);
global_max = global[0];
global_data = global[1];
MPI_Op_free(&myop);
}
namespace aspect
{
/**
* This is the "Solitary wave" benchmark defined in the following paper:
* @code
* @Article{DMGT11,
* author = {T. Keller and D. A. May and B. J. P. Kaus},
* title = {Numerical modelling of magma dynamics coupled
* to tectonic deformation of lithosphere and crust},
* journal = {Geophysical Journal International},
* year = 2013,
* volume = 195(3),
* pages = {1406-1442}
* @endcode
*
* To calculate the initial condition, which is a solitary wave solution of
* the magma dynamics equations, we use the equation for the one-dinemsional
* case and the non-dimensionalization as it is described in
* @code
* @Article{SS11,
* author = {G. Simpson and M. Spiegelman},
* title = {Solitary Wave Benchmarks in Magma Dynamics},
* journal = {Journal of Scientific Computing},
* year = 2011,
* volume = 49(3),
* pages = {268-290}
* @endcode
*
* Specifically, this means that we scale the porosity with the background
* porosity, and the coordinates with the compaction length $\delta_0$, which is
* defined as $\sqrt \frac{k(\phi_0) \xi^{*}+4/3 \eta^{*}}{\eta_f}$. $k(\phi_0)$ is the
* permeability at background porosity, $\xi^{*}$ is the compaction viscosity,
* $\eta^{*}$ is the shear viscosity of the fluid and $\eta_f$ is the shear viscosity
* of the melt.
*/
namespace SolitaryWaveBenchmark
{
using namespace dealii;
namespace AnalyticSolutions
{
// vectors to store the porosity field and the corresponding coordinate in
const unsigned int max_points = 2e8;
std::vector<double> porosity, coordinate;
/**
* @note The solitary wave solution only exists as a function x = func(phi)
* and not phi = func(x), which is what we would like to have for describing
* the shape of the wave. Thus, we calculate x = func(phi) for a range of phis
* between the background porosity and the amplitude of the wave. In a next
* step, we interpolate these values to the grid.
*
* @param phi The characteristic shape of the wave, with phi --> 1
* for x --> +- infinity
*
* @param amplitude The amplitude of the solitary wave, which is always
* greater than 1.
*/
double solitary_wave_solution (const double phi, const double amplitude)
{
AssertThrow(phi > 1.0 && phi <= amplitude,
ExcMessage("The solitary wave solution can only be computed "
"for porosities larger than the background porosity of 1 "
"and smaller than or equal to the amplitude of the wave."));
AssertThrow(amplitude > 1,
ExcMessage("Amplitude of the solitary wave must be larger than 1!"));
const double A_1 = std::sqrt(amplitude - 1.0);
const double A_phi = std::sqrt(amplitude - phi);
return std::sqrt(amplitude + 0.5)
* (2 * A_phi - 1.0/A_1 * std::log((A_1 - A_phi)/(A_1 + A_phi)));
}
/**
* This function reads the coordinate and the porosity of the solitary wave
* from an input file.
*
* @param filename Name of the input file.
*/
void read_solitary_wave_solution (const std::string &filename,
const MPI_Comm comm)
{
std::string temp;
std::stringstream in(Utilities::read_and_distribute_file_content(filename, comm));
while (!in.eof())
{
double x, f;
in >> x >> f;
if (in.eof())
break;
getline(in, temp);
coordinate.insert(coordinate.begin(),x);
porosity.insert(porosity.begin(),f);
}
}
/**
* This function gets the coordinate as an input parameters and gives
* back the porosity of the solitary wave. As this function is only defined
* implicitly, we have to interpolate from the coordinates where we have the
* porosity to our mesh.
*
* @param amplitude The amplitude of the solitary wave, which is always
* greater than 1.
* @param offset The offset of the center of the solitary wave from the
* boundary of the domain.
*/
void compute_porosity (const double amplitude,
const double background_porosity,
const double /*offset*/,
const double compaction_length,
const bool read_solution,
const std::string file_name,
const MPI_Comm comm)
{
// non-dimensionalize the amplitude
const double non_dim_amplitude = amplitude / background_porosity;
if (read_solution)
read_solitary_wave_solution(file_name, comm);
else
{
porosity.resize(max_points);
coordinate.resize(max_points);
// get the coordinates where we have the solution
for (unsigned int i=0; i<max_points; ++i)
{
porosity[i] = 1.0 + 1e-10*non_dim_amplitude
+ double(i)/double(max_points-1) * (non_dim_amplitude * (1.0 - 1e-10) - 1.0);
coordinate[i] = solitary_wave_solution(porosity[i], non_dim_amplitude);
}
}
for (unsigned int i=0; i<coordinate.size(); ++i)
{
// re-scale porosity and position
porosity[i] *= background_porosity;
coordinate[i] *= compaction_length;
}
}
double interpolate (const double position,
const double offset)
{
// interpolate from the solution grid to the mesh used in the simulation
// solitary wave is a monotonically decreasing function, so the coordinates
// should be in descending order
// we only have the solution of the solitary wave for
// coordinates larger than 0 (one half of the wave)
const double x = (position > offset
?
position - offset
:
offset - position);
if (x > coordinate[0])
return porosity[0];
unsigned int j= coordinate.size()-2;
unsigned int i = j/2;
while (!(x < coordinate[j] && x >= coordinate[j+1]))
{
if (x < coordinate[j])
j += i;
else
j -= i;
if (i>1)
i /= 2;
}
const double distance = (x - coordinate[j+1])
/(coordinate[j] - coordinate[j+1]);
return porosity[j+1] + distance * (porosity[j] - porosity[j+1]);
}
/**
* The exact solution for the Solitary wave benchmark.
*/
template <int dim>
class FunctionSolitaryWave : public Function<dim>
{
public:
FunctionSolitaryWave (const double offset, const double delta, const std::vector<double> &initial_pressure, const double max_z, const unsigned int n_components)
:
Function<dim>(n_components),
offset_(offset),
delta_(delta),
initial_pressure_(initial_pressure),
max_z_(max_z)
{}
void set_delta(const double delta)
{
delta_ = delta;
}
void vector_value (const Point<dim> &p,
Vector<double> &values) const override
{
unsigned int index = static_cast<int>((p[dim-1]-delta_)/max_z_ * (initial_pressure_.size()-1));
if (p[dim-1]-delta_ < 0)
index = 0;
else if (p[dim-1]-delta_ > max_z_)
index = initial_pressure_.size()-1;
AssertThrow(index < initial_pressure_.size(), ExcMessage("not in range"));
const double z_coordinate1 = static_cast<double>(index)/static_cast<double>(initial_pressure_.size()-1) * max_z_;
const double z_coordinate2 = static_cast<double>(index+1)/static_cast<double>(initial_pressure_.size()-1) * max_z_;
const double interpolated_pressure = (index == initial_pressure_.size()-1)
?
initial_pressure_[index]
:
initial_pressure_[index] + (initial_pressure_[index+1] - initial_pressure_[index])
* (p[dim-1]-delta_ - z_coordinate1) / (z_coordinate2 - z_coordinate1);
values[dim+2+dim+2] = AnalyticSolutions::interpolate(p[dim-1]-delta_,offset_); //porosity
values[dim+1] = interpolated_pressure; //compaction pressure
}
private:
const double offset_;
double delta_;
const std::vector<double> initial_pressure_;
const double max_z_;
};
}
/**
* An initial conditions model for the solitary waves benchmark.
*/
template <int dim>
class SolitaryWaveInitialCondition : public InitialComposition::Interface<dim>,
public ::aspect::SimulatorAccess<dim>
{
public:
/**
* Initialization function. Take references to the material model and
* get the compaction length, so that it can be used subsequently to
* compute the analytical solution for the shape of the solitary wave.
*/
void
initialize () override;
/**
* Return the boundary velocity as a function of position.
*/
double
initial_composition (const Point<dim> &position, const unsigned int n_comp) const override;
static
void
declare_parameters (ParameterHandler &prm);
void
parse_parameters (ParameterHandler &prm) override;
double
get_amplitude () const;
double
get_background_porosity () const;
double
get_offset () const;
private:
double amplitude;
double background_porosity;
double offset;
double compaction_length;
bool read_solution;
std::string file_name;
};
/**
* @note This benchmark only talks about the flow field, not about a
* temperature field. All quantities related to the temperature are
* therefore set to zero in the implementation of this class.
*
* @ingroup MaterialModels
*/
template <int dim>
class SolitaryWaveMaterial : public MaterialModel::MeltInterface<dim>, public ::aspect::SimulatorAccess<dim>
{
public:
bool is_compressible () const override
{
return false;
}
double reference_darcy_coefficient () const override
{
// Make sure we keep track of the initial composition manager and
// that it continues to live beyond the time when the simulator
// class releases its pointer to it.
if (initial_composition_manager == nullptr)
const_cast<std::shared_ptr<const aspect::InitialComposition::Manager<dim>>&>(initial_composition_manager)
= this->get_initial_composition_manager_pointer();
// Note that this number is based on the background porosity in the
// solitary wave initial condition.
const SolitaryWaveInitialCondition<dim> &initial_composition =
initial_composition_manager->template
get_matching_initial_composition_model<SolitaryWaveInitialCondition<dim>>();
return reference_permeability * pow(initial_composition.get_background_porosity(), 3.0) / eta_f;
}
double length_scaling (const double porosity) const
{
return std::sqrt(reference_permeability * std::pow(porosity,3) * (xi_0 + 4.0/3.0 * eta_0) / eta_f);
}
double velocity_scaling (const double porosity) const
{
const Point<dim> surface_point = this->get_geometry_model().representative_point(0.0);
return reference_permeability * std::pow(porosity,2) * (reference_rho_s - reference_rho_f)
* this->get_gravity_model().gravity_vector(surface_point).norm() / eta_f;
}
/**
* Declare the parameters this class takes through input files.
*/
static
void
declare_parameters (ParameterHandler &prm);
/**
* Read the parameters this class declares from the parameter file.
*/
void
parse_parameters (ParameterHandler &prm) override;
void evaluate(const typename MaterialModel::Interface<dim>::MaterialModelInputs &in,
typename MaterialModel::Interface<dim>::MaterialModelOutputs &out) const override
{
const unsigned int porosity_idx = this->introspection().compositional_index_for_name("porosity");
for (unsigned int i=0; i<in.n_evaluation_points(); ++i)
{
double porosity = in.composition[i][porosity_idx];
out.viscosities[i] = eta_0 * (1.0 - porosity);
out.densities[i] = reference_rho_s;
out.thermal_expansion_coefficients[i] = 0.0;
out.specific_heat[i] = 1.0;
out.thermal_conductivities[i] = 0.0;
out.compressibilities[i] = 0.0;
for (unsigned int c=0; c<in.composition[i].size(); ++c)
out.reaction_terms[i][c] = 0.0;
}
// fill melt outputs if they exist
aspect::MaterialModel::MeltOutputs<dim> *melt_out = out.template get_additional_output<aspect::MaterialModel::MeltOutputs<dim>>();
if (melt_out != NULL)
for (unsigned int i=0; i<in.n_evaluation_points(); ++i)
{
double porosity = in.composition[i][porosity_idx];
melt_out->compaction_viscosities[i] = xi_0 * (1.0 - porosity);
melt_out->fluid_viscosities[i]= eta_f;
melt_out->permeabilities[i]= reference_permeability * std::pow(porosity,3);
melt_out->fluid_densities[i]= reference_rho_f;
melt_out->fluid_density_gradients[i] = 0.0;
}
}
private:
double reference_rho_s;
double reference_rho_f;
double eta_0;
double xi_0;
double eta_f;
double reference_permeability;
/**
* A shared pointer to the initial composition object
* that ensures that the current object can continue
* to access the initial composition object beyond the
* first time step.
*/
std::shared_ptr<const aspect::InitialComposition::Manager<dim>> initial_composition_manager;
};
template <int dim>
void
SolitaryWaveMaterial<dim>::declare_parameters (ParameterHandler &prm)
{
prm.enter_subsection("Material model");
{
prm.enter_subsection("Solitary wave");
{
prm.declare_entry ("Reference solid density", "3000",
Patterns::Double (0),
"Reference density of the solid $\\rho_{s,0}$. "
"Units: \\si{\\kilogram\\per\\meter\\cubed}.");
prm.declare_entry ("Reference melt density", "2500",
Patterns::Double (0),
"Reference density of the melt/fluid$\\rho_{f,0}$. "
"Units: \\si{\\kilogram\\per\\meter\\cubed}.");
prm.declare_entry ("Reference shear viscosity", "1e20",
Patterns::Double (0),
"The value of the constant viscosity $\\eta_0$ of the solid matrix. "
"Units: \\si{\\pascal\\second}.");
prm.declare_entry ("Reference compaction viscosity", "1e20",
Patterns::Double (0),
"The value of the constant volumetric viscosity $\\xi_0$ of the solid matrix. "
"Units: \\si{\\pascal\\second}.");
prm.declare_entry ("Reference melt viscosity", "100.0",
Patterns::Double (0),
"The value of the constant melt viscosity $\\eta_f$. "
"Units: \\si{\\pascal\\second}.");
prm.declare_entry ("Reference permeability", "5e-9",
Patterns::Double(),
"Reference permeability of the solid host rock."
"Units: \\si{\\meter\\squared}.");
}
prm.leave_subsection();
}
prm.leave_subsection();
}
template <int dim>
void
SolitaryWaveMaterial<dim>::parse_parameters (ParameterHandler &prm)
{
prm.enter_subsection("Material model");
{
prm.enter_subsection("Solitary wave");
{
reference_rho_s = prm.get_double ("Reference solid density");
reference_rho_f = prm.get_double ("Reference melt density");
eta_0 = prm.get_double ("Reference shear viscosity");
xi_0 = prm.get_double ("Reference compaction viscosity");
eta_f = prm.get_double ("Reference melt viscosity");
reference_permeability = prm.get_double ("Reference permeability");
}
prm.leave_subsection();
}
prm.leave_subsection();
}
template <int dim>
double
SolitaryWaveInitialCondition<dim>::get_amplitude () const
{
return amplitude;
}
template <int dim>
double
SolitaryWaveInitialCondition<dim>::get_background_porosity () const
{
return background_porosity;
}
template <int dim>
double
SolitaryWaveInitialCondition<dim>::get_offset () const
{
return offset;
}
template <int dim>
void
SolitaryWaveInitialCondition<dim>::initialize ()
{
std::cout << "Initialize solitary wave solution"
<< std::endl;
AssertThrow(Plugins::plugin_type_matches<const SolitaryWaveMaterial<dim>>(this->get_material_model()),
ExcMessage("Initial condition Solitary Wave only works with the material model Solitary wave."));
const SolitaryWaveMaterial<dim> &
material_model
= Plugins::get_plugin_as_type<const SolitaryWaveMaterial<dim>>(this->get_material_model());
compaction_length = material_model.length_scaling(background_porosity);
AnalyticSolutions::compute_porosity(amplitude,
background_porosity,
offset,
compaction_length,
read_solution,
file_name,
this->get_mpi_communicator());
}
template <int dim>
double
SolitaryWaveInitialCondition<dim>::
initial_composition (const Point<dim> &position, const unsigned int /*n_comp*/) const
{
return AnalyticSolutions::interpolate(position[dim-1],
offset);
}
template <int dim>
void
SolitaryWaveInitialCondition<dim>::declare_parameters (ParameterHandler &prm)
{
prm.enter_subsection("Initial composition model");
{
prm.enter_subsection("Solitary wave initial condition");
{
prm.declare_entry ("Amplitude", "0.01",
Patterns::Double (0),
"Amplitude of the solitary wave. Units: none.");
prm.declare_entry ("Background porosity", "0.001",
Patterns::Double (0),
"Background porosity of the solitary wave. Units: none.");
prm.declare_entry ("Offset", "150",
Patterns::Double (0),
"Offset of the center of the solitary wave from the boundary"
"of the domain. "
"Units: \\si{\\meter}.");
prm.declare_entry ("Read solution from file", "false",
Patterns::Bool (),
"Whether to read the porosity initial condition from "
"a file or to compute it.");
prm.declare_entry ("File name", "solitary_wave.txt",
Patterns::Anything (),
"The file name of the porosity initial condition data. ");
}
prm.leave_subsection();
}
prm.leave_subsection();
}
template <int dim>
void
SolitaryWaveInitialCondition<dim>::parse_parameters (ParameterHandler &prm)
{
prm.enter_subsection("Initial composition model");
{
prm.enter_subsection("Solitary wave initial condition");
{
amplitude = prm.get_double ("Amplitude");
background_porosity = prm.get_double ("Background porosity");
offset = prm.get_double ("Offset");
read_solution = prm.get_bool ("Read solution from file");
file_name = prm.get ("File name");
AssertThrow(amplitude > background_porosity,
ExcMessage("Amplitude of the solitary wave must be larger "
"than the background porosity."));
}
prm.leave_subsection();
}
prm.leave_subsection();
}
/**
* A postprocessor that evaluates the accuracy of the solution.
*
* The implementation of error evaluators that correspond to the
* benchmarks defined in the paper Keller et al. reference above.
*/
template <int dim>
class SolitaryWavePostprocessor : public Postprocess::Interface<dim>, public ::aspect::SimulatorAccess<dim>
{
public:
/**
* Generate graphical output from the current solution.
*/
std::pair<std::string,std::string>
execute (TableHandler &statistics) override;
/**
* Initialization function. Take references to the material model and
* initial conditions model to get the parameters necessary for computing
* the analytical solution for the shape of the solitary wave and store them.
*/
void
initialize () override;
void
store_initial_pressure ();
double
compute_phase_shift ();
private:
double amplitude;
double background_porosity;
double offset;
double compaction_length;
double velocity_scaling;
double boundary_velocity;
unsigned int max_points;
std::vector<double> initial_pressure;
double maximum_pressure;
std::shared_ptr<AnalyticSolutions::FunctionSolitaryWave<dim>> ref_func;
};
template <int dim>
void
SolitaryWavePostprocessor<dim>::initialize ()
{
// verify that we are using the "Solitary wave" initial conditions and material model,
// then get the parameters we need
const SolitaryWaveInitialCondition<dim> &initial_composition
= this->get_initial_composition_manager().template get_matching_initial_composition_model<SolitaryWaveInitialCondition<dim>> ();
amplitude = initial_composition.get_amplitude();
background_porosity = initial_composition.get_background_porosity();
offset = initial_composition.get_offset();
AssertThrow(Plugins::plugin_type_matches<const SolitaryWaveMaterial<dim>>(this->get_material_model()),
ExcMessage("Postprocessor Solitary Wave only works with the material model Solitary wave."));
const SolitaryWaveMaterial<dim> &material_model
= Plugins::get_plugin_as_type<const SolitaryWaveMaterial<dim>>(this->get_material_model());
compaction_length = material_model.length_scaling(background_porosity);
velocity_scaling = material_model.velocity_scaling(background_porosity);
// we also need the boundary velocity, but we can not get it from simulator access
// TODO: write solitary wave boundary condition where the phase speed is calculated!
max_points = 1e6;
initial_pressure.resize(max_points);
maximum_pressure = 0.0;
}
template <int dim>
void
SolitaryWavePostprocessor<dim>::store_initial_pressure ()
{
const QGauss<dim> quadrature_formula (this->get_fe().base_element(this->introspection().base_elements.pressure).degree);
const unsigned int n_q_points = quadrature_formula.size();
const double max_depth = this->get_geometry_model().maximal_depth();
FEValues<dim> fe_values (this->get_mapping(),
this->get_fe(),
quadrature_formula,
update_values |
update_quadrature_points |
update_JxW_values);
typename DoFHandler<dim>::active_cell_iterator
cell = this->get_dof_handler().begin_active(),
endc = this->get_dof_handler().end();
// do the same stuff we do in depth average
std::vector<double> volume(max_points,0.0);
std::vector<double> pressure(max_points,0.0);
std::vector<double> p_c(n_q_points);
double local_max_pressure = 0.0;
for (; cell!=endc; ++cell)
if (cell->is_locally_owned())
{
fe_values.reinit (cell);
fe_values[this->introspection().variable("compaction pressure").extractor_scalar()]
.get_function_values (this->get_solution(),
p_c);
for (unsigned int q=0; q<n_q_points; ++q)
{
double z = fe_values.quadrature_point(q)[dim-1];
const unsigned int idx = static_cast<unsigned int>((z*(max_points-1))/max_depth);
AssertThrow(idx < max_points, ExcInternalError());
pressure[idx] += p_c[q] * fe_values.JxW(q);
volume[idx] += fe_values.JxW(q);
local_max_pressure = std::max (local_max_pressure, std::abs(p_c[q]));
}
}
std::vector<double> volume_all(max_points, 0.0);
Utilities::MPI::sum(volume, this->get_mpi_communicator(), volume_all);
Utilities::MPI::sum(pressure, this->get_mpi_communicator(), initial_pressure);
maximum_pressure = Utilities::MPI::max (local_max_pressure, this->get_mpi_communicator());
for (unsigned int i=0; i<initial_pressure.size(); ++i)
{
initial_pressure[i] = initial_pressure[i] / (static_cast<double>(volume_all[i])+1e-20);
}
// fill the first and last element of the initial_pressure vector if they are empty
// this makes sure they can be used for the interpolation later on
if (initial_pressure[0] == 0.0)
{
unsigned int j = 1;
while (initial_pressure[j] == 0.0)
j++;
initial_pressure[0] = initial_pressure[j];
}
if (initial_pressure[max_points-1] == 0.0)
{
unsigned int k = max_points-2;
while (initial_pressure[k] == 0.0)
k--;
initial_pressure[max_points-1] = initial_pressure[k];
}
// interpolate between the non-zero elements to fill the elements that are 0
for (unsigned int i=1; i<max_points-1; ++i)
{
if (initial_pressure[i] == 0.0)
{
// interpolate between the values we have
unsigned int k = i-1;
while (initial_pressure[k] == 0.0)
{
Assert(k > 0, ExcInternalError());
k--;
}
unsigned int j = i+1;
while (initial_pressure[j] == 0.0)
j++;
Assert(j < max_points, ExcInternalError());
initial_pressure[i] = initial_pressure[k] + (initial_pressure[j]-initial_pressure[k]) * static_cast<double>(i-k)/static_cast<double>(j-k);
}
}
}
template <int dim>
double
SolitaryWavePostprocessor<dim>::compute_phase_shift ()
{
AssertThrow(this->introspection().compositional_name_exists("porosity"),
ExcMessage("Postprocessor Solitary Wave only works if there is a compositional field called porosity."));
const unsigned int porosity_index = this->introspection().compositional_index_for_name("porosity");
// create a quadrature formula based on the compositional element alone.
// be defensive about determining that a compositional field actually exists
AssertThrow (this->introspection().base_elements.compositional_fields
!= numbers::invalid_unsigned_int,
ExcMessage("This postprocessor cannot be used without compositional fields."));
const QGauss<dim> quadrature_formula (this->get_fe().base_element(this->introspection().base_elements.compositional_fields).degree+1);
const unsigned int n_q_points = quadrature_formula.size();
FEValues<dim> fe_values (this->get_mapping(),
this->get_fe(),
quadrature_formula,
update_values |
update_quadrature_points |
update_JxW_values);
std::vector<double> compositional_values(n_q_points);
typename DoFHandler<dim>::active_cell_iterator
cell = this->get_dof_handler().begin_active(),
endc = this->get_dof_handler().end();
// The idea here is to first find the maximum, and then use the analytical solution of the
// solitary wave to calculate a phase shift for every point.
// This has to be done separately for points left and right of the maximum, as the analytical
// solution is only defined for coordinates > 0.
// In the end, these values for the phase shift are averaged.
// compute the maximum composition by quadrature (because we also need the coordinate)
double z_max_porosity = std::numeric_limits<double>::quiet_NaN();
{
double local_max_porosity = -std::numeric_limits<double>::max();
double local_max_z_location = std::numeric_limits<double>::quiet_NaN();
for (; cell!=endc; ++cell)
if (cell->is_locally_owned())
{
fe_values.reinit (cell);
fe_values[this->introspection().extractors.compositional_fields[porosity_index]].get_function_values (this->get_solution(),
compositional_values);
for (unsigned int q=0; q<n_q_points; ++q)
{
const double composition = compositional_values[q];
if (composition > local_max_porosity)
{
local_max_porosity = composition;
local_max_z_location = fe_values.quadrature_point(q)[dim-1];
}
}
}
double max_porosity = 0.0;
MPI_max_and_data(local_max_porosity, local_max_z_location, max_porosity, z_max_porosity);
}
// iterate over all points and calculate the phase shift
cell = this->get_dof_handler().begin_active();
double phase_shift_integral = 0.0;
unsigned int number_of_points = 0;
for (; cell!=endc; ++cell)
if (cell->is_locally_owned())
{
fe_values.reinit (cell);
fe_values[this->introspection().extractors.compositional_fields[porosity_index]].get_function_values (this->get_solution(),
compositional_values);
for (unsigned int q=0; q<n_q_points; ++q)
{
const double composition = compositional_values[q];
// we do not want to include the constant-porosity background in the calculation
// nor the peak of the wave where we maximum of the composition is not a good indicator
// if we are left or right of the maximum of the analytical solution
if (composition > background_porosity + (amplitude - background_porosity)*0.05 && composition <= amplitude*0.9)
{
double z_analytical = compaction_length
* AnalyticSolutions::solitary_wave_solution(composition/background_porosity,
amplitude/background_porosity);
double z = fe_values.quadrature_point(q)[dim-1];
if (z > z_max_porosity)
{
z -= offset;
phase_shift_integral += (z - z_analytical);
}
else
{
z = offset - z;
phase_shift_integral -= (z - z_analytical);
}
number_of_points += 1;
}
}
}
double integral = Utilities::MPI::sum (phase_shift_integral, this->get_mpi_communicator())
/ Utilities::MPI::sum (static_cast<double>(number_of_points), this->get_mpi_communicator());
// TODO: different case for moving wave (with zero boundary velocity)
// const double phase_speed = velocity_scaling * (2.0 * amplitude / background_porosity + 1);
return integral; // + phase_speed * this->get_time();
}
template <int dim>
std::pair<std::string,std::string>
SolitaryWavePostprocessor<dim>::execute (TableHandler & /*statistics*/)
{
// as we do not have an analytical solution for the pressure, we store the initial solution
if (this->get_timestep_number()==0)
{
store_initial_pressure();
ref_func = std::make_unique<AnalyticSolutions::FunctionSolitaryWave<dim>>(offset,0.0,initial_pressure,
this->get_geometry_model().maximal_depth(), this->introspection().n_components);
}
double delta=0;
delta = compute_phase_shift();
// reset the phase shift of the analytical solution so we can compare the shape of the wave
ref_func->set_delta(delta);
// what we want to compare:
// (1) error of the numerical phase speed c:
// c_numerical = c_analytical - Delta / time;
const double c_analytical = velocity_scaling * (2.0 * amplitude / background_porosity + 1);
const double c_numerical = c_analytical + (this->get_time() > 0 ? delta / this->get_time() : 0.0);
const double error_c = std::abs (c_numerical / c_analytical - 1);
// (3) preservation of shape of melt fraction
// (4) preservation of the shape of compaction pressure
Vector<float> cellwise_errors_f (this->get_triangulation().n_active_cells());
Vector<float> cellwise_errors_p (this->get_triangulation().n_active_cells());
// get correct components for porosity and compaction pressure
const unsigned int n_total_comp = this->introspection().n_components;
ComponentSelectFunction<dim> comp_f(dim+2+dim+2, n_total_comp);
ComponentSelectFunction<dim> comp_p(dim+1, n_total_comp);
const QGauss<dim> quadrature_formula (this->get_fe().base_element(this->introspection().base_elements.pressure).degree+1);
VectorTools::integrate_difference (this->get_mapping(),this->get_dof_handler(),
this->get_solution(),
*ref_func,
cellwise_errors_f,
quadrature_formula,
VectorTools::L2_norm,
&comp_f);
VectorTools::integrate_difference (this->get_mapping(),this->get_dof_handler(),
this->get_solution(),
*ref_func,
cellwise_errors_p,
quadrature_formula,
VectorTools::L2_norm,
&comp_p);
const double e_f = VectorTools::compute_global_error(this->get_triangulation(), cellwise_errors_f, VectorTools::L2_norm);
const double e_p = VectorTools::compute_global_error(this->get_triangulation(), cellwise_errors_p, VectorTools::L2_norm);
std::ostringstream os;
os << std::scientific << e_f / amplitude
<< ", " << e_p / maximum_pressure