tum-pbs / PhiFlow

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

3D obstacles not implemented #131

Closed KarlisFre closed 1 year ago

KarlisFre commented 1 year ago

Hi, I am considering implementing a 3D scenario involving moving obstacles. Currently the code crashes with the message that the code is not yet implemented. Can you please implement the missing parts?

from phi.flow import *  # minimal dependencies

object_geometry = Box(x=(40,60), y=(40, 60), z=(40, 60))
OBSTACLE = Obstacle(object_geometry, velocity=(0.,1.,0.))

velocity = StaggeredGrid((0, 0, 0), 0, x=32, y=32, z=32, bounds=Box(x=100, y=100, z=100))  # or CenteredGrid(...)
smoke = CenteredGrid(0, ZERO_GRADIENT, x=32, y=32, z=32, bounds=Box(x=100, y=100, z=100))
INFLOW = 0.2 * resample(Sphere(x=50, y=50, z=10, radius=5), to=smoke, soft=True)
pressure = None

#@jit_compile  # Only for PyTorch, TensorFlow and Jax
def step(v, s, p, dt=1.):
    s = advect.mac_cormack(s, v, dt) + INFLOW
    buoyancy = resample(s * (0, 0, 0.1), to=v)
    v = advect.semi_lagrangian(v, v, dt) + buoyancy * dt
    v, p = fluid.make_incompressible(v, (OBSTACLE), Solve('auto', 1e-5, x0=p))
    return v, s, p

for _ in view(smoke, velocity, 'pressure', play=True, namespace=globals()).range(warmup=1):
    velocity, smoke, pressure = step(velocity, smoke, pressure)

File "C:\Users\karlis\Data\EDI\flow-control\smoke_plume3d.py", line 17, in step v, p = fluid.make_incompressible(v, (OBSTACLE), Solve('auto', 1e-5, x0=p)) File "C:\Users\karlis\anaconda3\lib\site-packages\phi\physics\fluid.py", line 105, in make_incompressible velocity = apply_boundary_conditions(velocity, obstacles) File "C:\Users\karlis\anaconda3\lib\site-packages\phi\physics\fluid.py", line 183, in apply_boundary_conditions angular_velocity = AngularVelocity(location=obstacle.geometry.center, strength=obstacle.angular_velocity, falloff=None) @ velocity File "C:\Users\karlis\anaconda3\lib\site-packages\phi\field_field.py", line 78, in matmul return self.at(other, keep_extrapolation=False) File "C:\Users\karlis\anaconda3\lib\site-packages\phi\field_field.py", line 73, in at return resample(self, representation, keep_extrapolation, kwargs) File "C:\Users\karlis\anaconda3\lib\site-packages\phi\field_field.py", line 445, in resample resampled = reduce_sample(value, to.elements, kwargs) File "C:\Users\karlis\anaconda3\lib\site-packages\phi\field_field.py", line 396, in reduce_sample sampled = [c._sample(p, kwargs) for c, p in zip(components, geometry.unstack(geom_ch.name))] File "C:\Users\karlis\anaconda3\lib\site-packages\phi\field_field.py", line 396, in sampled = [c._sample(p, kwargs) for c, p in zip(components, geometry.unstack(geom_ch.name))] File "C:\Users\karlis\anaconda3\lib\site-packages\phi\field_angular_velocity.py", line 42, in _sample velocity = math.cross_product(strength, distances) File "C:\Users\karlis\anaconda3\lib\site-packages\phi\math_nd.py", line 141, in cross_product raise NotImplementedError(f'spatial_rank={spatial_rank} not yet implemented') NotImplementedError: spatial_rank=3 not yet implemented

holl- commented 1 year ago

Hi, I pushed a fix to 2.4-develop, https://github.com/tum-pbs/PhiFlow/commit/103e8be441a7fdb876973aefeb86c8cca09548ff .

pip uninstall phiflow
pip install git+https://github.com/tum-pbs/PhiFlow@2.4-develop