Optics Workbench

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
MSLDFreeCAD
Posts: 7
Joined: Sat Mar 25, 2023 10:09 am

Re: Optics Workbench

Post by MSLDFreeCAD »

Hi all, I hope it is ok if I post some of my additions to the very superb OpticsWorkbench here, since there was already the question if diffraction gratings might possibly be implemented.
I tried this in an approach following the raytracing equations for simple 1D gratings following Ludwig et al. 1973 (https://doi.org/10.1364/JOSA.63.001105).
For this, now Rays have orders which would only affect if they hit a body defined as a diffraction grating.
Gratings are now introduced as a new class and can be of type reflection, transmission with diffraction at 1st surface and transmission with diffraction at 2nd surface. For transmission gratings refractive index is also important and the equations take these into account.
The definition of the grating line orientation is also noteworthy, as it is not intuitive: It is defined by a set of planes (i.e. the normal of these planes), which intersect the grating and the intersection lines would correspond to the lines of the diffraction grating (this is as in Ludwig et al. 1973).
2 further things might be interesting. Gratings can also be specified with a working order, which in the calculation might be used instead of the order the ray already is defined with. This allows for rays modelled, that hit multiple gratings in their path, while being diffracted with different orders (e.g. echelle type spectrometer with 2nd grating as cross disperser).
The other thing is that the sign of the orders are not intuitive. So if a ray cannot be computed because of a complex number problem, try to change the sign of the order, If you think this order of diffraction should be possible.
Some word of caution: I mostly now tested this with reflection gratings and I found it accurate, everywhen I checked. I have not so much tested it with transmission gratings, especially as normal by hand calculation is not so straigt forward, since refractive indices should be taken into account, nevertheless, it should be right...
some images:

Image
this illustrates a simple reflection grating with 500 lpm hit by sunray, 1st order. Planes with normal 010 indicate the set of intersecting planes used to define the grating lines direction.



Image
this shows the same body, defined as transmission grating. Note that the diffraction happens at the 2nd surface as specified by the grating type. Differences in refractive indices are taken into accound.



Image
Image
screenshots show that also non-planar bodies can be used, in the example of a concave reflection grating. A stepfile for a thorlabs concave mirror is used and set as a grating with 500 lpm. Sunray of 1st and 2nd order are shown.



Image
an example of a simple echelle spectrometer using a R2 52.91 lpm grating and a set of sunrays from order -47 to -82 (each order comprises ~5-10 nm, sampled by 15 rays around a center wavelength from blue to red) and a flint glas prism. Collimation and camera optics are thorlabs step files and a transparent absorber shows the resulting echelle spectrum. Entrance into the spectrometer design is by a 50 mu slit. This is a example with very long calculation time due to the high number of rays. Note that the sign of the order is not intuitive. If an error occurs, stating that complex numbers are not supported, while diffraction with this order is considered valid by the user, try to change the sign of the order.

This fork of the original OpticsWorkbench is available on GitHub. Hope you can enjoy this.
Please also note, that I am not experienced in contributing to such projects, in fact this is the first time I am doing this, so if there are any problemes please have some patience with me...:)
User avatar
christi
Posts: 203
Joined: Wed Oct 24, 2018 7:03 am
Location: Karlsruhe, Germany
Contact:

Re: Optics Workbench

Post by christi »

This looks great !
Please create a merge request when you think you are ready. I will then merge your work into the main branch if there are no issues, or I will add some comments.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Optics Workbench

Post by Kunda1 »

Awesome. Looks like the code is at https://github.com/MSLDgit/OpticsWorkbench
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
MSLDFreeCAD
Posts: 7
Joined: Sat Mar 25, 2023 10:09 am

Re: Optics Workbench

Post by MSLDFreeCAD »

