Note
Go to the end to download the full example code.
phlower is a deep learning framework based on PyTorch Ignite especially for physical phenomenon such as fluid dynamics.
Phlower Tensor Operation with physical dimension¶
Usually, physics values have physics dimensions. Phlower can consider it.
Let’s check what kind of items are treated as physics dimension There are many possible choices of base physical dimensions. Following to the SI standard, physical dimensions and corresponding dimension symbols are shown blelow.
time (T), length (L), mass (M), electric current (I), absolute temperature (Θ), amount of substance (N) and luminous intensity (J).
from phlower.utils.enums import PhysicalDimensionSymbolType
print(f"{[str(v) for v in PhysicalDimensionSymbolType]}")
['T (time)', 'L (length)', 'M (mass)', 'I (electric current)', 'Theta (thermodynamic temperature)', 'N (amount of substance)', 'J (luminous intensity)']
Create PhlowerTensor which is tensor object with physical dimension. There are several way to create PhlowerTensor, the most simple way is shown. PhlowerTensor is a wrapper of torch.Tensor. Call print to check values and dimensions.
import torch
from phlower import phlower_tensor
sample_velocity = torch.rand(3, 4)
dimension = {"L": 1, "T": -1}
physical_tensor = phlower_tensor(sample_velocity, dimension=dimension)
print(physical_tensor)
PhlowerTensor(tensor([[0.4986, 0.1072, 0.9929, 0.5529],
[0.4707, 0.4580, 0.5383, 0.8241],
[0.8728, 0.1017, 0.3627, 0.2145]]), Dimension: PhlowerDimensionTensor(T: -1.0, L: 1.0, M: 0.0, I: 0.0, Theta: 0.0, N: 0.0, J: 0.0)), is_time_series: False, is_voxel: False
Let’s calculate square of velocity. You can find that physical dimension is also converted to new dimension.
square_velocity = torch.pow(physical_tensor, 2)
print(square_velocity)
PhlowerTensor(tensor([[0.2486, 0.0115, 0.9858, 0.3058],
[0.2216, 0.2098, 0.2898, 0.6791],
[0.7617, 0.0104, 0.1315, 0.0460]]), Dimension: PhlowerDimensionTensor(T: -2.0, L: 2.0, M: 0.0, I: 0.0, Theta: 0.0, N: 0.0, J: 0.0)), is_time_series: False, is_voxel: False
Create PhlowerTensor without physical dimension if you do not pass information of dimension to phlower_tensor. Raise error when it comes to calculate PhlowerTensor with a physical dimension and that without it.
try:
one_physical_tensor = phlower_tensor(
torch.rand(3, 4), dimension={"L": 1, "T": -1}
)
another_physical_tensor = phlower_tensor(torch.rand(3, 4))
_ = another_physical_tensor + one_physical_tensor
except Exception as ex:
print(ex)
Cannnot calcualte PhlowerTensor with physics dimension and PhlowerTensor without it. PhlowerTensor(tensor([[0.8414, 0.1556, 0.0756, 0.5658],
[0.7468, 0.6869, 0.0714, 0.8978],
[0.3873, 0.5436, 0.0325, 0.4023]]), Dimension: None), is_time_series: False, is_voxel: False
Some calculation operations are not allowed when physics dimension value is not the same,
try:
one_physical_tensor = phlower_tensor(
torch.rand(3, 4), dimension={"L": 1, "T": -1}
)
another_physical_tensor = phlower_tensor(
torch.rand(3, 4), dimension={"L": 1}
)
_ = another_physical_tensor + one_physical_tensor
except Exception as ex:
print(ex)
Add operation for different physical dimensions is not allowed.
Some calculation operations are allowed even when physics dimension value is not the same,
one_physical_tensor = phlower_tensor(
torch.rand(3, 4), dimension={"L": 1, "T": -1}
)
another_physical_tensor = phlower_tensor(
torch.rand(3, 4), dimension={"Theta": 1}
)
print(another_physical_tensor * one_physical_tensor)
PhlowerTensor(tensor([[0.0245, 0.4689, 0.2952, 0.2262],
[0.0606, 0.7343, 0.3823, 0.0156],
[0.5988, 0.0511, 0.2306, 0.0387]]), Dimension: PhlowerDimensionTensor(T: -1.0, L: 1.0, M: 0.0, I: 0.0, Theta: 1.0, N: 0.0, J: 0.0)), is_time_series: False, is_voxel: False
Total running time of the script: (0 minutes 0.017 seconds)