SargonCZ / laser-calculator

Simple calculator for optics and laser physics
MIT License
4 stars 1 forks source link
laser optics python

Laser and optics calculator

This is a simple calculator, which quickly allows one to calculate various functions and solve equations common in the optics and laser community. The two main things I have focused on are:

  1. Units - all the calculations are working with the physical units that can be chosen by the users.
  2. Extendibility - it is relatively simple to add a new function, change the available one, add allowed units and so on.

Additionally, when the calculated or solved function is linear, one can add an uncertainty to the values, which will be propagated into the results.

Nice online calculators, that can do similar stuff, are these:

In contrast to those, however, you can quickly and simply add anything you need to this one 👍.

If you don't know or use python and would like to just use the program, you can check the Releases section, or you can directly download the installer or the portable version.


Table of contents


Application usage

List of available function

New functions can be easily added by following the section Adding new functions.

Starting the application

To start the application by running laser-calculator.py, you need to have python installed and following packages have to be available: numpy, pint, uncertainties and sympy. In case you are using conda package manager, you can set up the environment like this and then start the program like this.

If you have installed the program using the .msi installer, you can start the program by double-clicking on the desktop icon .

Basic overview

After the application is started, following window appears:

Opening window

There is a dropdown menu, from which you can choose a function to calculate or an equation to solve. Short instructions of use are added below the dropdown menu.

After the selction of one of the functions, the program will rearange itself accordingly. First, let's take a look on the laser peak intensity calculation as an example of the calculate regime.

Calculate regime

After the selection of the desired function, window similar to the following one will appear:

Peak intensity on start

The calculate regime can be recognized by the Calculate button and by the possibility to choose the units of the output variable.

After the input variables are filled and the units chosen, you can click the Calculate button to obtain the result in your chosen units. The program will look like this:

Peak intensity calculated

In the label with a big font, the result is shown with the units. Below is an entry, from which you can simply copy the result as a number.

Solve equation regime

Sligthly different is the solve regime, as there is no predetermined output. You have to choose the desired output variable by writing x instead of its value. For example, let's take a look at beam divergence calculation:

Beam divergence on start

One can for example want to calculate the beam divergence from the beam quality and beam waist diameter at a particular wavelength. Or one can measure the beam divergence and then find out, how small is the beam diameter in waist. Both of these can be calculated like on the following pictures:

Beam divergence with divergence calculated Beam divergence with diameter calculated

Using uncertainties

If you have a measured quantity with an uncertainty, you can add this error after the magnitude by using +- like on the following picture:

Peak intensity with uncertainty

This works only for linear function (meaning no sin and similar):

Constants in the function

If there are some constants associated with the functions, they will be displayed in a list on the right part of the program, like this:

Showing associated constants

These constants can be easily changed or added, the process in described in this section.

History

History of calculations can be shown by clicking on the Options menu and then Show history button.

Options menu

Alternatively, one can use Ctrl+H shortcut. The history will be shown in separate window with four columns, showing the date and time of calculation, the name of the function, inputs and outputs, as is demonstrated on the following picture:

History

The history is saved in the history.json file and can be cleared by deleting the file or by clicking the Clear history button in the Options menu.

Displaying help

Help menu

If you need help, you can display the .html version of this README file by pressing F1 key or navigating to the Help menu and clicking on Show readme item.

You can also visit the project page (this one) with clicking on the Show on GitHub item in the Help menu.

Check for updates

You can check for updates from the last item in the Help menu. This will check your version from the current version in the main branch of the project GitHub repository, and if the versions are different, you can be redirected to the webpage to download the new version manually.

Starting from .bat file with conda

If you have conda installed and use the appropriate conda environment, and you would like to quickly start the program just by double-clicking some icon, you can create a .bat file in the notepad for example. In this file, something similar to the following command should be written:

Call C:\Users\user.name\Anaconda3\Scripts\activate.bat & conda activate laser-calculator & cd "C:\Users\user.name\Documents\Python\laser-calculator" & python "C:\Users\user.name\Documents\Python\laser-calculator\laser-calculator.py"
pause

Of course, you have to change the user.name and the file locations. This will first activate conda in the open command prompt, next it will activate the respective python environment. Afterwards, it will change the working directory to the folder, where the program is saved, and finally, it will start the program. You can of course use any other method. For this to work, you theoretically don't need to have python and conda added in your system path.


Adding new functions

New function can be added to the functions.json file. The program offers the option to directly open and edit the functions.json file in the default editor by selecting the (Re)define functions from the Edit menu.

Edit menu

