pyoomph.expressions package

Submodules

Module contents

pyoomph.expressions.absolute(x)[source]

Compute the absolute of the input expression or number.

The argument may carry any unit, which the result inherits. Differentiates as signum(x)*dx, i.e. as if it were smooth, and hence has no delta contribution at x=0.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The absolute of the input.

Return type:

Expression

pyoomph.expressions.acos(x)[source]

Compute the inverse cosine of the input expression or number.

The argument must be dimensionless and, for a real-valued result, within [-1,1].

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The inverse cosine of the input.

Return type:

Expression

pyoomph.expressions.acosh(x)[source]

Compute the inverse hyperbolic cosine of the input expression or number.

The argument must be dimensionless and, for a real-valued result, at least 1. Its derivative diverges at exactly 1, which can stall a Newton step starting there.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The inverse hyperbolic cosine of the input.

Return type:

Expression

pyoomph.expressions.asin(x)[source]

Compute the inverse sine of the input expression or number.

The argument must be dimensionless and, for a real-valued result, within [-1,1].

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The inverse sine of the input.

Return type:

Expression

pyoomph.expressions.asinh(x)[source]

Compute the inverse hyperbolic sine of the input expression or number.

The argument must be dimensionless.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The inverse hyperbolic sine of the input.

Return type:

Expression

pyoomph.expressions.atan(x)[source]

Compute the inverse tangent of the input expression or number.

The argument must be dimensionless.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The inverse tangent of the input.

Return type:

Expression

pyoomph.expressions.atan2(y, x)[source]

Compute atan2(y,x) of the input expression or number.

Unlike atan(y/x), this resolves the quadrant from the signs of both arguments and stays finite at x=0.

Both arguments must be dimensionless - a common unit is not divided out automatically, so pass e.g. atan2(y/(1*meter),x/(1*meter)) rather than atan2(y,x) for dimensional coordinates.

Parameters:
Returns:

atan2(y,x), i.e. atan(y/x) with case distinction.

Return type:

Expression

pyoomph.expressions.atanh(x)[source]

Compute the inverse hyperbolic tangent of the input expression or number.

The argument must be dimensionless and, for a real-valued result, within (-1,1).

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The inverse hyperbolic tangent of the input.

Return type:

Expression

pyoomph.expressions.conditional(cond, iftrue, iffalse)[source]

Branches on a symbolic comparison, e.g. conditional(var("time")<2*second, 1, 2). The comparison is kept symbolically until the expression is either called with concrete values, e.g. expr(time=4*second), or compiled into the generated code, where it becomes a ternary.

This function is required because Python’s own ternary iftrue if cond else iffalse cannot be used: Python always casts the condition to a plain bool first, so the branch would be selected immediately instead of symbolically.

Comparisons can be combined with ~ (not), & (and), | (or) and ^ (xor), e.g. conditional((var("time")>1*second) & (var("time")<2*second), 1, 2). Python’s keywords not, and and or cannot be used for the same reason as the ternary, and neither can a chained comparison like a<b<c, which Python expands into (a<b) and (b<c); write (a<b) & (b<c) instead. Mind that the bitwise operators bind tighter than the comparisons, so each operand needs its own parentheses. The named logical_and(), logical_or(), logical_not() and logical_xor() avoid that pitfall.

Only the four inequalities <, <=, > and >= build such a symbolic comparison. == and != are deliberately left alone, since Python requires them to return a plain bool for expressions to remain usable in dicts, sets and in tests; a == b therefore is an identity comparison and is_equal() or (a-b).is_zero() is what tests two expressions for equality. A symbolic equality branch can be written as two nested conditionals, e.g. conditional(a<=b, conditional(a>=b, iftrue, iffalse), iffalse), but mind that this compares floating point numbers exactly in the generated code.

Both sides of the comparison must have the same unit. As in piecewise_geq0(), the branches iftrue and iffalse must agree in units as well, which then are the units of the result, and a plain 0 is accepted as a branch irrespective of the unit of the other branch.

Parameters:
Returns:

The resulting conditional expression.

Return type:

Expression

pyoomph.expressions.cos(x)[source]

Compute the cosine of the input expression or number.

The argument must be dimensionless.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The cosine of the input.

Return type:

Expression

pyoomph.expressions.cosh(x)[source]

Compute the hyperbolic cosine of the input expression or number.

The argument must be dimensionless.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The hyperbolic cosine of the input.

Return type:

Expression

pyoomph.expressions.determinant(M, n=0)[source]

Compute the determinant of a matrix expression.

Parameters:
  • M (Expression) – The matrix expression for which to compute the determinant.

  • n (int) – Range of the matrix to consider for the determinant. Default is 0 (extract nonzero block), <0 means full matrix, >0 upper left matrix of n x n.

