pencil.math

Basic mathematical operators, including derivatives.

Submodules

Functions

dot(a, b)

dot(a, b)

dot2(a)

dot2(a)

cross(a, b)

cross(a, b)

is_number(num_str)

is_number(num_str)

is_int(num_str)

is_int(num_str)

is_float(num_str)

is_float(num_str)

is_iterable(i)

is_iterable(i)

log_range(a, b[, include_ab, finer_factor])

log_range(a, b, include_ab=False, finer_factor=1)

round_next_magnitude(num)

round_next_magnitude(num)

natural_sort(string_list[, reverse])

natural_sort(string_list, reverse=False)

pospolar2cart(r, th)

Transform from polar to cartesian coordinates

velpolar2cart(vr, vth, r, th[, zcoord])

Transform from polar velocities to cartesian velocities

coordinate_transformation(x, y, z[, xyz_from, xyz_to])

coordinate_transformation(x, y, z, xyz_from='', xyz_to='')

vector_field_transformation(field, x, y, z[, ...])

vector_field_transformation(field, x, y, z, xyz_from='', xyz_to='')

integrate(quantity[, dx, dy, dz, x, y, z, ...])

integrate(quantity, dx=1.0, dy=1.0, dz=1.0, x=None, y=None, z=None,

div(f[, dx, dy, dz, x, y, coordinate_system, grid])

div(f, dx=None, dy=None, dz=None, x=None, y=None, coordinate_system="cartesian", grid=None)

curl(f[, dx, dy, dz, x, y, run2D, coordinate_system, grid])

curl(f, dx=None, dy=None, dz=None, x=None, y=None, run2D=False, coordinate_system="cartesian", grid=None)

helmholtz_fft(tot_field, grid, params[, nghost, pot, ...])

helmholz_fft(field, grid, params)

prime_factors(n)

divisors(factors, nmax[, nmin])

common_factors(nx, ny, nz[, nmin])

cpu_optimal(nx, ny, nz[, mvar, maux, par, nmin, ...])

vec_int(xyz, field, dxyz, oxyz, nxyz[, interpolation, ...])

vec_int(xyz, field, dxyz, oxyz, nxyz, interpolation='trilinear')

structure_function(arr, y, x)

structure_function_shift_range(arr, nmin, nmax, x)

laplace_scalar_cartesian(bc, dx, dy, dz[, niter])

laplace_scalar_cartesian(bc, dx, dy, dz, niter=100)

laplace_vector_cartesian(bx, by, bz, dx, dy, dz[, niter])

laplace_vector_cartesian(bx, by, bz, dx, dy, dz, niter=1000)

laplace_scalar_cylindrical(bc, r, theta, z[, niter])

laplace_scalar_cylindrical(bc, r, theta, z, niter=1000)

laplace_vector_cylindrical(br, btheta, bz, r, theta, z)

laplace_vector_cylindrical(br, btheta, bz, r, theta, z, niter=200)

laplace_scalar_spherical(bc, r, theta, phi[, niter])

laplace_scalar_spherical(bc, r, theta, phi, niter=200)

laplace_vector_spherical(br, btheta, bphi, r, theta, phi)

laplace_vector_spherical(br, btheta, bphi, r, theta, phi, niter=200)

Package Contents

pencil.math.dot(a, b)

dot(a, b)

Take dot product of two pencil-code vectors a and b.

Parameters:
  • a (ndarrays) – Pencil-code vectors with shape [3, mz, my, mx].

  • b (ndarrays) – Pencil-code vectors with shape [3, mz, my, mx].

pencil.math.dot2(a)

dot2(a)

Take dot product of a pencil-code vector with itself.

Parameters:

a (ndarray) – Pencil-code vector with shape [3, mz, my, mx].

pencil.math.cross(a, b)

cross(a, b)

Take cross of two pencil-code vectors a and b.

Parameters:
  • a (ndarrays) – Pencil-code vectors with shape [3, mz, my, mx].

  • b (ndarrays) – Pencil-code vectors with shape [3, mz, my, mx].

pencil.math.is_number(num_str)

is_number(num_str)

Checks if string s is a number.

Parameters:

num_str (string) – String containing the number to be checked.

pencil.math.is_int(num_str)

is_int(num_str)

Checks if string num_str is an int.

Parameters:

num_str (string) – String containing the number to be checked.

pencil.math.is_float(num_str)

is_float(num_str)

Checks if string num_str is a float.

Parameters:

num_str (string) – String containing the number to be checked.

pencil.math.is_iterable(i)

is_iterable(i)

Checks if i is an iterable.

Parameters:

i (obj) – Object to be checked.

pencil.math.log_range(a, b, include_ab=False, finer_factor=1)

log_range(a, b, include_ab=False, finer_factor=1)

Compute the logarithmic range between a and b.

Parameters:
  • a (float) – Start and end values.

  • b (float) – Start and end values.

  • include_ab (bool) – Always include a and b in the interval.

  • finer_factor (float) – Factor by which the range is being refined.

Examples

>>> log_range(4, 40)
[4, 5, 6, 7, 8, 9, 10, 20, 30, 40]
pencil.math.round_next_magnitude(num)

round_next_magnitude(num)

Rounds a number up to the next multiple of 10, e.g. 4.56 will be rounded to 10.

Parameters:

num (float) – Number to be rounded.

pencil.math.natural_sort(string_list, reverse=False)

natural_sort(string_list, reverse=False)

Sort a list of float numbers in strings in a natural way.

Parameters:
  • string_list (list of strings) – Will be converted to float, then sorted.

  • reverse (bool) – If true, list in reverse order.

pencil.math.pospolar2cart(r, th)

Transform from polar to cartesian coordinates

pencil.math.velpolar2cart(vr, vth, r, th, zcoord=0)

Transform from polar velocities to cartesian velocities

call signature:

velpolar2cart(vr, vth, r, th, zcoord=0)

Keyword arguments:

vr:

3D array of velocities in radial direction

vth:

3D array of velocities in theta direction

r

coordinates in radial direction

th

coordinates in theta direction

zcoord

plane in z-dir

pencil.math.coordinate_transformation(x, y, z, xyz_from='', xyz_to='')

coordinate_transformation(x, y, z, xyz_from=’’, xyz_to=’’)

Tranform coordinates between coordinate systems. Returns a tuple (x, y, z).

Parameters:
  • x (1d ndarray) – Input coordinates.

  • y (1d ndarray) – Input coordinates.

  • z (1d ndarray) – Input coordinates.

  • xyz_from (string) – Origin coordinate system: ‘cartesian’, ‘cylindrical’ and ‘spherical’.

  • xyz_to (string) – Destination coordinate system: ‘cartesian’, ‘cylindrical’ and ‘spherical’.

Return type:

Tuple of 3 ndarrays containing the transformed coordinates.

pencil.math.vector_field_transformation(field, x, y, z, xyz_from='', xyz_to='')

vector_field_transformation(field, x, y, z, xyz_from=’’, xyz_to=’’)

Transform a vector field from one coordinate system to another.

Parameters:
  • field (4d ndarray) – Vector field.

  • x (1d ndarrays) – Input coordinates of the ‘from’ system.

  • y (1d ndarrays) – Input coordinates of the ‘from’ system.

  • z (1d ndarrays) – Input coordinates of the ‘from’ system.

  • xyz_from (string) – Origin coordinate system: ‘cartesian’, ‘cylindrical’ and ‘spherical’.

  • xyz_to (string) – Destination coordinate system: ‘cartesian’, ‘cylindrical’ and ‘spherical’.

Return type:

ndarray with the transformed vector field.

pencil.math.integrate(quantity, dx=1.0, dy=1.0, dz=1.0, x=None, y=None, z=None, coordinate_system='cartesian', axis=(0, 1, 2))
integrate(quantity, dx=1.0, dy=1.0, dz=1.0, x=None, y=None, z=None,

coordinate_system=’cartesian’, axis=[0, 1, 2])

Integrate a field along axis ‘axis’ using the trapezoidal rule. Works for Cartesian, cylindrical and spherical coordinates. Works with non-uniform grids.

Parameters:
  • quantity (ndarray) – Quantity to be integrated over of shape [nz, ny, nx].

  • dx (floats) – Grid spacing in the three dimensions. Not needed when x, y, z are specified.

  • dy (floats) – Grid spacing in the three dimensions. Not needed when x, y, z are specified.

  • dz (floats) – Grid spacing in the three dimensions. Not needed when x, y, z are specified.

  • x (ndarrays) – Radial (x), polar (y) and vertical (z) coordinates, 1d arrays. These override dx, dy and dz. Can be non-uniform grids.

  • y (ndarrays) – Radial (x), polar (y) and vertical (z) coordinates, 1d arrays. These override dx, dy and dz. Can be non-uniform grids.

  • z (ndarrays) – Radial (x), polar (y) and vertical (z) coordinates, 1d arrays. These override dx, dy and dz. Can be non-uniform grids.

  • coordinate_system (string) – Coordinate system under which to take the divergence. Takes ‘cartesian’, ‘cylindrical’ and ‘spherical’.

  • axis (list of int) – Axis along which to integrate.

pencil.math.div(f, dx=None, dy=None, dz=None, x=None, y=None, coordinate_system='cartesian', grid=None)

div(f, dx=None, dy=None, dz=None, x=None, y=None, coordinate_system=”cartesian”, grid=None)

Take divergence of pencil code vector array f in various coordinate systems.

Parameters:
  • f (ndarray) – Pencil code vector array f.

  • grid (pencil.read.grids.Grid) – Pencil grid object. See pc.read.grid().

  • coordinate_system (string) – Coordinate system under which to take the divergence. Takes ‘cartesian’, ‘cylindrical’ and ‘spherical’.

  • compatibility) (Deprecated parameters (only for backwards)

  • --------------------------------------------------------

  • dx (floats) – Grid spacing in the three dimensions. These will not have any effect if grid!=None

  • dy (floats) – Grid spacing in the three dimensions. These will not have any effect if grid!=None

  • dz (floats) – Grid spacing in the three dimensions. These will not have any effect if grid!=None

  • x (ndarrays) – Radial (x) and polar (y) coordinates, 1d arrays. These will not have any effect if grid!=None

  • y (ndarrays) – Radial (x) and polar (y) coordinates, 1d arrays. These will not have any effect if grid!=None

pencil.math.curl(f, dx=None, dy=None, dz=None, x=None, y=None, run2D=False, coordinate_system='cartesian', grid=None)

curl(f, dx=None, dy=None, dz=None, x=None, y=None, run2D=False, coordinate_system=”cartesian”, grid=None)

Take the curl of a pencil code vector array f in various coordinate systems.

Parameters:
  • f (ndarray) – Pencil code scalar array f.

  • grid (pencil.read.grids.Grid) – Pencil grid object. See pc.read.grid().

  • run2D (bool) – Deals with pure 2-D snapshots. !Only for Cartesian grids at the moment! Requires grid!=None.

  • coordinate_system (string) – Coordinate system under which to take the divergence. Takes ‘cartesian’, ‘cylindrical’ and ‘spherical’. !Does not work for 2d runs yet!

  • compatibility) (Deprecated parameters (only for backwards)

  • --------------------------------------------------------

  • dx (floats) – Grid spacing in the three dimensions. These will not have any effect if grid!=None

  • dy (floats) – Grid spacing in the three dimensions. These will not have any effect if grid!=None

  • dz (floats) – Grid spacing in the three dimensions. These will not have any effect if grid!=None

  • x (ndarrays) – Radial (x) and polar (y) coordinates, 1d arrays. These will not have any effect if grid!=None

  • y (ndarrays) – Radial (x) and polar (y) coordinates, 1d arrays. These will not have any effect if grid!=None

pencil.math.helmholtz_fft(tot_field, grid, params, nghost=3, pot=True, rot=True, lno_mean=False, nonperi_bc=None, field_scalar=[], s=None, quiet=True)

helmholz_fft(field, grid, params)

Creates the decomposition vector pair for the supplied vector field.

Parameters:
  • tot_field (ndarray) – Vector field of dimension [3, mz, my, mx], which is decomposed.

  • grid (obj) – Grid object with grid spacing dx, dy, dz.

  • params (obj) – Simulation Params object with domain dimensions Lx, Ly and Lz.

  • nghost (int) – Number of ghost zones to exclude from the fft.

  • lno_mean (float) – Exclude any mean flow from the decomposition - should drop anyway.

  • nonperi_bc (string) – String if not None with boundary condition label. How to apply the bc needs to be implemented as required.

  • field_scalar (ndarray) – Scalar field (density) as debug tool for energy comparison.

  • s (list of int) – List of three integers if not None for fft dimension. If none the dimension of the field [nz,ny,nx] is used.

Return type:

ndarray with decomposed field.

pencil.math.prime_factors(n)
pencil.math.divisors(factors, nmax, nmin=8)
pencil.math.common_factors(nx, ny, nz, nmin=8)
pencil.math.cpu_optimal(nx, ny, nz, mvar=8, maux=0, par=dict(), nmin=32, MBmin=5.0, minghosts=7, quiet=True, size=1, remesh=False, nsize=None)
pencil.math.vec_int(xyz, field, dxyz, oxyz, nxyz, interpolation='trilinear', splines=None)

vec_int(xyz, field, dxyz, oxyz, nxyz, interpolation=’trilinear’)

Interpolates the field around position xyz.

Parameters:
  • xyz (ndarray) – Position vector around which will be interpolated.

  • field (ndarray) – Vector field to be interpolated with shape [nz, ny, nx].

  • dxyz (ndarray) – Array with the three deltas.

  • oxyz (ndarray) – Array with the position of the origin.

  • nxyz (ndarray) – Number of grid points in each direction.

  • interpolation (string) – Interpolation method. Can be ‘mean’, ‘trilinear’ or ‘tricubic’. If ‘tricubic’ interpolation is chosen, then the splines functions need to be specified.

  • splines (ndarray, optional) – Field data for tricubic interpolation (deprecated, uses field parameter instead).

Return type:

ndarray with interpolated vector field.

pencil.math.structure_function(arr, y, x)
pencil.math.structure_function_shift_range(arr, nmin, nmax, x)
pencil.math.laplace_scalar_cartesian(bc, dx, dy, dz, niter=200)

laplace_scalar_cartesian(bc, dx, dy, dz, niter=100)

Solve the scalar Laplace equation in Cartesian coordinates in 3 dimensions using finite differences.

Parameters:

bc (ndarray of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

dx, dy, dzfloats

Grid spacing in eaach direction.

niterint

Number of iterations.

Return type:

ndarray with the same shape as bc, representing solution to the Laplace equation.

pencil.math.laplace_vector_cartesian(bx, by, bz, dx, dy, dz, niter=200)

laplace_vector_cartesian(bx, by, bz, dx, dy, dz, niter=1000)

Solve the vector Laplace equation in Cartesian coordinates in 3 dimensions using finite differences. This function simply applies the scalar function to the three components.

Parameters:
  • bx (ndarrays of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

  • by (ndarrays of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

  • bz (ndarrays of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

dx, dy, dzfloat

Grid spacing in eaach direction.

niterint

Number of iterations.

Return type:

ndarray with the shape [3, nz, ny, nx], representing solution to the Laplace equation.

pencil.math.laplace_scalar_cylindrical(bc, r, theta, z, niter=200)

laplace_scalar_cylindrical(bc, r, theta, z, niter=1000)

Solve the scalar Laplace equation in cylindical coordinates in 3 dimensions using finite differences.

Parameters:

bc (ndarray of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

r, theta, zndarrays of shape [nx], [ny] and [nz]

1D coordinate arrays of r, theta and z.

niterint

Number of iterations.

Return type:

ndarray with the same shape as bc, representing solution to the Laplace equation.

pencil.math.laplace_vector_cylindrical(br, btheta, bz, r, theta, z, niter=200)

laplace_vector_cylindrical(br, btheta, bz, r, theta, z, niter=200)

Solve the vector Laplace equation in cylindrical coordinates in 3 dimensions using finite differences.

Parameters:
  • br (ndarrays of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

  • btheta (ndarrays of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

  • bz (ndarrays of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

r, theta, zndarrays of shape [nx], [ny] and [nz]

1D coordinate arrays of r, theta and z.

niterint

Number of iterations.

Return type:

ndarray with the shape [3, nz, ny, nx], representing solution to the Laplace equation.

pencil.math.laplace_scalar_spherical(bc, r, theta, phi, niter=200)

laplace_scalar_spherical(bc, r, theta, phi, niter=200)

Solve the scalar Laplace equation in spherical coordinates in 3 dimensions using finite differences.

Parameters:

bc (ndarray of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

r, theta, phindarrays of shape [nx], [ny] and [nz]

1D coordinate arrays of r, theta and phi. The convention is taken that theta ranges from 0 to pi and phi ranges from 0 to 2pi. Note that singularities arise in the equation when theta = 0 and r = 0. This may produce unintended results.

niterint

Number of iterations.

Return type:

ndarray with the same shape as bc, representing solution to the Laplace equation.

pencil.math.laplace_vector_spherical(br, btheta, bphi, r, theta, phi, niter=200)

laplace_vector_spherical(br, btheta, bphi, r, theta, phi, niter=200)

Solve the scalar Laplace equation in spherical coordinates in 3 dimensions using finite differences.

Parameters:
  • bc (ndarray of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

  • btheta (ndarray of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

  • bphi (ndarray of shape [nz, ny, nx]) – Boundary conditions on exterior points. Keep the inner points 0.

r, theta, phindarrays of shape [nx], [ny] and [nz]

1D coordinate arrays of r, theta and phi. The convention is taken that theta ranges from 0 to pi and phi ranges from 0 to 2pi. Note that singularities arise in the equation when theta = 0 and r = 0. This may produce unintended results.

niterint

Number of iterations.

Return type:

ndarray with the shape [3, nz, ny, nx], representing solution to the Laplace equation.