def fig2():
    '''
        Plot histogram and solve characteristics of probability_cut_nooverlapsability, that we cut fibers.
        Compare with binomial distribution.
        Set n_sim >> 1
    '''
    figure( 2 )
    delta = 0.
    p = probability_cut_nooverlaps( spec.l_x, fib.lf, delta )

    rvb = binom( fib.n, p )
    rvp = poisson( fib.n * p )
    rvn = norm( fib.n * p, sqrt( fib.n * p * ( 1 - p ) ) )

    graph_from = floor( bin_mean - 4 * bin_stdv )
    graph_to = floor( bin_mean + 4 * bin_stdv ) + 1


    x = arange( graph_from , graph_to )
    plot( x, n_sim * rvb.pmf( x ), color = 'red', linewidth = 2, label = 'Binomial' )
    plot( x, n_sim * rvp.pmf( x ), color = 'green', linewidth = 2, label = 'Poisson' )
    plot( x, n_sim * rvn.pdf( x ), color = 'blue', linewidth = 2, label = 'Normal' )
    #plot( x, 20 * rv.pmf( x ) )

    pdf, bins, patches = hist( v, n_sim, normed = 0 ) #, facecolor='green', alpha=1
    #set_xlim( bin_mean - 2 * bin_stdv, bin_mean + 2 * bin_stdv )
    #plot( sx, sy, 'rx' )   # centroids
    #print sum( pdf * diff( bins ) )
    legend()
    draw()
def cut_fiber_distrib( L, l, phi ):
    if L >= 2 * l:
        return l * cos( phi ) * sin( phi ) / ( L - l * cos( phi ) ) / probability_cut_nooverlaps( L, l, 0 )
    #if phi > 0 and phi < arccos( L / l / 2. ): #L > l and L < 2 * l * cos( phi ):
    else:
        return ( l * cos( phi ) * sin( phi ) / ( L - l * cos( phi ) ) / probability_cut_nooverlaps( L, l, 0 ) ) * Heaviside( phi - arccos( L / 2. / l ) ) * Heaviside( pi / 2. - phi ) + sin( phi ) / probability_cut_nooverlaps( L, l, 0 ) * Heaviside( phi ) * Heaviside( arccos( L / 2. / l ) - phi )
