Dear Bruno,

Sure, I have stripped down example of step-83.
To run, first use `./restart` to create the checkpoint, then restart from checkpoint with `./restart restart`. In lines 75/76 you can change from a `parallel::shared::Triangulation` to a `parallel::distributed::Triangulation`. For the distributed Triangulation, the example works. For the shared Triangulation I get the aforementioned error. I use a current deal.II version compiled from master a few month back (9.8.0-pre).

Thanks for having a look!
Best, Florian

On 03.04.26 13:39, [email protected] wrote:
Dear Florian, Could you provide a very minimal example that reproduces the error? I have to admit I have never worked with checkpointing shared triangulations, only distributed and serial ones. I'll try to take a look and unwind what's happening.
Cheers
Bruno


On Thursday, March 19, 2026 at 4:50:15 p.m. UTC+1 Florian Schulze wrote:

    Dear all,

    I'm currently trying to implement a restart functionality for my
    code,
    and run into some problem with the restart for the
    `parallel::shared::Triangulation<dim>`.
    The code is MPI parallelized and multidimensional (1,2,3 and at some
    point maybe even 4D), so I use the
    `parallel::shared::Triangulation<dim>` for 1D, and
    `parallel::distributed::Triangulation<dim>` for 2D/3D.
    To implement the restart, I follow the tutorials step-83 and step-69
    (which are very well written and very helpful BTW - thanks a lot to
    whoever wrote them!).
    For the `parallel::distributed::Triangulation<dim>` I got it to
    work - I
    first create a (coarse) mesh and then load the checkpoint.
    But for the `parallel::shared::Triangulation<dim>` I run into the
    following error (full stacktrace see below):

    ```
    An error occurred in line <14142> of file
    <dealii/source/grid/tria.cc>
    in function
        void dealii::Triangulation<dim, spacedim>::load(const
    std::string&)
    [with int dim = 2; int spacedim = 2; std::string =
    std::__cxx11::basic_string<char>]
    The violated condition was:
        this->n_global_active_cells() == n_global_active_cells
    Additional information:
        The number of cells of the triangulation differs from the
    number of
        cells written into the .info file.
    ```

    I don't really understand this error - some help would be much
    appreciated!
    I use the same logic as for the distributed triangulation: I first
    create the mesh (in this example it is static) and then read the
    checkpoint.

    Best,
    Florian



    Full Stacktrace:

    ```
    --------------------------------------------------------
    An error occurred in line <14142> of file
    <dealii/source/grid/tria.cc>
    in function
        void dealii::Triangulation<dim, spacedim>::load(const
    std::string&)
    [with int dim = 2; int spacedim = 2; std::string =
    std::__cxx11::basic_string<char>]
    The violated condition was:
        this->n_global_active_cells() == n_global_active_cells
    Additional information:
        The number of cells of the triangulation differs from the
    number of
        cells written into the .info file.

    Stacktrace
    -----------

    # 0 /home/schulze/.local/lib/dealii/lib/libdeal_II.g.so.9.8.0-pre:
    dealii::Triangulation<2, 2>::load(std::__cxx11::basic_string<char,
    std::char_traits<char>, std::allocator<char> > const&)
    # 1  ./restart: Step83::CathodeRaySimulator<2>::restart()
    # 2  ./restart: Step83::CathodeRaySimulator<2>::run(bool)
    # 3  ./restart: main
    --------------------------------------------------------

    Abort() was called during dealing with an assertion or exception.
    Backtrace:
    [0x790b24158105] Kokkos::Impl::save_stacktrace()
    [0x790b241546dc] Kokkos::Impl::host_abort(char const*)
    [0x790b240fc22a]
    [0x790b2d0a33ce]
    
dealii::deal_II_exceptions::internals::do_issue_error_nothrow(dealii::ExceptionBase

    const&)
    [0x6470101ef59f] void
    
dealii::deal_II_exceptions::internals::issue_error_noreturn<dealii::StandardExceptions::ExcMessage>(dealii::deal_II_exceptions::internals::ExceptionHandling,

    char const*, int, char const*, char const*, char const*,
    dealii::StandardExceptions::ExcMessage)
    [0x790b2e6a56a6] dealii::Triangulation<2,
    2>::load(std::__cxx11::basic_string<char, std::char_traits<char>,
    std::allocator<char> > const&)
    [0x6470101f500a] Step83::CathodeRaySimulator<2>::restart()
    [0x6470101f1c37] Step83::CathodeRaySimulator<2>::run(bool)
    [0x6470101e5a9d] main
    [0x790b10e2a1ca]
    [0x790b10e2a28b]__libc_start_main
    [0x6470101e5835]_start
    ***Process received signal***
    Signal: Aborted (6)
    Signal code:  (-6)
    [ 0] /lib/x86_64-linux-gnu/libc.so.6[+0x45330](0x790b10e45330)
    [ 1]
    /lib/x86_64-linux-gnu/libc.so.6[pthread_kill+0x11c](0x790b10e9eb2c)
    [ 2] /lib/x86_64-linux-gnu/libc.so.6[gsignal+0x1e](0x790b10e4527e)
    [ 3] /lib/x86_64-linux-gnu/libc.so.6[abort+0xdf](0x790b10e288ff)
    [ 4]
    
/home/schulze/.local/lib/dealii/lib/libdeal_II.g.so.9.8.0-pre[+0x129546f0](0x790b241546f0)

    [ 5]
    
/home/schulze/.local/lib/dealii/lib/libdeal_II.g.so.9.8.0-pre[+0x128fc22a](0x790b240fc22a)

    [ 6]
    
/home/schulze/.local/lib/dealii/lib/libdeal_II.g.so.9.8.0-pre[_ZN6dealii18deal_II_exceptions9internals22do_issue_error_nothrowERKNS_13ExceptionBaseE+0x0](0x790b2d0a33ce)

    [ 7]
    
./restart[_ZN6dealii18deal_II_exceptions9internals20issue_error_noreturnINS_18StandardExceptions10ExcMessageEEEvNS1_17ExceptionHandlingEPKciS7_S7_S7_T_+0x89](0x6470101ef59f)

    [ 8]
    
/home/schulze/.local/lib/dealii/lib/libdeal_II.g.so.9.8.0-pre[_ZN6dealii13TriangulationILi2ELi2EE4loadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE+0x5e0](0x790b2e6a56a6)

    [ 9]
    
./restart[_ZN6Step8319CathodeRaySimulatorILi2EE7restartEv+0x1a2](0x6470101f500a)

    [10]
    ./restart[_ZN6Step8319CathodeRaySimulatorILi2EE3runEb+0x9d](0x6470101f1c37)

    [11] ./restart[main+0x145](0x6470101e5a9d)
    [12] /lib/x86_64-linux-gnu/libc.so.6[+0x2a1ca](0x790b10e2a1ca)
    [13]
    /lib/x86_64-linux-gnu/libc.so.6[__libc_start_main+0x8b](0x790b10e2a28b)

    [14] ./restart[_start+0x25](0x6470101e5835)
    ***End of error message***
    Aborted (core dumped)
    ```

