76class SoundEnergyCalculator
78 using This = SoundEnergyCalculator<dim, Number>;
80 using VectorType = dealii::LinearAlgebra::distributed::Vector<Number>;
82 using scalar = dealii::VectorizedArray<Number>;
83 using vector = dealii::Tensor<1, dim, scalar>;
85 using CellIntegratorU = CellIntegrator<dim, dim, Number>;
86 using CellIntegratorP = CellIntegrator<dim, 1, Number>;
89 SoundEnergyCalculator(MPI_Comm
const & comm)
93 dof_index_pressure(0),
94 dof_index_velocity(1),
100 setup(dealii::MatrixFree<dim, Number>
const & matrix_free_in,
102 unsigned int const dof_index_pressure_in,
103 unsigned int const dof_index_velocity_in,
104 unsigned int const quad_index_in)
106 time_control.setup(data_in.time_control_data);
108 matrix_free = &matrix_free_in;
111 dof_index_pressure = dof_index_pressure_in;
112 dof_index_velocity = dof_index_velocity_in;
113 quad_index = quad_index_in;
115 clear_files = data_in.clear_file;
117 if(data_in.time_control_data.is_active)
119 AssertThrow(data_in.density > 0.0 && data_in.speed_of_sound > 0.0,
120 dealii::ExcMessage(
"Material parameters not set in SoundEnergyCalculatorData."));
127 evaluate(VectorType
const & pressure,
128 VectorType
const & velocity,
132 AssertThrow(unsteady,
134 "This postprocessing tool can only be used for unsteady problems."));
136 do_evaluate(pressure, velocity, time);
143 do_evaluate(VectorType
const & pressure, VectorType
const & velocity,
double const time)
145 double sound_energy = calculate_sound_energy(pressure, velocity);
148 if(dealii::Utilities::MPI::this_mpi_process(mpi_comm) == 0)
150 std::ostringstream filename;
151 filename << data.directory + data.filename;
154 if(clear_files ==
true)
156 f.open(filename.str().c_str(), std::ios::trunc);
157 f <<
"Time, Sound energy: E = (1,p*p/(2*rho*c*c)+rho*u*u/2)_Omega" << std::endl;
158 f <<
"c = " << data.speed_of_sound << std::endl;
159 f <<
"rho = " << data.density << std::endl;
164 f.open(filename.str().c_str(), std::ios::app);
167 unsigned int precision = 12;
168 f << std::scientific << std::setprecision(precision) << std::setw(precision + 8) << time
169 <<
", " << std::setw(precision + 8) << sound_energy << std::endl;
181 calculate_sound_energy(VectorType
const & pressure, VectorType
const & velocity)
const
185 std::pair<VectorType const *, VectorType const *>
const pressure_velocity{
186 std::make_pair(&pressure, &velocity)};
188 matrix_free->cell_loop(&This::cell_loop,
this, energy, pressure_velocity);
191 double const sound_energy = dealii::Utilities::MPI::sum(energy, mpi_comm);
197 cell_loop(dealii::MatrixFree<dim, Number>
const & matrix_free_in,
199 std::pair<VectorType const *, VectorType const *>
const & src_pressure_velocity,
200 std::pair<unsigned int, unsigned int>
const & cell_range)
const
202 CellIntegratorP pressure(matrix_free_in, dof_index_pressure, quad_index);
203 CellIntegratorU velocity(matrix_free_in, dof_index_velocity, quad_index);
205 Number
const rho_inv =
static_cast<Number
>(1.0 / data.density);
206 Number
const rhocc_inv =
207 static_cast<Number
>(1.0 / (data.density * data.speed_of_sound * data.speed_of_sound));
212 for(
unsigned int cell = cell_range.first; cell < cell_range.second; ++cell)
214 pressure.reinit(cell);
215 velocity.reinit(cell);
216 pressure.read_dof_values(*src_pressure_velocity.first);
217 velocity.read_dof_values(*src_pressure_velocity.second);
218 pressure.evaluate(dealii::EvaluationFlags::values);
219 velocity.evaluate(dealii::EvaluationFlags::values);
221 scalar energy_batch = dealii::make_vectorized_array<Number>(0.);
223 for(
unsigned int q = 0; q < pressure.n_q_points; ++q)
225 vector rho_u = velocity.get_value(q);
226 scalar p = pressure.get_value(q);
230 energy_batch += Number{0.5} * rho_inv * rho_u * rho_u * velocity.JxW(q);
232 energy_batch += Number{0.5} * rhocc_inv * p * p * pressure.JxW(q);
236 for(
unsigned int v = 0; v < matrix_free_in.n_active_entries_per_cell_batch(cell); ++v)
237 energy += energy_batch[v];
243 MPI_Comm
const mpi_comm;
247 dealii::MatrixFree<dim, Number>
const * matrix_free;
250 unsigned int dof_index_pressure;
251 unsigned int dof_index_velocity;
252 unsigned int quad_index;