From e3fdc7f9052809cb7a8a1e346263ec2184a22dd9 Mon Sep 17 00:00:00 2001
From: Ce Qin <qince168@gmail.com>
Date: Wed, 6 Jan 2021 20:09:15 +0800
Subject: [PATCH] Fix bug in distribute_dofs

---
 source/dofs/dof_handler.cc                 |  3 ++
 tests/dofs/distribute_dofs_01.cc           | 58 ++++++++++++++++++++++
 tests/dofs/distribute_dofs_01.debug.output |  2 +
 3 files changed, 63 insertions(+)
 create mode 100644 tests/dofs/distribute_dofs_01.cc
 create mode 100644 tests/dofs/distribute_dofs_01.debug.output

diff --git a/source/dofs/dof_handler.cc b/source/dofs/dof_handler.cc
index f6becc1322..d0b2a6661b 100644
--- a/source/dofs/dof_handler.cc
+++ b/source/dofs/dof_handler.cc
@@ -2056,6 +2056,9 @@ DoFHandler<dim, spacedim>::reinit(const Triangulation<dim, spacedim> &tria)
   DoFHandler<dim, spacedim>::clear();
   this->policy.reset();
 
+  // reset the finite element collection
+  this->fe_collection = hp::FECollection<dim, spacedim>();
+
   //
   // call constructor
   //
diff --git a/tests/dofs/distribute_dofs_01.cc b/tests/dofs/distribute_dofs_01.cc
new file mode 100644
index 0000000000..337a9a6f4b
--- /dev/null
+++ b/tests/dofs/distribute_dofs_01.cc
@@ -0,0 +1,58 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2010 - 2021 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.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+#include <deal.II/dofs/dof_handler.h>
+
+#include <deal.II/fe/fe_nedelec.h>
+
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/tria.h>
+
+#include "../tests.h"
+
+// Check DoFHandler::distribute_dofs with multiple calls to reinit
+
+void
+test()
+{
+  dealii::Triangulation<3> tria;
+  dealii::GridGenerator::hyper_cube(tria, -1, 1);
+
+  dealii::FE_Nedelec<3> fe_nedelec(0);
+
+  dealii::DoFHandler<3> dh_nedelec;
+
+  dh_nedelec.reinit(tria);
+  dh_nedelec.distribute_dofs(fe_nedelec);
+
+  dh_nedelec.reinit(tria);
+  dh_nedelec.distribute_dofs(fe_nedelec);
+
+  for (const auto &cell : dh_nedelec.active_cell_iterators())
+    for (unsigned int i = 0; i < dealii::GeometryInfo<3>::lines_per_cell; ++i)
+      cell->line(i)->dof_index(0);
+
+  deallog << "ok" << std::endl;
+}
+
+int
+main()
+{
+  initlog();
+
+  test();
+
+  return 0;
+}
diff --git a/tests/dofs/distribute_dofs_01.debug.output b/tests/dofs/distribute_dofs_01.debug.output
new file mode 100644
index 0000000000..0de35217a9
--- /dev/null
+++ b/tests/dofs/distribute_dofs_01.debug.output
@@ -0,0 +1,2 @@
+
+DEAL::ok
-- 
2.39.5