-- Florian Schulze

    PhD Student
    Max-Planck-Institut für Kernphysik
    Saupfercheckweg 1
    69117 Heidelberg

    Tel.: +49 6221 516-591 <tel:+49%206221%20516591>

--
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]. To view this discussion visit https://groups.google.com/d/msgid/dealii/80813955-7376-4be7-b328-9213219d7ff6n%40googlegroups.com <https://groups.google.com/d/msgid/dealii/80813955-7376-4be7-b328-9213219d7ff6n%40googlegroups.com?utm_medium=email&utm_source=footer>.

--
Florian Schulze

PhD Student
Max-Planck-Institut für Kernphysik
Saupfercheckweg 1
69117 Heidelberg

Tel.: +49 6221 516-591

--
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].
To view this discussion visit 
https://groups.google.com/d/msgid/dealii/e152c153-26a6-4a9f-935f-ed95f400175b%40mpi-hd.mpg.de.
#include <deal.II/base/discrete_time.h>
#include <deal.II/base/quadrature_lib.h>

#include <deal.II/distributed/shared_tria.h>
#include <deal.II/distributed/tria.h>

#include <deal.II/dofs/dof_handler.h>
#include <deal.II/dofs/dof_tools.h>

#include <deal.II/fe/fe_q.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/fe/mapping_q.h>

#include <deal.II/grid/grid_generator.h>
#include <deal.II/grid/grid_refinement.h>
#include <deal.II/grid/grid_tools.h>
#include <deal.II/grid/tria.h>

#include <deal.II/lac/affine_constraints.h>
#include <deal.II/lac/dynamic_sparsity_pattern.h>
#include <deal.II/lac/full_matrix.h>
#include <deal.II/lac/precondition.h>
#include <deal.II/lac/solver_cg.h>
#include <deal.II/lac/sparse_matrix.h>
#include <deal.II/lac/vector.h>

