I used the methodology of step-18 for step-17 which showed memory usage 
improvements. I have attached a new version of step-17 that allow selecting 
between the NEW and OLD. I also attached the results from both and another 
plot. I don't know whether this version will work with more than one 
processor I run only on 1. It might be helpful if someone would try to run 
it and report success or failure.

One final thing, I left solution (was incremental_displacement) as a 
Vector<double> vs, PETScWrappers::MPI::Vector. This may add a little memory 
and/or slow it down slightly, but for my purposes modifying the program 
faster was for me better and sufficient.

Thanks All

Pete Griffin

On Monday, July 25, 2016 at 1:59:53 PM UTC-4, Pete Griffin wrote:
>
> I found that running step-8 and step-17 on a single processor Intel® Core™ 
> i7-3630QM CPU @ 2.40GHz × 8 used substantially more Peak resident memory (> 
> 5x) than I thought it would. This surprised me since I thought from reading 
> step-17 that the memory increase was on the order of the solution vector 
> which should have been << 2x greater. I verified some of the larger memory 
> usage numbers using top.
>
> Is my assumption correct that 5x Peak resident memory is more than it 
> should be?
>
> The results of other simulations with a beam with body-force load and with 
> traction loads with and without HP and with and without MPI/PETSc all show 
> the same results and they agree with beam theory.
>
> The output of the modified step-8.cc and step-17.cc are attached along 
> with a plot of peak virtual memory and peak resident memory
> vs. DOF. The changes between the original distributed step-8 and step-17, 
> with comments and extra newlines excluded (modified file >), are as below:
>
> Thanks beforehand
>
> Pete Griffin
>
>
> ======================================================================================
> diff ~/Documents/Zipstore2/dealii-8.4.1-PETSc/examples/step-8/step-8.cc 
> step-8.cc
> 56c47,48
> < // This again is C++:
> ---
> > #include <deal.II/base/utilities.h>
>
> 767a394,402
> >         
> >         Utilities::System::MemoryStats stats;
> >         Utilities::System::get_memory_stats(stats);
> >         std::stringstream Str;
> >         
> >         Str.str("");
> >         Str << "   Peak virtual memory: " << stats.VmSize/1024 << " MB, 
> Peak resident memory: "
> >                << stats.VmRSS/1024 << " MB" << std::endl;
> >         std::cout << Str.str();
>
> 781c411
> <       Step8::ElasticProblem<2> elastic_problem_2d;
> ---
> >       Step8::ElasticProblem<3> elastic_problem_2d;
>
>
> ======================================================================================
> diff ~/Documents/Zipstore2/dealii-8.4.1-PETSc/examples/step-17/step-17.cc 
> ../step-17/step-17.cc
> 84a50
> > #include <deal.II/base/utilities.h>
> 1015c355
> <     for (unsigned int cycle=0; cycle<10; ++cycle)
> ---
> >     for (unsigned int cycle=0; cycle<8; ++cycle)
> 1018d357
> < 
> 1022c361
> <             triangulation.refine_global (3);
> ---
> >             triangulation.refine_global (2);
> 1049a383,391
> > 
> >         Utilities::System::MemoryStats stats;
> >         Utilities::System::get_memory_stats(stats);
> >         std::stringstream Str;
> >         
> >         Str.str("");
> >         Str << "   Peak virtual memory: " << stats.VmSize/1024 << " MB, 
> Peak resident memory: "
> >                << stats.VmRSS/1024 << " MB" << std::endl;
> >         std::cout << Str.str();
> 1073,1074c403
> < 
> <       ElasticProblem<2> elastic_problem;
> ---
> >       ElasticProblem<3> elastic_problem;
>
>
> =============================================================================================
>
>

-- 
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see 
https://groups.google.com/d/forum/dealii?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
$ make release run
-> NEW Code
Cycle 0:
   Number of active cells:       64
   Number of degrees of freedom: 375 (by partition: 375)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 6 iterations.
   Peak virtual memory: 797 MB, Peak resident memory: 64 MB
Cycle 1:
   Number of active cells:       204
   Number of degrees of freedom: 1059 (by partition: 1059)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 10 iterations.
   Peak virtual memory: 798 MB, Peak resident memory: 68 MB
Cycle 2:
   Number of active cells:       708
   Number of degrees of freedom: 3231 (by partition: 3231)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 17 iterations.
   Peak virtual memory: 802 MB, Peak resident memory: 72 MB
