libMesh
Functions
miscellaneous_ex9.C File Reference

Go to the source code of this file.

Functions

void assemble_poisson (EquationSystems &es, const ElementSideMap &lower_to_upper)
 Assemble the system matrix and rhs vector. More...
 
int main (int argc, char **argv)
 

Function Documentation

◆ assemble_poisson()

void assemble_poisson ( EquationSystems es,
const ElementSideMap lower_to_upper 
)

Assemble the system matrix and rhs vector.

Definition at line 211 of file miscellaneous_ex9.C.

213 {
214  const MeshBase & mesh = es.get_mesh();
215  const unsigned int dim = mesh.mesh_dimension();
216 
217  Real R = es.parameters.get<Real>("R");
218 
219  LinearImplicitSystem & system = es.get_system<LinearImplicitSystem>("Poisson");
220 
221  const DofMap & dof_map = system.get_dof_map();
222 
223  FEType fe_type = dof_map.variable_type(0);
224 
225  std::unique_ptr<FEBase> fe (FEBase::build(dim, fe_type));
226  std::unique_ptr<FEBase> fe_elem_face (FEBase::build(dim, fe_type));
227  std::unique_ptr<FEBase> fe_neighbor_face (FEBase::build(dim, fe_type));
228 
229  QGauss qrule (dim, fe_type.default_quadrature_order());
230  QGauss qface(dim-1, fe_type.default_quadrature_order());
231 
232  fe->attach_quadrature_rule (&qrule);
233  fe_elem_face->attach_quadrature_rule (&qface);
234  fe_neighbor_face->attach_quadrature_rule (&qface);
235 
236  const std::vector<Real> & JxW = fe->get_JxW();
237  const std::vector<std::vector<Real>> & phi = fe->get_phi();
238  const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
239 
240  const std::vector<Real> & JxW_face = fe_elem_face->get_JxW();
241 
242  const std::vector<Point> & qface_points = fe_elem_face->get_xyz();
243 
244  const std::vector<std::vector<Real>> & phi_face = fe_elem_face->get_phi();
245  const std::vector<std::vector<Real>> & phi_neighbor_face = fe_neighbor_face->get_phi();
246 
249 
254 
255  std::vector<dof_id_type> dof_indices;
256 
257  for (const auto & elem : mesh.active_local_element_ptr_range())
258  {
259  dof_map.dof_indices (elem, dof_indices);
260  const unsigned int n_dofs = dof_indices.size();
261 
262  fe->reinit (elem);
263 
264  Ke.resize (n_dofs, n_dofs);
265  Fe.resize (n_dofs);
266 
267  // Assemble element interior terms for the matrix
268  for (unsigned int qp=0; qp<qrule.n_points(); qp++)
269  for (unsigned int i=0; i<n_dofs; i++)
270  for (unsigned int j=0; j<n_dofs; j++)
271  Ke(i,j) += JxW[qp]*(dphi[i][qp]*dphi[j][qp]);
272 
273  // Boundary flux provides forcing in this example
274  {
275  for (auto side : elem->side_index_range())
276  if (elem->neighbor_ptr(side) == nullptr)
277  {
278  if (mesh.get_boundary_info().has_boundary_id (elem, side, MIN_Z_BOUNDARY))
279  {
280  fe_elem_face->reinit(elem, side);
281 
282  for (unsigned int qp=0; qp<qface.n_points(); qp++)
283  for (std::size_t i=0; i<phi.size(); i++)
284  Fe(i) += JxW_face[qp] * phi_face[i][qp];
285  }
286 
287  }
288  }
289 
290  // Add boundary terms on the crack
291  {
292  for (auto side : elem->side_index_range())
293  if (elem->neighbor_ptr(side) == nullptr)
294  {
295  // Found the lower side of the crack. Assemble terms due to lower and upper in here.
296  if (mesh.get_boundary_info().has_boundary_id (elem, side, CRACK_BOUNDARY_LOWER))
297  {
298  fe_elem_face->reinit(elem, side);
299 
300  ElementSideMap::const_iterator ltu_it =
301  lower_to_upper.find(std::make_pair(elem, side));
302  libmesh_assert(ltu_it != lower_to_upper.end());
303 
304  const Elem * neighbor = ltu_it->second;
305 
306  std::vector<Point> qface_neighbor_points;
307  FEMap::inverse_map (elem->dim(), neighbor,
308  qface_points,
309  qface_neighbor_points);
310  fe_neighbor_face->reinit(neighbor, &qface_neighbor_points);
311 
312  std::vector<dof_id_type> neighbor_dof_indices;
313  dof_map.dof_indices (neighbor, neighbor_dof_indices);
314  const unsigned int n_neighbor_dofs = neighbor_dof_indices.size();
315 
316  Kne.resize (n_neighbor_dofs, n_dofs);
317  Ken.resize (n_dofs, n_neighbor_dofs);
318  Kee.resize (n_dofs, n_dofs);
319  Knn.resize (n_neighbor_dofs, n_neighbor_dofs);
320 
321  // Lower-to-lower coupling term
322  for (unsigned int qp=0; qp<qface.n_points(); qp++)
323  for (unsigned int i=0; i<n_dofs; i++)
324  for (unsigned int j=0; j<n_dofs; j++)
325  Kee(i,j) -= JxW_face[qp] * (1./R)*(phi_face[i][qp] * phi_face[j][qp]);
326 
327  // Lower-to-upper coupling term
328  for (unsigned int qp=0; qp<qface.n_points(); qp++)
329  for (unsigned int i=0; i<n_dofs; i++)
330  for (unsigned int j=0; j<n_neighbor_dofs; j++)
331  Ken(i,j) += JxW_face[qp] * (1./R)*(phi_face[i][qp] * phi_neighbor_face[j][qp]);
332 
333  // Upper-to-upper coupling term
334  for (unsigned int qp=0; qp<qface.n_points(); qp++)
335  for (unsigned int i=0; i<n_neighbor_dofs; i++)
336  for (unsigned int j=0; j<n_neighbor_dofs; j++)
337  Knn(i,j) -= JxW_face[qp] * (1./R)*(phi_neighbor_face[i][qp] * phi_neighbor_face[j][qp]);
338 
339  // Upper-to-lower coupling term
340  for (unsigned int qp=0; qp<qface.n_points(); qp++)
341  for (unsigned int i=0; i<n_neighbor_dofs; i++)
342  for (unsigned int j=0; j<n_dofs; j++)
343  Kne(i,j) += JxW_face[qp] * (1./R)*(phi_neighbor_face[i][qp] * phi_face[j][qp]);
344 
345  system.matrix->add_matrix(Kne, neighbor_dof_indices, dof_indices);
346  system.matrix->add_matrix(Ken, dof_indices, neighbor_dof_indices);
347  system.matrix->add_matrix(Kee, dof_indices);
348  system.matrix->add_matrix(Knn, neighbor_dof_indices);
349  }
350  }
351  }
352 
353  dof_map.constrain_element_matrix_and_vector (Ke, Fe, dof_indices);
354 
355  system.matrix->add_matrix (Ke, dof_indices);
356  system.rhs->add_vector (Fe, dof_indices);
357  }
358 }

