tum-pbs / PhiFlow

A differentiable PDE solving framework for machine learning
MIT License
1.43k stars 193 forks source link

SampledField.with_values does not copy all dimensions properly #105

Closed Dxyk closed 1 year ago

Dxyk commented 1 year ago

Hello,

I want to create a 2D staggered grid that contains not only the spatial dimensions, but also the temporal dimension throughout the simulation. Here's how I initialized the field, and everything seems to work correctly so far. I'm not sure if this is the optimal way to do so, so please let me know if there is a better way.

velocity_single = StaggeredGrid(0, extrapolation=extrapolation.ZERO, x=32, y=32, bounds=Box(x=32, y=32))
velocity_temporal = field.stack([Noise() @ velocity_single] * 10, dim=channel("temporal"))
print(velocity_temporal)
# StaggeredGrid[(xˢ=32, yˢ=32, vectorᶜ=x,y, temporalᶜ=10), size=(x=32, y=32), extrapolation=0]

Now I'd like to create another temporal staggered grid base on the dimension of the one I just created, but initialize all the values to 0. I attempted to use SampledField.with_values to do so, but it seems like the temporal channel dimension was not copied over.

velocity_temporal_copy = velocity_temporal.with_values(0)
print(velocity_temporal_copy)
# StaggeredGrid[(xˢ=32, yˢ=32, vectorᶜ=x,y), size=(x=32, y=32), extrapolation=0]

I'm not quite sure if this is a bug or is this the intended behaviour?

Thanks!

holl- commented 1 year ago

This is intended behavior. with_values() completely replaces the values. This removes all dimensions that were only part of the old values and are not integral to the field. If you just want to get a copy with all values being zero, you could write

velocity_temporal_copy = velocity_temporal * 0
Dxyk commented 1 year ago

I see. Thanks for the swift reply!