Returns:

The determinant of the matrix expression.

Return type:

Expression

pyoomph.expressions.div(arg, lagrangian=False, matrix=None, nondim=False, coordsys=None)[source]

Compute the divergence of the given argument. On surfaces, i.e. with a co-dimension, it is the surface divergence.

Parameters: arg (ExpressionOrNum): The argument for which the divergence is computed. lagrangian (bool, optional): Flag indicating whether the computation is with respect to Lagrangian coordinates. Defaults to False. matrix (bool, optional): Flag indicating whether the computation is for a matrix expression. Defaults to None, i.e. auto-select. nondim (bool, optional): Flag indicating whether the computation is with respect to non-dimensional coordinates. Defaults to False. coordsys (BaseCoordinateSystem, optional): The coordinate system in which the computation is performed. Defaults to None, i.e. the coordinate system of either the current or parent equations or the problem.

Returns: Expression: The computed divergence expression.

Return type:

Expression

Notes

if you calculate div(u) on a boundary, you will get the surface divergence, even if u is defined in the bulk. To get the bulk divergence at the boundary, use div(var(“u”,domain=”..”)) instead.

Index order: for a rank-2 tensor, div(T)[i] is \(\partial_j T_{ij}\), contracting the second index. This makes div the adjoint of grad() (which stores \(\partial u_i/\partial x_j\)) and div(T) the integration-by-parts partner of weak(T,grad(v)) together with the traction matproduct(T,n). For symmetric tensors, i.e. every usual stress tensor, the distinction does not matter. Note that a flux tensor has to be assembled accordingly: the momentum flux carrying \(\vec{u}\) along \(\rho\vec{q}\) is dyadic(u,rho*q), so that \(F_{ij}=\rho u_i q_j\).

pyoomph.expressions.erf(x)[source]

Compute the error function of the input expression or number.

The argument must be dimensionless and is assumed to be real-valued. Differentiates to 2/sqrt(pi)*exp(-x**2), so it can be used in residuals without further ado.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The error function of the input.

Return type:

Expression

pyoomph.expressions.erfc(x)[source]

Compute the complementary error function, i.e. 1-erf(x), of the input expression or number.

Prefer it over writing 1-erf(x) by hand: for large arguments, the latter is entirely cancellation, whereas erfc retains its accuracy.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The complementary error function of the input.

Return type:

Expression

pyoomph.expressions.exp(x)[source]

Compute the exponential of the input expression or number.

The argument must be dimensionless.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The exponential of the input.

Return type:

Expression

pyoomph.expressions.heaviside(x)[source]

Heaviside step function, i.e. 1 for x>0, 0 for x<0 and 1/2 for x=0.

The argument may carry any unit (its scale and unit are divided out), the result is always dimensionless. Note that it differentiates to zero everywhere, i.e. the delta contribution at the jump is not added to the Jacobian.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The argument of the step function.

Returns:

The step function of the input.

Return type:

Expression

pyoomph.expressions.imag_part(x)[source]

Compute the imaginary part of the input expression or number.

Units are kept, i.e. the imaginary part of a length is a length. Note that this assumes every field to be real-valued, so that only an explicit imaginary_i() makes a term imaginary.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The imaginary part of the input.

Return type:

Expression

pyoomph.expressions.imaginary_i()[source]

Return the imaginary unit i.

Residuals themselves must be real-valued; the imaginary unit is meant for the complex-valued expressions of e.g. an eigenmode or a Helmholtz problem, which are split into their real and imaginary parts before the code is generated. Use real_part() and imag_part() to do that splitting by hand.

Returns:

The imaginary unit i.

Return type:

Expression

pyoomph.expressions.inverse_matrix(M, n=0, use_subexpression_for_det=True, fill_to_vector_dim_3=False, skip_empty_rows_and_cols=False)[source]

Compute the inverse of a matrix expression.

Parameters:
  • M (Expression) – The matrix expression for which to compute the determinant.

  • n (int) – Range of the matrix to consider for the inverse. Default is 0 (extract nonzero block), <0 means full matrix, >0 upper left matrix of n x n.

  • use_subexpression_for_det (bool) – Flag indicating whether to use a subexpression for the determinant. Default is True.

  • skip_empty_rows_and_cols (bool) – Analyze the input and skip empty rows and columns. These empty rows and columns will be added to the result as zero rows and columns of the inverse. Default is False.

Returns:

The symbolical inverse of the matrix expression.

Return type:

Expression

pyoomph.expressions.log(x)[source]

Compute the logarithm of the input expression or number.

The argument must be dimensionless and, for a real-valued result, positive.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The logarithm of the input.