References libMesh::MeshBase::active_local_element_ptr_range(), libMesh::FEGenericBase< OutputType >::build(), libMesh::FEType::default_quadrature_order(), dim, libMesh::Parameters::get(), libMesh::MeshBase::get_boundary_info(), libMesh::EquationSystems::get_mesh(), libMesh::EquationSystems::get_system(), libMesh::BoundaryInfo::has_boundary_id(), libMesh::FEMap::inverse_map(), libMesh::libmesh_assert(), mesh, libMesh::MeshBase::mesh_dimension(), libMesh::EquationSystems::parameters, libMesh::Real, libMesh::DenseVector< T >::resize(), libMesh::DenseMatrix< T >::resize(), and libMesh::TypeVector< T >::size().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 98 of file miscellaneous_ex9.C.

99 {
100  // Initialize libMesh.
101  LibMeshInit init (argc, argv);
102 
103  // This example uses an ExodusII input file
104 #ifndef LIBMESH_HAVE_EXODUS_API
105  libmesh_example_requires(false, "--enable-exodus");
106 #endif
107 
108  // Skip this 3D example if libMesh was compiled as 1D or 2D-only.
109  libmesh_example_requires(3 <= LIBMESH_DIM, "3D support");
110 
111  // We use Dirichlet boundary conditions here
112 #ifndef LIBMESH_ENABLE_DIRICHLET
113  libmesh_example_requires(false, "--enable-dirichlet");
114 #endif
115 
116  GetPot command_line (argc, argv);
117 
118  Real R = 2.;
119  if (command_line.search(1, "-R"))
120  R = command_line.next(R);
121 
122  Mesh mesh(init.comm());
123 
124  EquationSystems equation_systems (mesh);
125 
126  LinearImplicitSystem & system =
127  equation_systems.add_system<LinearImplicitSystem> ("Poisson");
128  system.add_variable("u", FIRST, LAGRANGE);
129 
130  // We want to call assemble_poisson "manually" so that we can pass in
131  // lower_to_upper, hence set assemble_before_solve = false
132  system.assemble_before_solve = false;
133 
134 #ifdef LIBMESH_ENABLE_DIRICHLET
135  // Impose zero Dirichlet boundary condition on MAX_Z_BOUNDARY
136  std::set<boundary_id_type> boundary_ids;
137  boundary_ids.insert(MAX_Z_BOUNDARY);
138  std::vector<unsigned int> variables;
139  variables.push_back(0);
140  ZeroFunction<> zf;
141 
142  // Most DirichletBoundary users will want to supply a "locally
143  // indexed" functor
144  DirichletBoundary dirichlet_bc(boundary_ids, variables, zf,
146  system.get_dof_map().add_dirichlet_boundary(dirichlet_bc);
147 #endif // LIBMESH_ENABLE_DIRICHLET
148 
149  // Attach an object to the DofMap that will augment the sparsity pattern
150  // due to the degrees-of-freedom on the "crack"
151  //
152  // By attaching this object *before* reading our mesh, we also
153  // ensure that the connected elements will not be deleted on a
154  // distributed mesh.
155  AugmentSparsityOnInterface augment_sparsity
156  (mesh, CRACK_BOUNDARY_LOWER, CRACK_BOUNDARY_UPPER);
157  system.get_dof_map().add_coupling_functor(augment_sparsity);
158 
159  mesh.read("miscellaneous_ex9.exo");
160 
161  equation_systems.init();
162  equation_systems.print_info();
163 
164  // Set the jump term coefficient, it will be used in assemble_poisson
165  equation_systems.parameters.set<Real>("R") = R;
166 
167  // Assemble and then solve
168  assemble_poisson(equation_systems,
169  augment_sparsity.get_lower_to_upper());
170  system.solve();
171 
172 #ifdef LIBMESH_HAVE_EXODUS_API
173  // Plot the solution
174  ExodusII_IO (mesh).write_equation_systems ("solution.exo",
175  equation_systems);
176 #endif
177 
178 #ifdef LIBMESH_ENABLE_AMR
179  // Possibly solve on a refined mesh next.
180  MeshRefinement mesh_refinement (mesh);
181  unsigned int n_refinements = 0;
182  if (command_line.search("-n_refinements"))
183  n_refinements = command_line.next(0);
184 
185  for (unsigned int r = 0; r != n_refinements; ++r)
186  {
187  std::cout << "Refining the mesh" << std::endl;
188 
189  mesh_refinement.uniformly_refine ();
190  equation_systems.reinit();
191 
192  assemble_poisson(equation_systems,
193  augment_sparsity.get_lower_to_upper());
194  system.solve();
195 
196 #ifdef LIBMESH_HAVE_EXODUS_API
197  // Plot the refined solution
198  std::ostringstream out;
199  out << "solution_" << r << ".exo";
201  equation_systems);
202 #endif
203 
204  }
205 
206 #endif
207 
208  return 0;
209 }

