Commit 4fc26d98 authored by sfritschi's avatar sfritschi
Browse files

Refactoring and testing

parent d7c6560e
......@@ -4,9 +4,7 @@ import matplotlib.pyplot as plt
from netflow import *
import time
import random
import multiprocessing as mp
from time import perf_counter
def main():
basenet = netflow.load_network_from('../netflow/network/network.h5')
......@@ -16,14 +14,23 @@ def main():
print("Min. number of throats per pore: %d" % min([len(pore.throats) for pore in basenet.pores]))
print("Avg. number of throats per pore: %f" % (sum([len(pore.throats) for pore in basenet.pores]) / len(basenet.pores)))
target = [2, 2, 2]
target = [3, 3, 3]
cutoff = 0.5 * max([basenet.ub[i] - basenet.lb[i] \
for i in range(len(basenet.ub))])
start = perf_counter()
dendro = netflow.generate_dendrogram(basenet=basenet, targetsize=target, \
cutoff=cutoff, sd=42, mute=False)
end = perf_counter()
elapsed = end - start
print("Elapsed time: %e s" % elapsed)
"""
_ = netgen.plot_network(dendro)
plt.savefig('plots/dendro.png')
"""
print("Generated network statistics:")
print("Throats: %d" % len(dendro.throats))
print("Pores: %d" % len(dendro.pores))
if __name__ == '__main__':
main()
gen/plots/dendro.png

134 KB | W: | H:

gen/plots/dendro.png

104 KB | W: | H:

gen/plots/dendro.png
gen/plots/dendro.png
gen/plots/dendro.png
gen/plots/dendro.png
  • 2-up
  • Swipe
  • Onion skin
import multiprocessing as mp
def work(x, y):
res = []
for i in range(x):
res.append(y)
return res
class Dummy:
def __init__(self, n):
self.vals = [i for i in range(n)]
def square(self, i):
return self.vals[i]**2
def main():
with mp.Pool(processes=2) as p:
res = p.starmap(func=work, iterable=[(1, 2), (3, 4)])
dummy = Dummy(10)
with mp.Pool(processes=4) as p:
res = p.map(func=dummy.square, iterable=[i for i in range(6)])
print(res)
if __name__ == '__main__':
......
......@@ -166,9 +166,9 @@ class CellList:
for i in range(-1, 2):
for j in range(-1, 2):
for k in range(-1, 2):
current = [cellIdxTrip[0] + i, cellIdxTrip[1] + j, \
cellIdxTrip[2] + k]
nborIdx = self.flatten(*current)
nborIdx = self.flatten(cellIdxTrip[0] + i, \
cellIdxTrip[1] + j, \
cellIdxTrip[2] + k)
# Check if valid cell index
#assert(self.is_valid_index(nborIdx))
......@@ -550,19 +550,6 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
n = len(pores)
copies = __add_buffer_layers(pores, L, basenet.Lmax)
"""
DEBUG: CELL LIST STATISTICS
print(len(pores))
# Optimal cutoff; no interaction beyond cutoff
cutoff = max([distance(*throat_ends(throat,
[ub-lb for lb,ub in zip(basenet.lb,basenet.ub)])) for pore in pores for throat in pore.throats])
print("Cutoff: %e, Lmax: %e" % (cutoff, basenet.Lmax))
from collections import Counter
cellmembers = Counter([(int(pore.pos[0] / cutoff), int(pore.pos[1] / cutoff), int(pore.pos[2] / cutoff)) for pore in pores])
print("Max: %d" % max(cellmembers.values()))
print("Min: %d" % min(cellmembers.values()))
print("Avg: %f" % (sum(cellmembers.values()) / len(cellmembers)))
"""
# Domain size including periodic buffer layers on all sides
trueDomainSize = [L[i] + 2 * basenet.Lmax for i in range(d)]
# Initialize cell-list; Place each pore in respective cell-set
......@@ -573,6 +560,13 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
throats = []
k = 0 # count of unrealised throats
"""
# DEBUG
import sys
max_candidates = 0
min_candidates = sys.maxsize
avg_candidates = 0
"""
# Spawn parallel
for i, pore in enumerate(pores[:n]):
# (re)run triangulation if maximal number of neighbors exceeds
......@@ -585,6 +579,17 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
# find candidates in neighborhood and respective throat lengths
# TODO: Search neighboring cells in parallel
candidates = cellList.find_candidates(pore)
"""
# DEBUG
nCandidates = len(candidates)
avg_candidates += nCandidates
if (nCandidates > max_candidates):
max_candidates = nCandidates
if (nCandidates < min_candidates):
min_candidates = nCandidates
"""
# establish throat connections between pore and best candidate
while (len(pore.throats) > 0):
"""
......@@ -596,7 +601,6 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
lt = distance(*throat_ends(throat,
[ub-lb for lb,ub in zip(basenet.lb,basenet.ub)])) # target len.
# find best candidate
# avoid double connections and throats longer than Lmax
if (len(candidates) == 0):
k = k+1; continue # no candidates left, give up
nbor, lt = min(candidates.items(), key=lambda pl: abs(pl[1] - lt))
......@@ -631,7 +635,14 @@ def generate_dendrogram(basenet: Network, targetsize: List[int], \
print("\b"*24 + "{0:d} throats in total, {1:d} unrealised".\
format(len(throats), k))
"""
# DEBUG
avg_candidates /= n
print("Candidates statistics")
print("Max. candidates per pore: %d" % max_candidates)
print("Min. candidates per pore: %d" % min_candidates)
print("Avg. candidates per pore: %f" % avg_candidates)
"""
# assemble and return network
network = Network(lb=[0.0 for k in range(d)],
ub=L, Lmax=basenet.Lmax, label='from_' + basenet.label)
......
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