Package: cavcalc
A command line program and Python module for computing parameters associated with linear, Fabry-Perot optical cavities.Find the documentation at:https://cavcalc.readthedocs.io/en/latest/Follow the latest changes:https://gitlab.com/sjrowlinson/cavcalcSee the entry on PyPI:https://pypi.org/project/cavcalc/InstallationTo installcavcalc, simply run:pip install cavcalcCheck that the installation was successful with:cavcalc--versionif you see something along the lines ofcavcalc v1.2.0then you should be ready to start usingcavcalc!Note: As is often recommended with the installation of Python packages (especially those with dependencies
on packages such asnumpyandmatplotlib, as is the case here), you should prefer to installcavcalcin
a suitable virtual environment. Seethe official documentation on Python virtual environmentsfor details on how to set up these if you are unfamiliar with the topic.Example usage via the CLIFor details on available arguments runcavcalc -hon the command line.Some examples follow on how to usecavcalc. Seethe documentation on using cavcalcfor more in-depth examples and guides.Computing single parametersYou can ask for, e.g., the beam size on the mirrors of a symmetric cavity given its length and stability factor of the mirrors (gs) with:cavcalcw-L4000-gs0.91This would result in an output of:Given:
	Cavity length = 4000 m

	Stability g-factor of both mirrors = 0.91

	Wavelength of beam = 1064 nm

Computed:
	Radius of beam at mirrors = 57.16193267930482 mmUnits for both inputs and outputs can also be specified:cavcalcw-umm-L10km-gouy145degThis requests the beam radius (in mm) on the mirrors of a symmetric cavity of length 10km given that the
round-trip Gouy phase is 145 degrees; resulting in the following output:Given:
	Cavity length = 10 km

	Round-trip Gouy phase = 145 deg

	Wavelength of beam = 1064 nm

Computed:
	Radius of beam at mirrors = 59.59174828941794 mmSupport for units is provided via the packagePint, so any units
defined in the Pint unit-registry can be used in cavcalc.Computing all available parametersA compute target ofallis the default choice which is used to calculate all parameters which can be determined
from the arguments specified. For example, using approximate values of the Advanced LIGO arm cavity parameters,cavcalc-L4km-Rc11934-Rc22245-T10.014-L137.5e-6-T25e-6-L2L1gives the following output:Given:
	Loss of first mirror = 3.75e-05
	Loss of second mirror = 3.75e-05
	Transmission of first mirror = 0.014
	Transmission of second mirror = 5e-06

	Cavity length = 4 km

	Radius of curvature of first mirror = 1934 m
	Radius of curvature of second mirror = 2245 m

	Wavelength of beam = 1064 nm

Computed:
	FSR = 37474.05725 Hz
	FWHM = 84.56921734107604 Hz
	Mode separation frequency = 4988.072188176178 Hz
	Pole frequency = 42.28460867053802 Hz

	Finesse = 443.11699254426594
	Reflectivity of first mirror = 0.9859625
	Reflectivity of second mirror = 0.9999574999999999
	Internal resonance enhancement factor = 20036.317877295227
	External resonance enhancement factor = 281.2598122025325
	Fractional transmission intensity = 0.011953542018623487

	Position of beam waist (from first mirror) = 1837.2153886417168 m

	Radius of beam at first mirror = 53.421066433049255 mm
	Radius of beam at second mirror = 62.448079883230896 mm
	Radius of beam at waist = 11.950538458990879 mm

	Stability g-factor of cavity = 0.8350925761717987
	Stability g-factor of first mirror = -1.0682523267838677
	Stability g-factor of second mirror = -0.7817371937639199

	Round-trip Gouy phase = 312.0813565565169 deg

	Divergence angle = 0.0016237789746943276 degUnits of outputThe default behaviour for the output parameter units is to grab the relevant parameter type option under the[units]header
