libMesh
periodic_bc_test.C
Go to the documentation of this file.
1 #include <libmesh/dirichlet_boundaries.h>
2 #include <libmesh/dof_map.h>
3 #include <libmesh/elem.h>
4 #include <libmesh/equation_systems.h>
5 #include <libmesh/exodusII_io.h>
6 #include <libmesh/function_base.h>
7 #include <libmesh/linear_implicit_system.h>
8 #include <libmesh/mesh.h>
9 #include <libmesh/numeric_vector.h>
10 #include <libmesh/periodic_boundary.h>
11 #include <libmesh/quadrature_gauss.h>
12 #include <libmesh/sparse_matrix.h>
13 #include <libmesh/wrapped_function.h>
14 
15 #include "test_comm.h"
16 #include "libmesh_cppunit.h"
17 
18 
19 using namespace libMesh;
20 
21 
22 
24  const Parameters&,
25  const std::string&,
26  const std::string&)
27 {
28  const Real & y = LIBMESH_DIM > 1 ? p(1) : 0;
29 
30  // Discontinuity at y=1 from the forcing function there.
31  // Periodic on -3 < y < 2
32  return (y > 1) ? (y-3)*(y-3) : (y+1)*(y+1);
33 }
34 
35 
36 
37 struct PeriodicQuadFunction : public FunctionBase<Number>
38 {
39  PeriodicQuadFunction() = default;
40 
41  virtual std::unique_ptr<FunctionBase<Number>> clone () const
42  { return std::make_unique<PeriodicQuadFunction>(); }
43 
44  // We only really need the vector-valued output for projections
45  virtual Number operator() (const Point &,
46  const Real /*time*/ = 0.) override
47  { libmesh_error(); }
48 
49  virtual void operator() (const Point & p,
50  const Real,
51  DenseVector<Number> & output) override
52  {
53  libmesh_assert_equal_to(output.size(), 1);
54  Parameters params;
55  output(0) = quadratic_solution(p, params, "", "");
56  }
57 
58  Number component (unsigned int i,
59  const Point & p,
60  Real /* time */) override
61  {
62  Parameters params;
63  switch (i) {
64  case 0:
65  return quadratic_solution(p, params, "", "");
66  default:
67  libmesh_error();
68  }
69  return 0;
70  }
71 };
72 
73 
75  const std::string&)
76 {
77  const MeshBase& mesh = es.get_mesh();
79  const DofMap& dof_map = system.get_dof_map();
80 
81  // Interior FE to integrate smooth part of poisson problem
82  FEType fe_type = dof_map.variable_type(0);
83  std::unique_ptr<FEBase> fe (FEBase::build(2, fe_type));
84  QGauss qrule (2, fe_type.default_quadrature_order());
85  fe->attach_quadrature_rule(&qrule);
86 
87  const std::vector<Real> & JxW = fe->get_JxW();
88  const std::vector<std::vector<Real>> & phi = fe->get_phi();
89  const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
90 
91  // Face FE to integrate delta function part of forcing
92  std::unique_ptr<FEBase> fe_face (FEBase::build(2, fe_type));
93  QGauss qface (1, fe_type.default_quadrature_order());
94  fe_face->attach_quadrature_rule(&qface);
95 
96  const std::vector<Real> & JxW_face = fe_face->get_JxW();
97  const std::vector<std::vector<Real>> & phi_face = fe_face->get_phi();
98  std::unique_ptr<const Elem> elem_side;
99 
102 
103  std::vector<dof_id_type> dof_indices;
104 
105  for (const Elem * elem : mesh.active_local_element_ptr_range())
106  {
107  dof_map.dof_indices (elem, dof_indices);
108  const unsigned int n_dofs = dof_indices.size();
109 
110  Ke.resize (n_dofs, n_dofs);
111  Fe.resize (n_dofs);
112 
113  fe->reinit (elem);
114 
115  // Integrate the poisson problem over the element interior, where we have
116  // a nice f=2 forcing function aside from the discontinuity
117  for (unsigned int qp=0; qp<qrule.n_points(); qp++)
118  {
119  for (unsigned int i=0; i != n_dofs; i++)
120  {
121  for (unsigned int j=0; j != n_dofs; j++)
122  Ke(i,j) += -JxW[qp]*(dphi[i][qp]*dphi[j][qp]);
123 
124  Fe(i) += JxW[qp]*phi[i][qp]*2;
125  }
126  }
127 
128  for(unsigned int s=0; s != elem->n_sides(); ++s)
129  {
130  elem->build_side_ptr(elem_side, s);
131  Point centroid = elem_side->vertex_average();
132  if (std::abs(centroid(1) - 1) < TOLERANCE)
133  {
134  fe_face->reinit(elem, s);
135 
136  for (unsigned int qp=0; qp<qface.n_points(); qp++)
137  {
138  // delta function divided by 2 since we hit it from
139  // both sides
140  for (unsigned int i=0; i != n_dofs; i++)
141  Fe(i) += JxW_face[qp]*phi_face[i][qp]*(-4);
142  }
143  }
144  }
145 
146  dof_map.heterogenously_constrain_element_matrix_and_vector(Ke, Fe, dof_indices);
147  system.matrix->add_matrix (Ke, dof_indices);
148  system.rhs->add_vector (Fe, dof_indices);
149  }
150 
151  system.rhs->close();
152  system.matrix->close();
153 }
154 
155 
159 
160 
161 class PeriodicBCTest : public CppUnit::TestCase {
162 public:
163  LIBMESH_CPPUNIT_TEST_SUITE( PeriodicBCTest );
164 
165 #if LIBMESH_DIM > 1
166 #if defined(LIBMESH_HAVE_SOLVER) && defined(LIBMESH_HAVE_EXODUS_API) && defined(LIBMESH_HAVE_GZSTREAM)
167  CPPUNIT_TEST( testPeriodicLagrange2 );
168 #endif
169 #endif // LIBMESH_DIM > 1
170 
171  CPPUNIT_TEST_SUITE_END();
172 
173 private:
174 
175  void testPeriodicBC (FEType fe_type)
176  {
178  BoundaryInfo & boundary = mesh.get_boundary_info();
179 
180  EquationSystems es(mesh);
181  LinearImplicitSystem &sys =
182  es.add_system<LinearImplicitSystem> ("PBCSys");
183 
184  // We need to change around BCIDs and set up the periodic BC first
186 
187  mesh.read("meshes/shark_tooth_tri6.xda.gz");
188 
189  // Add some BCIDs on top and bottom for our periodic boundary
190  for (const Elem * elem : mesh.active_element_ptr_range())
191  {
192  for (unsigned int s=0; s != 3; ++s)
193  if (!elem->neighbor_ptr(s))
194  {
195  unsigned v2 = (s+1)%3;
196  // Left-running edges are on bottom, because this xda
197  // has a flipped mesh
198  if (elem->point(s)(0) - elem->point(v2)(0) < -.1)
199  boundary.add_side(elem, s, top_id);
200 
201  // Right-running edges are on top
202  else if (elem->point(s)(0) - elem->point(v2)(0) > .1)
203  boundary.add_side(elem, s, bottom_id);
204 
205  // Vertical edges are Dirichlet
206  else
207  boundary.add_side(elem, s, side_id);
208  }
209  }
210 
211  PeriodicBoundary vert(RealVectorValue(0., 4.0));
212  vert.myboundary = bottom_id;
213  vert.pairedboundary = top_id;
215 
216  // Okay, *now* the mesh knows to save periodic neighbors
219 
220  const unsigned int u_var = sys.add_variable("u", fe_type);
222 
223  std::set<boundary_id_type> side_bdy { side_id };
224  std::vector<unsigned int> all_vars (1, u_var);
226  DirichletBoundary exact_bc(side_bdy, all_vars, exact_val);
227  sys.get_dof_map().add_dirichlet_boundary(exact_bc);
228 
229  es.init();
230 
231  sys.solve();
232 
233  ExodusII_IO(mesh).write_equation_systems("periodic.e", es);
234 
235  Parameters params;
236 
237  for (Real x = 0.1; x < 10; x += 0.2)
238  for (Real ya = -2.9; ya < 1; ya += 0.2)
239  {
240  // Integrate the sharktooth shape
241  const Real offset = 1-std::abs(std::fmod(x,2)-1);
242  const Real y = ya + offset;
243  Point p{x,y};
244  const Number exact = quadratic_solution(p,params,"","");
245  const Number approx = sys.point_value(0,p);
246  LIBMESH_ASSERT_NUMBERS_EQUAL(exact, approx,
247  TOLERANCE*TOLERANCE*20);
248  }
249  }
250 
251 
252  void testPeriodicLagrange2() { LOG_UNIT_TEST; testPeriodicBC(FEType(SECOND, LAGRANGE)); }
253 };
254 
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:196
Wrap a libMesh-style function pointer into a FunctionBase object.
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.
void add_periodic_boundary(const PeriodicBoundaryBase &periodic_boundary)
Adds a copy of the specified periodic boundary to the system.
This class provides the ability to map between arbitrary, user-defined strings and several data types...
Definition: parameters.h:67
VectorValue< Real > RealVectorValue
Useful typedefs to allow transparent switching between Real and Complex data types.
libMesh::Parallel::Communicator * TestCommWorld
Definition: driver.C:171
static constexpr Real TOLERANCE
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 resize(const unsigned int n)
Resize the vector.
Definition: dense_vector.h:396
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]...
Number component(unsigned int i, const Point &p, Real) override
const boundary_id_type side_id
This is the base class from which all geometric element types are derived.
Definition: elem.h:94
MeshBase & mesh
Number point_value(unsigned int var, const Point &p, const bool insist_on_success=true, const NumericVector< Number > *sol=nullptr) const
Definition: system.C:2421
NumericVector< Number > * rhs
The system matrix.
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
Order default_quadrature_order() const
Definition: fe_type.h:371
The libMesh namespace provides an interface to certain functionality in the library.
The definition of a periodic boundary.
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition: mesh_base.h:165
virtual void solve() override
Assembles & solves the linear system A*x=b.
CPPUNIT_TEST_SUITE_REGISTRATION(PeriodicBCTest)
const T_sys & get_system(std::string_view name) const
virtual std::unique_ptr< FunctionBase< Number > > clone() const
This is the MeshBase class.
Definition: mesh_base.h:75
void testPeriodicLagrange2()
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
void allow_remote_element_removal(bool allow)
If false is passed in then this mesh will no longer have remote elements deleted when being prepared ...
Definition: mesh_base.h:1212
boundary_id_type myboundary
The boundary ID of this boundary and its counterpart.
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.
int8_t boundary_id_type
Definition: id_types.h:51
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
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
The BoundaryInfo class contains information relevant to boundary conditions including storing faces...
Definition: boundary_info.h:57
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
void attach_assemble_function(void fptr(EquationSystems &es, const std::string &name))
Register a user function to use in assembling the system matrix and RHS.
Definition: system.C:2161
virtual void close()=0
Calls the NumericVector&#39;s internal assembly routines, ensuring that the values are consistent across ...
const boundary_id_type top_id
virtual void close()=0
Calls the SparseMatrix&#39;s internal assembly routines, ensuring that the values are consistent across p...
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
SparseMatrix< Number > * matrix
The system matrix.
void add_side(const dof_id_type elem, const unsigned short int side, const boundary_id_type id)
Add side side of element number elem with boundary id id to the boundary information data structure...
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().
Definition: dense_matrix.h:895
This class implements specific orders of Gauss quadrature.
void periodic_bc_test_poisson(EquationSystems &es, const std::string &)
virtual unsigned int size() const override final
Definition: dense_vector.h:104
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
virtual void init()
Initialize all the systems.
virtual void delete_remote_elements()
When supported, deletes all nonlocal elements of the mesh except for "ghosts" which touch a local ele...
Definition: mesh_base.h:253
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.
Base class for functors that can be evaluated at a point and (optionally) time.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
void testPeriodicBC(FEType fe_type)
const DofMap & get_dof_map() const
Definition: system.h:2374
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:39
const boundary_id_type bottom_id
Number quadratic_solution(const Point &p, const Parameters &, const std::string &, const std::string &)