libMesh
Loading...
Searching...
No Matches
smoothness_estimator.C
Go to the documentation of this file.
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
19// libmesh includes
20#include "libmesh/libmesh_common.h"
21#include "libmesh/smoothness_estimator.h"
22#include "libmesh/dof_map.h"
23#include "libmesh/fe_base.h"
24#include "libmesh/error_vector.h"
25#include "libmesh/libmesh_logging.h"
26#include "libmesh/elem.h"
27#include "libmesh/quadrature.h"
28#include "libmesh/system.h"
29#include "libmesh/mesh_base.h"
30#include "libmesh/numeric_vector.h"
31#include "libmesh/enum_to_string.h"
32
33// C++ includes
34#include <algorithm> // for std::fill
35#include <cstdlib> // *must* precede <cmath> for proper std:abs() on PGI, Sun Studio CC
36#include <cmath> // for std::sqrt std::pow std::abs
37
38namespace libMesh
39{
40
41
42//-----------------------------------------------------------------
43// SmoothnessEstimator implementations
45 _extra_order(1)
46{
47}
48
49
50
51std::vector<Real> SmoothnessEstimator::legepoly(const unsigned int dim,
52 const Order order,
53 const Point p,
54 const unsigned int matsize)
55{
56 std::vector<Real> psi;
57 psi.reserve(matsize);
58
59 // Evaluate 1D Legendre polynomials at x, y, z up to order
60 const int npols = static_cast<int>(order) + 1;
61
62 std::vector<Real> Lx(npols, 0.), Ly(npols, 1.), Lz(npols, 1.);
63 const Real x = p(0);
64 Lx[0] = 1.;
65 if (npols > 1)
66 Lx[1] = x;
67 for (int n = 2; n < npols; ++n)
68 Lx[n] = ((2. * n - 1) * x * Lx[n - 1] - (n - 1) * Lx[n - 2]) / n;
69
70 if (dim > 1)
71 {
72 const Real y = p(1);
73 Ly[0] = 1.;
74 if (npols > 1)
75 Ly[1] = y;
76 for (int n = 2; n < npols; ++n)
77 Ly[n] = ((2. * n - 1) * y * Ly[n - 1] - (n - 1) * Ly[n - 2]) / n;
78 }
79
80 if (dim > 2)
81 {
82 const Real z = p(2);
83 Lz[0] = 1.;
84 if (npols > 1)
85 Lz[1] = z;
86 for (int n = 2; n < npols; ++n)
87 Lz[n] = ((2. * n - 1) * z * Lz[n - 1] - (n - 1) * Lz[n - 2]) / n;
88 }
89
90 // Loop over total degree and build tensor-product Legendre basis
91 for (unsigned int poly_deg = 0; poly_deg <= static_cast<unsigned int>(order); poly_deg++)
92 {
93 switch (dim)
94 {
95 case 3:
96 for (int i = poly_deg; i >= 0; --i)
97 for (int j = poly_deg - i; j >= 0; --j)
98 {
99 int k = poly_deg - i - j;
100 psi.push_back(Lx[i] * Ly[j] * Lz[k]);
101 }
102 break;
103
104 case 2:
105 for (int i = poly_deg; i >= 0; --i)
106 {
107 int j = poly_deg - i;
108 psi.push_back(Lx[i] * Ly[j]);
109 }
110 break;
111
112 case 1:
113 psi.push_back(Lx[poly_deg]);
114 break;
115
116 default:
117 libmesh_error_msg("Invalid dimension dim " << dim);
118 }
119 }
120
121 return psi;
122}
123
125{
126 const Real denom = (N * Sxx - Sx * Sx);
127 // Triggers for first order polynomials when there only 1 point
128 // available at log |c_k| and log |k| space to fit.
129 if (std::abs(denom) < std::numeric_limits<Real>::epsilon()) {
130 return std::numeric_limits<Real>::max();
131 }
132 return (N * Sxy - Sx * Sy) / denom;
133}
134
135void SmoothnessEstimator::reduce_smoothness (std::vector<ErrorVectorReal> & smoothness_per_cell,
136 const Parallel::Communicator & comm)
137{
138 // Aggregates element-wise contributions computed
139 // in parallel across all processors
140 comm.sum(smoothness_per_cell);
141}
142
144 ErrorVector & smoothness_per_cell,
145 const NumericVector<Number> * solution_vector)
146{
147 LOG_SCOPE("estimate_smoothness()", "SmoothnessEstimator");
148
149 // The current mesh
150 const MeshBase & mesh = system.get_mesh();
151
152 // Resize the smoothness_per_cell vector to be
153 // the number of elements, initialize it to 0.
154 smoothness_per_cell.resize (mesh.max_elem_id());
155 std::fill (smoothness_per_cell.begin(), smoothness_per_cell.end(), 0.);
156
157 // Prepare current_local_solution to localize a non-standard
158 // solution vector if necessary
159 if (solution_vector && solution_vector != system.solution.get())
160 {
161 NumericVector<Number> * newsol =
162 const_cast<NumericVector<Number> *>(solution_vector);
163 System & sys = const_cast<System &>(system);
164 newsol->swap(*sys.solution);
165 sys.update();
166 }
167
168 //------------------------------------------------------------
169 // Iterate over all the active elements in the mesh
170 // that live on this processor.
171 Threads::parallel_for (mesh.active_local_element_stored_range(),
172 EstimateSmoothness(system,
173 *this,
174 smoothness_per_cell)
175 );
176
177 // Each processor has now computed the smoothness for its local
178 // elements, and smoothness_per_cell contains 0 for all the
179 // non-local elements. Summing the vector will provide the true
180 // value for each element, local or remote
181 this->reduce_smoothness (smoothness_per_cell, system.comm());
182
183 // If we used a non-standard solution before, now is the time to fix
184 // the current_local_solution
185 if (solution_vector && solution_vector != system.solution.get())
186 {
187 NumericVector<Number> * newsol = const_cast<NumericVector<Number> *>(solution_vector);
188 System & sys = const_cast<System &>(system);
189 newsol->swap(*sys.solution);
190 sys.update();
191 }
192}
193
194
196{
197 // The current mesh
198 const MeshBase & mesh = system.get_mesh();
199
200 // The dimensionality of the mesh
201 const unsigned int dim = mesh.mesh_dimension();
202
203 // The number of variables in the system
204 const unsigned int n_vars = system.n_vars();
205
206 // The DofMap for this system
207 const DofMap & dof_map = system.get_dof_map();
208
209 //------------------------------------------------------------
210 // Iterate over all the elements in the range.
211for (const auto & elem : range)
212{
213 const dof_id_type e_id = elem->id();
214 // Loop over each variable
215 for (unsigned int var=0; var<n_vars; var++)
216 {
217 const FEType & fe_type = dof_map.variable_type(var);
218 const Order element_order = fe_type.order + elem->p_level();
219
220 std::unique_ptr<FEBase> fe(FEBase::build(dim, fe_type));
221 std::unique_ptr<QBase> qrule(fe_type.default_quadrature_rule(dim, smoothness_estimator._extra_order));
222 fe->attach_quadrature_rule(qrule.get());
223
224 const std::vector<Real> & JxW = fe->get_JxW();
225 const std::vector<Point> & q_point = fe->get_xyz();
226 const std::vector<std::vector<Real>> * phi = &(fe->get_phi());
227
228 std::vector<dof_id_type> dof_indices;
229
230 unsigned int matsize = element_order + 1;
231 if (dim > 1)
232 {
233 matsize *= (element_order + 2);
234 matsize /= 2;
235 }
236 if (dim > 2)
237 {
238 matsize *= (element_order + 3);
239 matsize /= 3;
240 }
241
242 DenseMatrix<Number> Kp(matsize, matsize);
245
246 F.resize(matsize);
247 Pu_h.resize(matsize);
248
249
250 fe->reinit(elem);
251
252 dof_map.dof_indices(elem, dof_indices, var);
253 libmesh_assert_equal_to(dof_indices.size(), phi->size());
254
255 const unsigned int n_dofs = cast_int<unsigned int>(dof_indices.size());
256 const unsigned int n_qp = qrule->n_points();
257
258 for (unsigned int qp = 0; qp < n_qp; qp++)
259 {
260 std::vector<Real> psi = legepoly(dim, element_order, q_point[qp], matsize);
261 const unsigned int psi_size = cast_int<unsigned int>(psi.size());
262
263 for (unsigned int i = 0; i < matsize; i++)
264 for (unsigned int j = 0; j < matsize; j++)
265 Kp(i, j) += JxW[qp] * psi[i] * psi[j];
266
267 Number u_h = libMesh::zero;
268 for (unsigned int i = 0; i < n_dofs; i++)
269 u_h += (*phi)[i][qp] * system.current_solution(dof_indices[i]);
270
271 for (unsigned int i = 0; i < psi_size; i++)
272 F(i) += JxW[qp] * u_h * psi[i];
273 }
274
275 Kp.lu_solve(F, Pu_h);
276
277 // Generate index to total degree map. Total_degree_per_index[i] gives the degree ith basis function
278 std::vector<unsigned int> total_degree_per_index;
279
280 for (unsigned int poly_deg = 0; poly_deg <= static_cast<unsigned int>(element_order); ++poly_deg)
281 {
282 switch (dim)
283 {
284 case 3:
285 for (int i = poly_deg; i >= 0; --i)
286 for (int j = poly_deg - i; j >= 0; --j)
287 {
288 int k = poly_deg - i - j;
289 total_degree_per_index.push_back(i + j + k);
290 }
291 break;
292
293 case 2:
294 for (int i = poly_deg; i >= 0; --i)
295 {
296 int j = poly_deg - i;
297 total_degree_per_index.push_back(i + j);
298 }
299 break;
300
301 case 1:
302 total_degree_per_index.push_back(poly_deg);
303 break;
304
305 default:
306 libmesh_error_msg("Invalid dimension dim " << dim);
307 }
308 }
309
310 // Group coefficients |c_k| by degree k
311 std::map<unsigned int, std::vector<Number>> coeff_by_degree;
312
313 for (unsigned int i = 0; i < Pu_h.size(); ++i)
314 {
315 unsigned int degree = total_degree_per_index[i];
316 // Excluding the constant mode i.e, zeroth order coefficient as we use ln|order| later
317 // Constant order term often dominates the scale and doesn't inform about regularity.
318 if (degree > 0)
319 coeff_by_degree[degree].push_back(Pu_h(i));
320 }
321
322 // Compute L2 norm of each group |c_k|
323 std::vector<unsigned int> degrees;
324 std::vector<double> norms;
325
326 for (const auto & pair : coeff_by_degree)
327 {
328 unsigned int deg = pair.first;
329 const std::vector<Number> & coeffs = pair.second;
330
331 double norm = 0.;
332 for (const auto & c : coeffs)
333 norm += std::norm(c);
334
335 norm = std::sqrt(norm);
336
337 degrees.push_back(deg);
338 norms.push_back(norm);
339 }
340
341
342 // Collect log |c_k| and log |k|
343 std::vector<double> log_norms, log_degrees;
344
345 for (unsigned int i = 0; i < degrees.size(); ++i)
346 {
347 // filter tiny terms
348 if (norms[i] > 1e-12)
349 {
350 log_degrees.push_back(std::log(static_cast<double>(degrees[i])));
351 log_norms.push_back(std::log(norms[i]));
352 }
353 }
354
355
356 // Fit log-log line - we use least-squares fit
357 double Sx = 0., Sy = 0., Sxx = 0., Sxy = 0.;
358 const size_t N = log_degrees.size();
359
360 for (size_t i = 0; i < N; ++i)
361 {
362 Sx += log_degrees[i];
363 Sy += log_norms[i];
364 Sxx += log_degrees[i] * log_degrees[i];
365 Sxy += log_degrees[i] * log_norms[i];
366 }
367
368 const double regularity = -compute_slope(N, Sx, Sy, Sxx, Sxy);
369 // const double intercept = (Sy - slope * Sx) / N;
370
371 Threads::spin_mutex::scoped_lock acquire(Threads::spin_mtx);
372 smoothness_per_cell[e_id] = regularity;
373 } // end variables loop
374
375} // end element loop
376
377} // End () operator definition
378
379} // namespace libMesh
unsigned int n_vars
unsigned int dim
Defines a dense matrix for use in Finite Element-type computations.
void lu_solve(const DenseVector< T > &b, DenseVector< T > &x)
Solve the system Ax=b given the input vector b.
Defines a dense vector for use in Finite Element-type computations.
void resize(const unsigned int n)
Resize the vector.
virtual unsigned int size() const override final
This class handles the numbering of degrees of freedom on a mesh.
Definition dof_map.h:181
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition dof_map.C:2231
const FEType & variable_type(const unsigned int i) const
Definition dof_map.h:2472
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition fe_type.h:197
std::unique_ptr< QBase > default_quadrature_rule(const unsigned int dim, const int extraorder=0) const
Definition fe_type.C:35
OrderWrapper order
The approximation order of the element (at 0 p-refinement level).
Definition fe_type.h:203
This is the MeshBase class.
Definition mesh_base.h:81
unsigned int mesh_dimension() const
Definition mesh_base.C:515
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
virtual void swap(NumericVector< T > &v)
Swaps the contents of this with v.
const Parallel::Communicator & comm() const
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:41
Class to compute the error contribution for a range of elements.
void operator()(const ConstElemRange &range) const
static std::vector< Real > legepoly(const unsigned int dim, const Order order, const Point p, const unsigned int matsize)
virtual void estimate_smoothness(const System &system, ErrorVector &smoothness_per_cell, const NumericVector< Number > *solution_vector=nullptr)
This function uses the Legendre expansion of solution to estimate coefficient decay to quantify the s...
static Real compute_slope(int N, Real Sx, Real Sy, Real Sxx, Real Sxy)
Computes slop in a linear regression.
int _extra_order
Extra order to use for quadrature rule.
void reduce_smoothness(std::vector< ErrorVectorReal > &error_per_cell, const Parallel::Communicator &comm)
The StoredRange class defines a contiguous, divisible set of objects.
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition system.h:100
Number current_solution(const dof_id_type global_dof_number) const
Definition system.C:164
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
Definition system.h:1667
unsigned int n_vars() const
Definition system.C:2729
const DofMap & get_dof_map() const
Definition system.h:2448
const MeshBase & get_mesh() const
Definition system.h:2432
MeshBase & mesh
spin_mutex spin_mtx
A convenient spin mutex object which can be used for obtaining locks.
Definition threads.C:30
void parallel_for(const Range &range, const Body &body, unsigned int n_threads=libMesh::n_threads())
Execute the provided function object in parallel on the specified range.
The libMesh namespace provides an interface to certain functionality in the library.
const Number zero
.
Definition libmesh.h:297
uint8_t dof_id_type
Definition id_types.h:67
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
auto norm(const libMesh::TypeVector< T > &vector) -> decltype(std::norm(T()))