The structure of the file is described below. All the function equations are evaluated by the python script with the use of eval or exec, which can make some mess, if you write something else than the intended function there. The program is not very strict in controlling what does it evaluate, since we are all consenting adults.

Each function can have an assigned equation saved as a png figure, which is displayed in the program. The equations are generated by webpage https://latex.codecogs.com/legacy/eqneditor/editor.php, downloaded in png format and saved in the formulas folder with the same name, as is the name of the function definition. The folder can be opened from the Edit menu.

If you want me to add a new function, you can open an issue. Preferably, attach also the formula, description and other parts yourself. You can of course fork the repository and eventually open a pull request.

Structure of the functions.json file

How does the definition of one sample function look like in the functions.json file is shown below.

"duration-bandwidth" : {
        "name" : "(Lasers) Pulse length-bandwidth",
        "regime" : "calc",
        "description" : "Calculates the minimal pulse duration based on a spectral bandwidth.",
        "function" : "I[2]*I[0]**2/(I[1]*299792458*self.ureg('m/s'))",
        "inputs": {
            "Wavelength" : {
                "position" : 0,
                "units" : [
                    "um",
                    "nm"
                ]
            },
            "Bandwidth" : {
                "position" : 1,
                "units" : [
                    "um",
                    "nm"
                ]
            },
            "TBP" : {
                "position" : 2,
                "units" : [
                ]
            }
        },
        "outputs" : {
            "name" : "Pulse duration",
            "units" : [
                "ns",
                "ps",
                "fs"
            ]
        },
        "constants" : [
            "TBP"
        ]
    },

The functions.json file consists of a dictionary of definitions similar to the one above. The key of the particular dictionary item (in this case "duration-bandwidth") is the name of the definition, used also as a filename for the png equation. The value of this dictionary is the function definition, which is a dictionary in itself, with the following keys:

If the regime of the calculation is "solve", following changes take place:

        "function" : "sympy.Eq(I[3],I[2]*I[0]**2/(I[1]*I[4]))",
        "variables": {
            "a" : {
                "name" : "Wavelength",
                "position" : 0,
                "units" : [
                    "um",
                    "nm"
                ]
            },
            "b" : {
                "name" : "Bandwidth",
                "position" : 1,
                "units" : [
                    "um",
                    "nm"
                ]
            },
            "c" : {
                "name" : "TBP",
                "position" : 2,
                "units" : [
                ]
            },
            "d" : {
                "name" : "Pulse duration",
                "position" : 3,
                "units" : [
                    "ns",
                    "ps",
                    "fs"
                ]
            },
            "e" : {
                "name" : "c",
                "position" : 2,
                "units" : [
                    "m/s"
                ],
                "value" : 299792458
            }
        },

In the program, this last variable will be shown, but it will be in readonly or disabled state, like it is shown in the next figure:

Variable in solve regime

Structure of the constants.json file

The program also offers the option to directly open and edit the constants.json file in the default editor by selecting the (Re)define constants from the Edit menu. Currently, the constants.json file looks like this:

{
  "n2": {
    "air, 308 nm, 1 atm": "1.22e-22 m^2/W",
    "fused silica, 1030 nm": "2.19e-20 m^2/W",
    "calcium fluoride (CaF2)": "1.71e-20 m^2/W",
    "BBO, 532 nm": "~8e-20 m^2/W",
    "LBO, 850 nm": "1.9e-20 m^2/W",
    "YAG, 1064 nm": "6.2e-20 m^2/W"
  },
  "TBP": {
    "Gaussian": "0.441271",
    "sech": "0.314833",
    "Lorentzian": "0.141999"
  }
}

It is a dictionary of constants defined by the keys (names, referenced in the definition of the functions) and values, which can take any form following the "name" : "value" convention and will be displayed in the program, when the associated functions are chosen.


Local development

The GUI and the calculation part is writen in Python. Version 3.8.10 was used for the development.

The instructions below presume you have cloned this git repository you are in a command line (bash, cmd etc.) in the root directory of this project.

Setting up conda environment

For the first time, create a new laser-calculator conda environment with necessary packages using

conda env create -f environment.yaml

This environment must be updated when the environment.yaml file changes using

conda env update -f environment.yaml

Every time you are working on the project, activate the environment using

conda activate laser-calculator

Packages

If you are not using conda, below is the list of packages you need in order to start the program (version added for completness):

Building and distribution

You can build the application using cx_Freeze package and running python setup.py build and python setup.py bdist_msi.

Updating README

If you update this file, you should also update the .html version of the file with the use of pandoc. It is made simple by running the make_html_pandoc.bat file.