libMesh
Classes | Functions
miscellaneous_ex3.C File Reference

Go to the source code of this file.

Classes

class  LaplaceYoung
 A class which provides the residual and jacobian assembly functions for the Laplace-Young system of equations. More...
 

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 135 of file miscellaneous_ex3.C.

References libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), libMesh::MeshBase::all_second_order(), libMesh::command_line_next(), libMesh::err, libMesh::NonlinearImplicitSystem::final_nonlinear_residual(), libMesh::EquationSystems::get_system(), libMesh::TriangleWrapper::init(), libMesh::EquationSystems::init(), mesh, libMesh::NonlinearImplicitSystem::n_nonlinear_iterations(), libMesh::NonlinearImplicitSystem::nonlinear_solver, libMesh::on_command_line(), libMesh::out, libMesh::EquationSystems::parameters, libMesh::EquationSystems::print_info(), libMesh::MeshBase::print_info(), libMesh::MeshBase::read(), libMesh::Real, libMesh::Parameters::set(), libMesh::MeshRefinement::uniformly_refine(), and libMesh::ExodusII_IO::write_equation_systems().

136 {
137  // Initialize libMesh and any dependent libraries, like in example 2.
138  LibMeshInit init (argc, argv);
139 
140  // This example requires a NonlinearSolver.
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");
143 #endif
144 
145  if (libMesh::on_command_line ("--use-eigen"))
146  {
147  libMesh::err << "This example requires a NonlinearSolver, and therefore does not "
148  << "support --use-eigen on the command line."
149  << std::endl;
150  return 0;
151  }
152 
153 #ifndef LIBMESH_ENABLE_AMR
154  libmesh_example_requires(false, "--enable-amr");
155 #else
156 
157  // Create a GetPot object to parse the command line
158  GetPot command_line (argc, argv);
159 
160  // Check for proper calling arguments.
161  libmesh_error_msg_if(argc < 3, "Usage:\n" << "\t " << argv[0] << " -r 2");
162 
163  // Brief message to the user regarding the program name
164  // and command line arguments.
165  libMesh::out << "Running " << argv[0];
166 
167  for (int i=1; i<argc; i++)
168  libMesh::out << " " << argv[i];
169 
170  libMesh::out << std::endl << std::endl;
171 
172  // Read number of refinements
173  const int nr = libMesh::command_line_next("-r", 2);
174 
175  // Read FE order from command line
176  std::string order = "FIRST";
177  order = libMesh::command_line_next("-o", order);
178  order = libMesh::command_line_next("-Order", order);
179 
180  // Read FE Family from command line
181  std::string family = "LAGRANGE";
182  family = libMesh::command_line_next("-f", family);
183  family = libMesh::command_line_next("-FEFamily", family);
184 
185  // Cannot use discontinuous basis.
186  libmesh_error_msg_if((family == "MONOMIAL") || (family == "XYZ"),
187  "This example requires a C^0 (or higher) FE basis.");
188 
189  if (libMesh::on_command_line("-pre"))
190  {
191 #ifdef LIBMESH_HAVE_PETSC
192  //Use the jacobian for preconditioning.
193 # if PETSC_VERSION_LESS_THAN(3,7,0)
194  LibmeshPetscCall2(init.comm(), PetscOptionsSetValue("-snes_mf_operator",
195  LIBMESH_PETSC_NULLPTR));
196 # else
197  LibmeshPetscCall2(init.comm(), PetscOptionsSetValue(LIBMESH_PETSC_NULLPTR, "-snes_mf_operator",
198  LIBMESH_PETSC_NULLPTR));
199 # endif
200 #else
201  libMesh::err << "Must be using PETSc to use jacobian based preconditioning" << std::endl;
202 
203  //returning zero so that "make run" won't fail if we ever enable this capability there.
204  return 0;
205 #endif //LIBMESH_HAVE_PETSC
206  }
207 
208  // Skip this 2D example if libMesh was compiled as 1D-only.
209  libmesh_example_requires(2 <= LIBMESH_DIM, "2D support");
210 
211  // Create a mesh, with dimension to be overridden by the file,
212  // distributed across the default MPI communicator.
213  Mesh mesh(init.comm());
214 
215  mesh.read ("lshaped.xda");
216 
217  if (order != "FIRST")
219 
221 
222  // Print information about the mesh to the screen.
223  mesh.print_info();
224 
225  // Create an equation systems object.
226  EquationSystems equation_systems (mesh);
227 
228  // Declare the system and its variables.
229 
230  // Creates a system named "Laplace-Young"
231  NonlinearImplicitSystem & system =
232  equation_systems.add_system<NonlinearImplicitSystem> ("Laplace-Young");
233 
234  // Here we specify the tolerance for the nonlinear solver and
235  // the maximum of nonlinear iterations.
236  equation_systems.parameters.set<Real> ("nonlinear solver tolerance") = 1.e-12;
237  equation_systems.parameters.set<unsigned int> ("nonlinear solver maximum iterations") = 50;
238 
239 
240  // Adds the variable "u" to "Laplace-Young". "u"
241  // will be approximated using second-order approximation.
242  system.add_variable("u",
243  Utility::string_to_enum<Order> (order),
244  Utility::string_to_enum<FEFamily>(family));
245 
246  // Construct object which provides the residual and jacobian
247  // computations and tell the solver to use it.
248  LaplaceYoung laplace_young;
249  system.nonlinear_solver->residual_object = &laplace_young;
250  system.nonlinear_solver->jacobian_object = &laplace_young;
251  system.nonlinear_solver->postcheck_object = &laplace_young;
252 
253  // Initialize the data structures for the equation system.
254  equation_systems.init();
255 
256  // Prints information about the system to the screen.
257  equation_systems.print_info();
258 
259  // Solve the system "Laplace-Young", print the number of iterations
260  // and final residual
261  equation_systems.get_system("Laplace-Young").solve();
262 
263  // Print out final convergence information. This duplicates some
264  // output from during the solve itself, but demonstrates another way
265  // to get this information after the solve is complete.
266  libMesh::out << "Laplace-Young system solved at nonlinear iteration "
267  << system.n_nonlinear_iterations()
268  << " , final nonlinear residual norm: "
269  << system.final_nonlinear_residual()
270  << std::endl;
271 
272 #ifdef LIBMESH_HAVE_EXODUS_API
273  // After solving the system write the solution
275  equation_systems);
276 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
277 #endif // #ifndef LIBMESH_ENABLE_AMR
278 
279  // All done.
280  return 0;
281 }
OStreamProxy err
T command_line_next(std::string name, T default_value)
Use GetPot&#39;s search()/next() functions to get following arguments from the command line...
Definition: libmesh.C:1078
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...
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:52
MeshBase & mesh
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:90
Parameters parameters
Parameters for the system. If a parameter is not provided, it should be retrieved from the EquationSy...
Definition: system.h:1526
Implements (adaptive) mesh refinement algorithms for a MeshBase.
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.
Definition: exodusII_io.C:2033
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:1562
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
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.
Definition: system.C:1357
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
T & set(const std::string &)
Definition: parameters.h:469
OStreamProxy out
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...
Definition: mesh_base.C:1608
bool on_command_line(std::string arg)
Definition: libmesh.C:987
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.