Cycle 3:
   Number of active cells:       2388
   Number of degrees of freedom: 9891 (by partition: 9891)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 29 iterations.
   Peak virtual memory: 810 MB, Peak resident memory: 80 MB
Cycle 4:
   Number of active cells:       7484
   Number of degrees of freedom: 29277 (by partition: 29277)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 48 iterations.
   Peak virtual memory: 832 MB, Peak resident memory: 102 MB
Cycle 5:
   Number of active cells:       23220
   Number of degrees of freedom: 82779 (by partition: 82779)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 74 iterations.
   Peak virtual memory: 909 MB, Peak resident memory: 179 MB
Cycle 6:
   Number of active cells:       71996
   Number of degrees of freedom: 247185 (by partition: 247185)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 113 iterations.
   Peak virtual memory: 1131 MB, Peak resident memory: 401 MB
Cycle 7:
   Number of active cells:       223140
   Number of degrees of freedom: 743361 (by partition: 743361)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 156 iterations.
   Peak virtual memory: 1849 MB, Peak resident memory: 1120 MB
[100%] Built target run


$ make release run
-> OLD Code
Cycle 0:
   Number of active cells:       64
   Number of degrees of freedom: 375 (by partition: 375)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 6 iterations.
   Peak virtual memory: 786 MB, Peak resident memory: 55 MB
Cycle 1:
   Number of active cells:       204
   Number of degrees of freedom: 1059 (by partition: 1059)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 10 iterations.
   Peak virtual memory: 798 MB, Peak resident memory: 71 MB
Cycle 2:
   Number of active cells:       708
   Number of degrees of freedom: 3231 (by partition: 3231)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 17 iterations.
   Peak virtual memory: 825 MB, Peak resident memory: 97 MB
Cycle 3:
   Number of active cells:       2388
   Number of degrees of freedom: 9891 (by partition: 9891)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 29 iterations.
   Peak virtual memory: 908 MB, Peak resident memory: 181 MB
Cycle 4:
   Number of active cells:       7484
   Number of degrees of freedom: 29277 (by partition: 29277)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 48 iterations.
   Peak virtual memory: 1142 MB, Peak resident memory: 415 MB
Cycle 5:
   Number of active cells:       23220
   Number of degrees of freedom: 82779 (by partition: 82779)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 74 iterations.
   Peak virtual memory: 1798 MB, Peak resident memory: 1070 MB
Cycle 6:
   Number of active cells:       71996
   Number of degrees of freedom: 247185 (by partition: 247185)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 113 iterations.
   Peak virtual memory: 3771 MB, Peak resident memory: 3044 MB
Cycle 7:
   Number of active cells:       223140
   Number of degrees of freedom: 743361 (by partition: 743361)
PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
   Solver converged in 156 iterations.
   Peak virtual memory: 9703 MB, Peak resident memory: 8861 MB
[100%] Built target run

/* ---------------------------------------------------------------------
 *
 * Copyright (C) 2000 - 2016 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.
 *
 * ---------------------------------------------------------------------
 *
 * Author: Wolfgang Bangerth, University of Texas at Austin, 2000, 2004
 *         Wolfgang Bangerth, Texas A&M University, 2016
 */
#define NEW 1
//#undef NEW

#include <deal.II/base/quadrature_lib.h>
#include <deal.II/base/function.h>
#include <deal.II/base/logstream.h>
#include <deal.II/base/multithread_info.h>
#include <deal.II/lac/vector.h>
#include <deal.II/lac/full_matrix.h>
#include <deal.II/lac/constraint_matrix.h>
#include <deal.II/grid/tria.h>
#include <deal.II/grid/grid_generator.h>
#include <deal.II/grid/grid_refinement.h>
#include <deal.II/grid/tria_accessor.h>
#include <deal.II/grid/tria_iterator.h>
#include <deal.II/grid/tria_boundary_lib.h>
#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_accessor.h>
#include <deal.II/dofs/dof_tools.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/fe/fe_system.h>
#include <deal.II/fe/fe_q.h>
#include <deal.II/numerics/vector_tools.h>
#include <deal.II/numerics/matrix_tools.h>
#include <deal.II/numerics/data_out.h>
#include <deal.II/numerics/error_estimator.h>
#include <deal.II/base/conditional_ostream.h>
#include <deal.II/base/mpi.h>
#include <deal.II/lac/petsc_parallel_vector.h>
#include <deal.II/lac/petsc_parallel_sparse_matrix.h>
#include <deal.II/lac/petsc_solver.h>
#include <deal.II/lac/petsc_precondition.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/dofs/dof_renumbering.h>
#include <deal.II/base/utilities.h>

