Difference between revisions of "User Defined Functions"

From OSUPDOCS
Jump to navigation Jump to search
Line 1: Line 1:
This document needs conversion to Wiki format
Some commands let you set properties using a function that is evaluated at run time.


<p>Some commands let you set properties using a function. When a function option is allowed, enter a valid function of <code>x</code>, <code>y</code>, <code>z</code> (postions in mm), <code>R</code>, <code>Z</code> (synonyms for <code>x</code> and <code>y</code> if axisymmetric), <code>t</code> (time in ms), <code>dt</code> (time step in ms), <code>q</code> (particle rotation in radians about the <code>z</code> (or &theta; if axisymmetric) axis), <code>D</code>, or <code>T</code> (polar coordinates in <code>x</code>-<code>y</code> (or <code>R</code>-<code>Z</code> if axisymmetric) plane in mm (D) and radians (T)), . The function will be evaluated to calculate the value of the property to be assigned. Not all variables can be used for all functions. Refer the command that allows a function to see what variables are allowed.</p>
== Function Variables ==


<p>(also?) For 3D MPM, <code>z</code> refers to the <code>z</code> coordinate. For 2D axisymmetric analyses, <code>R</code> and <code>Z</code> refer to radial and axial positions in the 2D plane (although <code>x</code> and <code>y</code> will work too). If a polar function is more appropriate for 2D analysis, the function can be of <code>D</code> and <code>T</code>, where <code>D</code> is distance from the origin to the <code>(x,y)</code> point (in mm) and <code>T</code> is counter-clockwise angle (in radians) from the positive <code>x</code> axis to the point.
When a function option is allowed in any command, you can enter any valid function of the following variable
</p>


<p> The time options is for MPM options that allow the function to depend on time. Some MPM options (as detailed in their documentation) require the function to depend <i>only</i> on time. Particle based MPM boundary conditions let the function depend on clockwise particle rotational angle <code>q</code> about the <code>z</code> axis (in radians), which allows rotation of the bondary conditions with the particle. Note that <code>q</code> 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 (the current material angle is the sum of <code>q</code> and its initial angle) (<font color="red">warning</font>: this definition of <code>q</code> was changed in NairnMPM 8.0.1, while before <code>q</code> was the sum of initial angle and rotation angle).
* <tt>x</tt> - particle <tt>x</tt> position in mm
</p>
* <tt>y</tt> - particle <tt>y</tt> position in mm
* <tt>z</tt> - particle <tt>z</tt> position in mm
* <tt>R</tt> - particle radial position in mm for axisymmetric calculation; <tt>R</tt> is a synonym for <tt>x</tt>, which also works
* <tt>Z</tt> - particle axial position in mm for axisymmetric calculation; <tt>Z</tt> is a synonym for <tt>y</tt>, which also works
* <code>D</code> - distance from origin in 2D calculations (''i.e.'', particle r in polar coordinates).
* <code>T</code> - counter-clockwise angle (in radians) from the positive x axis to a vector from the origin to the particle  (''i.e.'', particle &theta; in polar coordinates)
* <tt>t</tt> - current time in ms
* <tt>dt</tt> - the time step in ms
* <tt>q</tt> - particle rotation in radians about the <tt>z</tt> (or &theta; if axisymmetric) axis


<p>If a polar function is more appropriate for 2D analysis, the function can be of <code>D</code> and <code>T</code>, where <code>D</code> is distance from the origin to the <code>(x,y)</code> (or <code>(R,Z)</code> if axisymmetric) point (in mm) and <code>T</code> is counter-clockwise angle (in radians) from the positive <code>x</code> (or <code>R</code> if axisymmetric) axis to the point.
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.
</p>


<p>Some details on entering functions are:</p>
The time, time step, and rotation variables (<tt>t</tt>, <tt>dt</tt>, and <tt>q</tt>) options are for MPM commands only. Some MPM options (as detailed in their documentation) require the function to depend <i>only</i> 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 (the current material angle is the sum of <tt>q</tt> and its initial angle).
 
If a polar function is more appropriate for 2D analyses, the function can be of <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 mm) 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.
 
== Function Format ==
 
Some details on entering functions are:


<ul>
<ul>


<li>The function must be enclosed in quotes (<i>e.g.</i>, &quot;<code>x^2+y^2</code>&quot;) to prevent it from being evaluated as a <a href="expression.html#expr">command expression</a> prior to being used in the analysis.
<li>In scripted files, the function must be enclosed in quotes (<i>e.g.</i>, &quot;<code>x^2+y^2</code>&quot;) to prevent it from being evaluated as a command expression prior to being used in the analysis.
</li>


<li>Enter variables simply as <code>x</code>, <code>y</code>, <i>etc.</i>, and not with the preceding &quot;<code>#</code>&quot; used for <a href="expression.html#expr">command expression variables</a>.
<li>Enter variables simply as <tt>x</tt>, <tt>y</tt>, <i>etc.</i>, and not with the preceding "<tt>#</tt>" used for command expression variables.
</li>


