samoylv / WPG

WPG: WavePropaGator, Interactive framework for X-ray FEL optics design and simulations.
Other
26 stars 20 forks source link

Memory leaks? #63

Open CFGrote opened 8 years ago

CFGrote commented 8 years ago

I observed that while looping over a set of parameters (e.g. focus position) doing beamline setup and propagation (day one configuation for spb/sfx), memory occupation just goes up, eventually hitting the exflst server max capacity (which is some tens of GB). Can post script and memory trace if this is of interest. Current workaround is to delete all wavefronts and beamlines at the end of each iteration and set them up at the beginning of the loop.

samoylv commented 8 years ago

yes, it would be great if you could send the example to me and Alexey

CFGrote commented 8 years ago

see memory log and script used to generate it attached. it's not as bad as i recollect but still worthwhile exploring if time. wpg_memlog

""" """

from future import absolute_import from future import division from future import print_function from future import unicode_literals

import os import sys import numpy import copy import matplotlib matplotlib.use('Qt4Agg')

matplotlib.use('pdf')

sys.path.insert(0,'/afs/desy.de/group/exfel/software/wpg/latest/')

sys.path.insert(0,'/home/grotec/mnt/afs/desy.de/group/exfel/software/wpg/alpha/')

sys.path.insert(0,'/home/grotec/Work/XFEL/Codes/WPG')

sys.path.insert(0,'/data/fhgfs/grotec/Codes/wpg_alpha')

from wpg import Wavefront, Beamline from wpg.optical_elements import Drift from wpg.optical_elements import Aperture from wpg.optical_elements import CRL from wpg.optical_elements import WF_dist from wpg.optical_elements import calculateOPD from wpg.optical_elements import Use_PP

from auxiliary_functions import mkdir_p, calculate_theta_fwhm_cdr

import SRW core functions

from wpg.srwlib import srwl

Gaussian beam generator

from wpg.generators import build_gauss_wavefront

from wpg.wpg_uti_wf import calculate_fwhm from wpg.wpg_uti_wf import plot_t_wf,look_at_q_space from wpg.wpg_uti_oe import show_transmission

strInputDataFolder ='input_data' # sub-folder name for common input data strDataFolderName = 'output_data' # output data sub-folder name if not os.path.exists(strDataFolderName): mkdir_p(strDataFolderName)

Geometry

src_to_hom1 = 257.8 # Distance source to HOM 1 [m] src_to_hom2 = 267.8 # Distance source to HOM 2 [m] src_to_crl = 887.8 # Distance source to CRL [m] src_to_exp = 920.42 # Distance source to experiment [m]

Central photon energy.

ekev = 8.4 # Energy [keV]

Pulse parameters.

qnC = 0.5 # e-bunch charge, [nC] pulse_duration = 9.e-15 # [s] pulseEnergy = 1.5e-3 # total pulse energy, J

Coherence time

coh_time = 0.24e-15 # [s]

Distance to first HOM.

z1 = src_to_hom1

Angular distribution

theta_fwhm = calculate_theta_fwhm_cdr(ekev,qnC) # From tutorial

print(theta_fwhm)

theta_fwhm = 2.124e-6 # Beam divergence # From Patrick's raytrace.

wlambda = 12.4_1e-10/ekev # wavelength [AKM] w0 = wlambda/(numpy.pi_theta_fwhm) # beam waist zR = (numpy.pi_w0__2)/wlambda #Rayleigh range fwhm_at_zR = theta_fwhm_zR #FWHM at Rayleigh range sigmaAmp = w0/(2*numpy.sqrt(numpy.log(2))) #sigma of amplitude

Print out the calculated wavefront parameters and expected FWHM

print ('######################')

print ('# Beam size report #')

print('# theta_fwhm:\t{:.2f}urad'.format(theta_fwhm_1e6))

print('# fwhm waist:\t{:.1f}um'.format(w0_1e6))

print('# lambda:\t{:.2f}nm'.format(wlambda_1e9))

print('# 2_z_R:\t{:.1f}m'.format(2_zR))

print('# sigma for amplitude:\t{:.1f}um'.format(sigmaAmp_1e6))

#

print('# expected FWHM at distance {:.1f}m is {:.1f}mm'.format(src_to_hom1,theta_fwhm_src_to_hom1_1e3))

print ('######################')

