Garchupiter / Kraken-Optical-Simulator

Python - Exact ray tracing library
GNU General Public License v3.0
67 stars 15 forks source link

Simulation of optical fiber #11

Open ppfromero opened 1 year ago

ppfromero commented 1 year ago

Hello guys, I'm trying to simulate propagation trough optical fibers or tubes. I create a tube in stl format and import but the rays can't be traced... I have the following error:

runfile('C:/Users/j.romero/Desktop/untitled0.py', wdir='C:/Users/j.romero/Desktop')
Default catalog is loaded from:  C:\Users\j.romero\Anaconda3\envs\krakenos\lib\site-packages\KrakenOS/Cat/
Glass catalog loaded
Loading glass calatogs:
Traceback (most recent call last):

  File ~\Anaconda3\envs\krakenos\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\j.romero\desktop\untitled0.py:61
    Tube.Trace(pSource_0, dCos, W)

  File ~\Anaconda3\envs\krakenos\lib\site-packages\KrakenOS\KrakenSys.py:676 in Trace
    self.__EmptyCollect(RayOrig, ResVec, WaveLength, j)

  File ~\Anaconda3\envs\krakenos\lib\site-packages\KrakenOS\KrakenSys.py:376 in __EmptyCollect
    self.__CollectData(ValToSav)

  File ~\Anaconda3\envs\krakenos\lib\site-packages\KrakenOS\KrakenSys.py:454 in __CollectData
    self.TTBE.append(self.tt*self.BULK_TRANS[-1])

IndexError: list index out of range

Do you know if an optical fiber (with clading and coating indexes) can be created more easy with krakeos tools? My script and file stl:

Tube.zip


# -*- coding: utf-8 -*-
"""
Created on Fri Sep  1 09:52:23 2023

@author: j.romero
"""

import KrakenOS as Kos
import numpy as np
import matplotlib.pyplot as plt

# ______________________________________#

P_Obj = Kos.surf()
P_Obj.Rc = 0.0
P_Obj.Thickness = 30
P_Obj.Glass = "AIR"
P_Obj.Diameter = 50.0

# ______________________________________#

file = r"C:\\Users\\j.romero\\Desktop\\Tube.stl"
objeto = Kos.surf()
objeto.Solid_3d_stl = file
objeto.Glass = "BK7"
objeto.TiltX=90
objeto.AxisMove = 0
objeto.Diameter = 50.0
objeto.Thickness=50

# ______________________________________#
P_Ima = Kos.surf()
P_Ima.Rc = 0
P_Ima.Thickness = 0
P_Ima.Glass = "AIR"
P_Ima.Diameter = 50.0
P_Ima.Drawing = 1

# ______________________________________#

A = [P_Obj, objeto, P_Ima]
configuracion_1 = Kos.Setup()
Tube = Kos.system(A, configuracion_1)
RayosT = Kos.raykeeper(Tube)

# ______________________________________#

tam = 10
rad = 10.0
tsis = len(A) - 1
for j in range(-tam, tam + 1):
    for i in range(-tam, tam + 1):
        x_0 = (i / tam) * rad
        y_0 = (j / tam) * rad
        r = np.sqrt((x_0 * x_0) + (y_0 * y_0))
        if r < rad:
            tet = 0.0
            pSource_0 = [x_0, y_0, 0.0]
            dCos = [0.0, np.sin(np.deg2rad(tet)), np.cos(np.deg2rad(tet))]
            W = 0.6
            #Tube.Trace(pSource_0, dCos, W)
            #RayosT.push()

# ______________________________________#

Kos.display2d(Tube, RayosT, 0)
Kos.display3d(Tube, RayosT, 0)

Thanks José

Garchupiter commented 1 year ago

Hi. Your code is working fine on my computer. Could you tell me if another one of the examples included in KrakenOS is working correctly? KrakenOS cannot load STL items with multiple refractive indices within the same file. To simulate this, you can use a cylindrical STL and set the internal refractive index while considering the external one as air.

If your intention is to demonstrate the effect of different refractive indices, you can simulate a fiber using parallel plates with three different glasses and trace rays through the center. I've attached that example for you.

 Additionally, please remember that both of these cases involve non-sequential ray tracing, for which the NsTrace function is used, as shown in the example I've provided.

Best regards, and don't hesitate to reach out if you have any questions.

Joel H. V.

El 1 sep 2023, a las 3:06, ppfromero @.***> escribió:

Hello guys, I'm trying to simulate propagation trough optical fibers or tubes. I create a tube in stl format and import but the rays can't be traced... I have the following error:

