tum-pbs / PhiFlow

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

Pointcloud from (native) array #111

Closed FabricioArendTorres closed 1 year ago

FabricioArendTorres commented 1 year ago

Hi,

if I have a numpy / torch / tensorflow array that contains n points with dimension dim in the shape of (n, dim), how would I initialize a PointCloud with that? The docs and tutorials are not really helpful for that, but only describe how to generate point clouds directly from phiflow Tensors.

FabricioArendTorres commented 1 year ago

Ok, I think I figured it out, but I have to admit the named dimensions are a bit weird (and include redundant information?):

from phi.torch.flow import *
import numpy as np

data_x = np.random.rand(10, 2)
density = np.exp(-(data_x**2).sum(-1)).reshape(10, 1)
points = math.tensor(data_x, instance(points=data_x.shape[0]), channel(vector='x,y'))
vals = math.tensor(density, instance(points=data_x.shape[0]), channel(value='density'))
pc = PointCloud(elements=points, values=vals, )
plot(pc)
holl- commented 1 year ago

You can actually leave out the redundant sizes:

data_x = np.random.rand(10, 2)
density = np.exp(-(data_x**2).sum(-1))

points = tensor(data_x, instance('points'), channel(vector='x,y'))
vals = tensor(density, instance(points))
pc = PointCloud(elements=points, values=vals)
plot(pc)

But yeah, converting native arrays adds a bit of declaration to your code. However, everything you do with it afterwards should be shorter than the corresponding NumPy/TF/PyTorch/Jax code.