#if NEW
#include <deal.II/distributed/shared_tria.h>
#include <deal.II/grid/filtered_iterator.h>
#include <deal.II/base/index_set.h>
#include <deal.II/lac/sparsity_tools.h>
#endif

#include <fstream>
#include <iostream>
#include <sstream>

namespace Step17
{
  using namespace dealii;
  template <int dim>
  class ElasticProblem
  {
  public:
    ElasticProblem ();
    ~ElasticProblem ();
    void run ();
  private:
    void setup_system ();
    void assemble_system ();
    unsigned int solve ();
    void refine_grid ();
    void output_results (const unsigned int cycle) const;
#if NEW
    std::vector<types::global_dof_index> local_dofs_per_process;
    parallel::shared::Triangulation<dim>   triangulation;
#else
    Triangulation<dim>   triangulation;
#endif
    MPI_Comm mpi_communicator;
    const unsigned int n_mpi_processes;
    const unsigned int this_mpi_process;
    ConditionalOStream pcout;

    DoFHandler<dim>      dof_handler;
    FESystem<dim>        fe;
    ConstraintMatrix     hanging_node_constraints;
    PETScWrappers::MPI::SparseMatrix system_matrix;
#if NEW
    Vector<double>       solution; // was incremental_displacement in step18
    IndexSet locally_owned_dofs;
    IndexSet locally_relevant_dofs;
    unsigned int n_local_cells;
#else
    PETScWrappers::MPI::Vector       solution;
#endif
    PETScWrappers::MPI::Vector       system_rhs;
  };
  template <int dim>
  class RightHandSide :  public Function<dim>
  {
  public:
    RightHandSide ();
    virtual void vector_value (const Point<dim> &p,
                               Vector<double>   &values) const;
    virtual void vector_value_list (const std::vector<Point<dim> > &points,
                                    std::vector<Vector<double> >   &value_list) const;
  };
  template <int dim>
  RightHandSide<dim>::RightHandSide () :
    Function<dim> (dim)
  {}
  template <int dim>
  inline
  void RightHandSide<dim>::vector_value (const Point<dim> &p,
                                         Vector<double>   &values) const
  {
    Assert (values.size() == dim,
            ExcDimensionMismatch (values.size(), dim));
    Assert (dim >= 2, ExcInternalError());
    Point<dim> point_1, point_2;
    point_1(0) = 0.5;
    point_2(0) = -0.5;
    if (((p-point_1).norm_square() < 0.2*0.2) ||
        ((p-point_2).norm_square() < 0.2*0.2))
      values(0) = 1;
    else
      values(0) = 0;
    if (p.square() < 0.2*0.2)
      values(1) = 1;
    else
      values(1) = 0;
  }
  template <int dim>
  void RightHandSide<dim>::vector_value_list (const std::vector<Point<dim> > &points,
                                              std::vector<Vector<double> >   &value_list) const
  {
    const unsigned int n_points = points.size();
    Assert (value_list.size() == n_points,
            ExcDimensionMismatch (value_list.size(), n_points));
    for (unsigned int p=0; p<n_points; ++p)
      RightHandSide<dim>::vector_value (points[p],
                                        value_list[p]);
  }

  template <int dim>
  ElasticProblem<dim>::ElasticProblem ()
    :
#if NEW
    triangulation(MPI_COMM_WORLD),
#endif
    mpi_communicator (MPI_COMM_WORLD),
    n_mpi_processes (Utilities::MPI::n_mpi_processes(mpi_communicator)),
    this_mpi_process (Utilities::MPI::this_mpi_process(mpi_communicator)),
    pcout (std::cout,
           (this_mpi_process == 0)),
    dof_handler (triangulation),
    fe (FE_Q<dim>(1), dim)
  {}
  
  template <int dim>
  ElasticProblem<dim>::~ElasticProblem ()
  {
    dof_handler.clear ();
  }
  
