Skip to content

Commit 172a9ce

Browse files
committed
Funcional MK2.5
Unificadas funciones y variables en inglés, código correctamente comentado.
1 parent 15017f4 commit 172a9ce

File tree

14 files changed

+1105
-522
lines changed

14 files changed

+1105
-522
lines changed

aeropy/Xfoil_Interaction/.ipynb_checkpoints/result_drawer-checkpoint.ipynb

Lines changed: 466 additions & 0 deletions
Large diffs are not rendered by default.

aeropy/Xfoil_Interaction/Genetic_algorithm_files/ambient.py

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,37 @@
77
This is a submodule for the genetic algorithm that is explained in
88
https://docs.google.com/presentation/d/1_78ilFL-nbuN5KB5FmNeo-EIZly1PjqxqIB-ant-GfM/edit?usp=sharing
99
10-
This script is the main program. It will call the different submodules
11-
and manage the data transfer between them in order to achieve the
12-
genetic optimization of the profile.
13-
14-
'''
15-
10+
This script is the ambient subprogram. Its objective is to calculate the
11+
Mach and Reynolds numbers that XFoil uses to calculate the
12+
aerodynamics of airfoils.
1613
1714
15+
This subprogram consist mainly in models for the International Standard Atmosphere
16+
and an equivalent atmosphere model for Mars.
17+
'''
1818

1919
import numpy as np
2020

2121

2222

23-
24-
25-
def ventana (x, inicio=0, fin=1, f=1.):
26-
_1 = (np.sign(x-inicio))
27-
_2 = (np.sign(-x+fin))
23+
def window (x, start=0, end=1, f=1.):
24+
'''Returns a function f between 'start' and 'endn'.
25+
Returns 0 outside this interval'''
26+
_1 = (np.sign(x-start))
27+
_2 = (np.sign(-x+end))
2828
return 0.25 * (_1+1) * (_2+1) * f
2929

30-
def etc (x, inicio=0, f=1.):
31-
_1 = (np.sign(x-inicio))
30+
def etc (x, start=0, f=1.):
31+
'''Returns 0 until 'start', returns 'f' after.
32+
'''
33+
_1 = (np.sign(x-start))
3234
return 0.5 * (_1+1) * f
3335

3436
def earth_conditions():
37+
38+
'''Returns an array of atmospheric and physical data from which the complete
39+
model of the atmosphere can be built.
40+
'''
3541
heights = np.array([-1.000,
3642
11.019,
3743
20.063,
@@ -43,7 +49,7 @@ def earth_conditions():
4349
90.000])
4450

4551

