Commit 84462518 authored by hm-striegle's avatar hm-striegle 😁

memory optimizations

parent ad615ab3
#!/usr/bin/env pytsftp://104.199.77.123/var/www/html/ul/upload.php
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 2 21:26:04 2017
@author: arne
"""
import MySQLdb
import numpy as np
import pycurl
from StringIO import StringIO
########################################################################
def mysql_get(sql):
"Read data from DB"
db = MySQLdb.connect(host="130.211.56.253", user="root", passwd="pyphodb", db="pypho")
cur = db.cursor()
cur.execute( sql )
db.close()
return cur.fetchall()
########################################################################
def mysql_set(sql):
"Write data to DB"
db = MySQLdb.connect(host="130.211.56.253", user="root", passwd="pyphodb", db="pypho")
cur = db.cursor()
number_of_rows = cur.execute(sql)
db.commit( )
db.close()
return number_of_rows
########################################################################
def cloud_LoadFiles():
"Save, zip and send all files for cloude computing"
import json
Etmp = np.loadtxt('E.txt').view(complex)
with open('glova.json') as json_data:
glova = json.load(json_data)
with open('link.json') as json_data:
link = json.load(json_data)
return Etmp, glova, link
########################################################################
def local_SaveFiles(filename, E):
np.savetxt(filename + '.txt', E[0]['E'].view(float) )
try:
import zlib
import zipfile
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
zf = zipfile.ZipFile(filename +".zip", mode='w')
try:
zf.write(filename + '.txt', compress_type=compression)
finally:
zf.close()
########################################################################
def cloud_SaveFiles(cid, filename):
# Send files to cloud
print ('Sending files to master!')
c = pycurl.Curl()
buffer1 = StringIO()
c.setopt(c.URL, 'https://optical-fiber-transmission-simulation.com/ul/upload.php')
c.setopt(c.POST, 1)
c.setopt(c.HTTPPOST, [("sim_ticket", cid), ("filecontents", (c.FORM_FILE, filename))])
c.setopt(c.WRITEDATA, buffer1)
c.perform()
c.close()
body1 = buffer1.getvalue()
return body1[0:74]
\ No newline at end of file
......@@ -33,20 +33,12 @@ class pypho_arbmod(object):
def __call__(self, glova = None, E = None, constpoints = None, bits = None):
if E == None:
print ("ERROR: pypho_armod: You must define an optical signal E")
sys.exit("PyPho stopped!")
self.set(E, constpoints, bits)
return self.out(E)
########################################################################
def out(self, E = []):
const_dec = [];
for const_bin in np.asarray( self.constpoints[0][4] ) :
const_dec.append( const_bin.dot(2**np.arange(const_bin.size)[::-1]) )
......
......@@ -97,7 +97,7 @@ class pypho_cfiber(object):
E[z]['E'][0] = Ex_out
E[z]['E'][1] = Ey_out
E[z]['noise'] *= np.exp(-self.l * self.alpha)
print ('Fertig: ', time.time() - tic0 )
return E
......
......@@ -27,7 +27,6 @@ class pypho_cwlaser(object):
sys.exit("PyPho stopped!")
self.glova = glova
self.power = None
self.teta = None
self.Df = None
......@@ -40,17 +39,7 @@ class pypho_cwlaser(object):
def __call__(self, power = None, teta = None, Df = None):
self.set(power, teta, Df)
return self.out(power, teta, Df)
########################################################################
def out(self, power = None, teta = None, Df = None):
self.set(power, teta, Df)
if type(self.Df) != list:
self.Df = [self.Df]
......@@ -58,18 +47,17 @@ class pypho_cwlaser(object):
for Dfi in self.Df:
Etmp = {}
Etmp['Df'] = Dfi
self.omega = 0.0 + 1.0*np.exp(1j*2.0*np.pi*Dfi*1.0e9*self.glova.timeax) + np.exp(1j*1e-25)
a = np.sqrt(dbm2w( self.power )) * np.ones(self.glova.sps*self.glova.nos)
a = np.vstack((a, np.zeros(self.glova.sps*self.glova.nos)))
esig = np.sqrt( dbm2w( self.power ) ) * np.exp(1j*2.0*np.pi*Dfi*1.0e9*self.glova.timeax)
Etmp['E'] = polrot(a*self.omega, self.teta) + 0j
Etmp['noise'] = np.zeros(self.glova.nos * self.glova.sps) + np.mean(a) / 10**(5.0) * self.glova.fres / (0.1 * cv.lightspeed/(1550**2 *1e-9) )
Etmp['E'] = np.array([np.zeros(self.glova.sps*self.glova.nos), np.zeros(self.glova.sps*self.glova.nos) ]) + 0j
Etmp['E'][0][:] = esig*np.cos(self.teta)
Etmp['E'][1][:] = esig*np.sin(self.teta)
del esig
Etmp['Df'] = Dfi
Etmp['noise'] = np.zeros(self.glova.nos * self.glova.sps) + dbm2w( self.power ) / 10.0**(5.8) / 12.5e9 * self.glova.fres
E.append( Etmp )
del Etmp
return E
......
......@@ -27,13 +27,12 @@ class pypho_eye(object):
if glova == None:
print ("ERROR: pypho_optfi: You must define the global variables")
sys.exit("PyPho stopped!")
self.glova = glova
self.E = []
self.glova = glova
self.polarisation = None
self.figure = None
self.style = None
self.set(self.E, polarisation, style)
self.set( polarisation, style)
########################################################################
......@@ -44,7 +43,7 @@ class pypho_eye(object):
print ("ERROR: You must define an optical signal")
sys.exit("PyPho stopped!")
self.set(E, polarisation, style)
self.set(polarisation, style)
return self.out(E)
......@@ -65,7 +64,6 @@ class pypho_eye(object):
sc = 0
for plotwhat in plotwhatlist:
print(plotwhat)
if plotwhat == 'x':
signal = np.abs(E[0,:])**2
elif plotwhat == 'y':
......@@ -86,7 +84,7 @@ class pypho_eye(object):
########################################################################
def set(self, E = [], polarisation = None, style = None):
def set(self, polarisation = None, style = None):
if polarisation == None and self.polarisation == None:
......
......@@ -17,12 +17,12 @@ import time
import fftw3
import copy
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from itertools import product, combinations
#from mpl_toolkits.mplot3d import Axes3D
#import matplotlib.pyplot as plt
#from itertools import product, combinations
import pylab as p
import mpl_toolkits.mplot3d.axes3d as p3
#import pylab as p
#import mpl_toolkits.mplot3d.axes3d as p3
########################################################################
......@@ -43,7 +43,6 @@ class pypho_fiber(object):
self.birefarray = None
self.phi_max = None
self.useYPol = True
self.set(fibertype, D, S, gamma, alpha, l, useYPol, birefarray, phi_max)
......@@ -348,9 +347,14 @@ class pypho_fiber(object):
self.birefarray = birefarray
(self.beta_2, self.beta_3) = DS2beta(self.D, self.S, self.glova.lambda0)
self.Domega = 2.0 * np.pi * (self.glova.freqax - 1*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)
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
########################################################################
......
......@@ -32,13 +32,11 @@ def dbm2w( power_dbm ):
"Transfers power value from dBm to W"
return 10**(power_dbm/10.0) / 1000.0
########################################################################
def w2dbm( power_w ):
"Transfers power value from W to dBm"
return 10*np.log10(power_w*1e3)
########################################################################
def polrot(sig, alpha):
"Rotates the polarisation of the signal"
......
......@@ -43,34 +43,29 @@ class pypho_lasmod(object):
if len(esig) == 0 :
print ("Error: pypho_lasmod: No electrical signal defined!")
sys.exit("PyPho stopped!")
else:
self.esig = esig
self.set(power, Df, teta)
return self.out(self.esig, self.power, self.Df, self.teta)
########################################################################
def out(self, esig = [], power = None, Df = None, teta = None):
if type(self.Df) != list:
self.Df = [self.Df]
E = [];
esig = esig + 0.0j
for Dfi in self.Df:
Etmp = {}
Etmp['Df'] = Dfi
a = np.sqrt( dbm2w( self.power ) * esig / np.mean(esig) )
a = np.vstack((a, np.zeros(self.glova.sps*self.glova.nos)))
self.omega = np.exp(1j*2.0*np.pi*Dfi*1.0e9*self.glova.timeax)
Etmp['E'] = polrot(a*self.omega, self.teta) + 0j
Etmp['noise'] = np.zeros(self.glova.nos * self.glova.sps) + dbm2w( self.power ) / 10.0**(5.8) / 12.5e9 * self.glova.fres
Etmp = dict()
esig *= np.sqrt( dbm2w( self.power ) / np.mean(esig) )
omega = np.exp(1j*2.0*np.pi*Dfi*1.0e9*self.glova.timeax)
esig *= omega
del omega
Etmp['E'] = Etmp['E'] = np.zeros((2, self.glova.nos * self.glova.sps)) + 0.0j
Etmp['E'][0][:] = esig * np.cos(self.teta)
Etmp['E'][1][:] = esig * np.sin(self.teta)
del esig
Etmp['Df'] = Dfi
Etmp['noise'] = np.zeros(self.glova.nos * self.glova.sps) + dbm2w( self.power ) / 10.0**(5.8) / 12.5e9 * self.glova.fres
E.append( Etmp )
del Etmp
return E
......
......@@ -29,14 +29,9 @@ class pypho_sample(object):
sys.exit("PyPho stopped!")
self.glova = glova
self.sampletime = None
self.E = []
self.constpoints = None
self.bits = None
self.style = None
self.E_x_samp = []
self.E_y_samp = []
self.E_x_n_samp = []
self.E_y_n_samp = []
########################################################################
......@@ -56,8 +51,10 @@ class pypho_sample(object):
def out(self, E = []):
self.E_x_samp = []
self.E_y_samp = []
E_x_samp = []
E_y_samp = []
E_x_n_samp = []
E_y_n_samp = []
if (self.style != None):
styles = self.style.split(',')
......@@ -79,11 +76,9 @@ class pypho_sample(object):
plt.subplot(2, 1, 1)
plt.plot(np.real( E[0]['E'][0][symbol*self.glova.sps + self.sampletime] ), np.imag(E[0]['E'][0][symbol*self.glova.sps + self.sampletime]), marker=styles[0], color= self.constpoints[0][2][const_dec_x.index(const_num)])
self.E_x_samp.append( E[0]['E'][0][symbol*self.glova.sps + self.sampletime] )
self.E_x_n_samp.append(const_num)
E_x_samp.append( E[0]['E'][0][symbol*self.glova.sps + self.sampletime] )
E_x_n_samp.append(const_num)
if (styles[0] != 'NO'):
plt.ylabel('$imag( E_{x})$'); plt.xlabel('$real( E_{x})$');
# Y-Pol
b = self.bits[1][symbol*self.M : symbol*self.M + self.M]
......@@ -92,13 +87,18 @@ class pypho_sample(object):
plt.subplot(2, 1, 2)
plt.plot(np.real( E[0]['E'][1][symbol*self.glova.sps + self.sampletime] ), np.imag(E[0]['E'][1][symbol*self.glova.sps+ self.sampletime]), marker=styles[1], color= self.constpoints[1][2][const_dec_y.index(const_num)])
self.E_y_samp.append( E[0]['E'][1][symbol*self.glova.sps + self.sampletime] )
self.E_y_n_samp.append(const_num)
if (styles[0] != 'NO'):
plt.ylabel('$imag( E_{y})$'); plt.xlabel('$real( E_{y})$');
E_y_samp.append( E[0]['E'][1][symbol*self.glova.sps + self.sampletime] )
E_y_n_samp.append(const_num)
return (self.E_x_samp, self.E_y_samp, self.E_x_n_samp, self.E_y_n_samp)
if (styles[0] != 'NO'):
plt.ylabel('$imag( E_{y})$'); plt.xlabel('$real( E_{y})$');
if (styles[0] != 'NO'):
plt.ylabel('$imag( E_{x})$'); plt.xlabel('$real( E_{x})$');
del self.bits
return (E_x_samp, E_y_samp, E_x_n_samp, E_y_n_samp)
########################################################################
......
......@@ -7,9 +7,8 @@
#
#
#
# Creates an electrical signal from bitpattern
# Max. Amplitude is 1 V
#
#
########################################################################
......@@ -43,11 +42,7 @@ class pypho_signalsrc(object):
self.set(pulseshape, fwhm)
return self.out(self.bitsequence, self.pulseshape, self.fwhm)
########################################################################
def out(self, bitsequence = None, pulseshape = None, fwhm = None):
if self.pulseshape == "gauss_rz":
self.tau = self.fwhm * 1/self.glova.symbolrate / 2.3582
E = self.gauss_rz()
......@@ -66,36 +61,46 @@ class pypho_signalsrc(object):
else:
print ("ERROR: No valid pulse shape")
sys.exit("PyPho stopped!")
del self.bitsequence
return E
### Create Gauss RZ Pulse shape ########################################
def gauss_rz(self):
refgauss = np.exp(-( ( self.glova.timeax - 1/self.glova.symbolrate*self.glova.nos/2 )/ self.tau)**2 / 2.0 )
refgauss = np.roll(refgauss, self.glova.sps * self.glova.nos/2 + self.glova.sps/2)
gesig = 0*refgauss
bit_ones = self.bitsequence == 1
for ndx, member in enumerate(bit_ones):
if member:
gesig += np.roll(refgauss, self.glova.sps * ndx)
return gesig
NoAb = 4
refsig = np.exp(-( ( self.glova.timeax[self.glova.nos*self.glova.sps/2 - NoAb*self.glova.sps : self.glova.nos*self.glova.sps/2 + NoAb*self.glova.sps] - 1/self.glova.symbolrate*self.glova.nos/2 )/ self.tau)**2 / 2.0 )
gesig = np.zeros(self.glova.sps*self.glova.nos + 3*2*NoAb*self.glova.sps)
bits = np.append(self.bitsequence[-NoAb:], self.bitsequence)
bits = np.append(bits, self.bitsequence[0:NoAb])
for ndx in range(0, len(bits)):
if 1 == bits[ndx]:
gesig[self.glova.sps * (ndx) : self.glova.sps * (ndx + 2*NoAb )] += refsig
del bits
del refsig
return gesig[2*NoAb*self.glova.sps - self.glova.sps/2: 2*NoAb*self.glova.sps + self.glova.sps*self.glova.nos - self.glova.sps/2]
### Create sech RZ Pulse shape ########################################
def sech_rz(self):
refsech = 1/np.cosh( (self.glova.timeax - 1/self.glova.symbolrate*self.glova.nos/2 )/ self.tau)
refsech = np.roll(refsech, self.glova.sps * self.glova.nos/2 + self.glova.sps/2)
gesig = 0*refsech
bit_ones = self.bitsequence == 1
for ndx, member in enumerate(bit_ones):
if member:
gesig += np.roll(refsech, self.glova.sps * ndx)
return gesig
NoAb = 4
refsig = 1/np.cosh( (self.glova.timeax[self.glova.nos*self.glova.sps/2 - NoAb*self.glova.sps : self.glova.nos*self.glova.sps/2 + NoAb*self.glova.sps] - 1/self.glova.symbolrate*self.glova.nos/2 )/ self.tau)
gesig = np.zeros(self.glova.sps*self.glova.nos + 3*2*NoAb*self.glova.sps)
bits = np.append(self.bitsequence[-NoAb:], self.bitsequence)
bits = np.append(bits, self.bitsequence[0:NoAb])
for ndx in range(0, len(bits)):
if 1 == bits[ndx]:
gesig[self.glova.sps * (ndx) : self.glova.sps * (ndx + 2*NoAb )] += refsig
del bits
del refsig
return gesig[2*NoAb*self.glova.sps - self.glova.sps/2: 2*NoAb*self.glova.sps + self.glova.sps*self.glova.nos - self.glova.sps/2]
### Create rectabgle pulse shape ########################################
......
......@@ -173,7 +173,7 @@ def cyfiber(int noofsamples,float l, complex[:] E_x, complex[:] E_y, float alpha
powmean_x = 0
powmean_y = 0
for i in range(noofsamples):
for i in range(noofsamples): # das kann man noch verbessern!
powmean_x += Elx[i][0]*Elx[i][0] + Elx[i][1]*Elx[i][1]
powmean_y += Ely[i][0]*Ely[i][0] + Ely[i][1]*Ely[i][1]
powmean_x /= noofsamples
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment