PMEAL / OpenPNM

A Python package for performing pore network modeling of porous media
http://openpnm.org
MIT License
442 stars 175 forks source link

Find neighbor pores to X whose only neighbor is X #1817

Closed ma-sadeghi closed 3 years ago

ma-sadeghi commented 3 years ago
  2 - 5
 /
1 - 3
 \
  4 - 6

Here's what I'm looking for:

>>> f(pores=1)
[3]

I use it to find "orphan" pores in my problem. I'm not sure if it's worth implementing it in OpenPNM.

jgostick commented 3 years ago

You'd have to do it in reverse order:

z = pn.num_neighbors(pores=pn.Ps)
orphans = np.where(z == 1)[0]
hits = set(pn.find_neighbor_pores(pores=i)).intersection(set(orphans))

Do you think this is relevant enough to make it a function? Probably not on the network class, but maybe in topotools? It could be generalized by changing the second line to np.where(z == X), with X being a value supplied by the user.

jgostick commented 3 years ago

actually, the last line should be: np.intersect1d(pn.find_neighbor_pores(pores=i), orphans) to keep everything in numpy

jgostick commented 3 years ago
def find_neighbor_z(network, pore, z=1):
    Nz = network.num_neighbors(pores=network.Ps)
    orphans = np.where(Nz == z)[0]
    hits = np.intersect1d(network.find_neighbor_pores(pores=pore), orphans)
    return hits
ma-sadeghi commented 3 years ago

Thanks @jgostick. It might be handy to have it under topotools.

jgostick commented 3 years ago

Did it work for you?

ma-sadeghi commented 3 years ago

Did it work for you?

Yep, worked like a charm! Here's a sample script:

import numpy as np
import openpnm as op
import matplotlib.pyplot as plt

net = op.network.Cubic([4, 4])
Ts1 = net.find_neighbor_throats(pores=1)
op.topotools.trim(net, throats=Ts1[[1, 2]])
Ts2 = net.find_neighbor_throats(pores=4)
op.topotools.trim(net, throats=Ts2[[2]])

fig, ax = plt.subplots()
op.topotools.plot_coordinates(net, fig=fig, markersize=100)
op.topotools.plot_connections(net, fig=fig)

def find_neighbor_z(network, pore, z=1):
    Nz = network.num_neighbors(pores=network.Ps)
    orphans = np.where(Nz == z)[0]
    hits = np.intersect1d(network.find_neighbor_pores(pores=pore), orphans)
    return hits

Ps1 = find_neighbor_z(net, pore=0, z=1)
op.topotools.plot_coordinates(net, fig=fig, pores=Ps1, color="g", markersize=100)

Ps2 = find_neighbor_z(net, pore=0, z=2)
op.topotools.plot_coordinates(net, fig=fig, pores=Ps2, color="k", markersize=100)

Original network

Find neighbors of pore 0 that only have 1 neighbor

Find neighbors of pore 0 that only have 2 neighbor