LCOV - code coverage report
Current view: top level - src/mesh - mesh_smoother_vsmoother.C (source / functions) Hit Total Coverage
Test: libMesh/libmesh: #4640 (c5f9f0) with base a68dd6 Lines: 54 58 93.1 %
Date: 2026-11-02 23:02:27 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // The libMesh Finite Element Library.
       2             : // Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
       3             : 
       4             : // This library is free software; you can redistribute it and/or
       5             : // modify it under the terms of the GNU Lesser General Public
       6             : // License as published by the Free Software Foundation; either
       7             : // version 2.1 of the License, or (at your option) any later version.
       8             : 
       9             : // This library is distributed in the hope that it will be useful,
      10             : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             : // Lesser General Public License for more details.
      13             : 
      14             : // You should have received a copy of the GNU Lesser General Public
      15             : // License along with this library; if not, write to the Free Software
      16             : // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      17             : 
      18             : #include "libmesh/libmesh_config.h"
      19             : #if defined(LIBMESH_ENABLE_VSMOOTHER)
      20             : 
      21             : // Local includes
      22             : #include "libmesh/mesh_smoother_vsmoother.h"
      23             : #include "libmesh/mesh_tools.h"
      24             : #include "libmesh/elem.h"
      25             : #include "libmesh/unstructured_mesh.h"
      26             : #include "libmesh/utility.h"
      27             : #include "libmesh/boundary_info.h"
      28             : #include "libmesh/equation_systems.h"
      29             : #include "libmesh/distributed_mesh.h"
      30             : #include "libmesh/steady_solver.h"
      31             : #include "libmesh/diff_solver.h"
      32             : #include "libmesh/parallel_ghost_sync.h"
      33             : #include "libmesh/libmesh_logging.h"
      34             : #include "libmesh/node_range.h"
      35             : #include "libmesh/threads.h"
      36             : 
      37             : // C++ includes
      38             : #include <time.h> // for clock_t, clock()
      39             : #include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
      40             : #include <cmath>
      41             : #include <iomanip>
      42             : #include <limits>
      43             : 
      44             : namespace libMesh
      45             : {
      46             : 
      47             : // Optimization at -O2 or greater seem to break Intel's icc. So if we are
      48             : // being compiled with icc let's dumb-down the optimizations for this file
      49             : #ifdef __INTEL_COMPILER
      50             : #  pragma optimize ( "", off )
      51             : #endif
      52             : 
      53             : // Member functions for the Variational Smoother
      54        2556 : VariationalMeshSmoother::VariationalMeshSmoother(
      55             :     UnstructuredMesh &mesh, Real dilation_weight,
      56             :     const bool preserve_subdomain_boundaries,
      57             :     const Real relative_residual_tolerance,
      58        2556 :     const Real absolute_residual_tolerance, const unsigned int verbosity)
      59        2412 :     : MeshSmoother(mesh), _verbosity(verbosity),
      60        2412 :       _dilation_weight(dilation_weight),
      61        2412 :       _preserve_subdomain_boundaries(preserve_subdomain_boundaries),
      62        2412 :       _setup_called(false),
      63        2412 :       _relative_residual_tolerance(relative_residual_tolerance),
      64        2628 :       _absolute_residual_tolerance(absolute_residual_tolerance) {}
      65             : 
      66        2556 : void VariationalMeshSmoother::setup()
      67             : {
      68             :   // Check for multiple dimensions
      69        2628 :   if (!_mesh.preparation().has_cached_elem_data)
      70         923 :     _mesh.cache_elem_data();
      71             : 
      72        2628 :   if (_mesh.elem_dimensions().size() > 1)
      73           0 :     libmesh_not_implemented_msg("Meshes containing elements of differing dimension are not yet supported.");
      74             : 
      75             :   /*
      76             :   Ideally we'd want to update _mesh directly even if it already has an
      77             :   EquationSystems attached and doing work on it ... and that's thwarted by the
      78             :   problems:
      79             : 
      80             :   1. We can't easily tell if there's already an EquationSystems attached.
      81             :   2. We can't attach a second EquationSystems safely (if it already has a system 0)
      82             :      because the DoF indexing will need to be overwritten by our system.
      83             :   3. The destructor of es won't even clean up after itself (we generally expect
      84             :      a mesh to go unused after its EquationSystems is destroyed), much less know
      85             :      how to restore anything from a previous EquationSystems.
      86             : 
      87             :   To avoid these issues, we'll just construct a new DistributedMesh _mesh_copy
      88             :   from _mesh, then do the solve on _mesh_copy, then copy its node locations back
      89             :   to _mesh after the solve is done. That'll be slightly less memory-efficient
      90             :   and somewhat more CPU-efficient in the case where _mesh is serial, though
      91             :   it'll be significantly less memory-efficient when _mesh is already distributed,
      92             :   but either way the robustness is probably worth it.
      93             :   */
      94             : 
      95             :   // Create a new mesh, EquationSystems, and System
      96        5040 :   _mesh_copy = std::make_unique<DistributedMesh>(_mesh);
      97             : 
      98             :   // If the _mesh wasn't prepared, that's fine (we'll just be moving
      99             :   // its nodes), but we do need the copy to be prepared before our
     100             :   // solve does things like looking at neighbors.  We'll disable
     101             :   // repartitioning and renumbering first to make sure that we can
     102             :   // transfer our geometry changes back to the original mesh.
     103          72 :   _mesh_copy->allow_renumbering(false);
     104          72 :   _mesh_copy->skip_partitioning(true);
     105        2556 :   _mesh_copy->complete_preparation();
     106             : 
     107        5040 :   _equation_systems = std::make_unique<EquationSystems>(*_mesh_copy);
     108        2556 :   _system =
     109        2556 :       &(_equation_systems->add_system<VariationalSmootherSystem>("variational_smoother_system"));
     110             : 
     111             :   // Set this to something > 0 to add more quadrature points than the default
     112             :   // rule that integrates order 2 * fe_order + 1 polynomials exactly.
     113             :   // Using higher quadrature orders has not had a significant effect on observed solutions.
     114             :   //system()->extra_quadrature_order = 0;
     115             : 
     116             :   // The default Gauss rule samples the element interior only, so it can miss
     117             :   // degeneracies localized at element corners (e.g. an element collapsing
     118             :   // toward one of its nodes). Selecting a vertex-sampling rule (QTRAP,
     119             :   // QSIMPSON, QNODAL, QGAUSS_LOBATTO) evaluates the metric at the element
     120             :   // nodes instead. Defaults to QGAUSS.
     121        2556 :   system()->set_quadrature_type(_quadrature_type);
     122             : 
     123             :   // Uncomment these to debug
     124             :   //system()->print_element_solutions=true;
     125             :   //system()->print_element_residuals=true;
     126             :   //system()->print_element_jacobians=true;
     127             : 
     128             :   // Add boundary node and hanging node constraints
     129        2628 :   _constraint = std::make_unique<VariationalSmootherConstraint>(
     130        2556 :       *_system, _preserve_subdomain_boundaries, _verbosity);
     131        2628 :   system()->attach_constraint_object(*_constraint);
     132             : 
     133             :   // Set system parameters
     134        2556 :   system()->set_verbosity(_verbosity);
     135        2556 :   system()->get_dilation_weight() = _dilation_weight;
     136             : 
     137             :   // Set up solver
     138        2628 :   system()->time_solver = std::make_unique<SteadySolver>(*_system);
     139             : 
     140             :   // Uncomment this line and use -snes_test_jacobian and -snes_test_jacobian_view
     141             :   // flags to compare the hand-coded jacobian in VariationalSmootherSystem
     142             :   // to finite difference jacobians.
     143             :   //system()->time_solver->diff_solver() = std::make_unique<PetscDiffSolver>(*_system);
     144             : 
     145        2556 :   _equation_systems->init();
     146             : 
     147             :   // Solver verbosity
     148        2556 :   if (_verbosity > 15) {
     149         146 :     DiffSolver &solver = *(system()->time_solver->diff_solver().get());
     150         138 :     solver.quiet = false;
     151         142 :     solver.verbose = true;
     152             :   }
     153             : 
     154             :   // Solver convergence tolerances
     155        2628 :   system()->time_solver->diff_solver()->relative_residual_tolerance = _relative_residual_tolerance;
     156        2628 :   system()->time_solver->diff_solver()->absolute_residual_tolerance = _absolute_residual_tolerance;
     157             : 
     158        2556 :   _setup_called = true;
     159        2556 : }
     160             : 
     161        2556 : void VariationalMeshSmoother::smooth()
     162             : {
     163         144 :   LOG_SCOPE("smooth()", "VariationalMeshSmoother");
     164             : 
     165        2556 :   if (!_setup_called)
     166        2414 :     setup();
     167             : 
     168             :   libmesh_try
     169             :     {
     170        2556 :       system()->solve();
     171             :     }
     172             : 
     173           0 :   libmesh_catch (ConvergenceFailure& e)
     174             :     {
     175             : #ifdef LIBMESH_ENABLE_EXCEPTIONS
     176           0 :       throw ConvergenceFailure("The VariationalMeshSmoother solve failed to converge.");
     177             : #endif
     178           0 :     }
     179             : 
     180             :   // Update _mesh from _mesh_copy
     181             :   Threads::parallel_for_each
     182        2556 :     (_mesh_copy->local_node_stored_range(),
     183       42978 :      [this](Node * node_copy)
     184             :      {
     185       39672 :        Point & node = _mesh.node_ref(node_copy->id());
     186       39672 :        node = *node_copy;
     187       36366 :      });
     188             : 
     189        2556 :   SyncNodalPositions sync_object(_mesh);
     190        5040 :   Parallel::sync_dofobject_data_by_id (_mesh.comm(), _mesh.nodes_begin(), _mesh.nodes_end(), sync_object);
     191             : 
     192             :   // Release memory occupied by _mesh_copy
     193             :   // Destruct this before _mesh_copy because it references _mesh_copy
     194          72 :   _equation_systems.reset();
     195          72 :   _mesh_copy.reset();
     196             :   // We'll need to call setup again since we'll have to reconstruct _mesh_copy
     197        2556 :   _setup_called = false;
     198        2556 : }
     199             : 
     200         142 : const MeshQualityInfo & VariationalMeshSmoother::get_mesh_info() const
     201             : {
     202         142 :   libmesh_error_msg_if(!_setup_called, "Need to first call the setup() method of "
     203             :       << "this VariationalMeshSmoother object, then call get_mesh_info() prior "
     204             :       << "to calling smooth().");
     205         142 :   return _system->get_mesh_info();
     206             : }
     207             : 
     208             : } // namespace libMesh
     209             : 
     210             : #endif // defined(LIBMESH_ENABLE_VSMOOTHER)

Generated by: LCOV version 1.14