summaryrefslogtreecommitdiff
path: root/aircraftstudio/creator/base.py
blob: 92fe421ddbebba0893a963b3447d6dbe0b3fdc88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""The base.py module contains parent classes for components."""

import numpy as np
import os.path
import random
import logging

from aircraftstudio import creator

logging.basicConfig(filename='log_base.txt',
                    level=logging.DEBUG,
                    format='%(asctime)s - %(levelname)s - %(message)s')


class Aircraft:
    """This class tracks all sub-components and is fed to the evaluator."""
    name = None
    fuselage = None
    propulsion = None
    wing = None
    properties = {}

    naca = [2412, 3412, 2420]

    def __init__(self):
        self.results = {}

    def __str__(self):
        return self.name

    @classmethod
    def from_default(cls):
        aircraft = Aircraft()
        aircraft.name = 'default_aircraft_' + str(random.randrange(1000, 9999))
        airfoil = creator.wing.Airfoil(aircraft, 'default_airfoil')
        airfoil.add_naca(2412)
        soar1 = creator.wing.Spar(airfoil, 'default_spar_1', 0.30)
        soar2 = creator.wing.Spar(airfoil, 'default_spar_2', 0.60)
        stringer = creator.wing.Stringer(airfoil, 'default_stringer')
        return aircraft

    @classmethod
    def from_random(cls):
        aircraft = Aircraft()
        aircraft.name = 'random_aircraft_' + str(random.randrange(1000, 9999))
        airfoil = creator.wing.Airfoil(aircraft, 'random_airfoil')
        airfoil.add_naca(random.choice(cls.naca))
        soar1 = creator.wing.Spar(airfoil, 'random_spar_1',
                                  random.randrange(20, 80) / 100)
        soar2 = creator.wing.Spar(airfoil, 'random_spar_2',
                                  random.randrange(20, 80) / 100)
        stringer = creator.wing.Stringer(airfoil, 'random_stringer',
                                         random.randint(1, 10),
                                         random.randint(1, 10),
                                         random.randint(1, 10),
                                         random.randint(1, 10))
        return aircraft


class Component:
    """Basic component providing coordinates, tools and a component tree."""
    def __init__(self, parent, name):
        self.parent = parent
        self.name = name
        self.x = np.array([])
        self.z = np.array([])
        self.y = np.array([])
        self.material = None
        self.mass = float()
        self.properties = {}

    def __str__(self):
        return self.name

    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) is not np.ndarray:
                print(f'{k}:\n', v)
                print(num_of_dashes * '-')
        for k, v in self.__dict__.items():
            if type(v) is np.ndarray:
                print(f'{k}:\n', np.around(v, round))
        return None

    def info_save(self,
                  save_path='/home/blendux/Projects/Aircraft_Studio/save'):
        """Save all the object's coordinates (must be full path)."""
        file_name = f'{self.name}_info.txt'
        full_path = os.path.join(save_path, file_name)
        try:
            with open(full_path, 'w') as f:
                for k, v in self.__dict__.items():
                    if type(v) is not np.ndarray:
                        f.write(f'{k}=\n')
                        f.write(str(v))
                        f.write("\n")
                        # print(num_of_dashes * '-')
                for k, v in self.__dict__.items():
                    if type(v) is np.ndarray:
                        f.write(f'{k}=\n')
                        f.write(str(v))
                        f.write("\n")
            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
Copyright 2019--2024 Marius PETER