import math
import databaze_materialu_4v1

class vypocet:
    def vypocet_asme(self,Temp,Temp_mont,material_sroubu,tloustka_sroubu,material_tesneni,tlakova_zkouska,
                     A,B,C,g0,g1,h,G,P,t,nb,a,b0,material_priruby,tloustka_priruby,material_plaste,
                     tloustka_plaste):
        # integralni metoda

        _materialy = databaze_materialu_4v1.materialy()
        podminky_plneni = []

        # materiál těsnění
        m = _materialy.tesneni(material_tesneni)[0]
        y = _materialy.tesneni(material_tesneni)[1]
        w_min = _materialy.tesneni(material_tesneni)[2]
        beta_2 = _materialy.tesneni(material_tesneni)[3]

        # materiál šroubu
        Sb = _materialy.tyce(Temp, material_sroubu, tloustka_sroubu)[0]
        Sa = _materialy.tyce(20, material_sroubu, tloustka_sroubu)[0]
        material_info_sroubu = _materialy.tyce(Temp, material_sroubu, tloustka_sroubu)[1]
        tloustka_materialu_sroubu  = _materialy.tyce(Temp, material_sroubu, tloustka_sroubu)[2]
        podminky_plneni.append(tloustka_materialu_sroubu)

        # materiál pláště
        Sn = _materialy.plast(Temp, material_plaste, tloustka_plaste)[0]
        material_info_plaste = _materialy.plast(Temp, material_plaste, tloustka_plaste)[1]
        tloustka_materialu_plaste  = _materialy.plast(Temp, material_plaste, tloustka_plaste)[2]
        podminky_plneni.append(tloustka_materialu_plaste)
        
        # materiál příruby
        Sf = _materialy.priruba(Temp, material_priruby, tloustka_priruby)[0]
        material_info_priruby = _materialy.priruba(Temp, material_priruby, tloustka_priruby)[1]
        tloustka_materialu_priruby  = _materialy.priruba(Temp, material_priruby, tloustka_priruby)[2]
        podminky_plneni.append(tloustka_materialu_priruby)
        
        zaznam_asme = []
        zaznam_asme.append(m)
        zaznam_asme.append(y)
        zaznam_asme.append(w_min)
        zaznam_asme.append(beta_2)
        zaznam_asme.append(Sb)
        zaznam_asme.append(Sa)
        zaznam_asme.append(material_info_sroubu)
        zaznam_asme.append(Sn)
        zaznam_asme.append(material_info_plaste)
        zaznam_asme.append(Sf)
        zaznam_asme.append(material_info_priruby)

        Cb = 0.5 # faktor pro konverzi rovnic na US jednotky
        
        # Gasket
        if b0<=6:
            b=b0
        elif b0>6:
            b=Cb*math.sqrt(b0)
        else:
            pass
        
        K=A/B # ratio of outside diameter of flange to inside diameter of flange
        R=((C-B)/2)-g1 # radial distance from bolt circle to point of intersection of hub and back of flange. For integral and hub flanges,
        T=((K**2)*(1+8.55246*math.log10(K))-1)/((1.04720+1.9448*K**2)*(K-1)) # factor involving K (from Fig. 2-7.1)                         ???????????
        U=((K**2)*(1+8.55246*math.log10(K))-1)/(1.36136*((K**2)-1)*(K-1)) # factor involving K (from Fig. 2-7.1)                              ???????????
        h0 = math.sqrt(B*g0) # factor
        Xh = h/h0
        Xg = g1/g0
        if Xh >= 0.1 and Xh <= 0.5:
            try:
                V = 0.500244+0.227914/Xg-1.87071*Xh-0.34441/(Xg**2.0)+2.49189*(Xh**2.0)+0.873446*(Xh/Xg)+0.189953/(Xg**3.0)-1.06082*(Xh**3.0)-1.4997*(Xh**2.0/Xg)+0.719413(Xh/Xg**2.0)
            except TypeError:
                V = 0.35
        else:
            try:
                V = 0.0144868-0.135977/Xg-0.0461919/Xh+0.560718/(Xg**2.0)+0.0529829/(Xh**2.0)+0.244313/(Xg*Xh)+0.113929/(Xg**3.0)-0.0928265/(Xh**3.0)-0.0266293/(Xg*Xh**2.0)-0.217008/(Xh*Xg**2.0)
            except TypeError:
                V = 0.35
        Y=(0.66845+(5.71690*(K**2)*math.log10(K))/((K**2)-1))/(K-1) # factor involving K (from Fig. 2-7.1)                                   ???????????
        Z=((K**2)+1)/((K**2)-1) # factor involving K (from Fig. 2-7.1)                                                                        ???????????
        try:
            F = 0.897697-0.297012*math.log(Xg)+(9.5257E-3)*math.log(Xh)+0.123586*((math.log(Xg))**2.0)+0.0358580*((math.log(Xh))**2.0)-0.194422*math.log(Xg)*math.log(Xh)-0.0181259*((math.log(Xg))**3.0)+0.012936*((math.log(Xh))**3.0)-0.0377693*math.log(Xg)*((math.log(Xh))**2.0)+0.0273791*((math.log(Xg))**2.0)*math.log(Xh)
        except:
            F = None
        BS = C/nb # bolt spacing
        f1 = (0.0927779-0.0336633*Xg+0.964176*(Xg**2.0)+0.0566286*Xh+0.347074*(Xh**2.0)-4.18699*(Xh**3.0))/(1-5.96093E-3*Xg+1.62904*Xh+3.49329*(Xg**2.0)+1.39052*(Xh**3.0))
        if f1 >= 1:
            f = f1
        else:
            f = 1

        hD=R+0.5*g1
        hG=(C-G)/2 # radial distance from gasket load reaction to the bolt circle
        hT=(R+g1+hG)/2
        H=0.785*P*G**2 # total hydrostatic end force
        HD=0.785*P*B**2 # hydrostatic end force on area inside of flange
        Hp=2*b*3.14*G*m*P # total joint-contact surface compression load                                                                    ??????????
        HT=H-HD # difference between total hydrostatic end force and the hydrostatic end force on area inside of flange

        zaznam_asme.append(K)
        zaznam_asme.append(R)
        zaznam_asme.append(T)
        zaznam_asme.append(U)
        zaznam_asme.append(h0)
        zaznam_asme.append(Xh)
        zaznam_asme.append(Xg)
        zaznam_asme.append(V)
        zaznam_asme.append(Y)
        zaznam_asme.append(Z)
        zaznam_asme.append(F)
        zaznam_asme.append(BS)
        zaznam_asme.append(f)
        zaznam_asme.append(hD)
        zaznam_asme.append(hG)
        zaznam_asme.append(hT)
        zaznam_asme.append(H)
        zaznam_asme.append(HD)
        zaznam_asme.append(Hp)
        zaznam_asme.append(HT)

        # Bolt loads
        Wm1=H+Hp
        Wm2=3.14*b*G*y
        Am1=Wm1/Sb # total cross-sectional area of bolts at root of thread or section of least diameter under stress, required for the operating conditions
        Am2=Wm2/Sa # total cross-sectional area of bolts at root of thread or section of least diameter under stress, required for gasket seating
        Am=max(Am1,Am2)
        Ab = (nb*math.pi*a**(2.0))/4 # total actual cross sectional area of bolts

        Bsmax=2+6*t/(m+0.5) #                                                                           ???????????
        W_provoz=Wm1
        W_montaz=(Am+Ab)*Sa/2 #                                                                         ???????????

        HG=W_montaz-H # gasket load

        d=U*h0*(g0**2)/V # factor
        try:
            e=F/h0 # factor
        except:
            e = None

        try:
            L=((t*e+1)/T)+(t**3)/d # factor
        except:
            L = None
        MD=HD*hD # component of moment due to HD
        MG=HG*hG # component of moment due to HG
        MT=HT*hT # component of moment due to HT

        zaznam_asme.append(Wm1)
        zaznam_asme.append(Wm2)
        zaznam_asme.append(Am1)
        zaznam_asme.append(Am2)
        zaznam_asme.append(Am)
        zaznam_asme.append(Ab)
        zaznam_asme.append(Bsmax)
        zaznam_asme.append(W_provoz)
        zaznam_asme.append(W_montaz)
        zaznam_asme.append(HG)
        zaznam_asme.append(d)
        zaznam_asme.append(e)
        zaznam_asme.append(L)
        zaznam_asme.append(MD)
        zaznam_asme.append(MG)
        zaznam_asme.append(MT)

        # Podmínky
        if h/h0<1 and g1/g0<1:
            B1=B+g1
        else:
            B1=B+g0
            
        if B<20*g1:
            B=B1
        else:
            B=B

        zaznam_asme.append(B1)
        zaznam_asme.append(B)

        # Flange moments
        Mo_provoz=MD+MG+MT
        Mo_montaz=W_montaz*(C-G)/2
        Mo=max(Mo_provoz,Mo_montaz)

        BSC=math.sqrt(BS/(2*a+t))

        # Calculation of flange stresses
        try:
            SH=f*Mo/(L*(g1**2)*B) # Longitudinal hub stress
            SR=(1.33*t*e+1)*Mo/(L*B*t**2) # Radial flange stress
            ST=(Y*Mo/(B*t**2))-Z*SR # Tangential flange stress
        except:
            SH = None
            SR = None
            ST = None

        zaznam_asme.append(Mo_provoz)
        zaznam_asme.append(Mo_montaz)
        zaznam_asme.append(Mo)
        zaznam_asme.append(b)
        zaznam_asme.append(BSC)
        zaznam_asme.append(SH)
        zaznam_asme.append(SR)
        zaznam_asme.append(ST)

