pypho_fiber.py 13.4 KB
Newer Older
hm-striegle's avatar
hm-striegle committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/env python 
#  
#  Copyright 2014 Arne Striegler (arne.striegler@hm.edu)
#   
#   
#  
# Simulates a fiber
# uiukhuh
#
########################################################################

import numpy as np
import sys
from pypho_functions import *
from pypho_fiber_birefringence import pypho_fiber_birefringence
import time
import fftw3
import copy

hm-striegle's avatar
hm-striegle committed
20 21 22
#from mpl_toolkits.mplot3d import Axes3D
#import matplotlib.pyplot as plt
#from itertools import product, combinations  
hm-striegle's avatar
hm-striegle committed
23

hm-striegle's avatar
hm-striegle committed
24 25
#import pylab as p
#import mpl_toolkits.mplot3d.axes3d as p3
hm-striegle's avatar
hm-striegle committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

########################################################################


class pypho_fiber(object):
    def __init__(self, glova = None, fibertype = None, D = None, S = None, gamma = None, alpha = None, l = None, useYPol = True, birefarray = None, phi_max=None):

        if glova == None:            
            print ("ERROR: You must define the global variables")
            sys.exit("PyPho stopped!")

        self.glova     = glova
        self.D         = None
        self.S         = None
        self.l         = None
        self.gamma     = None
        self.alpha     = None
        self.birefarray = None
        self.phi_max   = None
        self.useYPol   = True
        self.set(fibertype, D, S, gamma, alpha, l, useYPol, birefarray, phi_max)


########################################################################

    def __call__(self, E = None, fibertype = None, D = None, S = None, gamma = None, alpha = None, l = None, useYPol = True, birefarray = None, phi_max=None):

        self.set(fibertype, D, S, gamma, alpha, l, useYPol, birefarray)

        return self.transmit(E, fibertype, D, S, gamma, alpha, l, phi_max)

########################################################################
    
    def transmit(self, E = None, fibertype = None, D = None, S = None, gamma = None, alpha = None, l = None, phi_max=None):
        """Transmit the signal"""

        if E == None:
            print ("ERROR: You must define an optical signal")
            sys.exit("PyPho stopped!")

        if type(E) != list:
            E = [E]


        self.input_array_fx    = np.zeros(self.glova.sps*self.glova.nos, dtype=np.complex128)
        self.output_array_fx   = np.zeros(self.glova.sps*self.glova.nos, dtype=np.complex128)
        self.input_array_bx    = np.zeros(self.glova.sps*self.glova.nos, dtype=np.complex128)
        self.output_array_bx   = np.zeros(self.glova.sps*self.glova.nos, dtype=np.complex128)
hm-striegle's avatar
hm-striegle committed
74 75
        self.fft_fwdx = fftw3.Plan(self.input_array_fx, self.output_array_fx, direction='forward', flags=['estimate'], nthreads=4)
        self.fft_bwdx = fftw3.Plan(self.input_array_bx, self.output_array_bx, direction='backward', flags=['estimate'], nthreads=4)
hm-striegle's avatar
hm-striegle committed
76 77 78 79 80 81 82


        if self.useYPol:
            self.input_array_fy    = np.zeros(self.glova.sps*self.glova.nos, dtype=np.complex128)
            self.output_array_fy   = np.zeros(self.glova.sps*self.glova.nos, dtype=np.complex128)
            self.input_array_by    = np.zeros(self.glova.sps*self.glova.nos, dtype=np.complex128)
            self.output_array_by   = np.zeros(self.glova.sps*self.glova.nos, dtype=np.complex128)
hm-striegle's avatar
hm-striegle committed
83 84
            self.fft_fwdy = fftw3.Plan(self.input_array_fy, self.output_array_fy, direction='forward', flags=['estimate'], nthreads=4) 
            self.fft_bwdy = fftw3.Plan(self.input_array_by, self.output_array_by, direction='backward', flags=['estimate'], nthreads=4)
hm-striegle's avatar
hm-striegle committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123


        self.set(fibertype, D, S, gamma, alpha, l, self.useYPol)
        
        self.E = E
        
        self.gamma_intern = self.gamma * 1e-3
        self.max_step = 100

        if self.useYPol:
            self.delta_beta = self.birefarray[0].delta_beta
            self.E[0]['E'] = self.coordrot(self.E[0]['E'], self.birefarray[0].angle)       
            self.birefindex = 1
            
        z = 0

        
        self.fibtrans(z)
        
        fftw3.destroy_plan(self.fft_fwdx)       # diese funktion tut mglweise nichts bei fftw3
        fftw3.destroy_plan(self.fft_bwdx)
        if self.useYPol:
            fftw3.destroy_plan(self.fft_fwdy)
            fftw3.destroy_plan(self.fft_bwdy)


        return self.E

     
 ########################################################################

    def fibtrans(self, zsep):
        """Calculate step"""
        
        lc = 0 
        lcd = 0 
       
        tic = time.time()

