MFEM  v4.5.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
tmop_tools.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2022, 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 
20 namespace mfem
21 {
22 
23 // Performs the full remap advection loop.
25 {
26 private:
27  RK4Solver ode_solver;
28  Vector nodes0;
29  Vector field0;
30  const double dt_scale;
31  const AssemblyLevel al;
33 
34  void ComputeAtNewPositionScalar(const Vector &new_nodes, Vector &new_field);
35 public:
37  double 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 {
57 private:
58  Vector nodes0;
59  GridFunction field0_gf;
60  FindPointsGSLIB *finder;
61 public:
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  finder->FreeData();
74  delete finder;
75  }
76 };
77 #endif
78 
79 /// Performs a single remap advection step in serial.
81 {
82 protected:
83  const Vector &x0;
87  mutable BilinearForm M, K;
89 
90 public:
91  /** Here @a fes is the FESpace of the function that will be moved. Note
92  that Mult() moves the nodes of the mesh corresponding to @a fes. */
94  FiniteElementSpace &fes,
96 
97  virtual void Mult(const Vector &ind, Vector &di_dt) const;
98 };
99 
100 #ifdef MFEM_USE_MPI
101 /// Performs a single remap advection step in parallel.
103 {
104 protected:
105  const Vector &x0;
109  mutable ParBilinearForm M, K;
111 
112 public:
113  /** Here @a pfes is the ParFESpace of the function that will be moved. Note
114  that Mult() moves the nodes of the mesh corresponding to @a pfes.
115  @a mt is used to set the memory type of the integrators. */
116  ParAdvectorCGOper(const Vector &x_start, GridFunction &vel,
117  ParFiniteElementSpace &pfes,
120 
121  virtual void Mult(const Vector &ind, Vector &di_dt) const;
122 };
123 #endif
124 
126 {
127 protected:
128  // 0 - Newton, 1 - LBFGS.
130  bool parallel;
131 
132  // Surface fitting variables.
133  bool adaptive_surf_fit = false;
134  mutable double surf_fit_err_avg_prvs = 10000.0;
135  mutable bool update_surf_fit_coeff = false;
136  double surf_fit_max_threshold = -1.0;
137 
138  // Minimum determinant over the whole mesh. Used for mesh untangling.
139  double *min_det_ptr = nullptr;
140  // Flag to compute minimum determinant and maximum metric in ProcessNewState,
141  // which is required for TMOP_WorstCaseUntangleOptimizer_Metric.
142  mutable bool compute_metric_quantile_flag = true;
143 
144  // Quadrature points that are checked for negative Jacobians etc.
146  // These fields are relevant for mixed meshes.
149 
151 
153  {
154  if (IntegRules)
155  {
156  return IntegRules->Get(el.GetGeomType(), integ_order);
157  }
158  return ir;
159  }
160 
161  void UpdateDiscreteTC(const TMOP_Integrator &ti, const Vector &x_new,
162  int x_ordering = Ordering::byNODES) const;
163 
164  double ComputeMinDet(const Vector &x_loc,
165  const FiniteElementSpace &fes) const;
166 
167  double MinDetJpr_2D(const FiniteElementSpace*, const Vector&) const;
168  double MinDetJpr_3D(const FiniteElementSpace*, const Vector&) const;
169 
170  /** @name Methods for adaptive surface fitting weight. */
171  ///@{
172  /// Get the average and maximum surface fitting error at the marked nodes.
173  /// If there is more than 1 TMOP integrator, we get the maximum of the
174  /// average and maximum error over all integrators.
175  virtual void GetSurfaceFittingError(double &err_avg, double &err_max) const;
176 
177  /// Update surface fitting weight as surf_fit_weight *= factor.
178  void UpdateSurfaceFittingWeight(double factor) const;
179 
180  /// Get the surface fitting weight for all the TMOP integrators.
181  void GetSurfaceFittingWeight(Array<double> &weights) const;
182  ///@}
183 
184 public:
185 #ifdef MFEM_USE_MPI
186  TMOPNewtonSolver(MPI_Comm comm, const IntegrationRule &irule, int type = 0)
187  : LBFGSSolver(comm), solver_type(type), parallel(true),
188  ir(irule), IntegRules(NULL), integ_order(-1) { }
189 #endif
190  TMOPNewtonSolver(const IntegrationRule &irule, int type = 0)
191  : LBFGSSolver(), solver_type(type), parallel(false),
192  ir(irule), IntegRules(NULL), integ_order(-1) { }
193 
194  /// Prescribe a set of integration rules; relevant for mixed meshes.
195  /** If called, this function has priority over the IntegrationRule given to
196  the constructor of the class. */
197  void SetIntegrationRules(IntegrationRules &irules, int order)
198  {
199  IntegRules = &irules;
200  integ_order = order;
201  }
202 
203  void SetMinDetPtr(double *md_ptr) { min_det_ptr = md_ptr; }
204 
205  /// Set the memory type for temporary memory allocations.
207 
208  /// Compute scaling factor for the node movement direction using line-search.
209  /// We impose constraints on TMOP energy, gradient, minimum Jacobian of
210  /// the mesh, and (optionally) on the surface fitting error.
211  virtual double ComputeScalingFactor(const Vector &x, const Vector &b) const;
212 
213  /// Update (i) discrete functions at new nodal positions, and
214  /// (ii) surface fitting weight.
215  virtual void ProcessNewState(const Vector &x) const;
216 
217  /** @name Methods for adaptive surface fitting weight. (Experimental) */
218  /// Enable adaptive surface fitting weight.
219  /// The weight is modified after each TMOPNewtonSolver iteration.
221 
222  /// Set the termination criterion for mesh optimization based on
223  /// the maximum surface fitting error.
225  {
226  surf_fit_max_threshold = max_error;
227  }
228 
229  virtual void Mult(const Vector &b, Vector &x) const
230  {
231  if (solver_type == 0)
232  {
233  NewtonSolver::Mult(b, x);
234  }
235  else if (solver_type == 1)
236  {
237  LBFGSSolver::Mult(b, x);
238  }
239  else { MFEM_ABORT("Invalid type"); }
240  }
241 
242  virtual void SetSolver(Solver &solver)
243  {
244  if (solver_type == 0)
245  {
246  NewtonSolver::SetSolver(solver);
247  }
248  else if (solver_type == 1)
249  {
250  LBFGSSolver::SetSolver(solver);
251  }
252  else { MFEM_ABORT("Invalid type"); }
253  }
254  virtual void SetPreconditioner(Solver &pr) { SetSolver(pr); }
255 };
256 
257 void vis_tmop_metric_s(int order, TMOP_QualityMetric &qm,
258  const TargetConstructor &tc, Mesh &pmesh,
259  char *title, int position);
260 #ifdef MFEM_USE_MPI
261 void vis_tmop_metric_p(int order, TMOP_QualityMetric &qm,
262  const TargetConstructor &tc, ParMesh &pmesh,
263  char *title, int position);
264 #endif
265 
266 }
267 
268 #endif
Abstract class for all finite elements.
Definition: fe_base.hpp:235
VectorGridFunctionCoefficient u_coeff
Definition: tmop_tools.hpp:86
void vis_tmop_metric_p(int order, TMOP_QualityMetric &qm, const TargetConstructor &tc, ParMesh &pmesh, char *title, int position)
Definition: tmop_tools.cpp:899
void UpdateDiscreteTC(const TMOP_Integrator &ti, const Vector &x_new, int x_ordering=Ordering::byNODES) const
Definition: tmop_tools.cpp:828
AssemblyLevel
Enumeration defining the assembly level for bilinear and nonlinear form classes derived from Operator...
Class for an integration rule - an Array of IntegrationPoint.
Definition: intrules.hpp:90
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:30
virtual void ComputeAtNewPosition(const Vector &new_nodes, Vector &new_field, int new_nodes_ordering=Ordering::byNODES)
Definition: tmop_tools.cpp:370
const IntegrationRule & Get(int GeomType, int Order)
Returns an integration rule for given GeomType and Order.
Definition: intrules.cpp:923
void SetIntegrationRules(IntegrationRules &irules, int order)
Prescribe a set of integration rules; relevant for mixed meshes.
Definition: tmop_tools.hpp:197
const IntegrationRule & ir
Definition: tmop_tools.hpp:145
double MinDetJpr_2D(const FiniteElementSpace *, const Vector &) const
Definition: tmop_pa_jp2.cpp:75
Base abstract class for first order time dependent operators.
Definition: operator.hpp:289
Container class for integration rules.
Definition: intrules.hpp:311
const IntegrationRule & GetIntegrationRule(const FiniteElement &el) const
Definition: tmop_tools.hpp:152
virtual void SetSolver(Solver &solver)
Set the linear solver for inverting the Jacobian.
Definition: solvers.hpp:661
Abstract parallel finite element space.
Definition: pfespace.hpp:28
void vis_tmop_metric_s(int order, TMOP_QualityMetric &qm, const TargetConstructor &tc, Mesh &mesh, char *title, int position)
Definition: tmop_tools.cpp:927
void SetMinDetPtr(double *md_ptr)
Definition: tmop_tools.hpp:203
virtual void ComputeAtNewPosition(const Vector &new_nodes, Vector &new_field, int new_nodes_ordering=Ordering::byNODES)
Definition: tmop_tools.cpp:29
void SetTerminationWithMaxSurfaceFittingError(double max_error)
Definition: tmop_tools.hpp:224
Geometry::Type GetGeomType() const
Returns the Geometry::Type of the reference element.
Definition: fe_base.hpp:320
void GetSurfaceFittingWeight(Array< double > &weights) const
Get the surface fitting weight for all the TMOP integrators.
Definition: tmop_tools.cpp:608
virtual void FreeData()
Definition: gslib.cpp:265
double b
Definition: lissajous.cpp:42
const AssemblyLevel al
Definition: tmop_tools.hpp:88
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...
Definition: tmop_tools.cpp:214
Performs a single remap advection step in serial.
Definition: tmop_tools.hpp:80
void UpdateSurfaceFittingWeight(double factor) const
Update surface fitting weight as surf_fit_weight *= factor.
Definition: tmop_tools.cpp:583
void EnableAdaptiveSurfaceFitting()
Definition: tmop_tools.hpp:220
TMOPNewtonSolver(const IntegrationRule &irule, int type=0)
Definition: tmop_tools.hpp:190
virtual void Mult(const Vector &b, Vector &x) const
Solve the nonlinear system with right-hand side b.
Definition: solvers.cpp:1814
IntegrationRules * IntegRules
Definition: tmop_tools.hpp:147
virtual void ProcessNewState(const Vector &x) const
Definition: tmop_tools.cpp:678
The classical explicit forth-order Runge-Kutta method, RK4.
Definition: ode.hpp:162
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
Definition: tmop_tools.hpp:254
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:96
virtual void Mult(const Vector &b, Vector &x) const
Solve the nonlinear system with right-hand side b.
Definition: solvers.cpp:2002
AdvectorCG(AssemblyLevel al=AssemblyLevel::LEGACY, double timestep_scale=0.5)
Definition: tmop_tools.hpp:36
const AssemblyLevel al
Definition: tmop_tools.hpp:110
MemoryType
Memory types supported by MFEM.
Definition: mem_manager.hpp:31
TMOPNewtonSolver(MPI_Comm comm, const IntegrationRule &irule, int type=0)
Definition: tmop_tools.hpp:186
Performs a single remap advection step in parallel.
Definition: tmop_tools.hpp:102
virtual void SetSolver(Solver &solver)
Set the linear solver for inverting the Jacobian.
Definition: solvers.hpp:752
A &quot;square matrix&quot; operator for the associated FE space and BLFIntegrators The sum of all the BLFInteg...
void SetMemoryType(MemoryType mt)
Definition: tmop_tools.hpp:51
double MinDetJpr_3D(const FiniteElementSpace *, const Vector &) const
Definition: tmop_pa_jp3.cpp:77
virtual void SetInitialField(const Vector &init_nodes, const Vector &init_field)
Definition: tmop_tools.cpp:22
VectorGridFunctionCoefficient u_coeff
Definition: tmop_tools.hpp:108
virtual void GetSurfaceFittingError(double &err_avg, double &err_max) const
Definition: tmop_tools.cpp:638
SerialAdvectorCGOper(const Vector &x_start, GridFunction &vel, FiniteElementSpace &fes, AssemblyLevel al=AssemblyLevel::LEGACY)
Definition: tmop_tools.cpp:193
void SetTempMemoryType(MemoryType mt)
Set the memory type for temporary memory allocations.
Definition: tmop_tools.hpp:206
Class for parallel bilinear form.
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...
Definition: tmop_tools.cpp:283
virtual void Mult(const Vector &b, Vector &x) const
Solve the nonlinear system with right-hand side b.
Definition: tmop_tools.hpp:229
ParAdvectorCGOper(const Vector &x_start, GridFunction &vel, ParFiniteElementSpace &pfes, AssemblyLevel al=AssemblyLevel::LEGACY, MemoryType mt=MemoryType::DEFAULT)
Definition: tmop_tools.cpp:253
FindPointsGSLIB can robustly evaluate a GridFunction on an arbitrary collection of points...
Definition: gslib.hpp:47
Vector data type.
Definition: vector.hpp:60
virtual double ComputeScalingFactor(const Vector &x, const Vector &b) const
Definition: tmop_tools.cpp:379
Vector coefficient defined by a vector GridFunction.
Base class for solvers.
Definition: operator.hpp:655
double ComputeMinDet(const Vector &x_loc, const FiniteElementSpace &fes) const
Definition: tmop_tools.cpp:846
virtual void SetSolver(Solver &solver)
Set the linear solver for inverting the Jacobian.
Definition: tmop_tools.hpp:242
virtual void SetInitialField(const Vector &init_nodes, const Vector &init_field)
Definition: tmop_tools.cpp:333
A TMOP integrator class based on any given TMOP_QualityMetric and TargetConstructor.
Definition: tmop.hpp:1565