  template <int dim>
  void ElasticProblem<dim>::setup_system ()
  {
#if NEW
    dof_handler.distribute_dofs (fe);
    locally_owned_dofs = dof_handler.locally_owned_dofs();
    DoFTools::extract_locally_relevant_dofs (dof_handler,locally_relevant_dofs);

    n_local_cells
      = GridTools::count_cells_with_subdomain_association (triangulation,
                                                           triangulation.locally_owned_subdomain ());

    local_dofs_per_process = dof_handler.n_locally_owned_dofs_per_processor();

    hanging_node_constraints.clear ();
    DoFTools::make_hanging_node_constraints (dof_handler,
                                             hanging_node_constraints);
    hanging_node_constraints.close ();

    DynamicSparsityPattern sparsity_pattern (locally_relevant_dofs);
    DoFTools::make_sparsity_pattern (dof_handler, sparsity_pattern,
                                     hanging_node_constraints, /*keep constrained dofs*/ false);
    SparsityTools::distribute_sparsity_pattern (sparsity_pattern,
                                                local_dofs_per_process,
                                                mpi_communicator,
                                                locally_relevant_dofs);
    system_matrix.reinit (locally_owned_dofs,
                          locally_owned_dofs,
                          sparsity_pattern,
                          mpi_communicator);

    system_rhs.reinit(locally_owned_dofs,mpi_communicator);
    solution.reinit (dof_handler.n_dofs());
#else
    GridTools::partition_triangulation (n_mpi_processes, triangulation);
    dof_handler.distribute_dofs (fe);
    DoFRenumbering::subdomain_wise (dof_handler);
    const types::global_dof_index n_local_dofs
      = DoFTools::count_dofs_with_subdomain_association (dof_handler,
                                                         this_mpi_process);
    system_matrix.reinit (mpi_communicator,
                          dof_handler.n_dofs(),
                          dof_handler.n_dofs(),
                          n_local_dofs,
                          n_local_dofs,
                          dof_handler.max_couplings_between_dofs());
    solution.reinit (mpi_communicator, dof_handler.n_dofs(), n_local_dofs);
    system_rhs.reinit (mpi_communicator, dof_handler.n_dofs(), n_local_dofs);
    hanging_node_constraints.clear ();
    DoFTools::make_hanging_node_constraints (dof_handler,
                                             hanging_node_constraints);
    hanging_node_constraints.close ();
#endif
  }
  
  template <int dim>
  void ElasticProblem<dim>::assemble_system ()
  {
    QGauss<dim>  quadrature_formula(2);
    FEValues<dim> 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<double>   cell_matrix (dofs_per_cell, dofs_per_cell);
    Vector<double>       cell_rhs (dofs_per_cell);
    std::vector<types::global_dof_index> local_dof_indices (dofs_per_cell);
    std::vector<double>     lambda_values (n_q_points);
    std::vector<double>     mu_values (n_q_points);
    ConstantFunction<dim> lambda(1.), mu(1.);
    RightHandSide<dim>      right_hand_side;
    std::vector<Vector<double> > rhs_values (n_q_points,
                                             Vector<double>(dim));
    typename DoFHandler<dim>::active_cell_iterator
    cell = dof_handler.begin_active(),
    endc = dof_handler.end();
    for (; cell!=endc; ++cell)
      if (cell->subdomain_id() == this_mpi_process)
        {
          cell_matrix = 0;
          cell_rhs = 0;
          fe_values.reinit (cell);
          lambda.value_list (fe_values.get_quadrature_points(), lambda_values);
          mu.value_list     (fe_values.get_quadrature_points(), mu_values);
          for (unsigned int i=0; i<dofs_per_cell; ++i)
            {
              const unsigned int
              component_i = fe.system_to_component_index(i).first;
              for (unsigned int j=0; j<dofs_per_cell; ++j)
                {
                  const unsigned int
                  component_j = fe.system_to_component_index(j).first;
                  for (unsigned int q_point=0; q_point<n_q_points;
                       ++q_point)
                    {
                      cell_matrix(i,j)
                      +=
                        (
                          (fe_values.shape_grad(i,q_point)[component_i] *
                           fe_values.shape_grad(j,q_point)[component_j] *
                           lambda_values[q_point])
                          +
                          (fe_values.shape_grad(i,q_point)[component_j] *
                           fe_values.shape_grad(j,q_point)[component_i] *
                           mu_values[q_point])
                          +
                          ((component_i == component_j) ?
                           (fe_values.shape_grad(i,q_point) *
                            fe_values.shape_grad(j,q_point) *
                            mu_values[q_point])  :
                           0)
                        )
                        *
                        fe_values.JxW(q_point);
                    }
                }
            }
          right_hand_side.vector_value_list (fe_values.get_quadrature_points(),
                                             rhs_values);
          for (unsigned int i=0; i<dofs_per_cell; ++i)
            {
              const unsigned int
              component_i = fe.system_to_component_index(i).first;
              for (unsigned int q_point=0; q_point<n_q_points; ++q_point)
                cell_rhs(i) += fe_values.shape_value(i,q_point) *
                               rhs_values[q_point](component_i) *
                               fe_values.JxW(q_point);
            }
          cell->get_dof_indices (local_dof_indices);
          hanging_node_constraints
          .distribute_local_to_global(cell_matrix, cell_rhs,
                                      local_dof_indices,
                                      system_matrix, system_rhs);
        }
    system_matrix.compress(VectorOperation::add);
    system_rhs.compress(VectorOperation::add);
    std::map<types::global_dof_index,double> boundary_values;
    VectorTools::interpolate_boundary_values (dof_handler,
                                              0,
                                              ZeroFunction<dim>(dim),
                                              boundary_values);
#if NEW
    PETScWrappers::MPI::Vector tmp (locally_owned_dofs,mpi_communicator);
    MatrixTools::apply_boundary_values (boundary_values,
                                        system_matrix, tmp,
                                        system_rhs, false);
    solution = tmp;
#else
    MatrixTools::apply_boundary_values (boundary_values,
                                        system_matrix,
                                        solution,
                                        system_rhs,
                                        false);
#endif
  }
  
