View raw

1 """The base.py module contains parent classes for components.""" 2 3 import numpy as np 4 import os.path 5 import random 6 import logging 7 8 from aircraftstudio import creator 9 10 logging.basicConfig(filename='log_base.txt', 11 level=logging.DEBUG, 12 format='%(asctime)s - %(levelname)s - %(message)s') 13 14 15 class Aircraft: 16 """This class tracks all sub-components and is fed to the evaluator.""" 17 name = None 18 fuselage = None 19 propulsion = None 20 wing = None 21 properties = {} 22 23 naca = [2412, 3412, 2420] 24 25 def __init__(self): 26 self.results = {} 27 28 def __str__(self): 29 return self.name 30 31 @classmethod 32 def from_default(cls): 33 aircraft = Aircraft() 34 aircraft.name = 'default_aircraft_' + str(random.randrange(1000, 9999)) 35 airfoil = creator.wing.Airfoil(aircraft, 'default_airfoil') 36 airfoil.add_naca(2412) 37 soar1 = creator.wing.Spar(airfoil, 'default_spar_1', 0.30) 38 soar2 = creator.wing.Spar(airfoil, 'default_spar_2', 0.60) 39 stringer = creator.wing.Stringer(airfoil, 'default_stringer') 40 return aircraft 41 42 @classmethod 43 def from_random(cls): 44 aircraft = Aircraft() 45 aircraft.name = 'random_aircraft_' + str(random.randrange(1000, 9999)) 46 airfoil = creator.wing.Airfoil(aircraft, 'random_airfoil') 47 airfoil.add_naca(random.choice(cls.naca)) 48 soar1 = creator.wing.Spar(airfoil, 'random_spar_1', 49 random.randrange(20, 80) / 100) 50 soar2 = creator.wing.Spar(airfoil, 'random_spar_2', 51 random.randrange(20, 80) / 100) 52 stringer = creator.wing.Stringer(airfoil, 'random_stringer', 53 random.randint(1, 10), 54 random.randint(1, 10), 55 random.randint(1, 10), 56 random.randint(1, 10)) 57 return aircraft 58 59 60 class Component: 61 """Basic component providing coordinates, tools and a component tree.""" 62 def __init__(self, parent, name): 63 self.parent = parent 64 self.name = name 65 self.x = np.array([]) 66 self.z = np.array([]) 67 self.y = np.array([]) 68 self.material = None 69 self.mass = float() 70 self.properties = {} 71 72 def __str__(self): 73 return self.name 74 75 def info_print(self, round): 76 """Print all the component's coordinates to the terminal.""" 77 name = f' CREATOR DATA FOR {str(self).upper()} ' 78 num_of_dashes = len(name) 79 print(num_of_dashes * '-') 80 print(name) 81 for k, v in self.__dict__.items(): 82 if type(v) is not np.ndarray: 83 print(f'{k}:\n', v) 84 print(num_of_dashes * '-') 85 for k, v in self.__dict__.items(): 86 if type(v) is np.ndarray: 87 print(f'{k}:\n', np.around(v, round)) 88 return None 89 90 def info_save(self, 91 save_path='/home/blendux/Projects/Aircraft_Studio/save'): 92 """Save all the object's coordinates (must be full path).""" 93 file_name = f'{self.name}_info.txt' 94 full_path = os.path.join(save_path, file_name) 95 try: 96 with open(full_path, 'w') as f: 97 for k, v in self.__dict__.items(): 98 if type(v) is not np.ndarray: 99 f.write(f'{k}=\n') 100 f.write(str(v)) 101 f.write("\n") 102 # print(num_of_dashes * '-') 103 for k, v in self.__dict__.items(): 104 if type(v) is np.ndarray: 105 f.write(f'{k}=\n') 106 f.write(str(v)) 107 f.write("\n") 108 logging.debug(f'Successfully wrote to file {full_path}') 109 except IOError: 110 print(f'Unable to write {file_name} to specified directory.\n', 111 'Was the full path passed to the function?') 112 return None 113