# -*- coding: cp1250 -*-
import math
import databaze_materialu_3v1
import dovolene_namahani

class vypocet:
    def vypocet_ad2000_merkblatt(self, Temp, Temp_mont, material_sroubu, tloustka_sroubu, tlakova_zkouska,
                                 neckeddown, yield_point, unmachined, p, di, dD, n, da, dL, sF, s1,
                                 dt, hA, hF, gasket_material, tekutina, bD, hD, material_priruby,
                                 tloustka_priruby):

        _materialy = databaze_materialu_3v1.materialy() # instance databaze materialu
        _namahani = dovolene_namahani.namahani() # instance databaze materialu
        podminky_plneni = []

        # materialove vlastnosti sroubu
        Rp02t_sroubu = _materialy.tyce(Temp, material_sroubu, tloustka_sroubu)[0]
        Rm_sroubu = _materialy.tyce(Temp, material_sroubu, tloustka_sroubu)[1]
        A_sroubu = _materialy.tyce(Temp, material_sroubu, tloustka_sroubu)[2] 
        material_info_sroubu = _materialy.tyce(Temp, material_sroubu, tloustka_sroubu)[3]
        tloustka_materialu_sroubu = _materialy.tyce(Temp, material_sroubu, tloustka_sroubu)[4] 
##        podminky_plneni.append(tloustka_materialu_sroubu)

        # materiálové vlastnosti příruba
        Rp02t_priruba = _materialy.priruba(Temp, material_priruby, tloustka_priruby)[0]
        Rm_priruba = _materialy.priruba(Temp, material_priruby, tloustka_priruby)[1]
        A_priruba = _materialy.priruba(Temp, material_priruby, tloustka_priruby)[2]
        material_info_priruba = _materialy.priruba(Temp, material_priruby, tloustka_priruby)[3]
        tloustka_materialu_priruba = _materialy.priruba(Temp, material_priruby, tloustka_priruby)[4]
##        podminky_plneni.append(tloustka_materialu_priruba)

        # materiálové vlastnosti těsnění
        gasket = _materialy.tesneni(tekutina, bD, hD, gasket_material, Temp)[0]
        k1 = _materialy.tesneni(tekutina, bD, hD, gasket_material, Temp)[1]
        KDtheta = _materialy.tesneni(tekutina, bD, hD, gasket_material, Temp)[2]
        
        # stanoveni dovoleneho namahani
        K_priruba = _namahani.dovolene_namahani_priruby(tlakova_zkouska, Temp, A_priruba, Rp02t_priruba, Rm_priruba, Rm_priruba)
        K_sroubu = _namahani.dovolene_namahani_sroubu(tlakova_zkouska, Temp, A_sroubu, Rp02t_sroubu, Rm_sroubu, Rm_sroubu)
        K20_priruba = _namahani.dovolene_namahani_priruby(tlakova_zkouska, 20., A_priruba, Rp02t_priruba, Rm_priruba, Rm_priruba)
        K20_sroubu = _namahani.dovolene_namahani_sroubu(tlakova_zkouska, 20., A_sroubu, Rp02t_sroubu, Rm_sroubu, Rm_sroubu)

        zaznam_ad2000_merkblatt = []
        
        zaznam_ad2000_merkblatt.append(Rp02t_sroubu)
        zaznam_ad2000_merkblatt.append(Rm_sroubu)
        zaznam_ad2000_merkblatt.append(A_sroubu)
        zaznam_ad2000_merkblatt.append(material_info_sroubu)
        zaznam_ad2000_merkblatt.append(Rp02t_priruba)
        zaznam_ad2000_merkblatt.append(Rm_priruba)
        zaznam_ad2000_merkblatt.append(A_priruba)
        zaznam_ad2000_merkblatt.append(material_info_priruba)

        zaznam_ad2000_merkblatt.append(K_priruba)
        zaznam_ad2000_merkblatt.append(K_sroubu)
        zaznam_ad2000_merkblatt.append(K20_priruba)
        zaznam_ad2000_merkblatt.append(K20_sroubu)

        # vstupní podmínky, rouby
        if Temp <= 120.:
            podminky_plneni.append(False)
            podminky_plneni.append('vstupni_podminky')
        else:
            podminky_plneni.append(True)
            podminky_plneni.append('vstupni_podminky')

        if tlakova_zkouska == False:
            if neckeddown == False:
                if yield_point == True:
                    S = 1.8
                    if unmachined == False:
                        Z = 1.75
                        rho = 0.75
                    else:
                        Z = 1.51
                        rho = 1.0
                else:
                    S = 5.0
                    if unmachined == False:
                        Z = 2.91
                    else:
                        Z = 2.52
            else:
                if yield_point == True:
                    S = 1.5
                    if unmachined == False:
                        Z = 1.6
                        rho = 0.75
                    else:
                        Z = 1.38
                        rho = 1.0
                else:
                    S = 5.0
                    if unmachined == False:
                        Z = 2.91
                    else:
                        Z = 2.52
        else:
            if neckeddown == False:
                if yield_point == True:
                    S = 1.3
                    if unmachined == False:
                        Z = 1.49
                        rho = 0.75
                    else:
                        Z = 1.29
                        rho = 1.0
                else:
                    S = 3.0
                    if unmachined == False:
                        Z = 2.26
                    else:
                        Z = 1.95
            else:
                if yield_point == True:
                    S = 1.1
                    if unmachined == False:
                        Z = 1.37
                        rho = 0.75
                    else:
                        Z = 1.18
                        rho = 1.0
                else:
                    S = 3.0
                    if unmachined == False:
                        Z = 2.26
                    else:
                        Z = 1.95
        
        # Circular bolted flange connections with the gasket within the bolt-circle diameter
        SD = 1.2
        FRB=(p*math.pi*di**2)/40
        FFB=(p*math.pi*((dD**2)-di**2))/40
        FDB=p*math.pi*dD*SD*k1/10
        FSB=FRB+FFB+FDB # FSB - force???

        FDV_=math.pi*dD*gasket # the minimum required bold load for the bolting-up condition
        if FDV_<FSB:
            FDV=FDV_
        else:
            FDV=0.2*FDV_+0.8*(math.sqrt(FSB*FDV_))

        FD_theta=math.pi*dD*gasket # dovolene zatizeni za provozu pro kovove tesneni