runfile('C:/Users/j.romero/Desktop/untitled0.py', wdir='C:/Users/j.romero/Desktop') Default catalog is loaded from: C:\Users\j.romero\Anaconda3\envs\krakenos\lib\site-packages\KrakenOS/Cat/ Glass catalog loaded Loading glass calatogs: Traceback (most recent call last):

File ~\Anaconda3\envs\krakenos\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals)

File c:\users\j.romero\desktop\untitled0.py:61 Tube.Trace(pSource_0, dCos, W)

File ~\Anaconda3\envs\krakenos\lib\site-packages\KrakenOS\KrakenSys.py:676 in Trace self.__EmptyCollect(RayOrig, ResVec, WaveLength, j)

File ~\Anaconda3\envs\krakenos\lib\site-packages\KrakenOS\KrakenSys.py:376 in EmptyCollect self.CollectData(ValToSav)

File ~\Anaconda3\envs\krakenos\lib\site-packages\KrakenOS\KrakenSys.py:454 in __CollectData self.TTBE.append(self.tt*self.BULK_TRANS[-1])

IndexError: list index out of range

Do you know if an optical fiber (with clading and coating indexes) can be created more easy with krakeos tools? My script and file stl:

Tube.zip https://github.com/Garchupiter/Kraken-Optical-Simulator/files/12496799/Tube.zip

-- coding: utf-8 --

""" Created on Fri Sep 1 09:52:23 2023

@author: j.romero """

import KrakenOS as Kos import numpy as np import matplotlib.pyplot as plt

__

P_Obj = Kos.surf() P_Obj.Rc = 0.0 P_Obj.Thickness = 30 P_Obj.Glass = "AIR" P_Obj.Diameter = 50.0

__

file = r"C:\Users\j.romero\Desktop\Tube.stl" objeto = Kos.surf() objeto.Solid_3d_stl = file objeto.Glass = "BK7" objeto.TiltX=90 objeto.AxisMove = 0 objeto.Diameter = 50.0 objeto.Thickness=50

__

P_Ima = Kos.surf() P_Ima.Rc = 0 P_Ima.Thickness = 0 P_Ima.Glass = "AIR" P_Ima.Diameter = 50.0 P_Ima.Drawing = 1

__

A = [P_Obj, objeto, P_Ima] configuracion_1 = Kos.Setup() Tube = Kos.system(A, configuracion_1) RayosT = Kos.raykeeper(Tube)

__

tam = 10 rad = 10.0 tsis = len(A) - 1 for j in range(-tam, tam + 1): for i in range(-tam, tam + 1): x_0 = (i / tam) rad y_0 = (j / tam) rad r = np.sqrt((x_0 x_0) + (y_0 y_0)) if r < rad: tet = 0.0 pSource_0 = [x_0, y_0, 0.0] dCos = [0.0, np.sin(np.deg2rad(tet)), np.cos(np.deg2rad(tet))] W = 0.6

Tube.Trace(pSource_0, dCos, W)

        #RayosT.push()

__

Kos.display2d(Tube, RayosT, 0) Kos.display3d(Tube, RayosT, 0) Thanks José

— Reply to this email directly, view it on GitHub https://github.com/Garchupiter/Kraken-Optical-Simulator/issues/11, or unsubscribe https://github.com/notifications/unsubscribe-auth/AED73RQHMMTXJR7ZC62RVCTXYGXRHANCNFSM6AAAAAA4HKUNWA. You are receiving this because you are subscribed to this thread.

ppfromero commented 1 year ago

Hi Joel,

Thanks for your comments. Some examples of repository don't works for me (I think that is due to numpy version >> 1.18 in my case) I have some errors due to complex numbers management. The most part of examples are working otherwise.

I will try your approach to import 3 plates and attribute different index. Could you please send again your example please? the attachement don't works.

Thanks

ppfromero commented 1 year ago

Hi Joel,

I try to load several stl (discs) for each layer of optical fiber: I get the following error:

File ~\Anaconda3\envs\kos\Lib\site-packages\KrakenOS\KrakenSys.py:454 in __CollectData self.TTBE.append(self.tt*self.BULK_TRANS[-1]) IndexError: list index out of range

Here below the code:


import KrakenOS as Kos
import numpy as np
import matplotlib.pyplot as plt

# ______________________________________#

P_Obj = Kos.surf()
P_Obj.Rc = 0.0
P_Obj.Thickness = 30
P_Obj.Glass = "AIR"
P_Obj.Diameter = 50.0

# ______________________________________#

file = r"core.stl"
core = Kos.surf()
core.Solid_3d_stl = file
core.Glass = "BK7"
core.TiltX=90
core.AxisMove = 0

# ______________________________________#

