18#include "libmesh/variational_smoother_system.h"
20#include "libmesh/elem.h"
21#include "libmesh/face_tri3.h"
22#include "libmesh/face_tri6.h"
23#include "libmesh/fe_base.h"
24#include "libmesh/fe_interface.h"
25#include "libmesh/fem_context.h"
26#include "libmesh/mesh.h"
27#include "libmesh/node_range.h"
28#include "libmesh/numeric_vector.h"
29#include "libmesh/parallel_ghost_sync.h"
30#include "libmesh/quadrature.h"
31#include "libmesh/string_to_enum.h"
32#include "libmesh/utility.h"
33#include "libmesh/enum_to_string.h"
34#include <libmesh/reference_elem.h>
49 comm.
minloc(pair.first, rank);
60 comm.
maxloc(pair.first, rank);
69 return 0.5 * (x + std::sqrt(epsilon_squared + Utility::pow<2>(x)));
77 const unsigned int &
dim,
78 const unsigned int & qp)
80 libmesh_error_msg_if(
dim > 3,
"Unsupported dimension.");
109 bool apply_heterogeneous_constraints,
110 bool apply_no_constraints)
116 const auto dim =
mesh.mesh_dimension();
119 (
mesh.local_node_stored_range(),
122 for (const auto d : make_range(dim))
124 const auto dof_id = node->dof_number(this->number(), d, 0);
126 (*node)(d) = libmesh_real((*current_local_solution)(dof_id));
134 compute_mesh_quality_info();
135 const bool is_tangled = _mesh_info.mesh_is_tangled;
139 if (_untangling_solve)
141 const Real & min_S = _mesh_info.min_qp_det_S;
143 const Real variable_component = 100. * Utility::pow<2>(_ref_vol * min_S);
144 _epsilon_squared_assembly = _epsilon_squared + variable_component;
148 _epsilon_squared_assembly = 0.;
150 FEMSystem::assembly(get_residual, get_jacobian, apply_heterogeneous_constraints, apply_no_constraints);
152 if (_untangling_solve && !is_tangled)
164void VariationalSmootherSystem::solve()
166 const auto & mesh_info = get_mesh_info();
169 if (mesh_info.mesh_is_tangled)
172 _untangling_solve =
true;
179 const auto dilation_weight = _dilation_weight;
180 _dilation_weight = 0.;
187 _dilation_weight = dilation_weight;
194 _untangling_solve =
false;
198 libmesh_error_msg_if(mesh_info.mesh_is_tangled,
"The smoothing solve tangled the mesh!");
203void VariationalSmootherSystem::init_data ()
205 auto &
mesh = this->get_mesh();
206 const auto & elem_orders =
mesh.elem_default_orders();
207 libmesh_error_msg_if(elem_orders.size() != 1,
208 "The variational smoother cannot be used for mixed-order meshes!");
209 const auto fe_order = *elem_orders.begin();
213 this->add_variable (
"r" + std::to_string(d), fe_order);
216 FEMSystem::init_data();
219 this->solution->close();
221 for (
auto * node :
mesh.local_node_ptr_range())
225 const auto dof_id = node->dof_number(this->number(), d, 0);
227 (*solution).set(dof_id, (*node)(d));
231 this->prepare_for_smoothing();
234void VariationalSmootherSystem::prepare_for_smoothing()
237 if (std::abs(_ref_vol) > TOLERANCE * TOLERANCE)
240 std::unique_ptr<DiffContext> con = this->build_context();
241 FEMContext & femcontext = cast_ref<FEMContext &>(*con);
242 this->init_context(femcontext);
244 const auto &
mesh = this->get_mesh();
246 Real elem_averaged_det_S_sum = 0.;
252 const auto & JxW = fe_map.get_JxW();
254 for (
const auto * elem :
mesh.active_local_element_ptr_range())
260 if (_target_jacobians.find(elem->type()) == _target_jacobians.end())
262 const auto [target_elem, target_nodes] = ReferenceElem::ideal_target(elem->type());
263 get_target_to_reference_jacobian(target_elem.get(),
265 _target_jacobians[elem->type()],
266 _target_jacobian_dets[elem->type()]);
270 Real elem_integrated_det_S = 0.;
272 elem_integrated_det_S += JxW[qp] / _target_jacobian_dets[elem->type()][qp];
273 const auto ref_elem_vol = elem->reference_elem()->volume();
274 elem_averaged_det_S_sum += elem_integrated_det_S / ref_elem_vol;
279 mesh.comm().sum(elem_averaged_det_S_sum);
281 _ref_vol = elem_averaged_det_S_sum /
mesh.n_active_elem();
283 libMesh::out <<
"Reference volume: " << _ref_vol << std::endl;
286void VariationalSmootherSystem::init_context(
DiffContext & context)
288 FEMContext & c = cast_ref<FEMContext &>(context);
296 const std::set<unsigned char> & elem_dims =
299 for (
const auto &
dim : elem_dims)
306 fe_map.get_dxyzdeta();
307 fe_map.get_dxyzdzeta();
311 fe_map.set_jacobian_tolerance(std::numeric_limits<Real>::lowest());
317 FEMSystem::init_context(context);
325 if (_quadrature_type !=
QGAUSS)
330bool VariationalSmootherSystem::element_time_derivative (
bool request_jacobian,
333 FEMContext & c = cast_ref<FEMContext &>(context);
339 unsigned int x_var = 0, y_var = 1, z_var = 2;
350 std::reference_wrapper<DenseSubVector<Number>> F[3] =
357 std::reference_wrapper<DenseSubMatrix<Number>> K[3][3] =
367 const auto distortion_weight = 1. - _dilation_weight;
374 const auto & dphidxi_map = fe_map.get_dphidxi_map();
375 const auto & dphideta_map = fe_map.get_dphideta_map();
376 const auto & dphidzeta_map = fe_map.get_dphidzeta_map();
378 const auto & target_jacobian_dets = _target_jacobian_dets[elem.
type()];
379 const auto & target_jacobians = _target_jacobians[elem.
type()];
393 const auto quad_weight = quad_weights[qp] / target_jacobian_dets[qp];
407 S *= _target_jacobians[elem.
type()][qp];
413 const Real det_sq = det * det;
414 const Real det_cube = det_sq * det;
416 const Real ref_vol_sq = _ref_vol * _ref_vol;
423 const Real tr_div_dim = tr /
dim;
426 const Real half_dim = 0.5 *
dim;
427 const std::vector<Real> trace_powers{
428 std::pow(tr_div_dim, half_dim),
429 std::pow(tr_div_dim, half_dim - 1.),
430 std::pow(tr_div_dim, half_dim - 2.),
444 const auto chi =
chi_epsilon(det, _epsilon_squared_assembly);
445 const Real chi_sq = chi * chi;
446 const Real sqrt_term = std::sqrt(_epsilon_squared_assembly + det_sq);
448 const Real chi_prime = 0.5 * (1. + det / sqrt_term);
449 const Real chi_prime_sq = chi_prime * chi_prime;
451 const Real chi_2prime = 0.5 * (1. / sqrt_term - det_sq / Utility::pow<3>(sqrt_term));
455 const RealTensor dbeta_dS = (trace_powers[1] / chi) * S - (trace_powers[0] / chi_sq * chi_prime * det) * S_inv_T;
460 const Real alpha = (-chi_prime * det_cube + 2. * det_sq * chi - ref_vol_sq * det * chi_prime) / (2. * _ref_vol * chi_sq);
465 const RealTensor dE_dS = distortion_weight * dbeta_dS + _dilation_weight * dmu_dS;
468 std::vector<std::vector<std::vector<Real>>> dphi_maps = {dphidxi_map, dphideta_map, dphidzeta_map};
480 dS_dR(var_id, jj) = dphi_maps[jj][l][qp];
481 dS_dR *= target_jacobians[qp];
485 F[var_id](l) += quad_weight * dE_dS.
contract(dS_dR);
490 if (request_jacobian)
504 const std::vector<Real> d2beta_dS2_coefs_times_distortion_weight = {
508 (trace_powers[1] / chi) * distortion_weight,
510 (((
dim - 2.) /
dim) * trace_powers[2] / chi) * distortion_weight,
512 (-(trace_powers[1] / chi_sq) * chi_prime * det) * distortion_weight,
517 (-(trace_powers[1] / chi_sq) * chi_prime * det) * distortion_weight,
519 (trace_powers[0] * (det / chi_sq)
520 * ((2. * chi_prime_sq / chi - chi_2prime) * det - chi_prime)) * distortion_weight,
522 ((trace_powers[0] / chi_sq) * chi_prime * det) * distortion_weight,
526 const Real dalpha_dS_coef_times_dilation_weight = ((det / (2. * _ref_vol * chi_sq))
527 * (-4. * _ref_vol * alpha * chi * chi_prime - chi_2prime * det_cube
528 - chi_prime * det_sq + 4 * chi * det - ref_vol_sq * (chi_prime + det * chi_2prime))) * _dilation_weight;
531 const Real alpha_times_dilation_weight = alpha * _dilation_weight;
634 dS_dR_l(var_id1, ii) = dphi_maps[ii][l][qp];
635 dS_dR_l *= target_jacobians[qp];
646 dS_dR_p(var_id2, jj) = dphi_maps[jj][p][qp];
647 dS_dR_p *= target_jacobians[qp];
657 const auto S_ij = S(i,j);
658 const auto S_inv_ji = S_inv(j,i);
662 const std::vector<Real> d2beta_dS2_coefs_ij_applied{
663 d2beta_dS2_coefs_times_distortion_weight[1] * S_ij,
664 d2beta_dS2_coefs_times_distortion_weight[2] * S_ij,
665 d2beta_dS2_coefs_times_distortion_weight[3] * S_inv_ji,
666 d2beta_dS2_coefs_times_distortion_weight[4] * S_inv_ji,
669 const auto dalpha_dS_coef_ij_applied = dalpha_dS_coef_times_dilation_weight * S_inv_ji;
671 Real d2E_dSdR_l = 0.;
672 Real d2E_dSdR_p = 0.;
681 if (!(a == var_id1 && i == var_id2) &&
682 !(a == var_id2 && i == var_id1))
685 const auto S_inv_ja = S_inv(j,a);
687 const Real d2beta_dS2_coef_ia_applied = d2beta_dS2_coefs_times_distortion_weight[0] * I(i,a);
688 const Real d2beta_dS2_coef_ja_applied = d2beta_dS2_coefs_times_distortion_weight[5] * S_inv_ja;
689 const Real alpha_ja_applied = alpha_times_dilation_weight * S_inv_ja;
691 const auto b_limit = (a == i) ? j + 1 :
dim;
697 Real d2beta_dS2_times_distortion_weight = (
698 d2beta_dS2_coef_ia_applied * I(j,
b) +
699 d2beta_dS2_coefs_ij_applied[0] * S(a,
b) +
700 d2beta_dS2_coefs_ij_applied[1] * S_inv(
b,a) +
701 d2beta_dS2_coefs_ij_applied[2] * S(a,
b) +
702 d2beta_dS2_coefs_ij_applied[3] * S_inv(
b,a) +
703 d2beta_dS2_coef_ja_applied * S_inv(
b,i)
708 const Real d2mu_dS2_times_dilation_weight = dalpha_dS_coef_ij_applied * S_inv(
b,a) - alpha_ja_applied * S_inv(
b,i);
713 d2beta_dS2_times_distortion_weight +
714 d2mu_dS2_times_dilation_weight;
718 d2E_dSdR_l += d2E_dS2 * dS_dR_l(a,
b);
720 if (!(i == a && j ==
b))
723 d2E_dSdR_p += d2E_dS2 * dS_dR_p(a,
b);
728 d2E_dSdR_l * dS_dR_p(i, j) + d2E_dSdR_p * dS_dR_l(i, j);
733 const Real jacobian_contribution = quad_weight * d2E_dR2;
734 K[var_id1][var_id2](l, p) += jacobian_contribution;
739 K[var_id2][var_id1](p, l) += jacobian_contribution;
748 return request_jacobian;
753 if (!_mesh_info.initialized)
754 compute_mesh_quality_info();
759void VariationalSmootherSystem::compute_mesh_quality_info()
762 if (std::abs(_ref_vol) < TOLERANCE * TOLERANCE)
763 prepare_for_smoothing();
765 std::unique_ptr<DiffContext> con = this->build_context();
766 FEMContext & femcontext = cast_ref<FEMContext &>(*con);
767 this->init_context(femcontext);
769 const auto &
mesh = this->get_mesh();
770 const auto dim =
mesh.mesh_dimension();
771 const Real half_dim = 0.5 *
dim;
772 const auto distortion_weight = 1. - _dilation_weight;
779 const auto & JxW = fe_map.get_JxW();
780 fe_map.get_dxyzdxi();
781 fe_map.get_dxyzdeta();
782 fe_map.get_dxyzdzeta();
786 for (
const auto * elem :
mesh.active_local_element_ptr_range())
795 Real combined_int = 0.;
797 const auto & target_jacobian_dets = _target_jacobian_dets[elem->type()];
801 det_S_int += JxW[qp] / _target_jacobian_dets[elem->type()][qp];
802 const auto quad_weight = quad_weights[qp] / target_jacobian_dets[qp];
808 S *= _target_jacobians[elem->type()][qp];
811 const auto det = S.
det();
812 const auto det_sq = det * det;
814 if (det > info.max_qp_det_S)
815 info.max_qp_det_S = det;
816 else if (det < info.min_qp_det_S)
817 info.min_qp_det_S = det;
819 if (det < TOLERANCE * TOLERANCE)
820 info.mesh_is_tangled =
true;
826 const auto chi =
chi_epsilon(det, _epsilon_squared_assembly);
829 const Real beta = std::pow(tr /
dim, half_dim) / chi;
830 beta_int += beta * quad_weight;
833 const Real mu = 0.5 * (_ref_vol + det_sq / _ref_vol) / chi;
834 mu_int += mu * quad_weight;
837 const Real E = distortion_weight * beta + _dilation_weight * mu;
838 combined_int += E * quad_weight;
841 info.total_det_S += det_S_int;
842 if (det_S_int > info.max_elem_det_S.first)
843 info.max_elem_det_S = std::make_pair(det_S_int, elem->id());
844 if (det_S_int < info.min_elem_det_S.first)
845 info.min_elem_det_S = std::make_pair(det_S_int, elem->id());
847 info.total_distortion += beta_int;
848 if (beta_int > info.max_elem_distortion.first)
849 info.max_elem_distortion = std::make_pair(beta_int, elem->id());
850 if (beta_int < info.min_elem_distortion.first)
851 info.min_elem_distortion = std::make_pair(beta_int, elem->id());
853 info.total_dilation += mu_int;
854 if (mu_int > info.max_elem_dilation.first)
855 info.max_elem_dilation = std::make_pair(mu_int, elem->id());
856 if (mu_int < info.min_elem_dilation.first)
857 info.min_elem_dilation = std::make_pair(mu_int, elem->id());
859 info.total_combined += combined_int;
860 if (combined_int > info.max_elem_combined.first)
861 info.max_elem_combined = std::make_pair(combined_int, elem->id());
862 if (combined_int < info.min_elem_combined.first)
863 info.min_elem_combined = std::make_pair(combined_int, elem->id());
867 libMesh::out <<
"Elem " << elem->id() <<
" quality:" << std::endl
868 <<
" distortion-dilation metric: " << combined_int << std::endl
869 <<
" distortion metric: " << beta_int << std::endl
870 <<
" dilation metric: " << mu_int << std::endl
871 <<
" det(S): " << det_S_int << std::endl;
879 mesh.comm().max(info.max_qp_det_S);
880 mesh.comm().min(info.min_qp_det_S);
881 mesh.comm().sum(info.total_det_S);
885 mesh.comm().sum(info.total_distortion);
889 mesh.comm().sum(info.total_dilation);
893 mesh.comm().sum(info.total_combined);
895 mesh.comm().max(info.mesh_is_tangled);
897 info.initialized =
true;
905void VariationalSmootherSystem::get_target_to_reference_jacobian(
906 const Elem *
const target_elem,
908 std::vector<RealTensor> & jacobians,
909 std::vector<Real> & jacobian_dets)
912 const auto dim = target_elem->
dim();
921 jacobians = std::vector<RealTensor>(nq_points,
RealTensor(
925 jacobian_dets = std::vector<Real>(nq_points, 1.0);
929 bool target_equals_reference =
true;
932 target_equals_reference &= target_elem->
node_ref(local_id) == ref_elem->node_ref(local_id);
933 if (target_equals_reference)
946 fe_map_target.
compute_map(
dim, qrule_weights, target_elem,
false);
956 jacobian_dets[qp] = jacobians[qp].det();
962 os <<
"Mesh quality info:" << std::endl
963 <<
" Mesh distortion-dilation metric: "
964 << info.total_combined << std::endl
965 <<
" Mesh distortion metric: "
966 << info.total_distortion << std::endl
967 <<
" Mesh dilation metric: "
968 << info.total_dilation << std::endl
969 <<
" Max distortion-dilation is in elem "
970 << info.max_elem_combined.second <<
": "
971 << info.max_elem_combined.first << std::endl
972 <<
" Max distortion is in elem "
973 << info.max_elem_distortion.second <<
": "
974 << info.max_elem_distortion.first << std::endl
975 <<
" Max dilation is in elem "
976 << info.max_elem_dilation.second <<
": "
977 << info.max_elem_dilation.first << std::endl
978 <<
" Max det(S) is in elem "
979 << info.max_elem_det_S.second <<
": "
980 << info.max_elem_det_S.first << std::endl
981 <<
" Min distortion-dilation is in elem "
982 << info.min_elem_combined.second <<
": "
983 << info.min_elem_combined.first << std::endl
984 <<
" Min distortion is in elem "
985 << info.min_elem_distortion.second <<
": "
986 << info.min_elem_distortion.first << std::endl
987 <<
" Min dilation is in elem "
988 << info.min_elem_dilation.second <<
": "
989 << info.min_elem_dilation.first << std::endl
990 <<
" Min det(S) is in elem "
991 << info.min_elem_det_S.second <<
": "
992 << info.min_elem_det_S.first << std::endl
993 <<
" Max qp det(S): " << info.max_qp_det_S << std::endl
994 <<
" Min qp det(S): " << info.min_qp_det_S << std::endl
995 <<
" Mesh-integrated det(S): " << info.total_det_S << std::endl
996 <<
" Tangled: " << info.mesh_is_tangled << std::endl;
void maxloc(T &r, unsigned int &max_id) const
void minloc(T &r, unsigned int &min_id) const
void broadcast(T &data, const unsigned int root_id=0, const bool identical_sizes=false) const
This class provides all data required for a physics package (e.g.
const DenseVector< Number > & get_elem_residual() const
Const accessor for element residual.
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
This is the base class from which all geometric element types are derived.
const Node & node_ref(const unsigned int i) const
virtual unsigned int n_nodes() const =0
virtual unsigned short dim() const =0
const Elem * reference_elem() const
virtual ElemType type() const =0
IntRange< unsigned short > node_index_range() const
const FEMap & get_fe_map() const
This class forms the foundation from which generic finite elements may be derived.
This class provides all data required for a physics package (e.g.
void get_side_fe(unsigned int var, FEGenericBase< OutputShape > *&fe) const
Accessor for edge/face (2D/3D) finite element object for variable var for the largest dimension in th...
const std::set< unsigned char > & elem_dimensions() const
virtual void pre_fe_reinit(const System &, const Elem *e)
Reinitializes local data vectors/matrices on the current geometric element.
const QBase & get_element_qrule() const
Accessor for element interior quadrature rule for the dimension of the current _elem.
unsigned char get_dim() const
Accessor for cached mesh dimension.
const Elem & get_elem() const
Accessor for current Elem object.
virtual void elem_fe_reinit(const std::vector< Point > *const pts=nullptr)
Reinitializes interior FE objects on the current geometric element.
void get_element_fe(unsigned int var, FEGenericBase< OutputShape > *&fe) const
Accessor for interior finite element object for variable var for the largest dimension in the mesh.
void use_quadrature_rules(QuadratureType qt, int extra_quadrature_order=0)
Use a specified quadrature rule type qt for every element, side, and edge dimension,...
virtual void assembly(bool get_residual, bool get_jacobian, bool apply_heterogeneous_constraints=false, bool apply_no_constraints=false) override
Prepares matrix or rhs for matrix assembly.
Class contained in FE that encapsulates mapping (i.e.
const std::vector< RealGradient > & get_dxyzdeta() const
const std::vector< RealGradient > & get_dxyzdzeta() const
void init_reference_to_physical_map(const std::vector< Point > &qp, const Elem *elem)
const std::vector< RealGradient > & get_dxyzdxi() const
virtual void compute_map(const unsigned int dim, const std::vector< Real > &qw, const Elem *elem, bool calculate_d2phi)
Compute the jacobian and some other additional data fields.
A Node is like a Point, but with more information.
const Parallel::Communicator & comm() const
const std::vector< Point > & get_points() const
unsigned int n_points() const
const std::vector< Real > & get_weights() const
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
const MeshBase & get_mesh() const
This class defines a tensor in LIBMESH_DIM dimensional Real or Complex space.
TypeTensor< T > inverse() const
TypeTensor< T > transpose() const
LIBMESH_DEVICE_INLINE CompareTypes< T, T2 >::supertype contract(const TypeTensor< T2 > &) const
Multiply 2 tensors together to return a scalar, i.e.
~VariationalSmootherSystem() override
virtual void assembly(bool get_residual, bool get_jacobian, bool apply_heterogeneous_constraints=false, bool apply_no_constraints=false) override
Assembly method to update the mesh based on the smoother solve.
void sync_dofobject_data_by_id(const Communicator &comm, const Iterator &range_begin, const Iterator &range_end, SyncFunctor &sync)
Request data about a range of ghost dofobjects uniquely identified by their id.
void parallel_for_each(Range &&range, Body &&body, unsigned int n_threads=libMesh::n_threads())
A nicer syntax for parallel_for calls in cases where no setup or teardown is needed outside the subra...
The libMesh namespace provides an interface to certain functionality in the library.
Real trace(const RealTensor &A, const unsigned int &dim)
Compute the trace of a dim-dimensional matrix.
Real chi_epsilon(const Real &x, const Real epsilon_squared)
Function to prevent dividing by zero for degenerate elements.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
std::ostream & operator<<(std::ostream &os, const OrderWrapper &order)
Overload stream operators.
void communicate_pair_min(std::pair< Real, dof_id_type > &pair, const Parallel::Communicator &comm)
RealTensorValue RealTensor
RealTensor get_jacobian_at_qp(const FEMap &fe_map, const unsigned int &dim, const unsigned int &qp)
Given an fe_map, element dimension, and quadrature point index, returns the Jacobian of the physical-...
RealVectorValue RealGradient
void communicate_pair_max(std::pair< Real, dof_id_type > &pair, const Parallel::Communicator &comm)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...
Struct to hold smoother-relevant information about the mesh quality.