-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12952 from bangerth/filters
Provide C++20-style operator| to filter iterator ranges.
- Loading branch information
Showing
8 changed files
with
328 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
New: In the same spirit as provided by the C++20 [range | ||
adaptors](https://en.cppreference.com/w/cpp/ranges) feature, it is now | ||
possible to "filter" ranges over the active cells of Triangulation | ||
and DoFHandler objects. | ||
<br> | ||
This allows to replace: | ||
@code | ||
DoFHandler<dim> dof_handler; | ||
... | ||
for (const auto &cell : dof_handler.active_cell_iterators()) | ||
{ | ||
if (cell->is_locally_owned()) | ||
{ | ||
fe_values.reinit (cell); | ||
...do the local integration on 'cell'...; | ||
} | ||
} | ||
@endcode | ||
by: | ||
@code | ||
DoFHandler<dim> dof_handler; | ||
... | ||
const auto filtered_iterators_range = | ||
filter_iterators(); | ||
for (const auto &cell : | ||
dof_handler.active_cell_iterators() | IteratorFilters::LocallyOwnedCell()) | ||
{ | ||
fe_values.reinit (cell); | ||
...do the local integration on 'cell'...; | ||
} | ||
@endcode | ||
Here, the `operator|` is to be interpreted in the same way as is done in | ||
the [range adaptors](https://en.cppreference.com/w/cpp/ranges) feature | ||
that is part of [C++20](https://en.wikipedia.org/wiki/C%2B%2B20). It has | ||
the same meaning as the `|` symbol on the command line: It takes what is | ||
on its left as its inputs, and filters and transforms to produce some | ||
output. In the example above, it "filters" all of the active cell iterators | ||
and removes those that do not satisfy the predicate -- that is, it produces | ||
a range of iterators that only contains those cells that are both active | ||
and locally owned. | ||
<br> | ||
(Wolfgang Bangerth, 2021/11/03) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// --------------------------------------------------------------------- | ||
// | ||
// Copyright (C) 2016 - 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. | ||
// | ||
// --------------------------------------------------------------------- | ||
|
||
|
||
// Check filtered iterators using multiple predicates | ||
// | ||
// Compared to filtered_iterator_04, this test checks the availability | ||
// of operator| to create the filter. | ||
|
||
#include <deal.II/grid/filtered_iterator.h> | ||
#include <deal.II/grid/grid_generator.h> | ||
#include <deal.II/grid/tria.h> | ||
#include <deal.II/grid/tria_accessor.h> | ||
#include <deal.II/grid/tria_iterator.h> | ||
|
||
#include <set> | ||
|
||
#include "../tests.h" | ||
|
||
using active_cell_iterator = Triangulation<2>::active_cell_iterator; | ||
|
||
void | ||
test() | ||
{ | ||
Triangulation<2> tria; | ||
GridGenerator::hyper_cube(tria, -1, 1); | ||
tria.refine_global(1); | ||
tria.begin_active()->set_refine_flag(); | ||
tria.execute_coarsening_and_refinement(); | ||
tria.refine_global(2); | ||
|
||
// we now have a number of cells, | ||
// flag them with some subdomain | ||
// ids based on their position, in | ||
// particular we take the quadrant | ||
// (octant) | ||
active_cell_iterator cell = tria.begin_active(), endc = tria.end(); | ||
for (unsigned int i = 0; cell != endc; ++cell) | ||
{ | ||
unsigned int subdomain = i % 3; | ||
|
||
cell->set_subdomain_id(subdomain); | ||
++i; | ||
}; | ||
|
||
// Count the cells that are on the boundary and have a subdomain_id of 0 | ||
std::set<active_cell_iterator> cell_set; | ||
for (cell = tria.begin_active(); cell != endc; ++cell) | ||
if ((cell->subdomain_id() == 0) && (cell->at_boundary())) | ||
cell_set.insert(cell); | ||
|
||
|
||
unsigned int n_filtered_cells = 0; | ||
for (auto filtered_cell : tria.active_cell_iterators() | | ||
IteratorFilters::AtBoundary() | | ||
IteratorFilters::SubdomainEqualTo(0)) | ||
{ | ||
AssertThrow(cell_set.count(filtered_cell) == 1, | ||
ExcMessage("Wrong cell filtered.")); | ||
++n_filtered_cells; | ||
} | ||
AssertThrow(n_filtered_cells == cell_set.size(), | ||
ExcMessage("Filtered cells missing.")); | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
initlog(); | ||
deallog << std::setprecision(4); | ||
|
||
test(); | ||
|
||
deallog << "OK" << std::endl; | ||
; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
DEAL::OK |
Oops, something went wrong.