-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpositivity.cc
210 lines (186 loc) · 8.32 KB
/
positivity.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
#include <deal.II/base/quadrature_lib.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/fe/fe_values_extractors.h>
#include <deal.II/dofs/dof_handler.h>
#include "equation.h"
#include "claw.h"
using namespace dealii;
//-----------------------------------------------------------------------------
// Positivity limiter of Zhang-Shu
//-----------------------------------------------------------------------------
template <int dim>
void ConservationLaw<dim>::apply_positivity_limiter ()
{
if(fe.degree == 0) return;
const double gas_gamma = EulerEquations<dim>::gas_gamma;
const unsigned int density_component = EulerEquations<dim>::density_component;
const unsigned int energy_component = EulerEquations<dim>::energy_component;
// Find mininimum density and pressure in the whole grid
const double eps = 1.0e-13;
{
for (unsigned int c=0; c<triangulation.n_active_cells(); ++c)
{
double eps1 = cell_average[c][density_component];
double pressure = EulerEquations<dim>::template compute_pressure<double> (cell_average[c]);
eps1 = std::min(eps1, pressure);
if(eps1 < eps)
{
//std::cout << "\n Negative state at position " << cell0->center() << "\n\n";
AssertThrow(false, ExcMessage("Fatal: Negative states"));
}
}
}
// Need 2N - 3 >= degree for the quadrature to be exact.
// Choose same order as used for assembly process.
unsigned int N = (fe.degree+3)%2==0 ? (fe.degree+3)/2 : (fe.degree+4)/2;
Quadrature<dim> quadrature_x (QGaussLobatto<1>(N), QGauss<1>(fe.degree+1));
Quadrature<dim> quadrature_y (QGauss<1>(fe.degree+1), QGaussLobatto<1>(N));
FEValues<dim> fe_values_x (mapping(), fe, quadrature_x, update_values);
FEValues<dim> fe_values_y (mapping(), fe, quadrature_y, update_values);
const unsigned int n_q_points = quadrature_x.size();
std::vector<double> density_values(n_q_points), energy_values(n_q_points);
std::vector< Tensor<1,dim> > momentum_values(n_q_points);
std::vector<unsigned int> local_dof_indices (fe.dofs_per_cell);
const FEValuesExtractors::Scalar density (density_component);
const FEValuesExtractors::Scalar energy (energy_component);
const FEValuesExtractors::Vector momentum (0);
typename DoFHandler<dim>::active_cell_iterator
cell = dof_handler.begin_active(),
endc = dof_handler.end();
for(; cell != endc; ++cell)
{
unsigned int c = cell_number(cell);
fe_values_x.reinit(cell);
fe_values_y.reinit(cell);
// First limit density
// find minimum density at GLL points
double rho_min = 1.0e20;
fe_values_x[density].get_function_values(current_solution, density_values);
for(unsigned int q=0; q<n_q_points; ++q)
rho_min = std::min(rho_min, density_values[q]);
fe_values_y[density].get_function_values(current_solution, density_values);
for(unsigned int q=0; q<n_q_points; ++q)
rho_min = std::min(rho_min, density_values[q]);
double density_average = cell_average[c][density_component];
double rat = std::fabs(density_average - eps) /
(std::fabs(density_average - rho_min) + 1.0e-13);
double theta1 = std::min(rat, 1.0);
if(theta1 < 1.0)
{
cell->get_dof_indices (local_dof_indices);
if(parameters.basis == Parameters::AllParameters<dim>::Qk)
{
for(unsigned int i=0; i<fe.dofs_per_cell; ++i)
{
unsigned int comp_i = fe.system_to_component_index(i).first;
if(comp_i == density_component)
current_solution(local_dof_indices[i]) =
theta1 * current_solution(local_dof_indices[i])
+ (1.0 - theta1) * density_average;
}
}
else
{
for(unsigned int i=0; i<fe.dofs_per_cell; ++i)
{
unsigned int comp_i = fe.system_to_component_index(i).first;
unsigned int base_i = fe.system_to_component_index(i).second;
if(comp_i == density_component && base_i > 0)
current_solution(local_dof_indices[i]) *= theta1;
}
}
}
// now limit pressure
double energy_average = cell_average[c][energy_component];
Tensor<1,dim> momentum_average;
for(unsigned int i=0; i<dim; ++i)
momentum_average[i] = cell_average[c][i];
double theta2 = 1.0;
for(int d=0; d<dim; ++d)
{
if(d==0)
{
fe_values_x[density].get_function_values(current_solution, density_values);
fe_values_x[momentum].get_function_values(current_solution, momentum_values);
fe_values_x[energy].get_function_values(current_solution, energy_values);
}
else
{
fe_values_y[density].get_function_values(current_solution, density_values);
fe_values_y[momentum].get_function_values(current_solution, momentum_values);
fe_values_y[energy].get_function_values(current_solution, energy_values);
}
for(unsigned int q=0; q<n_q_points; ++q)
{
double pressure = (gas_gamma-1.0)*(energy_values[q] -
0.5*momentum_values[q].norm_square()/density_values[q]);
if(pressure < eps)
{
double drho = density_values[q] - density_average;
Tensor<1,dim> dm = momentum_values[q] - momentum_average;
double dE = energy_values[q] - energy_average;
double a1 = 2.0*drho*dE - dm*dm;
double b1 = 2.0*drho*(energy_average - eps/(gas_gamma-1.0))
+ 2.0*density_average*dE
- 2.0*momentum_average*dm;
double c1 = 2.0*density_average*energy_average
- momentum_average*momentum_average
- 2.0*eps*density_average/(gas_gamma-1.0);
// Divide by a1 to avoid round-off error
b1 /= a1; c1 /= a1;
double D = std::sqrt( std::fabs(b1*b1 - 4.0*c1) );
double t1 = 0.5*(-b1 - D);
double t2 = 0.5*(-b1 + D);
double t;
if(t1 > -1.0e-12 && t1 < 1.0 + 1.0e-12)
t = t1;
else if(t2 > -1.0e-12 && t2 < 1.0 + 1.0e-12)
t = t2;
else
{
std::cout << "Problem in positivity limiter\n";
std::cout << "\t a1, b1, c1 = " << a1 << " " << b1 << " " << c1 << "\n";
std::cout << "\t t1, t2 = " << t1 << " " << t2 << "\n";
std::cout << "\t eps, rho_min = " << eps << " " << rho_min << "\n";
std::cout << "\t theta1 = " << theta1 << "\n";
std::cout << "\t pressure = " << pressure << "\n";
exit(0);
}
// t should strictly lie in [0,1]
t = std::min(1.0, t);
t = std::max(0.0, t);
// Need t < 1.0. If t==1 upto machine precision
// then we are suffering from round off error.
// In this case we take the cell average value, t=0.
if(std::fabs(1.0-t) < 1.0e-14) t = 0.0;
theta2 = std::min(theta2, t);
}
}
}
if(theta2 < 1.0)
{
if(!(theta1<1.0)) // local_dof_indices has not been computed before
cell->get_dof_indices (local_dof_indices);
if(parameters.basis == Parameters::AllParameters<dim>::Qk)
{
for(unsigned int i=0; i<fe.dofs_per_cell; ++i)
{
unsigned int comp_i = fe.system_to_component_index(i).first;
current_solution(local_dof_indices[i]) =
theta2 * current_solution(local_dof_indices[i])
+ (1.0 - theta2) * cell_average[c][comp_i];
}
}
else
{
for(unsigned int i=0; i<fe.dofs_per_cell; ++i)
{
unsigned int base_i = fe.system_to_component_index(i).second;
if(base_i > 0)
current_solution(local_dof_indices[i]) *= theta2;
}
}
}
}
}
template class ConservationLaw<2>;