# -*- coding: cp1250 -*-
## databaze_materialu

import math
import numpy as np

class materialy:
    def tyce(self, Temp, material, tloustka):
        tloustka_materialu = []
        #C35E+QT tyč(roub),1.1181,ČSN EN 10269 - kveten 2014:
        if material == "C35E+QT":
            teploty = [20,  50, 100, 150, 200, 250, 300, 350, 400]
            if tloustka < 61.:
                Rp02 = [300, 289, 270, 251, 229, 213, 192, 182, 173] # Rp02 pro material do 60 mm
                Rm = 500.
                A = 22.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            elif tloustka > 61 and tloustka < 151.:
                Rp02 = [300, 287, 264, 242, 220, 203, 186, 167, 147] # Rp02 pro material do 150 mm
                Rm = 500.
                A = 22.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu tyce")
            material_info = ["C35E+QT tyc(sroub)","1.1181","CSN EN 10269 - kveten 2014"]

        #C35E+N tyč(roub),1.1181,ČSN EN 10269 - kveten 2014:
        elif material == "C35E+N":
            teploty = [20,  50, 100, 150, 200, 250, 300, 350, 400]
            if tloustka < 61.:
                Rp02 = [300, 289, 270, 251, 229, 213, 192, 182, 173] # Rp02 pro material do 60 mm
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu tyce")
            Rm = 500.
            A = 20.
            material_info = ["C35E+N tyc(sroub)","1.1181","CSN EN 10269 - kveten 2014"]

        #25CrMo4+QT tyč(roub), 1.7218, ČSN EN 10269 - kveten 2014:
        elif material == "25CrMo4+QT":
            teploty = [20,  50, 100, 150, 200, 250, 300, 350, 400, 450, 500]
            if tloustka < 61.:
                Rp02 = [440, 435, 428, 420, 412, 392, 363, 333, 304, 275, 235] # Rp02 pro material do 60 mm
                Rm = 600.
                A = 18.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            elif tloustka > 61 and tloustka < 151.:
                Rp02 = [420, 414, 403, 393, 382, 372, 344, 324, 294, 265, 226] # Rp02 pro material do 150 mm
                Rm = 600.
                A = 18.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu tyce")
            material_info = ["25CrMo4+QT tyc(sroub)","1.7218","CSN EN 10269 - kveten 2014"]

        #42CrMo4+QT tyč(roub), 1.7225, ČSN EN 10269 - kveten 2014:
        elif material == "42CrMo4+QT":
            teploty  = [20,  50, 100, 150, 200, 250, 300, 350, 400, 450, 500]
            if tloustka < 61.:
                Rp02 = [730, 720, 702, 677, 640, 602, 562, 518, 475, 420, 375] # Rp02 pro material do 60 mm
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu tyce")
            Rm = 860.
            A = 14.
            material_info = ["42CrMo4+QT tyc(sroub)","1.7225","CSN EN 10269 - kveten 2014"]
                
        #42CrMo5-6+QT tyč(roub), 1.7233, ČSN EN 10269 - kveten 2014:
        elif material == "42CrMo5-6+QT":
            teploty  = [20,  50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600]
            if tloustka < 101.:
                Rp02 = [700, 681, 662, 639, 616, 601, 585, 570, 547, 516, 462, 362, 223] # Rp02 pro material do 100 mm
                Rm = 860.
                A = 16.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            elif tloustka > 61. and tloustka < 151.:
                Rp02 = [640, 625, 605, 584, 563, 549, 535, 521, 500, 472, 422, 331, 204] # Rp02 pro material do 150 mm
                Rm = 850.
                A = 16.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu tyce")
            material_info = ["42CrMo5-6+QT tyc(sroub)","1.7233","CSN EN 10269 - kveten 2014"]

        #40CrMoV4-6+QT tyč(roub), 1.7711, ČSN EN 10269 - kveten 2014:
        elif material == "40CrMoV4-6+QT":
            teploty  = [20,  50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600]
            if tloustka < 101.:
                Rp02 = [700, 687, 670, 647, 631, 608, 593, 577, 554, 523, 470, 400, 293] # Rp02 pro material do 100 mm
                Rm = 850.
                A = 14.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            elif tloustka < 161.:
                Rp02 = [640, 631, 612, 591, 577, 556, 542, 528, 507, 479, 429, 366, 268] # Rp02 pro material do 160 mm
                Rm = 850.
                A = 14.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu tyce")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu tyce")
            material_info = ["42CrMo5-6+QT tyc(sroub)","1.7233","CSN EN 10269 - kveten 2014"]
                
        else:
            pass
            
        Rp02t_sroubu = np.interp(Temp, teploty, Rp02) # lineární interpolace
        Rm_sroubu = Rm
        A_sroubu = A
        material_info_sroubu = material_info
        
        return Rp02t_sroubu, Rm_sroubu, A_sroubu, material_info_sroubu, tloustka_materialu           
    
    def priruba(self, Temp, material, tloustka):
        tloustka_materialu = []
        #P245GH, 1.0352, ČSN EN 10222-2 - duben 2002:
        if material == "P245GH":
            teploty  = [20, 100, 150, 200, 250, 300, 350, 400]
            if tloustka < 35.:
                Rp02 = [245, 195, 185, 175, 160, 145, 135, 125] # Rp02 pro material do 35 mm
                Rm = 410.
                A = 23.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 161.:
                Rp02 = [220, 180, 175, 165, 155, 135, 130, 120] # Rp02 pro material do 160 mm
                Rm = 410.
                A = 23.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu priruba")
            material_info = ["P245GH","1.0352","CSN EN 10222-2 - duben 2002"]

        #P280GH, 1.0426, ČSN EN 10222-2 - duben 2002:
        elif material == "P280GH":
            teploty  = [20, 100, 150, 200, 250, 300, 350, 400]
            if tloustka < 35.:
                Rp02 = [280,250, 235, 225, 205, 185, 170, 155] # Rp02 pro material do 35 mm
                Rm = 460.
                A = 21.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 161.:
                Rp02 = [255, 210, 200, 195, 185, 170, 155, 135] # Rp02 pro material do 160 mm
                Rm = 460.
                A = 21.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu priruba")
            material_info = ["P280GH","1.0426","CSN EN 10222-2 - duben 2002"]

        #16Mo3, 1.5415, ČSN EN 10222-2 - duben 2002:
        elif material == "16Mo3":
            teploty  = [20, 100, 150, 200, 250, 300, 350, 400, 450, 500]
            if tloustka < 35.:
                Rp02 = [295, 264, 245, 225, 205, 180, 170, 160, 155, 150] # Rp02 pro material do 35 mm
                Rm = 440.
                A = 21.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 60.:
                Rp02 = [285, 264, 245, 225, 205, 180, 170, 160, 155, 150] # Rp02 pro material do 60 mm
                Rm = 440.
                A = 21.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 90.:
                Rp02 = [275, 250, 230, 210, 195, 170, 160, 150, 145, 140] # Rp02 pro material do 90 mm
                Rm = 440.
                A = 21.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 375.:
                Rp02 = [250, 235, 210, 190, 175, 150, 145, 140, 135, 130] # Rp02 pro material do 375 mm
                Rm = 440.
                A = 21.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 500.:
                Rp02 = [250, 220, 200, 180, 165, 145, 140, 135, 130, 125] # Rp02 pro material do 500 mm
                Rm = 420.
                A = 21.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu priruba")
            material_info = ["16Mo3","1.5415","CSN EN 10222-2 - duben 2002"]

        #13CrMo4-5, 1.7335, ČSN EN 10222-2 - duben 2002:
        elif material == "13CrMo4-5":
            teploty  = [20, 100, 150, 200, 250, 300, 350, 400, 450, 500]
            if tloustka < 35.:
                Rp02 = [295, 260, 245, 240, 230, 215, 200, 190, 180, 175] # Rp02 pro material do 35 mm
                Rm = 440.
                A = 18.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 60.:
                Rp02 = [285, 260, 245, 240, 230, 215, 200, 190, 180, 175] # Rp02 pro material do 60 mm
                Rm = 440.
                A = 18.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 90.:
                Rp02 = [275, 250, 240, 230, 220, 205, 190, 180, 170, 165] # Rp02 pro material do 90 mm
                Rm = 440.
                A = 18.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 375.:
                Rp02 = [240, 240, 225, 210, 200, 185, 175, 165, 155, 150] # Rp02 pro material do 375 mm
                Rm = 440.
                A = 18.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            elif tloustka < 500.:
                Rp02 = [240, 220, 210, 200, 190, 175, 165, 160, 150, 145] # Rp02 pro material do 500 mm
                Rm = 420.
                A = 18.
                tloustka_materialu.append(True)
                tloustka_materialu.append("tloustka materialu priruba")
            else:
                tloustka_materialu.append(False)
                tloustka_materialu.append("tloustka materialu priruba")
            material_info = ["13CrMo4-5","1.7335","CSN EN 10222-2 - duben 2002"]
        else:
            pass

        Rp02t = np.interp(Temp, teploty, Rp02) # lineární interpolace
        return Rp02t, Rm, A, material_info, tloustka_materialu

    def tesneni(self, tekutina, bD, hD, gasket_material, Temp):
        teploty = [100, 200, 300, 400, 500]