#include <deal.II/matrix_free/fe_point_evaluation.h>

#include <deal.II/numerics/data_out.h>
#include <deal.II/numerics/error_estimator.h>
#include <deal.II/numerics/vector_tools.h>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <filesystem>
#include <fstream>
#include <string>

namespace Step83
{
  using namespace dealii;


  template <int dim>
  class CathodeRaySimulator
  {
  public:
    CathodeRaySimulator();

    void
    run(const bool do_restart);

    template <class Archive>
    void
    serialize(Archive &ar, const unsigned int version);

  private:
    void
    make_grid();
    void
    setup_system();
    void
    assemble_system();

    void
    output_results() const;

    void
    checkpoint();
    void
    restart();

    // Restart does not work for shared triangulation :(
    // parallel::shared::Triangulation<dim> triangulation;
    parallel::distributed::Triangulation<dim> triangulation; // Works :)
    
    const MappingQ<dim>       mapping;
    const FE_Q<dim>           fe;
    DoFHandler<dim>           dof_handler;
    AffineConstraints<double> constraints;

    SparseMatrix<double> system_matrix;
    SparsityPattern      sparsity_pattern;

    Vector<double> solution;

    DiscreteTime time;
  };



  template <int dim>
  CathodeRaySimulator<dim>::CathodeRaySimulator()
    : triangulation(MPI_COMM_WORLD)
    , mapping(1)
    , fe(2)
    , dof_handler(triangulation)
    , time(0, 1, 0.1)
  {}



  template <int dim>
  void
  CathodeRaySimulator<dim>::make_grid()
  {
    Point<dim>                p1;
    Point<dim>                p2;
    std::vector<unsigned int> n_cells(dim);

    for (unsigned int d = 0; d < dim; ++d)
      {
        p1[d]      = 0.;
        p2[d]      = 1.;
        n_cells[d] = 10;
      }

    GridGenerator::subdivided_hyper_rectangle(triangulation, n_cells, p1, p2);
  }


  template <int dim>
  void
  CathodeRaySimulator<dim>::setup_system()
  {
    dof_handler.distribute_dofs(fe);

    solution.reinit(dof_handler.n_dofs());

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

    DynamicSparsityPattern dsp(dof_handler.n_dofs());
    DoFTools::make_sparsity_pattern(dof_handler,
                                    dsp,
                                    constraints,
                                    /* keep_constrained_dofs = */ false);
    sparsity_pattern.copy_from(dsp);

    system_matrix.reinit(sparsity_pattern);
  }


  template <int dim>
  void
  CathodeRaySimulator<dim>::output_results() const
  {
    {
      DataOut<dim> data_out;
      data_out.attach_dof_handler(dof_handler);
      data_out.add_data_vector(solution, "electric_potential");
      data_out.build_patches();

      DataOutBase::VtkFlags output_flags;
      output_flags.time  = time.get_current_time();
      output_flags.cycle = time.get_step_number();
      output_flags.physical_units["electric_potential"] = "V";

      data_out.set_flags(output_flags);

      std::ofstream output("solution-" +
                           Utilities::int_to_string(time.get_step_number(), 4) +
                           ".vtu");
      data_out.write_vtu(output);
    }
  }



  template <int dim>
  template <class Archive>
  void
  CathodeRaySimulator<dim>::serialize(Archive &ar,
                                      const unsigned int /* version */)
  {
    ar & solution;
    ar & time;
  }



  template <int dim>
  void
  CathodeRaySimulator<dim>::checkpoint()
  {
    std::cout << "--- Writing checkpoint... ---" << std::endl << std::endl;

    {
      std::ofstream checkpoint_file("tmp.checkpoint_step_83");
      AssertThrow(checkpoint_file,
                  ExcMessage(
                    "Could not write to the <tmp.checkpoint_step_83> file."));

      boost::archive::text_oarchive archive(checkpoint_file);

      archive << *this;
    }

    triangulation.save("tmp.checkpoint");

    std::list<std::string> tmp_checkpoint_files;
    for (const auto &dir_entry : std::filesystem::directory_iterator("."))
      if (dir_entry.is_regular_file() &&
          (dir_entry.path().filename().string().find("tmp.checkpoint") == 0))
        tmp_checkpoint_files.push_back(dir_entry.path().filename().string());

    for (const std::string &filename : tmp_checkpoint_files)
      std::filesystem::rename(filename, filename.substr(4, std::string::npos));
  }