hm-striegle's avatar
hm-striegle committed
124
        self.E[zsep]['noise'] *= np.exp(-self.l * self.alpha)
hm-striegle's avatar
hm-striegle committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        
        while lc < self.l:
            
            power_x = self.get_power_Ax(self.E[zsep]['E'])
            power_y = self.get_power_Ay(self.E[zsep]['E'])
            power = power_x + power_y
            # calculate the steplength

            next_leff = self.phi_max / np.max(power)         #(1-np.exp(-self.alpha*5))/self.alpha
            
            if next_leff*self.alpha < 1 :
                next_l = -np.log(1-next_leff*self.alpha)/self.alpha
            else:
                next_l = next_leff
                
            if next_l < next_leff:
                next_l = next_leff
            
            if next_l > self.max_step:
                next_l =  self.max_step
                next_leff = (1-np.exp(-self.alpha*next_l)) / self.alpha
            
            if lc + next_l > self.l:
                next_l =  self.l - lc
                next_leff = (1-np.exp(-self.alpha*next_l)) / self.alpha
                
            if self.useYPol:
                            
                doCoordRot = False
                if self.birefindex < len(self.birefarray) and lc + next_l >= self.birefarray[self.birefindex].z_point:
                    next_length = self.birefarray[self.birefindex].z_point - lc
                    self.birefindex += 1
                    next_l = next_length
                    next_leff = (1-np.exp(-self.alpha*next_l)) / self.alpha
                    doCoordRot = True

hm-striegle's avatar
hm-striegle committed
161 162 163 164
            # linear & nonlinear calculation
            #next_l = 100
            #next_leff = next_l            
            #self.E[zsep]['E'][0], self.E[zsep]['E'][1] = self.timestep(self.E[zsep]['E'][0], self.E[zsep]['E'][1], power_x, power_y, next_leff)
hm-striegle's avatar
hm-striegle committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            
            self.E[zsep]['E'][0], self.E[zsep]['E'][1] = (self.E[zsep]['E'][0] * np.exp(-1j*next_leff*self.gamma_intern*(power_x + 0.6666666666666666 * power_y)), 
                self.E[zsep]['E'][1] * np.exp(-1j*next_leff*self.gamma_intern*(power_y + 0.6666666666666666 * power_x))) 

            self.E[zsep]['E'] = self.freqstep(self.E[zsep]['E'], next_l)
            

            if self.useYPol and doCoordRot:
                self.E[zsep]['E'] = self.coordrot(self.E[zsep]['E'], self.birefarray[self.birefindex-1].angle)
                self.delta_beta = self.birefarray[self.birefindex-1].delta_beta
                
                
            lc += next_l
            lcd += next_l
           # print (lc, next_l, next_leff)
            if lcd > 1000 :
hm-striegle's avatar
hm-striegle committed
181
                print (lc)
hm-striegle's avatar
hm-striegle committed
182 183 184 185 186 187 188 189 190 191 192
                lcd = 0
                
        if self.useYPol:
            total_Angle = 0
            for point in self.birefarray:
                total_Angle += point.angle
    
            self.E[zsep]['E'] = self.coordrot(self.E[zsep]['E'], -total_Angle)
               

        print (time.time() - tic )
hm-striegle's avatar
hm-striegle committed
193
        print ('OFF', lc)
hm-striegle's avatar
hm-striegle committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349


########################################################################

    def freqstep(self, E, dz):
        """Calculate the linear step"""

        res = copy.deepcopy(E)

        self.input_array_fx[:] = E[0]
         
        fftw3.execute(self.fft_fwdx)
        
        self.input_array_bx[:] = self.output_array_fx * np.exp( self.beta_fac * dz) / len(E[0])
        
        if self.useYPol:
            self.input_array_bx[:] *= np.exp(-0.5 * 1j * fftshift (self.delta_beta * self.Domega) * dz)
        
        fftw3.execute(self.fft_bwdx)
        
        res[0] = self.output_array_bx * np.sqrt( np.exp(-self.alpha * dz) )

        if self.useYPol:
            self.input_array_fy[:] = E[1]
            
            
            fftw3.execute(self.fft_fwdy)
            
            self.input_array_by[:] = self.output_array_fy * np.exp( self.beta_fac * dz) / len(E[1])
            self.input_array_by[:] *= np.exp(0.5 * 1j * fftshift (self.delta_beta * self.Domega) * dz)
            
            fftw3.execute(self.fft_bwdy)
            
            res[1] = self.output_array_by * np.sqrt( np.exp(-self.alpha * dz) )

        return res
             

########################################################################

    def coordrot(self, E, angle):
        A = copy.deepcopy(E)
        A[0] = E[0]*np.cos(angle) - E[1]*np.sin(angle)
        A[1] = E[0]*np.sin(angle) + E[1]*np.cos(angle)
        return A