def cut_fibers():
    '''
        Plot cut fibers (only ellipses), solve mean value of area and embedded length of cut fibers.
        Plot histogram of embedded length.
    '''
    mask = matrix( fib.cut_func( sx, lx, sec ).astype( 'bool' ) )
    sx_cut = sx[ mask ]
    sy_cut = sy[ mask ]
    sz_cut = sz[ mask ]
    phi_x_cut = phi_x[ mask ]
    theta_cut = theta[ mask ]
    def plot_cross_cut():
        #plot cut plane with crossection of cut fibers
        fig55 = figure( 55 )
        title( 'Cut fibers area (ellipses)' )
        ax55 = Axes( fig55, [.1, .1, .8, .8] )
        fig55.add_axes( ax55 )
        for i in range( 0, len( phi_x_cut ) ):
            sy_c = sy_cut[i] + ( sec - sx_cut[i] ) / cos( phi_x_cut[i] ) * sin( phi_x_cut[i] ) * cos( theta_cut[i] )
            sz_c = sz_cut[i] + ( sec - sx_cut[i] ) / cos( phi_x_cut[i] ) * sin( phi_x_cut[i] ) * sin( theta_cut[i] )
            if ( 0 < phi_x_cut[i] < arctan( ( fib.lf ) / fib.df ) ) and ( sx_cut[i] - fib.lf / 2. * cos( phi_x_cut[i] ) + fib.df / 2. * sin( phi_x_cut[i] ) < sec < sx_cut[i] + fib.lf / 2. * cos( phi_x_cut[i] ) - fib.df / 2. * sin( phi_x_cut[i] ) ):
                patch = Ellipse( [ sy_c, sz_c ] , fib.df, fib.df / cos( phi_x_cut[i] ), theta_cut[i] * 180 / pi , color = 'black' )
                ax55.add_artist( patch )
        ax55.set_xlim( -spec.l_y / 2., spec.l_y / 2. )
        ax55.set_ylim( -spec.l_z / 2., spec.l_z / 2. )
    #plot_cross_cut() #plot cut plane
    A_cut = sum( cut_area_func( fib.df, fib.lf, sx_cut, phi_x_cut, sec ) )
    le_cut = le_sim( phi_x_cut, abs( sx_cut - sec * ones_like( sx_cut ) ), fib.lf )
    le_cut_null = le_sim( phi_x, abs( sx - sec * ones_like( sx ) ), fib.lf ) * fib.cut_func( sx, lx, sec )
    fig6 = figure( 6 )
    ax6 = Axes( fig6, [.1, .1, .8, .8] )
    hist( le_cut, 50, normed = 0 )
    #hist( le_cut_null[0], 50, normed=0 )
    title( 'Embedded length l_e' )
    figure( 8 )
    n_mask = 10
    title( 'Histograms of $\phi$ fraction = $\pi/%i$' % n_mask )
    for i in range( 0, n_mask ):
        mask1 = phi_x_cut > i * pi / 2. / n_mask
        mask2 = phi_x_cut < ( i + 1 ) * pi / 2. / n_mask
        pdf, bins, patches = hist( le_cut[mask1 * mask2], 50 , histtype = 'step', label = '%i' % i )
        legend()
        #print mean( pdf )
    figure( 9 )
    title( 'Histogram of cut fibers angle' )
    xx = linspace( 0, pi / 2., 100 )
    hist( phi_x_cut, 50, normed = 1 )
    plot( xx, sin( 2 * xx ), color = 'red', linewidth = 4, label = 'overlaps' )
    plot( xx, cut_fiber_distrib( spec.l_x, fib.lf, xx ), color = 'green', linewidth = 4, label = 'nooverlaps' )
    legend()

    fig10 = figure( 10 )
    title( '2D histogram of le and phi_x' )
    ax10 = Axes3D( fig10 )
    bin = 20
    H, xedges, yedges = histogram2d( le_cut, phi_x_cut, ( bin, bin ), normed = 1 )
    #extent = [xedges[0] * 100, xedges[-1] * 100, yedges[0], yedges[-1]] #[xedges[0], xedges[-1], yedges[0], yedges[-1]]
    #im = imshow( H )#'binary', cmap='jet' , extent=extent
    #im.set_interpolation( 'bilinear' )
    #colorbar()
    x = ( xedges[range( 0, len( xedges ) - 1 )] + xedges[range( 1, len( xedges ) )] ) / 2.
    y = ( yedges[range( 0, len( yedges ) - 1 )] + yedges[range( 1, len( yedges ) )] ) / 2.
    #ax10.scatter3D( xedges.ravel(), yedges.ravel(), H.ravel() )
    xx = outer( x, ones( len( y ) ) )
    yy = outer( ones( len( x ) ), y )
    z = outer( ones( len( y ) ), sin( 2 * y ) ) / fib.lf * 2# * probability_cut_nooverlaps( spec.l_x, fib.lf, 0 )#* fib.n
    zz = outer( ones( len( y ) ), cut_fiber_distrib( spec.l_x, fib.lf, y ) / fib.lf * 2 )
    ax10.plot_wireframe( xx, yy, H )#, rstride=1, cstride=1 
    ax10.plot_wireframe( xx, yy, z , color = 'red', label = 'overlaps' )
    ax10.plot_wireframe( xx, yy, zz , color = 'green', label = 'nooverlaps' )
    legend()
    print '%%%%%%%%%%%%%%%%%%%'
    print '%%Embedded length%%'
    print '%%%%%%%%%%%%%%%%%%%'
    print 'probability cut fibers', probability_cut_nooverlaps( spec.l_x, fib.lf, 0. )
    print 'le_mean cut fibers (int/p) \t', le( spec.l_x, fib.lf ) / probability_cut_nooverlaps( spec.l_x, fib.lf, 0. )
    print 'le_mean of cut fibers (sim) \t', mean( le_cut ), '\t |\t standard deviation \t', std( le_cut )
    print 'include nulls (sim) \t\t', mean( le_cut_null )
    print 'E[le] (int) \t\t\t', le( spec.l_x, fib.lf )
    print 'cut fibers area', A_cut, 'Ac', spec._get_cross_area(), 'af', A_cut / spec._get_cross_area()*100, '%'


print 'mean P and var P', mean( P )









#print 'number of cut fibers', v

p = probability_cut_nooverlaps( spec.l_x, fib.lf, sec )
bin_mean = fib.n * p
bin_stdv = sqrt( bin_mean * ( 1 - p ) )
bin_skew = ( 1 - 2 * p ) / bin_stdv

poiss_lambda = fib.n * p
poiss_mean = poiss_lambda
poiss_stdv = poiss_lambda ** ( 1 / 2. )
poiss_skew = poiss_lambda ** ( -1 / 2. )

# Volume fraction
Vf = fib._get_volume()
Vc = spec._get_volume()
vol_frac_V = Vf / Vc * 100 * fib.n

#vol_frac_A = mean( v_vol_frac_A ) / Ac * 100