# %pip install --quiet phiflow
from phi.torch.flow import *
# from phi.flow import * # If JAX is not installed. You can use phi.torch or phi.tf as well.
from tqdm.notebook import trange
mesh = geom.load_gmsh('cylinder.msh', ('y-', 'x+', 'y+', 'x-', 'cyl+', 'cyl-'))
plot(Box(x=6, y=6), mesh, overlay='args', size=(4, 3), title='cylinder.msh')
<Figure size 288x216 with 1 Axes>
@jit_compile_linear
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
@jit_compile
def implicit_time_step(v, dt):
v = math.solve_linear(momentum_eq, v, Solve(x0=v), u_prev=v, dt=-dt)
v, p = fluid.make_incompressible(v, (), Solve('scipy-direct'))
return v
boundary = {'x-': vec(x=1, y=0), 'x+': ZERO_GRADIENT, 'y': 0, 'cyl': 0}
velocity = Field(mesh, tensor(vec(x=0, y=0)), boundary)
v_trj = math.iterate(implicit_time_step, batch(time=100), velocity, dt=0.001, range=trange)
0%| | 0/100 [00:00<?, ?it/s]
plot(v_trj * .1, v_trj.to_grid(), animate='time')