Skip to content

Class pylset_field

Scalar field, such as temperature or concentration, on the grid of a level set simulation, with transient diffusion, sources and boundary conditions on each face of the domain. Created by setup_simulation.add_scalar_field().

Scalar field, such as temperature or concentration, on the grid of a level set simulation.

The field u follows the transient diffusion equation

c du/dt = div(k grad u) + Q,

with the capacity c, the conductivity k and the volumetric source Q. It is discretized by finite volumes on the cell centered grid of the simulation, with the conductivity at a face between two cells taken as the harmonic mean of the cell values, which keeps the flux continuous where k jumps. Each face of the domain has its own boundary condition (set_bc()). Axes that are periodic for the level sets are periodic for the field.

A field is created by setup_simulation.add_scalar_field() and advanced to the time of the level sets by setup_simulation.do_field_update(), or by a chosen time step with do_update().

Methods:

__init__

__init__(sim: Any, name: str, value: Value = 0.0, capacity: Value = 1.0, conductivity: Value = 1.0, source: Value = 0.0, time_scheme: str = 'Implicit', tolerance: float = 1e-08, max_iterations: int = 2000, cfl: float = 0.5, preconditioner: str = 'Multigrid') -> None

Initialize a scalar field. Use setup_simulation.add_scalar_field() rather than calling this directly.

Parameters:

Name Type Description Default
sim setup_simulation

Simulation whose grid, device and precision the field uses.

required
name str

Name of the field.

required
value (float, ndarray or callable)

Initial value, a number, an array of shape ndiv or a function of the cell center coordinates f(x, y) or f(x, y, z).

0.0
capacity (float, ndarray or callable)

Capacity c (for heat conduction the density times the specific heat). A function is called with the field f(u) at the start of each step.

1.0
conductivity (float, ndarray or callable)

Conductivity k, given like the capacity.

1.0
source (float, ndarray or callable)

Volumetric source Q. A function is called as f(x, y, t) or f(x, y, z, t) with the cell center coordinates on the device.

0.0
time_scheme str

'Implicit' (backward Euler, the default), 'Crank-Nicolson', 'Explicit' (forward Euler with substeps within the stability limit) or 'Steady' (the steady state for the current sources and boundary values at each update). The case is ignored.

'Implicit'
tolerance float

Relative residual at which the conjugate gradient iterations stop.

1e-08
max_iterations int

Largest number of conjugate gradient iterations per update.

2000
cfl float

Safety factor on the stability limit of the 'Explicit' scheme.

0.5
preconditioner str

'Multigrid' (a geometric multigrid V-cycle, the default) or 'Jacobi' (the diagonal) for the conjugate gradient iterations. The case is ignored.

'Multigrid'

add_source

add_source(power: Union[float, Callable], radius: float, center: Optional[Any] = None, path: Optional[Union[ndarray, Callable]] = None, profile: str = 'Gaussian', time: Optional[Tuple[float, float]] = None, name: Optional[str] = None) -> str

Add a localized source, at rest or moving, for example a laser or electron beam.

The source deposits the total power (the integral of Q over the domain, energy per time for heat), distributed as a Gaussian Q ~ exp(-2 r^2 / radius^2), so that radius is the 1/e^2 radius as is common for laser beams, or uniformly over a disc (2D) or sphere (3D) of the given radius with an edge smoothed over one grid spacing. The discrete profile is normalized so that the deposited power is exact on any grid, and the part of a source that lies outside a nonperiodic face is lost. In each update the source is averaged over the step, with the center sampled along the path at intervals of at most half the radius, so a fast source leaves a continuous track. Sources are evaluated in a box around their path, not on the whole grid.

Parameters:

Name Type Description Default
power float or callable

Total power, a number or a function of the time f(t).

required
radius float

The 1/e^2 radius of a 'Gaussian' source or the radius of a 'Uniform' source.

required
center sequence of float

Position of a source at rest.

None
path ndarray or callable

Position of a moving source, an array of rows (t, x, y) or (t, x, y, z) interpolated linearly in time and constant before the first and after the last row, or a function f(t) that returns the coordinates.

None
profile str

'Gaussian' (default) or 'Uniform'. The case is ignored.

