libMesh
L2system.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 #include "L2system.h"
19 
20 #include "libmesh/fe_base.h"
21 #include "libmesh/fe_interface.h"
22 #include "libmesh/fem_context.h"
23 #include "libmesh/getpot.h"
24 #include "libmesh/mesh.h"
25 #include "libmesh/quadrature.h"
26 #include "libmesh/string_to_enum.h"
27 
28 using namespace libMesh;
29 
31 {
32  for (auto & pr : input_contexts)
33  delete pr.second;
34 }
35 
37 {
38  this->add_variable ("u", static_cast<Order>(_fe_order),
39  Utility::string_to_enum<FEFamily>(_fe_family));
40 
41  // Do the parent's initialization after variables are defined
43 }
44 
45 
46 
48 {
49  FEMContext & c = cast_ref<FEMContext &>(context);
50 
51  // Now make sure we have requested all the data
52  // we need to build the L2 system.
53  c.get_element_fe(0)->get_JxW();
54  c.get_element_fe(0)->get_phi();
55  c.get_element_fe(0)->get_xyz();
56 
57  // Build a corresponding context for the input system if we haven't
58  // already
59  FEMContext *& input_context = input_contexts[&c];
60  if (!input_context)
61  {
62  libmesh_assert(input_system);
63  input_context = new FEMContext(*input_system);
64 
65  libmesh_assert(goal_func.get());
66  goal_func->init_context(*input_context);
67  }
68 
69  FEMSystem::init_context(context);
70 }
71 
72 
73 bool L2System::element_time_derivative (bool request_jacobian,
74  DiffContext & context)
75 {
76  FEMContext & c = cast_ref<FEMContext &>(context);
77 
78  // First we get some references to cell-specific data that
79  // will be used to assemble the linear system.
80 
81  // Element Jacobian * quadrature weights for interior integration
82  const std::vector<Real> & JxW = c.get_element_fe(0)->get_JxW();
83 
84  const std::vector<std::vector<Real>> & phi = c.get_element_fe(0)->get_phi();
85 
86  const std::vector<Point> & xyz = c.get_element_fe(0)->get_xyz();
87 
88  // The number of local degrees of freedom in each variable
89  const unsigned int n_u_dofs = c.n_dof_indices(0);
90 
91  // The subvectors and submatrices we need to fill:
94 
95  unsigned int n_qpoints = c.get_element_qrule().n_points();
96 
97  libmesh_assert (input_contexts.find(&c) != input_contexts.end());
98 
99  FEMContext & input_c = *input_contexts[&c];
100  input_c.pre_fe_reinit(*input_system, &c.get_elem());
101  input_c.elem_fe_reinit();
102 
103  for (unsigned int qp=0; qp != n_qpoints; qp++)
104  {
105  Number u = c.interior_value(0, qp);
106 
107  Number ufunc = (*goal_func)(input_c, xyz[qp]);
108 
109  for (unsigned int i=0; i != n_u_dofs; i++)
110  F(i) += JxW[qp] * ((u - ufunc) * phi[i][qp]);
111  if (request_jacobian)
112  {
113  const Number JxWxD = JxW[qp] *
115 
116  for (unsigned int i=0; i != n_u_dofs; i++)
117  for (unsigned int j=0; j != n_u_dofs; ++j)
118  K(i,j) += JxWxD * (phi[i][qp] * phi[j][qp]);
119  }
120  } // end of the quadrature point qp-loop
121 
122  return request_jacobian;
123 }
L2system.h
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
L2System::init_data
virtual void init_data()
Initializes the member data fields associated with the system, so that, e.g., assemble() may be used.
Definition: L2system.C:36
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::FEMSystem::init_context
virtual void init_context(DiffContext &) override
Definition: fem_system.C:1338
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::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::FEMContext::pre_fe_reinit
virtual void pre_fe_reinit(const System &, const Elem *e)
Reinitializes local data vectors/matrices on the current geometric element.
Definition: fem_context.C:1642
libMesh::FEMContext::get_elem
const Elem & get_elem() const
Accessor for current Elem object.
Definition: fem_context.h:896
L2System::init_context
virtual void init_context(libMesh::DiffContext &context)
Definition: L2system.C:47
libMesh::libmesh_assert
libmesh_assert(ctx)
L2System::element_time_derivative
virtual bool element_time_derivative(bool request_jacobian, libMesh::DiffContext &context)
Adds the time derivative contribution on elem to elem_residual.
Definition: L2system.C:73
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::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::DiffContext::get_elem_jacobian
const DenseMatrix< Number > & get_elem_jacobian() const
Const accessor for element Jacobian.
Definition: diff_context.h:283
L2System::~L2System
~L2System()
Definition: L2system.C:30
libMesh::FEMContext::interior_value
Number interior_value(unsigned int var, unsigned int qp) const
Definition: fem_context.C:371
libMesh::FEMContext
This class provides all data required for a physics package (e.g.
Definition: fem_context.h:62