-
Notifications
You must be signed in to change notification settings - Fork 1
/
windturbine.py
executable file
·1869 lines (1618 loc) · 74 KB
/
windturbine.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import os
from math import ceil, floor
import matplotlib
matplotlib.use("tkagg")
import matplotlib.pyplot as plt
from core_utils import midLine
try:
# PYTHON OCC IMPORT
from OCC.BRepAlgoAPI import BRepAlgoAPI_Fuse
from OCC.BRepBuilderAPI import BRepBuilderAPI_Transform
from OCC.BRepOffsetAPI import BRepOffsetAPI_ThruSections
from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere
from OCC.Geom2dAPI import Geom2dAPI_PointsToBSpline
from OCC.GeomAPI import geomapi
from OCC.STEPControl import STEPControl_Writer
from OCC.StlAPI import StlAPI_Writer
from OCC.TColgp import TColgp_Array1OfPnt2d
from OCC.IFSelect import IFSelect_RetDone
from OCC.Interface import Interface_Static_SetCVal
from OCC.STEPControl import STEPControl_AsIs
from OCC.gp import gp_Pnt, gp_Pln, gp_Dir, gp_Pnt2d, gp_Trsf, gp_OZ, gp_OY
from OCC.Display.SimpleGui import init_display
# PYTHON OCC IMPORT
except:
print 'Warning... No python-occ libs found the visualization functions may not work'
import subprocess
from core_utils import *
from naca import naca4, naca5
class Airfoils(object):
def __init__(self, name, **kwargs):
"""
:param name:
:param kwargs:
:raise Exception:
"""
denomination = kwargs['denomination'] if 'denomination' in kwargs else None
radius = kwargs['radius'] if 'radius' in kwargs else 0.5
radius = float(radius)
n_points = 250 # number of on the profiles
self.coordinates = np.array([])
if name == 'circle':
self.coordinates = self.__circle(radius, (0.5, 0), n_points)
self.aerodynamic_center = np.array((0.5, 0))
self.type = 'circle'
elif name == 'naca4' or name == 'naca5':
self.type = name
self.coordinates = self.__naca(name, denomination, n_points)
self.aerodynamic_center = self.chordLine()[len(self.chordLine()) / 4]
else:
self.type = 'from_file'
self.coordinates = increase_resolution(self.__from_file(name), res=250)
self.aerodynamic_center = self.chordLine()[len(self.chordLine()) / 4]
if not self.coordinates.any():
raise Exception('Something Went\'s Wrong')
else:
self.coordinates[-1, :] = self.coordinates[0, :]
def extremePoint(self):
return getExtemePoints(self.coordinates)
def chordLine(self, points=50):
x = np.linspace(self.coordinates[getExtemePoints(self.coordinates)[2]][0], self.coordinates[0][0], points)
y = np.linspace(self.coordinates[getExtemePoints(self.coordinates)[2]][1], self.coordinates[0][1], points)
return np.dstack((x, y))[0]
def plot(self):
"""
:rtype : object
"""
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
ax.plot(self.coordinates[:, 0], self.coordinates[:, 1], 'ok')
ax.plot(self.chordLine()[:, 0], self.chordLine()[:, 1], '-y')
ax.plot(self.aerodynamic_center[0], self.aerodynamic_center[1], 'o')
ax.grid()
plt.show()
def __from_file(self, file):
"""
:rtype : object
:param name:
:return: :raise Exception:
"""
with open(file, 'rb') as f:
if f:
f.next()
return np.array([[float(x) for x in line.split()] for line in f if line.strip()])
else:
raise Exception('ERROR! Not found "' + file + '" in the airfoils database')
def __naca(self, airfoilType, denomination, n_points=120):
"""
:rtype : object
:param airfoilType:
:param denomination:
:return:
"""
if airfoilType == 'naca4':
return naca4(denomination, n_points)
elif airfoilType == 'naca5':
return naca5(denomination, n_points)
@staticmethod
def __circle(r, c=(0.5, 0), n_points=120):
"""
:rtype : object
:param r: 1
:param c:
:param n:
:return:
"""
x_y = [[c[0] + r * np.cos(theta), c[1] + r * np.sin(theta)] for theta in
np.linspace(0, 2 * np.pi, n_points + 1)]
x_y[n_points] = x_y[0]
return np.array(x_y)
class Blade(object):
def __init__(self, blade):
self.blade = blade
self.__modifyAirfoils()
self.length = self.blade[-1]['position'][2]
def __modifyAirfoils(self):
"""
:rtype : object
:return:
"""
airfoils = []
aerodynamic_center = []
for i in self.blade['airfoils']:
tmp = copy.deepcopy(i)
# tmp['airfoil'].coordinates = tmp['airfoil'].coordinates * tmp['scale']
# tmp['airfoil'].aerodynamic_center = tmp['airfoil'].aerodynamic_center * tmp['scale']
# move the airfoil by the aerodynamic center to [0;0]
tmp['airfoil'].coordinates = tmp['airfoil'].coordinates - tmp['airfoil'].aerodynamic_center
tmp['airfoil'].aerodynamic_center = (0., 0.)
# scale the airfoil
tmp['airfoil'].coordinates = tmp['airfoil'].coordinates * tmp['scale']
# make transformations twist and blade_twist to the airfoil, by its aerodynamic center
if not i['airfoil'].type is 'circle':
if tmp['twist']:
tmp['airfoil'].coordinates = rotate2d(tmp['airfoil'].coordinates, tmp['twist'],
tmp['airfoil'].aerodynamic_center)
if self.blade['blade_twist']:
tmp['airfoil'].coordinates = rotate2d(tmp['airfoil'].coordinates, self.blade['blade_twist'],
(0., 0.))
# move the airfoil to the design coordinates
tmp['airfoil'].coordinates = tmp['airfoil'].coordinates + i['position'][0:2]
tmp['airfoil'].aerodynamic_center = i['position']
tmp['airfoil'].coordinates = np.insert(tmp['airfoil'].coordinates, 2, i['position'][2], axis=1)
airfoils.append(tmp)
self.blade = airfoils
# self.blade_coords = [i['airfoil'].coordinates for i in airfoils]
def plot(self):
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
for i in self.blade:
x = i['airfoil'].coordinates[:, 0]
y = i['airfoil'].coordinates[:, 1]
ax.plot(x, y)
ax.plot(i['airfoil'].aerodynamic_center[0], i['airfoil'].aerodynamic_center[1], 'o')
ax.grid()
plt.show()
def view(self):
display, start_display, add_menu, add_function_to_menu = init_display()
blade = self.__OCCBladeModel()
display.DisplayShape(blade)
display.FitAll()
start_display()
def vtkview(self):
from mayavi import mlab
x, y, z = [], [], []
# poner todos los putos de los perfiles, en listas x,y,z
for i in self.blade:
for point in i['airfoil'].coordinates:
x.append(point[0])
y.append(i['position'][2])
z.append(point[1])
fig = mlab.figure(1, bgcolor=(1, 0.7, 1), fgcolor=(0.5, 0.5, 0.5))
# configuacion de la vista, de la ventana o algo por el estilo
vtk_source = mlab.points3d(x, y, z, opacity=0.3, mode='2dvertex')
vtk_source.actor.property.point_size = 3
# mayar y visualizar
delaunay = mlab.pipeline.delaunay2d(vtk_source)
edges = mlab.pipeline.extract_edges(delaunay)
surf = mlab.pipeline.surface(edges, colormap='jet')
mlab.show()
def __OCCBladeModel(self):
# FIXME revisar completo
"""
:rtype : object
:return:
"""
faces = []
for i in self.blade:
coordinates = i['airfoil'].coordinates
plane = gp_Pln(gp_Pnt(0., i['position'][2], 0.), gp_Dir(0., 1., 0.))
j = 1
array = TColgp_Array1OfPnt2d(1, len(coordinates))
for point in coordinates:
x = point[0]
z = point[2]
array.SetValue(j, gp_Pnt2d(x, z))
j += 1
curve = Geom2dAPI_PointsToBSpline(array).Curve()
spline = geomapi.To3d(curve, plane)
edge = make_edge(spline)
wire = make_wire(edge)
faces.append(wire)
blade = BRepOffsetAPI_ThruSections(True, False)
map(blade.AddWire, faces)
blade.Build()
my_Trsf = gp_Trsf()
blade = BRepBuilderAPI_Transform(blade.Shape(), my_Trsf).Shape()
return blade
def exportToStl(self, file):
"""
:param file:
"""
blade = self.__OCCBladeModel()
stl_output_file = file
stl_ascii_format = False
stl_export = StlAPI_Writer()
stl_export.Write(blade, stl_output_file, stl_ascii_format)
def exportToSTEP(self, file, schema="AP214"):
"""
:param file:
"""
print('Creating "' + file + '" this may take awhile')
blade = self.__OCCBladeModel
# initialize the STEP exporter
step_writer = STEPControl_Writer()
Interface_Static_SetCVal("write.step.schema", schema)
# transfer shapes and write file
step_writer.Transfer(blade, STEPControl_AsIs)
status = step_writer.Write(file)
assert (status == IFSelect_RetDone)
def exportToAnsys(self, file='coordinates.txt'):
f = open(file, 'w')
for i in self.blade:
for point in i['airfoil'].coordinates:
f.write('1 %s %s %s\n' % (point[0], i['position'][2], point[1]))
class Rotor(object):
def __init__(self, rotor):
self.rotor = rotor
self.radius = self.rotor['blade'].length + self.rotor['hub_radius']
self.diameter = self.radius * 2
self.hub_length = self.rotor['hub_length']
self.hub_radius = self.rotor['hub_radius']
def __OCCRotorModel(self):
angular = np.radians(360 / self.rotor['n_blades'])
blade = self.rotor['blade']
bladeShape = blade.__OCCBladeModel()
shapes = []
my_Trsf = gp_Trsf()
theSphere = BRepPrimAPI_MakeSphere(gp_Pnt(0, 0, 0),
self.rotor['blade'].blade[0]['airfoil'].coordinates.max()).Shape()
shapes.append(theSphere)
for i in range(0, self.rotor['n_blades']):
my_Trsf.SetRotation(gp_OZ(), i * angular)
# my_Trsf.SetDisplacement(self.rotor['hub_radius'],gp_OZ())
shapes.append(BRepBuilderAPI_Transform(bladeShape, my_Trsf).Shape())
return shapes
def view(self):
display, start_display, add_menu, add_function_to_menu = init_display()
display.DisplayShape(self.__OCCRotorModel())
display.FitAll()
start_display()
def exportToSTEP(self, file, schema="AP214"):
print('Creating "' + file + '" this may take awhile')
shapes = self.__OCCRotorModel()
rotor = shapes[0]
for shape in shapes:
rotor = BRepAlgoAPI_Fuse(rotor, shape).Shape()
# initialize the STEP exporter
step_writer = STEPControl_Writer()
Interface_Static_SetCVal("write.step.schema", schema)
# transfer shapes and write file
step_writer.Transfer(rotor, STEPControl_AsIs)
status = step_writer.Write(file)
assert (status == IFSelect_RetDone)
def exportToStl(self, file):
shapes = self.__OCCRotorModel()
rotor = shapes[0]
for shape in shapes:
rotor = BRepAlgoAPI_Fuse(rotor, shape).Shape()
stl_output_file = file
stl_ascii_format = False
stl_export = StlAPI_Writer()
stl_export.Write(rotor, stl_output_file, stl_ascii_format)
class Foam(object):
def __init__(self, rotor, path='OpenFoamCase'):
self.dirs = {}
self.path = path
self.rotor = rotor
self.rotorObj = Rotor(rotor)
# PpenFoam case directories
self.dirs['main'] = path if path.endswith('/') else path + '/'
self.dirs['system'] = path + '/system/'
self.dirs['constant'] = path + '/constant/'
self.dirs['geometry'] = path + '/geometry/'
self.dirs['polyMesh'] = path + '/constant/polyMesh/'
self.dirs['triSurface'] = path + '/constant/triSurface/'
self.__openFoamInit()
self.mesh = OpenFoamBlockMesh(self)
def __openFoamInit(self):
try:
os.mkdir(self.dirs['main'])
os.mkdir(self.dirs['system'])
os.mkdir(self.dirs['constant'])
os.mkdir(self.dirs['polyMesh'])
except:
pass
# FIXME crear clase para generar la configuracion del caso
renderTemplate('controlDict.jinja', self.dirs['system'] + 'controlDict', '')
renderTemplate('fvSchemes.jinja', self.dirs['system'] + 'fvSchemes', '')
renderTemplate('fvSolution.jinja', self.dirs['system'] + 'fvSolution', '')
# FIXME END
# Generar y dar permiso de ejecucion a los ficheros para correr el caso de openfoam
renderTemplate('Allclean.jinja', self.dirs['main'] + 'Allclean', '')
subprocess.call(['chmod', '+x', self.dirs['main'] + 'Allclean'])
class OpenFoamBlockMesh(object):
def __init__(self, study):
self.study = study
self.blade = self.study.rotor['blade'].blade
self.hub = self.study.rotor['hub_radius']
self.hub_length = self.study.rotor['hub_length']
self.n_blades = self.study.rotor['n_blades']
self.convert_to_meters = 1.0 # factor de conversion de unidades a metros
self.tunnel_radius = 80 # Radio del tunel
self.tunnel_length = [80, 80] # Longitud del tunel, [frontal, trasera]
self.rotor_disk_length = [20, 20] # Longitud del tunel, [frontal, trasera]
self.airfoil_offset = 0.5 # distancia del perfil al perfil offset
self.leading_edge_skewness_factor = 30
self.leading_edge_region = 10
self.__div_angle = np.deg2rad(360 / self.n_blades - 90)
def topology_O(self, n_point=0):
vertices = []
blocks = []
splines = []
faces = []
inc = 15 # <- Numero de vertices de la topologia
cells_z = 250 # <- Divisiones de la pala en sentido del eje Z (aproximadamente)
L = self.blade[-1]['position'][2]
block_divisions = []
for i in xrange(len(self.blade) - 1):
block_lenght = self.blade[i + 1]['position'][2] - self.blade[i]['position'][2]
block_divisions.append(ceil(block_lenght / L * cells_z))
def form_blocks(base_block, inc):
top_block = [i + inc for i in base_block]
if len(base_block) > 4:
return [i + inc for i in base_block]
else:
return base_block + top_block
b0 = form_blocks([14, 9, 3, 8], inc)
b1 = form_blocks([4, 3, 9, 10], inc)
b2 = form_blocks([5, 4, 10, 11], inc)
b3 = form_blocks([6, 5, 11, 12], inc)
b4 = form_blocks([12, 13, 7, 6], inc)
b5 = form_blocks([13, 14, 8, 7], inc)
def form_face(face, inc):
face.extend([face[1] + inc, face[0] + inc])
return face
f0 = form_face([4, 3, ], inc)
f1 = form_face([5, 4, ], inc)
f2 = form_face([5, 6, ], inc)
f3 = form_face([7, 6, ], inc)
f4 = form_face([8, 7, ], inc)
f5 = form_face([3, 8, ], inc)
spl0 = [3, 4]
spl1 = [4, 5]
spl2 = [5, 6]
spl3 = [6, 7]
spl4 = [7, 8]
spl5 = [8, 3]
spl6 = [9, 10]
spl7 = [10, 11]
spl8 = [11, 12]
spl9 = [12, 13]
spl10 = [13, 14]
spl11 = [14, 9]
for iter, airfoil in enumerate(self.blade):
coords = airfoil['airfoil'].coordinates
midline_coords = midLine(coords)
# FIXME make 3d offset and split the end circe in two parts
offset_value = airfoil['scale']/12
offset_airfoil = offset(coords[:, [0, 1]], offset_value)
offset_airfoil = np.insert(offset_airfoil, 2, airfoil['position'][2], axis=1)
if airfoil['airfoil'].type is 'circle':
offset_airfoil = offset_airfoil[50:-50]
# TODO add angle and parts to the mesh params
parts = 12.
angle = 60.
p = np.insert(split_curve(midline_coords, parts), 2, airfoil['position'][2], axis=1)
p0 = p[0]
p1 = p[(len(p) - 1) / 2]
p2 = p[-1]
normal_to_1 = [p1, p2]
normal_to_2 = [p0, p1]
tmp_3 = end_point_line(p2, angle, np.max(coords[:, [0, 1]]), normal_to_1)
tmp_5 = end_point_line(p0, 180 - angle, np.max(coords[:, [0, 1]]), normal_to_2)
tmp_6 = end_point_line(p0, 180 + angle, np.max(coords[:, [0, 1]]), normal_to_2)
tmp_8 = end_point_line(p2, -angle, np.max(coords[:, [0, 1]]), normal_to_1)
p3 = intersection(p2, tmp_3, coords)
p5 = intersection(p0, tmp_5, coords)
p4 = [coords[(p5[1] - p3[1]) / 2 + p3[1]].tolist(), (p5[1] - p3[1]) / 2 + p3[1]]
p6 = intersection(p0, tmp_6, coords)
p8 = intersection(p2, tmp_8, coords)
p7 = [coords[(p8[1] - p6[1]) / 2 + p6[1]].tolist(), (p8[1] - p6[1]) / 2 + p6[1]]
p9 = intersection(p2, tmp_3, offset_airfoil)
p11 = intersection(p0, tmp_5, offset_airfoil)
p10 = [offset_airfoil[(p11[1] - p9[1]) / 2 + p9[1]].tolist(), (p11[1] - p9[1]) / 2 + p9[1]]
p12 = intersection(p0, tmp_6, offset_airfoil)
p14 = intersection(p2, tmp_8, offset_airfoil)
p13 = [offset_airfoil[(p14[1] - p12[1]) / 2 + p12[1]].tolist(), (p14[1] - p12[1]) / 2 + p12[1]]
points = [
[0, p0.tolist()],
[1, p1.tolist()],
[2, p2.tolist()],
[3, p3[0]],
[4, p4[0]],
[5, p5[0]],
[6, p6[0]],
[7, p7[0]],
[8, p8[0]],
[9, p9[0]],
[10, p10[0]],
[11, p11[0]],
[12, p12[0]],
[13, p13[0]],
[14, p14[0]],
]
vertices.append(points)
blocks.append(
[
[form_blocks(b0, inc * iter), [50, 20, 2], [1, 1, 1]],
[form_blocks(b1, inc * iter), [25, 20, 2], [1, 1, 1]],
[form_blocks(b2, inc * iter), [25, 20, 2], [1, 1, 1]],
[form_blocks(b3, inc * iter), [50, 20, 2], [1, 1, 1]],
[form_blocks(b4, inc * iter), [25, 20, 2], [1, 1, 1]],
[form_blocks(b5, inc * iter), [25, 20, 2], [1, 1, 1]],
]
)
spline0 = coords[p3[1]: p4[1] + 1]
spline1 = coords[p4[1]: p5[1] + 1]
spline2 = coords[p5[1]: p6[1] + 1]
spline3 = coords[p6[1]: p7[1] + 1]
spline4 = coords[p7[1]: p8[1] + 1]
spline5 = np.concatenate((coords[p8[1]:-1], coords[0:p3[1]]))
spline6 = offset_airfoil[p9[1]: p10[1] + 1]
spline7 = offset_airfoil[p10[1]: p11[1] + 1]
spline8 = offset_airfoil[p11[1]:p12[1] + 1]
spline9 = offset_airfoil[p12[1]: p13[1] + 1]
spline10 = offset_airfoil[p13[1]: p14[1] + 1]
spline11 = np.concatenate((offset_airfoil[p14[1]:-1], offset_airfoil[0:p9[1]]))
splines.append([
[[i + inc * iter for i in spl0], spline0],
[[i + inc * iter for i in spl1], spline1],
[[i + inc * iter for i in spl2], spline2],
[[i + inc * iter for i in spl3], spline3],
[[i + inc * iter for i in spl4], spline4],
[[i + inc * iter for i in spl5], spline5],
[[i + inc * iter for i in spl6], spline6],
[[i + inc * iter for i in spl7], spline7],
[[i + inc * iter for i in spl8], spline8],
[[i + inc * iter for i in spl9], spline9],
[[i + inc * iter for i in spl10], spline10],
[[i + inc * iter for i in spl11], spline11],
]
)
faces.append([
[i + inc * iter for i in f0],
[i + inc * iter for i in f1],
[i + inc * iter for i in f2],
[i + inc * iter for i in f3],
[i + inc * iter for i in f4],
[i + inc * iter for i in f5],
])
#
# # FIXME add points label to plot
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
ax.plot(coords[:, 0], coords[:, 1],'-')
ax.plot(midline_coords[:, 0], midline_coords[:, 1],'x')
# # ax.plot(coords[len(coords) / 2, 0], coords[len(coords) / 2, 1], 'mo')
# # ax.plot(coords[0, 0], coords[0, 1], 'mo')
ax.plot(offset_airfoil[:, 0], offset_airfoil[:, 1], 'k')
# # ax.plot(offset_airfoil[0, 0], offset_airfoil[0, 1], 'bo')
# # ax.plot(offset_airfoil[-1, 0], offset_airfoil[-1, 1], 'bo')
# # ax.plot(midline_offset[:, 0], midline_offset[:, 1], 'b-')
# # ax.plot(spline0[:, 0], spline0[:, 1], 'x')
# # ax.plot(spline1[:, 0], spline1[:, 1], 'x')
# # ax.plot(spline2[:, 0], spline2[:, 1], 'x')
# # ax.plot(spline3[:, 0], spline3[:, 1], 'x')
# # ax.plot(spline4[:, 0], spline4[:, 1], 'x')
# # ax.plot(spline5[:, 0], spline5[:, 1], 'x')
# # ax.plot(spline6[:, 0], spline6[:, 1], '-')
# # ax.plot(spline7[:, 0], spline7[:, 1], '-')
# # ax.plot(spline8[:, 0], spline8[:, 1], '-')
# # ax.plot(spline9[:, 0], spline9[:, 1], '-')
# # ax.plot(spline10[:, 0], spline10[:, 1], '-')
# # ax.plot(spline11[:, 0], spline11[:, 1], '-')
# # ax.plot(tmp_3[0], tmp_3[1], 'ro')
# # ax.plot(tmp_4[0], tmp_4[1], 'ro')
# # ax.plot(tmp_6[0], tmp_6[1], 'ro')
# # ax.plot(tmp_7[0], tmp_7[1], 'ro')
#
for p in points:
ax.plot(p[1][0], p[1][1], 'bo')
ax.grid()
plt.show()
# TODO splines radial direction
# splines radial directions
trasposed_vertices = map(list, zip(*vertices))[0]
a = bspline(increase_resolution(np.array([i[1] for i in trasposed_vertices]), 100), n=300, degree=6)
# extend and reorder elements
vertices = ((id, val[1]) for id, val in enumerate((j for i in vertices for j in i)))
# FIXME preformances issues
final_blocks = []
for idx, block in enumerate(blocks[:-1]):
for blk in block:
b = blk[0]
cells = [blk[1][0], blk[1][1], int(block_divisions[idx])]
grading = blk[2]
final_blocks.append([b, cells, grading, ])
faces = (j for i in faces[:-1] for j in i)
face = [['Blade', 'wall', faces], ]
# topology splines
splines = (j for i in splines for j in i)
data = {
'vertices': vertices,
'blocks': final_blocks,
'splines': splines,
'faces': face,
}
renderTemplate('blockMeshDict_O.jinja', self.study.dirs['polyMesh'] + 'blockMeshDict', data)
def h_topology(self, init_point):
# init_point = 0
inc = 16
spline_points = 0 + init_point
arc_points = 0 + init_point
ptos = []
blocks = []
splines = []
arcss = []
blade_face = []
in_face = []
out_face = []
periodic_face_1 = []
periodic_face_2 = []
# bloques
b0 = [np.array([0, 4, 5, 1] + [x + inc for x in [0, 4, 5, 1]]) + init_point, [10, 10, 10], [1, 1, 1]]
b1 = [np.array([1, 5, 6, 2] + [x + inc for x in [1, 5, 6, 2]]) + init_point, [10, 10, 10], [1, 1, 1]]
b2 = [np.array([2, 6, 7, 3] + [x + inc for x in [2, 6, 7, 3]]) + init_point, [10, 10, 10], [1, 1, 1]]
b3 = [np.array([3, 7, 4, 0] + [x + inc for x in [3, 7, 4, 0]]) + init_point, [10, 10, 10], [1, 1, 1]]
b4 = [np.array([7, 11, 8, 4] + [x + inc for x in [7, 11, 8, 4]]) + init_point, [10, 10, 10], [1, 1, 1]]
b5 = [np.array([4, 8, 9, 5] + [x + inc for x in [4, 8, 9, 5]]) + init_point, [10, 10, 10], [1, 1, 1]]
b6 = [np.array([5, 9, 10, 6] + [x + inc for x in [5, 9, 10, 6]]) + init_point, [10, 10, 10], [1, 1, 1]]
b7 = [np.array([6, 10, 11, 7] + [x + inc for x in [6, 10, 11, 7]]) + init_point, [10, 10, 10], [1, 1, 1]]
b8 = [np.array([8, 12, 13, 9] + [x + inc for x in [8, 12, 13, 9]]) + init_point, [10, 10, 10], [1, 1, 1]]
b9 = [np.array([11, 10, 14, 15] + [x + inc for x in [11, 10, 14, 15]]) + init_point, [10, 10, 10], [1, 1, 1]]
# dominio de la pala
# FIXME cerrar el hub con un wall en alcho definido
f0 = np.array([0, 1] + [x + inc for x in [1, 0]]) + init_point
f1 = np.array([1, 2] + [x + inc for x in [2, 1]]) + init_point
f2 = np.array([2, 3] + [x + inc for x in [3, 2]]) + init_point
f3 = np.array([3, 0] + [x + inc for x in [0, 3]]) + init_point
hub_face = np.array([
[0, 4, 5, 1],
[4, 8, 9, 5],
[1, 5, 6, 2],
[5, 9, 10, 6],
[2, 6, 7, 3],
[6, 10, 11, 7],
[3, 7, 4, 0],
[7, 11, 8, 4],
]) + init_point
# dominio de entrada
f4 = np.array([9, 10] + [x + inc for x in [10, 9]]) + init_point
f5 = np.array([9, 13] + [x + inc for x in [13, 9]]) + init_point
f6 = np.array([10, 14] + [x + inc for x in [14, 10]]) + init_point
# dominio de salida
f7 = np.array([8, 11] + [x + inc for x in [11, 8]]) + init_point
f8 = np.array([8, 12] + [x + inc for x in [12, 8]]) + init_point
f9 = np.array([11, 15] + [x + inc for x in [15, 11]]) + init_point
# dominio periodico
f10 = np.array([14, 15] + [x + inc for x in [15, 14]]) + init_point
f11 = np.array([12, 13] + [x + inc for x in [13, 12]]) + init_point
for i in self.blade:
radial_position = self.hub + i['position'][2]
coord = np.insert(i['airfoil'].coordinates, 2, values=radial_position, axis=1)
offset_airfoil = offset(coord[:, [0, 1]], 0.03)
offset_airfoil = np.insert(offset_airfoil, 2, values=radial_position, axis=1)
control_points = self.__getControlPoint(coord, [self.leading_edge_region, 0, 0, self.leading_edge_region])
if i['airfoil'].type is 'circle':
offset_airfoil = offset_airfoil[:-84]
midline = i['airfoil'].midLine()
cp0 = midline[len(midline) * 1 / 6]
cp1 = midline[len(midline) / 2]
cp2 = midline[len(midline) * 5 / 6]
# FIXME parametrizar 60 grados
x1 = max(offset_airfoil[:, 0])
p0 = np.array(intersection(cp0[0], cp0[1], 60, coord[:, [0, 1]], x1=x1))
p1 = np.array(intersection(cp2[0], cp2[1], -60, coord[:, [0, 1]], x1=-x1))
p2 = np.array(intersection(cp2[0], cp2[1], 60, coord[:, [0, 1]], x1=-x1))
p3 = np.array(intersection(cp0[0], cp0[1], -60, coord[:, [0, 1]], x1=x1))
p8 = np.array([self.hub_length / 2, self.hub * np.cos(self.__div_angle)])
p9 = np.array([-self.hub_length / 2, self.hub * np.cos(self.__div_angle)])
p10 = np.array([-self.hub_length / 2, -self.hub * np.cos(self.__div_angle)])
p11 = np.array([self.hub_length / 2, -self.hub * np.cos(self.__div_angle)])
# p8 = np.array([1.2 * self.hub_length / 2, self.hub * np.cos(self.__div_angle)])
# p9 = np.array([1.2 * -self.hub_length / 2, self.hub * np.cos(self.__div_angle)])
# p10 = np.array([1.2 * -self.hub_length / 2, -self.hub * np.cos(self.__div_angle)])
# p11 = np.array([1.2 * self.hub_length / 2, -self.hub * np.cos(self.__div_angle)])
arc0 = arc3points(cp0, p0[0], p8)
arc1 = arc3points(cp2, p1[0], p9)
arc2 = arc3points(cp2, p2[0], p10)
arc3 = arc3points(cp0, p3[0], p11)
arc4 = np.matrix(
zip(np.linspace(p8[0], p11[0], 100), np.linspace(p8[1], p11[1], 100), [radial_position] * 100))
arc5 = np.matrix(
[np.linspace(p9[0], p10[0], 100), np.linspace(p9[1], p10[1], 100), [radial_position] * 100, ]).T
arc0 = arc0[~np.isnan(arc0).any(axis=1)]
arc1 = arc1[~np.isnan(arc1).any(axis=1)]
arc2 = arc2[~np.isnan(arc2).any(axis=1)]
arc3 = arc3[~np.isnan(arc3).any(axis=1)]
p4 = nearest(arc0, offset_airfoil[:, [0, 1]])
p5 = nearest(arc1, offset_airfoil[:, [0, 1]])
p6 = nearest(arc2, offset_airfoil[:, [0, 1]])
p7 = nearest(arc3, offset_airfoil[:, [0, 1]])
cp3 = nearest(arc0, coord[:, [0, 1]])[2]
cp4 = nearest(arc1, coord[:, [0, 1]])[2]
cp5 = nearest(arc2, coord[:, [0, 1]])[2]
cp6 = nearest(arc3, coord[:, [0, 1]])[2]
p12 = -radial_position * np.cos(self.__div_angle), radial_position * np.sin(self.__div_angle), p9[0]
p13 = -radial_position * np.cos(self.__div_angle), radial_position * np.sin(self.__div_angle), p8[0]
p14 = radial_position * np.cos(self.__div_angle), radial_position * np.sin(self.__div_angle), p8[0]
p15 = radial_position * np.cos(self.__div_angle), radial_position * np.sin(self.__div_angle), p9[0]
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
ax.plot(coord[:, 0], coord[:, 1])
ax.plot(offset_airfoil[:, 0], offset_airfoil[:, 1])
ax.plot(midline[:, 0], midline[:, 1])
# HUB
ax.plot([self.hub_length / 2, -self.hub_length / 2], [self.hub / 2, self.hub / 2],
[-self.hub_length / 2, -self.hub_length / 2], [self.hub / 2, -self.hub / 2],
[-self.hub_length / 2, self.hub_length / 2], [-self.hub / 2, -self.hub / 2],
[self.hub_length / 2, self.hub_length / 2], [-self.hub / 2, self.hub / 2])
# DOMINIO
ax.plot([p8[0], p9[0]], [p8[1], p9[1]],
[p9[0], p10[0]], [p9[1], p10[1]],
[p10[0], p11[0]], [p10[1], p11[1]],
[p11[0], p8[0]], [p11[1], p8[1]])
ax.plot(p0[0][0], p0[0][1], 'o')
ax.plot(p1[0][0], p1[0][1], 'o')
ax.plot(p2[0][0], p2[0][1], 'o')
ax.plot(p3[0][0], p3[0][1], 'o')
#
ax.plot(cp0[0], cp0[1], 'ro')
ax.plot(cp1[0], cp1[1], 'bo')
ax.plot(cp2[0], cp2[1], 'mo')
ax.plot(arc0[cp3:, 0], arc0[cp3:, 1], 'r--')
ax.plot(arc1[cp4:, 0], arc1[cp4:, 1], 'r--')
ax.plot(arc2[cp5:, 0], arc2[cp5:, 1], 'r--')
ax.plot(arc3[cp6:, 0], arc3[cp6:, 1], 'r--')
#
ax.plot(p4[0][0], p4[0][1], 'mo')
ax.plot(p5[0][0], p5[0][1], 'mo')
ax.plot(p6[0][0], p6[0][1], 'mo')
ax.plot(p7[0][0], p7[0][1], 'mo')
ax.plot(p8[0], p8[1], 'mo')
ax.plot(p9[0], p9[1], 'mo')
ax.plot(p10[0], p10[1], 'mo')
ax.plot(p11[0], p11[1], 'mo')
ax.grid()
plt.show()
coord = rotate3d(coord, 90, 'x')
coord = rotate3d(coord, -90, 'y')
offset_airfoil = rotate3d(offset_airfoil, 90, 'x')
offset_airfoil = rotate3d(offset_airfoil, -90, 'y')
coord = blend(coord, radial_position)
offset_airfoil = blend(offset_airfoil, radial_position)
pt = map(lambda x: np.insert(x, 2, values=radial_position),
[p0[0], p1[0], p2[0], p3[0], p4[0], p5[0], p6[0], p7[0], p8, p9, p10, p11])
pt = rotate3d(pt, 90, 'x')
pt = rotate3d(pt, -90, 'y')
pt = blend(pt, radial_position)
pt = np.array(pt + [p12, p13, p14, p15])
arcs = map(lambda x: np.insert(x, 2, values=radial_position, axis=1), [arc0, arc1, arc2, arc3])
arcs = map(lambda x: rotate3d(x, 90, 'x'), arcs)
arcs = map(lambda x: rotate3d(x, -90, 'y'), arcs)
arcs = map(lambda x: blend(x, radial_position), arcs)
ptos.append(pt)
blocks.append(
(b0, b1, b2, b3, b4, b5, b6, b7, b8, b9)
)
b0 = [b0[0] + inc, b0[1], b0[2]]
b1 = [b1[0] + inc, b1[1], b1[2]]
b2 = [b2[0] + inc, b2[1], b2[2]]
b3 = [b3[0] + inc, b3[1], b3[2]]
b4 = [b4[0] + inc, b4[1], b4[2]]
b5 = [b5[0] + inc, b5[1], b5[2]]
b6 = [b6[0] + inc, b6[1], b6[2]]
b7 = [b7[0] + inc, b7[1], b7[2]]
b8 = [b8[0] + inc, b8[1], b8[2]]
b9 = [b9[0] + inc, b9[1], b9[2]]
#
splines.append(
(
((spline_points, spline_points + 1),
(
coord[p0[1]:p1[1]]
)),
((spline_points + 1, spline_points + 2),
(
coord[p1[1]:p2[1]]
)),
((spline_points + 2, spline_points + 3),
(
coord[p2[1]:p3[1]]
)),
((spline_points + 3, spline_points),
(
[i for i in [coord[p3[1]:] + coord[:p0[1]]]][0]
)),
((spline_points + 4, spline_points + 5),
(
offset_airfoil[p4[1]:p5[1]]
)),
((spline_points + 5, spline_points + 6),
(
offset_airfoil[p5[1]:p6[1]]
)),
((spline_points + 6, spline_points + 7),
(
offset_airfoil[p6[1]:p7[1]]
)),
((spline_points + 7, spline_points + 4),
(
[i for i in [offset_airfoil[p7[1]:] + offset_airfoil[:p4[1]]]][0]
)),
((spline_points, spline_points + 4),
(
arcs[0][cp3:p4[2]]
)),
((spline_points + 4, spline_points + 8),
(
arcs[0][p4[2]:]
)),
((spline_points + 1, spline_points + 5),
(
arcs[1][cp4:p5[2]]
)),
((spline_points + 5, spline_points + 9),
(
arcs[1][p5[2]:]
)),
((spline_points + 2, spline_points + 6),
(
arcs[2][cp5:p6[2]]
)),
((spline_points + 6, spline_points + 10),
(
arcs[2][p6[2]:]
)),
((spline_points + 3, spline_points + 7),
(
arcs[3][cp6:p7[2]]
)),
((spline_points + 7, spline_points + 11),
(
arcs[3][p7[2]:]
)),
# ((spline_points + 8, spline_points + 11),
# (
# arc4
# )),
# ((spline_points + 9, spline_points + 10),
# (
# arc5
# ))
)
)
spline_points += inc
arcss.append([(arc_points + 8, arc_points + 11), (0, radial_position, pt[8][2])])
arcss.append([(arc_points + 9, arc_points + 10), (0, radial_position, pt[9][2])])
if not (round(pt[9][0], 3), round(pt[9][1], 3)) == (round(p13[0], 3), round(p13[1], 3)):
tmp_center = arc2points((0, 0), (pt[9][0], pt[9][1]), p13[:2])[25].tolist()
arcss.append([(arc_points + 9, arc_points + 13), (tmp_center[0], tmp_center[1], pt[9][2])])
if not (round(pt[8][0], 3), round(pt[9][1], 3)) == (round(p12[0], 3), round(p12[1], 3)):
tmp_center = arc2points((0, 0), (pt[8][0], pt[9][1]), p12[:2])[25].tolist()
arcss.append([(arc_points + 8, arc_points + 12), (tmp_center[0], tmp_center[1], pt[8][2])])
#
if not (round(pt[10][0], 3), round(pt[10][1], 3)) == (round(p14[0], 3), round(p14[1], 3)):
tmp_center = arc2points((0, 0), (pt[10][0], pt[10][1]), p14[:2])[25].tolist()
arcss.append([(arc_points + 10, arc_points + 14), (tmp_center[0], tmp_center[1], pt[9][2])])
if not (round(pt[11][0], 3), round(pt[11][1], 3)) == (round(p15[0], 3), round(p15[1], 3)):
tmp_center = arc2points((0, 0), (pt[11][0], pt[11][1]), p15[:2])[25].tolist()
arcss.append([(arc_points + 11, arc_points + 15), (tmp_center[0], tmp_center[1], pt[8][2])])
arc_points += inc
blade_face.append([f0, f1, f2, f3])
in_face.append([f4, f5, f6])
out_face.append([f7, f8, f9])
periodic_face_1.append(f10)
periodic_face_2.append(f11)
f0 = f0 + inc
f1 = f1 + inc
f2 = f2 + inc
f3 = f3 + inc
f4 = f4 + inc
f5 = f5 + inc
f6 = f6 + inc
f7 = f7 + inc
f8 = f8 + inc
f9 = f9 + inc
f10 = f10 + inc
f11 = f11 + inc
ggi = [j for i in [[j.tolist() for i in in_face[:-1] for j in i], # entrada
[j.tolist() for i in out_face[:-1] for j in i], # salida
[i[0][0:4].tolist() for i in blocks[-1]]] # parte de arriba
for j in i]
faces = [
(('Blade'), ('wall'),
(
hub_face.tolist() + [j.tolist() for i in blade_face[:-1] for j in i]
)
),
(('Blade_GGI'), ('patch'),
(
ggi,
)
),
(('Blade_Periodic_1'), ('cyclicGgi'),
(
periodic_face_1[:-1]
)
),
(('Blade_Periodic_2'), ('cyclicGgi'),
(
periodic_face_2[:-1]
)
),
]
data = {
'convertToMeters': self.convert_to_meters,
'vertices': [j for i in ptos for j in i],
'arcs': arcss,
'splines': [j for i in splines for j in i],
'blocks': [j for i in blocks[:-1] for j in i],
'faces': faces
}
# renderTemplate('blockMeshDict.jinja', self.study.dirs['polyMesh'] + 'blockMeshDict_pala', data)