tum-pbs / PhiFlow

A differentiable PDE solving framework for machine learning
MIT License
1.39k stars 189 forks source link

#enhencement - Include obstacles boundary conditions in implicit methods #128

Closed rcremese closed 1 year ago

rcremese commented 1 year ago

Hello, This is just a feature request to know if a function for applying obstacle boundary conditions when using diffuse.implicit method exists. If not, is it something that will be implemented in the future ?

For now, the only way I see to solve diffusion equation in an environment with rectangular obstacles is to use explicit scheme, a binary mask for the obstacles and multiplying both at each time step. Is there already an other way ?

Thank you.

holl- commented 1 year ago

Hi, This can be done the same way as with fluid.masked_laplace().

Here is some example code that only applies diffusion outside the obstacle.

from phi.flow import *

@math.jit_compile_linear
def explicit_masked_diffusion(u: Grid, dt, diffusivity, obstacle_mask):
    diffused = u + dt * diffusivity * field.laplace(u)
    return field.where(obstacle_mask, u, diffused)

def implicit_masked_diffusion(u: Grid, dt, diffusivity, obstacle_mask):
    return math.solve_linear(explicit_masked_diffusion, u, Solve(x0=u), dt=-dt, diffusivity=diffusivity, obstacle_mask=obstacle_mask)

u = CenteredGrid(Noise(smoothness=1.), x=100, y=100)
mask = resample(Box(x=(0, 80), y=(40, 90)), u)
u_diff = implicit_masked_diffusion(u, 1., 5., mask)
show(u_diff)

Hope that helps!

rcremese commented 1 year ago

Thanks a lot, That's exactly what I was looking for.