Return type:

Expression

pyoomph.expressions.logical_and(*conds)[source]

Combines conditions with a logical and, i.e. the same as a & b & ..., which Python’s and cannot express.

Parameters:

*conds (RelationalExpression) – At least one condition. All of them must hold.

Returns:

The combined condition.

Return type:

RelationalExpression

pyoomph.expressions.logical_not(cond)[source]

Negates a condition, i.e. the same as ~cond.

Python’s not cannot be used, since it casts the condition to a plain bool. The operator ~ does the job, but binds tighter than the comparisons, i.e. it must be written as ~(a<b). This function is the spelled-out alternative.

Parameters:

cond (RelationalExpression) – The condition to negate.

Returns:

The negated condition.

Return type:

RelationalExpression

pyoomph.expressions.logical_or(*conds)[source]

Combines conditions with a logical or, i.e. the same as a | b | ..., which Python’s or cannot express.

Parameters:

*conds (RelationalExpression) – At least one condition. At least one of them must hold.

Returns:

The combined condition.

Return type:

RelationalExpression

pyoomph.expressions.logical_xor(*conds)[source]

Combines conditions with an exclusive or, i.e. the same as a ^ b ^ ..., which holds whenever an odd number of the conditions hold.

Parameters:

*conds (RelationalExpression) – At least one condition.

Returns:

The combined condition.

Return type:

RelationalExpression

pyoomph.expressions.maximum(x, y)[source]

Compute the maximum of both input expressions or numbers.

Both arguments must agree in units, which are then the units of the result; a plain 0 is accepted irrespective of the unit of the other argument. Differentiates branch-wise, as dx*heaviside(x-y)+dy*heaviside(y-x).

Parameters:
Returns:

max(x,y).

Return type:

Expression

pyoomph.expressions.minimum(x, y)[source]

Compute the minimum of both input expressions or numbers.

Both arguments must agree in units, which are then the units of the result; a plain 0 is accepted irrespective of the unit of the other argument. Differentiates branch-wise, as dx*heaviside(y-x)+dy*heaviside(x-y).

Parameters:
Returns:

min(x,y).

Return type:

Expression

pyoomph.expressions.partial_x(f, order=1)[source]

Compute the partial derivative of a given expression with respect to the x-coordinate.

It differentiates with respect to the independent Eulerian coordinate "coordinate_x", i.e. it is a shorthand for diff() with respect to that coordinate. On a moving mesh, use grad() instead if you want the mesh coordinates to be used.

Parameters:
  • f (Expression | GiNaC_GlobalParam | int | float) – The expression to differentiate.

  • order (int) – The order of differentiation (default is 1).

Returns:

The resulting expression after differentiation.

Return type:

Expression

pyoomph.expressions.partial_y(f, order=1)[source]

Compute the partial derivative of a given expression with respect to the y-coordinate.

It differentiates with respect to the independent Eulerian coordinate "coordinate_y", i.e. it is a shorthand for diff() with respect to that coordinate. On a moving mesh, use grad() instead if you want the mesh coordinates to be used.

Parameters:
  • f (Expression | GiNaC_GlobalParam | int | float) – The expression to differentiate.

  • order (int) – The order of differentiation (default is 1).

Returns:

The resulting expression after differentiation.

Return type:

Expression

pyoomph.expressions.partial_z(f, order=1)[source]

Compute the partial derivative of a given expression with respect to the z-coordinate.

It differentiates with respect to the independent Eulerian coordinate "coordinate_z", i.e. it is a shorthand for diff() with respect to that coordinate. On a moving mesh, use grad() instead if you want the mesh coordinates to be used.

Parameters:
  • f (Expression | GiNaC_GlobalParam | int | float) – The expression to differentiate.

  • order (int) – The order of differentiation (default is 1).

Returns:

The resulting expression after differentiation.

Return type:

Expression

pyoomph.expressions.piecewise_geq0(cond, iftrue, iffalse)[source]

Returns a piecewise function that evaluates to iftrue when cond is greater than or equal to zero, and evaluates to iffalse otherwise.

All three arguments may carry units: cond only decides the branch, so its unit is arbitrary and divided out, whereas iftrue and iffalse must agree in units, which then are the units of the result. A plain 0 is accepted as a branch irrespective of the unit of the other branch.

Parameters:
Returns:

The resulting piecewise function.

Return type:

Expression

pyoomph.expressions.piecewise_gt0(cond, iftrue, iffalse)[source]

Strict counterpart of piecewise_geq0(): returns iftrue when cond is strictly greater than zero and iffalse otherwise, i.e. the two differ only at cond being exactly zero.

