From: kanschat Date: Wed, 27 Jul 2011 05:29:10 +0000 (+0000) Subject: make first steps a bit more conforming to later X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1f4b2506db501f2aaa14416a8a2599350a521081;p=dealii-svn.git make first steps a bit more conforming to later git-svn-id: https://svn.dealii.org/trunk@23966 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/examples/step-3/step-3.cc b/deal.II/examples/step-3/step-3.cc index a6dab141db..b6ad0b9046 100644 --- a/deal.II/examples/step-3/step-3.cc +++ b/deal.II/examples/step-3/step-3.cc @@ -1,9 +1,9 @@ /* $Id$ */ -/* Author: Wolfgang Bangerth, University of Heidelberg, 1999 */ +/* Author: Wolfgang Bangerth, 1999, Guido Kanschat, 2011 */ /* $Id$ */ /* */ -/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2010 by the deal.II authors */ +/* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2010, 2011 by the deal.II authors */ /* */ /* This file is subject to QPL and may not be distributed */ /* without copyright and license information. Please refer */ @@ -129,8 +129,10 @@ class LaplaceProblem // suggest. Since they do not need to be // called from outside, they are made // private to this class. + private: - void make_grid_and_dofs (); + void make_grid (); + void setup_system (); void assemble_system (); void solve (); void output_results () const; @@ -189,7 +191,7 @@ LaplaceProblem::LaplaceProblem () : {} - // @sect4{LaplaceProblem::make_grid_and_dofs} + // @sect4{LaplaceProblem::make_grid} // Now, the first thing we've got to // do is to generate the @@ -197,12 +199,8 @@ LaplaceProblem::LaplaceProblem () : // like to do our computation and // number each vertex with a degree // of freedom. We have seen this in - // the previous examples before. Then - // we have to set up space for the - // system matrix and right hand side - // of the discretized problem. This - // is what this function does: -void LaplaceProblem::make_grid_and_dofs () + // the previous examples before. +void LaplaceProblem::make_grid () { // First create the grid and refine // all cells five times. Since the @@ -236,18 +234,25 @@ void LaplaceProblem::make_grid_and_dofs () << std::endl; // Note the distinction between // n_active_cells() and n_cells(). +} + + // @sect4{LaplaceProblem::setup_system} // Next we enumerate all the degrees of - // freedom. This is done by using the - // distribute_dofs function, as we have + // freedom and set up matrix and + // vector objects to hold the + // system data. Enumerating is done by using + // DoFHandler::distribute_dofs(), as we have // seen in the step-2 example. Since we use - // the FE_Q class with a polynomial + // the FE_Q class with a polynomial // degree of 1, i.e. bilinear elements, // this associates one degree of freedom // with each vertex. While we're at // generating output, let us also take a // look at how many degrees of freedom are // generated: +void LaplaceProblem::setup_system () +{ dof_handler.distribute_dofs (fe); std::cout << "Number of degrees of freedom: " << dof_handler.n_dofs() @@ -351,7 +356,7 @@ void LaplaceProblem::make_grid_and_dofs () // objects. That's too much, so there is one // type of class that orchestrates // information exchange between these three: - // the FEValues class. If given one + // the FEValues class. If given one // instance of each three of these objects, // it will be able to provide you with // information about values and gradients of @@ -399,22 +404,22 @@ void LaplaceProblem::assemble_system () // actually need is given as a bitwise // connection of flags as the third // argument to the constructor of - // FEValues. Since these values have to + // FEValues. Since these values have to // be recomputed, or updated, every time we // go to a new cell, all of these flags // start with the prefix update_ and // then indicate what it actually is that // we want updated. The flag to give if we // want the values of the shape functions - // computed is update_values; for the + // computed is #update_values; for the // gradients it is - // update_gradients. The determinants + // #update_gradients. The determinants // of the Jacobians and the quadrature // weights are always used together, so // only the products (Jacobians times // weights, or short JxW) are computed; // since we need them, we have to list - // update_JxW_values as well: + // #update_JxW_values as well: FEValues<2> fe_values (fe, quadrature_formula, update_values | update_gradients | update_JxW_values); // The advantage of this proceeding is that @@ -561,7 +566,7 @@ void LaplaceProblem::assemble_system () // determinant and the quadrature point // weight (that one gets together by // the call to - // fe_values.JxW). Finally, this is + // FEValues::JxW() ). Finally, this is // repeated for all shape functions // phi_i and phi_j: for (unsigned int i=0; iVectorTools::interpolate_boundary_values. Its + // VectorTools::interpolate_boundary_values(). Its // parameters are (omitting parameters for // which default values exist and that we // don't care about): the DoFHandler object @@ -690,13 +695,13 @@ void LaplaceProblem::assemble_system () // the boundary. // // The function describing the boundary - // values is an object of type Function + // values is an object of type Function // or of a derived class. One of the - // derived classes is ZeroFunction, + // derived classes is ZeroFunction, // which describes (not unexpectedly) a // function which is zero everywhere. We // create such an object in-place and pass - // it to the interpolate_boundary_values + // it to the VectorTools::interpolate_boundary_values() // function. // // Finally, the output object is a @@ -751,7 +756,7 @@ void LaplaceProblem::solve () // First, we need to have an object that // knows how to tell the CG algorithm when // to stop. This is done by using a - // SolverControl object, and as + // SolverControl object, and as // stopping criterion we say: stop after a // maximum of 1000 iterations (which is far // more than is needed for 1089 variables; @@ -762,7 +767,7 @@ void LaplaceProblem::solve () // the one which stops the iteration: SolverControl solver_control (1000, 1e-12); // Then we need the solver itself. The - // template parameters to the SolverCG + // template parameters to the SolverCG // class are the matrix type and the type // of the vectors, but the empty angle // brackets indicate that we simply take @@ -803,13 +808,13 @@ void LaplaceProblem::output_results () const // To write the output to a file, // we need an object which knows // about output formats and the - // like. This is the DataOut class, + // like. This is the DataOut class, // and we need an object of that // type: DataOut<2> data_out; // Now we have to tell it where to take the // values from which it shall write. We - // tell it which DoFHandler object to + // tell it which DoFHandler object to // use, and the solution vector (and // the name by which the solution variable // shall appear in the output file). If @@ -826,7 +831,7 @@ void LaplaceProblem::output_results () const // handle. The reason is that we // have separated the frontend // (which knows about how to treat - // DoFHandler objects and data + // DoFHandler objects and data // vectors) from the back end (which // knows many different output formats) // and use an intermediate data @@ -861,7 +866,8 @@ void LaplaceProblem::output_results () const // to comment about: void LaplaceProblem::run () { - make_grid_and_dofs (); + make_grid (); + setup_system(); assemble_system (); solve (); output_results (); diff --git a/deal.II/examples/step-4/step-4.cc b/deal.II/examples/step-4/step-4.cc index 2ac2020914..8ef4990a50 100644 --- a/deal.II/examples/step-4/step-4.cc +++ b/deal.II/examples/step-4/step-4.cc @@ -55,10 +55,10 @@ // into the global namespace: using namespace dealii; - // @sect3{The LaplaceProblem class template} + // @sect3{The Step4 class template} // This is again the same - // LaplaceProblem class as in the + // Step4 class as in the // previous example. The only // difference is that we have now // declared it as a class with a @@ -75,14 +75,15 @@ using namespace dealii; // respectively. Apart from this, // everything is as before. template -class LaplaceProblem +class Step4 { public: - LaplaceProblem (); + Step4 (); void run (); private: - void make_grid_and_dofs (); + void make_grid (); + void setup_system(); void assemble_system (); void solve (); void output_results () const; @@ -236,7 +237,7 @@ double BoundaryValues::value (const Point &p, - // @sect3{Implementation of the LaplaceProblem class} + // @sect3{Implementation of the Step4 class} // Next for the implementation of the class // template that makes use of the functions @@ -246,7 +247,7 @@ double BoundaryValues::value (const Point &p, // the time we define the template // functions. Only later, the compiler will // find a declaration of - // LaplaceProblem@<2@> (in the + // Step4@<2@> (in the // main function, actually) and // compile the entire class with // dim replaced by 2, a process @@ -260,16 +261,16 @@ double BoundaryValues::value (const Point &p, // // In fact, the compiler will also find a // declaration - // LaplaceProblem@<3@> in + // Step4@<3@> in // main(). This will cause it to // again go back to the general - // LaplaceProblem@ + // Step4@ // template, replace all occurrences of // dim, this time by 3, and // compile the class a second time. Note that // the two instantiations - // LaplaceProblem@<2@> and - // LaplaceProblem@<3@> are + // Step4@<2@> and + // Step4@<3@> are // completely independent classes; their only // common feature is that they are both // instantiated from the same general @@ -279,24 +280,24 @@ double BoundaryValues::value (const Point &p, // completely independently). - // @sect4{LaplaceProblem::LaplaceProblem} + // @sect4{Step4::Step4} // After this introduction, here is the - // constructor of the LaplaceProblem + // constructor of the Step4 // class. It specifies the desired polynomial // degree of the finite elements and // associates the DoFHandler to the // triangulation just as in the previous // example program, step-3: template -LaplaceProblem::LaplaceProblem () +Step4::Step4 () : fe (1), dof_handler (triangulation) {} - // @sect4{LaplaceProblem::make_grid_and_dofs} + // @sect4{Step4::make_grid} // Grid creation is something inherently // dimension dependent. However, as long as @@ -306,7 +307,7 @@ LaplaceProblem::LaplaceProblem () // solve on the square $[-1,1]\times [-1,1]$ // in 2D, or on the cube $[-1,1] \times // [-1,1] \times [-1,1]$ in 3D; both can be - // termed hyper_cube, so we may + // termed GridGenerator::hyper_cube(), so we may // use the same function in whatever // dimension we are. Of course, the functions // that create a hypercube in two and three @@ -314,22 +315,8 @@ LaplaceProblem::LaplaceProblem () // that is something you need not care // about. Let the library handle the // difficult things. - // - // Likewise, associating a degree of freedom - // with each vertex is something which - // certainly looks different in 2D and 3D, - // but that does not need to bother you - // either. This function therefore looks - // exactly like in the previous example, - // although it performs actions that in their - // details are quite different if - // dim happens to be 3. The only - // significant difference from a user's - // perspective is the number of cells - // resulting, which is much higher in three - // than in two space dimensions! template -void LaplaceProblem::make_grid_and_dofs () +void Step4::make_grid () { GridGenerator::hyper_cube (triangulation, -1, 1); triangulation.refine_global (4); @@ -340,7 +327,22 @@ void LaplaceProblem::make_grid_and_dofs () << " Total number of cells: " << triangulation.n_cells() << std::endl; +} + + // @sect4{Step4::setup_system} + // This function looks + // exactly like in the previous example, + // although it performs actions that in their + // details are quite different if + // dim happens to be 3. The only + // significant difference from a user's + // perspective is the number of cells + // resulting, which is much higher in three + // than in two space dimensions! +template +void Step4::setup_system () +{ dof_handler.distribute_dofs (fe); std::cout << " Number of degrees of freedom: " @@ -358,7 +360,7 @@ void LaplaceProblem::make_grid_and_dofs () } - // @sect4{LaplaceProblem::assemble_system} + // @sect4{Step4::assemble_system} // Unlike in the previous example, we // would now like to use a @@ -391,7 +393,7 @@ void LaplaceProblem::make_grid_and_dofs () // don't have to care about most // things. template -void LaplaceProblem::assemble_system () +void Step4::assemble_system () { QGauss quadrature_formula(2); @@ -411,12 +413,12 @@ void LaplaceProblem::assemble_system () // presently on (previously, we only // required values and gradients of the // shape function from the - // FEValues object, as well as + // FEValues object, as well as // the quadrature weights, - // JxW). We can tell the - // FEValues object to do for + // FEValues::JxW() ). We can tell the + // FEValues object to do for // us by also giving it the - // update_quadrature_points + // #update_quadrature_points // flag: FEValues fe_values (fe, quadrature_formula, update_values | update_gradients | @@ -551,7 +553,7 @@ void LaplaceProblem::assemble_system () // values in this example, unlike the one // before. This is a simple task, we only // have to replace the - // ZeroFunction used there by + // ZeroFunction used there by // an object of the class which describes // the boundary values we would like to use // (i.e. the BoundaryValues @@ -568,7 +570,7 @@ void LaplaceProblem::assemble_system () } - // @sect4{LaplaceProblem::solve} + // @sect4{Step4::solve} // Solving the linear system of // equations is something that looks @@ -578,7 +580,7 @@ void LaplaceProblem::assemble_system () // function is copied verbatim from the // previous example. template -void LaplaceProblem::solve () +void Step4::solve () { SolverControl solver_control (1000, 1e-12); SolverCG<> solver (solver_control); @@ -596,7 +598,7 @@ void LaplaceProblem::solve () } - // @sect4{LaplaceProblem::output_results} + // @sect4{Step4::output_results} // This function also does what the // respective one did in step-3. No changes @@ -625,7 +627,7 @@ void LaplaceProblem::solve () // than 2 or 3, but we neglect this here for // the sake of brevity). template -void LaplaceProblem::output_results () const +void Step4::output_results () const { DataOut data_out; @@ -642,7 +644,7 @@ void LaplaceProblem::output_results () const - // @sect4{LaplaceProblem::run} + // @sect4{Step4::run} // This is the function which has the // top-level control over @@ -650,11 +652,12 @@ void LaplaceProblem::output_results () const // additional output, it is the same // as for the previous example. template -void LaplaceProblem::run () +void Step4::run () { std::cout << "Solving problem in " << dim << " space dimensions." << std::endl; - make_grid_and_dofs(); + make_grid(); + setup_system (); assemble_system (); solve (); output_results (); @@ -667,7 +670,7 @@ void LaplaceProblem::run () // looks mostly like in step-3, but if you // look at the code below, note how we first // create a variable of type - // LaplaceProblem@<2@> (forcing + // Step4@<2@> (forcing // the compiler to compile the class template // with dim replaced by // 2) and run a 2d simulation, @@ -740,12 +743,12 @@ int main () { deallog.depth_console (0); { - LaplaceProblem<2> laplace_problem_2d; + Step4<2> laplace_problem_2d; laplace_problem_2d.run (); } { - LaplaceProblem<3> laplace_problem_3d; + Step4<3> laplace_problem_3d; laplace_problem_3d.run (); }