Skip to content

Commit

Permalink
Refactor cell diameter computation to utilities (#1265)
Browse files Browse the repository at this point in the history
Description
The cell diameter calculation was repeated in every scratch data. This PR refactors it in an inline function in the utilities.

Testing
No impact on current tests.

Co-authored-by: Bruno Blais <blais.bruno@gmail.com>
  • Loading branch information
AmishgaAlphonius and blaisb authored Aug 29, 2024
1 parent 08fcd76 commit a9099c0
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 74 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to the Lethe project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/).

## [Master] - 2024-08-27

### Changed

- MINOR Refactored cell diameter computation as an inline function in the utilities. [#1265](https://github.com/chaos-polymtl/lethe/pull/1265)

## [Master] - 2024-08-23

### Added
Expand Down
30 changes: 30 additions & 0 deletions include/core/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,34 @@ value_string_to_tensor(const std::string &value_string_0,
return output_tensor;
}

/**
* @brief Computes equivalent cell diameter by comparing the area to a disk (2D)
* or the volume to a sphere (3D).
*
* @tparam dim Number of spatial dimensions (2D or 3D).
*
* @param[in] cell_measure Area (2D) or volume (3D) of the cell.
*
* @param[in] fe_degree Polynomial degree of the shape function.
*
* @return Cell diameter value.
*/
template <int dim>
inline double
compute_cell_diameter(const double cell_measure, const unsigned int fe_degree)
{
double h;
if constexpr (dim == 2)
h = std::sqrt(4. * cell_measure / numbers::PI) / fe_degree;
else if constexpr (dim == 3)
h = std::cbrt(6. * cell_measure / numbers::PI) / fe_degree;
else
Assert(
false,
ExcMessage(std::string(
"'dim' should have a value of either 2 or 3. Only 2D and 3D simulations "
"are supported.")));
return h;
}

#endif
10 changes: 3 additions & 7 deletions include/solvers/cahn_hilliard_scratch_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,9 @@ class CahnHilliardScratchData
source_chemical_potential,
1);


if (dim == 2)
this->cell_size =
std::sqrt(4. * cell->measure() / M_PI) / fe_cahn_hilliard.degree;
else if (dim == 3)
this->cell_size =
pow(6 * cell->measure() / M_PI, 1. / 3.) / fe_cahn_hilliard.degree;
// Compute cell diameter
this->cell_size =
compute_cell_diameter<dim>(cell->measure(), fe_cahn_hilliard.degree);

