torchphysics.problem.domains package

Domains handle the geometries of the underlying problems. Every input variable, that appears in the differentialequation has to get a domain, to which it belongs. Different 0D, 1D, 2D and 3D domains are pre implemented. For more complex domains four operations are implemented:

  • Union \(A \cup B\), implemented with: +

  • Intersection \(A \cap B\), implemented with: &

  • Cut \(A \setminus B\), implemented with: -

  • Cartesian product \(A \times B\), implemented with: *

It is possible to pass in functions as parameters of most domains. This leads to geometries that can change depending on other variables, e.g. a moving domain in time.

Boolean operations together with cartesian products and parameter-dependencies form an easy-to-use toolbox enable the user to define a large set of relevant domains. In addition, domains can be imported from shapely or .stl-files (through trimesh).

If you want to solve an inverse problem, the learnable parameters do not get a domain! They have to be defined with the torchphysics.model.Parameters class.

Subpackages

Submodules

torchphysics.problem.domains.domain module

class torchphysics.problem.domains.domain.BoundaryDomain(domain)[source]

Bases: Domain

The parent class for all built-in boundaries. Can be used just like the main Domain class.

Parameters:

domain (Domain) – The domain of which this object is the boundary.

__call__(**data)[source]

Evaluates the domain at the given data.

bounding_box(params=Points: {}, device='cpu')[source]

Computes the bounds of the domain.

Returns:

A torch.Tensor with the length of 2*self.dim. It has the form [axis_1_min, axis_1_max, axis_2_min, axis_2_max, …], where min and max are the minimum and maximum value that the domain reaches in each dimension-axis.

Return type:

tensor

abstract normal(points, params=Points: {}, device='cpu')[source]

Computes the normal vector at each point in points.

Parameters:
  • points (torch.tensor or torchphysics.problem.Points) – Different points for which the normal vector should be computed. The points should lay on the boundary of the domain, to get correct results. E.g in 2D: points = Points(torch.tensor([[2, 4], [9, 6], ….]), R2(…))

  • params (dict or torchphysics.problem.Points, optional) – Additional parameters that are maybe needed to evaluate the domain.

  • device (str, optional) – The device on which the points should be created. Default is ‘cpu’.

Returns:

The tensor is of the shape (len(points), self.dim) and contains the normal vector at each entry from points.

Return type:

torch.tensor

class torchphysics.problem.domains.domain.Domain(space, dim=None)[source]

Bases: object

The parent class for all built-in domains.

Parameters:
  • space (torchphysiscs.spaces.Space) – The space in which this object lays.

  • dim (int, optional) – The dimension of this domain. (if not specified, implicit given through the space)

__add__(other)[source]

Creates the union of the two input domains.

Parameters:

other (Domain) – The other domain that should be united with the domain. Has to be of the same dimension.

__and__(other)[source]

Creates the intersection of the two input domains.

Parameters:

other (Domain) – The other domain that should be intersected with the domain. Has to lie in the same space.

__call__(**data)[source]

Evaluates the domain at the given data.

__contains__(points)[source]

Checks for every point in points if it lays inside the domain.

Parameters:

points (torchphysics.problem.Points) – A Points object that should be checked.

Returns:

A boolean Tensor of the shape (len(points), 1) where every entry contains true if the point was inside or false if not.

Return type:

torch.Tensor

__mul__(other)[source]

Creates the cartesian product of this domain and another domain.

Parameters:

other (Domain) – The other domain to create the cartesian product with. Should lie in a disjoint space.

__sub__(other)[source]

Creates the cut of domain other from self.

Parameters:

other (Domain) – The other domain that should be cut off the domain. Has to be of the same dimension.

property boundary

Returns the boundary of this domain. Does not work on boundaries itself, e.g. Circle.boundary.boundary throws an error.

Returns:

boundary – The boundary-object of the domain.

Return type:

torchphysics.domains.Boundarydomain

abstract bounding_box(params=Points: {}, device='cpu')[source]

Computes the bounds of the domain.

Returns:

A torch.Tensor with the length of 2*self.dim. It has the form [axis_1_min, axis_1_max, axis_2_min, axis_2_max, …], where min and max are the minimum and maximum value that the domain reaches in each dimension-axis.

Return type:

tensor

compute_n_from_density(d, params)[source]

Transforms a given point density to a number of points, since all methods from PyTorch only work with a given number.

len_of_params(params)[source]

Finds the number of params, for which points should be sampled.

abstract sample_grid(n=None, d=None, params=Points: {}, device='cpu')[source]

Creates an equdistant grid in the domain.

Parameters:
  • n (int, optional) – The number of points that should be created.

  • d (float, optional) – The density of points that should be created, if n is not defined.

  • params (torchphysics.problem.Points, optional) – Additional paramters that are maybe needed to evaluate the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points object containing the sampled points.

Return type:

Points

abstract sample_random_uniform(n=None, d=None, params=Points: {}, device='cpu')[source]

Creates random uniformly distributed points in the domain.

Parameters:
  • n (int, optional) – The number of points that should be created.

  • d (float, optional) – The density of points that should be created, if n is not defined.

  • params (torchphysics.problem.Points, optional) – Additional paramters that are maybe needed to evaluate the domain.

  • device (str) – The device on which the points should be created. Default is ‘cpu’.

Returns:

A Points object containing the sampled points.

Return type:

Points

set_necessary_variables(*domain_params)[source]

Registers the variables/spaces that this domain needs to be properly defined

set_volume(volume)[source]

Set the volume of the given domain.

Parameters:

volume (number or callable) – The volume of the domain. Can be a function if the volume changes depending on other variables.

Notes

For all basic domains the volume (and surface) are implemented. But if the given domain has a complex shape or is dependent on other variables, the volume can only be approixmated. Therefore one can set here a exact expression for the volume, if known.

transform_to_user_functions(*domain_params)[source]

Transforms all parameters that define a given domain to a UserFunction. This enables that the domain can dependt on other variables.

Parameters:

*domain_params (callables, lists, arrays, tensors or numbers) – The parameters that define a domain.

volume(params=Points: {}, device='cpu')[source]

Computes the volume of the current domain.

Parameters:

params (torchphysics.problem.Points, optional) – Additional paramters that are needed to evaluate the domain.

Returns:

volume – Returns the volume of the domain. If dependent on other parameters, the value will be returned as tensor with the shape (len(params), 1). Where each row corresponds to the volume of the given values in the params row.

Return type:

torch.tensor