  template <int dim>
  unsigned int ElasticProblem<dim>::solve ()
  {
#if NEW
    PETScWrappers::MPI::Vector
      distributed_solution (locally_owned_dofs, mpi_communicator); // was distributed_incremental_displacement in step18
    distributed_solution = solution;
    SolverControl           solver_control (dof_handler.n_dofs(),
                                            1e-8*system_rhs.l2_norm()); //????
#else
    SolverControl           solver_control (solution.size(),
                                            1e-8*system_rhs.l2_norm());
#endif
    PETScWrappers::SolverCG cg (solver_control,
                                mpi_communicator);
    PETScWrappers::PreconditionBlockJacobi preconditioner(system_matrix);
#if NEW
    cg.solve (system_matrix, distributed_solution, system_rhs,
              preconditioner);
    solution = distributed_solution;
#else
    cg.solve (system_matrix, solution, system_rhs,
              preconditioner);
#endif
    Vector<double> localized_solution (solution);
    hanging_node_constraints.distribute (localized_solution);
    solution = localized_solution;
    return solver_control.last_step();
  }
  
  template <int dim>
  void ElasticProblem<dim>::refine_grid ()
  {
#ifdef NEW
    Vector<float> error_per_cell (triangulation.n_active_cells());
    KellyErrorEstimator<dim>::estimate (dof_handler,
                                        QGauss<dim-1>(2),
                                        typename FunctionMap<dim>::type(),
                                        solution,
                                        error_per_cell,
                                        ComponentMask(),
                                        0,
                                        MultithreadInfo::n_threads(),
                                        this_mpi_process);

    const unsigned int n_local_cells = triangulation.n_locally_owned_active_cells ();

    PETScWrappers::MPI::Vector
    distributed_error_per_cell (mpi_communicator,
                                triangulation.n_active_cells(),
                                n_local_cells);

    for (unsigned int i=0; i<error_per_cell.size(); ++i)
      if (error_per_cell(i) != 0)
        distributed_error_per_cell(i) = error_per_cell(i);
    distributed_error_per_cell.compress (VectorOperation::insert);

    error_per_cell = distributed_error_per_cell;
    GridRefinement::refine_and_coarsen_fixed_number (triangulation,
                                                     error_per_cell,
                                                     0.3, 0.03);
    triangulation.execute_coarsening_and_refinement ();
#else
    const Vector<double> localized_solution (solution);

    Vector<float> local_error_per_cell (triangulation.n_active_cells());
    KellyErrorEstimator<dim>::estimate (dof_handler,
                                        QGauss<dim-1>(2),
                                        typename FunctionMap<dim>::type(),
                                        localized_solution,
                                        local_error_per_cell,
                                        ComponentMask(),
                                        0,
                                        MultithreadInfo::n_threads(),
                                        this_mpi_process);
    const unsigned int n_local_cells
      = GridTools::count_cells_with_subdomain_association (triangulation,
                                                           this_mpi_process);
    PETScWrappers::MPI::Vector
    distributed_all_errors (mpi_communicator,
                            triangulation.n_active_cells(),
                            n_local_cells);

    for (unsigned int i=0; i<local_error_per_cell.size(); ++i)
      if (local_error_per_cell(i) != 0)
        distributed_all_errors(i) = local_error_per_cell(i);
    distributed_all_errors.compress (VectorOperation::insert);
    const Vector<float> localized_all_errors (distributed_all_errors);

    GridRefinement::refine_and_coarsen_fixed_number (triangulation,
                                                     localized_all_errors,
                                                     0.3, 0.03);
    triangulation.execute_coarsening_and_refinement ();
#endif
  }
  
