]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Observe that passing a lambda function to set_cell_selection() also works. 9287/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Thu, 16 Jan 2020 16:26:36 +0000 (09:26 -0700)
committerWolfgang Bangerth <bangerth@colostate.edu>
Thu, 16 Jan 2020 16:34:54 +0000 (09:34 -0700)
include/deal.II/numerics/data_out.h
tests/data_out/data_out_08_set_cell_selection_03.cc [new file with mode: 0644]
tests/data_out/data_out_08_set_cell_selection_03.output [new file with mode: 0644]

index 5df322a5aab701734d867e3ee1934828a9bf2382..a735adc9e9be0b5d07cfcac5395ac95e97917812 100644 (file)
@@ -367,6 +367,23 @@ public:
    * object given as argument. A typical way to generate the argument
    * is via the make_filtered_iterator() function.
    *
+   * Alternatively, since FilteredIterator objects can be created from
+   * just a predicate (i.e., a function object that returns a `bool`), it is
+   * possible to call this function with just a lambda function, which will then
+   * automatically be converted to a FilteredIterator object. For example, the
+   * following piece of code works:
+   * @code
+   *   DataOut<dim> data_out;
+   *   data_out.set_cell_selection(
+   *          [](const typename Triangulation<dim>::cell_iterator &cell) {
+   *              return (!cell->has_children() && cell->subdomain_id() == 0);
+   *          });
+   * @endcode
+   * In this case, the lambda function selects all of those cells that are
+   * @ref GlossActive "active"
+   * and whose subdomain id is zero. These will then be the only cells on
+   * which output is generated.
+   *
    * @note Not all filters will result in subsets of cells for which
    *   output can actually be generated. For example, if you are working
    *   on parallel meshes where data is only available on some cells,
diff --git a/tests/data_out/data_out_08_set_cell_selection_03.cc b/tests/data_out/data_out_08_set_cell_selection_03.cc
new file mode 100644 (file)
index 0000000..2299573
--- /dev/null
@@ -0,0 +1,124 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2003 - 2018 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.
+//
+// ---------------------------------------------------------------------
+
+
+// This test is an adaptation of the _08 test, but using the
+// DataOut::set_cell_selection() function with the FilteredIterator
+// argument instead of overloading member functions. Since
+// FilteredIterators can be created directly from lambda functions, we
+// check that passing a lambda function to set_cell_selection() as
+// argument works as well.
+//
+// Because the _08 test is eventually going away (as it uses
+// deprecated functions), here is the description of that test:
+// ....................
+// This test documents two unrelated bugs in DataOut when used with a Filter (by
+// deriving from DataOut):
+// 1. The patch index computation in data_out.cc is wrong and causes an SIGV (or
+// an Assert after adding that):
+/*
+466: --------------------------------------------------------
+466: An error occurred in line <306> of file
+</ssd/branch_port_the_testsuite/deal.II/source/numerics/data_out.cc> in function
+466:     void dealii::DataOut<dim, DoFHandlerType>::build_one_patch(const
+std::pair<typename dealii::DataOut_DoFData<DoFHandlerType, DoFHandlerType::
+dimension, DoFHandlerType:: space_dimension>::cell_iterator, unsigned int>*,
+dealii::internal::DataOut::ParallelData<DoFHandlerType:: dimension,
+DoFHandlerType:: space_dimension>&, dealii::DataOutBase::Patch<DoFHandlerType::
+dimension, DoFHandlerType:: space_dimension>&, dealii::DataOut<dim,
+DoFHandlerType>::CurvedCellRegion,
+std::vector<dealii::DataOutBase::Patch<DoFHandlerType:: dimension,
+DoFHandlerType:: space_dimension> >&) [with int dim = 2, DoFHandlerType =
+dealii::DoFHandler<2>, typename dealii::DataOut_DoFData<DoFHandlerType,
+DoFHandlerType:: dimension, DoFHandlerType:: space_dimension>::cell_iterator =
+dealii::TriaIterator<dealii::CellAccessor<2, 2> >] 466: The violated condition
+was: 466:     cell_and_index->second < patches.size() 466: The name and call
+sequence of the exception was: 466:     ExcInternalError()
+*/
+// 2. DataOut used begin_active() instead of first_cell() in two places which
+// caused a wrong patch to be generated when the first active cell is not picked
+// by the filter.
+
+#include <deal.II/dofs/dof_accessor.h>
+#include <deal.II/dofs/dof_handler.h>
+#include <deal.II/dofs/dof_tools.h>
+
+#include <deal.II/fe/fe_dgq.h>
+
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/tria.h>
+#include <deal.II/grid/tria_iterator.h>
+
+#include <deal.II/lac/vector.h>
+
+#include <deal.II/numerics/data_out.h>
+
+#include "../tests.h"
+
+
+
+template <int dim>
+void
+check()
+{
+  Triangulation<dim> tria;
+  GridGenerator::hyper_cube(tria, 0., 1.);
+  tria.refine_global(1);
+
+  Vector<double> cell_data(4);
+  for (unsigned int i = 0; i < 4; ++i)
+    cell_data(i) = i * 1.0;
+
+  // this should skip the first cell
+  typename Triangulation<dim>::active_cell_iterator it = tria.begin_active();
+  it->set_subdomain_id(1);
+
+  FE_DGQ<dim>     fe(0);
+  DoFHandler<dim> dof_handler(tria);
+  dof_handler.distribute_dofs(fe);
+
+  // we pick only subdomain==0 which will
+  // skip the first of the four cells
+  DataOut<dim> data_out;
+  data_out.set_cell_selection(
+    [](const typename Triangulation<dim>::cell_iterator &cell) {
+      return (!cell->has_children() && cell->subdomain_id() == 0);
+    });
+
+
+  data_out.attach_dof_handler(dof_handler);
+
+  data_out.add_data_vector(cell_data,
+                           "cell_data",
+                           DataOut<dim>::type_cell_data);
+  data_out.build_patches();
+
+  data_out.write_deal_II_intermediate(deallog.get_file_stream());
+
+  deallog << "OK" << std::endl;
+}
+
+int
+main(int argc, char **argv)
+{
+  std::ofstream logfile("output");
+  deallog << std::setprecision(2);
+  logfile << std::setprecision(2);
+  deallog.attach(logfile);
+
+  check<2>();
+
+  return 0;
+}
diff --git a/tests/data_out/data_out_08_set_cell_selection_03.output b/tests/data_out/data_out_08_set_cell_selection_03.output
new file mode 100644 (file)
index 0000000..bd4f3f7
--- /dev/null
@@ -0,0 +1,38 @@
+
+2 2
+[deal.II intermediate format graphics data]
+[written by deal.II x.y.z]
+[Version: 3]
+1
+cell_data
+3
+[deal.II intermediate Patch<2,2>]
+0.50 0.0 1.0 0.0 1.0 0.50 0.50 0.50 
+4294967295 4294967295 4294967295 2 
+0 1
+0
+1 4
+1.0 1.0 1.0 1.0 
+
+
+[deal.II intermediate Patch<2,2>]
+0.0 0.50 0.50 0.50 0.50 1.0 0.0 1.0 
+4294967295 2 4294967295 4294967295 
+1 1
+0
+1 4
+2.0 2.0 2.0 2.0 
+
+
+[deal.II intermediate Patch<2,2>]
+0.50 0.50 1.0 0.50 1.0 1.0 0.50 1.0 
+1 4294967295 0 4294967295 
+2 1
+0
+1 4
+3.0 3.0 3.0 3.0 
+
+
+0
+
+DEAL::OK

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.