libMesh
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
fem_system_ex6.C File Reference

Go to the source code of this file.

Classes

class  NonlinearDiffusionSystem
 An FEMSystem for -div((1 + u^2) grad u) = f whose element Jacobian is computed by forward-mode automatic differentiation of the element residual. More...
 

Typedefs

typedef MetaPhysicL::DualNumber< Number, MetaPhysicL::DynamicSparseNumberArray< Number, unsigned int > > ADNumber
 

Functions

Number exact_value (const Point &p, const Parameters &, const std::string &, const std::string &)
 
Gradient exact_gradient (const Point &p, const Parameters &, const std::string &, const std::string &)
 
Number forcing (const Point &p)
 
int main (int argc, char **argv)
 

Typedef Documentation

◆ ADNumber

typedef MetaPhysicL::DualNumber<Number, MetaPhysicL::DynamicSparseNumberArray<Number, unsigned int> > ADNumber

Definition at line 81 of file fem_system_ex6.C.

Function Documentation

◆ exact_gradient()

Gradient exact_gradient ( const Point &  p,
const Parameters &  ,
const std::string &  ,
const std::string &   
)

Definition at line 91 of file fem_system_ex6.C.

92{
93 const Real pi = libMesh::pi;
94 return RealGradient(pi * std::cos(pi * p(0)) * std::sin(pi * p(1)),
95 pi * std::sin(pi * p(0)) * std::cos(pi * p(1)));
96}
const Real pi
.
Definition libmesh.h:292
RealVectorValue RealGradient
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

References libMesh::pi, and libMesh::Real.

Referenced by forcing(), and main().

◆ exact_value()

Number exact_value ( const Point &  p,
const Parameters &  ,
const std::string &  ,
const std::string &   
)

Definition at line 85 of file fem_system_ex6.C.

86{
87 return std::sin(libMesh::pi * p(0)) * std::sin(libMesh::pi * p(1));
88}

References libMesh::pi.

Referenced by forcing(), and main().

◆ forcing()

Number forcing ( const Point &  p)

Definition at line 101 of file fem_system_ex6.C.

102{
103 const Number u = exact_value(p, Parameters(), "", "");
104 const Gradient grad_u = exact_gradient(p, Parameters(), "", "");
105 return 2 * libMesh::pi * libMesh::pi * (1. + u * u) * u - 2. * u * grad_u.norm_sq();
106}
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition parameters.h:75
auto norm_sq() const
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
Gradient exact_gradient(const Point &p, const Parameters &, const std::string &, const std::string &)
Number exact_value(const Point &p, const Parameters &, const std::string &, const std::string &)

References exact_gradient(), exact_value(), libMesh::TypeVector< T >::norm_sq(), and libMesh::pi.

Referenced by assemble_advection_diffusion(), HeatSystem::element_time_derivative(), and NonlinearDiffusionSystem::element_time_derivative().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 236 of file fem_system_ex6.C.

237{
238 // Initialize libMesh.
239 LibMeshInit init(argc, argv);
240
241 // This example requires a linear solver package.
242 libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
243 "--enable-petsc, --enable-trilinos, or --enable-eigen");
244
245 // Dual numbers are provided by MetaPhysicL.
246#ifndef LIBMESH_HAVE_METAPHYSICL
247 libmesh_example_requires(false, "--enable-metaphysicl");
248#else
249
250 // The mesh is two-dimensional.
251 libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
252
253 // We use Dirichlet boundary conditions here.
254#ifndef LIBMESH_ENABLE_DIRICHLET
255 libmesh_example_requires(false, "--enable-dirichlet");
256#endif
257
258 // Parse the input file, allowing the command line to override it.
259 GetPot infile("fem_system_ex6.in");
260 infile.parse_command_line(argc, argv);
261
262 const unsigned int grid_size = infile("grid_size", 16);
263
264 Mesh mesh(init.comm());
265 MeshTools::Generation::build_square(mesh, grid_size, grid_size, 0., 1., 0., 1., QUAD9);
267
268 EquationSystems equation_systems(mesh);
269 auto & system = equation_systems.add_system<NonlinearDiffusionSystem>("NonlinearDiffusion");
270 system.time_solver = std::make_unique<SteadySolver>(system);
271
272 // A positive tolerance checks each element Jacobian against a
273 // finite-difference approximation.
274 system.verify_analytic_jacobians = infile("verify_analytic_jacobians", 0.);
275
276 equation_systems.init();
277
278 DiffSolver & solver = *(system.time_solver->diff_solver());
279 solver.quiet = false;
280 solver.verbose = true;
281 solver.max_nonlinear_iterations = infile("max_nonlinear_iterations", 10);
282 solver.relative_residual_tolerance = infile("relative_residual_tolerance", 1.e-12);
283 solver.relative_step_tolerance = infile("relative_step_tolerance", 0.);
284 solver.absolute_residual_tolerance = infile("absolute_residual_tolerance", 0.);
285
286 equation_systems.print_info();
287
288 system.solve();
289
290 ExactSolution exact_sol(equation_systems);
291 exact_sol.attach_exact_value(exact_value);
292 exact_sol.attach_exact_deriv(exact_gradient);
293 exact_sol.compute_error("NonlinearDiffusion", "u");
294 libMesh::out << "L2 error: " << exact_sol.l2_error("NonlinearDiffusion", "u") << std::endl
295 << "H1 error: " << exact_sol.h1_error("NonlinearDiffusion", "u") << std::endl;
296
297#endif // LIBMESH_HAVE_METAPHYSICL
298
299 return 0;
300}
An FEMSystem for -div((1 + u^2) grad u) = f whose element Jacobian is computed by forward-mode automa...
This is a generic class that defines a solver to handle ImplicitSystem classes, including NonlinearIm...
Definition diff_solver.h:70
Real absolute_residual_tolerance
The DiffSolver should exit after the residual is reduced to either less than absolute_residual_tolera...
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...
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we're going to use.
This is the EquationSystems class.
This class handles the computation of the L2 and/or H1 error for the Systems in the EquationSystems o...
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
Definition mesh_base.C:1770
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
MeshBase & mesh
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.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
SolverPackage default_solver_package()
Definition libmesh.C:1064
OStreamProxy out

References libMesh::DiffSolver::absolute_residual_tolerance, libMesh::EquationSystems::add_system(), libMesh::ExactSolution::attach_exact_deriv(), libMesh::ExactSolution::attach_exact_value(), libMesh::MeshTools::Generation::build_square(), libMesh::ExactSolution::compute_error(), libMesh::default_solver_package(), exact_gradient(), exact_value(), libMesh::ExactSolution::h1_error(), libMesh::EquationSystems::init(), libMesh::INVALID_SOLVER_PACKAGE, libMesh::ExactSolution::l2_error(), main(), libMesh::DiffSolver::max_nonlinear_iterations, mesh, libMesh::out, libMesh::EquationSystems::print_info(), libMesh::MeshBase::print_info(), libMesh::QUAD9, libMesh::DiffSolver::quiet, libMesh::DiffSolver::relative_residual_tolerance, libMesh::DiffSolver::relative_step_tolerance, libMesh::DifferentiableSystem::time_solver, and libMesh::DiffSolver::verbose.