libMesh
fem_system_ex1.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 
20 // <h1>FEMSystem Example 1 - Unsteady Navier-Stokes Equations with
21 // FEMSystem</h1>
22 // \author Roy Stogner
23 // \date 2006
24 //
25 // This example shows how the transient nonlinear problem from
26 // example 13 can be solved using the
27 // DifferentiableSystem class framework
28 
29 // C++ includes
30 #include <iomanip>
31 
32 // Basic include files
33 #include "libmesh/equation_systems.h"
34 #include "libmesh/error_vector.h"
35 #include "libmesh/getpot.h"
36 #include "libmesh/exodusII_io.h"
37 #include "libmesh/kelly_error_estimator.h"
38 #include "libmesh/mesh.h"
39 #include "libmesh/replicated_mesh.h"
40 #include "libmesh/distributed_mesh.h"
41 #include "libmesh/mesh_generation.h"
42 #include "libmesh/mesh_refinement.h"
43 #include "libmesh/uniform_refinement_estimator.h"
44 #include "libmesh/auto_ptr.h" // libmesh_make_unique
45 #include "libmesh/enum_solver_package.h"
46 #include "libmesh/enum_norm_type.h"
47 
48 // The systems and solvers we may use
49 #include "naviersystem.h"
50 #include "libmesh/diff_solver.h"
51 #include "libmesh/petsc_diff_solver.h"
52 #include "libmesh/newton_solver.h"
53 #include "libmesh/euler_solver.h"
54 #include "libmesh/steady_solver.h"
55 #include "libmesh/partitioner.h"
56 
57 // Bring in everything from the libMesh namespace
58 using namespace libMesh;
59 
60 // The main program.
61 int main (int argc, char ** argv)
62 {
63  // Initialize libMesh.
64  LibMeshInit init (argc, argv);
65 
66  // This example requires a linear solver package.
67  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
68  "--enable-petsc, --enable-trilinos, or --enable-eigen");
69 
70  // This example fails without at least double precision FP
71 #ifdef LIBMESH_DEFAULT_SINGLE_PRECISION
72  libmesh_example_requires(false, "--disable-singleprecision");
73 #endif
74 
75 #ifndef LIBMESH_ENABLE_AMR
76  libmesh_example_requires(false, "--enable-amr");
77 #else
78 
79  // We use Dirichlet boundary conditions here
80 #ifndef LIBMESH_ENABLE_DIRICHLET
81  libmesh_example_requires(false, "--enable-dirichlet");
82 #endif
83 
84  // Trilinos and Eigen solvers NaN by default here.
85  // We'll skip this example for now.
86  libmesh_example_requires(libMesh::default_solver_package() != EIGEN_SOLVERS, "--enable-petsc");
87  libmesh_example_requires(libMesh::default_solver_package() != TRILINOS_SOLVERS, "--enable-petsc");
88 
89  // Parse the input file
90  GetPot infile("fem_system_ex1.in");
91 
92  // Override input file arguments from the command line
93  infile.parse_command_line(argc, argv);
94 
95  // Read in parameters from the input file
96  const Real global_tolerance = infile("global_tolerance", 0.);
97  const unsigned int nelem_target = infile("n_elements", 400);
98  const bool transient = infile("transient", true);
99  const Real deltat = infile("deltat", 0.005);
100  unsigned int n_timesteps = infile("n_timesteps", 20);
101  const unsigned int coarsegridsize = infile("coarsegridsize", 1);
102  const unsigned int coarserefinements = infile("coarserefinements", 0);
103  const unsigned int max_adaptivesteps = infile("max_adaptivesteps", 10);
104  const unsigned int dim = infile("dimension", 2);
105  const std::string slvr_type = infile("solver_type", "newton");
106  const std::string mesh_type = infile("mesh_type" , "replicated");
107 
108 #ifdef LIBMESH_HAVE_EXODUS_API
109  const unsigned int write_interval = infile("write_interval", 5);
110 #endif
111 
112  // Skip higher-dimensional examples on a lower-dimensional libMesh build
113  libmesh_example_requires(dim <= LIBMESH_DIM, "2D/3D support");
114 
115  // We have only defined 2 and 3 dimensional problems
116  libmesh_assert (dim == 2 || dim == 3);
117 
118  // Create a mesh, with dimension to be overridden later, distributed
119  // across the default MPI communicator.
120  std::shared_ptr<UnstructuredMesh> mesh;
121 
122  if (mesh_type == "distributed")
123  mesh = std::make_shared<DistributedMesh>(init.comm());
124  else if (mesh_type == "replicated")
125  mesh = std::make_shared<ReplicatedMesh>(init.comm());
126  else
127  libmesh_error_msg("Error: specified mesh_type not understood");
128 
129  // And an object to refine it
130  MeshRefinement mesh_refinement(*mesh);
131  mesh_refinement.coarsen_by_parents() = true;
132  mesh_refinement.absolute_global_tolerance() = global_tolerance;
133  mesh_refinement.nelem_target() = nelem_target;
134  mesh_refinement.refine_fraction() = 0.3;
135  mesh_refinement.coarsen_fraction() = 0.3;
136  mesh_refinement.coarsen_threshold() = 0.1;
137 
138  // Use the MeshTools::Generation mesh generator to create a uniform
139  // grid on the square [-1,1]^D. We instruct the mesh generator
140  // to build a mesh of 8x8 Quad9 elements in 2D, or Hex27
141  // elements in 3D. Building these higher-order elements allows
142  // us to use higher-order approximation, as in example 3.
143  if (dim == 2)
145  coarsegridsize,
146  coarsegridsize,
147  0., 1.,
148  0., 1.,
149  QUAD9);
150  else if (dim == 3)
152  coarsegridsize,
153  coarsegridsize,
154  coarsegridsize,
155  0., 1.,
156  0., 1.,
157  0., 1.,
158  HEX27);
159 
160  if (slvr_type == "petscdiff")
161  {
162  mesh->allow_renumbering(false);
164  mesh->partitioner() = nullptr;
165  }
166 
167  mesh_refinement.uniformly_refine(coarserefinements);
168 
169  // Print information about the mesh to the screen.
170  mesh->print_info();
171 
172  // Create an equation systems object.
173  EquationSystems equation_systems (*mesh);
174 
175  // Declare the system "Navier-Stokes" and its variables.
176  NavierSystem & system =
177  equation_systems.add_system<NavierSystem> ("Navier-Stokes");
178 
179  // Solve this as a time-dependent or steady system
180  if (transient)
181  system.time_solver = libmesh_make_unique<EulerSolver>(system);
182  else
183  {
184  system.time_solver = libmesh_make_unique<SteadySolver>(system);
185  libmesh_assert_equal_to (n_timesteps, 1);
186  }
187 
188  // Initialize the system
189  equation_systems.init ();
190 
191  // Set the time stepping options
192  system.deltat = deltat;
193 
194  // And the nonlinear solver options
195  if (slvr_type == "newton")
196  system.time_solver->diff_solver() = libmesh_make_unique<NewtonSolver>(system);
197  else if (slvr_type == "petscdiff")
198 #if defined(LIBMESH_HAVE_PETSC) && defined(LIBMESH_HAVE_METAPHYSICL)
199  system.time_solver->diff_solver() = libmesh_make_unique<PetscDiffSolver>(system);
200 #else
201  libmesh_example_requires(false, "--enable-petsc --enable-metaphysicl-required");
202 #endif
203  else
204  libmesh_error_msg("Error: specified solver_type not understood");
205 
206  DiffSolver & solver = *(system.time_solver->diff_solver().get());
207  solver.init();
208 
209  solver.quiet = infile("solver_quiet", true);
210  solver.verbose = !solver.quiet;
211  solver.max_nonlinear_iterations =
212  infile("max_nonlinear_iterations", 15);
213  solver.relative_step_tolerance =
214  infile("relative_step_tolerance", 1.e-3);
216  infile("relative_residual_tolerance", 0.0);
218  infile("absolute_residual_tolerance", 0.0);
219 
220  // And the linear solver options
221  solver.max_linear_iterations =
222  infile("max_linear_iterations", 50000);
223  solver.initial_linear_tolerance =
224  infile("initial_linear_tolerance", 1.e-3);
225 
226  // Print information about the system to the screen.
227  equation_systems.print_info();
228 
229  // Now we begin the timestep loop to compute the time-accurate
230  // solution of the equations.
231  for (unsigned int t_step=0; t_step != n_timesteps; ++t_step)
232  {
233  // A pretty update message
234  libMesh::out << "\n\nSolving time step "
235  << t_step
236  << ", time = "
237  << system.time
238  << std::endl;
239 
240  // Adaptively solve the timestep
241  unsigned int a_step = 0;
242  for (; a_step != max_adaptivesteps; ++a_step)
243  {
244  system.solve();
245 
246  system.postprocess();
247 
248  ErrorVector error;
249 
250  std::unique_ptr<ErrorEstimator> error_estimator;
251 
252  // To solve to a tolerance in this problem we
253  // need a better estimator than Kelly
254  if (global_tolerance != 0.)
255  {
256  // We can't adapt to both a tolerance and a mesh
257  // size at once
258  libmesh_assert_equal_to (nelem_target, 0);
259 
260  error_estimator = libmesh_make_unique<UniformRefinementEstimator>();
261 
262  // The lid-driven cavity problem isn't in H1, so
263  // lets estimate L2 error
264  error_estimator->error_norm = L2;
265  }
266  else
267  {
268  // If we aren't adapting to a tolerance we need a
269  // target mesh size
270  libmesh_assert_greater (nelem_target, 0);
271 
272  // Kelly is a lousy estimator to use for a problem
273  // not in H1 - if we were doing more than a few
274  // timesteps we'd need to turn off or limit the
275  // maximum level of our adaptivity eventually
276  error_estimator = libmesh_make_unique<KellyErrorEstimator>();
277  }
278 
279  // Calculate error based on u and v (and w?) but not p
280  std::vector<Real> weights(2,1.0); // u, v
281  if (dim == 3)
282  weights.push_back(1.0); // w
283  weights.push_back(0.0); // p
284  // Keep the same default norm type.
285  std::vector<FEMNormType>
286  norms(1, error_estimator->error_norm.type(0));
287  error_estimator->error_norm = SystemNorm(norms, weights);
288 
289  error_estimator->estimate_error(system, error);
290 
291  // Print out status at each adaptive step.
292  Real global_error = error.l2_norm();
293  libMesh::out << "Adaptive step "
294  << a_step
295  << ": "
296  << std::endl;
297 
298  if (global_tolerance != 0.)
299  libMesh::out << "Global_error = "
300  << global_error
301  << std::endl;
302 
303  if (global_tolerance != 0.)
304  libMesh::out << "Worst element error = "
305  << error.maximum()
306  << ", mean = "
307  << error.mean()
308  << std::endl;
309 
310  if (global_tolerance != 0.)
311  {
312  // If we've reached our desired tolerance, we
313  // don't need any more adaptive steps
314  if (global_error < global_tolerance)
315  break;
316  mesh_refinement.flag_elements_by_error_tolerance(error);
317  }
318  else
319  {
320  // If flag_elements_by_nelem_target returns true, this
321  // should be our last adaptive step.
322  if (mesh_refinement.flag_elements_by_nelem_target(error))
323  {
324  mesh_refinement.refine_and_coarsen_elements();
325  equation_systems.reinit();
326  a_step = max_adaptivesteps;
327  break;
328  }
329  }
330 
331  // Carry out the adaptive mesh refinement/coarsening
332  mesh_refinement.refine_and_coarsen_elements();
333  equation_systems.reinit();
334 
335  libMesh::out << "Refined mesh to "
336  << mesh->n_active_elem()
337  << " active elements and "
338  << equation_systems.n_active_dofs()
339  << " active dofs."
340  << std::endl;
341  }
342  // Do one last solve if necessary
343  if (a_step == max_adaptivesteps)
344  {
345  system.solve();
346 
347  system.postprocess();
348  }
349 
350  // Advance to the next timestep in a transient problem
351  system.time_solver->advance_timestep();
352 
353 #ifdef LIBMESH_HAVE_EXODUS_API
354  // Write out this timestep if we're requested to
355  if ((t_step+1)%write_interval == 0)
356  {
357  std::ostringstream file_name;
358 
359  // We write the file in the ExodusII format.
360  file_name << "out_"
361  << std::setw(3)
362  << std::setfill('0')
363  << std::right
364  << t_step+1
365  << ".e";
366 
367  ExodusII_IO(*mesh).write_timestep(file_name.str(),
368  equation_systems,
369  1, // This number indicates how many time steps
370  // are being written to the file
371  system.time);
372  }
373 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
374  }
375 #endif // #ifndef LIBMESH_ENABLE_AMR
376 
377  // All done.
378  return 0;
379 }
libMesh::DifferentiableSystem::deltat
Real deltat
For time-dependent problems, this is the amount delta t to advance the solution in time.
Definition: diff_system.h:260
libMesh::MeshRefinement::refine_and_coarsen_elements
bool refine_and_coarsen_elements()
Refines and coarsens user-requested elements.
Definition: mesh_refinement.C:476
libMesh::FEMSystem::solve
virtual void solve() override
Invokes the solver associated with the system.
Definition: fem_system.C:1046
libMesh::MeshRefinement::coarsen_threshold
Real & coarsen_threshold()
The coarsen_threshold provides hysteresis in AMR/C strategies.
Definition: mesh_refinement.h:897
libMesh::EquationSystems::add_system
virtual System & add_system(const std::string &system_type, const std::string &name)
Add the system of type system_type named name to the systems array.
Definition: equation_systems.C:345
libMesh::StatisticsVector::l2_norm
virtual Real l2_norm() const
Definition: statistics.C:37
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::MeshTools::Generation::build_cube
void build_cube(UnstructuredMesh &mesh, const unsigned int nx=0, const unsigned int ny=0, const unsigned int nz=0, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const Real zmin=0., const Real zmax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
Builds a (elements) cube.
Definition: mesh_generation.C:298
libMesh::DiffSolver
This is a generic class that defines a solver to handle ImplicitSystem classes, including NonlinearIm...
Definition: diff_solver.h:70
libMesh::default_solver_package
SolverPackage default_solver_package()
Definition: libmesh.C:993
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::ExodusII_IO::write_timestep
void write_timestep(const std::string &fname, const EquationSystems &es, const int timestep, const Real time, const std::set< std::string > *system_names=nullptr)
Writes out the solution at a specific timestep.
Definition: exodusII_io.C:1286
libMesh::ExodusII_IO
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:51
libMesh::MeshRefinement::flag_elements_by_nelem_target
bool flag_elements_by_nelem_target(const ErrorVector &error_per_cell)
Flags elements for coarsening and refinement based on the computed error passed in error_per_cell.
Definition: mesh_refinement_flagging.C:237
libMesh::DiffSolver::init
virtual void init()
The initialization function.
Definition: diff_solver.C:69
dim
unsigned int dim
Definition: adaptivity_ex3.C:113
libMesh::DifferentiableSystem::time_solver
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we're going to use.
Definition: diff_system.h:233
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::MeshRefinement
Implements (adaptive) mesh refinement algorithms for a MeshBase.
Definition: mesh_refinement.h:61
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::MeshTools::Generation::build_square
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
Definition: mesh_generation.C:1501
libMesh::HEX27
Definition: enum_elem_type.h:49
naviersystem.h
libMesh::EquationSystems::n_active_dofs
std::size_t n_active_dofs() const
Definition: equation_systems.C:1362
main
int main(int argc, char **argv)
Definition: fem_system_ex1.C:61
NavierSystem::postprocess
virtual void postprocess()
Runs a postprocessing loop over all elements, and if postprocess_sides is true over all sides.
Definition: naviersystem.C:621
libMesh::DiffSolver::relative_step_tolerance
Real relative_step_tolerance
Definition: diff_solver.h:204
libMesh::DiffSolver::verbose
bool verbose
The DiffSolver may print a lot more to libMesh::out if verbose is set to true; default is false.
Definition: diff_solver.h:168
libMesh::MeshRefinement::flag_elements_by_error_tolerance
void flag_elements_by_error_tolerance(const ErrorVector &error_per_cell)
Flags elements for coarsening and refinement based on the computed error passed in error_per_cell.
Definition: mesh_refinement_flagging.C:170
libMesh::INVALID_SOLVER_PACKAGE
Definition: enum_solver_package.h:43
libMesh::EquationSystems::init
virtual void init()
Initialize all the systems.
Definition: equation_systems.C:96
libMesh::ErrorVector
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
Definition: error_vector.h:50
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
libMesh::MeshRefinement::nelem_target
dof_id_type & nelem_target()
If nelem_target is set to a nonzero value, methods like flag_elements_by_nelem_target() will attempt ...
Definition: mesh_refinement.h:903
libMesh::EquationSystems::print_info
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
Definition: equation_systems.C:1314
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::System::time
Real time
For time-dependent problems, this is the time t at the beginning of the current timestep.
Definition: system.h:1561
libMesh::DiffSolver::quiet
bool quiet
The DiffSolver should not print anything to libMesh::out unless quiet is set to false; default is tru...
Definition: diff_solver.h:162
libMesh::TRILINOS_SOLVERS
Definition: enum_solver_package.h:37
libMesh::EquationSystems::reinit
virtual void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
Definition: equation_systems.C:121
libMesh::MeshRefinement::coarsen_fraction
Real & coarsen_fraction()
The coarsen_fraction sets either a desired target or a desired maximum number of elements to flag for...
Definition: mesh_refinement.h:885
libMesh::DiffSolver::relative_residual_tolerance
Real relative_residual_tolerance
Definition: diff_solver.h:192
libMesh::MeshRefinement::refine_fraction
Real & refine_fraction()
The refine_fraction sets either a desired target or a desired maximum number of elements to flag for ...
Definition: mesh_refinement.h:879
libMesh::DiffSolver::absolute_residual_tolerance
Real absolute_residual_tolerance
The DiffSolver should exit after the residual is reduced to either less than absolute_residual_tolera...
Definition: diff_solver.h:191
libMesh::MeshBase::print_info
void print_info(std::ostream &os=libMesh::out) const
Prints relevant information about the mesh.
Definition: mesh_base.C:585
libMesh::ErrorVector::mean
virtual Real mean() const override
Definition: error_vector.C:69
libMesh::DiffSolver::max_linear_iterations
unsigned int max_linear_iterations
Each linear solver step should exit after max_linear_iterations is exceeded.
Definition: diff_solver.h:148
libMesh::MeshRefinement::absolute_global_tolerance
Real & absolute_global_tolerance()
If absolute_global_tolerance is set to a nonzero value, methods like flag_elements_by_global_toleranc...
Definition: mesh_refinement.h:909
libMesh::L2
Definition: enum_norm_type.h:36
libMesh::MeshRefinement::coarsen_by_parents
bool & coarsen_by_parents()
If coarsen_by_parents is true, complete groups of sibling elements (elements with the same parent) wi...
Definition: mesh_refinement.h:873
libMesh::SystemNorm
This class defines a norm/seminorm to be applied to a NumericVector which contains coefficients in a ...
Definition: system_norm.h:51
libMesh::MeshBase::allow_remote_element_removal
void allow_remote_element_removal(bool allow)
If false is passed in then this mesh will no longer have remote elements deleted when being prepared ...
Definition: mesh_base.h:1034
libMesh::QUAD9
Definition: enum_elem_type.h:43
libMesh::MeshBase::allow_renumbering
void allow_renumbering(bool allow)
If false is passed in then this mesh will no longer be renumbered when being prepared for use.
Definition: mesh_base.h:1025
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::DiffSolver::max_nonlinear_iterations
unsigned int max_nonlinear_iterations
The DiffSolver should exit in failure if max_nonlinear_iterations is exceeded and continue_after_max_...
Definition: diff_solver.h:156
NavierSystem
Definition: naviersystem.h:26
libMesh::out
OStreamProxy out
libMesh::MeshRefinement::uniformly_refine
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.
Definition: mesh_refinement.C:1678
libMesh::EIGEN_SOLVERS
Definition: enum_solver_package.h:40
libMesh::MeshBase::n_active_elem
virtual dof_id_type n_active_elem() const =0
libMesh::StatisticsVector::maximum
virtual T maximum() const
Definition: statistics.C:62
libMesh::DiffSolver::initial_linear_tolerance
double initial_linear_tolerance
Any required linear solves will at first be done with this tolerance; the DiffSolver may tighten the ...
Definition: diff_solver.h:210
libMesh::MeshBase::partitioner
virtual std::unique_ptr< Partitioner > & partitioner()
A partitioner to use at each prepare_for_use()
Definition: mesh_base.h:127