Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Abstract away the parameters into a parameters class #16

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 160 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
52 changes: 27 additions & 25 deletions fvm/Continuation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from math import sqrt
from fvm.utils import norm
from fvm.interface import BaseInterface
from fvm.parameters import Parameters

class Continuation:

def __init__(self, interface, parameters):
def __init__(self, interface: BaseInterface, parameters: Parameters):
self.interface = interface
self.parameters = parameters

Expand All @@ -14,12 +16,12 @@ def __init__(self, interface, parameters):
self.zeta = None

def newton(self, x0):
residual_check = self.parameters.get('Residual Check', 'F')
verbose = self.parameters.get('Verbose', False)
residual_check = self.parameters.residual_check
verbose = self.parameters.verbose

# Set Newton some parameters
maxit = self.parameters.get('Maximum Newton Iterations', 10)
tol = self.parameters.get('Newton Tolerance', 1e-10)
maxit = self.parameters.maximum_newton_iterations
tol = self.parameters.newton_tolerance

x = x0
for k in range(maxit):
Expand Down Expand Up @@ -52,12 +54,12 @@ def newton(self, x0):
return x

def newtoncorrector(self, parameter_name, ds, x, x0, mu, mu0):
residual_check = self.parameters.get('Residual Check', 'F')
verbose = self.parameters.get('Verbose', False)
residual_check = self.parameters.residual_check
verbose = self.parameters.verbose

# Set Newton some parameters
maxit = self.parameters.get('Maximum Newton Iterations', 10)
tol = self.parameters.get('Newton Tolerance', 1e-4)
maxit = self.parameters.maximum_newton_iterations
tol = self.parameters.newton_tolerance

self.newton_iterations = 0

Expand Down Expand Up @@ -94,7 +96,7 @@ def newtoncorrector(self, parameter_name, ds, x, x0, mu, mu0):
dflval = (self.interface.rhs(x) - fval) / self.delta
self.interface.set_parameter(parameter_name, mu)

if self.parameters.get("Bordered Solver", False):
if self.parameters.bordered_solver:
# Solve the entire bordered system in one go (2.2.9)
dx, dmu = self.interface.solve(jac, -fval, -rnp1, dflval, 2 * self.zeta * diff,
2 * (1 - self.zeta) * (mu - mu0))
Expand Down Expand Up @@ -142,9 +144,9 @@ def newtoncorrector(self, parameter_name, ds, x, x0, mu, mu0):
def adjust_step_size(self, ds):
''' Step size control, see [Seydel p 188.] '''

min_step_size = self.parameters.get('Minimum Step Size', 0.01)
max_step_size = self.parameters.get('Maximum Step Size', 2000)
optimal_newton_iterations = self.parameters.get('Optimal Newton Iterations', 3)
min_step_size = self.parameters.minimum_step_size
max_step_size = self.parameters.maximum_step_size
optimal_newton_iterations = self.parameters.optimal_newton_iterations

factor = optimal_newton_iterations / max(self.newton_iterations, 1)
factor = min(max(factor, 0.5), 2.0)
Expand All @@ -153,15 +155,15 @@ def adjust_step_size(self, ds):

ds = math.copysign(min(max(abs(ds), min_step_size), max_step_size), ds)

if self.parameters.get('Verbose', False):
if self.parameters.verbose:
print('New stepsize: ds=%e, factor=%e' % (ds, factor), flush=True)

return ds

def detect_bifurcation(self, parameter_name, x, mu, dx, dmu, eig, deig, v, ds, maxit):
''' Converge onto a bifurcation '''

tol = self.parameters.get('Destination Tolerance', 1e-4)
tol = self.parameters.destination_tolerance

for j in range(maxit):
if abs(eig.real) < tol:
Expand All @@ -183,7 +185,7 @@ def detect_bifurcation(self, parameter_name, x, mu, dx, dmu, eig, deig, v, ds, m
def converge(self, parameter_name, x, mu, dx, dmu, target, ds, maxit):
''' Converge onto the target value '''

tol = self.parameters.get('Destination Tolerance', 1e-4)
tol = self.parameters.destination_tolerance

for j in range(maxit):
if abs(target - mu) < tol:
Expand Down Expand Up @@ -221,8 +223,8 @@ def step(self, parameter_name, x, mu, dx, dmu, ds):

print("%s: %f" % (parameter_name, mu), flush=True)

if 'Postprocess' in self.parameters and self.parameters['Postprocess']:
self.parameters['Postprocess'](self.interface, x, mu)
if self.parameters.postprocess is not None:
self.parameters.postprocess(self.interface, x, mu)

# Set the new values computed by the corrector
dmu = mu - mu0
Expand Down Expand Up @@ -251,7 +253,7 @@ def switch_branches_tangent(self, parameter_name, x, mu, dx, dmu, v, ds):
dflval = (self.interface.rhs(x) - fval) / self.delta
self.interface.set_parameter(parameter_name, mu)

if self.parameters.get("Bordered Solver", False):
if self.parameters.bordered_solver:
# Solve the entire bordered system in one go (5.16)
dx, dmu = self.interface.solve(jac, 0 * x, 0, dflval, dx, dmu)
else:
Expand All @@ -262,7 +264,7 @@ def switch_branches_tangent(self, parameter_name, x, mu, dx, dmu, v, ds):

if abs(dmu) < 1e-12:
dmu = dmu0
ds = self.parameters.get('Minimum Step Size', 0.01)
ds = self.parameters.minimum_step_size

return x, mu, dx, dmu, ds

Expand All @@ -277,7 +279,7 @@ def switch_branches_asymmetry(self, parameter_name, x, mu, ds):
return x, mu, dx, dmu, ds

def switch_branches(self, parameter_name, x, mu, dx, dmu, v, ds):
branch_switching_method = self.parameters.get('Branch Switching Method', 'Tangent')
branch_switching_method = self.parameters.branch_switching_method
if branch_switching_method == 'Asymmetry':
return self.switch_branches_asymmetry(parameter_name, x, mu, ds)

Expand Down Expand Up @@ -327,7 +329,7 @@ def continuation(self, x0, parameter_name, start, target, ds,
mu = start

# Set some parameters
self.delta = self.parameters.get('Delta', 1)
self.delta = self.parameters.delta
self.zeta = 1 / x.size

if not dx or not dmu:
Expand All @@ -340,11 +342,11 @@ def continuation(self, x0, parameter_name, start, target, ds,
eig = None

if not maxit:
maxit = self.parameters.get('Maximum Continuation Steps', 1000)
maxit = self.parameters.maximum_continuation_steps

# Some configuration for the detection of bifurcations
detect_bifurcations = self.parameters.get('Detect Bifurcation Points', False)
enable_branch_switching = self.parameters.get('Enable Branch Switching', False)
detect_bifurcations = self.parameters.detect_bifurcation_points
enable_branch_switching = self.parameters.enable_branch_switching
enable_recycling = False

# Perform the continuation
Expand Down
Loading