-
Notifications
You must be signed in to change notification settings - Fork 5
/
io_polymesh.py
1037 lines (825 loc) · 32.7 KB
/
io_polymesh.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# Input/Output routines for OpenFOAM PolyMesh unstructured grids.
# More information about PolyMesh:
# https://cfd.direct/openfoam/user-guide/mesh-description/
# Note: Unstructured Grids assumes that PolyMesh face definitions are
# ordered in such a way that they first define internal faces, and
# then boundary faces (each boundary patch at a time). This is normal
# way for OpenFOAM meshes, since it allows easy definition of boundary
# patches by startFace and nFaces. So, even though PolyMesh
# definition allows definition of cell label "-1" for faces, it is
# never encountered in a well-ordered PolyMesh.
# Note: OpenFOAM includes renumberMesh utility, which orders cells and
# optimizes bandwidth. It is recommended to run renumberMesh to
# optimize PolyMesh.
# Note: Face normals for all boundary faces must always point from
# domain towards outside. Therefore neighbour list is always shorter
# than owner list.
# Note: Neighbour face list can contain a cell with larger cell index
# than largest cell index in owner face list. This can happen e.g. for
# meshes created with SnappyHexMesh when a cell is located inside the
# mesh refinement region. Cell traversal path can form an "inward
# spiral" structure, in which case the cell in spiral center is
# defined completely by neighbour cells (all face normals of that cell
# point inwards. If domain cell traversal ends with such a spiral,
# then neighbour list ends having a larger cell index than owner list.
import bpy
from bpy_extras.io_utils import (
ImportHelper,
ExportHelper,
orientation_helper,
axis_conversion,
)
from . import ug
from . import ug_op
import logging
l = logging.getLogger(__name__)
# Global variables
print_interval = 100000 # Debug print progress interval
##### IMPORT #####
class UG_OT_ImportPolyMesh(bpy.types.Operator, ImportHelper):
'''Import OpenFOAM PolyMesh Files into Blender as UG Data'''
bl_idname = "unstructured_grids.import_openfoam_polymesh"
bl_label = "Import OpenFOAM PolyMesh (UG)"
@classmethod
def poll(cls, context):
return context.mode in {'OBJECT','EDIT_MESH'}
def execute(self, context):
read_polymesh_files(self)
return {'FINISHED'}
def read_polymesh_files(self):
'''Reads PolyMesh files' contents from a folder into strings'''
import os
ug_props = bpy.context.scene.ug_props
dirpath = os.path.dirname(self.filepath)
# Compulsory files
filenames = ['boundary', 'faces', 'neighbour', 'owner', 'points']
rval = read_in_files(True, dirpath, filenames)
if rval:
self.report({'ERROR'}, "Error: " + rval)
return None
# Optional files
filenames = ['cellZones', 'faceZones']
rval = read_in_files(False, dirpath, filenames)
polymesh_boundary_ingroup_fix()
polymesh_to_ugdata(self)
return None
def read_in_files(is_required, dirpath, filenames):
'''Read in file contents of filenames (gzip compressed or uncompressed
ASCII files) in dirpath. Return error message if is_required is
True and file does not exist.
'''
import os
import gzip
ug_props = bpy.context.scene.ug_props
for f in filenames:
varname = "text_" + f
filepath = os.path.join(dirpath, f)
filepathgz = os.path.join(dirpath, f + ".gz")
if os.path.isfile(filepathgz) and os.path.isfile(filepath):
error = "Both compressed and uncompressed file exist for " \
"%r" % filepath
l.debug(error)
return error
if os.path.isfile(filepathgz):
l.debug("Reading in as string: %s" % filepathgz)
with gzip.open(filepathgz, 'r') as infile:
# Assume UTF-8 decoding for converting bytes to string
setattr(ug_props, varname, infile.read().decode("utf-8"))
else:
l.debug("Reading in as string: %s" % filepath)
if not (os.path.isfile(filepath)):
error = "Could not find %r" % filepath
l.debug(error)
if is_required:
return error
else:
with open(filepath, 'r') as infile:
setattr(ug_props, varname, infile.read())
def polymesh_boundary_ingroup_fix():
'''Reformats ingroup entries spanning several lines into one line,
because otherwise multiline entry breaks regex matching logic
in polymesh_get_boundary
'''
import re
ug_props = bpy.context.scene.ug_props
text = ug_props.text_boundary
inside = False # boolean for marking boundary entries in text
result = ''
rec1 = re.compile(r'^\s*inGroups\s*$', re.M)
rec2 = re.compile(r'\;', re.M)
for line in text.splitlines():
regex = rec1.search(line)
if regex:
# Initialize
inside = True
res = " inGroups "
elif inside:
res += line + ' '
regex = rec2.search(line)
if regex:
# Reached end of inGroup
inside = False
result += res + '\n'
else:
result += line + '\n'
ug_props.text_boundary = result
return None
class UG_OT_PolyMeshToUG(bpy.types.Operator):
'''(Re)generate UG Data from PolyMesh Data in UG Storage'''
bl_idname = "unstructured_grids.polymesh_to_ug"
bl_label = "(Re)generate UG Data from UG Storage"
@classmethod
def poll(cls, context):
ug_props = bpy.context.scene.ug_props
return context.mode in {'OBJECT','EDIT_MESH'} and len(ug_props.text_points) > 0
def execute(self, context):
polymesh_to_ugdata(self)
return {'FINISHED'}
def polymesh_to_ugdata(self):
'''Convert OpenFOAM polyMesh data from text files
into UG data structures and Blender mesh
'''
ug_props = bpy.context.scene.ug_props
if len(ug_props.text_points) == 0:
return None
ug.hide_other_objects()
ob = ug.initialize_ug_object()
verts = polymesh_get_verts(ug_props.text_points)
[edges, faces] = polymesh_get_faces( \
ug_props.text_owner, ug_props.text_neighbour, ug_props.text_faces)
polymesh_get_boundary(ug_props.text_boundary)
# Create vertices and faces into mesh object
ob.data.from_pydata(verts, edges, faces)
ob.data.validate()
# Boundary patches
apply_materials_to_boundaries(ob)
ug.update_ugboundaries()
# Cell and face zones
polymesh_get_zone('cell', ug_props.text_cellZones)
polymesh_get_zone('face', ug_props.text_faceZones)
apply_vertex_groups_to_zones(ob)
ug.update_ugzones() # not necessary after import, but yes after regenerate
bpy.ops.object.mode_set(mode='OBJECT')
def polymesh_get_verts(text):
'''Creates list of vertex triplets from PolyMesh points text string'''
import re
verts = [] # list of x, y, z point coordinate triplets
rec = re.compile(r'^\(([dDeE\d\.\-]+)\s+([dDeE\d\.\-]+)\s+([dDeE\d\.\-]+)\)', re.M)
i = 0
for line in text.splitlines():
regex = rec.search(line)
if regex:
x = float(regex.group(1))
y = float(regex.group(2))
z = float(regex.group(3))
verts.append(tuple([x, y, z]))
ug.UGVertex()
if i % print_interval == 0:
l.debug("... processed vertex count: %d" % i)
i += 1
l.debug("Number of vertices read: %d" % len(verts))
return verts
def polymesh_get_faces(text_owner, text_neighbour, text_faces):
'''Creates edge and face list from PolyMesh owner, neighbour and
faces text blocks. Initializes UG faces and UG cells.
'''
# TODO: Separate initialization? Function is getting big.
edges = [] # List of edge vertex index pairs, to be generated
faces = [] # List of face vertex index lists, to be generated
gen_edges = bpy.context.scene.ug_props.generate_internal_edges
# Read in owner and neighbour lists
owner = polymesh_get_intlist(text_owner)
l.debug("Number of faces that have owner: %d" % len(owner))
neighbour = polymesh_get_intlist(text_neighbour)
l.debug("Number of faces that have neighbour: %d" % len(neighbour))
l.debug("Number of owner minus neighbour: %d" % (len(owner) - len(neighbour)))
face_verts = polymesh_get_list_intlist(text_faces)
l.debug("Number of face definitions: %d" % len(face_verts))
# Populate list of ugcells
if len(neighbour) > 0:
nCells = max(max(owner), max(neighbour)) + 1
else:
nCells = 1 # Special case for one cell mesh
for i in range(nCells):
# Add new UGCell
ug.UGCell()
if i % print_interval == 0:
l.debug("... processed cell count: %d" % i)
l.debug("Final cell count: %d" % i)
# Create faces at boundary and only edges for internal faces
for i in range(len(face_verts)):
# Add ugface and facemap entry
ugf = ug.UGFace(face_verts[i])
# Part 1. Process owner
c = ug.ugcells[owner[i]]
# Set owner cell
ugf.owner = c
# Add face to owner's ugfaces list
c.add_face_and_verts(ugf)
# Set UGVertex info in place for vertices
for v in face_verts[i]:
ugv = ug.ugverts[v]
# Add owner cell info for vertices
ugv.add_cell(c)
# Add face to ugverts' face list
ugv.add_face(ugf)
# Part 2. Process neighbour
if i < len(neighbour):
c = ug.ugcells[neighbour[i]]
# Add neighbour cell index
ugf.neighbour = c
# Add face to neighbour's ugfaces list
c.add_face_and_verts(ugf)
for v in face_verts[i]:
ugv = ug.ugverts[v]
# Add neighbour cell info for vertices
ugv.add_cell(c)
# Add edges for geometry generation if needed
if gen_edges:
for j in range(len(face_verts[i])):
edges.append(tuple([face_verts[i][j-1], face_verts[i][j]]))
else:
# Boundary face, add index and create face geometry for mesh object
ugf.add_mesh_face(len(faces))
faces.append(tuple(face_verts[i]))
if i % print_interval == 0:
l.debug("... processed face count: %d" % i)
l.debug("Final face count: %d" % i)
l.debug("Number of edge definitions: %d" % len(edges))
l.debug("Number of face definitions: %d" % len(faces))
return edges, faces
def polymesh_get_intlist(text):
'''Creates integer list from argument PolyMesh integer text block'''
import re
iList = [] # list of integers to be generated
inside = False # boolean for marking integer list in text
rec1 = re.compile(r'^\(', re.M)
rec2 = re.compile(r'^\)', re.M)
rec3 = re.compile(r'^(\d+)', re.M)
for line in text.splitlines():
# Opening of integer list by single parenthesis
regex = rec1.search(line)
if regex:
inside = True
# Closing of integer list by single parenthesis
regex2 = rec2.search(line)
if regex2:
inside = False
# Integer, at start of line
regex3 = rec3.search(line)
if inside and regex3:
iList.append(int(regex3.group(1)))
return iList
def polymesh_get_list_intlist(text):
'''Creates list of integer lists from argument PolyMesh
text block
'''
# TODO: Get rid of code duplication
import re
iList = [] # list of integers lists to be generated
inside = False # boolean for marking integer list in text
rec1 = re.compile(r'^\(', re.M)
rec2 = re.compile(r'^\)', re.M)
rec3 = re.compile(r'^\d+\(([\d\s]+)\)', re.M)
for line in text.splitlines():
# Opening of integer list by single parenthesis
regex = rec1.search(line)
if regex:
inside = True
# Closing of integer list by single parenthesis
regex2 = rec2.search(line)
if regex2:
inside = False
# List of integer list within parenthesis
regex3 = rec3.search(line)
if inside and regex3:
vals = regex3.group(1).split()
valList = []
for val in vals:
valList.append(int(val))
iList.append(valList)
return iList
def polymesh_get_boundary(text):
'''Creates boundary objects from PolyMesh boundary text string'''
import re
inside = False # boolean for marking boundary entries in text
rec1 = re.compile(r'^\(', re.M)
rec2 = re.compile(r'^\)', re.M)
rec3 = re.compile(r'^\s+([\w\%\:\-\.]+)$', re.M)
rec4 = re.compile(r'^\s+type\s+(\w+)\;$', re.M)
rec5 = re.compile(r'^\s+inGroups\s+([\w\s\(\)\<\>]+)\;\s*$', re.M)
rec6 = re.compile(r'^\s+nFaces\s+(\d+)\;$', re.M)
rec7 = re.compile(r'^\s+startFace\s+(\d+)\;$', re.M)
for line in text.splitlines():
# Opening of integer list by single parenthesis
regex = rec1.search(line)
if regex:
inside = True
# Closing of integer list by single parenthesis
regex = rec2.search(line)
if regex:
inside = False
if patch.nFaces == 0 or patch.startFace == -1:
l.error("Boundary definition " + str(patch.patchname) \
+ " is broken")
return None
# Add ugfaces to boundary
for i in range(patch.startFace, patch.startFace + patch.nFaces):
patch.ugfaces.append(ug.ugfaces[i])
if not inside:
continue
# New entry is a word (with possibly special characters) on its own line
regex = rec3.search(line)
if regex:
patchname = str(regex.group(1))
l.debug("Reading in boundary patch %d: %s" % (len(ug.ugboundaries), patchname))
patch = ug.UGBoundary(patchname)
continue
# type
regex = rec4.search(line)
if regex:
patch.typename = str(regex.group(1))
continue
# inGroups
regex = rec5.search(line)
if regex:
patch.inGroups = str(regex.group(1))
continue
# nFaces
regex = rec6.search(line)
if regex:
patch.nFaces = int(regex.group(1))
continue
# startFace
regex = rec7.search(line)
if regex:
patch.startFace = int(regex.group(1))
continue
return None
def polymesh_get_zone(zonetype, text):
'''Creates zone objects from PolyMesh zone text string for type
(either face or cell)
'''
import re
inside1 = False # boolean for marking top level entries in text
inside2 = False # boolean for marking bottom level entries in text
label = '' # label definition preceding list of integers
iList = [] # list of integers inside bottom level entry
zone = None # UGZone
rec1 = re.compile(r'^\s*\(', re.M)
rec2 = re.compile(r'^\s*\)', re.M)
rec3 = re.compile(r'^\s*([a-zA-Z][\w\%\:\-\.]*)$', re.M)
rec4 = re.compile(r'^\s*(\w+\s+[\w\<\>]+)\s*$', re.M)
rec5 = re.compile(r'^(\d+)', re.M)
for line in text.splitlines():
# Opening parenthesis
regex = rec1.search(line)
if regex:
if inside1:
inside2 = True
else:
inside1 = True
# Closing parenthesis
regex = rec2.search(line)
if regex:
if inside2:
# Add indices to correct UGZone list
if label.startswith('cellLabels'):
for i in iList:
zone.ugcells.append(ug.ugcells[i])
l.debug("CellZone: " + str(zonename) + ": %d cells" % len(iList))
elif label.startswith('faceLabels'):
for i in iList:
zone.ugfaces.append(ug.ugfaces[i])
l.debug("FaceZone: " + str(zonename) + ": %d faces" % len(iList))
elif label.startswith('flipMap'):
for i in iList:
zone.flipMap.append(i)
l.debug("Face FlipMap: " + str(zonename) + ": %d faces" % len(iList))
else:
l.error("Unknown label " + str(label))
return None
# Reinitialize
iList = []
inside2 = False
label = ''
else:
inside1 = False
if not inside1:
continue
if not inside2:
# New zone is a single word (with possibly special characters) on its own line
regex = rec3.search(line)
if regex:
zonename = str(regex.group(1))
l.debug("Reading in zone [%d]" % len(ug.ugzones))
zone = ug.UGZone(zonetype, zonename)
# Get label definition line
else:
regex = rec4.search(line)
if regex:
label = str(regex.group(1))
continue
# Integer, at start of line
regex = rec5.search(line)
if regex:
iList.append(int(regex.group(1)))
return None
def apply_materials_to_boundaries(ob):
'''Sets materials to faces in object ob according to boundary assignments'''
mati = 0 # Material index
facecount = 0
# Clear object's material slots - not needed but this should do it:
# for i in range(len(ob.material_slots)):
# ob.active_material_index = i
# bpy.ops.object.material_slot_select()
# bpy.ops.object.material_slot_remove()
# Process each boundary
for b in ug.ugboundaries:
# Create new material if needed
l.debug("Material for %s: %d" % (b.patchname, mati))
if b.patchname in bpy.data.materials:
mat = bpy.data.materials[b.patchname]
else:
mat = bpy.data.materials.new(name=b.patchname)
# Create new material slot to object and set material
bpy.ops.object.material_slot_add()
ob.active_material = mat
# Color is given to all patches but not the one named
# 'default', because that is used for new boundary faces. User
# probably wants to assign manually those, so we leave them
# uncolored to help spot them.
if b.patchname != 'default':
ob.data.materials[mati].diffuse_color = get_face_color(mati)
# Set material index for mesh faces
for i in range(b.nFaces):
ob.data.polygons[facecount].material_index = mati
facecount += 1
mati += 1
def get_face_color(mati):
'''Gives a color to argument material number'''
base_colors = [(0,0,1,1), (1,0,0,1), (0,1,0,1), \
(0.7,0.7,0,1), (0,0.7,0.7,1), (0.7,0,0.7,1)]
if mati < len(base_colors):
return base_colors[mati]
# Get random colors after base colors
import random
random.seed(10043 + mati)
[r, g, b] = [random.random() for i in range(3)]
return [r, g, b, 1.0]
def apply_vertex_groups_to_zones(ob):
'''Set/create vertex groups for object ob according to cell and face
zones data in ugzones
'''
def assign_to_new_vertex_group(ob, z):
'''Assigns currently selected vertices to new vertex group in object
ob for zone z
'''
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.object.vertex_group_assign_new()
# Set vertex group name
vgname = z.zonetype + "Zone_" + z.zonename
vg = ob.vertex_groups[-1]
vg.name = vgname
bpy.ops.object.mode_set(mode="OBJECT")
mode = ob.mode # Save original mode
for z in ug.ugzones:
# Deselect all vertices, edges and faces
bpy.ops.object.mode_set(mode="EDIT")
bpy.ops.mesh.select_all(action='DESELECT')
bpy.ops.object.mode_set(mode="OBJECT")
if z.zonetype == 'cell':
n = ug_op.select_vertices_from_ugcells(ob, z.ugcells)
if n > 0:
assign_to_new_vertex_group(ob, z)
elif z.zonetype == 'face':
n = ug_op.select_vertices_from_ugfaces(ob, z.ugfaces)
if n > 0:
assign_to_new_vertex_group(ob, z)
bpy.ops.object.mode_set(mode=mode)
##################
##### EXPORT #####
##################
class UG_OT_ExportPolyMesh(bpy.types.Operator, ExportHelper):
'''Export UG Data as OpenFOAM PolyMesh Files'''
bl_idname = "unstructured_grids.export_openfoam_polymesh"
bl_label = "Export OpenFOAM PolyMesh (UG)"
filename_ext = ".polyMesh" # Dummy, required by ExportHelper
@classmethod
def poll(cls, context):
return context.mode in {'OBJECT','EDIT_MESH'} and ug.exists_ug_state()
def execute(self, context):
ug.update_ug_all_from_blender(self)
write_polymesh_files(self)
return {'FINISHED'}
def ugdata_to_polymesh():
'''Convert up-to-date UG data into PolyMesh text data strings
and store those in ug_props.text_* string properties
'''
ob = bpy.data.objects[ug.obname]
update_text_points(ob)
owneri, neighbouri = update_ei_and_text_faces(ob)
update_text_owner_neighbour(owneri, neighbouri)
update_text_boundary()
update_text_cell_zones()
update_text_face_zones()
return None
def update_text_points(ob):
'''Updates PolyMesh points string contents from Blender object vertices'''
text_verts = '' # Text for vertex coordinates
n = 0
for ugv in ug.ugverts:
if ugv.deleted:
ugv.ei = -1
continue
# Update export index
ugv.ei = n
v = ob.data.vertices[ugv.bi]
text_verts += "(" + "%.6g" % v.co.x + " " \
+ "%.6g" % v.co.y + " " \
+ "%.6g" % v.co.z + ")\n"
n += 1
# Generate new text
text = of_file_header('vectorField', 'points') + "\n"
text += str(n) + "\n(\n"
text += text_verts + ")\n"
bpy.context.scene.ug_props.text_points = text
l.debug("text_points updated points: %d" % len(ob.data.vertices))
return None
def update_ei_and_text_faces(ob):
'''Updates export indices (ei) of ugcells, ugfaces and ugboundaries
and generates PolyMesh faces text string contents for object ob.
Exported face index is updated according to ugboundary ugfaces.
Update is done in two phases:
1. internal face pass:
Generates export cell indices according to PolyMesh requirement that
face normal points from lower cell index to higher cell index.
Face normal is determined by right hand rule from vertex list.
2. boundary face pass:
Boundar faces are numbered according to boundary assignments.
New owner and neighbour index lists are generated alongside face
indexing and returned.
'''
def gen_line(ugverts):
'''Construct face definition text line from vertex indices list'''
line = str(len(ugverts)) + "("
for j in range(len(ugverts) - 1):
line += str(ugverts[j].ei) + " "
line += str(ugverts[-1].ei) + ")\n"
return line
def reset_ei():
'''Resets export indices'''
for c in ug.ugcells:
c.ei = -1
for ugf in ug.ugfaces:
ugf.ei = -1
def internal_face_pass(clist):
'''Generate face definition text for internal faces from
argument cell list clist.
'''
text = ''
fei = 0 # face export index
cei = 0 # cell export index
export_ugfaces = [] # list of ugfaces in export order
owneri = [] # owner cell index list
neighbouri = [] # neighbour cell index list
# Go through each internal face of cells and number cells and
# internal faces
for c in clist:
if c.deleted:
continue
c.ei = cei # Set cell index
for ugf in c.ugfaces:
if ugf.deleted:
continue
if not ugf.neighbour:
continue
if ugf.owner == c and ugf.neighbour.ei != -1:
# This leads to "Faces not in upper triangular order" errors
l.warning("Face normal should be mirrored for face ei %d" % ugf.ei)
if ugf.ei != -1:
continue
export_ugfaces.append(ugf)
ugf.ei = fei # Set face index
text += gen_line(ugf.ugverts) # Add definition line and proceed
fei += 1
cei += 1
# Update owner and neighbour index lists
for ugf in export_ugfaces:
owneri.append(ugf.owner.ei)
neighbouri.append(ugf.neighbour.ei)
return text, fei, owneri, neighbouri
def boundary_face_pass(fei, ob, owneri):
'''Generate face definition text for boundary faces.
Return text and number of internal faces.
'''
startind = fei # Face index for start of boundary faces
text = ''
for patch in ug.ugboundaries:
if patch.deleted:
continue
# Update boundary index numbers
patch.startFace = fei
for ugf in patch.ugfaces:
# Sanity check
if ugf.ei != -1:
l.error("Face export index already exists for face %d" % ugf.bi)
return None
ugf.ei = fei # Set face index
owneri.append(ugf.owner.ei) # Append owner index
text += gen_line(ugf.ugverts)
fei += 1
return text, fei, owneri
# Ordering of cells and initialization
#ordered_ugcells = ug.order_ugcells_by_internal_face_search() # Very slow
#ordered_ugcells = ug.order_ugcells_by_BFS() # Somewhat slow
ordered_ugcells = ug.ugcells # No ordering of cells, no guarantee of single region
reset_ei()
# Internal pass
text_internal, i, owneri, neighbouri = internal_face_pass(ordered_ugcells)
l.debug("text_faces updated internal faces: %d", i)
# Boundary pass
text_boundary, i, owneri = boundary_face_pass(i, ob, owneri)
l.debug("text_faces updated total number of faces: %d", i)
# Generate text string
text = of_file_header('faceList', 'faces') + "\n"
text += str(i) + "\n(\n"
text += text_internal + text_boundary + ")\n"
bpy.context.scene.ug_props.text_faces = text
return owneri, neighbouri
def update_text_owner_neighbour(owneri, neighbouri):
'''Updates PolyMesh owner and neighbour text string contents from
argument integer lists.
'''
nall = len(owneri)
nneighbour = len(neighbouri)
# Generate text string
text_owner = of_file_header('labelList', 'owner') + "\n"
text_owner += str(nall) + "\n(\n"
text_neighbour = of_file_header('labelList', 'neighbour') + "\n"
text_neighbour += str(nneighbour) + "\n(\n"
for i in owneri:
text_owner += str(i) + "\n"
for i in neighbouri:
text_neighbour += str(i) + "\n"
text_owner += ")\n"
text_neighbour += ")\n"
bpy.context.scene.ug_props.text_owner = text_owner
bpy.context.scene.ug_props.text_neighbour = text_neighbour
l.debug("text_owner updated faces: %d" % nall)
l.debug("text_neighbour updated faces: %d" % nneighbour)
return None
def update_text_boundary():
'''Updates PolyMesh boundary text string contents from UG data'''
btext = '' # generated boundary entries
nboundaries = 0 # number of boundaries
for patch in ug.ugboundaries:
if patch.deleted:
continue
text = " " + patch.patchname + "\n {\n"
text += " type " + patch.typename + ";\n"
if patch.inGroups != '':
text += " inGroups " + patch.inGroups + ";\n"
text += " nFaces " + str(patch.nFaces) + ";\n"
text += " startFace " + str(patch.startFace) + ";\n"
text += " }\n"
btext += text
nboundaries += 1
# Generate new text
text = of_file_header('polyBoundaryMesh', 'boundary') + "\n"
text += str(nboundaries) + "\n(\n"
text += btext + ")\n"
bpy.context.scene.ug_props.text_boundary = text
l.debug("text_boundary updated patches: %d" % nboundaries)
return None
def update_text_cell_zones():
'''Update PolyMesh cellZones text string contents from UG data'''
ztext = '' # generated zone entries
nzones = 0 # number of cell zones
# Zone texts
for z in ug.ugzones:
if z.zonetype != 'cell':
continue
if z.deleted:
continue
l.debug("Updating text for %s zone %r" % (z.zonetype, z.zonename))
text = z.zonename + "\n"
text += "{\n"
text += "type " + z.zonetype + "Zone;\n"
text += "cellLabels List<label>\n"
text += str(len(z.ugcells)) + "\n"
text += "(\n"
for c in z.ugcells:
text += str(c.ei) + "\n"
text += ");\n}\n\n"
ztext += text
nzones +=1
# Generate new text
if nzones == 0:
bpy.context.scene.ug_props.text_cellZones = ''
l.debug("text_cellZones is empty (no cell zones defined)")
return None
text = of_file_header('regIOobject', 'cellZones') + "\n"
text += str(nzones) + "\n(\n"
text += ztext + ")\n"
bpy.context.scene.ug_props.text_cellZones = text
l.debug("updated cell zones: %d" % nzones)
return None
def update_text_face_zones():
'''Update PolyMesh faceZones text string contents from UG data'''
ztext = '' # generated zone entries
nzones = 0 # number of face zones
# Zone texts
for z in ug.ugzones:
if z.zonetype != 'face':
continue
if z.deleted:
continue
l.debug("Updating text for %s zone %r" % (z.zonetype, z.zonename))
text = z.zonename + "\n"
text += "{\n"
text += "type " + z.zonetype + "Zone;\n"
# Labels
text += "faceLabels List<label>\n"
text += str(len(z.ugfaces)) + "\n"
text += "(\n"
for ugf in z.ugfaces:
text += str(ugf.ei) + "\n"
text += ");\n"
# flipMap. Zero is used if no value is specified.
text += "flipMap List<bool>\n"
text += str(len(z.ugfaces)) + "\n"
text += "(\n"
numFlipMap = len(z.flipMap)
for i in range(len(z.ugfaces)):
if i >= numFlipMap:
text += str(0) + "\n"
else:
text += str(z.flipMap[i]) + "\n"
text += ");\n}\n\n"
ztext += text
nzones +=1
# Generate new text
if nzones == 0:
bpy.context.scene.ug_props.text_faceZones = ''
l.debug("text_faceZones is empty (no face zones defined)")
return None
text = of_file_header('regIOobject', 'faceZones') + "\n"
text += str(nzones) + "\n(\n"
text += ztext + ")\n"
bpy.context.scene.ug_props.text_faceZones = text
l.debug("updated face zones: %d" % nzones)
return None
def write_polymesh_files(self):
'''Write contents of data strings into PolyMesh files'''
import os
ug_props = bpy.context.scene.ug_props
dirpath = os.path.dirname(self.filepath)