// Gather Phi and eta (values, gradient and laplacian)
this->fe_values_cahn_hilliard[phase_order].get_function_values(
Expand Down
6 changes: 2 additions & 4 deletions include/solvers/heat_transfer_scratch_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,8 @@ class HeatTransferScratchData

source_function->value_list(quadrature_points, source);

if (dim == 2)
this->cell_size = std::sqrt(4. * cell->measure() / M_PI) / fe_T.degree;
else if (dim == 3)
this->cell_size = pow(6 * cell->measure() / M_PI, 1. / 3.) / fe_T.degree;
// Compute cell diameter
this->cell_size = compute_cell_diameter<dim>(cell->measure(), fe_T.degree);

// Gather temperature (values, gradient and laplacian)
this->fe_values_T.get_function_values(current_solution,
Expand Down
6 changes: 2 additions & 4 deletions include/solvers/navier_stokes_scratch_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,8 @@ class NavierStokesScratchData
components[k] = fe.system_to_component_index(k).first;
}

if (dim == 2)
this->cell_size = std::sqrt(4. * cell->measure() / M_PI) / fe.degree;
else if (dim == 3)
this->cell_size = pow(6 * cell->measure() / M_PI, 1. / 3.) / fe.degree;
// Compute cell diameter
this->cell_size = compute_cell_diameter<dim>(cell->measure(), fe.degree);

// Gather velocity (values, gradient and laplacian)
this->fe_values[velocities].get_function_values(current_solution,
Expand Down
9 changes: 3 additions & 6 deletions include/solvers/tracer_scratch_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,9 @@ class TracerScratchData
levelset_function->value_list(quadrature_points, sdf_values);
}

if (dim == 2)
this->cell_size =
std::sqrt(4. * cell->measure() / M_PI) / fe_tracer.degree;
else if (dim == 3)
this->cell_size =
pow(6 * cell->measure() / M_PI, 1. / 3.) / fe_tracer.degree;
// Compute cell diameter
this->cell_size =
compute_cell_diameter<dim>(cell->measure(), fe_tracer.degree);

// Gather tracer (values, gradient and laplacian)
this->fe_values_tracer.get_function_values(current_solution,
Expand Down
8 changes: 3 additions & 5 deletions include/solvers/vof_scratch_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,9 @@ class VOFScratchData
this->quadrature_points = fe_values_vof.get_quadrature_points();
auto &fe_vof = fe_values_vof.get_fe();

if (dim == 2)
this->cell_size = std::sqrt(4. * cell->measure() / M_PI) / fe_vof.degree;
else if (dim == 3)
this->cell_size =
pow(6 * cell->measure() / M_PI, 1. / 3.) / fe_vof.degree;
// Compute cell diameter
this->cell_size =
compute_cell_diameter<dim>(cell->measure(), fe_vof.degree);

fe_values_vof.get_function_values(current_solution,
this->present_phase_values);
Expand Down
30 changes: 7 additions & 23 deletions source/solvers/fluid_dynamics_nitsche.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,9 @@ FluidDynamicsNitsche<dim, spacedim>::assemble_nitsche_restriction()
local_matrix = 0;
local_rhs = 0;


const auto &cell = particle->get_surrounding_cell();
double h_cell = 0;
if (dim == 2)
h_cell = std::sqrt(4. * cell->measure() / M_PI) /
this->velocity_fem_degree;
else if (dim == 3)
h_cell = pow(6 * cell->measure() / M_PI, 1. / 3.) /
this->velocity_fem_degree;
const auto &cell = particle->get_surrounding_cell();
double h_cell = compute_cell_diameter<dim>(cell->measure(),
this->velocity_fem_degree);
const double penalty_parameter =
1. / std::pow(h_cell * h_cell, double(dim) / double(spacedim));
const auto &dh_cell =
Expand Down Expand Up @@ -362,13 +356,8 @@ FluidDynamicsNitsche<dim, spacedim>::calculate_forces_on_solid(
#else
const auto &cell = particle->get_surrounding_cell();
#endif
double h_cell = 0;
if (dim == 2)
h_cell =
std::sqrt(4. * cell->measure() / M_PI) / this->velocity_fem_degree;
else if (dim == 3)
h_cell =
pow(6 * cell->measure() / M_PI, 1. / 3.) / this->velocity_fem_degree;
double h_cell =
compute_cell_diameter<dim>(cell->measure(), this->velocity_fem_degree);
const double penalty_parameter =
1. / std::pow(h_cell * h_cell, double(dim) / double(spacedim));
const auto &dh_cell =
Expand Down Expand Up @@ -451,13 +440,8 @@ FluidDynamicsNitsche<dim, spacedim>::calculate_torque_on_solid(
#else
const auto &cell = particle->get_surrounding_cell();
#endif
double h_cell = 0;
if (dim == 2)
h_cell =
std::sqrt(4. * cell->measure() / M_PI) / this->velocity_fem_degree;
else if (dim == 3)
h_cell =
pow(6 * cell->measure() / M_PI, 1. / 3.) / this->velocity_fem_degree;
double h_cell =
compute_cell_diameter<dim>(cell->measure(), this->velocity_fem_degree);
const double penalty_parameter =
1. / std::pow(h_cell * h_cell, double(dim) / double(spacedim));
const auto &dh_cell =
Expand Down
5 changes: 1 addition & 4 deletions source/solvers/postprocessing_cfd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,7 @@ calculate_CFL(const DoFHandler<dim> &dof_handler,
{
if (cell->is_locally_owned())
{
if (dim == 2)
h = std::sqrt(4. * cell->measure() / M_PI) / degree;
else if (dim == 3)
h = pow(6 * cell->measure() / M_PI, 1. / 3.) / degree;
h = compute_cell_diameter<dim>(cell->measure(), degree);
fe_values.reinit(cell);
fe_values[velocities].get_function_values(evaluation_point,
present_velocity_values);
Expand Down
28 changes: 7 additions & 21 deletions source/solvers/vof.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1437,10 +1437,8 @@ VolumeOfFluid<dim>::assemble_projection_phase_fraction(
fe_values_phase_fraction.reinit(cell);

cell_volume = cell->measure();
if (dim == 2)
h = std::sqrt(4. * cell_volume / M_PI) / fe->degree;
else if (dim == 3)
h = pow(6 * cell_volume / M_PI, 1. / 3.) / fe->degree;

h = compute_cell_diameter<dim>(cell_volume, fe->degree);

local_matrix_phase_fraction = 0;
local_rhs_phase_fraction = 0;
Expand Down Expand Up @@ -1624,17 +1622,9 @@ VolumeOfFluid<dim>::assemble_projected_phase_fraction_gradient_matrix_and_rhs(
auto &fe_filtered_phase_fraction =
fe_values_projected_phase_fraction_gradient.get_fe();

if (dim == 2)
h =
std::sqrt(4. * projected_phase_fraction_gradient_cell->measure() /
M_PI) /
fe_filtered_phase_fraction.degree;
else if (dim == 3)
h =
pow(6 * projected_phase_fraction_gradient_cell->measure() / M_PI,
1. / 3.) /
fe_filtered_phase_fraction.degree;

h = compute_cell_diameter<dim>(
projected_phase_fraction_gradient_cell->measure(),
fe_filtered_phase_fraction.degree);

local_matrix_projected_phase_fraction_gradient = 0;
local_rhs_projected_phase_fraction_gradient = 0;
Expand Down Expand Up @@ -1831,12 +1821,8 @@ VolumeOfFluid<dim>::assemble_curvature_matrix_and_rhs(

auto &fe_curvature = fe_values_curvature.get_fe();

if (dim == 2)
h = std::sqrt(4. * curvature_cell->measure() / M_PI) /
fe_curvature.degree;
else if (dim == 3)
h = pow(6 * curvature_cell->measure() / M_PI, 1. / 3.) /
fe_curvature.degree;
h = compute_cell_diameter<dim>(curvature_cell->measure(),
fe_curvature.degree);

// Get pfg values, curvature values and gradients
fe_values_projected_phase_fraction_gradient[pfg].get_function_values(
Expand Down

0 comments on commit a9099c0

Please sign in to comment.