/* --------------------------------------------------------------------- * * Copyright (C) 1999 - 2014 by the deal.II authors * * This file is part of the deal.II library. * * The deal.II library is free software; you can use it, redistribute * it, and/or modify it under the terms of the GNU Lesser General * Public License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * The full text of the license can be found in the file LICENSE at * the top level of the deal.II distribution. * * --------------------------------------------------------------------- * * Authors: Wolfgang Bangerth, 1999, * Guido Kanschat, 2011 */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace dealii; class Step3 { public: Step3 (); void run (); private: void make_grid (); void setup_system (); void assemble_system (); void solve (); void output_results () const; Triangulation<2> triangulation; FE_Q<2> fe; DoFHandler<2> dof_handler; SparsityPattern sparsity_pattern; SparseMatrix system_matrix; Vector solution; Vector system_rhs; }; Step3::Step3 () : fe (1), dof_handler (triangulation) {} template class RightHandSide : public Function { public: RightHandSide () : Function() {} virtual double value (const Point &p, const unsigned int component = 0) const; }; template class BoundaryValues : public Function { public: BoundaryValues () : Function() {} virtual double value (const Point &p, const unsigned int component = 0) const; }; template double RightHandSide::value (const Point &p, const unsigned int /*component*/) const { double return_value = 0.0; // for (unsigned int i=0; i double BoundaryValues::value (const Point &p, const unsigned int /*component*/) const { return p(0)*p(0)*p(1);//p.square(); } void Step3::make_grid () { // GridGenerator::hyper_cube (triangulation, -1, 1); GridGenerator::hyper_L (triangulation, -1, 1); triangulation.refine_global (5); std::cout << "Number of active cells: " << triangulation.n_active_cells() << std::endl; } void Step3::setup_system () { dof_handler.distribute_dofs (fe); std::cout << "Number of degrees of freedom: " << dof_handler.n_dofs() << std::endl; CompressedSparsityPattern c_sparsity(dof_handler.n_dofs()); DoFTools::make_sparsity_pattern (dof_handler, c_sparsity); sparsity_pattern.copy_from(c_sparsity); system_matrix.reinit (sparsity_pattern); solution.reinit (dof_handler.n_dofs()); system_rhs.reinit (dof_handler.n_dofs()); } void Step3::assemble_system () { QGauss<2> quadrature_formula(2); const RightHandSide<2> right_hand_side; 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 cell_matrix (dofs_per_cell, dofs_per_cell); Vector cell_rhs (dofs_per_cell); std::vector local_dof_indices (dofs_per_cell); DoFHandler<2>::active_cell_iterator cell = dof_handler.begin_active(), endc = dof_handler.end(); for (; cell!=endc; ++cell) { fe_values.reinit (cell); cell_matrix = 0; cell_rhs = 0; for (unsigned int q_index=0; q_indexget_dof_indices (local_dof_indices); for (unsigned int i=0; i boundary_values; VectorTools::interpolate_boundary_values (dof_handler, 0, BoundaryValues<2>(), boundary_values); MatrixTools::apply_boundary_values (boundary_values, system_matrix, solution, system_rhs); } void Step3::solve () { SolverControl solver_control (1000, 1e-12); SolverCG<> solver (solver_control); solver.solve (system_matrix, solution, system_rhs, PreconditionIdentity()); } void Step3::output_results () const { DataOut<2> data_out; data_out.attach_dof_handler (dof_handler); data_out.add_data_vector (solution, "solution"); data_out.build_patches (); std::ofstream output ("solution.svg"); data_out.write_gnuplot (output); } void Step3::run () { make_grid (); setup_system (); assemble_system (); solve (); output_results (); } int main () { Step3 laplace_problem; laplace_problem.run (); return 0; }