GridTools::find_cells_adjacent_to_vertex: how to avoid multiple calling ?

54 views
Skip to first unread message

Simon

unread,
Apr 30, 2021, 12:13:12 PM4/30/21
to deal.II User Group

Dear all,

the mentioned function is exactly doing the right thing for me, i.e. returning an vector of iterators to all cells, which share this vertex. After calling this function I will loop over these cells and compute certain things ...

My problem is that I have to work with two different dof_handlers and also need some quantities from FEValues of both DoF_Handlers on my neighbor cells. So far I handled this like this:

auto cells_ref = GridTools::find_cells_adjacent_to_vertex(dof_handler_ref, /*vertex*/);
auto cells_tmp = GridTools::find_cells_adjacent_to_vertex(dof_handler_tmp, /*vertex*/);

I am not sure how "expensive" this function actually is. I have to call this function for all vertices of my triangulation, so currently I call it "2*n_vertices()" times.

I´d like to call this function only once for a given vertex, i.e. "n_vertices()"times.
Is there a way to realize this in combination with two different dof_handlers?

Thanks for helping!

Best
Simon


Wolfgang Bangerth

unread,
Apr 30, 2021, 12:16:11 PM4/30/21
to dea...@googlegroups.com
On 4/30/21 10:13 AM, Simon wrote:
>
> auto cells_ref = GridTools::find_cells_adjacent_to_vertex(dof_handler_ref,
> /*vertex*/);
> auto cells_tmp = GridTools::find_cells_adjacent_to_vertex(dof_handler_tmp,
> /*vertex*/);
>
> I am not sure how "expensive" this function actually is. I have to call this
> function for all vertices of my triangulation, so currently I call it
> "2*n_vertices()" times.
>
> I´d like to call this function only once for a given vertex, i.e.
> "n_vertices()"times.
> Is there a way to realize this in combination with two different dof_handlers?

There are probably ways to work around this, but I'd like to repeat a point
Bruno made the other day on this very mailing list:

"Unless you have measured that this is a bottleneck in you code, you should
use what's the more readable. If there is a difference between these two
codes, it would need to be in a hot loop to matter. My advice is to write easy
to understand code and once you have made sure that the code works, then use a
profiler to find the bottleneck and optimize the code."

I think this is good advice!

Best
W.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/

Simon Wiesheier

unread,
Apr 30, 2021, 12:37:15 PM4/30/21
to dea...@googlegroups.com
Hello Wolfgang, 

I actually have a Time Measurement in my code implemented. 
For my current computations the difference (one call vs two calls per vertex) is neglible. But my model will become much bigger and therefore I'd like to avoid unnecessary calls to that function. 

Since I can pass only one dof_handler to that function, I guess there is no "obvious" way to make only one call, is it? 

-Simon 

--
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 dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/966f38ec-5ce2-f879-c9c4-2df21ab1110b%40colostate.edu.

Jean-Paul Pelteret

unread,
May 1, 2021, 1:20:21 AM5/1/21
to dea...@googlegroups.com
Hi Simon,

You could call GridTools:: find_cells_adjacent_to_vertex() with the triangulation instead of a DoFHandler, and then just convert the returned iterators to the type used by each DoFHandler using the method outlined here:

Best,
Jean-Paul

Simon Wiesheier

unread,
May 2, 2021, 6:26:11 AM5/2/21
to dea...@googlegroups.com
Hi Jean-Paul,

your suggestion worked fine, thanks a lot!

Best
Simon

luca.heltai

unread,
May 3, 2021, 3:47:15 AM5/3/21
to Deal.II Users
Dear Simon,

one solution you have is to pass a *Triangulation*, and then generate the dof cell accessors using the constructor

typename DoFHandler<dim>::cell_iterator cell1(*tria_iterator, dh1);
typename DoFHandler<dim>::cell_iterator cell2(*tria_iterator, dh2);


Best,
Luca.
> To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/CAM50jEtCWSTAhzEi7uPmgvkC94DMGJnP43AAhOuJw_97KCVT7g%40mail.gmail.com.

Simon Wiesheier

unread,
May 4, 2021, 2:15:26 PM5/4/21
to dea...@googlegroups.com
Thanks for all answers!

I've got a similar question:

I looked up the Implementation of the mentioned function. Basically the function loops over all active cells and compares those global vertices with the one passed as second argument, so far so good. 

But is there a similar function which accepts a cell_iterator (the given vertex is located on that cell)  so that inside the function body only the neigbor and the neighbor's neighbor are possible cells on which the given vertex can be located?
So I'd like to tell the function where it should begin its "search", in order to avoid looping over all cells (worst case). 
 
There is a function which is doing this for a patch of neighbor_cells (element-patch), but I didn't find a function for the problem description from above.

Is such a function not yet implemented or is there already one available? 

Best
Simon 

Wolfgang Bangerth

unread,
May 4, 2021, 4:35:18 PM5/4/21
to dea...@googlegroups.com
On 5/4/21 12:15 PM, Simon Wiesheier wrote:
>
> I looked up the Implementation of the mentioned function. Basically the
> function loops over all active cells and compares those global vertices with
> the one passed as second argument, so far so good.
>
> But is there a similar function which accepts a cell_iterator (the given
> vertex is located on that cell)  so that inside the function body only the
> neigbor and the neighbor's neighbor are possible cells on which the given
> vertex can be located?
> So I'd like to tell the function where it should begin its "search", in order
> to avoid looping over all cells (worst case).
> There is a function which is doing this for a patch of neighbor_cells
> (element-patch), but I didn't find a function for the problem description from
> above.
>
> Is such a function not yet implemented or is there already one available?

I don't think so, but it shouldn't be very hard to add an argument to the
function that by default is simply a default-constructed cell iterator
indicating "no initial guess given" but if not at its default value indicates
to start search on this cell and its neighbors.

luca.heltai

unread,
May 5, 2021, 8:01:15 AM5/5/21
to Deal.II Users
In addition to that, I’d suggest you use the GridTools::Cache::get_vertex_to_cell_map class, which builds the map once, and stores it for future reference. This one uses GridTools::vertex_to_cell_map, which builds the map with one pass, instead of looping over all cells.

Indeed, if you want to call this only for one vertex, your choice is better. But if you need this for several vertices, I’d build the map once, and then use the map all subsequent times.

https://www.dealii.org/current/doxygen/deal.II/classGridTools_1_1Cache.html#a1e51292fbaeae9d7d3fcec2f9eb6a8dd


Best,
Luca.
> --
> 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 dealii+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/47cadef0-3287-db4f-27e4-a4923757374a%40colostate.edu.

Simon Wiesheier

unread,
May 5, 2021, 4:08:08 PM5/5/21
to dea...@googlegroups.com
Thanks to both of you!

The implementation of a own function was the best solution to my issue. In there I basically ask the neighbor-cells and again those neighbors if the given vertex is located on that cells. My function is a bit faster than your suggestion Luca, but not that much.

Nonetheless, the GridTools::Cache-Class is actually much faster than calling GridTools::adjacent_cell_to_vertex for all vertices of my triangulation. So thanks for that hint, I guess I can you use some other features of that class in my program.

Best
Simon

Reply all
Reply to author
Forward
0 new messages