<li>Operators: The function uses standard operators + - * / and ^ with standard operator precedence for addition, subtraction, multiplication, division, and raising to a power.
<li>Operators: The function uses standard operators + - * / and ^ with standard operator precedence for addition, subtraction, multiplication, division, and raising to a power.
</li>


<li>The function can contain the following defined functions:
<li>The function can contain the following defined functions:
<ul>
<ul>


<li><tt>sin(x)</tt>, <tt>cos(x)</tt>, and <tt>tan(x)</tt> - trigonometric function of angle in radians.


<li><code>sin(x)</code>, <code>cos(x)</code>, and <code>tan(x)</code> - trigonometric function of angle in radians.
<li><tt>asin(x)</tt>, <tt>acos(x)</tt>, and <tt>atan(x)</tt> - inverse trigonometric function with result in radians.
</li>
 
<li><code>asin(x)</code>, <code>acos(x)</code>, and <code>atan(x)</code> - inverse trigonometric function with result in radians.
</li>


<li><code>sqrt(x)</code> - square root.
<li><tt>sqrt(x)</tt> - square root.
</li>


<li><code>log(x)</code> and <code>ln(x)</code> - log base 10 and natural log, respectively (note these are different than log functions used in <a href="expression.html#expr">command expressions</a> due to different math expression parser in the code engines).
<li><tt>log(x)</tt> and <tt>ln(x)</tt> - 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).
</li>


<li><code>exp(x)</code> - exponential of <code>x</code>.
<li><tt>exp(x)</tt> - exponential of <tt>x</tt>.
</li>


<li><code>abs(x)</code> - absolute value of <code>x</code>.
<li><tt>abs(x)</tt> - absolute value of <tt>x</tt>.
</li>


<li><code>int(x)</code> - integer part of <code>x</code> as next lower integer. For negative numbers, it is next lower integer or <code>int(-3.4)=-4</code>.
<li><tt>int(x)</tt> - integer part of <tt>x</tt> as next lower integer. For negative numbers, it is next lower integer or <tt>int(-3.4)=-4</tt>.


<li><code>sign(x)</code> - 1 if <code>x</code> is greater than zero or 0 if <code>x</code> is negative or equal to zero.
<li><tt>sign(x)</tt> - 1 if <code>x</code> is greater than zero or 0 if <tt>x</tt> is negative or equal to zero.


<li><code>Sinh(x)</code>, <code>Cosh(x)</code>, and <code>Tanh(x)</code> - hyperbolic trigonometric functions.  (note the initial uppercase is needed here, and differs from <a href="expression.html#expr">command expressions</a> due to different math expression parser in the code engines).
<li><tt>Sinh(x)</tt>, <tt>Cosh(x)</tt>, and <tt>Tanh(x)</tt> - 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).
</li>


</ul>
</ul>


<li>Functions can include <code>pi</code> (or <code>Pi</code> or <code>PI</code>) for the number &pi;.
<li>Functions can include <tt>pi</tt> (or <tt>Pi</tt> or <tt>PI</tt>) for the number &pi;.
</li>


<li>Exponential Notation: numbers can have &quot;<code>e</code>&quot; or &quot;<code>E</code>&quot; for powers of ten such as <code>1.4e3</code> for <code>1400</code>.
<li>Exponential Notation: numbers can have "<tt>e</tt>" or "<tt>E</tt>" for powers of ten such as <tt>1.4e3</tt> for <cttde>1400</tt>.
</li>


<li>Extra spaces in the function are ignored.
<li>Extra spaces in the function are ignored.
</li>


</ul>
</ul>

Revision as of 11:40, 18 November 2013

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 variable

  • x - particle x position in mm
  • y - particle y position in mm
  • z - particle z position in mm
  • R - particle radial position in mm for axisymmetric calculation; R is a synonym for x, which also works
  • Z - particle axial position in mm for axisymmetric calculation; Z is a synonym for y, which also works
  • D - distance from origin in 2D calculations (i.e., particle r in polar coordinates).
  • T - counter-clockwise angle (in radians) from the positive x axis to a vector from the origin to the particle (i.e., particle θ in polar coordinates)
  • t - current time in ms
  • dt - the time step in ms
  • q - particle rotation in radians about the z (or θ if axisymmetric) axis

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.

The time, time step, and rotation variables (t, dt, and q) options are for MPM commands only. 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 bondary 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 (the current material angle is the sum of q and its initial angle).

If a polar function is more appropriate for 2D analyses, the function can be of D and T, where D is distance from the origin to the (x,y) (or (R,Z) if axisymmetric) point (in mm) and T is counter-clockwise angle (in radians) from the positive x (or R if axisymmetric) axis to the point.

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.
    • sign(x) - 1 if x is greater than zero or 0 if x is negative or equal to zero.
    • 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).
  • 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 <cttde>1400.
  • Extra spaces in the function are ignored.