'Gaussian'
time tuple of float

Interval (t_on, t_off) in which the source is active. Always active if None.

None
name str

Name of the source, used by remove_source(). A name is generated if None.

None

Returns:

Name Type Description
name str

Name of the source.

do_update

do_update(dtime: float) -> None

Advance the field by one time step.

With 'Implicit' and 'Crank-Nicolson', the theta scheme c (u1 - u0)/dt = theta (L u1 + b1) + (1 - theta)(L u0 + b0), with theta 1 or ½, is solved by preconditioned conjugate gradients, with the source and boundary values taken at t + theta dt. Capacity and conductivity functions are evaluated at u0, and the localized sources of add_source() are averaged over the step. 'Explicit' takes forward Euler substeps within get_stable_dtime(). 'Steady' solves L u + b = 0, which is well posed only with a condition that fixes the level of the field, so it requires at least one 'Dirichlet', 'Robin' or 'Radiation' face or domain. 'Radiation' conditions are linearized at the surface values, and the linear solve is repeated in Newton iterations until the field no longer changes. 'Explicit' takes the exact radiation flux of each substep, with the stability limit of the start of the step.

Parameters:

Name Type Description Default
dtime float

Time step, positive, or zero for a 'Steady' solve at the current time.

required

get_bc

get_bc() -> Dict[str, Tuple[str, Any, Any]]

Get the boundary conditions of all faces.

Returns:

Name Type Description
bc dict

Face name to the tuple (kind, value, coefficient).

get_domain_conditions

get_domain_conditions() -> Dict[int, str]

Get the kinds of the conditions set on immobile domains.

Returns:

Name Type Description
conditions dict

Domain ID to 'Material', 'Dirichlet', 'Neumann', 'Robin' or 'Radiation'.

get_name

get_name() -> str

Get the name of the field.

Returns:

Name Type Description
name str

Name given in add_scalar_field().

get_solver_info

get_solver_info() -> Dict[str, float]

Get the iterations and the relative residual of the last implicit update.

Returns:

Name Type Description
info dict

'iterations' (conjugate gradient iterations, summed over the Newton iterations), 'residual' (of the last solve) and 'newton_iterations' (linear solves for the 'Radiation' conditions, 1 without them).

get_sources

get_sources() -> List[str]

Get the names of the localized sources.

Returns:

Name Type Description
names list of str

Names of the sources added with add_source().

get_stable_dtime

get_stable_dtime() -> float

Get the stability limit of the 'Explicit' scheme for the current properties.

Returns:

Name Type Description
dtime float

cfl times the forward Euler limit min(c) / max(diag), where diag is the diagonal of the discrete operator including the boundary terms.

get_time

get_time() -> float

Get the time up to which the field has been advanced.

Returns:

Name Type Description
time float

Time of the field.

get_time_scheme

get_time_scheme() -> str

Get the time integration scheme.

Returns:

Name Type Description
time_scheme str

'Implicit', 'Crank-Nicolson', 'Explicit' or 'Steady'.

get_value

get_value() -> np.ndarray

Get the field values.

Returns:

Name Type Description
u ndarray of shape `ndiv`

Host copy of the field.

get_value_device

get_value_device() -> torch.Tensor

Get the field on the computing device (not a copy, do not change it in place).

Returns:

Name Type Description
u torch.Tensor of shape `ndiv`

Field on the device.

remove_source

remove_source(name: str) -> None

Remove a localized source.

Parameters:

Name Type Description Default
name str

Name returned by add_source().

required

set_bc

set_bc(face: str, kind: str, value: Value = 0.0, coefficient: Optional[Value] = None) -> None

Set the boundary condition on one face of the domain.

Parameters:

Name Type Description Default
face str

'x-', 'x+', 'y-', 'y+' and, in 3D, 'z-' or 'z+'.

required
kind str

'Dirichlet' (the field value on the face), 'Neumann' (the flux value into the domain, zero for an insulated face), 'Robin' (the flux coefficient (value - u) into the domain, with the face value u, for example convection to a fluid at the temperature value with the heat transfer coefficient coefficient) or 'Radiation' (the flux coefficient (value^4 - u^4) into the domain, radiation exchange with surroundings at the absolute temperature value, where coefficient is the emissivity times the Stefan-Boltzmann constant in the units of the field, which must then be an absolute temperature). Faces of periodic axes are 'Periodic' and cannot be changed. The case is ignored.

