libMesh
heatsystem.C
Go to the documentation of this file.
1 #include "heatsystem.h"
2 
3 #include "libmesh/dirichlet_boundaries.h"
4 #include "libmesh/dof_map.h"
5 #include "libmesh/fe_base.h"
6 #include "libmesh/fe_interface.h"
7 #include "libmesh/fem_context.h"
8 #include "libmesh/getpot.h"
9 #include "libmesh/mesh.h"
10 #include "libmesh/quadrature.h"
11 #include "libmesh/string_to_enum.h"
12 #include "libmesh/zero_function.h"
13 #include "libmesh/elem.h"
14 
15 using namespace libMesh;
16 
18 {
19  T_var = this->add_variable("T", static_cast<Order>(_fe_order),
20  Utility::string_to_enum<FEFamily>(_fe_family));
21 
22 #ifdef LIBMESH_ENABLE_DIRICHLET
23  const unsigned int dim = this->get_mesh().mesh_dimension();
24 
25  // Add dirichlet boundaries on all but the boundary element side
26  const boundary_id_type all_ids[6] = {0, 1, 2, 3, 4, 5};
27  std::set<boundary_id_type> nonyplus_bdys(all_ids, all_ids+(dim*2));
28  const boundary_id_type yplus_id = (dim == 3) ? 3 : 2;
29  nonyplus_bdys.erase(yplus_id);
30 
31  std::vector<unsigned int> T_only(1, T_var);
33 
34  // Most DirichletBoundary users will want to supply a "locally
35  // indexed" functor
36  this->get_dof_map().add_dirichlet_boundary
37  (DirichletBoundary (nonyplus_bdys, T_only, zero, LOCAL_VARIABLE_ORDER));
38 #endif // LIBMESH_ENABLE_DIRICHLET
39 
40  // Do the parent's initialization after variables are defined
42 
43  // The temperature is evolving, with a first-order time derivative
44  this->time_evolving(T_var, 1);
45 }
46 
47 
48 
50 {
51  FEMContext & c = cast_ref<FEMContext &>(context);
52 
53  const std::set<unsigned char> & elem_dims =
54  c.elem_dimensions();
55 
56  for (std::set<unsigned char>::const_iterator dim_it =
57  elem_dims.begin(); dim_it != elem_dims.end(); ++dim_it)
58  {
59  const unsigned char dim = *dim_it;
60 
61  FEBase * fe = nullptr;
62 
63  c.get_element_fe(T_var, fe, dim);
64 
65  fe->get_JxW(); // For integration
66  fe->get_dphi(); // For bilinear form
67  fe->get_xyz(); // For forcing
68  fe->get_phi(); // For forcing
69  }
70 
71  FEMSystem::init_context(context);
72 }
73 
74 
75 bool HeatSystem::element_time_derivative (bool request_jacobian,
76  DiffContext & context)
77 {
78  FEMContext & c = cast_ref<FEMContext &>(context);
79 
80  const unsigned int mesh_dim =
82 
83  // First we get some references to cell-specific data that
84  // will be used to assemble the linear system.
85  const unsigned short dim = c.get_elem().dim();
86  FEBase * fe = nullptr;
87  c.get_element_fe(T_var, fe, dim);
88 
89  // Element Jacobian * quadrature weights for interior integration
90  const std::vector<Real> & JxW = fe->get_JxW();
91 
92  const std::vector<Point> & xyz = fe->get_xyz();
93 
94  const std::vector<std::vector<Real>> & phi = fe->get_phi();
95 
96  const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
97 
98  // The number of local degrees of freedom in each variable
99  const unsigned int n_T_dofs = c.n_dof_indices(T_var);
100 
101  // The subvectors and submatrices we need to fill:
102  DenseSubMatrix<Number> & K = c.get_elem_jacobian(T_var, T_var);
104 
105  // Now we will build the element Jacobian and residual.
106  // Constructing the residual requires the solution and its
107  // gradient from the previous timestep. This must be
108  // calculated at each quadrature point by summing the
109  // solution degree-of-freedom values by the appropriate
110  // weight functions.
111  unsigned int n_qpoints = c.get_element_qrule().n_points();
112 
113  for (unsigned int qp=0; qp != n_qpoints; qp++)
114  {
115  // Compute the solution gradient at the Newton iterate
116  Gradient grad_T = c.interior_gradient(T_var, qp);
117 
118  const Number k = _k[dim];
119 
120  const Point & p = xyz[qp];
121 
122  // solution + laplacian depend on problem dimension
123  const Number u_exact = (mesh_dim == 2) ?
124  std::sin(libMesh::pi*p(0)) * std::sin(libMesh::pi*p(1)) :
125  std::sin(libMesh::pi*p(0)) * std::sin(libMesh::pi*p(1)) *
126  std::sin(libMesh::pi*p(2));
127 
128  // Only apply forcing to interior elements
129  const Number forcing = (dim == mesh_dim) ?
130  -k * u_exact * (dim * libMesh::pi * libMesh::pi) : 0;
131 
132  const Number JxWxNK = JxW[qp] * -k;
133 
134  for (unsigned int i=0; i != n_T_dofs; i++)
135  F(i) += JxWxNK * (grad_T * dphi[i][qp] + forcing * phi[i][qp]);
136  if (request_jacobian)
137  {
138  const Number JxWxNKxD = JxWxNK *
140 
141  for (unsigned int i=0; i != n_T_dofs; i++)
142  for (unsigned int j=0; j != n_T_dofs; ++j)
143  K(i,j) += JxWxNKxD * (dphi[i][qp] * dphi[j][qp]);
144  }
145  } // end of the quadrature point qp-loop
146 
147  return request_jacobian;
148 }
libMesh::Number
Real Number
Definition: libmesh_common.h:195
libMesh::DiffContext::get_elem_residual
const DenseVector< Number > & get_elem_residual() const
Const accessor for element residual.
Definition: diff_context.h:249
libMesh::FEMContext::get_element_qrule
const QBase & get_element_qrule() const
Accessor for element interior quadrature rule for the dimension of the current _elem.
Definition: fem_context.h:790
libMesh::pi
const Real pi
.
Definition: libmesh.h:237
libMesh::FEMSystem::init_context
virtual void init_context(DiffContext &) override
Definition: fem_system.C:1338
libMesh::FEAbstract::get_xyz
const std::vector< Point > & get_xyz() const
Definition: fe_abstract.h:237
HeatSystem::init_context
virtual void init_context(DiffContext &context)
Definition: heatsystem.C:79
libMesh::FEMSystem::init_data
virtual void init_data() override
Initializes the member data fields associated with the system, so that, e.g., assemble() may be used.
Definition: fem_system.C:843
libMesh::DenseSubMatrix
Defines a dense submatrix for use in Finite Element-type computations.
Definition: dense_submatrix.h:45
libMesh::QBase::n_points
unsigned int n_points() const
Definition: quadrature.h:126
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::Elem::dim
virtual unsigned short dim() const =0
libMesh::DiffContext::get_elem_solution_derivative
Real get_elem_solution_derivative() const
The derivative of the current elem_solution w.r.t.
Definition: diff_context.h:436
libMesh::FEGenericBase
This class forms the foundation from which generic finite elements may be derived.
Definition: exact_error_estimator.h:39
libMesh::DiffContext::get_system
const System & get_system() const
Accessor for associated system.
Definition: diff_context.h:105
libMesh::FEAbstract::get_JxW
const std::vector< Real > & get_JxW() const
Definition: fe_abstract.h:244
libMesh::FEMContext::get_elem
const Elem & get_elem() const
Accessor for current Elem object.
Definition: fem_context.h:896
libMesh::MeshBase::mesh_dimension
unsigned int mesh_dimension() const
Definition: mesh_base.C:135
libMesh::FEGenericBase::get_dphi
const std::vector< std::vector< OutputGradient > > & get_dphi() const
Definition: fe_base.h:214
libMesh::boundary_id_type
int8_t boundary_id_type
Definition: id_types.h:51
dim
unsigned int dim
Definition: adaptivity_ex3.C:113
libMesh::VectorValue< Number >
libMesh::zero
const Number zero
.
Definition: libmesh.h:243
libMesh::LOCAL_VARIABLE_ORDER
Definition: dirichlet_boundaries.h:62
HeatSystem::init_data
virtual void init_data()
Initializes the member data fields associated with the system, so that, e.g., assemble() may be used.
Definition: heatsystem.C:36
libMesh::System::get_mesh
const MeshBase & get_mesh() const
Definition: system.h:2083
libMesh::Point
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
HeatSystem::element_time_derivative
virtual bool element_time_derivative(bool request_jacobian, DiffContext &context)
Adds the time derivative contribution on elem to elem_residual.
Definition: heatsystem.C:112
libMesh::DiffContext::n_dof_indices
unsigned int n_dof_indices() const
Total number of dof indices on the element.
Definition: diff_context.h:399
libMesh::ZeroFunction
ConstFunction that simply returns 0.
Definition: zero_function.h:36
libMesh::DiffContext
This class provides all data required for a physics package (e.g.
Definition: diff_context.h:55
libMesh::DenseSubVector< Number >
libMesh::FEMContext::get_element_fe
void get_element_fe(unsigned int var, FEGenericBase< OutputShape > *&fe) const
Accessor for interior finite element object for variable var for the largest dimension in the mesh.
Definition: fem_context.h:275
libMesh::FEMContext::interior_gradient
Gradient interior_gradient(unsigned int var, unsigned int qp) const
Definition: fem_context.C:426
libMesh::DiffContext::get_elem_jacobian
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
Definition: diff_context.h:283
libMesh::DirichletBoundary
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
Definition: dirichlet_boundaries.h:88
libMesh::FEMContext::elem_dimensions
const std::set< unsigned char > & elem_dimensions() const
Definition: fem_context.h:938
libMesh::FEGenericBase::get_phi
const std::vector< std::vector< OutputShape > > & get_phi() const
Definition: fe_base.h:206
libMesh::FEMContext
This class provides all data required for a physics package (e.g.
Definition: fem_context.h:62