From eaa9f814eb2f3e7479bcb9605b709372d4b9601c Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Thu, 18 Jul 2024 00:06:54 +0200 Subject: [PATCH 1/8] [doc] Update Smoke_Plume.ipynb --- examples/grids/Smoke_Plume.ipynb | 6463 ++++++++++++++++++++++++++---- 1 file changed, 5706 insertions(+), 757 deletions(-) diff --git a/examples/grids/Smoke_Plume.ipynb b/examples/grids/Smoke_Plume.ipynb index 947eaca2e..115e62305 100644 --- a/examples/grids/Smoke_Plume.ipynb +++ b/examples/grids/Smoke_Plume.ipynb @@ -13,12 +13,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%pip install phiflow\n", "from phi.jax.flow import *\n", + "from tqdm.notebook import trange\n", "# from phi.flow import * # If JAX is not installed. You can use phi.torch or phi.tf as well." ] }, @@ -31,7 +32,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -50,33 +51,56 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "@jit_compile\n", - "def step(v, s, p, dt=1.):\n", + "def step(v, s, p, dt):\n", " s = advect.mac_cormack(s, v, dt) + inflow_rate * resample(inflow, to=s, soft=True)\n", " buoyancy = resample(s * (0, 0.1), to=v)\n", " v = advect.semi_lagrangian(v, v, dt) + buoyancy * dt\n", - " v, p = fluid.make_incompressible(v, (), Solve('CG', 1e-3, x0=p, preconditioner='auto'))\n", + " v, p = fluid.make_incompressible(v, (), Solve('CG', 1e-3, x0=p))\n", " return v, s, p\n", "\n", "v0 = StaggeredGrid(0, 0, domain, x=64, y=64)\n", - "smoke0 = CenteredGrid(0, ZERO_GRADIENT, domain, x=200, y=200)\n", - "v_trj, s_trj, p_trj = iterate(step, batch(time=100), v0, smoke0, None)\n" + "smoke0 = CenteredGrid(0, ZERO_GRADIENT, domain, x=200, y=200)" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "760ba9ee88c243f38fc682cb5db7bb73", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + " 0%| | 0/300 [00:00\n", - " \n", " Your browser does not support the video tag.\n", "" ], "text/plain": [ - "" + "" ] }, - "execution_count": 13, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "plot(s_trj, animate='time')" + "plot(s_trj, animate='time', frame_time=80)" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { + "kernelspec": { + "display_name": "phiflow2", + "language": "python", + "name": "python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" } }, "nbformat": 4, From e91330a4cc9df3a6d49b16b2bb789d14bf525e28 Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Sun, 28 Jul 2024 22:00:06 +0200 Subject: [PATCH 2/8] [geom] Fix Mesh.__variable_attrs__() --- phi/geom/_mesh.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phi/geom/_mesh.py b/phi/geom/_mesh.py index 4e235ffcd..7de88023b 100644 --- a/phi/geom/_mesh.py +++ b/phi/geom/_mesh.py @@ -68,7 +68,7 @@ def __init__(self, self._max_cell_walk = max_cell_walk def __variable_attrs__(self): - return '_vertices', '_elements', '_vertex_count', '_center', '_volume', '_face_centers', '_face_normals', '_face_areas', '_face_vertices', '_relative_face_distance', '_neighbor_offsets' + return '_vertices', '_elements', '_center', '_volume', '_face_centers', '_face_normals', '_face_areas', '_face_vertices', '_relative_face_distance', '_neighbor_offsets' def __value_attrs__(self): return '_vertices', From 08c86351603e92fca60f2366d7e888180e85841f Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Sat, 3 Aug 2024 19:44:04 +0200 Subject: [PATCH 3/8] [physics] Fix make_incompressible() for FVM --- phi/physics/fluid.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phi/physics/fluid.py b/phi/physics/fluid.py index 43b316a03..db554e15f 100644 --- a/phi/physics/fluid.py +++ b/phi/physics/fluid.py @@ -172,7 +172,7 @@ def make_incompressible(velocity: Field, wide_stencil = not velocity.is_staggered pressure = math.solve_linear(masked_laplace, div, solve, velocity.boundary, hard_bcs, active, wide_stencil=wide_stencil, order=order, implicit=None, upwind=None, correct_skew=correct_skew) # --- Subtract grad p --- - grad_pressure = field.spatial_gradient(pressure, input_velocity.extrapolation, at=velocity.sampled_at, order=order) + grad_pressure = field.spatial_gradient(pressure, input_velocity.extrapolation, at=velocity.sampled_at, order=order, scheme='green-gauss') if hard_bcs is not None: grad_pressure *= hard_bcs velocity = (velocity - grad_pressure).with_extrapolation(input_velocity.extrapolation) From 6faa4c6815efb1cc7dbe8531d4dc05a068c95129 Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Sat, 3 Aug 2024 19:44:13 +0200 Subject: [PATCH 4/8] [tests] Add FVM test --- tests/commit/physics/test_fvm.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/commit/physics/test_fvm.py diff --git a/tests/commit/physics/test_fvm.py b/tests/commit/physics/test_fvm.py new file mode 100644 index 000000000..c99c9187e --- /dev/null +++ b/tests/commit/physics/test_fvm.py @@ -0,0 +1,25 @@ +from unittest import TestCase + +from phi import math +from phi.field import Field +from phi.geom import mesh_from_numpy +from phi.physics import diffuse, advect +from phiml.math import spatial, vec, tensor +from phiml.math.extrapolation import ZERO_GRADIENT + + +class TestSPH(TestCase): + + def test_matrix_adv_diff(self): + points = [(0, 0), (0, 1), (1, 1), (1, 0)] + mesh = mesh_from_numpy(points, [(0, 1, 2), (0, 2, 3)], {'x': [(1, 2), (3, 0)], 'y': [(0, 1), (2, 3)]}) + def momentum_eq(u, u_prev, dt, diffusivity=0.01): + diffusion_term = dt * diffuse.differential(u, diffusivity, correct_skew=False) + advection_term = dt * advect.differential(u, u_prev, order=1) + return u + advection_term + diffusion_term + velocity = Field(mesh, tensor(vec(x=1, y=0)), {'x': vec(x=.1, y=0), 'y': ZERO_GRADIENT}) + A, b = math.matrix_from_function(momentum_eq, velocity, velocity, 0.01) + r_lin = A @ velocity.values + b + r_call = momentum_eq(velocity, velocity, 0.01) + math.assert_close(r_lin, r_call.values) + From 1862579577497bdaa8df9c4cb39e20c9b3e10ead Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Sat, 3 Aug 2024 19:47:50 +0200 Subject: [PATCH 5/8] =?UTF-8?q?[=CE=A6]=20Update=20PhiML=20requirement=20t?= =?UTF-8?q?o=201.7.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 97800a877..838ac5d54 100644 --- a/setup.py +++ b/setup.py @@ -40,7 +40,7 @@ url='https://github.com/tum-pbs/PhiFlow', include_package_data=True, install_requires=[ - 'phiml==1.7.0.post1', + 'phiml>=1.7.1', 'matplotlib>=3.5.0', # also required by dash for color maps 'packaging', ], From f25f90f8e4ec9034e996e433c64481e48a6579d2 Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Fri, 9 Aug 2024 20:08:26 +0200 Subject: [PATCH 6/8] [field] Fix Field.__eq__ --- phi/field/_field.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/phi/field/_field.py b/phi/field/_field.py index 161426e2b..1fbff483c 100644 --- a/phi/field/_field.py +++ b/phi/field/_field.py @@ -698,17 +698,10 @@ def __replace_dims__(self, dims: Tuple[str, ...], new_dims: Shape, **kwargs) -> def __eq__(self, other): if not isinstance(other, Field): return False - # Check everything but __variable_attrs__ (values): elements type, extrapolation, add_overlapping - if type(self._geometry) is not type(other._geometry): + if self._geometry != other._geometry: return False if self._boundary != other.boundary: return False - if self._values is None: - return other._values is None - if other._values is None: - return False - if self._values.shape == other._values.shape: - return False return math.always_close(self._values, other._values) def __hash__(self): From bfeaa1d4289ed1962afd2cc4e11290b09eb39cc5 Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Fri, 9 Aug 2024 22:32:15 +0200 Subject: [PATCH 7/8] [field] Fix Field.shape --- phi/field/_field.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phi/field/_field.py b/phi/field/_field.py index 1fbff483c..67dffdc0d 100644 --- a/phi/field/_field.py +++ b/phi/field/_field.py @@ -207,7 +207,7 @@ def shape(self) -> Shape: """ if self.is_staggered and self.is_grid: return batch(self._geometry) & self.resolution & non_dual(self._values).without(self.resolution) & self._geometry.shape['vector'] - return self._geometry.shape.non_channel & self._values + return self._geometry.shape.without('vector') & self._values @property def resolution(self): From 60046dbeda1f4f159c5118de9db24eba5d62c1b7 Mon Sep 17 00:00:00 2001 From: Philipp Holl Date: Fri, 9 Aug 2024 22:32:28 +0200 Subject: [PATCH 8/8] =?UTF-8?q?[=CE=A6]=20Bump=20version=20to=203.0.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phi/VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phi/VERSION b/phi/VERSION index 56fea8a08..13d683ccb 100644 --- a/phi/VERSION +++ b/phi/VERSION @@ -1 +1 @@ -3.0.0 \ No newline at end of file +3.0.1 \ No newline at end of file