From 93cc49e414766f2980e06cbe2b9b8be10ec4472d Mon Sep 17 00:00:00 2001 From: blendoit Date: Tue, 1 Oct 2019 23:29:24 -0700 Subject: Proper package structure --- __init__.py | 0 creator/__init__.py | 1 + creator/base.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ creator/wing.py | 59 +++++--------------------------------------------- evaluator/__init__.py | 0 evaluator/evaluator.py | 41 +++++++++++++++++++++++++---------- example_airfoil.py | 8 ++++--- 7 files changed, 99 insertions(+), 69 deletions(-) create mode 100644 __init__.py create mode 100644 creator/__init__.py create mode 100644 creator/base.py create mode 100644 evaluator/__init__.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/creator/__init__.py b/creator/__init__.py new file mode 100644 index 0000000..6fc3a2c --- /dev/null +++ b/creator/__init__.py @@ -0,0 +1 @@ +__all__ = ['base', 'fuselage', 'propulsion', 'wing'] diff --git a/creator/base.py b/creator/base.py new file mode 100644 index 0000000..91d4c70 --- /dev/null +++ b/creator/base.py @@ -0,0 +1,59 @@ +"""The base.py module contains parent classes for components.""" +import numpy as np +import sys +import os.path +import logging + +logging.basicConfig(filename='log.txt', + level=logging.DEBUG, + format='%(asctime)s - %(levelname)s - %(message)s') + + +class Component: + """Basic component providing coordinates and tools.""" + + # TODO: define defaults in separate module + def __init__(self): + self.x = np.array([]) + self.z = np.array([]) + self.material = str() + self.mass = float() + + def set_material(self, material): + """Set the component bulk material.""" + self.material = material + + def info_print(self, round): + """Print all the component's coordinates to the terminal.""" + name = f' CREATOR DATA FOR {str(self).upper()} ' + num_of_dashes = len(name) + print(num_of_dashes * '-') + print(name) + for k, v in self.__dict__.items(): + if type(v) != list: + print('{}:\n'.format(k), v) + print(num_of_dashes * '-') + for k, v in self.__dict__.items(): + if type(v) == list: + print('{}:\n'.format(k), np.around(v, round)) + return None + + def info_save(self, save_path, number): + """Save all the object's coordinates (must be full path).""" + file_name = f'{str(self).lower()}_{number}.txt' + full_path = os.path.join(save_path, file_name) + try: + with open(full_path, 'w') as sys.stdout: + self.info_print(6) + # This line required to reset behavior of sys.stdout + sys.stdout = sys.__stdout__ + logging.debug(f'Successfully wrote to file {full_path}') + except IOError: + print(f'Unable to write {file_name} to specified directory.\n', + 'Was the full path passed to the function?') + return None + + +class Aircraft: + """This class tracks all sub-components and is fed to the evaluator.""" + pass diff --git a/creator/wing.py b/creator/wing.py index 4988cb5..be8e18a 100644 --- a/creator/wing.py +++ b/creator/wing.py @@ -11,65 +11,16 @@ Functions: plot_geom(airfoil): generates a 2D plot of the airfoil & any components. """ -import sys -import os.path +import creator.base as base + import logging import numpy as np from math import sin, cos, atan import bisect as bi import matplotlib.pyplot as plt -logging.basicConfig(filename='log.txt', - level=logging.DEBUG, - format='%(asctime)s - %(levelname)s - %(message)s') - - -class Component: - """Basic component providing coordinates and tools.""" - - # TODO: define defaults in separate module - def __init__(self): - self.x = np.array([]) - self.z = np.array([]) - self.material = str() - self.mass = float() - - def set_material(self, material): - """Set the component bulk material.""" - self.material = material - - def info_print(self, round): - """Print all the component's coordinates to the terminal.""" - name = f' CREATOR DATA FOR {str(self).upper()} ' - num_of_dashes = len(name) - print(num_of_dashes * '-') - print(name) - for k, v in self.__dict__.items(): - if type(v) != list: - print('{}:\n'.format(k), v) - print(num_of_dashes * '-') - for k, v in self.__dict__.items(): - if type(v) == list: - print('{}:\n'.format(k), np.around(v, round)) - return None - - def info_save(self, save_path, number): - """Save all the object's coordinates (must be full path).""" - file_name = f'{str(self).lower()}_{number}.txt' - full_path = os.path.join(save_path, file_name) - try: - with open(full_path, 'w') as sys.stdout: - self.info_print(6) - # This line required to reset behavior of sys.stdout - sys.stdout = sys.__stdout__ - logging.debug(f'Successfully wrote to file {full_path}') - except IOError: - print(f'Unable to write {file_name} to specified directory.\n', - 'Was the full path passed to the function?') - return None - -class Airfoil(Component): +class Airfoil(base.Component): """This class represents a single NACA airfoil. The coordinates are saved as two lists @@ -185,7 +136,7 @@ class Airfoil(Component): return None -class Spar(Component): +class Spar(base.Component): """Contains a single spar's data.""" def __init__(self, airfoil, loc_percent, material): """Set spar location as percent of chord length.""" @@ -214,7 +165,7 @@ class Spar(Component): return None -class Stringer(Component): +class Stringer(base.Component): """Contains the coordinates of all stringers.""" def __init__(self): super().__init__() diff --git a/evaluator/__init__.py b/evaluator/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/evaluator/evaluator.py b/evaluator/evaluator.py index 6c7f901..afbde9c 100644 --- a/evaluator/evaluator.py +++ b/evaluator/evaluator.py @@ -14,18 +14,11 @@ import matplotlib.pyplot as plt class Evaluator: """Performs structural evaluations for the airfoil passed as argument.""" - def __init__(self, airfoil): + def __init__(self, aircraft): # Evaluator knows all geometrical info from evaluated airfoil - self.airfoil = airfoil - self.spar = airfoil.spar - self.stringer = airfoil.stringer - # Global dimensions - self.chord = airfoil.chord - self.semi_span = airfoil.semi_span - # Mass & spanwise distribution - self.mass_total = float(airfoil.mass + airfoil.spar.mass + - airfoil.stringer.mass) - self.mass_dist = [] + self.airfoil = self.get_airfoil(aircraft) + self.spars = self.get_spars(aircraft) + self.stringers = self.get_stringers(aircraft) # Lift self.lift_rectangular = [] self.lift_elliptical = [] @@ -40,9 +33,33 @@ class Evaluator: def __str__(self): return type(self).__name__ + def get_airfoil(self, aircraft): + """Get data of spars belonging to aircraft.""" + try: + pass + except: + pass + pass + + def get_spars(self, aircraft): + """Get data of spars belonging to aircraft.""" + try: + pass + except: + pass + pass + + def get_stringers(self, aircraft): + """Get data of spars belonging to aircraft.""" + try: + pass + except: + pass + pass + def info_print(self, round): """Print all the component's evaluated data to the terminal.""" - name = ' EVALUATOR DATA FOR {} '.format(str(self).upper()) + name = f' {print(self)} DATA FOR {str(self).upper()} ' num_of_dashes = len(name) print(num_of_dashes * '-') print(name) diff --git a/example_airfoil.py b/example_airfoil.py index 08282a3..a5e1cce 100644 --- a/example_airfoil.py +++ b/example_airfoil.py @@ -10,8 +10,8 @@ Generate a population of airfoils & optimize. """ from resources import materials as mt -from creator import wing, fuselage, propulsion -# from evaluator import +from creator import * +from evaluator import evaluator # from generator import import time @@ -41,6 +41,8 @@ NOSE_BOTTOM_STRINGERS = 5 SAVE_PATH = '/home/blendux/Projects/Aircraft_Studio/save' +# Create aircraft instance +aircraft = base.Aircraft # Create airfoil instance af = wing.Airfoil(20, 150, mt.aluminium) af.add_naca(NACA_NUM) @@ -69,7 +71,7 @@ af.spar2 = wing.Spar(af, 0.57, mt.aluminium) # wing.plot_geom(af, [af.spar1, af.spar2], None) # Evaluator object contains airfoil analysis results. -# eval = evaluator.Evaluator(af) +eval = evaluator.Evaluator(aircraft) # The analysis is performed in the evaluator.py module. # eval.analysis(1, 1) # eval.info_print(2) -- cgit v1.2.3