References libMesh::DofMap::add_coupling_functor(), libMesh::DofMap::add_dirichlet_boundary(), libMesh::System::add_variable(), libMesh::System::assemble_before_solve, assemble_poisson(), libMesh::FIRST, libMesh::System::get_dof_map(), AugmentSparsityOnInterface::get_lower_to_upper(), libMesh::TriangleWrapper::init(), libMesh::LAGRANGE, libMesh::LOCAL_VARIABLE_ORDER, mesh, libMesh::out, libMesh::MeshBase::read(), libMesh::Real, libMesh::LinearImplicitSystem::solve(), libMesh::MeshRefinement::uniformly_refine(), and libMesh::MeshOutput< MT >::write_equation_systems().

libMesh::Mesh
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
libMesh::EquationSystems::get_mesh
const MeshBase & get_mesh() const
Definition: equation_systems.h:637
libMesh::MeshBase::read
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.
libMesh::MeshBase::get_boundary_info
const BoundaryInfo & get_boundary_info() const
The information about boundary ids on the mesh.
Definition: mesh_base.h:132
libMesh::QGauss
This class implements specific orders of Gauss quadrature.
Definition: quadrature_gauss.h:39
libMesh::DofMap::add_dirichlet_boundary
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
Definition: dof_map_constraints.C:4390
libMesh::MeshBase::active_local_element_ptr_range
virtual SimpleRange< element_iterator > active_local_element_ptr_range()=0
libMesh::EquationSystems::get_system
const T_sys & get_system(const std::string &name) const
Definition: equation_systems.h:757
libMesh::DenseMatrix< Number >
libMesh::LinearImplicitSystem::solve
virtual void solve() override
Assembles & solves the linear system A*x=b.
Definition: linear_implicit_system.C:108
mesh
MeshBase & mesh
Definition: mesh_communication.C:1257
libMesh::MeshBase::mesh_dimension
unsigned int mesh_dimension() const
Definition: mesh_base.C:135
libMesh::ExodusII_IO
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:51
dim
unsigned int dim
Definition: adaptivity_ex3.C:113
libMesh::DenseMatrix::resize
void resize(const unsigned int new_m, const unsigned int new_n)
Resize the matrix.
Definition: dense_matrix.h:822
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::System::assemble_before_solve
bool assemble_before_solve
Flag which tells the system to whether or not to call the user assembly function during each call to ...
Definition: system.h:1493
libMesh::MeshRefinement
Implements (adaptive) mesh refinement algorithms for a MeshBase.
Definition: mesh_refinement.h:61
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::LOCAL_VARIABLE_ORDER
Definition: dirichlet_boundaries.h:62
libMesh::MeshBase
This is the MeshBase class.
Definition: mesh_base.h:78
libMesh::System::add_variable
unsigned int add_variable(const std::string &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:1069
libMesh::LibMeshInit
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:83
libMesh::ZeroFunction
ConstFunction that simply returns 0.
Definition: zero_function.h:36
libMesh::EquationSystems
This is the EquationSystems class.
Definition: equation_systems.h:74
libMesh::MeshOutput::write_equation_systems
virtual void write_equation_systems(const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
This method implements writing a mesh with data to a specified file where the data is taken from the ...
Definition: mesh_output.C:31
libMesh::DenseVector::resize
void resize(const unsigned int n)
Resize the vector.
Definition: dense_vector.h:355
libMesh::FEType
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Definition: fe_type.h:178
libMesh::DofMap
This class handles the numbering of degrees of freedom on a mesh.
Definition: dof_map.h:176
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
assemble_poisson
void assemble_poisson(EquationSystems &es, const ElementSideMap &lower_to_upper)
Assemble the system matrix and rhs vector.
Definition: miscellaneous_ex9.C:211
AugmentSparsityOnInterface
Definition: augment_sparsity_on_interface.h:19
libMesh::Elem
This is the base class from which all geometric element types are derived.
Definition: elem.h:100
libMesh::System::get_dof_map
const DofMap & get_dof_map() const
Definition: system.h:2099
libMesh::LAGRANGE
Definition: enum_fe_family.h:36
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::FEType::default_quadrature_order
Order default_quadrature_order() const
Definition: fe_type.h:353
libMesh::LinearImplicitSystem
Manages consistently variables, degrees of freedom, coefficient vectors, matrices and linear solvers ...
Definition: linear_implicit_system.h:55
libMesh::BoundaryInfo::has_boundary_id
bool has_boundary_id(const Node *const node, const boundary_id_type id) const
Definition: boundary_info.C:972
libMesh::Parameters::get
const T & get(const std::string &) const
Definition: parameters.h:421
libMesh::out
OStreamProxy out
libMesh::FIRST
Definition: enum_order.h:42
libMesh::DenseVector< Number >
libMesh::DofMap::add_coupling_functor
void add_coupling_functor(GhostingFunctor &coupling_functor, bool to_mesh=true)
Adds a functor which can specify coupling requirements for creation of sparse matrices.
Definition: dof_map.C:1838
libMesh::EquationSystems::parameters
Parameters parameters
Data structure holding arbitrary parameters.
Definition: equation_systems.h:557