required
value (float, ndarray or callable)

A number, an array of the shape of the face (the grid shape without the normal axis) or a function of the time f(t) that returns either.

0.0
coefficient (float, ndarray or callable)

Transfer coefficient of the 'Robin' condition or emissivity times the Stefan-Boltzmann constant of the 'Radiation' condition, given like value.

None

Raises:

Type Description
ValueError

If the face or the kind is not recognized, or if the face belongs to a periodic axis.

set_capacity

set_capacity(capacity: Value) -> None

Set the capacity c.

Parameters:

Name Type Description Default
capacity (float, ndarray or callable)

A positive number, an array of shape ndiv, or a function of the field, f(u), evaluated at the start of each update.

required

set_conductivity

set_conductivity(conductivity: Value) -> None

Set the conductivity k.

Parameters:

Name Type Description Default
conductivity (float, ndarray or callable)

A nonnegative number, an array of shape ndiv, or a function of the field, f(u), evaluated at the start of each update.

required

set_domain_condition

set_domain_condition(domain_id: int, kind: Optional[str], value: Value = 0.0, coefficient: Optional[Value] = None, capacity: Optional[Value] = None, conductivity: Optional[Value] = None, source: Optional[Value] = None) -> None

Set the field condition of an immobile domain.

Parameters:

Name Type Description Default
domain_id int

ID of an immobile domain (see setup_simulation.set_domain_mobile()).

required
kind str or None

'Material' (the domain is part of the problem with its own capacity, conductivity and source), 'Dirichlet' (the domain is removed and the field takes value on its surface), 'Neumann' (the domain is removed and its surface gives the flux value into the rest of the domain, zero for an insulated surface), 'Robin' (the surface gives the flux coefficient (value - u)) or 'Radiation' (the surface gives the flux coefficient (value^4 - u^4), see set_bc()). None removes the condition, so that the domain is ordinary material again. The case is ignored. After each update, the cells of a removed domain hold the surface value ('Dirichlet') or the value of the nearest remaining cell.

required
value float or callable

Surface value or flux, a number or a function of the time f(t).

0.0
coefficient float or callable

Transfer coefficient of 'Robin' or emissivity times the Stefan-Boltzmann constant of 'Radiation', given like value.

None
capacity float or callable

Properties of a 'Material' domain, numbers or functions of the field f(u). Properties that are not given keep the values of the surroundings.

None
conductivity float or callable

Properties of a 'Material' domain, numbers or functions of the field f(u). Properties that are not given keep the values of the surroundings.

None
source float or callable

Volumetric source in a 'Material' domain, a number or a function f(t).

None

Raises:

Type Description
ValueError

If the domain is unknown or mobile, or the kind is not recognized.

set_preconditioner

set_preconditioner(preconditioner: str) -> None

Set the preconditioner of the conjugate gradient iterations.

Parameters:

Name Type Description Default
preconditioner str

'Multigrid' or 'Jacobi'. The case is ignored.

required

set_source

set_source(source: Value) -> None

Set the volumetric source Q.

Parameters:

Name Type Description Default
source (float, ndarray or callable)

A number, an array of shape ndiv, or a function f(x, y, t) or f(x, y, z, t) of the cell center coordinates on the device and the time. A function with one more argument, f(x, y, t, u) or f(x, y, z, t, u), also receives the field, taken at the start of the step, for example for heat generation that depends on temperature. Localized and moving sources are added with add_source().

required

set_time_scheme

set_time_scheme(time_scheme: str) -> None

Set the time integration scheme.

Parameters:

Name Type Description Default
time_scheme str

'Implicit', 'Crank-Nicolson', 'Explicit' or 'Steady'. The case is ignored.

required

set_value

set_value(value: Value) -> None

Set the field values.

Parameters:

Name Type Description Default
value (float, ndarray or callable)

A number, an array of shape ndiv or a function of the cell center coordinates, f(x, y) or f(x, y, z), evaluated on the device.

required