62{
63
65
66
68 "--enable-petsc, --enable-trilinos, or --enable-eigen");
69
70
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
80#ifndef LIBMESH_ENABLE_DIRICHLET
81 libmesh_example_requires(false, "--enable-dirichlet");
82#endif
83
84
85
87
88
89 GetPot infile("fem_system_ex1.in");
90
91
92 infile.parse_command_line(argc, argv);
93
94
95 const Real global_tolerance = infile(
"global_tolerance", 0.);
96 const unsigned int nelem_target = infile("n_elements", 400);
97 const bool transient = infile("transient", true);
98 const Real deltat = infile(
"deltat", 0.005);
99 unsigned int n_timesteps = infile("n_timesteps", 20);
100 const unsigned int coarsegridsize = infile("coarsegridsize", 1);
101 const unsigned int coarserefinements = infile("coarserefinements", 0);
102 const unsigned int max_adaptivesteps = infile("max_adaptivesteps", 10);
103 const unsigned int dim = infile(
"dimension", 2);
104 const std::string slvr_type = infile("solver_type", "newton");
105 const std::string mesh_type = infile("mesh_type" , "replicated");
106 const bool constrain_in_solver = infile("constrain_in_solver", true);
107
108
109 const bool print_solutions = infile("print_solutions", false);
110 const bool print_residuals = infile("print_residuals", false);
111 const bool print_jacobians = infile("print_jacobians", false);
112
113#ifdef LIBMESH_HAVE_EXODUS_API
114 const unsigned int write_interval = infile("write_interval", 5);
115#endif
116
117
118 libmesh_example_requires(
dim <= LIBMESH_DIM,
"2D/3D support");
119
120
122
123
124
125 std::shared_ptr<UnstructuredMesh>
mesh;
126
127 if (mesh_type == "distributed")
128 mesh = std::make_shared<DistributedMesh>(
init.comm());
129 else if (mesh_type == "replicated")
130 mesh = std::make_shared<ReplicatedMesh>(
init.comm());
131 else
132 libmesh_error_msg("Error: specified mesh_type not understood");
133
134
136 mesh_refinement.coarsen_by_parents() = true;
137 mesh_refinement.absolute_global_tolerance() = global_tolerance;
138 mesh_refinement.nelem_target() = nelem_target;
139 mesh_refinement.refine_fraction() = 0.3;
140 mesh_refinement.coarsen_fraction() = 0.3;
141 mesh_refinement.coarsen_threshold() = 0.1;
142
143
144
145
146
147
150 coarsegridsize,
151 coarsegridsize,
152 0., 1.,
153 0., 1.,
157 coarsegridsize,
158 coarsegridsize,
159 coarsegridsize,
160 0., 1.,
161 0., 1.,
162 0., 1.,
164
165 mesh_refinement.uniformly_refine(coarserefinements);
166
167
169
170
172
173
175 equation_systems.add_system<
NavierSystem> (
"Navier-Stokes");
176
177
184
185
186 if (transient)
187 system.
time_solver = std::make_unique<EulerSolver>(system);
188 else
189 {
190 system.
time_solver = std::make_unique<SteadySolver>(system);
191 libmesh_assert_equal_to (n_timesteps, 1);
192 }
193
194
195 equation_systems.init ();
196
197
199
200
201 if (slvr_type == "newton")
202 system.
time_solver->diff_solver() = std::make_unique<NewtonSolver>(system);
203 else if (slvr_type == "petscdiff")
204#if defined(LIBMESH_HAVE_PETSC) && defined(LIBMESH_HAVE_METAPHYSICL)
205 system.
time_solver->diff_solver() = std::make_unique<PetscDiffSolver>(system);
206#else
207 libmesh_example_requires(false, "--enable-petsc --enable-metaphysicl-required");
208#endif
209 else
210 libmesh_error_msg("Error: specified solver_type not understood");
211
214
215 solver.
quiet = infile(
"solver_quiet",
true);
218 infile("max_nonlinear_iterations", 15);
220 infile("relative_step_tolerance", 1.e-3);
222 infile("relative_residual_tolerance", 0.0);
224 infile("absolute_residual_tolerance", 0.0);
225
227
228
230 infile("max_linear_iterations", 50000);
232 infile("initial_linear_tolerance", 1.e-3);
233
234
235 equation_systems.print_info();
236
237
238
239 for (unsigned int t_step=0; t_step != n_timesteps; ++t_step)
240 {
241
243 << t_step
244 << ", time = "
246 << std::endl;
247
248
249 unsigned int a_step = 0;
250 for (; a_step != max_adaptivesteps; ++a_step)
251 {
253
255
257
258 std::unique_ptr<ErrorEstimator> error_estimator;
259
260
261
262 if (global_tolerance != 0.)
263 {
264
265
266 libmesh_assert_equal_to (nelem_target, 0);
267
268 error_estimator = std::make_unique<UniformRefinementEstimator>();
269
270
271
272 error_estimator->error_norm =
L2;
273 }
274 else
275 {
276
277
278 libmesh_assert_greater (nelem_target, 0);
279
280
281
282
283
284 error_estimator = std::make_unique<KellyErrorEstimator>();
285 }
286
287
288 std::vector<Real> weights(2,1.0);
290 weights.push_back(1.0);
291 weights.push_back(0.0);
292
293 std::vector<FEMNormType>
294 norms(1, error_estimator->error_norm.type(0));
295 error_estimator->error_norm =
SystemNorm(norms, weights);
296
297 error_estimator->estimate_error(system, error);
298
299
302 << a_step
303 << ": "
304 << std::endl;
305
306 if (global_tolerance != 0.)
308 << global_error
309 << std::endl;
310
311 if (global_tolerance != 0.)
314 << ", mean = "
316 << std::endl;
317
318 if (global_tolerance != 0.)
319 {
320
321
322 if (global_error < global_tolerance)
323 break;
324 mesh_refinement.flag_elements_by_error_tolerance(error);
325 }
326 else
327 {
328
329
330 if (mesh_refinement.flag_elements_by_nelem_target(error))
331 {
332 mesh_refinement.refine_and_coarsen_elements();
333 equation_systems.reinit();
334 a_step = max_adaptivesteps;
335 break;
336 }
337 }
338
339
340 mesh_refinement.refine_and_coarsen_elements();
341 equation_systems.reinit();
342
345 << " active elements and "
346 << equation_systems.n_active_dofs()
347 << " active dofs."
348 << std::endl;
349 }
350
351 if (a_step == max_adaptivesteps)
352 {
354
356 }
357
358
360
361#ifdef LIBMESH_HAVE_EXODUS_API
362
363 if ((t_step+1)%write_interval == 0)
364 {
365 std::ostringstream file_name;
366
367
368 file_name << "out_"
369 << std::setw(3)
370 << std::setfill('0')
371 << std::right
372 << t_step+1
373 << ".e";
374
376 equation_systems,
377 1,
378
380 }
381#endif
382 }
383#endif
384
385
386 return 0;
387}
virtual void postprocess()
Runs a postprocessing loop over all elements, and if postprocess_sides is true over all sides.
This is a generic class that defines a solver to handle ImplicitSystem classes, including NonlinearIm...
Real absolute_residual_tolerance
The DiffSolver should exit after the residual is reduced to either less than absolute_residual_tolera...
unsigned int max_linear_iterations
Each linear solver step should exit after max_linear_iterations is exceeded.
virtual void init()
The initialization function.
double initial_linear_tolerance
Any required linear solves will at first be done with this tolerance; the DiffSolver may tighten the ...
Real relative_residual_tolerance
Real relative_step_tolerance
bool verbose
The DiffSolver may print a lot more to libMesh::out if verbose is set to true; default is false.
unsigned int max_nonlinear_iterations
The DiffSolver should exit in failure if max_nonlinear_iterations is exceeded and continue_after_max_...
bool quiet
The DiffSolver should not print anything to libMesh::out unless quiet is set to false; default is tru...
bool print_jacobians
Set print_jacobians to true to print J whenever it is assembled.
bool print_residuals
Set print_residuals to true to print F whenever it is assembled.
Real deltat
For time-dependent problems, this is the amount delta t to advance the solution in time.
virtual void set_constrain_in_solver(bool enable)
set_constrain_in_solver to false to apply constraints only via residual terms in the systems to be so...
bool print_solution_norms
Set print_residual_norms to true to print |U| whenever it is used in an assembly() call.
bool print_solutions
Set print_solutions to true to print U whenever it is used in an assembly() call.
bool print_residual_norms
Set print_residual_norms to true to print |F| whenever it is assembled.
bool print_jacobian_norms
Set print_jacobian_norms to true to print |J| whenever it is assembled.
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we're going to use.
This is the EquationSystems class.
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
virtual Real mean() const override
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
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.
virtual void solve() override
Invokes the solver associated with the system.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
virtual dof_id_type n_active_elem() const =0
Implements (adaptive) mesh refinement algorithms for a MeshBase.
virtual T maximum() const
virtual Real l2_norm() const
This class defines a norm/seminorm to be applied to a NumericVector which contains coefficients in a ...
Real time
For time-dependent problems, this is the time t at the beginning of the current timestep.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
SolverPackage default_solver_package()
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real