# The Script Language in Software from RP Photonics

All the software products of RP Photonics work with a powerful script language, which is briefly described here.
Whereas in **RP Q-switch** you can only utilize the mathematical functions, and **RP Fiber Calculator** is limited in a similar way, all other products can execute complex scripts which allow for full-blown programming by the user.
There is even an integrated powerful debugger.

Some people asked whether they can use **MATLAB**, for example, for processing of inputs or outputs of RP Software.
The answer:

- Sure, you can do any such data imports or exports, as our software is extremely flexible.
- On the other hand, you will normally not need any additional tool for data processing or graphical output, as you might as well (and probably more conveniently) do everything within our software.

In the end, you might completely work without MATLAB or similar, even in applications not related at all to the physics simulations.

## Typical Applications

The script language can be used for many purposes. Some examples are:

## Get Tips & Tricks

- You can
**pre-process input data**. You can calculate the inputs for some simulation from inputs given in a different form. Apply any set of equations or even iterative procedures to process your data. - Similarly, you can
**post-process output data**. For example, if**RP Coating**produces the design of a multilayer structure, you may output that in any form – say, with thickness values in any units, in reversed order, only those layers consisting of a certain material, or whatever you need. As another example, you may simulate the amplification of many ultrashort pulses under influences of amplifier noise in**RP Fiber Power**and statistically process the data to obtain noise spectra. **Generate diagrams**as needed. For example, plot any quantity as a function of any other quantity, or make color diagrams, where the color of each pixel is calculated according to its coordinates.**Handle files**in the most flexible manner. Read in files with any known text or binary format and get your inputs from those. For example, read measured data from all files matching a mask like**Data*.dat**in order to plot them together with your calculated data. Similarly, write outputs into files with any text or binary format.**Control other programs from our software**, i.e., call such programs and supply certain data to them. For example, you could write data to an html file and call your web browser to display it.

## The Arithmetics

Our software provides very powerful arithmetics. Some key features:

- There is a wide range of mathematical standard functions e.g. for trigonometry, logarithms, random numbers (e.g. with Gaussian probability distribution), etc.
- Complete complex arithmetics are offered.
- There are multidimensional arrays with linear, quadratic and spline interpolation. For example, read measured data points into an array and interpolate the data with cubic splines. Also, do multi-dimensional fast Fourier transforms (FFT).
- There are various functions for numerical purposes, such as finding roots, minima or maxima, derivatives and integrals, and even for integrating differential equations.
- There are functions to process whole arrays in one go; e.g., multiply each element of an array with the corresponding value in another array, or sum up all elements of an array, or find the maximum in an array.

## Processing of Strings

- You can handle strings of basically arbitrary length, limited only by your computer's main memory.
- There are many string functions, e.g. for getting or replacing parts of strings, converting strings to upper or lower case, formatting date and time values, etc.
- You can even use regular expressions for finding, replacing and extracting text parts. For example, you could easily extract all h1 headings from an html file.
- Numerical values can be formatted as strings, e.g. with a given number of digits and given units.

## Control Structures

Even within mathematical expressions, you can control the flow of operation with

- if-then-else statements
- while-do, repeat-until and for-do loops
- goto statements (where jumping labels can be calculated)

For example, in **RP ProPulse** or **RP Fiber Power** you may propagate an ultrashort pulse through a laser resonator until the pulse energy, duration and bandwidth do no more change significantly, i.e., the steady state has been reached.

## Files

You can read and write files with arbitrary text or binary format (e.g. MATLAB files) – variable by variable for complex formats, or all into one numerical array or string. For text files, you can also exploit the powerful string functions, including the use of regular expressions. For example, with just a few lines of code you may load an XML file and extract the contents of a certain tag.

## Specialized Functions for Physics

Each program supports specialized functions for the relevant physics simulations.
For example, **RP Fiber Power** offers far over 100 functions for purposes like changing input powers of a fiber device, extracting calculated powers and excitation densities, starting dynamical simulations or ultrashort pulse propagation, etc.
Such functions can then be used within any mathematical expression, for example for the ordinate values in a function plot.

## User-defined Functions

Define your own functions for all sorts of purposes.
For example, in **RP Fiber Power** you may define a function which first generates a Gaussian pulse with a given energy and duration, then propagates that pulse through the fiber, and finally returns the duration of the output pulse.
In **RP Coating**, there are functions for modifying layer thickness values and for extracting all optical properties of the coating.
Such functions can then be used, for example, in plots.

Functions, and even any begin-end block within any expression, can contain local variables, avoiding unintended interactions with global variables.

## Numerical Optimization

You may numerically optimize some user-defined figure-of-merit function, using either local optimization with a gradient method or a Monte-Carlo algorithm, starting with many random sets of parameters. Your figure-of-merit function can set values in your physical model, utilize any calculated results, and put them together to describe exactly what you need to optimize.

## Graphics

With a few commands, you can create a diagram:

- Define a coordinate system with lots of options to control its exact appearance.
- Define function plots and parametric plots, or color plots.
- Draw points as rectangles, triangles, circles, stars, etc., for example in order to indicate data points.
- Write text into diagrams, with calculated values, font types and sizes, etc.

## New: Debugger

In 2017, an important new feature has been introduced: a powerful debugger, which makes it much easier to locate and fix errors in sophisticated scripts. With that, you can execute scripts command by command. In addition, it is even possible to do efficient debugging within expressions. And of course you can inspect the detailed state of the system at any point.

The details are explained on the **page on debugging**.

## Advantages of Scripting

The use of scripts instead of a purely graphical user interface are manifold:

- The extremely powerful scripting features give you an extraordinary flexibility. You can do real programming in order to implement all sorts of algorithms. The detailed documentation, the demo files and the technical support make sure that you will be successful.
- The scripts naturally document all your work. If you read your script a year later, you will clearly see what you have done. It would be much more tedious to inspect dozens of interactive forms in order to reconstruct that.
- You can easily reuse some script parts (of your own or from demo scripts), either by copying them into other scripts or by making include files.

In **RP Fiber Power**, you can even start your work with interactive forms, and the script code is created automatically.
Additional script commands can be inserted via the forms.
At any point, you may take the obtained script and edit that further.

For additional information, please see some postings in our newsletters:

**Article of 2015-10-31**: “The Powerful Expression Engine in Software from RP Photonics”**Article of 2015-10-01**: “Making Plots With a Variation of Operation or System Parameters”**Article of 2015-07-13**: “Regular Expressions – a Powerful Mechanism for Search & Replace Operations and Input Validation”**Article of 2014-11-24**: “Some Tips Concerning the Script Language of RP Software”