You're right, applying boundary conditions during assembly is a common and efficient strategy to avoid modifying the sparse matrix structure later. Deal.II provides mechanisms to do this effectively. Here's a breakdown of how it's accomplished in Deal.II:
Core Idea: The key is to recognize that boundary conditions effectively modify the rows and columns of the stiffness matrix associated with degrees of freedom on the boundary. We achieve this by:
- Identifying Boundary Degrees of Freedom: We need a way to figure out which degrees of freedom belong to the boundary.
- Modifying the Local Contributions: When assembling the local element stiffness matrix, we check if any of the element's degrees of freedom are boundary DOFs. If they are, we modify the local matrix contribution before adding it to the global stiffness matrix.
- Modifying the Right-Hand Side (RHS): Boundary conditions often require adjustments to the right-hand side (load vector) as well.
Deal.II Implementation Strategies:
Here's a more detailed breakdown using Deal.II concepts and code snippets:
1. Identifying Boundary Degrees of Freedom (DOF Handlers and Boundary Indicators):
DoFHandler: This class manages the distribution of degrees of freedom (DOFs) on the mesh. It's essential for accessing DOF numbers.
Triangulation::active_cell_iterator: You iterate over the active cells in your mesh.
CellIterator::face(face_no): Get a face iterator for a given face number.
FaceIterator::boundary_indicator(): Each face has a boundary indicator, an integer you assign during mesh creation. You'll use this to identify which faces are on the boundary and which boundary they are on.
FEValues<dim> or FEValuesViews::DoFs: These classes provide access to the degrees of freedom associated with a cell or face.
Example of finding boundary DOFs and their boundary indicators:
#include <deal.II/base/types.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/fe/fe_q.h>
#include <deal.II/grid/tria.h>
#include <map>
#include <vector>
using namespace dealii;
void find_boundary_dofs(const Triangulation<2>& triangulation,
const DoFHandler<2>& dof_handler,
std::map<types::global_dof_index, int>& boundary_dof_map)
{
boundary_dof_map.clear();
for (const auto& cell : triangulation.active_cell_iterators())
{
for (unsigned int face_no = 0; face_no < GeometryInfo<2>::faces_per_cell; ++face_no)
{
if (cell->face(face_no)->at_boundary())
{
const int boundary_id = cell->face(face_no)->boundary_id();
std::vector<types::global_dof_index> local_dof_indices(dof_handler.dofs_per_cell);
cell->get_dof_indices(local_dof_indices);
for (unsigned int i = 0; i < dof_handler.dofs_per_cell; ++i)
{
if (dof_handler.get_fe().face_support(i,face_no))
{
boundary_dof_map[local_dof_indices[i]] = boundary_id;
}
}
}
}
}
}
int main() {
Triangulation<2> triangulation;
GridGenerator::hyper_cube (triangulation);
triangulation.refine_global (3);
FE_Q<2> fe(1); // Linear elements
DoFHandler<2> dof_handler(triangulation);
dof_handler.distribute_dofs(fe);
std::map<types::global_dof_index, int> boundary_dofs;
find_boundary_dofs(triangulation, dof_handler, boundary_dofs);
// Now boundary_dofs contains a mapping of DOF index -> boundary indicator.
return 0;
}
2. Modifying Local Contributions During Assembly (Crucial Part):
This is where you modify the local element stiffness matrix (and potentially the local load vector) based on the boundary conditions. The most common approaches are:
-
Dirichlet (Essential) Boundary Conditions:
- Zeroing Out Rows and Columns: For each boundary DOF with a Dirichlet condition, set the corresponding row and column in the local stiffness matrix to zero. Then, set the diagonal entry to 1.0 (or any non-zero value). Also, set the corresponding entry in the load vector (RHS) to the specified Dirichlet value.
- Elimination (Substitution): In more advanced approaches, you can perform static condensation to eliminate the boundary DOFs from the system entirely, reducing the size of the linear system.
-
Neumann (Natural) Boundary Conditions:
- Integrating the Neumann Flux: Neumann boundary conditions are usually incorporated by adding an integral term to the weak form of the PDE, which is then assembled into the RHS (load vector).
FEFaceValues are useful for integrating along the boundary.
-
Robin (Mixed) Boundary Conditions:
- Adding Boundary Terms to the Local Stiffness Matrix and RHS: Robin conditions introduce terms that need to be integrated on the boundary and added to both the local stiffness matrix and the local load vector.
Example (Dirichlet, Zeroing Out):
#include <deal.II/base/types.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/fe/fe_q.h>
#include <deal.II/grid/tria.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/lac/full_matrix.h>
#include <deal.II/lac/vector.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/system/matrix_tools.h>
#include <map>
#include <vector>
using namespace dealii;
void assemble_system(const Triangulation<2>& triangulation,
const DoFHandler<2>& dof_handler,
const std::map<types::global_dof_index, int>& boundary_dof_map,
SparseMatrix<double>& stiffness_matrix,
Vector<double>& rhs)
{
FE_Q<2> fe(1);
QGauss<2> quadrature_formula(fe.degree + 1);
FEValues<2> fe_values(fe,
quadrature_formula,
update_values | update_gradients |
update_quadrature_points | update_JxW_values);
const unsigned int dofs_per_cell = fe.dofs_per_cell;
const unsigned int n_q_points = quadrature_formula.size();
FullMatrix<double> cell_matrix(dofs_per_cell, dofs_per_cell);
Vector<double> cell_rhs(dofs_per_cell);
std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);
stiffness_matrix.reinit(dof_handler.n_dofs(), dof_handler.n_dofs(), dof_handler.max_couplings_between_dofs());
rhs = 0;
for (const auto& cell : triangulation.active_cell_iterators())
{
cell_matrix = 0;
cell_rhs = 0;
fe_values.reinit(cell);
for (unsigned int q = 0; q < n_q_points; ++q)
{
for (unsigned int i = 0; i < dofs_per_cell; ++i)
{
for (unsigned int j = 0; j < dofs_per_cell; ++j)
{
cell_matrix(i, j) += (fe_values.shape_grad(i, q) *
fe_values.shape_grad(j, q) *
fe_values.JxW(q));
}
// Example: Source term is just 1.0
cell_rhs(i) += fe_values.shape_value(i, q) * 1.0 * fe_values.JxW(q);
}
}
cell->get_dof_indices(local_dof_indices);
for (unsigned int i = 0; i < dofs_per_cell; ++i)
{
const types::global_dof_index dof_i = local_dof_indices[i];
// Apply Dirichlet BC (Zero value)
if (boundary_dof_map.count(dof_i))
{
// Zero out row and column
for (unsigned int k = 0; k < dofs_per_cell; ++k)
{
cell_matrix(i, k) = 0.0;
cell_matrix(k, i) = 0.0; // Make sure to keep matrix symmetric
}
// Set diagonal to 1.0
cell_matrix(i, i) = 1.0;
// Set RHS to the Dirichlet value (in this case, zero)
cell_rhs(i) = 0.0;
}
}
for (unsigned int i = 0; i < dofs_per_cell; ++i)
{
for (unsigned int j = 0; j < dofs_per_cell; ++j)
{
stiffness_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_matrix(i, j));
}
rhs(local_dof_indices[i]) += cell_rhs(i);
}
}
stiffness_matrix.compress(VectorOperation::add);
}
int main() {
Triangulation<2> triangulation;
GridGenerator::hyper_cube (triangulation);
triangulation.refine_global (3);
FE_Q<2> fe(1); // Linear elements
DoFHandler<2> dof_handler(triangulation);
dof_handler.distribute_dofs(fe);
std::map<types::global_dof_index, int> boundary_dofs;
find_boundary_dofs(triangulation, dof_handler, boundary_dofs);
SparseMatrix<double> stiffness_matrix;
Vector<double> rhs(dof_handler.n_dofs());
assemble_system(triangulation, dof_handler, boundary_dofs, stiffness_matrix, rhs);
// Now stiffness_matrix and rhs contain the system with boundary conditions applied.
return 0;
}
3. Right-Hand Side (RHS) Modifications:
- Dirichlet: As mentioned above, if you are zeroing out rows and columns, the corresponding RHS entry should be set to the Dirichlet boundary value.
- Neumann: Neumann boundary conditions are incorporated as boundary integrals. Use
FEFaceValues to evaluate the boundary flux and integrate it into the local load vector.
- Robin: Similar to Neumann, Robin conditions introduce boundary integrals that affect both the local stiffness matrix and the local load vector.
Key Deal.II Classes and Functions:
DoFHandler: Manages DOFs.
FEValues, FEFaceValues: Provide shape function values, gradients, Jacobians, etc., at quadrature points, for cells and faces, respectively.
FullMatrix, Vector: Used for local element matrices and vectors.
SparseMatrix: The global stiffness matrix.
VectorTools::interpolate_boundary_values: This function can be used after assembly to directly set the solution values on Dirichlet boundaries. However, it's often preferable to modify the system during assembly, as described above.
MatrixTools::apply_boundary_values: A more general function for applying boundary conditions after assembly. It handles various types of BCs.
Function (and subclasses): Deal.II provides function classes for representing spatially varying functions. These are useful for defining Dirichlet boundary values that are not simply constants.
Workflow Summary:
- Create the Mesh and Assign Boundary Indicators: Use
GridGenerator and Triangulation to create your mesh. Assign unique integer IDs to each boundary using CellIterator::face(face_no)->set_boundary_id(some_id);.
- Distribute Degrees of Freedom: Use
DoFHandler::distribute_dofs.
- Find Boundary DOFs: Iterate through the faces of the cells, check for boundary faces (using
FaceIterator::at_boundary()), and determine the DOFs associated with those faces. Store the mapping between DOF index and boundary indicator.
- Assemble the System (Stiffness Matrix and RHS):
- Loop over cells.
- Compute the local element stiffness matrix and load vector.
- Before adding the local contributions to the global matrix/vector, check if any of the DOFs associated with the cell are boundary DOFs.
- If a boundary DOF is found, modify the local stiffness matrix and/or load vector according to the type of boundary condition (Dirichlet, Neumann, Robin).
- Add the (potentially modified) local contributions to the global stiffness matrix and load vector.
- Solve the Linear System: Use a linear solver (e.g.,
SolverControl, SolverGMRES, PreconditionBlock) to solve for the solution.
Important Considerations:
- Symmetry: If your original problem has a symmetric stiffness matrix, make sure that your boundary condition application preserves symmetry. If you're zeroing out rows and columns, zero out both the row and column.
- Parallel Computing: When using parallel computing, you'll need to ensure that the boundary condition application is done consistently across all processors. Use
parallel::distributed::Triangulation and parallel::distributed::DoFHandler. Utilities::MPI::min/max can be useful for ensuring consistent application of boundary conditions.
- Choice of BC Application Method: Zeroing out rows and columns is the simplest approach for Dirichlet conditions. Elimination (static condensation) is more complex but can lead to a smaller system size and potentially faster solution. The best approach depends on the specific problem and the desired level of performance.
- Documentation: Refer to the Deal.II documentation and examples. The
step-* tutorials provide excellent examples of finite element assembly and boundary condition handling.
By implementing these strategies, you can effectively incorporate boundary conditions directly during the assembly process in Deal.II, resulting in a more efficient and streamlined finite element simulation. Remember to carefully consider the type of boundary condition and choose the appropriate modification to the local stiffness matrix and RHS.