Skip to content

Commit

Permalink
Merge pull request #97 from tldr-group/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
daubners authored Nov 4, 2024
2 parents 3b0c37f + 46624bf commit 982efc5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/draft-pdf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
# This should be the path to the paper within your repo.
paper-path: paper.md
- name: Upload
uses: actions/upload-artifact@v1
uses: actions/upload-artifact@v3
with:
name: paper
# This is the output path where Pandoc will write the compiled
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_theme_path = ["_themes", ]

# Theme options are theme-specific and customize the look and feel of a
# theme further. For a list of options available for each theme, see the
Expand Down
5 changes: 4 additions & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ torch
scipy
matplotlib
tifffile
myst_parser
myst_parser
sphinx==5.3.0
sphinx_rtd_theme==1.1.1
readthedocs-sphinx-search==0.1.1
44 changes: 27 additions & 17 deletions taufactor/taufactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def __init__(self, img, bc=(-0.5, 0.5), D_0=1, device=torch.device('cuda')):
f'Input image must only contain 0s and 1s. Your image must be segmented to use this tool. If your image has been segmented, ensure your labels are 0 for non-conductive and 1 for conductive phase. Your image has the following labels: {torch.unique(img).numpy()}. If you have more than one conductive phase, use the multi-phase solver.')

# calculate
self.ph_bot = torch.sum(img[:, -1]).to(self.device) * self.bot_bc
self.ph_top = torch.sum(img[:, 0]).to(self.device) * self.top_bc

# init conc
self.conc = self.init_conc(img)
# create nn map
Expand Down Expand Up @@ -137,7 +136,7 @@ def solve(self, iter_limit=5000, verbose=True, conv_crit=2*10**-2):

with torch.no_grad():
start = timer()
while not self.converged:
while not self.converged and self.iter < iter_limit:
# find sum of all nearest neighbours
out = self.conc[:, 2:, 1:-1, 1:-1] + \
self.conc[:, :-2, 1:-1, 1:-1] + \
Expand All @@ -149,8 +148,7 @@ def solve(self, iter_limit=5000, verbose=True, conv_crit=2*10**-2):
out /= self.nn
# check convergence using criteria
if self.iter % 100 == 0:
self.converged = self.check_convergence(
verbose, conv_crit, start, iter_limit)
self.converged = self.check_convergence(verbose, conv_crit)
# efficient way of adding flux to old conc with overrelaxation
out -= self.crop(self.conc, 1)
out *= self.cb[self.iter % 2]
Expand All @@ -161,7 +159,7 @@ def solve(self, iter_limit=5000, verbose=True, conv_crit=2*10**-2):
self.end_simulation(iter_limit, verbose, start)
return self.tau

def check_convergence(self, verbose, conv_crit, start, iter_limit):
def check_convergence(self, verbose, conv_crit):
# print progress
self.semi_converged, self.new_fl, err = self.check_vertical_flux(
conv_crit)
Expand Down Expand Up @@ -192,13 +190,18 @@ def check_convergence(self, verbose, conv_crit, start, iter_limit):
self.old_fl = self.new_fl
return False

def check_vertical_flux(self, conv_crit):
def calc_vertical_flux(self):
'''Calculates the vertical flux through the volume'''
vert_flux = self.conc[:, 1:-1, 1:-1, 1:-1] - \
self.conc[:, :-2, 1:-1, 1:-1]
vert_flux[self.conc[:, :-2, 1:-1, 1:-1] == 0] = 0
vert_flux[self.conc[:, 1:-1, 1:-1, 1:-1] == 0] = 0
return vert_flux