##        print gasket_material
##        if gasket_material == 'soft_al':
##            KD = 100
##            KDtheta_ = [40, 20, 5, 0, 0]
##            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
##        elif gasket_material == 'cooper':
##            KD = 200
##            KDtheta_ = [180, 130, 100, 40, 0]
##            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
##        elif gasket_material == 'soft_iron':
##            KD = 350
##            KDtheta_ = [310, 260, 210, 170, 80]
##            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
##        elif gasket_material == 'St35':
##            KD = 400
##            KDtheta_ = [380, 330, 260, 190, 120]
##            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
##        elif gasket_material == 'St13CrMo44':
##            KD = 450
##            KDtheta_ = [450, 420, 390, 330, 280]
##            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
##        elif gasket_material == 'austenitic':
##            KD = 500
##            KDtheta_ = [480, 450, 420, 390, 350]
##            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
##        else:
##            KDtheta = 0
        
        if gasket_material == 'rubber' and tekutina == 'liquid':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = bD
            k1 = 0.5*bD
        elif gasket_material == 'rubber' and tekutina == 'gas':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 2.*bD
            k1 = 0.5*bD
        elif gasket_material == 'PTFE' and tekutina == 'liquid':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 20.*bD
            k1 = 1.1*bD            
        elif gasket_material == 'PTFE' and tekutina == 'gas':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 25.*bD
            k1 = 1.1*bD            
        elif gasket_material == 'it' and tekutina == 'liquid':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 15.*bD
            k1 = 1.0*bD            
        elif gasket_material == 'it' and tekutina == 'gas':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 200.*math.sqrt(bD/hD)
            k1 = 1.3*bD            
        elif gasket_material == 'PTEF' and tekutina == 'liquid':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 12.*bD
            k1 = 0.8*bD            
        elif gasket_material == 'PTEF' and tekutina == 'gas':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 12.*bD
            k1 = 0.8*bD            
        elif gasket_material == 'wound_metal' and tekutina == 'liquid':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 15.*bD
            k1 = 1.0*bD            
        elif gasket_material == 'wound_metal' and tekutina == 'gas':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 50.*bD
            k1 = 1.3*bD            
        elif gasket_material == 'corrugated_metal_al' and tekutina == 'liquid':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 8.*bD
            k1 = 0.6*bD            
        elif gasket_material == 'corrugated_metal_al' and tekutina == 'gas':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 30.*bD
            k1 = 0.6*bD            
        elif gasket_material == 'corrugated_metal_cu' and tekutina == 'liquid':
            KD = 200
            KDtheta_ = [180, 130, 100, 40, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 9.*bD
            k1 = 0.6*bD            
        elif gasket_material == 'corrugated_metal_cu' and tekutina == 'gas':
            KD = 200
            KDtheta_ = [180, 130, 100, 40, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 35.*bD
            k1 = 0.7*bD            
        elif gasket_material == 'corrugated_metal_steel' and tekutina == 'liquid':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 10.*bD
            k1 = 0.6*bD            
        elif gasket_material == 'corrugated_metal_steel' and tekutina == 'gas':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 45.*bD
            k1 = 1.0*bD            
        elif gasket_material == 'flat_metal_al' and tekutina == 'liquid':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 10.*bD
            k1 = 1.0*bD            
        elif gasket_material == 'flat_metal_al' and tekutina == 'gas':
            KD = 100
            KDtheta_ = [40, 20, 5, 0, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 50.*bD
            k1 = 1.4*bD            
        elif gasket_material == 'flat_metal_cu' and tekutina == 'liquid':
            KD = 200
            KDtheta_ = [180, 130, 100, 40, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 20.*bD
            k1 = 1.0*bD            
        elif gasket_material == 'flat_metal_cu' and tekutina == 'gas':
            KD = 200
            KDtheta_ = [180, 130, 100, 40, 0]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 60.*bD
            k1 = 1.6*bD            
        elif gasket_material == 'flat_metal_steel' and tekutina == 'liquid':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 40.*bD
            k1 = 1.0*bD            
        elif gasket_material == 'flat_metal_steel' and tekutina == 'gas':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 70.*bD
            k1 = 1.8*bD
        elif gasket_material == 'solid_flat' and tekutina == 'liquid':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 0.8*bD*KD
            k1 = bD+5           
        elif gasket_material == 'solid_flat' and tekutina == 'gas':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = bD*KD
            k1 = bD+5             
        elif gasket_material == 'diamond_ring' and tekutina == 'liquid':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 0.8*KD
            k1 = 5.         
        elif gasket_material == 'diamond_ring' and tekutina == 'gas':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 1.*KD
            k1 = 5.           
        elif gasket_material == 'oval_ring' and tekutina == 'liquid':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 1.6*KD
            k1 = 6.         
        elif gasket_material == 'oval_ring' and tekutina == 'gas':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 2.*KD
            k1 = 6.           
        elif gasket_material == 'o_ring' and tekutina == 'liquid':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 1.2*KD
            k1 = 6.         
        elif gasket_material == 'o_ring' and tekutina == 'gas':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 1.5*KD
            k1 = 6.           
        elif gasket_material == 'ring_gasket' and tekutina == 'liquid':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 1.6*KD
            k1 = 6.         
        elif gasket_material == 'ring_gasket' and tekutina == 'gas':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 2.0*KD
            k1 = 6.           
        elif gasket_material == 'convex_gasket' and tekutina == 'liquid':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 1.6*KD
            k1 = 6.         
        elif gasket_material == 'convex_gasket' and tekutina == 'gas':
            KD = 350
            KDtheta_ = [310, 260, 210, 170, 80]
            KDtheta = np.interp(Temp, teploty, KDtheta_) # lineární interpolace
            gasket = 2.0*KD
            k1 = 6.           
        else:
            pass

        return gasket, k1, KDtheta