expected beam radius at HOM1 position to get the range of the wavefront

range_xy = w0_numpy.sqrt(1+(src_to_hom1/zR)__2) *5.5

print('range_xy at HOM1: {:.1f} mm'.format(range_xy_1e3))

fname = 'at_{:.0f}_m'.format(src_to_hom1)

Number of points in each x and y dimension.

np=400

bSaved=False dx = 10.e-6; range_xy = dx*(np-1)

print ('range_xy = ', range_xy)

nslices = 20;

fwhm_file = open('../results/w_crl_aperture/fwhm.txt', 'w')

zs = numpy.linspace(31.5, 33.5, 21)

mirror_profile_scales = [1.0] crl_front_aperture_diameters = [2.8e-3] for crl_front_aperture_diameter in crl_front_aperture_diameters: for mirror_profile_scale in mirror_profile_scales: print ('#############################################') print (' Running case:') print (' mirror profile scale %2.1f' % (mirror_profile_scale)) print (' crl aperture diameter %2.1f mm' % (crl_front_aperture_diameter*1.0e3))

Construct srw wavefront.

    srwl_wf = build_gauss_wavefront(np, np, nslices, ekev, -range_xy/2, range_xy/2,
                                    -range_xy/2, range_xy/2 ,coh_time/numpy.sqrt(2),
                                    sigmaAmp, sigmaAmp, src_to_hom1,
                                    pulseEn=pulseEnergy, pulseRange=8.)
    # Convert to wpg.
    wf = Wavefront(srwl_wf)

    # Set first propagation distance.
    z0 = src_to_hom1
    #defining name HDF5 file for storing wavefront
    strOutInDataFolder = 'output_data'
    #store wavefront to HDF5 file
    if bSaved:
        wf.store_hdf5(fname+'.h5'); print('saving WF to %s' %fname+'.h5')
    #print('FWHM at distance {:.1f} m:'.format(z0));print(calculate_fwhm(wf))
#
    #print( 'x-range[mm] ({:.1f}, {:.1f}),\tn = {:d}'.format(wf.params.Mesh.xMin*1e3,wf.params.Mesh.xMax*1e3,
                                                        #wf.params.Mesh.nx))
    #print( 'y-range[mm] ({:.1f}, {:.1f}),\tn = {:d}'.format(wf.params.Mesh.yMin*1e3,wf.params.Mesh.yMax*1e3,
                                                        #wf.params.Mesh.ny))
    #print( 'dy {:.1f} um'.format((wf.params.Mesh.yMax-wf.params.Mesh.yMin)*1e6/(wf.params.Mesh.ny-1.)))
    #print( 'dx {:.1f} um'.format((wf.params.Mesh.xMax-wf.params.Mesh.xMin)*1e6/(wf.params.Mesh.nx-1.)))