##        print SH, SR, ST, 'SH-SR-ST'

        S_min=min(1.5*Sf,2.5*Sn)

        zaznam_asme.append(S_min)

        if SH == None or SR == None or ST == None:
            podminky_plneni.append(False)
            podminky_plneni.append('longitudinal_hub_stress')
            podminky_plneni.append(False)
            podminky_plneni.append('welded')
            podminky_plneni.append(False)
            podminky_plneni.append('radial_flange')
            podminky_plneni.append(False)
            podminky_plneni.append('tangential_flange')
            podminky_plneni.append(False)
            podminky_plneni.append('another_stress')
        else:
            if SH<=Sf:
                podminky_plneni.append(True)
                podminky_plneni.append('longitudinal_hub_stress')
            else:
                podminky_plneni.append(False)
                podminky_plneni.append('longitudinal_hub_stress')
                
            if SH<=S_min:
                podminky_plneni.append(True)
                podminky_plneni.append('welded')
            else:
                podminky_plneni.append(False)
                podminky_plneni.append('welded')

            if SR<=Sf:
                podminky_plneni.append(True)
                podminky_plneni.append('radial_flange')
            else:
                podminky_plneni.append(False)
                podminky_plneni.append('radial_flange')
            
            if ST<=Sf:
                podminky_plneni.append(True)
                podminky_plneni.append('tangential_flange')
            else:
                podminky_plneni.append(False)
                podminky_plneni.append('tangential_flange')

            if (SH+SR)/2<=Sf and (SH+ST)/2<=Sf:
                podminky_plneni.append(True)
                podminky_plneni.append('another_stress')
            else:
                podminky_plneni.append(False)
                podminky_plneni.append('another_stress')
            
        return zaznam_asme, podminky_plneni

        def vypis(self):
            if vypis == True:
                # asme výpočet
                print "K= ",K
                print "R= ",R
                print "T= ",T
                print "U= ",U
                print "V= ",V
                print "Y= ",Y
                print "Z= ",Z
                print "Am= ",Am
                print "B= ",B
                print "B1= ",B1
            else:
                pass # příkaz k přeskočení podmínky - neprovede nic
