libMesh
Loading...
Searching...
No Matches
transient_ex3.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
55using 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.
66int 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
109
110 equation_systems.init ();
111
112 equation_systems.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 class extends ClawSystem to implement pure advection in 2D.
virtual void print_info() override
Print out some info about the system's configuration.
virtual void process_parameters_file(const std::string &parameters_filename) override
Read in data from input file.
void solve_conservation_law()
Solve the conservation law.
Definition claw_system.C:95
void assemble_all_matrices()
Assemble the matrices we need to solve a conservation law.
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.
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.
const T_sys & get_system(std::string_view name) const
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition exodusII_io.h:53
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.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition libmesh.h:92
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and linear solvers ...
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:1755
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition mesh.h:51
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition parameters.h:75
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition point.h:40
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr, std::optional< ConstElemRange > active_local_range=std::nullopt, std::optional< std::vector< unsigned int > > variable_numbers=std::nullopt) const
Projects arbitrary functions onto the current solution.
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:1928
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.
The libMesh namespace provides an interface to certain functionality in the library.
SolverPackage default_solver_package()
Definition libmesh.C:1064
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void set_initial_condition(EquationSystems &es, const std::string &system_name)
Number initial_condition(const Point &p, const Parameters &parameters, const std::string &, const std::string &)
int main()