#
    # Wavefront diagnostic.
    #plot_t_wf(wf, save='../results/w_crl_aperture/wf_R_hom1.pdf')
    #plot_t_wf(wf, save='')
    #look_at_q_space(wf, save='../results/w_crl_aperture/wf_Q_hom1.pdf')
    #look_at_q_space(wf, save='')
    #sys.exit()

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

    #Incidence angle at HOM
    theta_om = 3.6e-3       # [rad]

    om_mirror_length = 0.8 # [m]
    om_clear_ap = om_mirror_length*theta_om

    #define the beamline:
    src_to_crl_beamline = Beamline()
    zoom=1

    # Define HOM1 = Aperture + Wavefront distortion.
    aperture_x_to_y_ratio = 1
    hom1_aperture = Aperture(shape='r',ap_or_ob='a',Dx=om_clear_ap,Dy=om_clear_ap/aperture_x_to_y_ratio)
    hom1_wavefront_distortion = WF_dist(nx=1500,ny=100,Dx=om_clear_ap,Dy=range_xy) #nx, ny from tutorial #3 (new).
    # Load mirror profile data.
    mirror_data_file = 'mirror2.dat'
    mirror_data = numpy.loadtxt(mirror_data_file)
    # Apply distortion.
    hom1_wavefront_distortion = calculateOPD( wf_dist=hom1_wavefront_distortion,
                                              mdatafile=mirror_data_file,
                                              ncol=2,
                                              delim=' ',
                                              Orient='x',
                                              theta=theta_om,
                                              scale=mirror_profile_scale,
                                              stretching=1.)
    ## Visualize mirror profile.
    #pylab.plot( mirror_data[:,0], mirror_data[:,1], label="Mirror profile")
    #pylab.safefig()

    # Append to beamline.
    src_to_crl_beamline.append( hom1_aperture, Use_PP(semi_analytical_treatment=0, zoom=zoom, sampling=zoom) )
    if mirror_profile_scale != 0.0:
        src_to_crl_beamline.append( hom1_wavefront_distortion, Use_PP(semi_analytical_treatment=0, zoom=zoom, sampling=zoom) )

    # Free space propagation from hom1 to hom2
    hom1_to_hom2_drift = Drift(src_to_hom2 - src_to_hom1)
    src_to_crl_beamline.append( hom1_to_hom2_drift, Use_PP(semi_analytical_treatment=0))

    # Define HOM2.
    zoom = 1.0
    hom2_aperture = Aperture('r','a', om_clear_ap, om_clear_ap/aperture_x_to_y_ratio)
    hom2_wavefront_distortion = WF_dist(nx=1500,ny=100,Dx=om_clear_ap,Dy=range_xy) #nx, ny from tutorial #3 (new).
    # Apply distortion.
    hom2_wavefront_distortion = calculateOPD( wf_dist=hom2_wavefront_distortion,
                                              mdatafile=mirror_data_file,
                                              ncol=2,
                                              delim=' ',
                                              Orient='x',
                                              theta=theta_om,
                                              scale=mirror_profile_scale,
                                              stretching=1.)
    # Append to beamline.
    src_to_crl_beamline.append( hom2_aperture,  Use_PP(semi_analytical_treatment=0, zoom=zoom, sampling=zoom))
    if mirror_profile_scale != 0.0:
        src_to_crl_beamline.append( hom2_wavefront_distortion, Use_PP(semi_analytical_treatment=0, zoom=zoom, sampling=zoom) )

    #drift to CRL aperture
    hom2_to_crl_drift = Drift( src_to_crl - src_to_hom2 )
    src_to_crl_beamline.append( hom2_to_crl_drift, Use_PP(semi_analytical_treatment=1))

    ##### Propagate to CRL aperture. #####
    #srwl.SetRepresElecField(wf._srwl_wf, 'f') # <---- switch to frequency domain
    #src_to_crl_beamline.propagate(wf)
    #srwl.SetRepresElecField(wf._srwl_wf, 't')
    #print('FWHM at crl entrance aperture:')
    #print(calculate_fwhm(wf))
    #plot_t_wf(wf, save='../results/w_crl_aperture/wf_R_crl_aperture_mp%2.1f.pdf' % (mirror_profile_scale) )
    #look_at_q_space(wf, savefig='../results/w_crl_aperture/wf_Q_crl_aperture_mp%2.1f.pdf' % (mirror_profile_scale))
    ######################################

    # Circular Aperture before CRL.
    crl_front_aperture = Aperture('c','a', crl_front_aperture_diameter, crl_front_aperture_diameter)

    ### Define CRL
    crl_focussing_plane = 3 # Both horizontal and vertical.
    crl_delta = 4.8308e-06 # Refractive index decrement (n = 1- delta - i*beta) @ 8.4 keV
    crl_attenuation_length  = 6.053e-3    # Attenuation length [m], Henke data.
    crl_shape = 1         # Parabolic lenses
    crl_aperture = 3.0e-3 # [m]
    crl_curvature_radius = 5.8e-3 # [m]
    crl_number_of_lenses = 19
    crl_wall_thickness = 8.0e-5 # Thickness
    crl_center_horizontal_coordinate = 0.0
    crl_center_vertical_coordinate = 0.0
    crl_initial_photon_energy = 8.48e3 # [eV]
    crl_final_photon_energy = 8.52e3 # [eV]

    crl = CRL( _foc_plane=crl_focussing_plane,
               _delta=crl_delta,
               _atten_len=crl_attenuation_length,
               _shape=crl_shape,
               _apert_h=crl_aperture,
               _apert_v=crl_aperture,
               _r_min=crl_curvature_radius,
               _n=crl_number_of_lenses,
               _wall_thick=crl_wall_thickness,
               _xc=crl_center_horizontal_coordinate,
               _yc=crl_center_vertical_coordinate,
               _void_cen_rad=None,
               _e_start=crl_initial_photon_energy,
               _e_fin=crl_final_photon_energy,
               )

    #pylab.set_cmap('bone')
    #show_transmission(crl)
    #pylab.show()
    #pylab.set_cmap('YlGnBu_r')

    zoom = 0.6
    src_to_crl_beamline.append( crl_front_aperture, Use_PP(semi_analytical_treatment=0, zoom=zoom, sampling=zoom/0.1) )
    src_to_crl_beamline.append( crl, Use_PP(semi_analytical_treatment=0, zoom=1, sampling=1) )

    # Propagate:
    srwl.SetRepresElecField( wf._srwl_wf, 'f')
    src_to_crl_beamline.propagate(wf)
    srwl.SetRepresElecField( wf._srwl_wf, 't')
    focal_size_dict = calculate_fwhm(wf)
    fwhm_file.write('%2.1f\t%2.1f\t%e\t%e\n' % ( crl_front_aperture_diameter*1e3, mirror_profile_scale, focal_size_dict['fwhm_x'], focal_size_dict['fwhm_y'] ) )
    #############################################################################

    for z in numpy.linspace(31., 33., 21):
        # Beamline from crl to experiment.
        crl_to_exp_beamline = Beamline()

        wf1 = copy.deepcopy(wf)
        # Drift to focus aperture
        crl_to_exp_drift = Drift( z )
        crl_to_exp_beamline.append( crl_to_exp_drift, Use_PP(semi_analytical_treatment=1, zoom=1, sampling=1) )

        # Propagate:
        srwl.SetRepresElecField( wf1._srwl_wf, 'f')
        crl_to_exp_beamline.propagate(wf1)
        srwl.SetRepresElecField( wf1._srwl_wf, 't')
        focal_size_dict = calculate_fwhm(wf1)
        fwhm_file.write('%2.1f\t%2.1f\t%e\t%e\n' % ( crl_front_aperture_diameter*1e3, mirror_profile_scale, focal_size_dict['fwhm_x'], focal_size_dict['fwhm_y'] ) )
        #plot_t_wf(wf, save='../results/w_crl_aperture/wf_R_exp_mp%2.1f_crlap%2.1fmm_z%4.2fm.pdf' % (mirror_profile_scale, 1e3*crl_front_aperture_diameter, z), range_x=0.04, range_y=0.04)
        #look_at_q_space(wf, savefig='../results/w_crl_aperture/wf_Q_exp_mp%2.1f_crlap%2.1fmm_z%4.2fm.pdf' % (mirror_profile_scale, 1e3*crl_front_aperture_diameter, z))

        del wf1
        del crl_to_exp_beamline

    #exp_zoom_beamline = Beamline()
    #exp_zoom_beamline.append(Empty(),Use_PP(zoom_v=0.5,zoom_h=0.5))
    #srwl.SetRepresElecField(wf._srwl_wf, 'f') # <---- switch to frequency domain
    #exp_zoom_beamline.propagate(wf)
    #srwl.SetRepresElecField(wf._srwl_wf, 't')
    #print('FWHM at distance {:.1f} m:'.format(z0));print(calculate_fwhm(wf))
    #plot_t_wf(wf)

fwhm_file.close()

samoylv commented 8 years ago

@buzmakov: could it be connected with #74 ?

CFGrote commented 8 years ago

still present: screenshot from 2016-09-05 13-40-42

buzmakov commented 8 years ago

Python has I'ts own garbage collector, so I even say 'del object_name' python may keep it in memory until GC runs.

if you use IPython notebooks for calculations please use '%xdel wf1, crl_to_exp_beamline' instead 'del wf1, crl_to_exp_beamline'. This tell IPython remove objects from cache.

I'll look on it more accuralty.

пн, 5 сент. 2016 г. в 14:42, Carsten Fortmann-Grote < notifications@github.com>:

still present: [image: screenshot from 2016-09-05 13-40-42] https://cloud.githubusercontent.com/assets/15476807/18246827/82641ee0-736e-11e6-9519-df08dac59731.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/samoylv/WPG/issues/63#issuecomment-244727106, or mute the thread https://github.com/notifications/unsubscribe-auth/AA6Pt1PlY2BxJ84PIalOLaw_w5HT6b3Oks5qnAAJgaJpZM4HuirG .