########################################################################

    def get_power_t(self, E):
        ox_real = np.real(E[0,:])
        ox_imag = np.imag(E[0,:])
        oy_real = np.real(E[1,:])
        oy_imag = np.imag(E[1,:])

        return ox_real**2 + ox_imag**2 + oy_real**2 + oy_imag**2


########################################################################
    def get_power_Ax(self, E):

       
        ox_real = np.real(E[0])
        ox_imag = np.imag(E[0])
        return  ox_real**2 + ox_imag**2

########################################################################

    def get_power_Ay(self, E):
                
        
        oy_real = np.real(E[1])
        oy_imag = np.imag(E[1])
        return oy_real**2 + oy_imag**2
               
########################################################################
        
    def get_parameters(self):
        """Get all fiber parameters set by the user"""
        biref_angle = []
        biref_zpoint = []
        biref_delta_beta = []
        for x in self.birefarray:
            biref_angle.append(x.angle)
            biref_zpoint.append(x.z_point)
            biref_delta_beta.append(x.delta_beta)

        params = {'type' : 'fiber', 'D':  self.D, 'S' : self.S,'l' : self.l, 'gamma' : self.gamma, 'alpha' : 4.343e3*self.alpha, 'biref_delta_beta' : biref_delta_beta, 'biref_zpoint' : biref_zpoint, 'biref_angle' : biref_angle, 'useYPol' : self.useYPol}
        return params

########################################################################
    def set(self, fibertype = None, D = None, S = None, gamma = None, alpha = None, l = None, useYPol = None, birefarray = None, phi_max=None):
        """Set fibre properties"""


        if fibertype ==  'SSMF':
            self.D         = 17.0                   # [ps/(nm km)]
            self.S         = 0.0                         # [ps/(nm2 km)]
            self.gamma     = 1.14                   # [1/(W m)]
            self.alpha     = db2neper(0.20)  / 1.0e3     # [1/m]


        if fibertype == 'DCF':
            self.D         = -100.0                    # [ps/(nm km)]
            self.S         = 0.0                        # [ps/(nm2 km)]
            self.gamma     = 1.7                        # [1/(W m)]
            self.alpha     = db2neper(0.20)  / 1.0e3   # [1/m]


        if D == None and fibertype == None and self.D == None:
            print ("Warning: D and fibertype not specified, so I set D = 16.8 ps/nm/km")
            self.D             = 17.0
        elif D != None:
            self.D = D

        if S == None and fibertype == None and self.S == None:
            print ("Warning: S and fibertype not specified, so I set S = 0.68 ps/nm**2/km")
            self.S             = 0.68
        elif S != None:
            self.S = S

        if gamma == None and fibertype == None and self.gamma == None:
            print ("Warning: gamma and fibertype not specified, so I set gamma = 1.14 1/W/km")
            self.gamma            = 1.14
        elif gamma != None:
            self.gamma = gamma

        if alpha == None and fibertype == None and self.alpha == None:
            print ("Warning: alpha and fibertype not specified, so I set alpha = 0.25 dB/km")
            self.alpha         = db2neper(0.25)  / 1.0e3
        elif alpha != None:
            self.alpha = db2neper(alpha) / 1.0e3

        if l == None and fibertype == None and self.l == None:
            print ("Warning: L and fibertype not specified, so I set L = 80 km")
            self.l         = 80e3
        elif l != None:
            self.l = l

        if useYPol == None and self.useYPol == None:
            self.useYPol = True
        elif useYPol != None:
            self.useYPol = useYPol
            
        if phi_max == None and self.phi_max == None:
            self.phi_max = 0.01
            print ("Warning: phi_max not specified, so I set phi_max = ", self.phi_max)
        elif phi_max != None:
            self.phi_max = phi_max            
        
        if birefarray == None and self.birefarray == None:
            nosiref = pypho_fiber_birefringence (0, 0, 0)
            self.birefarray = [nosiref]
        elif birefarray != None:
            self.birefarray = birefarray

        (self.beta_2, self.beta_3) =  DS2beta(self.D, self.S, self.glova.lambda0)
hm-striegle's avatar
hm-striegle committed
350 351 352 353 354 355 356 357
        self.Domega     = 2.0 * np.pi * (self.glova.freqax - self.glova.f0) / 1.0e12
        #self.beta_fac   = -1j * fftshift  ( self.beta_2*0.5e24 * self.Domega**2.0 + self.beta_3*1.0e36 / 6.0  * self.Domega**3.0)
        Domega_tmp     = self.Domega**2.0 
        self.beta_fac       = self.beta_2*0.5e24 * Domega_tmp
        Domega_tmp     *= self.Domega
        self.beta_fac       += self.beta_3*1.0e36 / 6.0 * Domega_tmp
        self.beta_fac       = -1j * fftshift  (self.beta_fac)
        Domega_tmp     = 0
hm-striegle's avatar
hm-striegle committed
358 359 360 361 362

########################################################################