adelpopelkova / lenscalc

a lens calculator
http://adelpopelkova.pythonanywhere.com
MIT License
1 stars 1 forks source link
calculator lens

lenscalc

Lenscalc is a more universal version of Edmund Optics' Focal Length Calculator.

Unlike the original version, lenscalc gives you the option to enter any set of parameters. Now you don't have to slightly adjust the surface radius to get your desired back focal length, you can just type it in. In the background there are the exact same equations.

Lens variables

The variables used to define the lens are the same as in the original calculator. Some of them have been renamed to be easier to type (e.g. ΦOS → D1). Only the variables that have been renamed have all three columns filled in.

Lenscalc name Original name Description
D1 ΦOS Surface 1 (object) power
D2 ΦIS Surface 2 (image) power
D Φ Lens power
n1 nOS Object space index
nL Lens index
n2 nIS Image space index
r1 R1 Surface 1 (object) radius
r2 R2 Surface 2 (image) radius
CT Central thickness
P1 P Primary principle point
P2 P" Secondary principle point
f1 fF Front (object) focal point
f2 fR Back (image) focal point
EFL Effective focal length
FFL Front focal length
BFL Back focal length
NPS Shift in nodal point

If you are using lenscalc in your code (i.e. you aren't using the web version), the variables have the same name, they are just written without the subscript, (e.g. D1D1).

All variables except refractive indexes (n1, nL, n2) have the same unit (usually mm or cm).

Important notice

Lenscalc requires Python version 3.8 or above.

How to use the calculator

There are a few ways how you can use the calculator.

Using the web version

The web app currently runs here.

Using it in your Python code

  1. Download or clone the repository.
  2. Create and/or activate a virtual environment. (Optional, although highly recommended).
  3. Install needed dependencies using this command:
    $ python -m pip install -r requirements.txt
  4. Import the Lens class.
    from lenscalc import Lens
  5. Using one of these two ways create a lens:
    # The first one
    lens = Lens(
        n1 = 1.0003,
        nL = 1.5,
        n2 = 1.0003,
        r1 = 50,
        r2 = -40,
        CT = 3
    )
    # The second one
    lens = Lens()
    lens.n1 = 1.0003
    lens.nL = 1.5
    lens.n2 = 1.0003
    lens.r1 = 50
    lens.r2 = -40
    lens.CT = 3
  6. Calculate the missing variables using the calculate method.
    lens.calculate()
  7. To get the calculated variables use print(lens) for all variables or print(lens.BFL) and similar to get them one by one.

Using the web app locally

  1. Create and/or activate a virtual environment.
  2. Install the dependencies using this command:
    $ python -m pip install -r requirements-web.txt
  3. Use these commands to run the web app (On Windows use set instead of export.)
    $ export FLASK_APP=lenscalc_web
    $ export FLASK_ENV=development
    $ export FLASK_DEBUG=1
    $ flask run

Testing the calculator

  1. To an activated virtual environment install the dependencies for testing.
    • You can use this command to install only the necessary ones:
      $ python -m pip install -r requirements-dev.txt
    • If you want to install some extra tools (for easier development) use:
      $ python -m pip install -r requirements-extra.txt
  2. Run the tests
    • The easiest way to run the test is this command:
      $ python -m pytest
    • Use this command to get also the time of the 5 slowest tests.
      $ python -m pytest -v --durations=5
    • If you don't want to run the tests with the combinations (there are a lot of them and they take a bit longer to run), use the following command (after the ignore switch, use the path to the test_variable_combinations.py file). Example for running the tests from the root of the repository.
      $ python -m pytest --ignore=tests/test_variable_combinations.py
    • To run the tests with combinations faster, you can use the following command (you'll need to install extra requirements or only pytest-xdist for this command to work) to run the tests in parallel:
      $ python -m pytest -n <Number of CPUs>

Found a bug?

Have you found something that doesn't work as expected? Don't hesitate to open an issue or send a Pull Request!

License

This project is licensed under the MIT License.