#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) {} void Step3::make_grid ( ) { GridGenerator::hyper_cube ( triangulation, -1, 1 ); // // CHANGE: Only refine once to start with. // triangulation.refine_global ( 1 ); // // End change. // 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); FEValues<2> fe_values (fe, quadrature_formula, update_values | update_gradients | 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, ZeroFunction<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 (); // // CHANGE TO PRINT SOLUTION AT (1/3,1/3). // std::cout << "Solution at (1/3,1/3): " << VectorTools::point_value (dof_handler, solution, Point<2>(1./3, 1./3)) << std::endl; // // CHANGE TO PRINT SOLUTION AVERAGE. // std::cout << "Mean value: " << VectorTools::compute_mean_value ( dof_handler, QGauss<2>(3), solution, 0 ) << std::endl; // // WE DON'T WANT ANY GRAPHICS OUTPUT. // //std::ofstream output_gpl ( "solution.gpl" ); //data_out.write_gnuplot ( output_gpl ); //std::ofstream output_vtk ( "solution.vtk" ); //data_out.write_vtk ( output_vtk ); // // END THESE CHANGES // } void Step3::run () { // // CHANGE TO DO 9 REFINEMENT STEPS, ONE AT A TIME. // int refinements; for ( refinements = 1; refinements <= 9; refinements++ ) { if ( refinements == 1 ) { make_grid ( ); } else { triangulation.refine_global ( 1 ); } setup_system (); assemble_system (); solve (); output_results (); } // // END THESE CHANGES. // } int main () { deallog.depth_console (2); Step3 laplace_problem; laplace_problem.run (); return 0; }