torchphysics.problem.domains.domainoperations namespace

Submodules

torchphysics.problem.domains.domainoperations.cut module

class torchphysics.problem.domains.domainoperations.cut.CutBoundaryDomain(domain: CutDomain)[source]

Bases: BoundaryDomain

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

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

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

class torchphysics.problem.domains.domainoperations.cut.CutDomain(domain_a: Domain, domain_b: Domain, contained=False)[source]

Bases: Domain

Implements the logical cut of two domains.

Parameters:
  • domain_a (Domain) – The first domain.

  • domain_b (Domain) – The second domain.

__call__(**data)[source]

Evaluates the domain at the given data.

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

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

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

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

torchphysics.problem.domains.domainoperations.intersection module

class torchphysics.problem.domains.domainoperations.intersection.IntersectionBoundaryDomain(domain: IntersectionDomain)[source]

Bases: BoundaryDomain

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

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

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

class torchphysics.problem.domains.domainoperations.intersection.IntersectionDomain(domain_a: Domain, domain_b: Domain)[source]

Bases: Domain

Implements the logical intersection of two domains.

Parameters:
  • domain_a (Domain) – The first domain.

  • domain_b (Domain) – The second domain.

__call__(**data)[source]

Evaluates the domain at the given data.

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

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

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

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

torchphysics.problem.domains.domainoperations.product module

class torchphysics.problem.domains.domainoperations.product.ProductDomain(domain_a, domain_b)[source]

Bases: Domain

The ‘cartesian’ product of two domains. Additionally supports dependence of domain_a on domain_b, i.e. if the definition of domain_a contains functions of variables in domain_b.space, they are evaluated properly.

Parameters:
  • domain_a (Domain) – The (optionally dependent) first domain.

  • domain_b (Domain) – The second domain.

__call__(**data)[source]

Evaluates the domain at the given data.

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

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

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

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_bounding_box(bounds)[source]

To set the bounds of the domain.

Parameters:

bounds (list) – The bounding box of the domain. Whereby the lenght of the list has to be two times the domain dimension. And the bounds need to be in the following order: [min_axis_1, max_axis_1, min_axis_2, max_axis_2, …]

torchphysics.problem.domains.domainoperations.rotate module

class torchphysics.problem.domains.domainoperations.rotate.Rotate(domain: Domain, rotation_matrix, rotate_around=None)[source]

Bases: Domain

Class that rotates a given domain via a given matrix.

Parameters:
  • domain (torchphysics.domain.Domain) – The domain that should be rotated.

  • rotation_matrix (array_like or callable) – The matrix that describes the rotation, can also be a function that returns different matrices, depending on other parameters.

  • rotate_around (array_like or callable, optional) – The point around which the rotation occurs, can also be a function. Default is the origin.

Notes

All domains can already be rotated by passing in a function as the needed domain parameter. But for complex domains (cut, etc.) or objects with many corners (cube, square) it is easier to just rotate the whole domain with this class.

__call__(**data)[source]

Evaluates the domain at the given data.

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

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

classmethod from_angles(domain: Domain, *angles, rotate_around=None)[source]

Creates the rotation from given angles.

Parameters:
  • domain (torchphysics.domain.Domain) – The domain that should be rotated.

  • *angles (float or callable) – The angles that describe the rotation, can also be a functions. In 2D one angle \(\alpha\) is needed and internally the rotation matrix \((\cos(\alpha), -\sin(\alpha); \sin(\alpha), \cos(\alpha))\) is constructed. For 3D three angles are needed and the euler (extrinsic) rotation matrix from https://en.wikipedia.org/wiki/Rotation_matrix is used.

  • rotate_around (array_like or callable, optional) – The point around which the rotation occurs, can also be a function. Default is the origin.

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

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_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.

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

class torchphysics.problem.domains.domainoperations.rotate.RotationMatrix2D(fun, defaults={}, args={})[source]

Bases: DomainUserFunction

Given a function \(f:\Omega \to R\) will create the two dimensional rotation matrix \((cos(f), -sin(f); sin(f), cos(f))\).

__call__(args={}, device='cpu')[source]

To evalute the function. Will automatically extract the needed arguments from the input data and will set the possible default values.

Parameters:
  • args (dict or torchphysics.Points) – The input data, where the function should be evaluated.

  • device (str, optional) – The device on which the output of th efunction values should lay. Default is ‘cpu’.

Returns:

The output values of the function.

Return type:

torch.tensor

partially_evaluate(**args)[source]

(partially) evaluates a given function.

Parameters:

**args – The arguments where the function should be (partially) evaluated.

Returns:

Out – If the input arguments are enough to evalate the whole function, the corresponding output is returned. If some needed arguments are missing, a copy of this UserFunction will be returned. Whereby the values of **args will be added to the default values of the returned UserFunction.

Return type:

value or UserFunction

class torchphysics.problem.domains.domainoperations.rotate.RotationMatrix3D(alpha, beta, gamma, defaults=Ellipsis, args=Ellipsis)[source]

Bases: DomainUserFunction

property necessary_args

Returns the function arguments that are needed to evaluate this function.

Returns:

The needed arguments.

Return type:

list

torchphysics.problem.domains.domainoperations.sampler_helper module

This file contains some sample functions for the domain operations. Since Union/Cut/Intersection follow the same idea for sampling for a given number of points.

torchphysics.problem.domains.domainoperations.translate module

class torchphysics.problem.domains.domainoperations.translate.Translate(domain: Domain, translation)[source]

Bases: Domain

Class that translates a given domain by a given vector (or vector function).

Parameters:
  • domain (torchphysics.domain.Domain) – The domain that should be translated.

  • translation (array_like or callable) – The vector that describes the translation, can also be a function that returns different vectors.

Notes

All domains can already be moved by passing in a function as the needed domain parameter. But for complex domains (cut, etc.) or objects with many corners (cube, square) it is easier to just translate the whole domain with this class.

__call__(**data)[source]

Evaluates the domain at the given data.

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

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

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

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_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.

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

torchphysics.problem.domains.domainoperations.union module

class torchphysics.problem.domains.domainoperations.union.UnionBoundaryDomain(domain: UnionDomain)[source]

Bases: BoundaryDomain

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

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

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

class torchphysics.problem.domains.domainoperations.union.UnionDomain(domain_a: Domain, domain_b: Domain, disjoint=False)[source]

Bases: Domain

Implements the logical union of two domains.

Parameters:
  • domain_a (Domain) – The first domain.

  • domain_b (Domain) – The second domain.

__call__(**data)[source]

Evaluates the domain at the given data.

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

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

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

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