from tarfile import SUPPORTED_TYPES
import numpy as np
import glog
import bisect
class KdeGearbox:
def __init__(self, maxTooth=200):
self.minTooth = 6
self.maxTooth = maxTooth
def get_validToothPair(self, ratio):
arToothPair = []
for toothNumber1 in np.arange(self.minTooth, self.maxTooth+1):
for toothNumber2 in np.arange(toothNumber1, self.maxTooth+1):
if toothNumber2/toothNumber1<ratio:
arToothPair.append([toothNumber1, toothNumber2, toothNumber2*1.0/toothNumber1])
return sorted(arToothPair, key=lambda x: x[-1], reverse=False)
def get_gearbox_tooth_numer(self, target_ratio, tolerance=0.001):
arResult = []
glog.info(f"start to get all ratios of per stage..")
arPairs = self.get_validToothPair(target_ratio)
glog.info(f"total 1 step space cnts:{len(arPairs)}")
valid_pairs = arPairs
sorted_pairs = valid_pairs
sorted_ratios = [p[2] for p in sorted_pairs]
results = []
for p1 in valid_pairs:
tooth1, tooth2, ratio1 = p1
target_p2_ratio = target_ratio / ratio1
idx = bisect.bisect_left(sorted_ratios, target_p2_ratio)
for i in [idx-2, idx-1, idx, idx+1, idx+2]:
if 0 <= i < len(sorted_pairs):
p2 = sorted_pairs[i]
tooth3, tooth4, ratio2 = p2
total_ratio = ratio1 * ratio2
error = abs(total_ratio - target_ratio)
if error <= tolerance:
results.append({
'tooth1': tooth1, 'tooth2': tooth2,
'tooth3': tooth3, 'tooth4': tooth4,
'total_ratio': round(total_ratio, 4),
'error': round(error, 6)
})
results.sort(key=lambda x: (x['error'], abs(x['total_ratio'] - target_ratio)))
return [[r['tooth1'], r['tooth2'], r['tooth3'], r['tooth4'], r['total_ratio'], r['error']] for r in results]
kgb = KdeGearbox()
r = kgb.get_gearbox_tooth_numer(20.267)
for i in r:
print(i)