  template <int dim>
  void ElasticProblem<dim>::output_results (const unsigned int cycle) const
  {
    const Vector<double> localized_solution (solution);
    if (this_mpi_process == 0)
      {
        std::ostringstream filename;
#if NEW
        filename << "sol-NEW-" << cycle << ".vtk";
#else
        filename << "sol-" << cycle << ".vtk";
#endif
        std::ofstream output (filename.str().c_str());
        DataOut<dim> data_out;
        data_out.attach_dof_handler (dof_handler);
        std::vector<std::string> solution_names;
        switch (dim)
          {
          case 1:
            solution_names.push_back ("displacement");
            break;
          case 2:
            solution_names.push_back ("x_displacement");
            solution_names.push_back ("y_displacement");
            break;
          case 3:
            solution_names.push_back ("x_displacement");
            solution_names.push_back ("y_displacement");
            solution_names.push_back ("z_displacement");
            break;
          default:
            Assert (false, ExcInternalError());
          }
        data_out.add_data_vector (localized_solution, solution_names);
        std::vector<unsigned int> partition_int (triangulation.n_active_cells());
        GridTools::get_subdomain_association (triangulation, partition_int);
        const Vector<double> partitioning(partition_int.begin(),
                                          partition_int.end());
        data_out.add_data_vector (partitioning, "partitioning");
        data_out.build_patches ();
        data_out.write_vtk (output);
      }
  }
  
  template <int dim>
  void ElasticProblem<dim>::run ()
  {
    for (unsigned int cycle=0; cycle<8; ++cycle)
      {
        pcout << "Cycle " << cycle << ':' << std::endl;
        if (cycle == 0)
          {
            GridGenerator::hyper_cube (triangulation, -1, 1);
            triangulation.refine_global (2);
          }
        else
          refine_grid ();
        pcout << "   Number of active cells:       "
              << triangulation.n_active_cells()
              << std::endl;
        setup_system ();
        pcout << "   Number of degrees of freedom: "
              << dof_handler.n_dofs()
              << " (by partition:";
        for (unsigned int p=0; p<n_mpi_processes; ++p)
          pcout << (p==0 ? ' ' : '+')
                << (DoFTools::
                    count_dofs_with_subdomain_association (dof_handler,
                                                           p));
        pcout << ")" << std::endl;
        assemble_system ();
        const unsigned int n_iterations = solve ();
        pcout << "   Solver converged in " << n_iterations
              << " iterations." << std::endl;
        output_results (cycle);
#if 1
        Utilities::System::MemoryStats stats;
        Utilities::System::get_memory_stats(stats);
        std::stringstream Str;
        
        Str.str("");
        Str << "   Peak virtual memory: " << stats.VmSize/1024 << " MB, Peak resident memory: "
               << stats.VmRSS/1024 << " MB" << std::endl;
        std::cout << Str.str();
#endif
      }
  }
}

int main (int argc, char **argv)
{
#if NEW
  std::cout << "NEW Code" << std::endl;
#else
  std::cout << "OLD Code" << std::endl;
#endif
  try
    {
      using namespace dealii;
      using namespace Step17;
      Utilities::MPI::MPI_InitFinalize mpi_initialization(argc, argv, 1);
      ElasticProblem<3> elastic_problem;
      elastic_problem.run ();
    }
  catch (std::exception &exc)
    {
      std::cerr << std::endl << std::endl
                << "----------------------------------------------------"
                << std::endl;
      std::cerr << "Exception on processing: " << std::endl
                << exc.what() << std::endl
                << "Aborting!" << std::endl
                << "----------------------------------------------------"
                << std::endl;
      return 1;
    }
  catch (...)
    {
      std::cerr << std::endl << std::endl
                << "----------------------------------------------------"
                << std::endl;
      std::cerr << "Unknown exception!" << std::endl
                << "Aborting!" << std::endl
                << "----------------------------------------------------"
                << std::endl;
      return 1;
    }
  return 0;
}

Reply via email to