def check_vertical_flux(self, conv_crit):
vert_flux = self.calc_vertical_flux()
fl = torch.sum(vert_flux, (0, 2, 3))[1:-1]
err = (fl.max() - fl.min())*2/(fl.max() + fl.min())
err = (fl.max() - fl.min())/(fl.max())
if fl.min() == 0:
return 'zero_flux', torch.mean(fl), err
if err < conv_crit or torch.isnan(err).item():
Expand Down Expand Up @@ -276,8 +279,7 @@ def solve(self, iter_limit=5000, verbose=True, conv_crit=2*10**-2, D_0=1):
out = out[:, 2:-2]
out /= self.nn
if self.iter % 50 == 0:
self.converged = self.check_convergence(
verbose, conv_crit, start, iter_limit)
self.converged = self.check_convergence(verbose, conv_crit)
out -= self.conc[:, 2:-2]
out *= self.cb[self.iter % 2]
self.conc[:, 2:-2] += out
Expand All @@ -288,10 +290,15 @@ def solve(self, iter_limit=5000, verbose=True, conv_crit=2*10**-2, D_0=1):
self.end_simulation(iter_limit, verbose, start)
return self.tau

def check_vertical_flux(self, conv_crit):
def calc_vertical_flux(self):
'''Calculates the vertical flux through the volume'''
vert_flux = abs(self.conc - torch.roll(self.conc, 1, 1))
vert_flux[self.conc == 0] = 0
vert_flux[torch.roll(self.conc, 1, 1) == 0] = 0
return vert_flux

def check_vertical_flux(self, conv_crit):
vert_flux = self.calc_vertical_flux()
fl = torch.sum(vert_flux, (0, 2, 3))[3:-2]
err = (fl.max() - fl.min())*2/(fl.max() + fl.min())
if err < conv_crit or torch.isnan(err).item():
Expand Down Expand Up @@ -342,8 +349,7 @@ def __init__(self, img, cond={1: 1}, bc=(-0.5, 0.5), device=torch.device('cuda:0
img = torch.tensor(img, dtype=self.precision, device=self.device)

# calculate
self.ph_bot = torch.sum(img[:, -1]).to(self.device) * self.bot_bc
self.ph_top = torch.sum(img[:, 0]).to(self.device) * self.top_bc

# init conc
self.conc = self.init_conc(img)
# create nn map
Expand Down Expand Up @@ -441,16 +447,15 @@ def solve(self, iter_limit=5000, verbose=True, conv_crit=2*10**-2):
self.pre_factors[5][:, 1:-1, 1:-1, :-2]
out /= self.nn
if self.iter % 20 == 0:
self.converged = self.check_convergence(
verbose, conv_crit, start, iter_limit)
self.converged = self.check_convergence(verbose, conv_crit)
out -= self.crop(self.conc, 1)
out *= self.cb[self.iter % 2]
self.conc[:, 1:-1, 1:-1, 1:-1] += out

self.end_simulation(iter_limit, verbose, start)
return self.tau

def check_convergence(self, verbose, conv_crit, start, iter_limit):
def check_convergence(self, verbose, conv_crit):
# print progress
if self.iter % 100 == 0:
self.semi_converged, self.new_fl, err = self.check_vertical_flux(
Expand Down Expand Up @@ -491,10 +496,15 @@ def check_convergence(self, verbose, conv_crit, start, iter_limit):

return False

def check_vertical_flux(self, conv_crit):
def calc_vertical_flux(self):
'''Calculates the vertical flux through the volume'''
vert_flux = (self.conc[:, 1:-1, 1:-1, 1:-1] - self.conc[:,
:-2, 1:-1, 1:-1]) * self.pre_factors[1][:, :-2, 1:-1, 1:-1]
vert_flux[self.nn == torch.inf] = 0
return vert_flux

def check_vertical_flux(self, conv_crit):
vert_flux = self.calc_vertical_flux()
fl = torch.sum(vert_flux, (0, 2, 3))[2:-2]
err = (fl.max() - fl.min())*2/(fl.max() + fl.min())
if err < conv_crit or torch.isnan(err).item():
Expand Down

0 comments on commit 982efc5

Please sign in to comment.