torchphysics.problem.spaces package

Contains the Spaces and Points classes.

Spaces

It’s purpose is to define variable names and dimensions that can be used in functions, domains, models and more.

Points

The Points object is a central part of TorchPhysics. They consit of a PyTorch-Tensor and a space. Points store data in a tensor with 2-axis, the first corresponding the batch-dimension in a batch of multiple points. The second axis collects the space dimensionalities.

Submodules

torchphysics.problem.spaces.functionspace module

class torchphysics.problem.spaces.functionspace.FunctionSpace(input_domain, output_space)[source]

Bases: object

A FunctionSpace collects functions that map from a specific input domain to a previously defined output space.

Parameters:
  • input_domain (torchphysics.Domain) – The input domain of the functions in this function space.

  • output_space (torchphysics.Space) – The space of the image of the functions in this function space.

torchphysics.problem.spaces.points module

Contains a class that handles the storage of all created data points.

class torchphysics.problem.spaces.points.Points(data, space, **kwargs)[source]

Bases: object

A set of points in a space, stored as a torch.Tensor. Can contain multiple axis which keep batch-dimensions.

Parameters:
  • data (torch.tensor, np.array or list) – The data points that should be stored. Have to be of the shape (batch_length, space.dimension).

  • space (torchphysics.spaces.Space) – The space to which these points belongs to.

Notes

This class is essentially a combination of a torch.Tensor and a dictionary. So all data points can be stored as a single tensor, where we efficently can access and transform the data. But at the same time have the knowledge of what points belong to which space/variable.

__add__(other)[source]

Adds the data of two Points, have to lay in the same space.

__eq__(other)[source]

Compares two Points if they are equal.

__getitem__(val)[source]

Supports usual slice operations like points[1:3,(‘x’,’t’)]. Returns a new, sliced, points object.

__iter__()[source]

Iterates through first batch-dim. It is in general not recommended to use this operation because it may lead to huge (and therefore slow) loops.

__len__()[source]

Returns the number of points in this object.

__mul__(other)[source]

Pointwise multiplies the data of two Points, have to lay in the same space.

__or__(other)[source]

Appends the data points of the second Points behind the data of the first Points in the first batch-dim. (torch.cat((data_1, data_2), dim=0))

__pow__(other)[source]

Pointwise raises the data of the first Points object to the power of the second one.

__sub__(other)[source]

Substracts the data of two Points, have to lay in the same space.

__truediv__(other)[source]

Pointwise divides the data of two Points, have to lay in the same space.

property as_tensor

Returns the underlying tensor.

property coordinates

Returns a dict containing the coordinates of all points for each variable, e.g. {‘x’: torch.Tensor, ‘t’: torch.Tensor}

cuda(*args, **kwargs)[source]
property device

Returns the device of the underlying tensor.

property dim

Returns the dimension of the points.

classmethod empty(**kwargs)[source]

Creates an empty Points object.

Returns:

The empty Points-object.

Return type:

Points

classmethod from_coordinates(coords)[source]

Concatenates sample coordinates from a dict to create a point object.

Parameters:

coords (dict) – The dictionary containing the data for every variable.

Returns:

points – the created points object.

Return type:

Points

property isempty

Checks whether no points and no structure are saved in this object.

join(other)[source]

Stacks the data points of the second Point behind the data of the first Point. (torch.cat((data_1, data_2), dim=-1))

classmethod joined(*points_l)[source]

Concatenates different Points to one single Points-Object. Will we use torch.cat on the data of the different Points and create the product space of the Points spaces.

Parameters:

*points_l – The different Points that should be connected.

Returns:

the created Points object.

Return type:

Points

repeat(*n)[source]

Repeats this points data along the first batch-dimension. Uses torch.repeat and will therefore repeat the data ‘batchwise’.

Parameters:

n – The number of repeats.

property requires_grad

Returns the ‘.requires_grad’ property of the underlying Tensor.

property shape

The shape of the batch-dimensions of this points object.

to(*args, **kwargs)[source]

Moves the underlying Tensor to other hardware parts.

track_coord_gradients()[source]
unsqueeze(dim)[source]

Adds an additional dimension inside the batch dimensions.

Parameters:

dim – Where to add the additional axis (considered only inside batch dimensions).

property variables

Returns variables of the points as an unordered set, e.g {‘x’, ‘t’}.

torchphysics.problem.spaces.space module

class torchphysics.problem.spaces.space.R1(variable_name)[source]

Bases: Space

The space for one dimensional real numbers.

Parameters:

variable_name (str) – The name of the variable that belongs to this space.

class torchphysics.problem.spaces.space.R2(variable_name)[source]

Bases: Space

The space for two dimensional real numbers.

Parameters:

variable_name (str) – The name of the variable that belongs to this space.

class torchphysics.problem.spaces.space.R3(variable_name)[source]

Bases: Space

The space for three dimensional real numbers.

Parameters:

variable_name (str) – The name of the variable that belongs to this space.

class torchphysics.problem.spaces.space.Rn(variable_name, n: int)[source]

Bases: Space

The space for n dimensional real numbers.

Parameters:
  • variable_name (str) – The name of the variable that belongs to this space.

  • n (int) – The dimension of this space.

class torchphysics.problem.spaces.space.Space(variables_dims)[source]

Bases: Counter, OrderedDict

A Space defines (and assigns) the dimensions of the variables that appear in the differentialequation. This class sholud not be instanced directly, rather the corresponding child classes.

Parameters:

variables_dims (dict) – A dictionary containing the name of the variables and the dimension of the respective variable.

__contains__(space)[source]

Checks if the variables of the other space are contained in this space.

Parameters:

space (torchphysics.spaces.Space) – The other Space that should be checked if this is included.

__eq__(o: object) bool[source]

True if all counts agree. Missing counts are treated as zero.

__getitem__(val)[source]

Returns a part of the Space dicitionary, specified in the input. Mathematically, this constructs a subspace.

Parameters:

val (str, slice, list or tuple) – The keys that correspond to the variables that should be used in the subspace.

__mul__(other)[source]

Creates the product space of the two input spaces. Allows the construction of higher dimensional spaces with ‘mixed’ variable names. E.g R1(‘x’)*R1(‘y’) is a two dimensional space where one axis is ‘x’ and the other stands for ‘y’.

check_values_in_space(values)[source]

Checks if a given tensor is valid to belong to this space.

Parameters:

values (torch.tensor) – A tensor of values that should be checked. Generally the last dimension of the tensor has to fit the dimension of this space.

Returns:

In the case, that the values have not the corrected shape, but can be reshaped, thet reshaped values are returned. This is used in the matrix-space.

Return type:

torch.tensor

property dim

Returns the dimension of the space (sum of factor spaces)

property variables

A unordered (!) set of variables.