file = r"clad.stl"
clad = Kos.surf()
clad.Solid_3d_stl = file
clad.Glass = "BK7"
clad.TiltX=90
clad.AxisMove = 0

# ______________________________________#

file = r"c1.stl"
c1 = Kos.surf()
c1.Solid_3d_stl = file
c1.Glass = "BK7"
c1.TiltX=90
c1.AxisMove = 0

# ______________________________________#

file = r"c2.stl"
c2 = Kos.surf()
c2.Solid_3d_stl = file
c2.Glass = "BK7"
c2.TiltX=90
c2.AxisMove = 0
c2.Thickness = 100

# ______________________________________#
P_Ima = Kos.surf()
P_Ima.Rc = 0
P_Ima.Thickness = 0
P_Ima.Glass = "AIR"
P_Ima.Diameter = 50.0
P_Ima.Drawing = 1
# ______________________________________#

A = [P_Obj, core,clad,c1, c2, P_Ima]
configuracion_1 = Kos.Setup()
fo = Kos.system(A, configuracion_1)
RayosT = Kos.raykeeper(fo)

# ______________________________________#

tam = 10
rad = 10.0
tsis = len(A) - 1
for j in range(-tam, tam + 1):
    for i in range(-tam, tam + 1):
        x_0 = (i / tam) * rad
        y_0 = (j / tam) * rad
        r = np.sqrt((x_0 * x_0) + (y_0 * y_0))
        if r < rad:
            tet = 0.0
            pSource_0 = [x_0, y_0, 0.0]
            dCos = [0.0, np.sin(np.deg2rad(tet)), np.cos(np.deg2rad(tet))]
            W = 0.6
            #☺fo.Trace(pSource_0, dCos, W)
            #RayosT.push()

# ______________________________________#

Kos.display2d(fo, RayosT, 0)
Kos.display3d(fo, RayosT, 0)

And the files: layers.zip

One additional question: It is possible to assign refractive index of each layer? I use attribute Glass but I need to change the refractive index.

Thanks for your help

Garchupiter commented 1 year ago

Hi Indeed KrakenOS is not the best solution when you have two solid elements with different indices in contact. However, something that can be done easily is to use only the solid representing the inner part of the fiber and define a different index for the air. Then, you can assign the index you want. I will implement this in the next version in a few days.

Here is the example in which you can simulate a fiber with flat plates. In the 3D view, you can understand the trick, and in the 2D view, it can be visualized as if it were fiber :


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Examp fibra2D"""

# import os
import numpy as np
import pkg_resources
required = {'KrakenOS'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed

if missing:
    print("No instalado")
    import sys
    sys.path.append("../..")

import KrakenOS as Kos

# ______________________________________#

P_Obj = Kos.surf()
P_Obj.Rc = 0.0
P_Obj.Thickness = 51
P_Obj.Glass = "AIR"
P_Obj.Diameter = 50.0

# ______________________________________#

f1 = Kos.surf()
f1.Glass = 1.343
f1.TiltX=90
f1.AxisMove = 1
f1.Diameter = 100.0
f1.Thickness=1

f2 = Kos.surf()
f2.Glass = 1.522
f2.Diameter = 100.0
f2.Thickness=1

f3 = Kos.surf()
f3.Glass = 1.343
f3.Diameter = 100.0
f3.Thickness=1

f4 = Kos.surf()
f4.Glass = "AIR"
f4.Diameter = 100.0
f4.Thickness=0

# ______________________________________#
P_Ima = Kos.surf()
P_Ima.Rc = 0
P_Ima.Thickness = 0
P_Ima.Glass = "AIR"
P_Ima.Diameter = 50.0
P_Ima.Drawing = 1

P_Ima.TiltX=-90
P_Ima.DespY =100
P_Ima.AxisMove = 1
P_Ima.Diameter = 100.0

# ______________________________________#

A = [P_Obj, f1, f2, f3, f4, P_Ima]
configuracion_1 = Kos.Setup()
Tube = Kos.system(A, configuracion_1)
RayosT = Kos.raykeeper(Tube)

# ______________________________________#
ang = 10
ARR = np.arange(-ang*0.5, ang*0.5, 0.1)
for i in ARR:

    tet = i
    pSource_0 = [0 , -1.5 , 0.0]
    dCos = [0.0, np.sin(np.deg2rad(tet)), np.cos(np.deg2rad(tet))]
    W = 0.5
    Tube.NsTrace(pSource_0, dCos, W)
    RayosT.push()

# ______________________________________#

Kos.display2d(Tube, RayosT, 0)
Kos.display3d(Tube, RayosT, 0)

´´´