of thecavcalc.iniconfiguration file. When installingcavcalc, this file is written to a newcavcalc/directory within
your config directory (i.e. typically~/.config/cavcalc/cavcalc.iniunder Unix systems). See the comments in this file for
details on the options available for the output units of each parameter type.cavcalcattempts to read acavcalc.iniconfig file from several locations in this fixed order:Firstly from the current working directory, thenfrom$XDG_CONFIG_HOME/cavcalc/(or%APPDATA%/cavcalc/on Windows), thenthe final read attempt is from the within the source of the package directory itself.The config options from these read attempts are loaded in a standard way; that is, any options appearing
first in the sequence defined above will take priority. If any of the above read attempts fails, then this
will be a silent failure; the only situation where an error could occur would be whenallof the above
read attempts fail (which is very unlikely to happen).Note that if you specify a-uargument when runningcavcalcfrom the CLI, then this takes priority over
the options in the config file (as we saw in an example above).Evaluating, and plotting, parameters over data rangesParameters can be computed over ranges of data using:the data range syntax:-<param_name> "start stop num [<units>]",or data from an input file with-<param_name> <file>.We can use data-ranges to compute, and plot, arrays of target values, e.g:cavcalcw-L"1 10 100 km"-Rc5.1km--plotThis results in a plot (see below) showing how the beam radius at the mirrors of a symmetric cavity varies from
a cavity length of 1 km to 10 km with 100 data points, with the radii of curvature of both mirrors fixed at 5.1 km.Alternatively one could use a file of data, e.g:cavcalcgouy-L5cm-wbeam_radii.txt--plot--saveplotsymmcav_gouy_vs_ws.pngThis then computes the round-trip Gouy phase (in degrees) of a symmetric cavity of length 5cm
using beam radii data stored in a filebeam_radii.txt, and plots the results (see below). Note also that
you can save the resulting figure using the--saveplot <filename>syntax as seen in the above command.From the plot above you can also see that cavcalc supports automatically plotting of quantities which can be
dual-valued. In this case, the Gouy phase can be one of two values for each beam radius; this is due to the
nature of the equations which govern the Fabry-Perot cavity dynamics.Image / density plots via--meshWhen multiple arguments are given as data-ranges, one can use the--meshoption to construct mesh-grids
of these parameters. This allows cavcalc to automatically produce image plots. For example:cavcalcw-L"1 10 100 km"-gouy"20 120 100 deg"--meshtrue--plotcomputes the radius of the beam on the mirrors of a symmetric cavity, against both the cavity length and
round-trip Gouy phase on a grid. This results in the plot shown below. Note that we simply used--mesh truehere, which automatically determines the ordering of the mesh-grid parameters based on the order in which
these parameters were given. One could replace the above with, e.g.,--mesh "gouy,L"to reverse the order
of the mesh-grid; and thereby flip the parameter axes on any automated plots.A matplotlib compliant colour-map can be specified when making an image plot using the--cmap <name>option. For example,
the following command gives the plot shown below.cavcalcgouy-g1"-2 2 499"-g2g1--meshtrue--plot--cmapSpectral_rNotethat here we also used the parameter-referencing feature ofcavcalc, introduced in v1.2.0, to set
the values ofg1to those ofg2.A note on g-factorsStability (g) factors are split into four different parameters for implementation purposes and to
hopefully make it clearer as to which argument is being used and whether the resulting cavity
computations are for a symmetric or asymmetric cavity. These arguments are detailed here:-gs: The symmetric, singular stability factor. This represents the individual g-factors ofbothcavity mirrors. Use this to define asymmetriccavity where the overall cavity g-factor is
then simplyg = gs * gs.-g: The overall cavity stability factor. This is the product of the individual g-factors of the
cavity mirrors. Use this to define asymmetriccavity where the individual g-factors ofbothmirrors are thengs = +/- sqrt(g).-g1: The stability factor of the first cavity mirror. Use this to define anasymmetriccavity
along with the argument-g2such that the overall cavity g-factor is theng = g1 * g2.-g2: The stability factor of the second cavity mirror. Use this to define anasymmetriccavity
along with the argument-g1such that the overall cavity g-factor is theng = g1 * g2.UsingcavcalcprogrammaticallyAs well as providing a CLI, cavcalc has a full API which allows users to interact with this tool
via Python. The recommended method for doing this is to use the single-function interface viacavcalc.calculate. This
function works similarly to the CLI, where a target can be specified along with a variable number of keyword
arguments corresponding to the physical parameters. This function then returns one of two output objects (SingleOutputif a target was given,MultiOutputotherwise); seecavcalc.outputfor details.For example, the following script will compute all available targets from the cavity length and mirror radii
of curvature provided:importcavcalcascc# Specifying no target means all possible targets are computedout=cc.calculate(L="4km",Rc1=1934,Rc2=2245)# Printing the output object results in the same output as# you would see when running via the CLIprint(out)producing:Given:
	Cavity length = 4 km

	Radius of curvature of first mirror = 1934 m
	Radius of curvature of second mirror = 2245 m

	Wavelength of beam = 1064 nm

Computed:
	FSR = 37474.05725 Hz
	Mode separation frequency = 4988.072188176178 Hz

	Position of beam waist (from first mirror) = 1837.2153886417168 m

	Radius of beam at first mirror = 53.421066433049255 mm
	Radius of beam at second mirror = 62.448079883230896 mm
	Radius of beam at waist = 11.950538458990879 mm

	Stability g-factor of cavity = 0.8350925761717987
	Stability g-factor of first mirror = -1.0682523267838677
	Stability g-factor of second mirror = -0.7817371937639199

	Round-trip Gouy phase = 312.0813565565169 deg

	Divergence angle = 0.0016237789746943276 degAn extra feature of the API is the ability to use thecavcalc.configurefunction for overriding
default behaviour. For example, in the script below we use this in a context-managed scope to
temporarily use microns for any beam radius parameters, mm for distances, and GHz for any frequencies:importcavcalcascc# Temporarily override units...withcc.configure(beamsizes="um",distances="mm",frequencies="GHz"):out=cc.calculate(L=8,gouy=121)print(out)# ... previous state (using loaded config options) will# be restored on exit from the with block aboveresulting in:Given:
    Cavity length = 8 mm

    Round-trip Gouy phase = 121 deg

    Wavelength of beam = 1064 nm

Computed:
    FSR = 18.737028625 GHz
    Mode separation frequency = 6.297723510069445 GHz

    Position of beam waist (from first mirror) = 4.0 mm

    Radius of curvature of both mirrors = 0.01576117284251957 m

    Radius of beam at mirrors = 55.79464044247193 µm
    Radius of beam at waist = 48.19739141432035 µm

    Stability g-factor of cavity = 0.2424809625449729
    Stability g-factor of both mirrors = 0.4924235601034671

    Divergence angle = 0.40260921048107506 deg