44 #include "libmesh/libmesh.h" 45 #include "libmesh/mesh.h" 46 #include "libmesh/mesh_refinement.h" 47 #include "libmesh/exodusII_io.h" 48 #include "libmesh/equation_systems.h" 49 #include "libmesh/fe.h" 50 #include "libmesh/quadrature_gauss.h" 51 #include "libmesh/dof_map.h" 52 #include "libmesh/sparse_matrix.h" 53 #include "libmesh/numeric_vector.h" 54 #include "libmesh/dense_matrix.h" 55 #include "libmesh/dense_vector.h" 56 #include "libmesh/elem.h" 57 #include "libmesh/string_to_enum.h" 58 #include "libmesh/getpot.h" 61 #include "libmesh/nonlinear_solver.h" 62 #include "libmesh/nonlinear_implicit_system.h" 65 #ifdef LIBMESH_HAVE_PETSC 67 #include "libmesh/petsc_macro.h" 68 #include "libmesh/petsc_solver_exception.h" 120 bool & changed_search_direction,
121 bool & changed_new_soln,
135 int main (
int argc,
char ** argv)
141 #if !defined(LIBMESH_HAVE_PETSC) && (!defined(LIBMESH_TRILINOS_HAVE_NOX) || !defined(LIBMESH_TRILINOS_HAVE_EPETRA)) 142 libmesh_example_requires(
false,
"--enable-petsc or --enable-trilinos");
147 libMesh::err <<
"This example requires a NonlinearSolver, and therefore does not " 148 <<
"support --use-eigen on the command line." 153 #ifndef LIBMESH_ENABLE_AMR 154 libmesh_example_requires(
false,
"--enable-amr");
158 GetPot command_line (argc, argv);
161 libmesh_error_msg_if(argc < 3,
"Usage:\n" <<
"\t " << argv[0] <<
" -r 2");
167 for (
int i=1; i<argc; i++)
176 std::string order =
"FIRST";
181 std::string family =
"LAGRANGE";
186 libmesh_error_msg_if((family ==
"MONOMIAL") || (family ==
"XYZ"),
187 "This example requires a C^0 (or higher) FE basis.");
191 #ifdef LIBMESH_HAVE_PETSC 193 # if PETSC_VERSION_LESS_THAN(3,7,0) 194 LibmeshPetscCall2(
init.comm(), PetscOptionsSetValue(
"-snes_mf_operator",
195 LIBMESH_PETSC_NULLPTR));
197 LibmeshPetscCall2(
init.comm(), PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR,
"-snes_mf_operator",
198 LIBMESH_PETSC_NULLPTR));
201 libMesh::err <<
"Must be using PETSc to use jacobian based preconditioning" << std::endl;
205 #endif //LIBMESH_HAVE_PETSC 209 libmesh_example_requires(2 <= LIBMESH_DIM,
"2D support");
217 if (order !=
"FIRST")
237 equation_systems.
parameters.
set<
unsigned int> (
"nonlinear solver maximum iterations") = 50;
243 Utility::string_to_enum<Order> (order),
244 Utility::string_to_enum<FEFamily>(family));
254 equation_systems.
init();
261 equation_systems.
get_system(
"Laplace-Young").solve();
266 libMesh::out <<
"Laplace-Young system solved at nonlinear iteration " 268 <<
" , final nonlinear residual norm: " 272 #ifdef LIBMESH_HAVE_EXODUS_API 276 #endif // #ifdef LIBMESH_HAVE_EXODUS_API 277 #endif // #ifndef LIBMESH_ENABLE_AMR 297 libmesh_assert_equal_to (
dim, 2);
311 FEType fe_type = dof_map.variable_type(0);
323 fe->attach_quadrature_rule (&qrule);
336 fe_face->attach_quadrature_rule (&qface);
342 const std::vector<Real> & JxW = fe->get_JxW();
345 const std::vector<std::vector<Real>> & phi = fe->get_phi();
349 const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
357 std::vector<dof_id_type> dof_indices;
364 for (
const auto & elem :
mesh.active_local_element_ptr_range())
370 dof_map.dof_indices (elem, dof_indices);
383 const unsigned int n_dofs =
384 cast_int<unsigned int>(dof_indices.size());
395 for (
unsigned int qp=0; qp<qrule.
n_points(); qp++)
400 for (
unsigned int j=0; j<n_dofs; j++)
402 u += phi[j][qp]*soln(dof_indices[j]);
403 grad_u += dphi[j][qp]*soln(dof_indices[j]);
406 const Number K = 1./std::sqrt(1. + grad_u*grad_u);
408 for (
unsigned int i=0; i<n_dofs; i++)
410 K*(dphi[i][qp]*grad_u) +
422 for (
auto side : elem->side_index_range())
423 if (elem->neighbor_ptr(side) ==
nullptr)
427 const std::vector<std::vector<Real>> & phi_face = fe_face->get_phi();
431 const std::vector<Real> & JxW_face = fe_face->get_JxW();
434 fe_face->reinit(elem, side);
437 for (
unsigned int qp=0; qp<qface.
n_points(); qp++)
441 for (
unsigned int i=0; i<n_dofs; i++)
442 Re(i) -= JxW_face[qp]*_sigma*phi_face[i][qp];
446 dof_map.constrain_element_vector (Re, dof_indices);
481 FEType fe_type = dof_map.variable_type(0);
493 fe->attach_quadrature_rule (&qrule);
499 const std::vector<Real> & JxW = fe->get_JxW();
502 const std::vector<std::vector<Real>> & phi = fe->get_phi();
506 const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
516 std::vector<dof_id_type> dof_indices;
521 for (
const auto & elem :
mesh.active_local_element_ptr_range())
527 dof_map.dof_indices (elem, dof_indices);
541 const unsigned int n_dofs =
542 cast_int<unsigned int>(dof_indices.size());
543 Ke.
resize (n_dofs, n_dofs);
551 for (
unsigned int qp=0; qp<qrule.
n_points(); qp++)
555 for (
unsigned int i=0; i<n_dofs; i++)
556 grad_u += dphi[i][qp]*soln(dof_indices[i]);
559 sa = 1. + grad_u*grad_u,
560 K = 1. / std::sqrt(sa),
563 for (
unsigned int i=0; i<n_dofs; i++)
564 for (
unsigned int j=0; j<n_dofs; j++)
566 K * (dphi[i][qp]*dphi[j][qp]) +
567 dK * (grad_u*dphi[j][qp]) * (grad_u*dphi[i][qp]) +
568 _kappa * phi[i][qp] * phi[j][qp]
572 dof_map.constrain_element_matrix (Ke, dof_indices);
588 bool & changed_new_soln,
603 new_soln.
add(-_gamma, search_direction);
604 changed_new_soln =
true;
607 changed_new_soln =
false;
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
T command_line_next(std::string name, T default_value)
Use GetPot's search()/next() functions to get following arguments from the command line...
This is the EquationSystems class.
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false)=0
Interfaces for reading/writing a mesh to/from a file.
std::unique_ptr< NonlinearSolver< Number > > nonlinear_solver
The NonlinearSolver defines the default interface used to solve the nonlinear_implicit system...
int main(int argc, char **argv)
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
void resize(const unsigned int n)
Resize the vector.
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i]...
const EquationSystems & get_equation_systems() const
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
virtual void postcheck(const NumericVector< Number > &old_soln, NumericVector< Number > &search_direction, NumericVector< Number > &new_soln, bool &changed_search_direction, bool &changed_new_soln, NonlinearImplicitSystem &S)
Function which performs a postcheck on the solution.
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
unsigned int n_nonlinear_iterations() const
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
The libMesh namespace provides an interface to certain functionality in the library.
const T_sys & get_system(std::string_view name) const
This is the MeshBase class.
virtual void zero()=0
Set all entries to zero.
This class handles the numbering of degrees of freedom on a mesh.
Abstract base class to be used to calculate the residual of a nonlinear system.
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
Add the full matrix dm to the SparseMatrix.
Real final_nonlinear_residual() const
Implements (adaptive) mesh refinement algorithms for a MeshBase.
Abstract base class to be used to calculate the Jacobian of a nonlinear system.
virtual void write_equation_systems(const std::string &fname, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
Writes out the solution for no specific time or timestep.
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and non-linear solv...
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.
unsigned int n_points() const
A class which provides the residual and jacobian assembly functions for the Laplace-Young system of e...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void jacobian(const NumericVector< Number > &X, SparseMatrix< Number > &J, NonlinearImplicitSystem &S)
Function which computes the jacobian.
T & set(const std::string &)
const MeshBase & get_mesh() const
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
void all_second_order(const bool full_ordered=true)
Calls the range-based version of this function with a range consisting of all elements in the mesh...
This class implements specific orders of Gauss quadrature.
unsigned int mesh_dimension() const
Parameters parameters
Data structure holding arbitrary parameters.
bool on_command_line(std::string arg)
virtual void init()
Initialize all the systems.
virtual void add(const numeric_index_type i, const T value)=0
Adds value to the vector entry specified by i.
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.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
virtual void residual(const NumericVector< Number > &X, NumericVector< Number > &R, NonlinearImplicitSystem &S)
Function which computes the residual.
const DofMap & get_dof_map() const
Abstract base class to be used for applying user modifications to the solution vector and/or Newton u...
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.