Units are handled as in piecewise_geq0(), i.e. the unit of cond is arbitrary and divided out, whereas iftrue and iffalse must agree in units, which then are the units of the result.

Parameters:
Returns:

The resulting piecewise function.

Return type:

Expression

pyoomph.expressions.real_part(x)[source]

Compute the real part of the input expression or number.

Units are kept, i.e. the real part of a length is a length. Note that this assumes every field to be real-valued, so that only an explicit imaginary_i() makes a term imaginary.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The real part of the input.

Return type:

Expression

pyoomph.expressions.signum(x)[source]

Compute the signum of the input expression or number, i.e. 1 for x>0, -1 for x<0 and exactly 0 at x=0.

The argument may carry any unit (only its sign matters), the result is dimensionless. Like heaviside(), it differentiates to zero everywhere, i.e. the jump at the origin does not enter the Jacobian.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The signum of the input.

Return type:

Expression

pyoomph.expressions.sin(x)[source]

Compute the sine of the input expression or number.

The argument must be dimensionless.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The sine of the input.

Return type:

Expression

pyoomph.expressions.sinh(x)[source]

Compute the hyperbolic sine of the input expression or number.

The argument must be dimensionless.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The hyperbolic sine of the input.

Return type:

Expression

pyoomph.expressions.square_root(what, order=2)[source]

Calculates the square root, or with order the order-th root, of the given expression or number.

The argument may carry a unit, which is raised to the power 1/order along with it, i.e. the root of an area is a length. It is just a shorthand for what**rational_num(1,order), hence an exact rational exponent rather than a floating point one, which matters for the unit bookkeeping.

Parameters:
  • what (Expression | GiNaC_GlobalParam | int | float) – The expression or number to calculate the square root of.

  • order (int) – The order of the square root. Default is 2.

Returns:

The square root of the given expression or number.

Return type:

Expression

pyoomph.expressions.sym(a)[source]

Calculate the symmetric part of a given matrix.

Parameters:

a (Expression) – The input matrix expression.

Returns:

The symmetric part of the matrix.

Return type:

Expression

pyoomph.expressions.tan(x)[source]

Compute the tangent of the input expression or number.

The argument must be dimensionless. Nothing guards against the poles at odd multiples of pi/2.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The tangent of the input.

Return type:

Expression

pyoomph.expressions.tanh(x)[source]

Compute the hyperbolic tangent of the input expression or number.

The argument must be dimensionless.

Parameters:

x (Expression | GiNaC_GlobalParam | int | float) – The input expression or number.

Returns:

The hyperbolic tangent of the input.

Return type:

Expression

pyoomph.expressions.time_derivative_of_integral(expr, scheme='BDF2_degr', apply_on_others=True)[source]

Computes the time derivative of an integral expression using a given time stepping scheme. For moving meshes, this can be different, i.e. weak(partial_t(u),v) != d_by_dt_of_integral(weak(u,v)). The latter is d/dt (Integral_Element(t) u(t)*v(t)*dx ), i.e. considers the change of the element size as well.

Parameters:
  • expr (Expression | GiNaC_GlobalParam | int | float) – The expression to differentiate.

  • scheme (Literal['BDF1', 'BDF2', 'Newmark2', 'BDF2_degr', 'Newmark2_degr', 'TPZ', 'MPT', 'Simpson', 'Boole', 'trapezoidal', 'Kepler', 'Milne', 'midpoint']) – The time stepping scheme to apply (“BDF1”,”BDF2”,”Newmark2”,”BDF2_degr”,”Newmark2_degr”). Defaults to “BDF2_degr”, “_degr” means that the time derivative is approximated with a lower order scheme in the first step, since initial conditions might not have history values.

  • apply_on_others (bool) – Whether the history evaluations also take the normal, the Eulerian element sizes and the Eulerian spatial derivatives in grad/div from the mesh of the corresponding history step. Defaults to True: this is a derivative of the integral over the moving element, so each history term belongs to the configuration the element had then. Has no effect unless the mesh moves.

Return type:

Expression

pyoomph.expressions.trace(M)[source]

Compute the trace of a matrix expression.

Parameters: M (Expression): The matrix expression for which to compute the trace.

Returns: Expression: The trace of the matrix expression.

Return type:

Expression

pyoomph.expressions.var_and_test(n, tag=[], domain=None)[source]

Bind a variable of an unknown field the corresponding test function for a given name.

Parameters:
  • n (str) – The name of the unkown.

  • tag (list[str]) – List of tags for the variable and test function. Defaults to [], see var()

  • domain (None | str | FiniteElementCodeGenerator) – The domain of the variable and test function. Defaults to None, see var()

Returns:

A tuple containing the field and test function as expressions.

Return type:

tuple[Expression, Expression]