# Querying the Topology to Find Neighbors#

[1]:

import openpnm as op
%config InlineBackend.figure_formats = ['svg']
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(10)
%matplotlib inline
ws = op.Workspace()
ws.settings["loglevel"] = 40


## Setup Network#

The OpenPNM GenericNetwork objects (e.g. Cubic, Voronoi, etc) have methods that let you query the connected pores and throats. This tutorial will explain how these work and illustrate why they are useful.

[2]:

pn = op.network.Cubic(shape=[4, 4, 1])


The following examples are relatively trivial, but their intention is to illustrate the different functions and options. More realistic use cases will be presented further down.

Start by finding all pores on the ‘left’ and ‘back’

[3]:

P_left = pn.pores('left')
P_bottom = pn.pores('back')


## Find Neighoring Pores#

We now have two sets of pores that actually overlap each other, as illustrated below:

[4]:

fig, ax = plt.subplots()
op.topotools.plot_coordinates(pn, pn.Ps, c='lightgrey',
markersize=50, ax=ax)
op.topotools.plot_coordinates(pn, P_left, c='red', marker='*',
markersize=50, ax=ax)
op.topotools.plot_coordinates(pn, P_bottom, c='blue', marker='.',
markersize=50, ax=ax)

[4]:

<matplotlib.collections.PathCollection at 0x7f2868758820>


We’ll merge these pores into a single set, and explore the different ways to find neighbors to this set. Note that the pore at [x,y] = [1.5, 1.5] has two neighbors (one ‘bottom’ and one ‘left’).

### Find All Neighbors: OR#

Finds all pores with one or more connections to the input pores

Given a set of pores, find the pores that are neighbors to one or more of the inputs. This is called OR since it gives the neighbors of either the bottom pores or the left pores, or both.

[5]:

Ps = pn.pores(['left', 'back'])
print(Ps)
Ps = pn.find_neighbor_pores(pores=Ps, mode='or')
print(Ps)

[ 0  1  2  3  7 11 15]
[ 4  5  6 10 14]

[6]:

fig, ax = plt.subplots()
op.topotools.plot_coordinates(pn, pn.Ps, c='lightgrey',
markersize=50, ax=ax)
op.topotools.plot_coordinates(pn, P_left, c='red',
markersize=50, marker='*', ax=ax)
op.topotools.plot_coordinates(pn, P_bottom, c='blue',
markersize=50, marker='.', ax=ax)
op.topotools.plot_coordinates(pn, Ps, c='green',
markersize=50, marker='s', ax=ax)

[6]:

<matplotlib.collections.PathCollection at 0x7f284b398c40>


### Find Non-Shared Neighbors: XOR#

Finds all pores with exactly one connection to the input pores

Given a set of pores find the pores that are neighbors of one and only one of the input pores. This is called XOR, or ‘exclusve_or’ because it finds the pores that are neigbhors to the ‘bottom’ or the ‘left’, but not both.

[7]:

Ps = pn.pores(['left', 'back'])
print(Ps)
Ps = pn.find_neighbor_pores (pores=Ps, mode='xor')
print(Ps)

[ 0  1  2  3  7 11 15]
[ 4  5 10 14]

[8]:

fig, ax = plt.subplots()
op.topotools.plot_coordinates(pn, pn.Ps, c='lightgrey',
markersize=50, ax=ax)
op.topotools.plot_coordinates(pn, P_left, c='red',
markersize=50, marker='*', ax=ax)
op.topotools.plot_coordinates(pn, P_bottom, c='blue',
markersize=50, marker='.', ax=ax)
op.topotools.plot_coordinates(pn, Ps, c='green',
markersize=50, marker='s', ax=ax)

[8]:

<matplotlib.collections.PathCollection at 0x7f284b3649d0>


### Find Common Neighbors of Two Sets: XNOR#

Finds all the pores with 2 or more connections to the input pores

This finds pores that are common to both ‘left’ and ‘bottom’ pores. It is called XNOR since it is the opposite of XOR , incidated by the N for not . Note that XNOR and NXOR are interchangable.

[9]:

Ps = pn.pores(['left', 'back'])
print(Ps)
Ps = pn.find_neighbor_pores(pores=Ps, mode='xnor')
print(Ps)

[ 0  1  2  3  7 11 15]
[6]

[10]:

fig, ax = plt.subplots()
op.topotools.plot_coordinates(pn, pn.Ps, c='lightgrey',
markersize=50, ax=ax)
op.topotools.plot_coordinates(pn, P_left, c='red',
markersize=50, marker='*', ax=ax)
op.topotools.plot_coordinates(pn, P_bottom, c='blue',
markersize=50, marker='.', ax=ax)
op.topotools.plot_coordinates(pn, Ps, c='green',
markersize=50, marker='s', ax=ax)

[10]:

<matplotlib.collections.PathCollection at 0x7f284b2e4e50>


## Find Neighboring Throats#

Neighbor throat queries follow essentially the same logic as the neighboring queries outlined above.

### Find All Neighboring Throats: OR#

Finds all throats connected to any of the input pores

[11]:

Ps = pn.pores(['left', 'back'])
Ts = pn.find_neighbor_throats(pores=Ps, mode='or')

[12]:

fig, ax = plt.subplots()
op.topotools.plot_connections(pn, Ts, ax=ax)
op.topotools.plot_coordinates(pn, pn.Ps, c='lightgrey',
markersize=50, ax=ax)
op.topotools.plot_coordinates(pn, P_left, c='red',
markersize=50, marker='*', ax=ax)
op.topotools.plot_coordinates(pn, P_bottom, c='blue',
markersize=50, marker='.', ax=ax)

[12]:

<matplotlib.collections.PathCollection at 0x7f284b1f3b20>


### Find Common Neighbors: XNOR#

Finds throats shared by input pores only

[13]:

Ps = pn.pores(['left', 'back'])
Ts = pn.find_neighbor_throats(pores=Ps, mode='xnor')

[14]:

fig, ax = plt.subplots()
op.topotools.plot_connections(pn, Ts, ax=ax)
op.topotools.plot_coordinates(pn, pn.Ps, c='lightgrey',
markersize=50, ax=ax)
op.topotools.plot_coordinates(pn, P_left, c='red',
markersize=50, marker='*', ax=ax)
op.topotools.plot_coordinates(pn, P_bottom, c='blue',
markersize=50, marker='.', ax=ax)

[14]:

<matplotlib.collections.PathCollection at 0x7f284b1e4df0>


### Find Non-Shared Neighbors: XOR#

Finds throats that are only connected to one input pore

[15]:

Ps = pn.pores(['left', 'back'])
Ts = pn.find_neighbor_throats(pores=Ps, mode='xor')

[16]:

fig, ax = plt.subplots()
op.topotools.plot_connections(pn, Ts, ax=ax)
op.topotools.plot_coordinates(pn, pn.Ps, c='lightgrey',
markersize=50, ax=ax)
op.topotools.plot_coordinates(pn, P_left, c='red',
markersize=50, marker='*', ax=ax)
op.topotools.plot_coordinates(pn, P_bottom, c='blue',
markersize=50, marker='.', ax=ax)

[16]:

<matplotlib.collections.PathCollection at 0x7f284b155f10>