libMesh
L-shaped.C
Go to the documentation of this file.
1 /* This is where we define the assembly of the Laplace system */
2 
3 // General libMesh includes
4 #include "libmesh/getpot.h"
5 #include "libmesh/fe_base.h"
6 #include "libmesh/quadrature.h"
7 #include "libmesh/string_to_enum.h"
8 #include "libmesh/parallel.h"
9 #include "libmesh/fem_context.h"
10 
11 // Local includes
12 #include "L-shaped.h"
13 
14 // Bring in everything from the libMesh namespace
15 using namespace libMesh;
16 
18 {
19  unsigned int T_var =
20  this->add_variable ("T", static_cast<Order>(_fe_order),
21  Utility::string_to_enum<FEFamily>(_fe_family));
22 
23  GetPot infile("l-shaped.in");
24  parameters.push_back(infile("alpha_1", 1.0));
25  parameters.push_back(infile("alpha_2", 1.0));
26 
27  // Do the parent's initialization after variables are defined
29 
30  // The temperature is evolving, with a first order time derivative
31  this->time_evolving(T_var, 1);
32 }
33 
35 {
36  FEMContext & c = cast_ref<FEMContext &>(context);
37 
38  // Now make sure we have requested all the data
39  // we need to build the linear system.
40  FEBase * elem_fe = libmesh_nullptr;
41  c.get_element_fe(0, elem_fe);
42  elem_fe->get_JxW();
43  elem_fe->get_dphi();
44 
45  FEBase * side_fe = libmesh_nullptr;
46  c.get_side_fe(0, side_fe);
47  side_fe->get_JxW();
48  side_fe->get_phi();
49  side_fe->get_xyz();
50 }
51 
52 #define optassert(X) {if (!(X)) libmesh_error_msg("Assertion " #X " failed.");}
53 
54 // Assemble the element contributions to the stiffness matrix
55 bool LaplaceSystem::element_time_derivative (bool request_jacobian,
56  DiffContext & context)
57 {
58  // Are the jacobians specified analytically ?
59  bool compute_jacobian = request_jacobian && _analytic_jacobians;
60 
61  FEMContext & c = cast_ref<FEMContext &>(context);
62 
63  // First we get some references to cell-specific data that
64  // will be used to assemble the linear system.
65  FEBase * elem_fe = libmesh_nullptr;
66  c.get_element_fe(0, elem_fe);
67 
68  // Element Jacobian * quadrature weights for interior integration
69  const std::vector<Real> & JxW = elem_fe->get_JxW();
70 
71  // Element basis functions
72  const std::vector<std::vector<RealGradient>> & dphi = elem_fe->get_dphi();
73 
74  // The number of local degrees of freedom in each variable
75  const unsigned int n_T_dofs = c.get_dof_indices(0).size();
76 
77  // The subvectors and submatrices we need to fill:
80 
81  // Now we will build the element Jacobian and residual.
82  // Constructing the residual requires the solution and its
83  // gradient from the previous timestep. This must be
84  // calculated at each quadrature point by summing the
85  // solution degree-of-freedom values by the appropriate
86  // weight functions.
87  unsigned int n_qpoints = c.get_element_qrule().n_points();
88 
89  for (unsigned int qp=0; qp != n_qpoints; qp++)
90  {
91  // Compute the solution gradient at the Newton iterate
92  Gradient grad_T = c.interior_gradient(0, qp);
93 
94  // The residual contribution from this element
95  for (unsigned int i=0; i != n_T_dofs; i++)
96  F(i) += JxW[qp] * (parameters[0] + (2.*parameters[1])) * (grad_T * dphi[i][qp]);
97  if (compute_jacobian)
98  for (unsigned int i=0; i != n_T_dofs; i++)
99  for (unsigned int j=0; j != n_T_dofs; ++j)
100  // The analytic jacobian
101  K(i,j) += JxW[qp] * (parameters[0] + (2.*parameters[1])) * (dphi[i][qp] * dphi[j][qp]);
102  } // end of the quadrature point qp-loop
103 
104  return compute_jacobian;
105 }
106 
107 // Set Dirichlet bcs, side contributions to global stiffness matrix
108 bool LaplaceSystem::side_constraint (bool request_jacobian,
109  DiffContext & context)
110 {
111  // Are the jacobians specified analytically ?
112  bool compute_jacobian = request_jacobian && _analytic_jacobians;
113 
114  FEMContext & c = cast_ref<FEMContext &>(context);
115 
116  // First we get some references to cell-specific data that
117  // will be used to assemble the linear system.
118  FEBase * side_fe = libmesh_nullptr;
119  c.get_side_fe(0, side_fe);
120 
121  // Element Jacobian * quadrature weights for interior integration
122  const std::vector<Real> & JxW = side_fe->get_JxW();
123 
124  // Side basis functions
125  const std::vector<std::vector<Real>> & phi = side_fe->get_phi();
126 
127  // Side Quadrature points
128  const std::vector<Point > & qside_point = side_fe->get_xyz();
129 
130  // The number of local degrees of freedom in each variable
131  const unsigned int n_T_dofs = c.get_dof_indices(0).size();
132 
133  // The subvectors and submatrices we need to fill:
136 
137  unsigned int n_qpoints = c.get_side_qrule().n_points();
138 
139  const Real penalty = 1./(TOLERANCE*TOLERANCE);
140 
141  for (unsigned int qp=0; qp != n_qpoints; qp++)
142  {
143  // Compute the solution at the old Newton iterate
144  Number T = c.side_value(0, qp);
145 
146  // We get the Dirichlet bcs from the exact solution
147  Number u_dirichlet = exact_solution (qside_point[qp]);
148 
149  // The residual from the boundary terms, penalize non-zero temperature
150  for (unsigned int i=0; i != n_T_dofs; i++)
151  F(i) += JxW[qp] * penalty * (T - u_dirichlet) * phi[i][qp];
152  if (compute_jacobian)
153  for (unsigned int i=0; i != n_T_dofs; i++)
154  for (unsigned int j=0; j != n_T_dofs; ++j)
155  // The analytic jacobian
156  K(i,j) += JxW[qp] * penalty * phi[i][qp] * phi[j][qp];
157 
158  } // end of the quadrature point qp-loop
159 
160  return compute_jacobian;
161 }
162 
163 // The exact solution to the singular problem,
164 // u_exact = r^(2/3)*sin(2*theta/3). We use this to set the Dirichlet boundary conditions
166 {
167  const Real x1 = p(0);
168  const Real x2 = p(1);
169 
170  Real theta = atan2(x2, x1);
171 
172  // Make sure 0 <= theta <= 2*pi
173  if (theta < 0)
174  theta += 2. * libMesh::pi;
175 
176  return (parameters[0] + (2.*parameters[1])) * pow(x1*x1 + x2*x2, 1./3.)*sin(2./3.*theta);
177 }
virtual void init_context(DiffContext &context)
Definition: L-shaped.C:34
This class provides all data required for a physics package (e.g.
Definition: diff_context.h:54
Number(* exact_solution)(const Point &p, const Parameters &, const std::string &, const std::string &)
const QBase & get_side_qrule() const
Accessor for element side quadrature rule for the dimension of the current _elem. ...
Definition: fem_context.h:772
const std::vector< dof_id_type > & get_dof_indices() const
Accessor for element dof indices.
Definition: diff_context.h:366
const class libmesh_nullptr_t libmesh_nullptr
virtual bool element_time_derivative(bool request_jacobian, DiffContext &context)
Adds the time derivative contribution on elem to elem_residual.
Definition: L-shaped.C:58
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:262
static const Real TOLERANCE
The libMesh namespace provides an interface to certain functionality in the library.
const DenseVector< Number > & get_elem_residual() const
Const accessor for element residual.
Definition: diff_context.h:248
Defines a dense subvector for use in finite element computations.
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
Definition: diff_context.h:282
const std::vector< Real > & get_JxW() const
Definition: fe_abstract.h:237
Defines a dense submatrix for use in Finite Element-type computations.
const std::vector< std::vector< OutputShape > > & get_phi() const
Definition: fe_base.h:208
This class provides all data required for a physics package (e.g.
Definition: fem_context.h:61
Gradient interior_gradient(unsigned int var, unsigned int qp) const
Definition: fem_context.C:381
double pow(double a, int b)
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Number side_value(unsigned int var, unsigned int qp) const
Definition: fem_context.C:575
const QBase & get_element_qrule() const
Accessor for element interior quadrature rule for the dimension of the current _elem.
Definition: fem_context.h:765
LaplaceExactSolution exact_solution
virtual void init_data() libmesh_override
Initializes the member data fields associated with the system, so that, e.g., assemble() may be used...
Definition: fem_system.C:845
void get_side_fe(unsigned int var, FEGenericBase< OutputShape > *&fe) const
Accessor for edge/face (2D/3D) finite element object for variable var for the largest dimension in th...
Definition: fem_context.h:299
virtual bool side_constraint(bool request_jacobian, DiffContext &context)
Adds the constraint contribution on side of elem to elem_residual.
Definition: L-shaped.C:111
A Point defines a location in LIBMESH_DIM dimensional Real space.
Definition: point.h:38
virtual void init_data()
Initializes the member data fields associated with the system, so that, e.g., assemble() may be used...
Definition: L-shaped.C:17
unsigned int n_points() const
Definition: quadrature.h:113
const std::vector< Point > & get_xyz() const
Definition: fe_abstract.h:230
This class forms the foundation from which generic finite elements may be derived.
const Real pi
.
Definition: libmesh.h:172