Difference between revisions of "User Defined Functions"

From OSUPDOCS
Jump to navigation Jump to search
Line 3: Line 3:
== Function Variables ==
== Function Variables ==


When a function option is allowed in any command, you can enter any valid function of the following variables. Note that commands that allow functions may only allow a subset of these variables (due to command context). You can refer to each command for the allowed variables. When a function is used, it will be calculated using these variables and should return a results in the units expected by the command.These variables specify position and may refer for particle (in MPM), element centroid (in FEA), or node (in FEA) positions:
When a function option is allowed in any command, you can enter any valid function of the following variables when doing MPM simulations:


* <tt>x</tt> - <tt>x</tt> position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]
* <tt>x</tt> - <tt>x</tt> position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]
* <tt>y</tt> -  <tt>y</tt> position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]
* <tt>y</tt> -  <tt>y</tt> position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]
* <tt>z</tt> -  <tt>z</tt> position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]
* <tt>z</tt> -  <tt>z</tt> position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]
* <tt>t</tt> - current time in [[ConsistentUnits Command#Legacy and Consistent Units|alt time units]]
* <tt>dt</tt> - the time step in [[ConsistentUnits Command#Legacy and Consistent Units|alt time units]]
* <tt>q</tt> - particle rotation in radians about the <tt>z</tt> (or &theta; if axisymmetric) axis
For FEA calculations, the following variables are allowed (hese variables may refer to positiong of element centroid or node (in FEA) depending on the command):
* <tt>x</tt> - <tt>x</tt> position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]
* <tt>y</tt> -  <tt>y</tt> position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]
* <tt>r</tt> - radial position of axisymmetric calculations in [[ConsistentUnits Command#Legacy and Consistent Units|length units]] (synonym for <tt>x</tt>).
* <tt>z</tt> -  axial position of axisymmetric calculations in [[ConsistentUnits Command#Legacy and Consistent Units|length units]] (synonym for <tt>x</tt>)
When a function is used, it will be calculated using these variables and should return a result in the units expected by the command.
Note that commands that allow functions may only allow a subset of these variables (due to command context). You can refer to each command for the allowed variables. For example, some MPM options (as detailed in their documentation) require the function to depend ''only'' on time. Particle-based MPM boundary conditions let the function depend on clockwise particle rotational angle <tt>q</tt> about the <tt>z</tt> axis (in radians), which allows rotation of the boundary conditions with the particle. Note that <tt>q</tt> is particle rotation since the start of the simulation and will differ from material angle if the particle started with a non-zero orientation angle (''i.e.'', the current material angle is the sum of <tt>q</tt> and its initial angle). 
When setting up MPM simulations or FEA calculations, a few more variables are sometimes allowed:
* <tt>R</tt> - radial position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]] for axisymmetric calculation; <tt>R</tt> is a synonym for <tt>x</tt>, which also works
* <tt>R</tt> - radial position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]] for axisymmetric calculation; <tt>R</tt> is a synonym for <tt>x</tt>, which also works
* <tt>Z</tt> - axial position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]] for axisymmetric calculation; <tt>Z</tt> is a synonym for <tt>y</tt>, which also works
* <tt>Z</tt> - axial position in [[ConsistentUnits Command#Legacy and Consistent Units|length units]] for axisymmetric calculation; <tt>Z</tt> is a synonym for <tt>y</tt>, which also works
* <code>D</code> - distance from origin (''i.e.'', r in polar coordinates).
* <code>D</code> - distance from origin.
* <code>T</code> - counter-clockwise angle (in radians) from the positive x axis  (''i.e.'', &theta; in polar coordinates)
* <code>T</code> - counter-clockwise angle (in radians) from the positive x axis  (''i.e.'', &theta; in polar coordinates)


If a polar function is more appropriate for 2D analyses, the function can depend on <tt>D</tt> and <tt>T</tt>, where <tt>D</tt> is distance from the origin to the <tt>(x,y)</tt> (or <tt>(R,Z)</tt> if axisymmetric) point (in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]) and <tt>T</tt> is counter-clockwise angle (in radians) from the positive <tt>x</tt> (or <tt>R</tt> if axisymmetric) axis to the point.
Here <tt>(R,Z)</tt> are axisymmetric coordinates and <tt>(D,T)</tt> are polar coordinates, where <tt>D</tt> is distance from the origin to the <tt>(x,y)</tt> (or <tt>(R,Z)</tt> if axisymmetric) point (in [[ConsistentUnits Command#Legacy and Consistent Units|length units]]) and <tt>T</tt> is counter-clockwise angle (in radians) from the positive <tt>x</tt> (or <tt>R</tt> if axisymmetric) axis to the point. The extra variables are allowed in:
 
The following variables are only allowed in MPM calculations:
 
* <tt>t</tt> - current time in [[ConsistentUnits Command#Legacy and Consistent Units|alt time units]]
* <tt>dt</tt> - the time step in [[ConsistentUnits Command#Legacy and Consistent Units|alt time units]]
* <tt>q</tt> - particle rotation in radians about the <tt>z</tt> (or &theta; if axisymmetric) axis
 
For example, some MPM options (as detailed in their documentation) require the function to depend ''only'' on time. Particle based MPM boundary conditions let the function depend on clockwise particle rotational angle <tt>q</tt> about the <tt>z</tt> axis (in radians), which allows rotation of the bondary conditions with the particle. Note that <tt>q</tt> is particle rotation since the start of the simulation and will differ from material angle if the particle started with a non-zero orientation angle (''i.e.'', the current material angle is the sum of <tt>q</tt> and its initial angle).


== Function Format ==
== Function Format ==

Revision as of 10:25, 19 August 2019

Some commands let you set properties using a function that is evaluated at run time.

Function Variables

When a function option is allowed in any command, you can enter any valid function of the following variables when doing MPM simulations:

For FEA calculations, the following variables are allowed (hese variables may refer to positiong of element centroid or node (in FEA) depending on the command):

When a function is used, it will be calculated using these variables and should return a result in the units expected by the command. Note that commands that allow functions may only allow a subset of these variables (due to command context). You can refer to each command for the allowed variables. For example, some MPM options (as detailed in their documentation) require the function to depend only on time. Particle-based MPM boundary conditions let the function depend on clockwise particle rotational angle q about the z axis (in radians), which allows rotation of the boundary conditions with the particle. Note that q is particle rotation since the start of the simulation and will differ from material angle if the particle started with a non-zero orientation angle (i.e., the current material angle is the sum of q and its initial angle).

When setting up MPM simulations or FEA calculations, a few more variables are sometimes allowed:

  • R - radial position in length units for axisymmetric calculation; R is a synonym for x, which also works
  • Z - axial position in length units for axisymmetric calculation; Z is a synonym for y, which also works
  • D - distance from origin.
  • T - counter-clockwise angle (in radians) from the positive x axis (i.e., θ in polar coordinates)

Here (R,Z) are axisymmetric coordinates and (D,T) are polar coordinates, where D is distance from the origin to the (x,y) (or (R,Z) if axisymmetric) point (in length units) and T is counter-clockwise angle (in radians) from the positive x (or R if axisymmetric) axis to the point. The extra variables are allowed in:

Function Format

Some details on entering functions are:

  • In scripted files, the function must be enclosed in quotes (e.g., "x^2+y^2") to prevent it from being evaluated as a command expression prior to being used in the analysis.
  • Enter variables simply as x, y, etc., and not with the preceding "#" used for command expression variables.
  • Operators: The function uses standard operators + - * / and ^ with standard operator precedence for addition, subtraction, multiplication, division, and raising to a power.
  • The function can contain the following defined functions:
    • sin(x), cos(x), and tan(x) - trigonometric function of angle in radians.
    • asin(x), acos(x), and atan(x) - inverse trigonometric function with result in radians.
    • sqrt(x) - square root.
    • log(x) and ln(x) - log base 10 and natural log, respectively (note these are different than log functions used in command expressions in scripted files due to different math expression parser in the code engines).
    • exp(x) - exponential of x.
    • abs(x) - absolute value of x.
    • int(x) - integer part of x as next lower integer. For negative numbers, it is next lower integer or int(-3.4)=-4.
    • Sinh(x), Cosh(x), and Tanh(x) - hyperbolic trigonometric functions. (note the initial uppercase is needed here, and differs from scripted command expressions due to different math expression parser in the code engines).
    • erf(x) and erfc(x) - error function and error function complement.
  • The following functions are helpful if creating boundary conditions:
    • [math]\displaystyle{ {\rm ramp}(A,x) = \left\{\begin{array}{ll} 0 & x\le 0 \\ Ax & 0\lt x\lt 1 \\ A & x \ge 1 \end{array}\right. }[/math]
    • [math]\displaystyle{ {\rm cosramp}(A,x) = \left\{\begin{array}{ll} 0 & x\le 0 \\ {A\over 2}\left(1-\cos(\pi x)\right) & 0\lt x\lt 1 \\ A & x \ge 1 \end{array}\right. }[/math]
    • [math]\displaystyle{ {\rm box}(A,x) = \left\{\begin{array}{ll} 0 & x\le 0 \\ A & 0\lt x\lt 1 \\ 0 & x \ge 1 \end{array}\right. }[/math]
    • [math]\displaystyle{ {\rm sinbox}(A,x) = \left\{\begin{array}{ll} 0 & x\le 0 \\ A\sin(\pi x) & 0\lt x\lt 1 \\ 0 & x \ge 1 \end{array}\right. }[/math]
    • [math]\displaystyle{ {\rm sgn}(x) = \left\{\begin{array}{ll} -1 & x \lt 0 \\ 0 & x = 0 \\ +1 & x \gt 0 \end{array}\right. }[/math]
    • [math]\displaystyle{ {\rm sign}(x) = \left\{\begin{array}{ll} 0 & x \lt 0 \\ 1 & x \ge0 \end{array}\right. }[/math]
    • [math]\displaystyle{ {\rm tri}(x) = \left\{\begin{array}{ll} 1-|x| & |x| \lt 1 \\ 0 & |x| \ge1 \end{array}\right. }[/math]

    For example, to apply a ramp from 0 to tf, use x = t/tf; to apply a ramp from ts to tf,use x = (t-ts)/(tf-ts). The new functions (all except sign(x)) are only available in versions after about September 1, 2018.

  • Functions can include pi (or Pi or PI) for the number π.
  • Exponential Notation: numbers can have "e" or "E" for powers of ten such as 1.4e3 for 1400.
  • Extra spaces in the function are ignored.

Notes

A common use for cosramp(A,x) is to ramp from zero velocity to V over time 0 to tr, which is done by setting A = V and x = t/tr. Integrating this velocity, the resulting displacement is:

    [math]\displaystyle{ d(t) = \left\{\begin{array}{ll} {V\over 2}\left(t-{t_r\over \pi}\sin\left({\pi t\over t_r}\right)\right) & t\lt t_r \\ V\left(t-{t_r\over 2}\right) & x \ge t_r \end{array}\right. }[/math]

In terms of defined functions, this displacement can be written

    [math]\displaystyle{ d(t) = {V\over2}\left[t + (t-t_r){\rm sign}(t-t_r) - {\rm sinramp}\left({t_r\over\pi},{t\over t_r}\right)\right] }[/math]

For example, to ramp to V and then hold constant velocity until reaching desired displacement dmax, the simulation time should be set to:

    [math]\displaystyle{ t_{max} = {d_{max}\over V} + {t_r\over 2} }[/math]

The first term is the simulation time using constant velocity while the second term is the extra time needed because the velocity was ramped to velocity V over time tr.