diff --git a/CHANGELOG.md b/CHANGELOG.md index e3f3598fbf..5aaf693672 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/include/core/utilities.h b/include/core/utilities.h index 1aeeb515b3..ee119c15c9 100644 --- a/include/core/utilities.h +++ b/include/core/utilities.h @@ -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 +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 diff --git a/include/solvers/cahn_hilliard_scratch_data.h b/include/solvers/cahn_hilliard_scratch_data.h index f32c181968..9cba746d52 100644 --- a/include/solvers/cahn_hilliard_scratch_data.h +++ b/include/solvers/cahn_hilliard_scratch_data.h @@ -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(cell->measure(), fe_cahn_hilliard.degree); // Gather Phi and eta (values, gradient and laplacian) this->fe_values_cahn_hilliard[phase_order].get_function_values( diff --git a/include/solvers/heat_transfer_scratch_data.h b/include/solvers/heat_transfer_scratch_data.h index 091ccdc779..1ccf44f3e3 100644 --- a/include/solvers/heat_transfer_scratch_data.h +++ b/include/solvers/heat_transfer_scratch_data.h @@ -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(cell->measure(), fe_T.degree); // Gather temperature (values, gradient and laplacian) this->fe_values_T.get_function_values(current_solution, diff --git a/include/solvers/navier_stokes_scratch_data.h b/include/solvers/navier_stokes_scratch_data.h index 592e47484c..b24e162288 100644 --- a/include/solvers/navier_stokes_scratch_data.h +++ b/include/solvers/navier_stokes_scratch_data.h @@ -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(cell->measure(), fe.degree); // Gather velocity (values, gradient and laplacian) this->fe_values[velocities].get_function_values(current_solution, diff --git a/include/solvers/tracer_scratch_data.h b/include/solvers/tracer_scratch_data.h index ad0c90ed0a..fa6c4cc32c 100644 --- a/include/solvers/tracer_scratch_data.h +++ b/include/solvers/tracer_scratch_data.h @@ -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(cell->measure(), fe_tracer.degree); // Gather tracer (values, gradient and laplacian) this->fe_values_tracer.get_function_values(current_solution, diff --git a/include/solvers/vof_scratch_data.h b/include/solvers/vof_scratch_data.h index e2054af4ca..f50389192f 100644 --- a/include/solvers/vof_scratch_data.h +++ b/include/solvers/vof_scratch_data.h @@ -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(cell->measure(), fe_vof.degree); fe_values_vof.get_function_values(current_solution, this->present_phase_values); diff --git a/source/solvers/fluid_dynamics_nitsche.cc b/source/solvers/fluid_dynamics_nitsche.cc index ecd75d2c76..4f1a36018c 100644 --- a/source/solvers/fluid_dynamics_nitsche.cc +++ b/source/solvers/fluid_dynamics_nitsche.cc @@ -109,15 +109,9 @@ FluidDynamicsNitsche::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(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 = @@ -362,13 +356,8 @@ FluidDynamicsNitsche::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(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 = @@ -451,13 +440,8 @@ FluidDynamicsNitsche::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(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 = diff --git a/source/solvers/postprocessing_cfd.cc b/source/solvers/postprocessing_cfd.cc index f1a675500d..f09a9a511b 100644 --- a/source/solvers/postprocessing_cfd.cc +++ b/source/solvers/postprocessing_cfd.cc @@ -260,10 +260,7 @@ calculate_CFL(const DoFHandler &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(cell->measure(), degree); fe_values.reinit(cell); fe_values[velocities].get_function_values(evaluation_point, present_velocity_values); diff --git a/source/solvers/vof.cc b/source/solvers/vof.cc index 93766822c9..4745227855 100644 --- a/source/solvers/vof.cc +++ b/source/solvers/vof.cc @@ -1437,10 +1437,8 @@ VolumeOfFluid::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(cell_volume, fe->degree); local_matrix_phase_fraction = 0; local_rhs_phase_fraction = 0; @@ -1624,17 +1622,9 @@ VolumeOfFluid::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( + 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; @@ -1831,12 +1821,8 @@ VolumeOfFluid::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(curvature_cell->measure(), + fe_curvature.degree); // Get pfg values, curvature values and gradients fe_values_projected_phase_fraction_gradient[pfg].get_function_values(