MFEM v4.8.0
Finite element discretization library
Loading...
Searching...
No Matches
dist_solver.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_DIST_SOLVER_HPP
13#define MFEM_DIST_SOLVER_HPP
14
15#include "mfem.hpp"
16
17#ifdef MFEM_USE_MPI
18
19namespace mfem
20{
21
22namespace common
23{
24
25real_t AvgElementSize(ParMesh &pmesh);
26
28{
29protected:
31
32public:
33 // 0 = nothing, 1 = main solver only, 2 = full (solver + preconditioner).
35
37 virtual ~DistanceSolver() { }
38
39 // Computes a scalar ParGridFunction which is the length of the shortest path
40 // to the zero level set of the given Coefficient. It is expected that the
41 // given [distance] has a valid (scalar) ParFiniteElementSpace, and that the
42 // result is computed in the same space. Some implementations may output a
43 // "signed" distance, i.e., the distance has different signs on both sides of
44 // the zero level set.
45 virtual void ComputeScalarDistance(Coefficient &zero_level_set,
46 ParGridFunction &distance) = 0;
47
48 // Computes a vector ParGridFunction where the magnitude is the length of the
49 // shortest path to the zero level set of the given Coefficient, and the
50 // direction is the starting direction of the shortest path. It is expected
51 // that the given [distance] has a valid (vector) ParFiniteElementSpace, and
52 // that the result is computed in the same space.
53 virtual void ComputeVectorDistance(Coefficient &zero_level_set,
54 ParGridFunction &distance);
55};
56
57
58// K. Crane et al: "Geodesics in Heat: A New Approach to Computing Distance
59// Based on Heat Flow", DOI:10.1145/2516971.2516977.
61{
62public:
64 : DistanceSolver(), parameter_t(diff_coeff), smooth_steps(0),
65 diffuse_iter(1), transform(true), vis_glvis(false) { }
66
67 // The computed distance is not "signed". In addition to the standard usage
68 // (with zero level sets), this function can be applied to point sources when
69 // transform = false.
70 void ComputeScalarDistance(Coefficient &zero_level_set,
71 ParGridFunction &distance);
72
76};
77
78// A. Belyaev et al: "On Variational and PDE-based Distance Function
79// Approximations", Section 6, DOI:10.1111/cgf.12611.
80// This solver is computationally cheap, but is accurate for distance
81// approximations only near the zero level set.
83{
84private:
85
86 class NormalizationCoeff : public Coefficient
87 {
88 private:
90
91 public:
92 NormalizationCoeff(ParGridFunction &u_gf) : u(u_gf) { }
94 const IntegrationPoint &ip) override;
95 };
96
97public:
99
101};
102
103
104// A. Belyaev et al: "On Variational and PDE-based Distance Function
105// Approximations", Section 7, DOI:10.1111/cgf.12611.
107{
108public:
109 PLapDistanceSolver(int maxp_ = 30, int newton_iter_ = 10,
110 real_t rtol = 1e-7, real_t atol = 1e-12)
111 : maxp(maxp_), newton_iter(newton_iter_),
112 newton_rel_tol(rtol), newton_abs_tol(atol) { }
113
114 void SetMaxPower(int new_pp) { maxp = new_pp; }
115
116 // The computed distance is "signed".
118
119private:
120 int maxp; // maximum value of the power p
121 const int newton_iter;
122 const real_t newton_rel_tol, newton_abs_tol;
123};
124
126{
127private:
128 const GridFunction &u;
129
130public:
133
135
137 {
138 T.SetIntPoint(&ip);
139
140 u.GetGradient(T, V);
141 const real_t norm = V.Norml2() + 1e-12;
142 V /= -norm;
143 }
144};
145
146
147// Product of the modulus of the first coefficient and the second coefficient
149{
150private:
151 Coefficient &basef, &corrf;
152
153public:
155 : basef(basec_), corrf(corrc_) { }
156
158 const IntegrationPoint &ip) override
159 {
160 T.SetIntPoint(&ip);
161 real_t u = basef.Eval(T,ip);
162 real_t c = corrf.Eval(T,ip);
163 if (u<0.0) { u*=-1.0; }
164 return u*c;
165 }
166};
167
168
169// Formulation for the ScreenedPoisson equation. The positive part of the input
170// coefficient supply unit volumetric loading, the negative part - negative unit
171// volumetric loading. The parameter rh is the radius of a linear cone filter
172// which will deliver similar smoothing effect as the Screened Poisson
173// equation. It determines the length scale of the smoothing.
175{
176protected:
179
180public:
182 {
183 real_t rd=rh/(2*std::sqrt(3.0));
184 diffcoef= rd*rd;
185 }
186
188
189 void SetInput(Coefficient &nfunc) { func = &nfunc; }
190
193 const Vector &elfun) override;
194
197 const Vector &elfun,
198 Vector &elvect) override;
199
200 void AssembleElementGrad(const FiniteElement &el,
202 const Vector &elfun,
203 DenseMatrix &elmat) override;
204};
205
206
208{
209
210protected:
215
216public:
217 // The VectorCoefficent should contain a vector with entries:
218 // [0] - derivative with respect to x
219 // [1] - derivative with respect to y
220 // [2] - derivative with respect to z
222 bool ownership_=true)
223 : func(nfunc), fgrad(nfgrad), ownership(ownership_), pp(2.0), ee(1e-7) { }
224
225 void SetPower(real_t pp_) { pp = pp_; }
226 void SetReg(real_t ee_) { ee = ee_; }
227
229 {
230 if (ownership)
231 {
232 delete func;
233 delete fgrad;
234 }
235 }
236
239 const Vector &elfun) override;
240
243 const Vector &elfun,
244 Vector &elvect) override;
245
246 void AssembleElementGrad(const FiniteElement &el,
248 const Vector &elfun,
249 DenseMatrix &elmat) override;
250};
251
252// Low-pass filter based on the Screened Poisson equation.
253// B. S. Lazarov, O. Sigmund: "Filters in topology optimization based on
254// Helmholtz-type differential equations", DOI:10.1002/nme.3072.
256{
257public:
258 PDEFilter(ParMesh &mesh, real_t rh, int order = 2,
259 int maxiter = 100, real_t rtol = 1e-12,
260 real_t atol = 1e-15, int print_lv = 0)
261 : rr(rh),
262 fecp(order, mesh.Dimension()),
263 fesp(&mesh, &fecp, 1),
264 gf(&fesp)
265 {
266 sv = fesp.NewTrueDofVector();
267
268 nf = new ParNonlinearForm(&fesp);
269 prec = new HypreBoomerAMG();
270 prec->SetPrintLevel(print_lv);
271
272 gmres = new GMRESSolver(mesh.GetComm());
273
274 gmres->SetAbsTol(atol);
275 gmres->SetRelTol(rtol);
276 gmres->SetMaxIter(maxiter);
277 gmres->SetPrintLevel(print_lv);
278 gmres->SetPreconditioner(*prec);
279
280 sint=nullptr;
281 }
282
284 {
285 delete gmres;
286 delete prec;
287 delete nf;
288 delete sv;
289 }
290
292 {
293 GridFunctionCoefficient gfc(&func);
294 Filter(gfc, ffield);
295 }
296
297 void Filter(Coefficient &func, ParGridFunction &ffield);
298
299private:
300 const real_t rr;
301 H1_FECollection fecp;
304
306 HypreBoomerAMG* prec;
307 GMRESSolver *gmres;
308 HypreParVector *sv;
309
310 ScreenedPoisson* sint;
311};
312
313} // namespace common
314
315} // namespace mfem
316
317#endif // MFEM_USE_MPI
318#endif
Base class Coefficients that optionally depend on space and time. These are used by the BilinearFormI...
virtual real_t Eval(ElementTransformation &T, const IntegrationPoint &ip)=0
Evaluate the coefficient in the element described by T at the point ip.
Data type dense matrix using column-major storage.
Definition densemat.hpp:24
void SetIntPoint(const IntegrationPoint *ip)
Set the integration point ip that weights and Jacobians will be evaluated at.
Definition eltrans.hpp:106
Abstract class for all finite elements.
Definition fe_base.hpp:244
GMRES method.
Definition solvers.hpp:572
Coefficient defined by a GridFunction. This coefficient is mesh dependent.
Class for grid function - Vector with associated FE space.
Definition gridfunc.hpp:31
Arbitrary order H1-conforming (continuous) finite elements.
Definition fe_coll.hpp:275
The BoomerAMG solver in hypre.
Definition hypre.hpp:1717
void SetPrintLevel(int print_level)
Definition hypre.hpp:1797
Wrapper for hypre's parallel vector class.
Definition hypre.hpp:219
Class for integration point with weight.
Definition intrules.hpp:35
void SetRelTol(real_t rtol)
Definition solvers.hpp:229
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
Definition solvers.cpp:174
virtual void SetPrintLevel(int print_lvl)
Legacy method to set the level of verbosity of the solver output.
Definition solvers.cpp:72
void SetMaxIter(int max_it)
Definition solvers.hpp:231
void SetAbsTol(real_t atol)
Definition solvers.hpp:230
This class is used to express the local action of a general nonlinear finite element operator....
Abstract parallel finite element space.
Definition pfespace.hpp:29
HypreParVector * NewTrueDofVector()
Definition pfespace.hpp:398
Class for parallel grid function.
Definition pgridfunc.hpp:50
Class for parallel meshes.
Definition pmesh.hpp:34
MPI_Comm GetComm() const
Definition pmesh.hpp:402
Parallel non-linear operator on the true dofs.
Base class for vector Coefficients that optionally depend on time and space.
virtual void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)=0
Evaluate the vector coefficient in the element described by T at the point ip, storing the result in ...
Vector data type.
Definition vector.hpp:82
real_t Norml2() const
Returns the l2 norm of the vector.
Definition vector.cpp:931
IterativeSolver::PrintLevel print_level
virtual void ComputeScalarDistance(Coefficient &zero_level_set, ParGridFunction &distance)=0
void ScalarDistToVector(ParGridFunction &dist_s, ParGridFunction &dist_v)
virtual void ComputeVectorDistance(Coefficient &zero_level_set, ParGridFunction &distance)
HeatDistanceSolver(real_t diff_coeff)
void ComputeScalarDistance(Coefficient &zero_level_set, ParGridFunction &distance)
void ComputeScalarDistance(Coefficient &u_coeff, ParGridFunction &dist)
NormalizedGradCoefficient(const GridFunction &u_gf, int dim)
void Eval(Vector &V, ElementTransformation &T, const IntegrationPoint &ip)
Evaluate the vector coefficient in the element described by T at the point ip, storing the result in ...
void Filter(ParGridFunction &func, ParGridFunction &ffield)
PDEFilter(ParMesh &mesh, real_t rh, int order=2, int maxiter=100, real_t rtol=1e-12, real_t atol=1e-15, int print_lv=0)
void ComputeScalarDistance(Coefficient &func, ParGridFunction &fdist)
PLapDistanceSolver(int maxp_=30, int newton_iter_=10, real_t rtol=1e-7, real_t atol=1e-12)
real_t Eval(ElementTransformation &T, const IntegrationPoint &ip) override
Evaluate the coefficient in the element described by T at the point ip.
PProductCoefficient(Coefficient &basec_, Coefficient &corrc_)
void AssembleElementGrad(const FiniteElement &el, ElementTransformation &trans, const Vector &elfun, DenseMatrix &elmat) override
Assemble the local gradient matrix.
real_t GetElementEnergy(const FiniteElement &el, ElementTransformation &trans, const Vector &elfun) override
Compute the local energy.
VectorCoefficient * fgrad
PUMPLaplacian(Coefficient *nfunc, VectorCoefficient *nfgrad, bool ownership_=true)
void AssembleElementVector(const FiniteElement &el, ElementTransformation &trans, const Vector &elfun, Vector &elvect) override
Perform the local action of the NonlinearFormIntegrator.
void AssembleElementGrad(const FiniteElement &el, ElementTransformation &trans, const Vector &elfun, DenseMatrix &elmat) override
Assemble the local gradient matrix.
ScreenedPoisson(Coefficient &nfunc, real_t rh)
void SetInput(Coefficient &nfunc)
real_t GetElementEnergy(const FiniteElement &el, ElementTransformation &trans, const Vector &elfun) override
Compute the local energy.
void AssembleElementVector(const FiniteElement &el, ElementTransformation &trans, const Vector &elfun, Vector &elvect) override
Perform the local action of the NonlinearFormIntegrator.
int dim
Definition ex24.cpp:53
void trans(const Vector &u, Vector &x)
Definition ex27.cpp:412
real_t AvgElementSize(ParMesh &pmesh)
real_t u(const Vector &xvec)
Definition lor_mms.hpp:22
float real_t
Definition config.hpp:43
Settings for the output behavior of the IterativeSolver.
Definition solvers.hpp:98