Commit 28b50d13 authored by sfritschi's avatar sfritschi
Browse files

Improvements/Refactoring

parent 8dba1790
......@@ -7,8 +7,8 @@ from netflow import *
from time import perf_counter
def main():
if len(sys.argv) != 2:
raise AssertionError("Usage: python3 generate.py <nthreads>")
if len(sys.argv) != 5:
raise AssertionError("Usage: python3 generate.py <nthreads> <n1> <n2> <n3>")
nthreads = int(sys.argv[1])
print("Using %d threads" % nthreads)
......@@ -20,7 +20,7 @@ def main():
print("Min. number of throats per pore: %d" % min(pore_throat_counts))
print("Avg. number of throats per pore: %f" % (sum(pore_throat_counts) / len(pore_throat_counts)))
target = [3, 3, 3]
target = [int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4])]
print("Target size: {}".format(target))
cutoff = 0.5 * max([basenet.ub[i] - basenet.lb[i] \
for i in range(len(basenet.ub))])
......
......@@ -199,7 +199,7 @@ class CellList:
throatIdx += 1
throatTotal = offset
self.poreThroatTable = mp.RawArray('i', throatTotal)
self.poreMatchTable = mp.RawArray('i', throatTotal)
# Helper for converting 3D index triple into 1D (flattened) index
def flatten(self, i: int, j: int, k: int) -> int:
......@@ -224,19 +224,13 @@ class CellList:
def match_maker(self, pores: List[int], tid: int):
n = len(pores)
for poreIdx in pores:
if tid == 0:
print(f"progress {((poreIdx+1) / n)*100.:.1f}%", end="\r", flush=True)
porePos = self.fetch_pos(poreIdx)
neighborhood = self.nbor_indices(poreIdx)
candidates = {}
for nborCellIdx in neighborhood:
for nborIdx in self.poresSorted[nborCellIdx]:
if (nborIdx == poreIdx): continue
l = distance(porePos, self.fetch_pos(nborIdx))
if (l < self.Lmax):
candidates[nborIdx] = l
candidates = self.find_candidates(poreIdx)
throats = self.fetch_throat_indices(poreIdx)
for idx, throatIdx in enumerate(throats):
......@@ -246,6 +240,8 @@ class CellList:
matchIdx, matchLen = min(candidates.items(), key=lambda il: abs(il[1] - lt))
# Remove match from candidates
del candidates[matchIdx]
# Put best match index in poreThroatTable
self.set_throat(poreIdx, matchIdx, idx)
# Remove periodic copies as well if targetsize contains 1
if self.isPeriodicCheck:
# Get index of original pore in interior of domain
......@@ -257,34 +253,39 @@ class CellList:
for cporeIdx in self.periodicCopies[originalIdx]:
if cporeIdx in candidates:
del candidates[cporeIdx]
# Put best match index in poreThroatTable
self.set_throat(poreIdx, matchIdx, idx)
# Compute dictionary of all nbor candidates of given pore
def find_candidates(self, poreIdx: int) -> Dict[int, float]:
candidates = {}
porePos = self.fetch_pos(poreIdx)
cellIdxTrip = self.pore_to_triplet(poreIdx)
nearestCellIdx = self.flatten(*cellIdxTrip)
neighborhood = self.nbor_indices(poreIdx)
nearestCellIdx = neighborhood[0]
# Search current cell
for nborIdx in self.poresSorted[nearestCellIdx]:
if (nborIdx == poreIdx): continue
l = distance(self.fetch_pos(nborIdx), porePos)
if (l < self.Lmax):
candidates[nborIdx] = l
# Search neighboring cells
for nborCellIdx in neighborhood[1:]:
for nborIdx in self.poresSorted[nborCellIdx]:
l = distance(self.fetch_pos(nborIdx), porePos)
if (l < self.Lmax):
candidates[nborIdx] = l
return candidates
def set_throat(self, poreIdx: int, matchIdx: int, idx: int):
index = self.throatOffsets[poreIdx] + idx
# Set matchIdx
self.poreThroatTable[index] = matchIdx
self.poreMatchTable[index] = matchIdx
def fetch_throats(self, poreIdx: int) -> List[int]:
def fetch_matches(self, poreIdx: int) -> List[int]:
lb = self.throatOffsets[poreIdx]
ub = self.throatOffsets[poreIdx + 1]
return self.poreThroatTable[lb:ub]
return self.poreMatchTable[lb:ub]
def fetch_throat_indices(self, poreIdx: int) -> List[int]:
lb = self.throatOffsets[poreIdx]
......@@ -722,7 +723,7 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
raise NameError('targetsize leads network < Lmax of basenet!')
# pores, distributed based on dendrogram of basenet
if (not mute): print("distributing pores...", end="", flush=True)
if (not mute): print("distributing pores...")
# make indexable and discard in-/outflow pores
basepores = [pore for pore in basenet.pores \
if ((pore.label[:len(LABELS[1])] != LABELS[1]) \
......@@ -826,7 +827,7 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
worker.join()
# throats, connect pores
if (not mute): print("connecting")
if (not mute): print("\nconnecting")
throats = []
k = 0 # count of unrealised throats
......@@ -844,7 +845,7 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
ttbr = cellList.poreThroatIndices[poreIdx]
if (len(ttbr) == 0): continue # Nothing left to do
matches = cellList.fetch_throats(poreIdx)
matches = cellList.fetch_matches(poreIdx)
throatIndices = cellList.fetch_throat_indices(poreIdx)
for i, match in enumerate(matches):
......@@ -906,11 +907,11 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
cellList.expel(cpore)
# Free space
del cellList.poreThroatTable
del cellList.poreMatchTable
# Finally, find next best candidates for left-out pores
# in REVERSED order
if (not mute): print("finding next best candidates")
if (not mute): print("\nfinding next best candidates")
poreIdx = n
for it, pore in enumerate(reversed(pores[:n])):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment