#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; }; //****************************************************************************80 // // CHANGE: // Add classes for RightHandSide and BoundaryValues. // // Add righthandside and boundaryvalues functions. // 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; return_value = - 2.0 * p(1); return return_value; } template double BoundaryValues::value (const Point &p, const unsigned int /*component*/) const { return p(0) * p(0) * p(1); } Step3::Step3 () : fe (1), dof_handler (triangulation) {} void Step3::make_grid () { GridGenerator::hyper_cube (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; DynamicSparsityPattern dsp(dof_handler.n_dofs()); DoFTools::make_sparsity_pattern (dof_handler, dsp); sparsity_pattern.copy_from(dsp); 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); //****************************************************************************80 // // CHANGE: Declare a right hand side function. // const RightHandSide<2> right_hand_side; //****************************************************************************80 //****************************************************************************80 // // Change: add "| update_quadrature points" to list of things returned by fe_values. // FEValues<2> fe_values (fe, quadrature_formula, update_values | update_gradients | update_JxW_values | update_quadrature_points ); //****************************************************************************80 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; //****************************************************************************80 // // CHANGE: // Replace zero boundary value by BoundaryValues<2>() function. // VectorTools::interpolate_boundary_values (dof_handler, 0, BoundaryValues<2>(), boundary_values); //****************************************************************************80 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_eps ( "solution.eps" ); data_out.write_eps ( output_eps ); std::ofstream output_vtk ( "solution.vtk" ); data_out.write_vtk ( output_vtk ); } void Step3::run () { make_grid (); setup_system (); assemble_system (); solve (); output_results (); } int main () { deallog.depth_console (2); //****************************************************************************80 std::cout << "\n"; std::cout << "STEP-3MOD2\n"; std::cout << " U=X^2Y is exact solution.\n"; //****************************************************************************80 Step3 laplace_problem; laplace_problem.run (); return 0; }