pypho_fiber.py 13.7 KB
Newer Older
Arne Striegler's avatar
Arne Striegler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/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
Arne Striegler's avatar
Arne Striegler committed
17
import pyfftw
Arne Striegler's avatar
Arne Striegler committed
18 19
import copy

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

Arne Striegler's avatar
Arne Striegler committed
24 25
#import pylab as p
#import mpl_toolkits.mplot3d.axes3d as p3
Arne Striegler's avatar
Arne Striegler 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)
Arne Striegler's avatar
Arne Striegler committed
74 75 76
        pyfftw.import_wisdom(self.glova.wisdom_pyfftw)
        self.fft_fwdx = pyfftw.FFTW(self.input_array_fx, self.output_array_fx, direction='FFTW_FORWARD', flags=[self.glova.fftw3_plan], threads=self.glova.fftw3_threads)
        self.fft_bwdx = pyfftw.FFTW(self.input_array_bx, self.output_array_bx, direction='FFTW_BACKWARD', flags=[self.glova.fftw3_plan], threads=self.glova.fftw3_threads)
Arne Striegler's avatar
Arne Striegler committed
77 78 79 80 81 82 83


        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)
Arne Striegler's avatar
Arne Striegler committed
84 85
            self.fft_fwdy = pyfftw.FFTW(self.input_array_fy, self.output_array_fy, direction='FFTW_FORWARD', flags=[self.glova.fftw3_plan], threads=self.glova.fftw3_threads)
            self.fft_bwdy = pyfftw.FFTW(self.input_array_by, self.output_array_by, direction='FFTW_BACKWARD', flags=[self.glova.fftw3_plan], threads=self.glova.fftw3_threads)
Arne Striegler's avatar
Arne Striegler committed
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


        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)


        return self.E

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

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

Arne Striegler's avatar
Arne Striegler committed
119
        self.E[zsep]['noise'] *= np.exp(-self.l * self.alpha)
Arne Striegler's avatar
Arne Striegler committed
120 121 122 123 124 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
        
        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

Arne Striegler's avatar
Arne Striegler committed
156 157 158 159
            # 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)
Arne Striegler's avatar
Arne Striegler committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
            
            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 :
Arne Striegler's avatar
Arne Striegler committed
176
                print (lc)
Arne Striegler's avatar
Arne Striegler committed
177 178 179 180 181 182 183 184 185 186 187
                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 )
Arne Striegler's avatar
Arne Striegler committed
188
        print ('OFF', lc)
Arne Striegler's avatar
Arne Striegler committed
189 190 191 192 193 194 195 196 197 198 199


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

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

        res = copy.deepcopy(E)

        self.input_array_fx[:] = E[0]
         
Arne Striegler's avatar
Arne Striegler committed
200
        pyfftw.FFTW.execute(self.fft_fwdx)
Arne Striegler's avatar
Arne Striegler committed
201 202 203 204 205 206
        
        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)
        
Arne Striegler's avatar
Arne Striegler committed
207
        pyfftw.FFTW.execute(self.fft_bwdx)
Arne Striegler's avatar
Arne Striegler committed
208 209 210 211 212 213 214
        
        res[0] = self.output_array_bx * np.sqrt( np.exp(-self.alpha * dz) )

        if self.useYPol:
            self.input_array_fy[:] = E[1]
            
            
Arne Striegler's avatar
Arne Striegler committed
215
            pyfftw.FFTW.execute(self.fft_fwdy)
Arne Striegler's avatar
Arne Striegler committed
216 217 218 219
            
            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)
            
Arne Striegler's avatar
Arne Striegler committed
220
            pyfftw.FFTW.execute(self.fft_bwdy)
Arne Striegler's avatar
Arne Striegler committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            
            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):
Arne Striegler's avatar
Arne Striegler committed
238
        
Arne Striegler's avatar
Arne Striegler committed
239 240 241 242 243 244 245 246
        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

########################################################################
Arne Striegler's avatar
Arne Striegler committed
247
  
Arne Striegler's avatar
Arne Striegler committed
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
    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:
            self.D             = 17.0
Arne Striegler's avatar
Arne Striegler committed
297
            print ("Warning: D and fibertype not specified, so I set D = ",self.D," ps/nm/km")
Arne Striegler's avatar
Arne Striegler committed
298 299 300 301 302
        elif D != None:
            self.D = D

        if S == None and fibertype == None and self.S == None:
            self.S             = 0.68
Arne Striegler's avatar
Arne Striegler committed
303
            print ("Warning: S and fibertype not specified, so I set S = ",self.S," ps/nm**2/km")            
Arne Striegler's avatar
Arne Striegler committed
304 305 306 307 308
        elif S != None:
            self.S = S

        if gamma == None and fibertype == None and self.gamma == None:
            self.gamma            = 1.14
Arne Striegler's avatar
Arne Striegler committed
309
            print ("Warning: gamma and fibertype not specified, so I set gamma = ",self.gamma," 1/W/km")            
Arne Striegler's avatar
Arne Striegler committed
310 311 312 313 314 315 316 317
        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
Arne Striegler's avatar
Arne Striegler committed
318
            
Arne Striegler's avatar
Arne Striegler committed
319
        if l == None and fibertype == None and self.l == None:
Arne Striegler's avatar
Arne Striegler committed
320 321
            self.l         = 80.0e3
            print ("Warning: L and fibertype not specified, so I set L = ",self.l," km")            
Arne Striegler's avatar
Arne Striegler committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        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)
Arne Striegler's avatar
Arne Striegler committed
343
        self.Domega     = 2.0 * np.pi * (self.glova.freqax() - self.glova.f0) / 1.0e12
Arne Striegler's avatar
Arne Striegler committed
344 345 346 347 348 349 350
        #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
Arne Striegler's avatar
Arne Striegler committed
351 352 353 354
        
        y_max = self.glova.frange * 2.0 * np.pi  / 1.0e12
        x = np.arange(0,self.glova.sps*self.glova.nos)
        k =  2.0 * np.pi *self.glova.fres  / 1.0e12
Arne Striegler's avatar
Arne Striegler committed
355
        #self.y = x*k - ( (x*k-y_max/2.0)/np.abs(x*k-y_max/2.0) +1)*y_max/2.0
Arne Striegler's avatar
Arne Striegler committed
356 357
        
        
Arne Striegler's avatar
Arne Striegler committed
358 359 360 361 362

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