/* ------------------------------------------------------------------------ * * SPDX-License-Identifier: LGPL-2.1-or-later * Copyright (C) 2000 - 2024 by the deal.II authors * * This file is part of the deal.II library. * * Part of the source code is dual licensed under Apache-2.0 WITH * LLVM-exception OR LGPL-2.1-or-later. Detailed license information * governing the source code and code contributions can be found in * LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. * * ------------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace dealii; template class Step6 { public: Step6(); void run(); private: void setup_system(); void assemble_system(); void solve(); void refine_grid(); void output_results(const unsigned int cycle) const; Triangulation triangulation; const FE_Q fe; DoFHandler dof_handler; AffineConstraints constraints; SparseMatrix system_matrix; SparsityPattern sparsity_pattern; Vector solution; Vector system_rhs; }; template double coefficient(const Point &p) { if (p.square() < 0.5 * 0.5) return 20; else return 1; } template Step6::Step6() : fe(/* polynomial degree = */ 2) , dof_handler(triangulation) {} template void Step6::setup_system() { dof_handler.distribute_dofs(fe); solution.reinit(dof_handler.n_dofs()); system_rhs.reinit(dof_handler.n_dofs()); constraints.clear(); DoFTools::make_hanging_node_constraints(dof_handler, constraints); VectorTools::interpolate_boundary_values(dof_handler, types::boundary_id(0), Functions::ZeroFunction(), constraints); constraints.close(); DynamicSparsityPattern dsp(dof_handler.n_dofs()); DoFTools::make_sparsity_pattern(dof_handler, dsp, constraints, /*keep_constrained_dofs = */ false); sparsity_pattern.copy_from(dsp); system_matrix.reinit(sparsity_pattern); } template void Step6::assemble_system() { const QGauss quadrature_formula(fe.degree + 1); FEValues fe_values(fe, quadrature_formula, update_values | update_gradients | update_quadrature_points | update_JxW_values); const unsigned int dofs_per_cell = fe.n_dofs_per_cell(); FullMatrix cell_matrix(dofs_per_cell, dofs_per_cell); Vector cell_rhs(dofs_per_cell); std::vector local_dof_indices(dofs_per_cell); for (const auto &cell : dof_handler.active_cell_iterators()) { fe_values.reinit(cell); cell_matrix = 0; cell_rhs = 0; for (const unsigned int q_index : fe_values.quadrature_point_indices()) { const double current_coefficient = coefficient(fe_values.quadrature_point(q_index)); for (const unsigned int i : fe_values.dof_indices()) { for (const unsigned int j : fe_values.dof_indices()) cell_matrix(i, j) += (current_coefficient * // a(x_q) fe_values.shape_grad(i, q_index) * // grad phi_i(x_q) fe_values.shape_grad(j, q_index) * // grad phi_j(x_q) fe_values.JxW(q_index)); // dx cell_rhs(i) += (fe_values.shape_value(i, q_index) * // phi_i(x_q) 1.0 * // f(x) fe_values.JxW(q_index)); // dx } } cell->get_dof_indices(local_dof_indices); constraints.distribute_local_to_global( cell_matrix, cell_rhs, local_dof_indices, system_matrix, system_rhs); } } template void Step6::solve() { SolverControl solver_control(1000, 1e-6 * system_rhs.l2_norm()); SolverCG> solver(solver_control); PreconditionSSOR> preconditioner; preconditioner.initialize(system_matrix, 1.2); solver.solve(system_matrix, solution, system_rhs, preconditioner); constraints.distribute(solution); } template void Step6::refine_grid() { Vector estimated_error_per_cell(triangulation.n_active_cells()); KellyErrorEstimator::estimate(dof_handler, QGauss(fe.degree + 1), {}, solution, estimated_error_per_cell); GridRefinement::refine_and_coarsen_fixed_number(triangulation, estimated_error_per_cell, 0.3, 0.03); triangulation.execute_coarsening_and_refinement(); } template void Step6::output_results(const unsigned int cycle) const { { GridOut grid_out; std::ofstream output("grid-" + std::to_string(cycle) + ".gnuplot"); GridOutFlags::Gnuplot gnuplot_flags(false, 5); grid_out.set_flags(gnuplot_flags); MappingQ mapping(3); grid_out.write_gnuplot(triangulation, output, &mapping); } { DataOut data_out; data_out.attach_dof_handler(dof_handler); data_out.add_data_vector(solution, "solution"); data_out.build_patches(); std::ofstream output("solution-" + std::to_string(cycle) + ".vtu"); data_out.write_vtu(output); } } template void Step6::run() { for (unsigned int cycle = 0; cycle < 8; ++cycle) { std::cout << "Cycle " << cycle << ':' << std::endl; if (cycle == 0) { GridGenerator::hyper_ball(triangulation); triangulation.refine_global(1); } else refine_grid(); std::cout << " Number of active cells: " << triangulation.n_active_cells() << std::endl; setup_system(); std::cout << " Number of degrees of freedom: " << dof_handler.n_dofs() << std::endl; assemble_system(); solve(); output_results(cycle); } } int main() { cout << "\n"; cout << "step-6():\n"; cout << " dealii example code.\n"; cout << "\n"; try { Step6<2> laplace_problem_2d; laplace_problem_2d.run(); } catch (std::exception &exc) { std::cerr << std::endl << std::endl << "----------------------------------------------------" << std::endl; std::cerr << "Exception on processing: " << std::endl << exc.what() << std::endl << "Aborting!" << std::endl << "----------------------------------------------------" << std::endl; return 1; } catch (...) { std::cerr << std::endl << std::endl << "----------------------------------------------------" << std::endl; std::cerr << "Unknown exception!" << std::endl << "Aborting!" << std::endl << "----------------------------------------------------" << std::endl; return 1; } cout << "\n"; cout << "step-6():\n"; cout << " Normal end of execution.\n"; return 0; }