Kunda1 wrote: Sun Mar 26, 2023 8:00 pm Awesome. Looks like the code is at https://github.com/MSLDgit/OpticsWorkbench
Yes that's it.
However, I just noticed a first caveat. Apparently I have not been using the up to date version of OpticsWorkbench, so apart from the diffraction stuff, other up-to-date functions are not available. This seems to include (amongst others) the new spherical ray (and cone angle) addition, and also some other changes like the lens inside lenses, etc...
I already tried to update the code so that it also works with the newest OpticsWorkbench. However, I already had a small peek into the ray file and there were some changes that are not so intuitive to me, and I will probably need some time to do this. Also, of course any help is appreciated.
Sorry for this.
User avatar
christi
Posts: 203
Joined: Wed Oct 24, 2018 7:03 am
Location: Karlsruhe, Germany
Contact:

Re: Optics Workbench

Post by christi »

MSLDFreeCAD wrote: Mon Mar 27, 2023 12:37 pm I already tried to update the code so that it also works with the newest OpticsWorkbench. However, I already had a small peek into the ray file and there were some changes that are not so intuitive to me, and I will probably need some time to do this. Also, of course any help is appreciated.
Sorry for this.
It looks like you cloned the latest official version and overwritten some files with old versions that include your changes. This will not work this way.
Do you still have a local copy of the old codebase with your changes ? If yes, commit your changes, and then do git pull --rebase, this will put your changes on top of the latest version. An alternative would be getting a fresh clone of the official version and use a merge tool to merge only the changes you made into the new version.
Please do not name a commit "Update someFile.py". The commit message should include a short description of a new feature. Include all files into one commit that belong to the same feature.
What else should be done is creating a new icon fore the diffraction grating. You can take one of the existing ones and edit it with Inkscape.

Anyway, I cloned your code and tested it - it works fine. Please go on!
User avatar
christi
Posts: 203
Joined: Wed Oct 24, 2018 7:03 am
Location: Karlsruhe, Germany
Contact:

Re: Optics Workbench

Post by christi »

Another good thing to do is creating an example. There are already some examples in the Optics menu. Each example creates a FreeCAD project from a python script to test one ore more special features. This are unit tests for all our features. Once your example is working, you can test your design with only one click. If you modified something, you can also test if all the other features are still working.

Creating an example is easy. Make a design in FreeCAD. Install the macro ObjectsToPython from the Addon manager. Select your design and execute the macro. It will create the needed python code for you. This does not work for all objects, especially not for the objects from the Optics workbench :shock: See the existing python files in the examples folder how to integrate the generated code.
User avatar
Vincent B
Veteran
Posts: 4713
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

Re: Optics Workbench

Post by Vincent B »

Hi,
Why all rays not following the lens rules in my file? :roll:
Attachments
myLens.FCStd
(42.81 KiB) Downloaded 44 times
User avatar
christi
Posts: 203
Joined: Wed Oct 24, 2018 7:03 am
Location: Karlsruhe, Germany
Contact:

Re: Optics Workbench

Post by christi »

Vincent B wrote: Sun Apr 02, 2023 10:39 am Hi,
Why all rays not following the lens rules in my file? :roll:
What do you think is wrong ?
User avatar
Vincent B
Veteran
Posts: 4713
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

Re: Optics Workbench

Post by Vincent B »

I was wandering why rays crossing edges are not following the lens curve. But probably not a bug, as they are just on the edge, then can't change their direction. If you subtract 1° to the cone angle, the result is weird.
Attachments
Capture.JPG
Capture.JPG (92.92 KiB) Viewed 1978 times
MSLDFreeCAD
Posts: 7
Joined: Sat Mar 25, 2023 10:09 am

Re: Optics Workbench

Post by MSLDFreeCAD »

The rays being reflected inside the lens, rather than refracted to leave the lens is due to total reflection at the lens surface for these angles. With n(lens) of 1.5163 and angles of ~43.9 (use a sketch to estimate these roughly) snells law would predict reflection instead of refraction.
Post Reply