78class SoundEnergyCalculator
80 using This = SoundEnergyCalculator<dim, Number>;
82 using VectorType = dealii::LinearAlgebra::distributed::Vector<Number>;
84 using scalar = dealii::VectorizedArray<Number>;
85 using vector = dealii::Tensor<1, dim, scalar>;
87 using CellIntegratorU = CellIntegrator<dim, dim, Number>;
88 using CellIntegratorP = CellIntegrator<dim, 1, Number>;
91 SoundEnergyCalculator(MPI_Comm
const & comm)
95 dof_index_pressure(0),
96 dof_index_velocity(1),
102 setup(dealii::MatrixFree<dim, Number>
const & matrix_free_in,
104 unsigned int const dof_index_pressure_in,
105 unsigned int const dof_index_velocity_in,
106 unsigned int const quad_index_in)
108 time_control.setup(data_in.time_control_data);
110 matrix_free = &matrix_free_in;
113 dof_index_pressure = dof_index_pressure_in;
114 dof_index_velocity = dof_index_velocity_in;
115 quad_index = quad_index_in;
117 clear_files = data_in.clear_file;
119 if(data_in.time_control_data.is_active)
121 AssertThrow(data_in.density > 0.0 && data_in.speed_of_sound > 0.0,
122 dealii::ExcMessage(
"Material parameters not set in SoundEnergyCalculatorData."));
129 evaluate(VectorType
const & pressure,
130 VectorType
const & velocity,
134 AssertThrow(unsteady,
136 "This postprocessing tool can only be used for unsteady problems."));
138 do_evaluate(pressure, velocity, time);
145 do_evaluate(VectorType
const & pressure, VectorType
const & velocity,
double const time)
147 double sound_energy = calculate_sound_energy(pressure, velocity);
150 if(dealii::Utilities::MPI::this_mpi_process(mpi_comm) == 0)
152 std::ostringstream filename;
153 filename << data.directory + data.filename;
156 if(clear_files ==
true)
158 f.open(filename.str().c_str(), std::ios::trunc);
159 f <<
"Time, Sound energy: E = (1,p*p/(2*rho*c*c)+rho*u*u/2)_Omega" << std::endl;
160 f <<
"c = " << data.speed_of_sound << std::endl;
161 f <<
"rho = " << data.density << std::endl;
166 f.open(filename.str().c_str(), std::ios::app);
169 unsigned int precision = 12;
170 f << std::scientific << std::setprecision(precision) << std::setw(precision + 8) << time
171 <<
", " << std::setw(precision + 8) << sound_energy << std::endl;
183 calculate_sound_energy(VectorType
const & pressure, VectorType
const & velocity)
const
187 std::pair<VectorType const *, VectorType const *>
const pressure_velocity{
188 std::make_pair(&pressure, &velocity)};
190 matrix_free->cell_loop(&This::cell_loop,
this, energy, pressure_velocity);
193 double const sound_energy = dealii::Utilities::MPI::sum(energy, mpi_comm);
199 cell_loop(dealii::MatrixFree<dim, Number>
const & matrix_free_in,
201 std::pair<VectorType const *, VectorType const *>
const & src_pressure_velocity,
202 std::pair<unsigned int, unsigned int>
const & cell_range)
const
204 CellIntegratorP pressure(matrix_free_in, dof_index_pressure, quad_index);
205 CellIntegratorU velocity(matrix_free_in, dof_index_velocity, quad_index);
207 Number
const rho_inv =
static_cast<Number
>(1.0 / data.density);
208 Number
const rhocc_inv =
209 static_cast<Number
>(1.0 / (data.density * data.speed_of_sound * data.speed_of_sound));
214 for(
unsigned int cell = cell_range.first; cell < cell_range.second; ++cell)
216 pressure.reinit(cell);
217 velocity.reinit(cell);
218 pressure.read_dof_values(*src_pressure_velocity.first);
219 velocity.read_dof_values(*src_pressure_velocity.second);
220 pressure.evaluate(dealii::EvaluationFlags::values);
221 velocity.evaluate(dealii::EvaluationFlags::values);
223 scalar energy_batch = dealii::make_vectorized_array<Number>(0.);
225 for(
unsigned int q = 0; q < pressure.n_q_points; ++q)
227 vector rho_u = velocity.get_value(q);
228 scalar p = pressure.get_value(q);
232 energy_batch += Number{0.5} * rho_inv * rho_u * rho_u * velocity.JxW(q);
234 energy_batch += Number{0.5} * rhocc_inv * p * p * pressure.JxW(q);
238 for(
unsigned int v = 0; v < matrix_free_in.n_active_entries_per_cell_batch(cell); ++v)
239 energy += energy_batch[v];
245 MPI_Comm
const mpi_comm;
249 dealii::MatrixFree<dim, Number>
const * matrix_free;
252 unsigned int dof_index_pressure;
253 unsigned int dof_index_velocity;
254 unsigned int quad_index;