libMesh
systems_of_equations_ex9.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 
19 
20 // <h1> Systems Example 9 - Linear elasticity problem with periodic constraints </h1>
21 //
22 // In this example we illustrate periodic constraints with a linear elasticity example.
23 // We consider a sector of a circular domain, and hence we must impose an "azimuthal"
24 // periodic condition, which entails a dof-transformation between the two periodic
25 // boundaries to account for the rotation of the coordinate system.
26 //
27 // The baseline code is from systems_of_equations_ex6, and we edit to use a different mesh
28 // and to impose the periodic boundary condition.
29 
30 
31 // C++ include files that we need
32 #include <iostream>
33 #include <algorithm>
34 #include <math.h>
35 
36 // libMesh includes
37 #include "libmesh/libmesh_config.h"
38 #include "libmesh/libmesh.h"
39 #include "libmesh/mesh.h"
40 #include "libmesh/mesh_generation.h"
41 #include "libmesh/exodusII_io.h"
42 #include "libmesh/gnuplot_io.h"
43 #include "libmesh/linear_implicit_system.h"
44 #include "libmesh/equation_systems.h"
45 #include "libmesh/fe.h"
46 #include "libmesh/quadrature_gauss.h"
47 #include "libmesh/dof_map.h"
48 #include "libmesh/sparse_matrix.h"
49 #include "libmesh/numeric_vector.h"
50 #include "libmesh/dense_matrix.h"
51 #include "libmesh/dense_submatrix.h"
52 #include "libmesh/dense_vector.h"
53 #include "libmesh/dense_subvector.h"
54 #include "libmesh/perf_log.h"
55 #include "libmesh/elem.h"
56 #include "libmesh/boundary_info.h"
57 #include "libmesh/zero_function.h"
58 #include "libmesh/dirichlet_boundaries.h"
59 #include "libmesh/string_to_enum.h"
60 #include "libmesh/getpot.h"
61 #include "libmesh/mesh_refinement.h"
62 #include "libmesh/solver_configuration.h"
63 #include "libmesh/petsc_linear_solver.h"
64 #include "libmesh/petsc_macro.h"
65 #include "libmesh/periodic_boundaries.h"
66 #include "libmesh/periodic_boundary.h"
67 #include "libmesh/enum_solver_package.h"
68 #include "libmesh/tensor_value.h"
69 #include "libmesh/vector_value.h"
70 
71 // Bring in everything from the libMesh namespace
72 using namespace libMesh;
73 
74 #ifdef LIBMESH_ENABLE_PERIODIC
75 // Here we define the azimuthal periodic boundary condition class.
76 // This class assumes that (u,v,w) are variables 0, 1, and 2 in
77 // the System, but this could be made more general if needed.
79 {
80 public:
85  Point center,
86  Point axis,
87  Real angle)
88  :
90  _center(center),
91  _axis(axis),
92  _theta(angle)
93  {
94  set_variable(0);
95  set_variable(1);
96  set_variable(2);
97  set_up_rotation_matrix();
98  }
99 
104  :
106  _center(o._center),
107  _axis(o._axis),
108  _theta(o._theta)
109  {
110  if (t == INVERSE)
111  {
112  std::swap(myboundary, pairedboundary);
113  _theta *= -1.0;
114  }
115 
116  set_variable(0);
117  set_variable(1);
118  set_variable(2);
119  set_up_rotation_matrix();
120  }
121 
125  virtual ~AzimuthalPeriodicBoundary() = default;
126 
134  {
135  // Formula for rotation matrix about an axis is given on wikipedia:
136  // en.wikipedia.org/wiki/Rotation_matrix
137  // We rotate by angle theta about the axis defined by u, which is a
138  // unit vector in the direction of _axis.
139  // Note: this example already requires LIBMESH_DIM==3 so we don't
140  // explicitly check for that here.
141  Point u = _axis.unit();
142  Real u_x = u(0);
143  Real u_y = u(1);
144  Real u_z = u(2);
145  Real cost = std::cos(_theta);
146  Real sint = std::sin(_theta);
147  _R = RealTensorValue
148  (cost + u_x*u_x*(1.0 - cost), u_x*u_y*(1.0 - cost) - u_z*sint, u_x*u_z*(1.0 - cost) + u_y*sint,
149  u_y*u_x*(1.0 - cost) + u_z*sint, cost + u_y*u_y*(1.0 - cost), u_y*u_z*(1.0 - cost) - u_x*sint,
150  u_z*u_x*(1.0 - cost) - u_y*sint, u_z*u_y*(1.0 - cost) + u_x*sint, cost + u_z*u_z*(1.0 - cost));
151 
152  // For generality, PeriodicBoundaryBase::set_transformation_matrix() takes an
153  // (n_vars * n_vars) DenseMatrix which we construct on the fly.
154  this->set_transformation_matrix
155  (DenseMatrix<Real> (3, 3, {_R(0,0), _R(0,1), _R(0,2), _R(1,0), _R(1,1), _R(1,2), _R(2,0), _R(2,1), _R(2,2)}));
156  }
157 
163  virtual Point get_corresponding_pos(const Point & pt) const override
164  {
165  // Note that since _theta defines the angle from "paired boundary" to
166  // "my boundary", and we want the inverse of that here, we multiply
167  // by the transpose of the transformation matrix below.
168  return _R.left_multiply(pt - _center) + _center;
169  }
170 
176  virtual std::unique_ptr<PeriodicBoundaryBase> clone(TransformationType t = FORWARD) const override
177  {
178  return std::make_unique<AzimuthalPeriodicBoundary>(*this, t);
179  }
180 
181 private:
182 
183  // Define the properties needed for the azimuthal periodic boundary.
184  // Note that _theta specifies the angle from "paired boundary" to
185  // "my boundary".
190 };
191 
192 #endif // LIBMESH_ENABLE_PERIODIC
193 
194 class LinearElasticity : public System::Assembly
195 {
196 private:
197  EquationSystems & es;
198 
199 public:
200 
202  es(es_in)
203  {}
204 
208  Real kronecker_delta(unsigned int i,
209  unsigned int j)
210  {
211  return i == j ? 1. : 0.;
212  }
213 
217  Real elasticity_tensor(unsigned int i,
218  unsigned int j,
219  unsigned int k,
220  unsigned int l)
221  {
222  // Hard code material parameters for the sake of simplicity
223  const Real poisson_ratio = 0.3;
224  const Real young_modulus = 1.;
225 
226  // Define the Lame constants
227  const Real lambda_1 = (young_modulus*poisson_ratio)/((1.+poisson_ratio)*(1.-2.*poisson_ratio));
228  const Real lambda_2 = young_modulus/(2.*(1.+poisson_ratio));
229 
230  return lambda_1 * kronecker_delta(i, j) * kronecker_delta(k, l) +
231  lambda_2 * (kronecker_delta(i, k) * kronecker_delta(j, l) + kronecker_delta(i, l) * kronecker_delta(j, k));
232  }
233 
237  void assemble()
238  {
239  const MeshBase & mesh = es.get_mesh();
240 
241  const unsigned int dim = mesh.mesh_dimension();
242 
243  LinearImplicitSystem & system = es.get_system<LinearImplicitSystem>("Elasticity");
244 
245  const unsigned int u_var = system.variable_number ("u");
246 
247  const DofMap & dof_map = system.get_dof_map();
248  FEType fe_type = dof_map.variable_type(u_var);
249  std::unique_ptr<FEBase> fe (FEBase::build(dim, fe_type));
250  QGauss qrule (dim, fe_type.default_quadrature_order());
251  fe->attach_quadrature_rule (&qrule);
252 
253  std::unique_ptr<FEBase> fe_face (FEBase::build(dim, fe_type));
254  QGauss qface(dim-1, fe_type.default_quadrature_order());
255  fe_face->attach_quadrature_rule (&qface);
256 
257  const std::vector<Real> & JxW = fe->get_JxW();
258  const std::vector<std::vector<Real>> & phi = fe->get_phi();
259  const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
260 
262  DenseSubMatrix<Number> Ke_var[3][3] =
263  {
267  };
268 
270 
271  DenseSubVector<Number> Fe_var[3] =
275 
276  std::vector<dof_id_type> dof_indices;
277  std::vector<std::vector<dof_id_type>> dof_indices_var(3);
278 
279  SparseMatrix<Number> & matrix = system.get_system_matrix();
280 
281  for (const auto & elem : mesh.active_local_element_ptr_range())
282  {
283  dof_map.dof_indices (elem, dof_indices);
284  for (unsigned int var=0; var<3; var++)
285  dof_map.dof_indices (elem, dof_indices_var[var], var);
286 
287  const unsigned int n_dofs = dof_indices.size();
288  const unsigned int n_var_dofs = dof_indices_var[0].size();
289 
290  fe->reinit (elem);
291 
292  Ke.resize (n_dofs, n_dofs);
293  for (unsigned int var_i=0; var_i<3; var_i++)
294  for (unsigned int var_j=0; var_j<3; var_j++)
295  Ke_var[var_i][var_j].reposition (var_i*n_var_dofs, var_j*n_var_dofs, n_var_dofs, n_var_dofs);
296 
297  Fe.resize (n_dofs);
298  for (unsigned int var=0; var<3; var++)
299  Fe_var[var].reposition (var*n_var_dofs, n_var_dofs);
300 
301  for (unsigned int qp=0; qp<qrule.n_points(); qp++)
302  {
303  // assemble \int_Omega C_ijkl u_k,l v_i,j \dx
304  for (unsigned int dof_i=0; dof_i<n_var_dofs; dof_i++)
305  for (unsigned int dof_j=0; dof_j<n_var_dofs; dof_j++)
306  for (unsigned int i=0; i<3; i++)
307  for (unsigned int j=0; j<3; j++)
308  for (unsigned int k=0; k<3; k++)
309  for (unsigned int l=0; l<3; l++)
310  Ke_var[i][k](dof_i,dof_j) +=
311  JxW[qp] * elasticity_tensor(i,j,k,l) * dphi[dof_j][qp](l) * dphi[dof_i][qp](j);
312 
313  // assemble \int_Omega f_i v_i \dx
314  // The mesh for this example has two subdomains with ids 1
315  // and 101, and the forcing function is different on each.
316  auto f_vec = (elem->subdomain_id() == 101 ?
317  VectorValue<Number>(1., 1., 0.) :
318  VectorValue<Number>(0.36603, 1.36603, 0.));
319 
320  for (unsigned int dof_i=0; dof_i<n_var_dofs; dof_i++)
321  for (unsigned int i=0; i<3; i++)
322  Fe_var[i](dof_i) += JxW[qp] * (f_vec(i) * phi[dof_i][qp]);
323  }
324 
325  dof_map.constrain_element_matrix_and_vector (Ke, Fe, dof_indices);
326 
327  matrix.add_matrix (Ke, dof_indices);
328  system.rhs->add_vector (Fe, dof_indices);
329  }
330  }
331 };
332 
333 
334 // Begin the main program.
335 int main (int argc, char ** argv)
336 {
337  // Initialize libMesh and any dependent libraries
338  LibMeshInit init (argc, argv);
339 
340  // This example requires a linear solver package.
341  libmesh_example_requires(libMesh::default_solver_package() != INVALID_SOLVER_PACKAGE,
342  "--enable-petsc, --enable-trilinos, or --enable-eigen");
343 
344 #ifndef LIBMESH_HAVE_EXODUS_API
345  // example requires ExodusII to load the mesh
346  libmesh_example_requires(false, "--enable-exodus");
347 #endif
348 
349  // We use Dirichlet and Periodic boundary conditions here
350 #ifndef LIBMESH_ENABLE_DIRICHLET
351  libmesh_example_requires(false, "--enable-dirichlet");
352 #endif
353 #ifndef LIBMESH_ENABLE_PERIODIC
354  libmesh_example_requires(false, "--enable-periodic");
355 #endif
356 
357  // Initialize the cantilever mesh
358  const unsigned int dim = 3;
359 
360  // Make sure libMesh was compiled for 3D
361  libmesh_example_requires(dim == LIBMESH_DIM, "3D support");
362 
363  // Make sure libMesh has normal boundary id sizes
364  libmesh_example_requires(sizeof(boundary_id_type) > 1, "boundary_id_size > 1");
365 
366 #if LIBMESH_BOUNDARY_ID_BYTES > 1
367  // Create a 3D mesh distributed across the default MPI communicator.
368  Mesh mesh(init.comm());
369 
370  // Create an equation systems object.
371  EquationSystems equation_systems (mesh);
372 
373  // Declare the system and its variables.
374  // Create a system named "Elasticity"
375  LinearImplicitSystem & system =
376  equation_systems.add_system<LinearImplicitSystem> ("Elasticity");
377 
378 #ifdef LIBMESH_ENABLE_PERIODIC
379  // Add two azimuthal periodic boundaries on two adjacent domains.
380  // We do this to show that the periodic boundary condition that
381  // we impose leads to a continuous solution across adjacent domains.
382  //
383  // We add the periodic boundaries *before* reading the Mesh, so
384  // that periodic neighbors will be retained when a DistributedMesh
385  // is distributed.
386  //
387  // The angle specified below defines the mapping
388  // from "pairedboundary" to "myboundary".
389  {
390  Point center(0., 0., 0.);
391  Point axis(0., 0., 1.);
392  Real angle = 2*libMesh::pi/12.0;
393  AzimuthalPeriodicBoundary periodic_bc(center, axis, angle);
394  periodic_bc.myboundary = 301;
395  periodic_bc.pairedboundary = 302;
396  system.get_dof_map().add_periodic_boundary(periodic_bc);
397  }
398  {
399  Point center(0., 0., 0.);
400  Point axis(0., 0., 1.);
401  Real angle = 2*libMesh::pi/12.0;
402  AzimuthalPeriodicBoundary periodic_bc(center, axis, angle);
403  periodic_bc.myboundary = 401;
404  periodic_bc.pairedboundary = 402;
405  system.get_dof_map().add_periodic_boundary(periodic_bc);
406  }
407 #endif // LIBMESH_ENABLE_PERIODIC
408 
409  mesh.read("systems_of_equations_ex9.exo");
410 
411  GetPot input(argc, argv);
412  const unsigned int n_refinements = input("n_refinements", 0);
413  // Skip adaptive runs on a non-adaptive libMesh build
414 #ifndef LIBMESH_ENABLE_AMR
415  libmesh_example_requires(n_refinements==0, "--enable-amr");
416 #else
417  MeshRefinement mesh_refinement(mesh);
418  mesh_refinement.uniformly_refine(n_refinements);
419 #endif
420 
421  // Print information about the mesh to the screen.
422  mesh.print_info();
423 
424  // Add three displacement variables, u and v, to the system
425  unsigned int u_var = system.add_variable("u", FIRST, LAGRANGE);
426  unsigned int v_var = system.add_variable("v", FIRST, LAGRANGE);
427  unsigned int w_var = system.add_variable("w", FIRST, LAGRANGE);
428 
429  LinearElasticity le(equation_systems);
430  system.attach_assemble_object(le);
431 
432  ZeroFunction<> zf;
433 
434 #ifdef LIBMESH_ENABLE_DIRICHLET
435  DirichletBoundary clamped_bc({300,400}, {u_var,v_var,w_var}, zf);
436  system.get_dof_map().add_dirichlet_boundary(clamped_bc);
437 #endif
438 
439  // Initialize the data structures for the equation system.
440  equation_systems.init();
441 
442  // Print information about the system to the screen.
443  equation_systems.print_info();
444 
445  // Solve the system
446  system.solve();
447 
448  // Plot the solution
449 #ifdef LIBMESH_HAVE_EXODUS_API
450 
451  ExodusII_IO (mesh).write_equation_systems("solution.exo",
452  equation_systems);
453 
454 #endif // #ifdef LIBMESH_HAVE_EXODUS_API
455 #endif // #if LIBMESH_BOUNDARY_ID_BYTES > 1
456 
457  // All done.
458  return 0;
459 }
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:196
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.
Real elasticity_tensor(unsigned int i, unsigned int j, unsigned int k, unsigned int l)
Definition: assembly.C:43
void add_periodic_boundary(const PeriodicBoundaryBase &periodic_boundary)
Adds a copy of the specified periodic boundary to the system.
ConstFunction that simply returns 0.
Definition: zero_function.h:38
void assemble()
Assemble the system matrix and right-hand side vector.
virtual std::unique_ptr< PeriodicBoundaryBase > clone(TransformationType t=FORWARD) const override
If we want the DofMap to be able to make copies of references and store them in the underlying map...
void constrain_element_matrix_and_vector(DenseMatrix< Number > &matrix, DenseVector< Number > &rhs, std::vector< dof_id_type > &elem_dofs, bool asymmetric_constraint_rows=true) const
Constrains the element matrix and vector.
Definition: dof_map.h:2330
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition: dof_map.C:2164
Real kronecker_delta(unsigned int i, unsigned int j)
Definition: assembly.C:37
unsigned int dim
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 ...
AzimuthalPeriodicBoundary(Point center, Point axis, Real angle)
Constructor.
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]...
const FEType & variable_type(const unsigned int c) const
Definition: dof_map.h:2220
LinearElasticity(EquationSystems &es_in)
MeshBase & mesh
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
This class defines a vector in LIBMESH_DIM dimensional Real or Complex space.
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.
Abstract base class to be used for system assembly.
Definition: system.h:155
virtual void solve() override
Assembles & solves the linear system A*x=b.
const T_sys & get_system(std::string_view name) const
This is the MeshBase class.
Definition: mesh_base.h:75
unsigned int variable_number(std::string_view var) const
Definition: system.C:1609
Defines a dense subvector for use in finite element computations.
SolverPackage default_solver_package()
Definition: libmesh.C:1117
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:179
TensorValue< Real > RealTensorValue
Useful typedefs to allow transparent switching between Real and Complex data types.
virtual Point get_corresponding_pos(const Point &pt) const override
This function should be overridden by derived classes to define how one finds corresponding nodes on ...
TypeVector< T > unit() const
Definition: type_vector.h:1104
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.
Implements (adaptive) mesh refinement algorithms for a MeshBase.
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
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
Defines a dense submatrix for use in Finite Element-type computations.
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.
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
Real elasticity_tensor(unsigned int i, unsigned int j, unsigned int k, unsigned int l)
Evaluate the fourth order tensor (C_ijkl) that relates stress to strain.
void attach_assemble_object(Assembly &assemble)
Register a user object to use in assembling the system matrix and RHS.
Definition: system.C:2178
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
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
AzimuthalPeriodicBoundary(const AzimuthalPeriodicBoundary &o, TransformationType t=FORWARD)
Copy constructor, with option for the copy to represent an inverse transformation.
This class implements specific orders of Gauss quadrature.
unsigned int mesh_dimension() const
Definition: mesh_base.C:372
int main(int argc, char **argv)
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
The base class for defining periodic boundaries.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
Real kronecker_delta(unsigned int i, unsigned int j)
Kronecker delta function.
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 SparseMatrix< Number > & get_system_matrix() const
This class defines a tensor in LIBMESH_DIM dimensional Real or Complex space.
void set_up_rotation_matrix()
Computes and stores the rotation matrix for this transformation, and then calls the base class API to...
const Real pi
.
Definition: libmesh.h:299
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.