  template <int dim>
  void
  CathodeRaySimulator<dim>::restart()
  {
    {
      std::ifstream checkpoint_file("checkpoint_step_83");
      AssertThrow(checkpoint_file,
                  ExcMessage(
                    "Could not read from the <checkpoint_step_83> file."));

      boost::archive::text_iarchive archive(checkpoint_file);
      archive >> *this;
    }

    triangulation.load("checkpoint");

    std::cout << "--- Restarting at t=" << time.get_current_time()
              << ", dt=" << time.get_next_step_size() << std::endl
              << std::endl;
  }


  template <int dim>
  void
  CathodeRaySimulator<dim>::run(const bool do_restart)
  {
    if (do_restart == false)
      {
        make_grid();

        const unsigned int n_pre_refinement_cycles = 3;
        for (unsigned int refinement_cycle = 0;
             refinement_cycle < n_pre_refinement_cycles;
             ++refinement_cycle)
          {
            setup_system();
          }
      }
    else
      {
        make_grid(); // (Coarse) Grid need to be already created for restart
        restart();
      }

    setup_system();
    do
      {
        std::cout << "Timestep " << time.get_step_number() + 1 << std::endl;
        std::cout << "  Field degrees of freedom:                 "
                  << dof_handler.n_dofs() << std::endl;

        time.advance_time();
        dealii::VectorTools::interpolate(
          dof_handler,
          dealii::Functions::ConstantFunction<dim>({time.get_current_time()}),
          solution);

        output_results();

        if (time.get_step_number() == 5)
          {
            checkpoint();
            std::cout << "Abort, waiting for restart" << std::endl;
            return;
          }
      }
    while (time.is_at_end() == false);
  }
} // namespace Step83


int
main(int argc, char *argv[])
{
  try
    {
      dealii::Utilities::MPI::MPI_InitFinalize mpi_initialization(argc,
                                                                  argv,
                                                                  1);

      Step83::CathodeRaySimulator<2> cathode_ray_simulator;

      if (argc == 1)
        cathode_ray_simulator.run(false); // no restart
      else if ((argc == 2) && (std::string(argv[1]) == "restart"))
        cathode_ray_simulator.run(true); // yes restart
      else
        {
          std::cerr << "Error: The only allowed command line argument to this\n"
                       "       program is 'restart'."
                    << std::endl;
          return 1;
        }
    }
  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;
}
##
#  CMake script for the step-83 tutorial program:
##

# Set the name of the project and target:
set(TARGET "restart")

# Declare all source files the target consists of. Here, this is only
# the one step-X.cc file, but as you expand your project you may wish
# to add other source files as well. If your project becomes much larger,
# you may want to either replace the following statement by something like
#  file(GLOB_RECURSE TARGET_SRC  "source/*.cc")
#  file(GLOB_RECURSE TARGET_INC  "include/*.h")
#  set(TARGET_SRC ${TARGET_SRC}  ${TARGET_INC})
# or switch altogether to the large project CMakeLists.txt file discussed
# in the "CMake in user projects" page accessible from the "User info"
# page of the documentation.
set(TARGET_SRC
  ${TARGET}.cc
)

# Usually, you will not need to modify anything beyond this point...

cmake_minimum_required(VERSION 3.13.4)

find_package(deal.II 9.8.0
  HINTS ${deal.II_DIR} ${DEAL_II_DIR} ../ ../../ $ENV{DEAL_II_DIR}
)
if(NOT ${deal.II_FOUND})
  message(FATAL_ERROR "\n"
    "*** Could not locate a (sufficiently recent) version of deal.II. ***\n\n"
    "You may want to either pass a flag -DDEAL_II_DIR=/path/to/deal.II to 
cmake\n"
    "or set an environment variable \"DEAL_II_DIR\" that contains this path."
  )
endif()

#
# Are all dependencies fulfilled?
#
if(NOT DEAL_II_WITH_MPI)
  message(FATAL_ERROR "
Error! This tutorial requires a deal.II library that was configured with the 
following option:
    DEAL_II_WITH_MPI = ON
However, the deal.II library found at ${DEAL_II_PATH} was configured with these 
options:
    DEAL_II_WITH_MPI = ${DEAL_II_WITH_MPI}
This conflicts with the requirements."
  )
endif()


deal_ii_initialize_cached_variables()
set(CLEAN_UP_FILES *.log *.gmv *.gnuplot *.gpl *.eps *.pov *.ucd *.d2 *.vtu 
*.pvtu)
project(${TARGET})
deal_ii_invoke_autopilot()

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to