From: Wolfgang Bangerth Date: Thu, 9 May 2024 07:13:18 +0000 (+0530) Subject: Get rid of the use of deallog in step-3 and friends. X-Git-Tag: v9.6.0-rc1~295^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f912e87dd1ef9f7c3f05cb9e67a9a56fae85c5a2;p=dealii.git Get rid of the use of deallog in step-3 and friends. Simply copy what we already in step-4 and nearly everywhere else. --- diff --git a/examples/doxygen/step_3_mixed.cc b/examples/doxygen/step_3_mixed.cc index d6b10ec93b..98ccbf30f9 100644 --- a/examples/doxygen/step_3_mixed.cc +++ b/examples/doxygen/step_3_mixed.cc @@ -355,6 +355,9 @@ void Step3::solve() SolverControl solver_control(1000, 1e-12); SolverCG> solver(solver_control); solver.solve(system_matrix, solution, system_rhs, PreconditionIdentity()); + + std::cout << solver_control.last_step() + << " CG iterations needed to obtain convergence." << std::endl; } @@ -395,8 +398,6 @@ void Step3::run() // Nothing has changed here. int main() { - deallog.depth_console(2); - Step3 laplace_problem; laplace_problem.run(); diff --git a/examples/doxygen/step_3_simplex.cc b/examples/doxygen/step_3_simplex.cc index 5f5d8aa2f0..66fee20c81 100644 --- a/examples/doxygen/step_3_simplex.cc +++ b/examples/doxygen/step_3_simplex.cc @@ -275,6 +275,9 @@ void Step3::solve() SolverControl solver_control(1000, 1e-12); SolverCG> solver(solver_control); solver.solve(system_matrix, solution, system_rhs, PreconditionIdentity()); + + std::cout << solver_control.last_step() + << " CG iterations needed to obtain convergence." << std::endl; } @@ -315,8 +318,6 @@ void Step3::run() // Nothing has changed here. int main() { - deallog.depth_console(2); - Step3 laplace_problem; laplace_problem.run(); diff --git a/examples/step-3/doc/results.dox b/examples/step-3/doc/results.dox index 9c7a582999..7da1994a7e 100644 --- a/examples/step-3/doc/results.dox +++ b/examples/step-3/doc/results.dox @@ -4,26 +4,12 @@ The output of the program looks as follows: @code Number of active cells: 1024 Number of degrees of freedom: 1089 -DEAL:cg::Starting value 0.121094 -DEAL:cg::Convergence step 36 value 7.07761e-08 +36 CG iterations needed to obtain convergence. Output written to solution.vtk @endcode -The first two lines is what we wrote to std::cout. The next -two lines were generated by the CG solver because we called -`deallog.depth_console(2);` in the `main()` function -- see -the lengthy discussion there. These two lines prefixed by `DEAL:cg:` state -the residual at the start of the -iteration and that the solver needed 36 -iterations to bring the norm of the residual to $7.08\cdot 10^{-8}$, i.e. below -the threshold $\tau$ which we have set in the `solve()` function. -Note that we have asked the residual to be less than $10^{-6}$ times the -norm of the right hand side vector; the starting value is (here) this -norm of the right hand side vector, and there is indeed more than a -factor of $10^{-6}$ between the two values shown. - The last line is the output we generated at the bottom of the -`output_results()` function. The program also generated the file +`output_results()` function: The program generated the file solution.vtk, which is in the VTK format that is widely used by many visualization programs today -- including the two heavy-weights VisIt and diff --git a/examples/step-3/step-3.cc b/examples/step-3/step-3.cc index 148c0ec855..23f52ce1e9 100644 --- a/examples/step-3/step-3.cc +++ b/examples/step-3/step-3.cc @@ -550,12 +550,17 @@ void Step3::assemble_system() // constructing better preconditioners. // // At the end of this process, the `solution` variable contains the -// nodal values of the solution function. +// nodal values of the solution function. At the end of the function, we +// output how many Conjugate Gradients iterations it took to solve the +// linear system. void Step3::solve() { SolverControl solver_control(1000, 1e-6 * system_rhs.l2_norm()); SolverCG> solver(solver_control); solver.solve(system_matrix, solution, system_rhs, PreconditionIdentity()); + + std::cout << solver_control.last_step() + << " CG iterations needed to obtain convergence." << std::endl; } @@ -625,55 +630,8 @@ void Step3::run() // before C++ programming, it often does not do much more than // creating an object of the top-level class and calling its principle // function. -// -// Finally, the first line of the function is used to enable output of -// some diagnostics that deal.II can generate. The @p deallog -// variable (which stands for deal-log, not de-allog) represents a -// stream to which some parts of the library write output. For -// example, iterative solvers will generate diagnostics (starting -// residual, number of solver steps, final residual) as can be seen -// when running this tutorial program. -// -// The output of @p deallog can be written to the console, to a file, -// or both. Both are disabled by default since over the years we have -// learned that a program should only generate output when a user -// explicitly asks for it. But this can be changed, and to explain how -// this can be done, we need to explain how @p deallog works: When -// individual parts of the library want to log output, they open a -// "context" or "section" into which this output will be placed. At -// the end of the part that wants to write output, one exits this -// section again. Since a function may call another one from within -// the scope where this output section is open, output may in fact be -// nested hierarchically into these sections. The LogStream class of -// which @p deallog is a variable calls each of these sections a -// "prefix" because all output is printed with this prefix at the left -// end of the line, with prefixes separated by colons. There is always -// a default prefix called "DEAL" (a hint at deal.II's history as the -// successor of a previous library called "DEAL" and from which the -// LogStream class is one of the few pieces of code that were taken -// into deal.II). -// -// By default, @p logstream only outputs lines with zero prefixes -- -// i.e., all output is disabled because the default "DEAL" prefix is -// always there. But one can set a different maximal number of -// prefixes for lines that should be output to something larger, and -// indeed here we set it to two by calling -// LogStream::depth_console(). This means that for all screen output, -// a context that has pushed one additional prefix beyond the default -// "DEAL" is allowed to print its output to the screen ("console"), -// whereas all further nested sections that would have three or more -// prefixes active would write to @p deallog, but @p deallog does not -// forward this output to the screen. Thus, running this example (or -// looking at the "Results" section), you will see the solver -// statistics prefixed with "DEAL:CG", which is two prefixes. This is -// sufficient for the context of the current program, but you will see -// examples later on (e.g., in step-22) where solvers are nested more -// deeply and where you may get useful information by setting the -// depth even higher. int main() { - deallog.depth_console(2); - Step3 laplace_problem; laplace_problem.run(); diff --git a/examples/step-4/step-4.cc b/examples/step-4/step-4.cc index dabf47ba7f..51794719de 100644 --- a/examples/step-4/step-4.cc +++ b/examples/step-4/step-4.cc @@ -453,8 +453,6 @@ void Step4::solve() SolverCG> solver(solver_control); solver.solve(system_matrix, solution, system_rhs, PreconditionIdentity()); - // We have made one addition, though: since we suppress output from the - // linear solvers, we have to print the number of iterations by hand. std::cout << " " << solver_control.last_step() << " CG iterations needed to obtain convergence." << std::endl; } diff --git a/include/deal.II/base/logstream.h b/include/deal.II/base/logstream.h index 8bdf3ce4f8..6b7d0a8b4c 100644 --- a/include/deal.II/base/logstream.h +++ b/include/deal.II/base/logstream.h @@ -412,7 +412,45 @@ operator<<(LogStream &log, const T &t) /** - * The standard log object of deal.II: + * The standard log object of deal.II. Take a look at the documentation of + * the LogStream class for more information, as well as below. + * + * The @p deallog + * variable (which stands for deal-log, not de-allog) represents a + * stream to which some parts of the library write output. For + * example, iterative solvers will generate diagnostics (starting + * residual, number of solver steps, final residual). + * + * The output of @p deallog can be written to the console, to a file, + * or both. Both are disabled by default since over the years we have + * learned that a program should only generate output when a user + * explicitly asks for it. But this can be changed, and to explain how + * this can be done, we need to explain how @p deallog works: When + * individual parts of the library want to log output, they open a + * "context" or "section" into which this output will be placed. At + * the end of the part that wants to write output, one exits this + * section again. Since a function may call another one from within + * the scope where this output section is open, output may in fact be + * nested hierarchically into these sections. The LogStream class of + * which @p deallog is a variable calls each of these sections a + * "prefix" because all output is printed with this prefix at the left + * end of the line, with prefixes separated by colons. There is always + * a default prefix called "DEAL" (a hint at deal.II's history as the + * successor of a previous library called "DEAL" and from which the + * LogStream class is one of the few pieces of code that were taken + * into deal.II). + * + * By default, @p logstream only outputs lines with zero prefixes -- + * i.e., all output is disabled because the default "DEAL" prefix is + * always there. But one can set a different maximal number of + * prefixes for lines that should be output to something larger, + * by calling LogStream::depth_console() with an integer argument. Let's + * assume you call it with `2` as argument. This means that for all screen + * output a context that has pushed one additional prefix beyond the default + * "DEAL" is allowed to print its output to the screen ("console"), + * whereas all further nested sections that would have three or more + * prefixes active would write to @p deallog, but @p deallog does not + * forward this output to the screen. */ extern LogStream deallog; diff --git a/tests/examples/step-3.diff b/tests/examples/step-3.diff index ee0abdb272..e69de29bb2 100644 --- a/tests/examples/step-3.diff +++ b/tests/examples/step-3.diff @@ -1,4 +0,0 @@ -675c675 -< deallog.depth_console(2); ---- -> //deallog.depth_console(2); diff --git a/tests/examples/step-3.output b/tests/examples/step-3.output index 34ffa9f0ad..f4c4eb80a5 100644 --- a/tests/examples/step-3.output +++ b/tests/examples/step-3.output @@ -1,3 +1,4 @@ Number of active cells: 1024 Number of degrees of freedom: 1089 +36 CG iterations needed to obtain convergence. Output written to solution.vtk