50#include "libmesh/libmesh.h"
51#include "libmesh/mesh.h"
52#include "libmesh/mesh_generation.h"
53#include "libmesh/equation_systems.h"
54#include "libmesh/fem_system.h"
55#include "libmesh/fem_context.h"
56#include "libmesh/steady_solver.h"
57#include "libmesh/diff_solver.h"
58#include "libmesh/dof_map.h"
59#include "libmesh/dirichlet_boundaries.h"
60#include "libmesh/zero_function.h"
61#include "libmesh/exact_solution.h"
62#include "libmesh/getpot.h"
63#include "libmesh/enum_solver_package.h"
64#include "libmesh/int_range.h"
65#include "libmesh/vector_value.h"
68#ifdef LIBMESH_HAVE_METAPHYSICL
69#include "metaphysicl/dualnumber.h"
70#include "metaphysicl/dynamicsparsenumberarray.h"
71#include "metaphysicl/raw_type.h"
76#ifdef LIBMESH_HAVE_METAPHYSICL
80typedef MetaPhysicL::DualNumber<Number, MetaPhysicL::DynamicSparseNumberArray<Number, unsigned int>>
95 pi * std::sin(
pi * p(0)) * std::cos(
pi * p(1)));
142 FEMSystem::init_data();
148 FEMContext & c = cast_ref<FEMContext &>(context);
158 FEBase * side_fe =
nullptr;
162 FEMSystem::init_context(context);
168 FEMContext & c = cast_ref<FEMContext &>(context);
172 const std::vector<Real> & JxW = fe->
get_JxW();
173 const std::vector<std::vector<Real>> & phi = fe->
get_phi();
174 const std::vector<std::vector<RealGradient>> & dphi = fe->
get_dphi();
175 const std::vector<Point> & xyz = fe->
get_xyz();
184 std::vector<ADNumber> u_dofs(
n_dofs);
187 u_dofs[j] = u_coefs(j);
188 u_dofs[j].derivatives().insert(j) = 1;
193 std::vector<ADNumber> residual(
n_dofs, 0);
200 u += u_dofs[j] * phi[j][qp];
201 grad_u += u_dofs[j] * dphi[j][qp];
208 const ADNumber diffusivity = 1. + u * u;
212 residual[i] += JxW[qp] * (-diffusivity * (grad_u * dphi[i][qp]) + f * phi[i][qp]);
219 F(i) += MetaPhysicL::raw_value(residual[i]);
221 if (request_jacobian)
223 const auto & derivatives = residual[i].derivatives();
224 for (
const auto k :
index_range(derivatives.nude_indices()))
225 K(i, derivatives.nude_indices()[k]) +=
230 return request_jacobian;
243 "--enable-petsc, --enable-trilinos, or --enable-eigen");
246#ifndef LIBMESH_HAVE_METAPHYSICL
247 libmesh_example_requires(
false,
"--enable-metaphysicl");
251 libmesh_example_requires(2 <= LIBMESH_DIM,
"2D support");
254#ifndef LIBMESH_ENABLE_DIRICHLET
255 libmesh_example_requires(
false,
"--enable-dirichlet");
259 GetPot infile(
"fem_system_ex6.in");
260 infile.parse_command_line(argc, argv);
262 const unsigned int grid_size = infile(
"grid_size", 16);
270 system.
time_solver = std::make_unique<SteadySolver>(system);
274 system.verify_analytic_jacobians = infile(
"verify_analytic_jacobians", 0.);
276 equation_systems.
init();
278 DiffSolver & solver = *(system.time_solver->diff_solver());
279 solver.
quiet =
false;
295 <<
"H1 error: " << exact_sol.
h1_error(
"NonlinearDiffusion",
"u") << std::endl;
An FEMSystem for -div((1 + u^2) grad u) = f whose element Jacobian is computed by forward-mode automa...
NonlinearDiffusionSystem(EquationSystems &es, const std::string &name, const unsigned int number)
virtual void init_context(DiffContext &context) override
virtual void init_data() override
Initializes the member data fields associated with the system, so that, e.g., assemble() may be used.
virtual bool element_time_derivative(bool request_jacobian, DiffContext &context) override
Adds the time derivative contribution on elem to elem_residual.
Defines a dense submatrix for use in Finite Element-type computations.
Defines a dense subvector for use in finite element computations.
This class provides all data required for a physics package (e.g.
Real get_elem_solution_derivative() const
The derivative of the current elem_solution w.r.t.
const DenseVector< Number > & get_elem_solution() const
Accessor for element solution.
unsigned int n_dof_indices() const
Total number of dof indices on the element.
const DenseVector< Number > & get_elem_residual() const
Const accessor for element residual.
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
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...
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...
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we're going to use.
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
This is the EquationSystems class.
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
virtual void init()
Initialize all the systems.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
This class handles the computation of the L2 and/or H1 error for the Systems in the EquationSystems o...
Real l2_error(std::string_view sys_name, std::string_view unknown_name)
Real h1_error(std::string_view sys_name, std::string_view unknown_name)
void attach_exact_deriv(unsigned int sys_num, FunctionBase< Gradient > *g)
Clone and attach an arbitrary functor which computes the exact gradient of the system sys_num solutio...
void attach_exact_value(unsigned int sys_num, FunctionBase< Number > *f)
Clone and attach an arbitrary functor which computes the exact value of the system sys_num solution a...
void compute_error(std::string_view sys_name, std::string_view unknown_name)
Computes and stores the error in the solution value e = u-u_h, the gradient grad(e) = grad(u) - grad(...
virtual_for_inffe const std::vector< Real > & get_JxW() const
virtual_for_inffe const std::vector< Point > & get_xyz() const
This class forms the foundation from which generic finite elements may be derived.
const std::vector< std::vector< OutputShape > > & get_phi() const
const std::vector< std::vector< OutputGradient > > & get_dphi() const
This class provides all data required for a physics package (e.g.
void get_side_fe(unsigned int var, FEGenericBase< OutputShape > *&fe) const
Accessor for edge/face (2D/3D) finite element object for variable var for the largest dimension in th...
void get_element_fe(unsigned int var, FEGenericBase< OutputShape > *&fe) const
Accessor for interior finite element object for variable var for the largest dimension in the mesh.
This class provides a specific system class.
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.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
This class provides the ability to map between arbitrary, user-defined strings and several data types...
A Point defines a location in LIBMESH_DIM dimensional Real space.
const std::string & name() const
dof_id_type n_dofs() const
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
const DofMap & get_dof_map() const
unsigned int number() const
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
ConstFunction that simply returns 0.
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 &)
MetaPhysicL::DualNumber< Number, MetaPhysicL::DynamicSparseNumberArray< Number, unsigned int > > ADNumber
Number forcing(const Point &p)
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
SolverPackage default_solver_package()
RealVectorValue RealGradient
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
IntRange< T > make_range(T beg, T end)
The 2-parameter make_range() helper function returns an IntRange<T> when both input parameters are of...