D3PLOT 22.1

JavaScript File

JavaScript file

The third way of creating user-defined data components is via an externally defined JavaScript file. Each node or element runs a short JavaScript, the result of which becomes its value.

You can use the JavaScript API Reference Manual to access the full range of "built-in" data components, in the same way that you can with the Simple Formula option above. You can also access other element properties in this way via further data functions.

Using these capabilities, expressions can be evaluated in real-time outside D3PLOT, possibly with reference to external data, and then fed back in for plotting.

The JavaScript File method is limited in that it can only work on the current node or element, however it does provide a bit more functionality than Simple Formulae in that conditional ("if") statements can be used. This makes it possible, for example, to generate results from an internal lookup table.

For more complex processing it is recommended that you consider generating UBIN components using the main JavaScript API, see "User Defined Binary" (UBIN) Components Generated from the JavaScript API below for more information.

How the JavaScript user components work

  • You create a JavaScript file (by convention ".js") outside D3PLOT using standard expressions, and also references to built-in data components.
  • This is defined as the evaluation method for a user-defined component, currently limited to scalar components.
  • It is test compiled to check for syntax errors, and if it is OK the compiled script is stored as the evaluation method for this component
  • Thereafter nodes or elements run the compiled script on demand to obtain results.

The value used is the return value of the JavaScript function. By convention this is the result of the final executable statement.

The (generally small) JavaScript function is run for every element or node that requires data evaluation, so for a large model there may be some delay while all the values are computed.

Using built-in data components

The full range of data components used for Simple Formulae is available, with the following differences:

  • Each component is a function in JavaScript, not a variable. Therefore it uses the acronym in the Simple Formula table above followed by () . For example:

    sxx , X stress, is referred to as sxx() inside Javas
    Script. Note that the function has no arguments.

  • Variable components above, e.g. "extra" values such as sox , pass the variable as the function argument. For example:

    sox 3 , Solid Extra component #3, is referred to as sox(3) inside JavaScript.

Using maths functions

Maths functions in JavaScript are supplied by a "Math" class built into the language. For example:

Math.sin(x) evaluates sine (x)
Math.sqrt(y) evaluates the square root of (y)

Extra functions provided for JavaScript

At present these are limited to:

label() Returns the label of the item (eg node or element) currently being evaluated
pid() Returns the part number of the element currently being evaluated. Zero is returned if called for an item that does not reference a part.
print(args) Will write <args> to the controlling terminal.

Examples of JavaScript function

This function evaluates von Mises stress.

JavaScript
var i = (sxx() - syy());
var j = (syy() - szz());
var k = (szz() - sxx());

var l = Math.sqrt(i*i + j*j + k*k + 6 * (sxy()*sxy() + syz()*syz() + szx()*szx()));

var result = 1 / Math.sqrt(2) * l;

Here is another example used to handle a "what should I do if I get divide by zero" problem.

Triaxiality is defined as hydrostatic stress (Sxx + Syy + Szz) / 3.0 divided by deviatoric stress (Svon), which can be written (Sxx + Syy + Szz) / (3 * Svon). Obviously the case may arise that the deviatoric stress is zero, giving a "divide by zero" error, and indeed you may want to write some special value in this case, making a simple formula inadequate. A small JavaScript will solve the problem since it permits conditional branching, for example:

JavaScript
var a = svon();
var result;

if(a > 0.0) result = (sxx() + syy() + szz()) / (3.0 * a);

In this case a potential divide by zero returns 0.0, but you could substitute any value.

These first two examples both cover the case of using a JavaScript function to generate a scalar value. In order to generate a vector or tensor component, you will need to return an array or object, rather than a single numeric result. Here is a third example which demonstrates how to return an array for a vector component.

JavaScript
var a = new Array();

a[0] = dx();
a[1] = dy() * 1.5;
a[2] = dz() * 2.0;

var result = a;

Those used to Fortran or C programming should note that JavaScript is a very weakly typed language: numeric variables are "numbers", not integers, floats or other specific data types. Expressions are evaluated using double precision floating point arithmetic.