##        FD_theta=math.pi*dD*k0*KD_theta # dovolene zatizeni za provozu pro kovove tesneni
##        FD_theta2=math.pi*dD*(math.sqrt(X))*k0*KD_theta # pro vroubkované kovové těsnění
        
        if FD_theta >= FSB:
            podminky_plneni.append(True)
            podminky_plneni.append('podminka_predepjatosti')
        else:
            podminky_plneni.append(False)
            podminky_plneni.append('podminka_predepjatosti')

        # Určení průměru roubu
        Z_= Z
        if Z_*math.sqrt(FSB/(K_sroubu*n)) <= 20.:
            c5 = 3.
        elif Z_*math.sqrt(FSB/(K_sroubu*n))>= 50.:
            c5 = 1.
        else:
            c5 = (65-Z_*math.sqrt(FSB/(K_sroubu*n)))/15.

        dK_provoz=Z_*(math.sqrt(FSB/(K_sroubu*n)))+c5
        dK_test=Z_*(math.sqrt(FSB/(K20_sroubu*n)))
        dK_montaz=Z_*(math.sqrt(FDV/(K20_sroubu*n)))

        dK=max(dK_provoz,dK_test,dK_montaz)
        dS=dK
        
        zaznam_ad2000_merkblatt.append(S)
        zaznam_ad2000_merkblatt.append(Z)
        zaznam_ad2000_merkblatt.append(FRB)
        zaznam_ad2000_merkblatt.append(FFB)
        zaznam_ad2000_merkblatt.append(FDB)
        zaznam_ad2000_merkblatt.append(FSB)
        zaznam_ad2000_merkblatt.append(FDV)
        zaznam_ad2000_merkblatt.append(FD_theta)
        zaznam_ad2000_merkblatt.append(c5)
        zaznam_ad2000_merkblatt.append(dK_provoz)
        zaznam_ad2000_merkblatt.append(dK_test)
        zaznam_ad2000_merkblatt.append(dK_montaz)
        zaznam_ad2000_merkblatt.append(dS)

        # Výpočet přírub
        if n >= 4:
            podminky_plneni.append(True)
            podminky_plneni.append('podminka_poctu_sroubu')
        else:
            podminky_plneni.append(False)
            podminky_plneni.append('podminka_poctu_sroubu')
            
        if sF <= hF/3.:
            podminky_plneni.append(True)
            podminky_plneni.append('podminka_zuzeni_krku')
        else:
            podminky_plneni.append(False)
            podminky_plneni.append('podminka_zuzeni_krku')

        Z=(di+sF)*(sF**2) # auxiliary value (pomocná hodnota)
        Z1=(3.*(di+s1)*(s1**2))/4

        # fig.3
        if di <= 500.:
            v = 0.5+0.001*di
        else:
            v = 0.5
            
        d_L=v*dL # d_L - reduced bolt hole diameter Fig. 3
        b=da-di-2*d_L # b - width
        S = 4.0
        S_ = 3.0

        # section A-A (AA)
        a_AA=(dt-di-sF)/2. # a - moment arms of the bolt  load (operating condition)
        aD_AA=(dt-dD)/2. # aD - moment arms of the bolt  load (bolting-up condition)
        W_provozAA=FSB*S*a_AA/K_priruba # W - flange section modulus 
        W_montazAA=FDV*S_*aD_AA/K20_priruba
        if di > 1000. and (hA - hF) >= 0.6 and (sF - s1) >= (0.25*hF):
            try:
                hF_provozAA=(math.sqrt(((1.06*W_provozAA-0.8*Z)/b)+(0.05*Z/(b*sF))))-(0.05*Z/(b*sF))
            except ValueError:
                hF_provozAA=None
            try:
                hF_montazAA=(math.sqrt(((1.06*W_montazAA-0.8*Z)/b)+(0.05*Z/(b*sF))))-(0.05*Z/(b*sF))
            except ValueError:
                hF_montazAA=None
        else:
            try:
                hF_provozAA=math.sqrt((1.27*W_provozAA-Z)/b) # hF - flange thickness
            except ValueError:
                hF_provozAA = None
            try:
                hF_montazAA=math.sqrt((1.27*W_montazAA-Z)/b)
            except ValueError:
                hF_montazAA = None

        # section B-B (BB)
        if 0.5 <= (hA-hF)/hF <= 1.0:
            podminky_plneni.append(True)
            podminky_plneni.append('podminka_sectionBB-1')
        else:
            podminky_plneni.append(False)
            podminky_plneni.append('podminka_sectionBB-1')
            
        if 0.1 <= (s1+sF)/b <= 0.3:
            podminky_plneni.append(True)
            podminky_plneni.append('podminka_sectionBB-2')
        else:
            podminky_plneni.append(False)
            podminky_plneni.append('podminka_sectionBB-2')

        sm = (sF+s1)/2
        B1 = (hA-hF)/hF
        a_BB=(dt-di-s1)/2.
        aD_BB=(dt-dD)/2.
        W_provozBB=FSB*S*a_BB/K_priruba # W - flange section modulus 
        W_montazBB=FDV*S_*aD_BB/K20_priruba
        
        B=(1+(2*sm*B1/b))/(1+(2*sm*(2*B1+B1**2))/b)

        if di > 1000. and (hA - hF) >= 0.6 and (sF - s1) >= (0.25*hF):
            try:
                hF_provozBB = (math.sqrt(((1.06*W_provozBB-2.0*Z1)/b)))
            except ValueError:
                hF_provozBB=None
            try:
                hF_montazBB = (math.sqrt(((1.06*W_montazBB-2.0*Z1)/b)))
            except ValueError:
                hF_montazBB=None
        else:
            try:
                hF_provozBB=B*(math.sqrt((1.27*W_provozBB-Z1)/b))
            except ValueError:
                hF_provozBB = None
            try:
                hF_montazBB=B*(math.sqrt((1.27*W_montazBB-Z1)/b))
            except ValueError:
                hF_montazBB = None

        zaznam_ad2000_merkblatt.append(b)
        zaznam_ad2000_merkblatt.append(Z)
        zaznam_ad2000_merkblatt.append(Z1)
        zaznam_ad2000_merkblatt.append(v)
        zaznam_ad2000_merkblatt.append(d_L)
        zaznam_ad2000_merkblatt.append(a_AA)
        zaznam_ad2000_merkblatt.append(aD_AA)
        zaznam_ad2000_merkblatt.append(W_provozAA)
        zaznam_ad2000_merkblatt.append(W_montazAA)
        zaznam_ad2000_merkblatt.append(hF_provozAA)
        zaznam_ad2000_merkblatt.append(hF_montazAA)
        zaznam_ad2000_merkblatt.append(sm)
        zaznam_ad2000_merkblatt.append(B1)
        zaznam_ad2000_merkblatt.append(a_BB)
        zaznam_ad2000_merkblatt.append(aD_BB)
        zaznam_ad2000_merkblatt.append(W_provozBB)
        zaznam_ad2000_merkblatt.append(W_montazBB)
        zaznam_ad2000_merkblatt.append(B)
        zaznam_ad2000_merkblatt.append(hF_provozBB)
        zaznam_ad2000_merkblatt.append(hF_montazBB)

        return zaznam_ad2000_merkblatt, podminky_plneni

    def vypis(self):
        if vypis == True:
            # ZKOUKA VÝPOČTU SF
            print "Podmínka velikosti sF"
            if sF<=1*hF_provozAA/3. and sF<=1*hF_montazAA/3. and sF<=1*hF_provozBB/3. and sF<=1*hF_montazBB/3.:
                    print "True", "sF=", round(sF,2)
            else:
                    print "False", "sF=", round(sF,2)

            print "_____________________"
            print "VLASTNOSTI U ROUBŮ"
            print "FRB =",round(FRB,2)
            print "FFB =",round(FFB,2)
            print "FDB =",round(FDB,2)
            print "FSB =",round(FSB,2)
            print "FDV =",round(FDV,2)
            print "c5 =",round(c5,2)
            print "dK =",round(dK,2)
            print "dS =",round(dS,2)
            print "_____________________"
            print "d_L =",round(d_L,2)
            print "b =",round(b,2)
            print "Z =",round(Z,2)
            print "Z1 =",round(Z1,2)
            print "a_AA =",round(a_AA,2)
            print "aD_AA =",round(aD_AA,2)
            print "a_BB =",round(a_BB,2)
            print "aD_BB =",round(aD_BB,2)
            print "W_provozAA =",round(W_provozAA,2)
            print "W_montazAA =",round(W_montazAA,2)
            print "hF_provozAA =",round(hF_provozAA,2)
            print "hF_montazAA =",round(hF_montazAA,2)
            print "hF_provozBB =",round(hF_provozBB,2)
            print "hF_montazBB =",round(hF_montazBB,2)
        else:
            pass # příkaz k přeskočení podmínky - neprovede nic
