MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
tmop_tools.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, 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_TMOP_TOOLS_HPP
13#define MFEM_TMOP_TOOLS_HPP
14
15#include "bilinearform.hpp"
16#include "pbilinearform.hpp"
17#include "tmop.hpp"
18#include "gslib.hpp"
19
20namespace mfem
21{
22
23// Performs the full remap advection loop.
25{
26private:
27 RK4Solver ode_solver;
28 Vector nodes0;
29 Vector field0;
30 const real_t dt_scale;
31 const AssemblyLevel al;
33
34 void ComputeAtNewPositionScalar(const Vector &new_nodes, Vector &new_field);
35public:
37 real_t timestep_scale = 0.5)
39 ode_solver(), nodes0(), field0(), dt_scale(timestep_scale), al(al) { }
40
41 virtual void SetInitialField(const Vector &init_nodes,
42 const Vector &init_field);
43
44 virtual void ComputeAtNewPosition(const Vector &new_nodes,
45 Vector &new_field,
46 int new_nodes_ordering = Ordering::byNODES);
47
48 /// Set the memory type used for large memory allocations. This memory type
49 /// is used when constructing the AdvectorCGOper but currently only for the
50 /// parallel variant.
51 void SetMemoryType(MemoryType mt) { opt_mt = mt; }
52};
53
54#ifdef MFEM_USE_GSLIB
56{
57private:
58 Vector nodes0;
59 GridFunction field0_gf;
60 FindPointsGSLIB *finder;
61public:
62 InterpolatorFP() : finder(NULL) { }
63
64 virtual void SetInitialField(const Vector &init_nodes,
65 const Vector &init_field);
66
67 virtual void ComputeAtNewPosition(const Vector &new_nodes,
68 Vector &new_field,
69 int new_nodes_ordering = Ordering::byNODES);
70
72 {
73 return finder;
74 }
75
77 {
78 finder->FreeData();
79 delete finder;
80 }
81};
82#endif
83
84/// Performs a single remap advection step in serial.
86{
87protected:
88 const Vector &x0;
92 mutable BilinearForm M, K;
94
95public:
96 /** Here @a fes is the FESpace of the function that will be moved. Note
97 that Mult() moves the nodes of the mesh corresponding to @a fes. */
101
102 virtual void Mult(const Vector &ind, Vector &di_dt) const;
103};
104
105#ifdef MFEM_USE_MPI
106/// Performs a single remap advection step in parallel.
108{
109protected:
110 const Vector &x0;
116
117public:
118 /** Here @a pfes is the ParFESpace of the function that will be moved. Note
119 that Mult() moves the nodes of the mesh corresponding to @a pfes.
120 @a mt is used to set the memory type of the integrators. */
121 ParAdvectorCGOper(const Vector &x_start, GridFunction &vel,
125
126 virtual void Mult(const Vector &ind, Vector &di_dt) const;
127};
128#endif
129
131{
132protected:
133 // 0 - Newton, 1 - LBFGS.
136
137 // Line search step is rejected if min(detJ) <= min_detJ_threshold.
139
140 // Surface fitting variables.
141 mutable real_t surf_fit_err_avg_prvs = 10000.0;
143 mutable bool update_surf_fit_coeff = false;
147 mutable int adapt_inc_count = 0;
148 mutable int max_adapt_inc_count = 10;
149
150 // Minimum determinant over the whole mesh. Used for mesh untangling.
151 real_t *min_det_ptr = nullptr;
152 // Flag to compute minimum determinant and maximum metric in ProcessNewState,
153 // which is required for TMOP_WorstCaseUntangleOptimizer_Metric.
154 mutable bool compute_metric_quantile_flag = true;
155
156 // Quadrature points that are checked for negative Jacobians etc.
158 // These fields are relevant for mixed meshes.
161
163
165 {
166 if (IntegRules)
167 {
168 return IntegRules->Get(el.GetGeomType(), integ_order);
169 }
170 return ir;
171 }
172
173 real_t ComputeMinDet(const Vector &x_loc,
174 const FiniteElementSpace &fes) const;
175
176 real_t MinDetJpr_2D(const FiniteElementSpace*, const Vector&) const;
177 real_t MinDetJpr_3D(const FiniteElementSpace*, const Vector&) const;
178
179 /** @name Methods for adaptive surface fitting weight. */
180 ///@{
181 /// Get the average and maximum surface fitting error at the marked nodes.
182 /// If there is more than 1 TMOP integrator, we get the maximum of the
183 /// average and maximum error over all integrators.
184 virtual void GetSurfaceFittingError(const Vector &x_loc,
185 real_t &err_avg, real_t &err_max) const;
186
187 /// Update surface fitting weight as surf_fit_weight *= factor.
188 void UpdateSurfaceFittingWeight(real_t factor) const;
189
190 /// Get the surface fitting weight for all the TMOP integrators.
191 void GetSurfaceFittingWeight(Array<real_t> &weights) const;
192 ///@}
193
194public:
195#ifdef MFEM_USE_MPI
196 TMOPNewtonSolver(MPI_Comm comm, const IntegrationRule &irule, int type = 0)
197 : LBFGSSolver(comm), solver_type(type), parallel(true),
198 ir(irule), IntegRules(NULL), integ_order(-1) { }
199#endif
200 TMOPNewtonSolver(const IntegrationRule &irule, int type = 0)
201 : LBFGSSolver(), solver_type(type), parallel(false),
202 ir(irule), IntegRules(NULL), integ_order(-1) { }
203
204 /// Prescribe a set of integration rules; relevant for mixed meshes.
205 /** If called, this function has priority over the IntegrationRule given to
206 the constructor of the class. */
207 void SetIntegrationRules(IntegrationRules &irules, int order)
208 {
209 IntegRules = &irules;
210 integ_order = order;
211 }
212
213 void SetMinDetPtr(real_t *md_ptr) { min_det_ptr = md_ptr; }
214
215 /// Set the memory type for temporary memory allocations.
217
218 /// Compute scaling factor for the node movement direction using line-search.
219 /// We impose constraints on TMOP energy, gradient, minimum Jacobian of
220 /// the mesh, and (optionally) on the surface fitting error.
221 virtual real_t ComputeScalingFactor(const Vector &x, const Vector &b) const;
222
223 /// Update (i) discrete functions at new nodal positions, and
224 /// (ii) surface fitting weight.
225 virtual void ProcessNewState(const Vector &x) const;
226
227 /** @name Methods for adaptive surface fitting weight. (Experimental) */
228 /// Enable/Disable adaptive surface fitting weight.
229 /// The weight is modified after each TMOPNewtonSolver iteration as:
230 /// w_{k+1} = w_{k} * @a surf_fit_scale_factor if relative change in
231 /// max surface fitting error < @a surf_fit_rel_change_threshold.
232 /// The solver terminates if the maximum surface fitting error does
233 /// not sufficiently decrease for @a max_adapt_inc_count consecutive
234 /// solver iterations or if the max error falls below @a surf_fit_max_threshold.
249 {
250 max_adapt_inc_count = count;
251 }
253 {
254 surf_fit_max_threshold = max_error;
255 }
257 {
258 min_detJ_threshold = threshold;
259 }
260
261 virtual void Mult(const Vector &b, Vector &x) const
262 {
263 if (solver_type == 0)
264 {
266 }
267 else if (solver_type == 1)
268 {
270 }
271 else { MFEM_ABORT("Invalid type"); }
272 }
273
274 virtual void SetSolver(Solver &solver)
275 {
276 if (solver_type == 0)
277 {
279 }
280 else if (solver_type == 1)
281 {
283 }
284 else { MFEM_ABORT("Invalid type"); }
285 }
286 virtual void SetPreconditioner(Solver &pr) { SetSolver(pr); }
287};
288
289void vis_tmop_metric_s(int order, TMOP_QualityMetric &qm,
290 const TargetConstructor &tc, Mesh &pmesh,
291 char *title, int position);
292#ifdef MFEM_USE_MPI
293void vis_tmop_metric_p(int order, TMOP_QualityMetric &qm,
294 const TargetConstructor &tc, ParMesh &pmesh,
295 char *title, int position);
296#endif
297
298}
299
300#endif
virtual void ComputeAtNewPosition(const Vector &new_nodes, Vector &new_field, int new_nodes_ordering=Ordering::byNODES)
AdvectorCG(AssemblyLevel al=AssemblyLevel::LEGACY, real_t timestep_scale=0.5)
virtual void SetInitialField(const Vector &init_nodes, const Vector &init_field)
void SetMemoryType(MemoryType mt)
A "square matrix" operator for the associated FE space and BLFIntegrators The sum of all the BLFInteg...
FindPointsGSLIB can robustly evaluate a GridFunction on an arbitrary collection of points....
Definition gslib.hpp:53
virtual void FreeData()
Definition gslib.cpp:283
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition fespace.hpp:220
Abstract class for all finite elements.
Definition fe_base.hpp:239
Geometry::Type GetGeomType() const
Returns the Geometry::Type of the reference element.
Definition fe_base.hpp:326
Class for grid function - Vector with associated FE space.
Definition gridfunc.hpp:31
Class for an integration rule - an Array of IntegrationPoint.
Definition intrules.hpp:100
Container class for integration rules.
Definition intrules.hpp:416
const IntegrationRule & Get(int GeomType, int Order)
Returns an integration rule for given GeomType and Order.
virtual void ComputeAtNewPosition(const Vector &new_nodes, Vector &new_field, int new_nodes_ordering=Ordering::byNODES)
virtual void SetInitialField(const Vector &init_nodes, const Vector &init_field)
const FindPointsGSLIB * GetFindPointsGSLIB() const
virtual void SetSolver(Solver &solver)
Set the linear solver for inverting the Jacobian.
Definition solvers.hpp:805
virtual void Mult(const Vector &b, Vector &x) const
Solve the nonlinear system with right-hand side b.
Definition solvers.cpp:2009
virtual void SetSolver(Solver &solver)
Set the linear solver for inverting the Jacobian.
Definition solvers.hpp:714
virtual void Mult(const Vector &b, Vector &x) const
Solve the nonlinear system with right-hand side b.
Definition solvers.cpp:1821
Performs a single remap advection step in parallel.
ParAdvectorCGOper(const Vector &x_start, GridFunction &vel, ParFiniteElementSpace &pfes, AssemblyLevel al=AssemblyLevel::LEGACY, MemoryType mt=MemoryType::DEFAULT)
VectorGridFunctionCoefficient u_coeff
virtual void Mult(const Vector &ind, Vector &di_dt) const
Perform the action of the operator: y = k = f(x, t), where k solves the algebraic equation F(x,...
const AssemblyLevel al
Class for parallel bilinear form.
Abstract parallel finite element space.
Definition pfespace.hpp:29
The classical explicit forth-order Runge-Kutta method, RK4.
Definition ode.hpp:164
Performs a single remap advection step in serial.
VectorGridFunctionCoefficient u_coeff
SerialAdvectorCGOper(const Vector &x_start, GridFunction &vel, FiniteElementSpace &fes, AssemblyLevel al=AssemblyLevel::LEGACY)
const AssemblyLevel al
virtual void Mult(const Vector &ind, Vector &di_dt) const
Perform the action of the operator: y = k = f(x, t), where k solves the algebraic equation F(x,...
Base class for solvers.
Definition operator.hpp:683
virtual void ProcessNewState(const Vector &x) const
void SetAdaptiveSurfaceFittingScalingFactor(real_t factor)
virtual real_t ComputeScalingFactor(const Vector &x, const Vector &b) const
void SetMinimumDeterminantThreshold(real_t threshold)
virtual void GetSurfaceFittingError(const Vector &x_loc, real_t &err_avg, real_t &err_max) const
void SetTempMemoryType(MemoryType mt)
Set the memory type for temporary memory allocations.
real_t MinDetJpr_3D(const FiniteElementSpace *, const Vector &) const
const IntegrationRule & ir
virtual void SetSolver(Solver &solver)
Set the linear solver for inverting the Jacobian.
IntegrationRules * IntegRules
void SetIntegrationRules(IntegrationRules &irules, int order)
Prescribe a set of integration rules; relevant for mixed meshes.
virtual void Mult(const Vector &b, Vector &x) const
Solve the nonlinear system with right-hand side b.
const IntegrationRule & GetIntegrationRule(const FiniteElement &el) const
void UpdateSurfaceFittingWeight(real_t factor) const
Update surface fitting weight as surf_fit_weight *= factor.
real_t MinDetJpr_2D(const FiniteElementSpace *, const Vector &) const
void SetAdaptiveSurfaceFittingRelativeChangeThreshold(real_t threshold)
void SetMaxNumberofIncrementsForAdaptiveFitting(int count)
void SetMinDetPtr(real_t *md_ptr)
TMOPNewtonSolver(const IntegrationRule &irule, int type=0)
void SetTerminationWithMaxSurfaceFittingError(real_t max_error)
real_t ComputeMinDet(const Vector &x_loc, const FiniteElementSpace &fes) const
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
void GetSurfaceFittingWeight(Array< real_t > &weights) const
Get the surface fitting weight for all the TMOP integrators.
TMOPNewtonSolver(MPI_Comm comm, const IntegrationRule &irule, int type=0)
Base abstract class for first order time dependent operators.
Definition operator.hpp:317
Vector coefficient defined by a vector GridFunction.
Vector data type.
Definition vector.hpp:80
real_t b
Definition lissajous.cpp:42
AssemblyLevel
Enumeration defining the assembly level for bilinear and nonlinear form classes derived from Operator...
void vis_tmop_metric_s(int order, TMOP_QualityMetric &qm, const TargetConstructor &tc, Mesh &mesh, char *title, int position)
void vis_tmop_metric_p(int order, TMOP_QualityMetric &qm, const TargetConstructor &tc, ParMesh &pmesh, char *title, int position)
float real_t
Definition config.hpp:43
MemoryType
Memory types supported by MFEM.
void vel(const Vector &x, real_t t, Vector &u)