libMesh
Functions
fem_system_ex1.C File Reference

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 61 of file fem_system_ex1.C.

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 }

References libMesh::MeshRefinement::absolute_global_tolerance(), libMesh::DiffSolver::absolute_residual_tolerance, libMesh::EquationSystems::add_system(), libMesh::MeshBase::allow_remote_element_removal(), libMesh::MeshBase::allow_renumbering(), libMesh::MeshTools::Generation::build_cube(), libMesh::MeshTools::Generation::build_square(), libMesh::MeshRefinement::coarsen_by_parents(), libMesh::MeshRefinement::coarsen_fraction(), libMesh::MeshRefinement::coarsen_threshold(), libMesh::default_solver_package(), libMesh::DifferentiableSystem::deltat, dim, libMesh::EIGEN_SOLVERS, libMesh::MeshRefinement::flag_elements_by_error_tolerance(), libMesh::MeshRefinement::flag_elements_by_nelem_target(), libMesh::HEX27, libMesh::TriangleWrapper::init(), libMesh::DiffSolver::init(), libMesh::EquationSystems::init(), libMesh::DiffSolver::initial_linear_tolerance, libMesh::INVALID_SOLVER_PACKAGE, libMesh::L2, libMesh::StatisticsVector< T >::l2_norm(), libMesh::libmesh_assert(), libMesh::DiffSolver::max_linear_iterations, libMesh::DiffSolver::max_nonlinear_iterations, libMesh::StatisticsVector< T >::maximum(), libMesh::ErrorVector::mean(), mesh, libMesh::EquationSystems::n_active_dofs(), libMesh::MeshBase::n_active_elem(), libMesh::MeshRefinement::nelem_target(), libMesh::out, libMesh::MeshBase::partitioner(), NavierSystem::postprocess(), libMesh::EquationSystems::print_info(), libMesh::MeshBase::print_info(), libMesh::QUAD9, libMesh::DiffSolver::quiet, libMesh::Real, libMesh::MeshRefinement::refine_and_coarsen_elements(), libMesh::MeshRefinement::refine_fraction(), libMesh::EquationSystems::reinit(), libMesh::DiffSolver::relative_residual_tolerance, libMesh::DiffSolver::relative_step_tolerance, libMesh::FEMSystem::solve(), libMesh::System::time, libMesh::DifferentiableSystem::time_solver, libMesh::TRILINOS_SOLVERS, libMesh::MeshRefinement::uniformly_refine(), libMesh::DiffSolver::verbose, and libMesh::ExodusII_IO::write_timestep().

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::FEMSystem::solve
virtual void solve() override
Invokes the solver associated with the system.
Definition: fem_system.C:1046
libMesh::StatisticsVector::l2_norm
virtual Real l2_norm() const
Definition: statistics.C:37
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::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::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::INVALID_SOLVER_PACKAGE
Definition: enum_solver_package.h:43
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::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::DiffSolver::relative_residual_tolerance
Real relative_residual_tolerance
Definition: diff_solver.h:192
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::L2
Definition: enum_norm_type.h:36
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::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