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:
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.
.bat
file with condafunctions.json
fileconstants.json
fileNew functions can be easily added by following the section Adding new functions.
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 .
After the application is started, following window appears:
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.
After the selection of the desired function, window similar to the following one will appear:
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:
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.
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:
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:
If you have a measured quantity with an uncertainty, you can add this error after the magnitude by using +-
like on the following picture:
This works only for linear function (meaning no sin
and similar):
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:
These constants can be easily changed or added, the process in described in this section.
History of calculations can be shown by clicking on the Options
menu and then Show history
button.
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:
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.
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.
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.
.bat
file with condaIf 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.
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.
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.
functions.json
fileHow 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:
"name"
is the name shown in the program. Should be short and descriptive and can also contain a category for quick orientation."regime"
can be either "calc"
or "solve"
. First, I will describe the "calc"
regime and then, I will shown the differences for the "solve"
regime."description"
is the extended description also shown in the program."function"
is the function to be evaluated. Each of the variables is represented by I[ind]
, where ind
is defined in the "inputs"
. If there is a constant with a unit, as in the example above, the unit can be added by multiplying the magnitude with ureg('unit')
. The function represents the right side of the particular equation, if the left side is the desired output."inputs"
is a dictionary of input variables, each defined by a key (name shown in the program) and values of "position"
(starting from zero and corresponding to the index ind
in the function definition) and a list of allowed units."outputs"
defines a name and allowed units for the result of the calculation."constants"
- if there are some constants in the calculation (like the time-bandwidth product in the example above), it can be written in the optional key "constants"
. Multiple constants can be added. The respective values for the constants have to be added to the constants.json
, which is described in the next section.If the regime of the calculation is "solve"
, following changes take place:
"function"
definition is now an equation in the form of "sympy.Eq(
+ left-hand side + ,
+ right-hand side + )"
."variables"
- all variables used in the function definition are listed under the "variables"
keys, and "inputs"
and "output"
keys are missing. Every variable has an associated name, position (corresponding to the index ind
) and units. If there is some constant with units in the equation, it can be added as an additional variable with it's value filled in, as is done in the following example (the last variable, speed of light). "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:
constants.json
fileThe 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.
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.
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
If you are not using conda, below is the list of packages you need in order to start the program (version added for completness):
You can build the application using cx_Freeze
package and running python setup.py build
and python setup.py bdist_msi
.
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.