Skip to content
Snippets Groups Projects
Commit 9b29a46d authored by Markus Holzer's avatar Markus Holzer
Browse files

Merge branch 'dependency-deprecations' into 'master'

Fix usages of deprecated features.

See merge request pycodegen/lbmpy!174
parents 942e11d7 81873d09
Branches
No related tags found
No related merge requests found
...@@ -107,18 +107,25 @@ class IPyNbTest(pytest.Item): ...@@ -107,18 +107,25 @@ class IPyNbTest(pytest.Item):
# disable matplotlib output # disable matplotlib output
exec("import matplotlib.pyplot as p; " exec("import matplotlib.pyplot as p; "
"p.close('all'); "
"p.switch_backend('Template')", global_dict) "p.switch_backend('Template')", global_dict)
# in notebooks there is an implicit plt.show() - if this is not called a warning is shown when the next # in notebooks there is an implicit plt.show() - if this is not called a warning is shown when the next
# plot is created. This warning is suppressed here # plot is created. This warning is suppressed here
# Also animations cannot be shown, which also leads to a warning.
exec("import warnings;" exec("import warnings;"
"warnings.filterwarnings('ignore', 'Adding an axes using the same arguments as a previous.*');", "warnings.filterwarnings('ignore', 'Adding an axes using the same arguments as a previous.*');"
"warnings.filterwarnings('ignore', 'Animation was deleted without rendering anything.*');",
global_dict) global_dict)
with tempfile.NamedTemporaryFile() as f: with tempfile.NamedTemporaryFile() as f:
f.write(self.code.encode()) f.write(self.code.encode())
f.flush() f.flush()
runpy.run_path(f.name, init_globals=global_dict, run_name=self.name) runpy.run_path(f.name, init_globals=global_dict, run_name=self.name)
# Close any open figures
exec("import matplotlib.pyplot as p; "
"p.close('all')", global_dict)
class IPyNbFile(pytest.File): class IPyNbFile(pytest.File):
def collect(self): def collect(self):
...@@ -141,10 +148,19 @@ class IPyNbFile(pytest.File): ...@@ -141,10 +148,19 @@ class IPyNbFile(pytest.File):
pass pass
def pytest_collect_file(path, parent): if pytest_version >= 70000:
glob_exprs = ["*demo*.ipynb", "*tutorial*.ipynb", "test_*.ipynb"] # Since pytest 7.0, usage of `py.path.local` is deprecated and `pathlib.Path` should be used instead
if any(path.fnmatch(g) for g in glob_exprs): import pathlib
if pytest_version >= 50403:
return IPyNbFile.from_parent(fspath=path, parent=parent) def pytest_collect_file(file_path: pathlib.Path, parent):
else: glob_exprs = ["*demo*.ipynb", "*tutorial*.ipynb", "test_*.ipynb"]
return IPyNbFile(path, parent) if any(file_path.match(g) for g in glob_exprs):
return IPyNbFile.from_parent(path=file_path, parent=parent)
else:
def pytest_collect_file(path, parent):
glob_exprs = ["*demo*.ipynb", "*tutorial*.ipynb", "test_*.ipynb"]
if any(path.fnmatch(g) for g in glob_exprs):
if pytest_version >= 50403:
return IPyNbFile.from_parent(fspath=path, parent=parent)
else:
return IPyNbFile(path, parent)
...@@ -39,8 +39,8 @@ def circle_intersections(midpoint0, midpoint1, radius0, radius1): ...@@ -39,8 +39,8 @@ def circle_intersections(midpoint0, midpoint1, radius0, radius1):
def interface_region(concentration_arr, phase0, phase1, area=3): def interface_region(concentration_arr, phase0, phase1, area=3):
import scipy.ndimage as sc_image import scipy.ndimage as sc_image
area_phase0 = sc_image.morphology.binary_dilation(concentration_arr[..., phase0] > 0.5, iterations=area) area_phase0 = sc_image.binary_dilation(concentration_arr[..., phase0] > 0.5, iterations=area)
area_phase1 = sc_image.morphology.binary_dilation(concentration_arr[..., phase1] > 0.5, iterations=area) area_phase1 = sc_image.binary_dilation(concentration_arr[..., phase1] > 0.5, iterations=area)
return np.logical_and(area_phase0, area_phase1) return np.logical_and(area_phase0, area_phase1)
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import pytest import pytest
pytest.importorskip('cupy') pytest.importorskip('cupy')
``` ```
%% Output %% Output
<module 'cupy' from '/home/markus/.local/lib/python3.11/site-packages/cupy/__init__.py'> <module 'cupy' from '/home/markus/.local/lib/python3.11/site-packages/cupy/__init__.py'>
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
from lbmpy.session import * from lbmpy.session import *
from lbmpy.phasefield.n_phase_boyer import * from lbmpy.phasefield.n_phase_boyer import *
from lbmpy.phasefield.kerneleqs import * from lbmpy.phasefield.kerneleqs import *
from lbmpy.phasefield.contact_angle_circle_fitting import * from lbmpy.phasefield.contact_angle_circle_fitting import *
from scipy.ndimage.filters import gaussian_filter from scipy.ndimage import gaussian_filter
from pystencils.simp import sympy_cse_on_assignment_list from pystencils.simp import sympy_cse_on_assignment_list
one = sp.sympify(1) one = sp.sympify(1)
import pyximport import pyximport
pyximport.install(language_level=3) pyximport.install(language_level=3)
from lbmpy.phasefield.simplex_projection import simplex_projection_2d # NOQA from lbmpy.phasefield.simplex_projection import simplex_projection_2d # NOQA
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# Simulation arbitrary surface tension case # Simulation arbitrary surface tension case
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
n = 4 n = 4
dx, dt = 1, 1 dx, dt = 1, 1
mobility = 2e-3 mobility = 2e-3
domain_size = (150, 150) domain_size = (150, 150)
ε = one * 4 ε = one * 4
penalty_factor = 0 penalty_factor = 0
stabilization_factor = 10 stabilization_factor = 10
κ = (one, one/2, one/3, one/4) κ = (one, one/2, one/3, one/4)
sigma_factor = one / 15 sigma_factor = one / 15
σ = sp.ImmutableDenseMatrix(n, n, lambda i,j: sigma_factor* (κ[i] + κ[j]) if i != j else 0 ) σ = sp.ImmutableDenseMatrix(n, n, lambda i,j: sigma_factor* (κ[i] + κ[j]) if i != j else 0 )
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dh = create_data_handling(domain_size, periodicity=True, default_target=ps.Target.GPU) dh = create_data_handling(domain_size, periodicity=True, default_target=ps.Target.GPU)
c = dh.add_array('c', values_per_cell=n) c = dh.add_array('c', values_per_cell=n)
c_tmp = dh.add_array_like('c_tmp', 'c') c_tmp = dh.add_array_like('c_tmp', 'c')
μ = dh.add_array('mu', values_per_cell=n) μ = dh.add_array('mu', values_per_cell=n)
cvec = c.center_vector cvec = c.center_vector
μvec = μ.center_vector μvec = μ.center_vector
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
α, _ = diffusion_coefficients(σ) α, _ = diffusion_coefficients(σ)
f = lambda c: c**2 * ( 1 - c ) **2 f = lambda c: c**2 * ( 1 - c ) **2
a, b = compute_ab(f) a, b = compute_ab(f)
capital_f = capital_f0(cvec, σ) + correction_g(cvec, σ) + stabilization_factor * stabilization_term(cvec, α) capital_f = capital_f0(cvec, σ) + correction_g(cvec, σ) + stabilization_factor * stabilization_term(cvec, α)
f_bulk = free_energy_bulk(capital_f, b, ε) + penalty_factor * (one - sum(cvec)) f_bulk = free_energy_bulk(capital_f, b, ε) + penalty_factor * (one - sum(cvec))
f_if = free_energy_interfacial(cvec, σ, a, ε) f_if = free_energy_interfacial(cvec, σ, a, ε)
f = f_bulk + f_if f = f_bulk + f_if
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#f_bulk #f_bulk
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
μ_assignments = mu_kernel(f, cvec, c, μ) μ_assignments = mu_kernel(f, cvec, c, μ)
μ_assignments = [Assignment(a.lhs, a.rhs.doit()) for a in μ_assignments] μ_assignments = [Assignment(a.lhs, a.rhs.doit()) for a in μ_assignments]
μ_assignments = sympy_cse_on_assignment_list(μ_assignments) μ_assignments = sympy_cse_on_assignment_list(μ_assignments)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
discretize = fd.Discretization2ndOrder(dx=dx, dt=dt) discretize = fd.Discretization2ndOrder(dx=dx, dt=dt)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def lapl(e): def lapl(e):
return sum(ps.fd.diff(e, d, d) for d in range(dh.dim)) return sum(ps.fd.diff(e, d, d) for d in range(dh.dim))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
rhs = α * μvec rhs = α * μvec
discretized_rhs = [discretize(fd.expand_diff_full( lapl(mobility * rhs_i) + fd.transient(cvec[i], idx=i), functions=μvec)) discretized_rhs = [discretize(fd.expand_diff_full( lapl(mobility * rhs_i) + fd.transient(cvec[i], idx=i), functions=μvec))
for i, rhs_i in enumerate(rhs)] for i, rhs_i in enumerate(rhs)]
c_assignments = [Assignment(lhs, rhs) c_assignments = [Assignment(lhs, rhs)
for lhs, rhs in zip(c_tmp.center_vector, discretized_rhs)] for lhs, rhs in zip(c_tmp.center_vector, discretized_rhs)]
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#c_assignments #c_assignments
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
μ_sync = dh.synchronization_function(μ.name) μ_sync = dh.synchronization_function(μ.name)
c_sync = dh.synchronization_function(c.name) c_sync = dh.synchronization_function(c.name)
optimization = {'cpu_openmp': False, 'cpu_vectorize_info': None} optimization = {'cpu_openmp': False, 'cpu_vectorize_info': None}
config = ps.CreateKernelConfig(cpu_openmp=False, target=dh.default_target) config = ps.CreateKernelConfig(cpu_openmp=False, target=dh.default_target)
μ_kernel = create_kernel(μ_assignments, config=config).compile() μ_kernel = create_kernel(μ_assignments, config=config).compile()
c_kernel = create_kernel(c_assignments, config=config).compile() c_kernel = create_kernel(c_assignments, config=config).compile()
def set_c(slice_obj, values): def set_c(slice_obj, values):
for block in dh.iterate(slice_obj): for block in dh.iterate(slice_obj):
arr = block[c.name] arr = block[c.name]
arr[..., : ] = values arr[..., : ] = values
def smooth(): def smooth():
for block in dh.iterate(ghost_layers=True): for block in dh.iterate(ghost_layers=True):
c_arr = block[c.name] c_arr = block[c.name]
for i in range(n): for i in range(n):
gaussian_filter(c_arr[..., i], sigma=2, output=c_arr[..., i]) gaussian_filter(c_arr[..., i], sigma=2, output=c_arr[..., i])
def time_loop(steps): def time_loop(steps):
dh.all_to_gpu() dh.all_to_gpu()
for t in range(steps): for t in range(steps):
c_sync() c_sync()
dh.run_kernel(μ_kernel) dh.run_kernel(μ_kernel)
μ_sync() μ_sync()
dh.run_kernel(c_kernel) dh.run_kernel(c_kernel)
dh.swap(c.name, c_tmp.name) dh.swap(c.name, c_tmp.name)
#simplex_projection_2d(dh.cpu_arrays[c.name]) #simplex_projection_2d(dh.cpu_arrays[c.name])
dh.all_to_cpu() dh.all_to_cpu()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
set_c(make_slice[:, :], [0, 0, 0, 0]) set_c(make_slice[:, :], [0, 0, 0, 0])
set_c(make_slice[:, 0.5:], [1, 0, 0, 0]) set_c(make_slice[:, 0.5:], [1, 0, 0, 0])
set_c(make_slice[:, :0.5], [0, 1, 0, 0]) set_c(make_slice[:, :0.5], [0, 1, 0, 0])
set_c(make_slice[0.3:0.7, 0.3:0.7], [0, 0, 1, 0]) set_c(make_slice[0.3:0.7, 0.3:0.7], [0, 0, 1, 0])
smooth() smooth()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#dh.load_all('n_phases_state_size200_stab10.npz') #dh.load_all('n_phases_state_size200_stab10.npz')
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
plt.phase_plot(dh.gather_array(c.name)) plt.phase_plot(dh.gather_array(c.name))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
neumann_angles_from_surface_tensions(lambda i, j: float(σ[i, j])) neumann_angles_from_surface_tensions(lambda i, j: float(σ[i, j]))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import time import time
for i in range(10): for i in range(10):
start = time.perf_counter() start = time.perf_counter()
time_loop(1_000) time_loop(1_000)
end = time.perf_counter() end = time.perf_counter()
try: try:
print(i, end - start, liquid_lens_neumann_angles(dh.gather_array(c.name))) print(i, end - start, liquid_lens_neumann_angles(dh.gather_array(c.name)))
except Exception: except Exception:
print(i, end - start, "none found") print(i, end - start, "none found")
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
plt.subplot(1,3,1) plt.subplot(1,3,1)
t = dh.gather_array(c.name, make_slice[25, :]).squeeze() t = dh.gather_array(c.name, make_slice[25, :]).squeeze()
plt.plot(t); plt.plot(t);
plt.subplot(1,3,2) plt.subplot(1,3,2)
plt.phase_plot(dh.gather_array(c.name), linewidth=1) plt.phase_plot(dh.gather_array(c.name), linewidth=1)
plt.subplot(1,3,3) plt.subplot(1,3,3)
plt.scalar_field(dh.gather_array(μ.name)[:, :, 2]) plt.scalar_field(dh.gather_array(μ.name)[:, :, 2])
plt.colorbar(); plt.colorbar();
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
assert not np.isnan(dh.max(c.name)) assert not np.isnan(dh.max(c.name))
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
t = dh.gather_array(c.name, make_slice[25, 55:90]).squeeze() t = dh.gather_array(c.name, make_slice[25, 55:90]).squeeze()
plt.hlines(0.5, 0, 30) plt.hlines(0.5, 0, 30)
plt.plot(t); plt.plot(t);
``` ```
......
...@@ -33,7 +33,13 @@ def _skip_instruction_sets_windows(instruction_sets): ...@@ -33,7 +33,13 @@ def _skip_instruction_sets_windows(instruction_sets):
def single_component_maxwell(x1, x2, kT, mass): def single_component_maxwell(x1, x2, kT, mass):
"""Integrate the probability density from x1 to x2 using the trapezoidal rule""" """Integrate the probability density from x1 to x2 using the trapezoidal rule"""
x = np.linspace(x1, x2, 1000) x = np.linspace(x1, x2, 1000)
return np.trapz(np.exp(-mass * x ** 2 / (2. * kT)), x) / np.sqrt(2. * np.pi * kT / mass)
try:
trapezoid = np.trapezoid # since numpy 2.0
except AttributeError:
trapezoid = np.trapz
return trapezoid(np.exp(-mass * x ** 2 / (2. * kT)), x) / np.sqrt(2. * np.pi * kT / mass)
def rr_getter(moment_group): def rr_getter(moment_group):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment