File I/O and User Defined Functions — Lesson 1

Manipulating Project Files

In this video, you will learn about Lumerical script commands for manipulating project files. You can use these commands to create new project files, open existing ones, and save them.

Data in Text File Format

In this video, you will learn about Lumerical script commands for reading and saving the simulation data in a text file format.

Data in Binary File Format

In this video, you will learn about Lumerical script commands for reading and saving the simulation data in a binary file format.

User-Defined Functions

Function - Script Command

The function command enables the user to define their own Lumerical script functions, which is useful in writing more reusable, maintainable and understandable code.

The name of each function needs to be unique (unlike any other standard or new Lumerical commands).

Syntax Description
function function_name(input1, input2, ...){

....

return output;

}

The function keyword is used to define a function which can have arbitrary number of inputs and one output. The return statement is used to specify the output. The function body will be located in between the brackets.
out=function_name(input1, input2, ...); The name of the function is used to call the function and the inputs should be in the same format as the definition.

Here are some examples:

Defining a function that rounds down the input number to the nearest even number

function even_floor(a) {return floor(a) - mod(floor(a),2);}

This function will change the current working directory to the currently opened file directory.

function change_to_project_directory() {
if(currentfilename != "") {
cd(filedirectory(currentfilename ));
}
}

The user-defined functions can be defined within the main script file or in a separate file. To use a function defined in a separate file, begin by running the script file containing the function. Then the function is loaded and can be used.