46-
# an es el gradiente válido entre H(n-1) y H(n)
52+
4753
temp_gradient = np.array([-6.49,
4854
0,
4955
0.99,
@@ -72,8 +78,8 @@ def earth_conditions():
7278
return conditions
7379

7480

75-
def temperatura(h, conditions, dT = 0):
76-
'''Calcula la temperatura a una altura h en Km sobre el nivel del mar'''
81+
def temperature(h, conditions, dT = 0):
82+
'''Calculates the value for temperature in Kelvin at a certain altitude'''
7783
grad = 0
7884
heights = conditions[0]
7985
gradient = conditions[1]
@@ -84,50 +90,48 @@ def temperatura(h, conditions, dT = 0):
8490

8591
for layer in np.arange(0, atm_layer-1,1):
8692
increase = temp_points[layer] + gradient[layer] * (h - heights[layer])
87-
grad = grad + ventana(h, heights[layer],heights[layer+1], increase)
93+
grad = grad + window(h, heights[layer],heights[layer+1], increase)
8894
grad = grad + etc(h, heights[atm_layer-1],temp_points[atm_layer-1] + gradient[atm_layer-1]*(h - heights[atm_layer-1]))
8995
return grad + dT
9096

9197

92-
def segmento_presion_1(z, pi, z0, dT, conditions):
93-
'''calcula la presión en un segmento de atmósfera de temperatura constante'''
98+
def pressure_segment_1(z, pi, z0, dT, conditions):
99+
'''Calculates pressure throught a constant temperature segment of the atmosphere'''
94100
g = conditions[3]
95101
R = conditions[4]
96102
radius = conditions[5]
97103
h = (radius * z) /(radius + z)
98104
h0 = (radius * z0)/(radius + z0)
99-
_ = 1000*(h-h0) * g / (R * temperatura(z, conditions, dT))
105+
_ = 1000*(h-h0) * g / (R * temperature(z, conditions, dT))
100106
return pi * np.e ** -_
101107

102-
def segmento_presion_2(z, pi, Ti, a, dT, conditions):
103-
'''calcula la presión en un segmento de atmósfera con gradiente de temperatura "a" '''
108+
def pressure_segment_2(z, pi, Ti, a, dT, conditions):
109+
'''Calculates pressure throught a variant temperature segment of the atmosphere '''
104110
g = conditions[3]
105111
R = conditions[4]
106112
_ = g / (a*R/1000)
107-
return pi * (temperatura(z, conditions, dT)/(Ti + dT)) ** -_
113+
return pi * (temperature(z, conditions, dT)/(Ti + dT)) ** -_
108114

109-
def presion (h, conditions, dT = 0):
110-
'''Calcula la presion en Pa a una altura h en m sobre el nivel del mar'''
115+
def pressure (h, conditions, dT = 0):
116+
'''Calculates the value for pressure in Pascal at a certain altitude'''
111117

112118
heights = conditions[0]
113119
gradient = conditions[1]
114120
temp_points = conditions[2]
115121
atm_layer = gradient.shape[0]
116-
#Primero, calculamos la presion de cada punto de cambio de capa para la condición de dT pedida
117-
#Suponemos que la presión es siempre constante a 101325 Pa a nivel del mar
118122

119123

120124
pressure_points = np.zeros([atm_layer])
121125
pressure_points[0] = conditions[6]
122126

123127
for layer in np.arange(1, atm_layer, 1):
124128
if (abs(gradient[layer-1]) < 1e-8):
125-
pressure_points[layer] = segmento_presion_1(heights[layer],
129+
pressure_points[layer] = pressure_segment_1(heights[layer],
126130
pressure_points[layer - 1],
127131
heights[layer - 1],
128132
dT, conditions)
129133
else:
130-
pressure_points[layer] = segmento_presion_2(heights[layer],
134+
pressure_points[layer] = pressure_segment_2(heights[layer],
131135
pressure_points[layer - 1],
132136
temp_points[layer - 1],
133137
gradient[layer-1],
@@ -139,24 +143,24 @@ def presion (h, conditions, dT = 0):
139143
grad = 0
140144
for layer in np.arange(1, atm_layer, 1):
141145
if (abs(gradient[layer-1]) < 1e-8):
142-
funcion = segmento_presion_1(h,
146+
funcion = pressure_segment_1(h,
143147
pressure_points[layer - 1],
144148
heights[layer - 1],
145149
dT, conditions)
146150
else:
147-
funcion = segmento_presion_2(h,
151+
funcion = pressure_segment_2(h,
148152
pressure_points[layer - 1],
149153
temp_points[layer - 1],
150154
gradient[layer-1],
151155
dT, conditions)
152-
grad = grad + ventana(h, heights[layer-1], heights[layer], funcion)
156+
grad = grad + window(h, heights[layer-1], heights[layer], funcion)
153157
if (abs(gradient[layer-1])< 10e-8):
154-
funcion = segmento_presion_1(h,
158+
funcion = pressure_segment_1(h,
155159
pressure_points[layer - 1],
156160
heights[layer - 1],
157161
dT, conditions)
158162
else:
159-
funcion = segmento_presion_2(h,
163+
funcion = pressure_segment_2(h,
160164
pressure_points[layer - 1],
161165
temp_points[layer - 1],
162166
gradient[layer-1],
@@ -166,13 +170,16 @@ def presion (h, conditions, dT = 0):
166170
return grad
167171

168172

169-
def densidad(h, conditions, dT = 0):
170-
'''Calcula la densidad a una altura h en m sobre el nivel del mar'''
173+
def density(h, conditions, dT = 0):
174+
'''Calculates the value for density in Kg/m3 at a certain altitude'''
171175
R = conditions[4]
172-
return presion(h, conditions, dT)/(R * temperatura(h, conditions, dT))
176+
return pressure(h, conditions, dT)/(R * temperature(h, conditions, dT))
173177

174178

175179
def mars_conditions():
180+
'''Returns an array of atmospheric and physical data from which the complete
181+
model of the atmosphere can be built.
182+
'''
176183
heights = np.array([-8.3,
177184
8.85,
178185
30])
@@ -200,7 +207,10 @@ def mars_conditions():
200207

201208

202209

203-
def viscosidad(temp, planet):
210+
def viscosity(temp, planet):
211+
'''Calculates the value for viscosity in microPascal*second
212+
for a certain temperature'''
213+
204214
if (planet == 'Earth'):
205215
c = 120
206216
lamb = 1.512041288
@@ -213,16 +223,20 @@ def viscosidad(temp, planet):
213223

214224

215225
def Reynolds(dens, longitud, vel, visc):
226+
'''Calculates the Reynolds number'''
216227
re = 1000000 * dens * longitud * vel / visc
217228
return re
218229

219230

220231
def aero_conditions(ambient_data):
232+
'''Given a certain conditions, return the value of the Mach and Reynolds
233+
numbers, in that order.
234+
'''
221235
(planet, chord, height, speed_type, speed) = ambient_data
222236
planet_dic = {'Mars':mars_conditions(), 'Earth':earth_conditions()}
223237

224238

225-
sound = (1.4 *presion(height, planet_dic[planet]) / densidad(height,planet_dic[planet]))**0.5
239+
sound = (1.4 *pressure(height, planet_dic[planet]) / density(height,planet_dic[planet]))**0.5
226240

227241
if (speed_type == 'mach'):
228242
mach = speed
@@ -234,14 +248,9 @@ def aero_conditions(ambient_data):
234248
print('error in the data, invalid speed parameter')
235249

236250

237-
238-
re = Reynolds(densidad(height, planet_dic[planet]), chord, vel, viscosidad(temperatura(height, planet_dic[planet]), planet))
251+
visc = viscosity(temperature(height, planet_dic[planet]), planet)
252+
re = Reynolds(density(height, planet_dic[planet]), chord, vel, visc)
239253

240254

241255

242256
return [mach, re]
243-
#
244-
#ambient_data = ('Earth', 03.0003, 11, 'speed', 30.1)
245-
#
246-
#result = aero_conditions(('Earth', 0.03, 11, 'mach', 0.1))
247-
#print(result)

aeropy/Xfoil_Interaction/Genetic_algorithm_files/analice.py renamed to aeropy/Xfoil_Interaction/Genetic_algorithm_files/analyze.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,38 @@
77
This is a submodule for the genetic algorithm that is explained in
88
https://docs.google.com/presentation/d/1_78ilFL-nbuN5KB5FmNeo-EIZly1PjqxqIB-ant-GfM/edit?usp=sharing
99
10-
This script is the main program. It will call the different submodules
11-
and manage the data transfer between them in order to achieve the
12-
genetic optimization of the profile.
10+
This script is the analysis subprogram. Its objective is to assign a score to
11+
every airfoil by reading the aerodynamic characteristics that XFoil
12+
has calculated.
1313
1414
'''
1515

1616

1717

18-
import subprocess
19-
import sys
20-
import os
21-
import interfaz as interfaz
18+
2219
import numpy as np
23-
import initial as initial
2420

2521

2622

27-
#generation = 0
28-
#starting_profiles = 20
29-
#
30-
#genome = initial.start_pop(starting_profiles)
31-
#
32-
#interfaz.xfoil_calculate_population(generation,genome)
33-
#
34-
#num_pop = genome_matrix.shape[0]
3523

3624
def pop_analice (generation, num_pop):
25+
'''For a given generation and number of airfoils, returns an array which
26+
contains the maximun Lift Coefficient and Maximum Aerodinamic Efficiency
27+
for every airfoil.
28+
'''
3729
pop_results = np.zeros([num_pop,2])
3830
for profile_number in np.arange(1,num_pop+1,1):
3931
pop_results[profile_number - 1, :] = profile_analice (generation, profile_number)
4032

4133
return pop_results
4234

4335

44-
def profile_analice (generation, profile_number):
36+
def profile_analice (generation, profile_number):
37+
'''For a given generation and profile, searches for the results of the
38+
aerodynamic analysis made in Xfoil. Then, searches for the maximum
39+
values of the Lift Coefficient and Aerodynamic Efficiency and returns them
40+
as an 1x2 array.
41+
'''
4542
profile_name = 'gen' + str(generation) + 'prof' + str(profile_number)
4643
data_root = "aerodata\data" + profile_name + '.txt'
4744
datos = np.loadtxt(data_root, skiprows=12, usecols=[1,2])
@@ -60,14 +57,18 @@ def profile_analice (generation, profile_number):
6057
return np.array([clmax , maxefic])
6158

6259
def adimension(array):
60+
'''Adimensionalyzes an array with its maximun value
61+
'''
6362
max_value = max(array)
6463
adim = array / max_value
6564
return adim
6665

67-
def score(generation, num_pop):
68-
66+
def score(generation, num_pop, weights):
67+
'''For a given generation, number of airfoils and weight parameters, returns
68+
an array of the scores of all airfoils.
69+
'''
6970
pop_results = pop_analice (generation, num_pop)
7071
cl_score = adimension(pop_results[:,0])
7172
efic_score = adimension(pop_results[:,1])
72-
total_score = 0.3 * cl_score + 0.7 * efic_score
73+
total_score = weights[0] * cl_score + weights[1] * efic_score
7374
return total_score

aeropy/Xfoil_Interaction/Genetic_algorithm_files/cross.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,27 @@
77
This is a submodule for the genetic algorithm that is explained in
88
https://docs.google.com/presentation/d/1_78ilFL-nbuN5KB5FmNeo-EIZly1PjqxqIB-ant-GfM/edit?usp=sharing
99
10-
This script is the main program. It will call the different submodules
11-
and manage the data transfer between them in order to achieve the
12-
genetic optimization of the profile.
10+
This script is the cross subprogramme. Its objective is to generate
11+
a whole new population genome from the genome of the previous generation
12+
winners (parents).
13+
14+
In order to eliminate the chance of randomly getting worse results,
15+
the parents are preserved as the first elements of the new population.
1316
1417
'''
1518

1619

1720

18-
import subprocess
19-
import sys
20-
import os
21-
import interfaz as interfaz
22-
import numpy as np
23-
import initial as initial
2421

22+
import numpy as np
2523

2624

2725

2826
def cross(parents, num_pop):
27+
'''Generates a population of (num_pop) airfoil genomes by mixing randomly
28+
the genomes of the given parents.
29+
The parents are preserved as the first elements of the new population.
30+
'''
2931
children = np.zeros([num_pop, 16])
3032
num_parents = parents.shape[0]
3133
children[0:num_parents] = parents

0 commit comments

Comments
 (0)