pencil.math =========== .. py:module:: pencil.math .. autoapi-nested-parse:: Basic mathematical operators, including derivatives. Submodules ---------- .. toctree:: :maxdepth: 1 /code/sourcePython/pencil/math/Helmholtz/index /code/sourcePython/pencil/math/derivatives/index /code/sourcePython/pencil/math/general/index /code/sourcePython/pencil/math/integration/index /code/sourcePython/pencil/math/interpolation/index /code/sourcePython/pencil/math/laplace_solver/index /code/sourcePython/pencil/math/poisson_solver/index /code/sourcePython/pencil/math/primes/index /code/sourcePython/pencil/math/stats/index /code/sourcePython/pencil/math/structure_function/index /code/sourcePython/pencil/math/transform/index /code/sourcePython/pencil/math/vector_multiplication/index Functions --------- .. autoapisummary:: pencil.math.dot pencil.math.dot2 pencil.math.cross pencil.math.is_number pencil.math.is_int pencil.math.is_float pencil.math.is_iterable pencil.math.log_range pencil.math.round_next_magnitude pencil.math.natural_sort pencil.math.pospolar2cart pencil.math.velpolar2cart pencil.math.coordinate_transformation pencil.math.vector_field_transformation pencil.math.integrate pencil.math.div pencil.math.curl pencil.math.helmholtz_fft pencil.math.prime_factors pencil.math.divisors pencil.math.common_factors pencil.math.cpu_optimal pencil.math.vec_int pencil.math.structure_function pencil.math.structure_function_shift_range pencil.math.laplace_scalar_cartesian pencil.math.laplace_vector_cartesian pencil.math.laplace_scalar_cylindrical pencil.math.laplace_vector_cylindrical pencil.math.laplace_scalar_spherical pencil.math.laplace_vector_spherical Package Contents ---------------- .. py:function:: dot(a, b) dot(a, b) Take dot product of two pencil-code vectors a and b. :param a: Pencil-code vectors with shape [3, mz, my, mx]. :type a: ndarrays :param b: Pencil-code vectors with shape [3, mz, my, mx]. :type b: ndarrays .. py:function:: dot2(a) dot2(a) Take dot product of a pencil-code vector with itself. :param a: Pencil-code vector with shape [3, mz, my, mx]. :type a: ndarray .. py:function:: cross(a, b) cross(a, b) Take cross of two pencil-code vectors a and b. :param a: Pencil-code vectors with shape [3, mz, my, mx]. :type a: ndarrays :param b: Pencil-code vectors with shape [3, mz, my, mx]. :type b: ndarrays .. py:function:: is_number(num_str) is_number(num_str) Checks if string s is a number. :param num_str: String containing the number to be checked. :type num_str: string .. py:function:: is_int(num_str) is_int(num_str) Checks if string num_str is an int. :param num_str: String containing the number to be checked. :type num_str: string .. py:function:: is_float(num_str) is_float(num_str) Checks if string num_str is a float. :param num_str: String containing the number to be checked. :type num_str: string .. py:function:: is_iterable(i) is_iterable(i) Checks if i is an iterable. :param i: Object to be checked. :type i: obj .. py:function:: 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. :param a: Start and end values. :type a: float :param b: Start and end values. :type b: float :param include_ab: Always include a and b in the interval. :type include_ab: bool :param finer_factor: Factor by which the range is being refined. :type finer_factor: float .. rubric:: Examples >>> log_range(4, 40) [4, 5, 6, 7, 8, 9, 10, 20, 30, 40] .. py:function:: 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. :param num: Number to be rounded. :type num: float .. py:function:: natural_sort(string_list, reverse=False) natural_sort(string_list, reverse=False) Sort a list of float numbers in strings in a natural way. :param string_list: Will be converted to float, then sorted. :type string_list: list of strings :param reverse: If true, list in reverse order. :type reverse: bool .. py:function:: pospolar2cart(r, th) Transform from polar to cartesian coordinates .. py:function:: 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 .. py:function:: 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). :param x: Input coordinates. :type x: 1d ndarray :param y: Input coordinates. :type y: 1d ndarray :param z: Input coordinates. :type z: 1d ndarray :param xyz_from: Origin coordinate system: 'cartesian', 'cylindrical' and 'spherical'. :type xyz_from: string :param xyz_to: Destination coordinate system: 'cartesian', 'cylindrical' and 'spherical'. :type xyz_to: string :rtype: Tuple of 3 ndarrays containing the transformed coordinates. .. py:function:: 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. :param field: Vector field. :type field: 4d ndarray :param x: Input coordinates of the 'from' system. :type x: 1d ndarrays :param y: Input coordinates of the 'from' system. :type y: 1d ndarrays :param z: Input coordinates of the 'from' system. :type z: 1d ndarrays :param xyz_from: Origin coordinate system: 'cartesian', 'cylindrical' and 'spherical'. :type xyz_from: string :param xyz_to: Destination coordinate system: 'cartesian', 'cylindrical' and 'spherical'. :type xyz_to: string :rtype: ndarray with the transformed vector field. .. py:function:: 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. :param quantity: Quantity to be integrated over of shape [nz, ny, nx]. :type quantity: ndarray :param dx: Grid spacing in the three dimensions. Not needed when x, y, z are specified. :type dx: floats :param dy: Grid spacing in the three dimensions. Not needed when x, y, z are specified. :type dy: floats :param dz: Grid spacing in the three dimensions. Not needed when x, y, z are specified. :type dz: floats :param x: Radial (x), polar (y) and vertical (z) coordinates, 1d arrays. These override dx, dy and dz. Can be non-uniform grids. :type x: ndarrays :param y: Radial (x), polar (y) and vertical (z) coordinates, 1d arrays. These override dx, dy and dz. Can be non-uniform grids. :type y: ndarrays :param z: Radial (x), polar (y) and vertical (z) coordinates, 1d arrays. These override dx, dy and dz. Can be non-uniform grids. :type z: ndarrays :param coordinate_system: Coordinate system under which to take the divergence. Takes 'cartesian', 'cylindrical' and 'spherical'. :type coordinate_system: string :param axis: Axis along which to integrate. :type axis: list of int .. py:function:: 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. :param f: Pencil code vector array f. :type f: ndarray :param grid: Pencil grid object. See pc.read.grid(). :type grid: pencil.read.grids.Grid :param coordinate_system: Coordinate system under which to take the divergence. Takes 'cartesian', 'cylindrical' and 'spherical'. :type coordinate_system: string :param Deprecated parameters (only for backwards compatibility): :param --------------------------------------------------------: :param dx: Grid spacing in the three dimensions. These will not have any effect if grid!=None :type dx: floats :param dy: Grid spacing in the three dimensions. These will not have any effect if grid!=None :type dy: floats :param dz: Grid spacing in the three dimensions. These will not have any effect if grid!=None :type dz: floats :param x: Radial (x) and polar (y) coordinates, 1d arrays. These will not have any effect if grid!=None :type x: ndarrays :param y: Radial (x) and polar (y) coordinates, 1d arrays. These will not have any effect if grid!=None :type y: ndarrays .. py:function:: 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. :param f: Pencil code scalar array f. :type f: ndarray :param grid: Pencil grid object. See pc.read.grid(). :type grid: pencil.read.grids.Grid :param run2D: Deals with pure 2-D snapshots. !Only for Cartesian grids at the moment! Requires grid!=None. :type run2D: bool :param coordinate_system: Coordinate system under which to take the divergence. Takes 'cartesian', 'cylindrical' and 'spherical'. !Does not work for 2d runs yet! :type coordinate_system: string :param Deprecated parameters (only for backwards compatibility): :param --------------------------------------------------------: :param dx: Grid spacing in the three dimensions. These will not have any effect if grid!=None :type dx: floats :param dy: Grid spacing in the three dimensions. These will not have any effect if grid!=None :type dy: floats :param dz: Grid spacing in the three dimensions. These will not have any effect if grid!=None :type dz: floats :param x: Radial (x) and polar (y) coordinates, 1d arrays. These will not have any effect if grid!=None :type x: ndarrays :param y: Radial (x) and polar (y) coordinates, 1d arrays. These will not have any effect if grid!=None :type y: ndarrays .. py:function:: 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. :param tot_field: Vector field of dimension [3, mz, my, mx], which is decomposed. :type tot_field: ndarray :param grid: Grid object with grid spacing dx, dy, dz. :type grid: obj :param params: Simulation Params object with domain dimensions Lx, Ly and Lz. :type params: obj :param nghost: Number of ghost zones to exclude from the fft. :type nghost: int :param lno_mean: Exclude any mean flow from the decomposition - should drop anyway. :type lno_mean: float :param nonperi_bc: String if not None with boundary condition label. How to apply the bc needs to be implemented as required. :type nonperi_bc: string :param field_scalar: Scalar field (density) as debug tool for energy comparison. :type field_scalar: ndarray :param s: List of three integers if not None for fft dimension. If none the dimension of the field [nz,ny,nx] is used. :type s: list of int :rtype: ndarray with decomposed field. .. py:function:: prime_factors(n) .. py:function:: divisors(factors, nmax, nmin=8) .. py:function:: common_factors(nx, ny, nz, nmin=8) .. py:function:: 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) .. py:function:: 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. :param xyz: Position vector around which will be interpolated. :type xyz: ndarray :param field: Vector field to be interpolated with shape [nz, ny, nx]. :type field: ndarray :param dxyz: Array with the three deltas. :type dxyz: ndarray :param oxyz: Array with the position of the origin. :type oxyz: ndarray :param nxyz: Number of grid points in each direction. :type nxyz: ndarray :param interpolation: Interpolation method. Can be 'mean', 'trilinear' or 'tricubic'. If 'tricubic' interpolation is chosen, then the splines functions need to be specified. :type interpolation: string :param splines: Field data for tricubic interpolation (deprecated, uses field parameter instead). :type splines: ndarray, optional :rtype: ndarray with interpolated vector field. .. py:function:: structure_function(arr, y, x) .. py:function:: structure_function_shift_range(arr, nmin, nmax, x) .. py:function:: 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. :param bc: Boundary conditions on exterior points. Keep the inner points 0. :type bc: ndarray of shape [nz, ny, nx] dx, dy, dz : floats Grid spacing in eaach direction. niter : int Number of iterations. :rtype: ndarray with the same shape as bc, representing solution to the Laplace equation. .. py:function:: 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. :param bx: Boundary conditions on exterior points. Keep the inner points 0. :type bx: ndarrays of shape [nz, ny, nx] :param by: Boundary conditions on exterior points. Keep the inner points 0. :type by: ndarrays of shape [nz, ny, nx] :param bz: Boundary conditions on exterior points. Keep the inner points 0. :type bz: ndarrays of shape [nz, ny, nx] dx, dy, dz : float Grid spacing in eaach direction. niter : int Number of iterations. :rtype: ndarray with the shape [3, nz, ny, nx], representing solution to the Laplace equation. .. py:function:: 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. :param bc: Boundary conditions on exterior points. Keep the inner points 0. :type bc: ndarray of shape [nz, ny, nx] r, theta, z : ndarrays of shape [nx], [ny] and [nz] 1D coordinate arrays of r, theta and z. niter : int Number of iterations. :rtype: ndarray with the same shape as bc, representing solution to the Laplace equation. .. py:function:: 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. :param br: Boundary conditions on exterior points. Keep the inner points 0. :type br: ndarrays of shape [nz, ny, nx] :param btheta: Boundary conditions on exterior points. Keep the inner points 0. :type btheta: ndarrays of shape [nz, ny, nx] :param bz: Boundary conditions on exterior points. Keep the inner points 0. :type bz: ndarrays of shape [nz, ny, nx] r, theta, z : ndarrays of shape [nx], [ny] and [nz] 1D coordinate arrays of r, theta and z. niter : int Number of iterations. :rtype: ndarray with the shape [3, nz, ny, nx], representing solution to the Laplace equation. .. py:function:: 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. :param bc: Boundary conditions on exterior points. Keep the inner points 0. :type bc: ndarray of shape [nz, ny, nx] r, theta, phi : ndarrays 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. niter : int Number of iterations. :rtype: ndarray with the same shape as bc, representing solution to the Laplace equation. .. py:function:: 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. :param bc: Boundary conditions on exterior points. Keep the inner points 0. :type bc: ndarray of shape [nz, ny, nx] :param btheta: Boundary conditions on exterior points. Keep the inner points 0. :type btheta: ndarray of shape [nz, ny, nx] :param bphi: Boundary conditions on exterior points. Keep the inner points 0. :type bphi: ndarray of shape [nz, ny, nx] r, theta, phi : ndarrays 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. niter : int Number of iterations. :rtype: ndarray with the shape [3, nz, ny, nx], representing solution to the Laplace equation.