libMesh
transient_ex3.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 // <h1>Transient Example 3 - DG/FV formulation of the 2D Advection Equation with Explicit timestepping</h1>
19 // \author David Knezevic
20 // \date 2012
21 // \author John W. Peterson
22 // \date 2025 (modernization and libmesh example)
23 //
24 // This example program demonstrates one way to implement an explicit
25 // timestepping Discontinous Galerkin/Finite Volume formulation of the
26 // 2D advection equation. The example comes with an input file which
27 // is set up as a finite volume model by default, but it can be
28 // changed into a DG formulation by changing the fe_order and
29 // fe_family parameters in the input file appropriately. The example
30 // uses the Lax-Friedrichs numerical flux. This is known to be more
31 // diffusive than e.g. upwinding, but it is also relatively simple
32 // to implement since one does not need to consider the advective
33 // velocity direction during assembly. Finally, the example comes
34 // with two different time discretization options: explicit "forward"
35 // Euler and explicit fourth-order Runge-Kutta (RK4). The latter
36 // is used by default, but the "temporal_discretization_type" can
37 // be changed to ForwardEuler in the input file to test that option.
38 
39 // Basic include file needed for the mesh functionality.
40 #include "libmesh/libmesh.h"
41 #include "libmesh/mesh.h"
42 #include "libmesh/mesh_generation.h"
43 #include "libmesh/exodusII_io.h"
44 #include "libmesh/equation_systems.h"
45 #include "libmesh/getpot.h"
46 #include "libmesh/enum_solver_package.h"
47 
48 // Application includes
49 #include "advection_system.h"
50 
51 // C++ include files that we need
52 #include <iostream>
53 
54 // Bring in everything from the libMesh namespace
55 using namespace libMesh;
56 
57 // Function to set the initial condition
59  const std::string& system_name);
61  const Parameters& parameters,
62  const std::string&,
63  const std::string&);
64 
65 // The main program.
66 int main (int argc, char** argv)
67 {
68  // Initialize libMesh.
69  LibMeshInit init (argc, argv);
70 
71  // This example requires a linear solver package.
72  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
73  "--enable-petsc, --enable-trilinos, or --enable-eigen");
74 
75  // Create GetPot object to parse the command line
76  GetPot command_line (argc, argv);
77 
78  // Allow the user specify a custom input file on the command line
79  // using the "-i" flag, and provide a sensible default value in case
80  // they don't.
81  std::string parameters_filename = "advection_2D.in";
82  if ( command_line.search(2, "-i", "--input_file") )
83  parameters_filename = command_line.next(parameters_filename);
84 
85  // Read mesh options from the input file
86  GetPot infile(parameters_filename);
87  unsigned int n_elem = infile("n_elem", 1); // Determines the number of elements in the "truth" mesh
88 
89  // Build a mesh
90  Mesh mesh (init.comm());
92  n_elem, n_elem,
93  0., 1.,
94  0., 1.,
95  QUAD9);
96 
97  // Create an equation systems object
98  EquationSystems equation_systems (mesh);
99 
100  // Create an AdvectionSystem within the EquationSystems
101  AdvectionSystem & advection_system =
102  equation_systems.add_system<AdvectionSystem> ("Advection2D");
103 
104  // Process the input file values
105  advection_system.process_parameters_file(parameters_filename);
106 
107  // Set the initial condition function
108  advection_system.attach_init_function (set_initial_condition);
109 
110  equation_systems.init ();
111 
112  equation_systems.print_info();
113  mesh.print_info();
114 
115  advection_system.print_info();
116 
117  advection_system.assemble_all_matrices();
118  // advection_system.write_out_discretization_matrices();
119 
120 #ifdef LIBMESH_HAVE_EXODUS_API
121  ExodusII_IO(mesh).write_equation_systems ("claw_solution.0000.e", equation_systems);
122 #endif
123 
124  advection_system.solve_conservation_law();
125 
126  return 0;
127 }
128 
130  const std::string&)
131 {
132  LinearImplicitSystem & system =
133  es.get_system<LinearImplicitSystem>("Advection2D");
134 
135  system.project_solution(initial_condition, /*gradient=*/nullptr, es.parameters);
136 }
137 
139  const Parameters& ,
140  const std::string&,
141  const std::string&)
142 {
143  Real x = p(0);
144  Real y = p(1);
145 
146  return std::exp( -100.*(std::pow(x-0.5,2.) + std::pow(y-0.5,2.)) );
147 }
This is the EquationSystems class.
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:67
dof_id_type n_elem(const MeshBase::const_element_iterator &begin, const MeshBase::const_element_iterator &end)
Count up the number of elements of a specific type (as defined by an iterator range).
Definition: mesh_tools.C:969
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:52
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and linear solvers ...
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
MeshBase & mesh
virtual void print_info() override
Print out some info about the system&#39;s configuration.
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 assemble_all_matrices()
Assemble the matrices we need to solve a conservation law.
Definition: claw_system.C:262
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:90
The libMesh namespace provides an interface to certain functionality in the library.
const T_sys & get_system(std::string_view name) const
SolverPackage default_solver_package()
Definition: libmesh.C:1117
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
virtual void process_parameters_file(const std::string &parameters_filename) override
Read in data from input file.
T pow(const T &x)
Definition: utility.h:328
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
Number initial_condition(const Point &p, const Parameters &parameters, const std::string &, const std::string &)
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
void set_initial_condition(EquationSystems &es, const std::string &system_name)
void solve_conservation_law()
Solve the conservation law.
Definition: claw_system.C:95
This class extends ClawSystem to implement pure advection in 2D.
int main(int argc, char **argv)
Definition: transient_ex3.C:66
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Parameters parameters
Data structure holding arbitrary parameters.
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.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39
void attach_init_function(void fptr(EquationSystems &es, const std::string &name))
Register a user function to use in initializing the system.
Definition: system.C:2130