How to find the neighbor of one cell

140 views
Skip to first unread message

Lance Zhang

unread,
Nov 27, 2023, 4:52:14 PM11/27/23
to deal.II User Group
Hello team,

I have one quick question.

May I know how to find the neighbor of a cell?

Here is one part of my code:
----------------------------------------------------------------------------------------------------------------------
for (const auto &cell : solid_3d.get_dof_handler().active_cell_iterators()
{
    const unsigned int cell_index = cell->active_cell_index();

    // Loop over all degrees of freedom on the current cell
    for (unsigned int i = 0; i < dofs_per_cell.dofs_per_cell; ++i)
    {
        // Loop over all neighboring cells
        for (const auto &neighbor : Find_neighbors?)){
     ...

---------------------------------------------------------------------------------------------------------------------
Thanks in advance!
Best regards
Lance  

Wolfgang Bangerth

unread,
Nov 27, 2023, 6:54:12 PM11/27/23
to dea...@googlegroups.com

On 11/27/23 14:52, Lance Zhang wrote:
>
> May I know how to find the neighbor of a cell?
>
> Here is one part of my code:
> ----------------------------------------------------------------------------------------------------------------------
> for (const auto &cell : solid_3d.get_dof_handler().active_cell_iterators()
> {
>     const unsigned int cell_index = cell->active_cell_index();
>
>     // Loop over all degrees of freedom on the current cell
>     for (unsigned int i = 0; i < dofs_per_cell.dofs_per_cell; ++i)
>     {
>         // Loop over all neighboring cells
>         for (const auto &neighbor : Find_neighbors?)){

You can write this as
for (unsigned int f=0; f<cell->n_faces(); ++f)
{
DoFHandler::cell_iterator neighbor = cell->neighbor(f);
...
}

Best
W.

Lance Zhang

unread,
Nov 29, 2023, 9:43:24 AM11/29/23
to dea...@googlegroups.com
Hello Wolfgang,

thanks for your message.

May I know which method or keywords can be used to stand for the non-existing cell and could I use cell->at_boundary() to check whether the cell exists or not?

I would like to know how we can identify if the object of dealii::IteratorRange<dealii::TriaActiveIterator<dealii::DoFCellAccessor<3, 3, false> > >::IteratorOverIterators does not exist. I have tried ==NULL and nullptr, however, it doe snot work.

There is the code snippet below, please take a look:

 for (const auto &cell : solid_3d.dof_handler_ref.active_cell_iterators())
  {

        if(cell->at_boundary()){
          continue;
          }//to check whether the cell exists


          const unsigned int cell_index = cell->active_cell_index();

          if (visited_cells[cell_index])
             continue; // Skip if the cell has been visited by its all neighbors

      
            // Loop over all degrees of freedom on the current cell
        for (unsigned int i = 0; i < solid_3d.dofs_per_cell; ++i)

      {
     
        // Loop over all neighboring cells
        for (unsigned int f = 0; f < cell->n_faces(); ++f)
        {
            auto neighbor = cell->neighbor(f);//find the neighbor ,but not sure if it exists

            if(neighbor->at_boundary())
            continue;//check if the neighbor of cell exists
            int neighbor_index = neighbor->active_cell_index();//get the global index of the cell if it exists
...


Thanks in advance!

Best regards
Lance

Wolfgang Bangerth <bang...@colostate.edu> 于2023年11月28日周二 01:54写道:
--
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 a topic in the Google Groups "deal.II User Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/dealii/XYNlOZYskis/unsubscribe.
To unsubscribe from this group and all its topics, send an email to dealii+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dealii/ef1fdaad-0bde-4478-8054-74df1a572c5f%40colostate.edu.

Daniel Arndt

unread,
Nov 29, 2023, 11:04:45 AM11/29/23
to dea...@googlegroups.com
Lance,

You would check for the cell being at a boundary first before asking for its neighbor:

// Loop over all neighboring cells
        for (unsigned int f = 0; f < cell->n_faces(); ++f)
        {
            if (cell->at_boundary(f)) 
              continue;
            auto neighbor = cell->neighbor(f);
            int neighbor_index = neighbor->active_cell_index();//get the global index of the cell if it exists
[...]


Best,
Daniel

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/CADwW6P%3Dy7uJAdD5q8DwpUnK-3iQBgTnVh5ioi-Pr62w54D0ikw%40mail.gmail.com.

Lance Zhang

unread,
Nov 29, 2023, 11:23:00 AM11/29/23
to deal.II User Group
Hello Daniel,

thanks for your message.

I wrote a statement to check if the cell  exists.


 for (const auto &cell : solid_3d.dof_handler_ref.active_cell_iterators())
  {
//***********************************************************************************************************************************//

        if(cell->at_boundary()){
          continue;
          }//to check whether the cell exists
//**********************filter if the cell is at boundary ,if yes, the cell iterator  will  move forward*************************************//
//*******otherwise if the cell exists,the cell index will be calculated*************************************************************//

          const unsigned int cell_index = cell->active_cell_index();

          if (visited_cells[cell_index])
             continue; // Skip if the cell has been visited by its all neighbors

      
            // Loop over all degrees of freedom on the current cell
        for (unsigned int i = 0; i < solid_3d.dofs_per_cell; ++i)

      {
     
        // Loop over all neighboring cells
        for (unsigned int f = 0; f < cell->n_faces(); ++f)
        {
            auto neighbor = cell->neighbor(f);//find the neighbor ,but not sure if it exists

            if(neighbor->at_boundary())
            continue;//check if the neighbor of cell exists
            int neighbor_index = neighbor->active_cell_index();//get the global index of the cell if it exists
Best regards
Lance

Wolfgang Bangerth

unread,
Nov 29, 2023, 11:40:44 AM11/29/23
to dea...@googlegroups.com
On 11/29/23 09:23, Lance Zhang wrote:
>
> //***********************************************************************************************************************************//
>         if(cell->at_boundary()){
>           continue;
>           }//to check whether the cell exists

I don't think this is what you want. Here, you are only asking whether
the cell has *any* faces that are at the boundary. But if you want to
know whether a cell has a neighbor across a certain face, you need to
ask whether the cell is at the boundary *at that particular face*.
Daniel's code does that.

Best
W.
Reply all
Reply to author
Forward
0 new messages