/
geometry.py
1042 lines (923 loc) · 35.6 KB
/
geometry.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
# -*- coding: utf-8 -*-
#
import warnings
import numpy
from ..__about__ import __version__
from ..helpers import _is_string, get_gmsh_major_version
from .bspline import Bspline
from .circle_arc import CircleArc
from .compound_line import CompoundLine
from .compound_surface import CompoundSurface
from .compound_volume import CompoundVolume
from .define_constant import DefineConstant
from .dummy import Dummy
from .ellipse_arc import EllipseArc
from .line import Line
from .line_base import LineBase
from .line_loop import LineLoop
from .plane_surface import PlaneSurface
from .point import Point
from .point_base import PointBase
from .spline import Spline
from .surface import Surface
from .surface_base import SurfaceBase
from .surface_loop import SurfaceLoop
from .volume import Volume
from .volume_base import VolumeBase
class Geometry(object):
def __init__(self, gmsh_major_version=None):
self._EXTRUDE_ID = 0
self._BOOLEAN_ID = 0
self._ARRAY_ID = 0
self._FIELD_ID = 0
self._GMSH_MAJOR = gmsh_major_version
self._TAKEN_PHYSICALGROUP_IDS = []
self._GMSH_CODE = [
"// This code was created by pygmsh v{}.".format(__version__)
]
return
def _gmsh_major(self):
if self._GMSH_MAJOR is None:
self._GMSH_MAJOR = get_gmsh_major_version()
return self._GMSH_MAJOR
def get_code(self):
"""Returns properly formatted Gmsh code.
"""
return "\n".join(self._GMSH_CODE)
# All of the add_* method below could be replaced by
#
# def add(self, entity):
# self._GMSH_CODE.append(entity.code)
# return entity
#
# to be used like
#
# geom.add(pg.Circle(...))
#
# However, this would break backwards compatibility and perhaps encourage
# users to do
#
# c = pg.Circle(...)
# # ... use c
#
# in which case the circle code never gets added to geom.
def add_bspline(self, *args, **kwargs):
p = Bspline(*args, **kwargs)
self._GMSH_CODE.append(p.code)
return p
def add_circle_arc(self, *args, **kwargs):
p = CircleArc(*args, **kwargs)
self._GMSH_CODE.append(p.code)
return p
def add_compound_line(self, *args, **kwargs):
assert self._gmsh_major() == 3
e = CompoundLine(*args, **kwargs)
self._GMSH_CODE.append(e.code)
return e
def add_compound_surface(self, *args, **kwargs):
assert self._gmsh_major() == 3
e = CompoundSurface(*args, **kwargs)
self._GMSH_CODE.append(e.code)
return e
def add_compound_volume(self, *args, **kwargs):
assert self._gmsh_major() == 3
e = CompoundVolume(*args, **kwargs)
self._GMSH_CODE.append(e.code)
return e
def add_ellipse_arc(self, *args, **kwargs):
p = EllipseArc(*args, **kwargs)
self._GMSH_CODE.append(p.code)
return p
def add_line(self, *args, **kwargs):
p = Line(*args, **kwargs)
self._GMSH_CODE.append(p.code)
return p
def add_line_loop(self, *args, **kwargs):
p = LineLoop(*args, **kwargs)
self._GMSH_CODE.append(p.code)
return p
def add_plane_surface(self, *args, **kwargs):
p = PlaneSurface(*args, **kwargs)
self._GMSH_CODE.append(p.code)
return p
def add_point(self, *args, **kwargs):
p = Point(*args, **kwargs)
self._GMSH_CODE.append(p.code)
return p
def add_spline(self, *args, **kwargs):
p = Spline(*args, **kwargs)
self._GMSH_CODE.append(p.code)
return p
def add_surface(self, *args, **kwargs):
s = Surface(*args, api_level=self._gmsh_major(), **kwargs)
self._GMSH_CODE.append(s.code)
return s
def add_surface_loop(self, *args, **kwargs):
e = SurfaceLoop(*args, **kwargs)
self._GMSH_CODE.append(e.code)
return e
def add_volume(self, *args, **kwargs):
e = Volume(*args, **kwargs)
self._GMSH_CODE.append(e.code)
return e
def define_constant(self, *args, **kwargs):
e = DefineConstant(*args, **kwargs)
self._GMSH_CODE.append(e.code)
return e
def _new_physical_group(self, label=None):
# See
# https://github.com/nschloe/pygmsh/issues/46#issuecomment-286684321
# for context.
max_id = (
0
if not self._TAKEN_PHYSICALGROUP_IDS
else max(self._TAKEN_PHYSICALGROUP_IDS)
)
if label is None:
label = max_id + 1
if isinstance(label, int):
assert (
label not in self._TAKEN_PHYSICALGROUP_IDS
), "Physical group label {} already taken.".format(label)
self._TAKEN_PHYSICALGROUP_IDS += [label]
return str(label)
assert _is_string(label)
self._TAKEN_PHYSICALGROUP_IDS += [max_id + 1]
return '"{}"'.format(label)
def add_physical(self, entities, label=None):
if not isinstance(entities, list):
entities = [entities]
d = {0: "Point", 1: "Line", 2: "Surface", 3: "Volume"}
tpe = d[entities[0].dimension]
for e in entities:
assert isinstance(
e,
(
Point,
LineBase,
Surface,
PlaneSurface,
SurfaceBase,
Volume,
VolumeBase,
),
), "Can add physical groups only for Points, Lines, Surfaces, Volumes, not {}.".format(
type(e)
)
assert d[e.dimension] == tpe
label = self._new_physical_group(label)
self._GMSH_CODE.append(
"Physical {}({}) = {{{}}};".format(
tpe, label, ", ".join([e.id for e in entities])
)
)
return
def add_physical_point(self, points, label=None):
warnings.warn("add_physical_point() is deprecated. use add_physical() instead.")
self.add_physical(points, label=label)
return
def add_physical_line(self, lines, label=None):
warnings.warn("add_physical_line() is deprecated. use add_physical() instead.")
self.add_physical(lines, label=label)
return
def add_physical_surface(self, surfaces, label=None):
warnings.warn(
"add_physical_surface() is deprecated. use add_physical() instead."
)
self.add_physical(surfaces, label=label)
return
def add_physical_volume(self, volumes, label=None):
warnings.warn(
"add_physical_volume() is deprecated. use add_physical() instead."
)
self.add_physical(volumes, label=label)
return
def set_transfinite_lines(self, lines, size, progression=None, bump=None):
code = "Transfinite Line {{{0}}} = {1}".format(
", ".join([l.id for l in lines]), size
)
if progression is not None and bump is not None:
raise ValueError("only one optional argument possible", progression, bump)
elif progression is not None:
code += " Using Progression " + str(progression)
elif bump is not None:
code += " Using Bump " + str(bump)
self._GMSH_CODE.append(code + ";")
return
def set_transfinite_surface(self, surface, size=None, orientation=None):
assert surface.num_edges == 4, "a transfinite surface can only have 4 sides"
# size is not mandatory because in general a user can create it's own
# transfinite lines and then just tell gmsh that the surface is
# transfinite too
if size is not None:
assert isinstance(
surface, (PlaneSurface, Surface, self.Polygon)
), "we can create transfinite lines only if we have a line loop"
self.set_transfinite_lines(
[surface.line_loop.lines[0], surface.line_loop.lines[2]], size[0]
)
self.set_transfinite_lines(
[surface.line_loop.lines[1], surface.line_loop.lines[3]], size[1]
)
code = "Transfinite Surface {{{}}}".format(surface.id)
if orientation is not None:
code += " " + orientation
self._GMSH_CODE.append(code + ";")
return
def add_circle(
self,
x0,
radius,
lcar=None,
R=None,
compound=False,
num_sections=3,
holes=None,
make_surface=True,
):
"""Add circle in the :math:`x`-:math:`y`-plane.
"""
if holes is None:
holes = []
else:
assert make_surface
# Define points that make the circle (midpoint and the four cardinal
# directions).
X = numpy.zeros((num_sections + 1, len(x0)))
if num_sections == 4:
# For accuracy, the points are provided explicitly.
X[1:, [0, 1]] = numpy.array(
[[radius, 0.0], [0.0, radius], [-radius, 0.0], [0.0, -radius]]
)
else:
X[1:, [0, 1]] = numpy.array(
[
[
radius * numpy.cos(2 * numpy.pi * k / num_sections),
radius * numpy.sin(2 * numpy.pi * k / num_sections),
]
for k in range(num_sections)
]
)
if R is not None:
assert numpy.allclose(
abs(numpy.linalg.eigvals(R)), numpy.ones(X.shape[1])
), "The transformation matrix doesn't preserve circles; at least one eigenvalue lies off the unit circle."
X = numpy.dot(X, R.T)
X += x0
# Add Gmsh Points.
p = [self.add_point(x, lcar=lcar) for x in X]
# Define the circle arcs.
arcs = [self.add_circle_arc(p[k], p[0], p[k + 1]) for k in range(1, len(p) - 1)]
arcs.append(self.add_circle_arc(p[-1], p[0], p[1]))
if compound:
if self._gmsh_major() == 3:
arcs = [self.add_compound_line(arcs)]
elif self._gmsh_major() == 4:
self.add_raw_code(
"Compound Curve{{{}}};".format(",".join([arc.id for arc in arcs]))
)
line_loop = self.add_line_loop(arcs)
if make_surface:
plane_surface = self.add_plane_surface(line_loop, holes)
if compound and self._gmsh_major() == 4:
self.add_raw_code("Compound Surface{{{}}};".format(plane_surface.id))
else:
plane_surface = None
class Circle(object):
def __init__(
self,
x0,
radius,
R,
compound,
num_sections,
holes,
line_loop,
plane_surface,
lcar=None,
):
self.x0 = x0
self.radius = radius
self.lcar = lcar
self.R = R
self.compound = compound
self.num_sections = num_sections
self.holes = holes
self.line_loop = line_loop
self.plane_surface = plane_surface
return
return Circle(
x0,
radius,
R,
compound,
num_sections,
holes,
line_loop,
plane_surface,
lcar=lcar,
)
def extrude(
self,
input_entity,
translation_axis=None,
rotation_axis=None,
point_on_axis=None,
angle=None,
num_layers=None,
recombine=False,
):
"""Extrusion (translation + rotation) of any entity along a given
translation_axis, around a given rotation_axis, about a given angle. If
one of the entities is not provided, this method will produce only
translation or rotation.
"""
self._EXTRUDE_ID += 1
if _is_string(input_entity):
entity = Dummy(input_entity)
elif isinstance(input_entity, PointBase):
entity = Dummy("Point{{{}}}".format(input_entity.id))
elif isinstance(input_entity, SurfaceBase):
entity = Dummy("Surface{{{}}}".format(input_entity.id))
elif hasattr(input_entity, "surface"):
entity = Dummy("Surface{{{}}}".format(input_entity.surface.id))
else:
assert isinstance(input_entity, LineBase), "Illegal extrude entity."
entity = Dummy("Line{{{}}}".format(input_entity.id))
extrusion_string = ""
# out[] = Extrude{0,1,0}{ Line{1}; };
name = "ex{}".format(self._EXTRUDE_ID)
if translation_axis is not None:
if rotation_axis is not None:
extrusion_string += "{}[] = Extrude{{{{{}}}, {{{}}}, {{{}}}, {}}}{{{};".format(
name,
",".join(repr(x) for x in translation_axis),
",".join(repr(x) for x in rotation_axis),
",".join(repr(x) for x in point_on_axis),
angle,
entity.id,
)
else:
# Only translation
extrusion_string += "{}[] = Extrude {{{}}} {{{};".format(
name, ",".join(repr(x) for x in translation_axis), entity.id
)
else:
assert (
rotation_axis is not None
), "Specify at least translation or rotation."
# Only rotation
extrusion_string += "{}[] = Extrude{{{{{}}}, {{{}}}, {}}}{{{};".format(
name,
",".join(repr(x) for x in rotation_axis),
",".join(repr(x) for x in point_on_axis),
angle,
entity.id,
)
if num_layers is not None:
extrusion_string += " Layers{{{}}}; {}".format(
num_layers, "Recombine;" if recombine else ""
)
# close command
extrusion_string += "};"
self._GMSH_CODE.append(extrusion_string)
# From <https://www.manpagez.com/info/gmsh/gmsh-2.4.0/gmsh_66.php>:
#
# > In this last extrusion command we retrieved the volume number
# > programatically by saving the output of the command into a
# > list. This list will contain the "top" of the extruded surface (in
# > out[0]) as well as the newly created volume (in out[1]).
#
top = "{}[0]".format(name)
extruded = "{}[1]".format(name)
if isinstance(input_entity, LineBase):
top = LineBase(top)
# A surface extruded from a single line has always 4 edges
extruded = SurfaceBase(extruded, 4)
elif isinstance(input_entity, SurfaceBase):
top = SurfaceBase(top, input_entity.num_edges)
extruded = VolumeBase(extruded)
elif isinstance(input_entity, PointBase):
top = PointBase(top)
extruded = LineBase(extruded)
else:
top = Dummy(top)
extruded = Dummy(extruded)
lat = []
# lateral surfaces can be deduced only if we start from a SurfaceBase
if isinstance(input_entity, SurfaceBase):
# out[0]` is the surface, out[1] the top, and everything after that
# the sides, cf.
# <https://gmsh.info/doc/texinfo/gmsh.html#Extrusions>. Each
# lateral surface has 4 edges: the one from input_entity, the one
# from top, and the two lines (or splines) connecting their extreme
# points.
lat = [
SurfaceBase("{}[{}]".format(name, i + 2), 4)
for i in range(input_entity.num_edges)
]
return top, extruded, lat
def add_boundary_layer(
self,
edges_list=None,
faces_list=None,
nodes_list=None,
anisomax=None,
hfar=None,
hwall_n=None,
ratio=None,
thickness=None,
):
# Don't use [] as default argument, cf.
# <https://stackoverflow.com/a/113198/353337>
if edges_list is None:
edges_list = []
if faces_list is None:
faces_list = []
if nodes_list is None:
nodes_list = []
self._FIELD_ID += 1
name = "field{}".format(self._FIELD_ID)
self._GMSH_CODE.append("{} = newf;".format(name))
self._GMSH_CODE.append("Field[{}] = BoundaryLayer;".format(name))
if edges_list:
self._GMSH_CODE.append(
"Field[{}].EdgesList = {{{}}};".format(
name, ",".join([e.id for e in edges_list])
)
)
if faces_list:
self._GMSH_CODE.append(
"Field[{}].FacesList = {{{}}};".format(name, ",".join(faces_list))
)
if nodes_list:
self._GMSH_CODE.append(
"Field[{}].NodesList = {{{}}};".format(
name, ",".join([n.id for n in nodes_list])
)
)
if hfar:
self._GMSH_CODE.append("Field[{}].hfar= {!r};".format(name, hfar))
if hwall_n:
self._GMSH_CODE.append("Field[{}].hwall_n= {!r};".format(name, hwall_n))
if ratio:
self._GMSH_CODE.append("Field[{}].ratio= {!r};".format(name, ratio))
if thickness:
self._GMSH_CODE.append("Field[{}].thickness= {!r};".format(name, thickness))
if anisomax:
self._GMSH_CODE.append("Field[{}].AnisoMax= {!r};".format(name, anisomax))
return name
def add_background_field(self, fields, aggregation_type="Min"):
self._FIELD_ID += 1
name = "field{}".format(self._FIELD_ID)
self._GMSH_CODE.append("{} = newf;".format(name))
self._GMSH_CODE.append("Field[{}] = {};".format(name, aggregation_type))
self._GMSH_CODE.append(
"Field[{}].FieldsList = {{{}}};".format(name, ", ".join(fields))
)
self._GMSH_CODE.append("Background Field = {};".format(name))
return name
def add_comment(self, string):
self._GMSH_CODE.append("// " + string)
return
def add_raw_code(self, string_or_list):
"""Add raw Gmsh code.
"""
if _is_string(string_or_list):
self._GMSH_CODE.append(string_or_list)
else:
assert isinstance(string_or_list, list)
for string in string_or_list:
self._GMSH_CODE.append(string)
return
def add_rectangle(
self, xmin, xmax, ymin, ymax, z, lcar=None, holes=None, make_surface=True
):
return self.add_polygon(
[[xmin, ymin, z], [xmax, ymin, z], [xmax, ymax, z], [xmin, ymax, z]],
lcar=lcar,
holes=holes,
make_surface=make_surface,
)
class Polygon(object):
def __init__(self, points, lines, line_loop, surface, lcar=None):
self.points = points
self.lines = lines
self.line_loop = line_loop
self.surface = surface
self.lcar = lcar
if surface is not None:
self.id = self.surface.id
self.dimension = 2
return
def add_polygon(self, X, lcar=None, holes=None, make_surface=True):
if holes is None:
holes = []
else:
assert make_surface
if isinstance(lcar, list):
assert len(X) == len(lcar)
else:
lcar = len(X) * [lcar]
# Create points.
p = [self.add_point(x, lcar=l) for x, l in zip(X, lcar)]
# Create lines
lines = [self.add_line(p[k], p[k + 1]) for k in range(len(p) - 1)]
lines.append(self.add_line(p[-1], p[0]))
ll = self.add_line_loop((lines))
surface = self.add_plane_surface(ll, holes) if make_surface else None
return self.Polygon(p, lines, ll, surface, lcar=lcar)
def add_ellipsoid(self, x0, radii, lcar=None, with_volume=True, holes=None):
"""Creates an ellipsoid with radii around a given midpoint
:math:`x_0`.
"""
if holes is None:
holes = []
if holes:
assert with_volume
# Add points.
p = [
self.add_point(x0, lcar=lcar),
self.add_point([x0[0] + radii[0], x0[1], x0[2]], lcar=lcar),
self.add_point([x0[0], x0[1] + radii[1], x0[2]], lcar=lcar),
self.add_point([x0[0], x0[1], x0[2] + radii[2]], lcar=lcar),
self.add_point([x0[0] - radii[0], x0[1], x0[2]], lcar=lcar),
self.add_point([x0[0], x0[1] - radii[1], x0[2]], lcar=lcar),
self.add_point([x0[0], x0[1], x0[2] - radii[2]], lcar=lcar),
]
# Add skeleton.
# Alternative for circles:
# `self.add_circle_arc(a, b, c)`
c = [
self.add_ellipse_arc(p[1], p[0], p[6], p[6]),
self.add_ellipse_arc(p[6], p[0], p[4], p[4]),
self.add_ellipse_arc(p[4], p[0], p[3], p[3]),
self.add_ellipse_arc(p[3], p[0], p[1], p[1]),
self.add_ellipse_arc(p[1], p[0], p[2], p[2]),
self.add_ellipse_arc(p[2], p[0], p[4], p[4]),
self.add_ellipse_arc(p[4], p[0], p[5], p[5]),
self.add_ellipse_arc(p[5], p[0], p[1], p[1]),
self.add_ellipse_arc(p[6], p[0], p[2], p[2]),
self.add_ellipse_arc(p[2], p[0], p[3], p[3]),
self.add_ellipse_arc(p[3], p[0], p[5], p[5]),
self.add_ellipse_arc(p[5], p[0], p[6], p[6]),
]
# Add surfaces (1/8th of the ball surface).
ll = [
# one half
self.add_line_loop([c[4], c[9], c[3]]),
self.add_line_loop([c[8], -c[4], c[0]]),
self.add_line_loop([-c[9], c[5], c[2]]),
self.add_line_loop([-c[5], -c[8], c[1]]),
# the other half
self.add_line_loop([c[7], -c[3], c[10]]),
self.add_line_loop([c[11], -c[7], -c[0]]),
self.add_line_loop([-c[10], -c[2], c[6]]),
self.add_line_loop([-c[1], -c[6], -c[11]]),
]
# Create a surface for each line loop.
s = [self.add_surface(l) for l in ll]
# Combine the surfaces to avoid seams
if self._gmsh_major() == 3:
s = [self.add_compound_surface(s[:4]), self.add_compound_surface(s[4:])]
else:
assert self._gmsh_major() == 4
# <https://gitlab.onelab.info/gmsh/gmsh/issues/507>
self.add_raw_code(
"Compound Surface{{{}}};".format(",".join([surf.id for surf in s[:4]]))
)
self.add_raw_code(
"Compound Surface{{{}}};".format(",".join([surf.id for surf in s[4:]]))
)
# Create the surface loop.
surface_loop = self.add_surface_loop(s)
# if holes:
# # Create an array of surface loops; the first entry is the outer
# # surface loop, the following ones are holes.
# surface_loop = self.add_array([surface_loop] + holes)
# Create volume.
volume = self.add_volume(surface_loop, holes) if with_volume else None
class Ellipsoid(object):
dimension = 3
def __init__(self, x0, radii, surface_loop, volume, lcar=None):
self.x0 = x0
self.lcar = lcar
self.radii = radii
self.surface_loop = surface_loop
self.volume = volume
return
return Ellipsoid(x0, radii, surface_loop, volume, lcar=lcar)
def add_ball(self, x0, radius, **kwargs):
return self.add_ellipsoid(x0, [radius, radius, radius], **kwargs)
def add_box(self, x0, x1, y0, y1, z0, z1, lcar=None, with_volume=True, holes=None):
if holes is None:
holes = []
if holes:
assert with_volume
# Define corner points.
p = [
self.add_point([x1, y1, z1], lcar=lcar),
self.add_point([x1, y1, z0], lcar=lcar),
self.add_point([x1, y0, z1], lcar=lcar),
self.add_point([x1, y0, z0], lcar=lcar),
self.add_point([x0, y1, z1], lcar=lcar),
self.add_point([x0, y1, z0], lcar=lcar),
self.add_point([x0, y0, z1], lcar=lcar),
self.add_point([x0, y0, z0], lcar=lcar),
]
# Define edges.
e = [
self.add_line(p[0], p[1]),
self.add_line(p[0], p[2]),
self.add_line(p[0], p[4]),
self.add_line(p[1], p[3]),
self.add_line(p[1], p[5]),
self.add_line(p[2], p[3]),
self.add_line(p[2], p[6]),
self.add_line(p[3], p[7]),
self.add_line(p[4], p[5]),
self.add_line(p[4], p[6]),
self.add_line(p[5], p[7]),
self.add_line(p[6], p[7]),
]
# Define the six line loops.
ll = [
self.add_line_loop([e[0], e[3], -e[5], -e[1]]),
self.add_line_loop([e[0], e[4], -e[8], -e[2]]),
self.add_line_loop([e[1], e[6], -e[9], -e[2]]),
self.add_line_loop([e[3], e[7], -e[10], -e[4]]),
self.add_line_loop([e[5], e[7], -e[11], -e[6]]),
self.add_line_loop([e[8], e[10], -e[11], -e[9]]),
]
# Create a surface for each line loop.
s = [self.add_surface(l) for l in ll]
# Create the surface loop.
surface_loop = self.add_surface_loop(s)
# Create volume
vol = self.add_volume(surface_loop, holes) if with_volume else None
class Box(object):
def __init__(self, x0, x1, y0, y1, z0, z1, surface_loop, volume, lcar=None):
self.x0 = x0
self.x1 = x1
self.y0 = y0
self.y1 = y1
self.z0 = z0
self.z1 = z1
self.lcar = lcar
self.surface_loop = surface_loop
self.volume = volume
return
return Box(x0, x1, y0, y1, z0, z1, surface_loop, vol, lcar=lcar)
def add_torus(
self,
irad,
orad,
lcar=None,
R=numpy.eye(3),
x0=numpy.array([0.0, 0.0, 0.0]),
variant="extrude_lines",
):
if variant == "extrude_lines":
return self._add_torus_extrude_lines(irad, orad, lcar=lcar, R=R, x0=x0)
assert variant == "extrude_circle"
return self._add_torus_extrude_circle(irad, orad, lcar=lcar, R=R, x0=x0)
def _add_torus_extrude_lines(
self, irad, orad, lcar=None, R=numpy.eye(3), x0=numpy.array([0.0, 0.0, 0.0])
):
"""Create Gmsh code for the torus in the x-y plane under the coordinate
transformation
.. math::
\\hat{x} = R x + x_0.
:param irad: inner radius of the torus
:param orad: outer radius of the torus
"""
self.add_comment("Torus")
# Add circle
x0t = numpy.dot(R, numpy.array([0.0, orad, 0.0]))
# Get circles in y-z plane
Rc = numpy.array([[0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [1.0, 0.0, 0.0]])
c = self.add_circle(x0 + x0t, irad, lcar=lcar, R=numpy.dot(R, Rc))
rot_axis = [0.0, 0.0, 1.0]
rot_axis = numpy.dot(R, rot_axis)
point_on_rot_axis = [0.0, 0.0, 0.0]
point_on_rot_axis = numpy.dot(R, point_on_rot_axis) + x0
# Form the torus by extruding the circle three times by 2/3*pi. This
# works around the inability of Gmsh to extrude by pi or more. The
# Extrude() macro returns an array; the first [0] entry in the array is
# the entity that has been extruded at the far end. This can be used
# for the following Extrude() step. The second [1] entry of the array
# is the surface that was created by the extrusion.
previous = c.line_loop.lines
angle = "2*Pi/3"
all_surfaces = []
for i in range(3):
self.add_comment("Round no. {}".format(i + 1))
for k, p in enumerate(previous):
# ts1[] = Extrude {{0,0,1}, {0,0,0}, 2*Pi/3}{Line{tc1};};
# ...
top, surf, _ = self.extrude(
p,
rotation_axis=rot_axis,
point_on_axis=point_on_rot_axis,
angle=angle,
)
all_surfaces.append(surf)
previous[k] = top
# compound_surface = CompoundSurface(all_surfaces)
surface_loop = self.add_surface_loop(all_surfaces)
vol = self.add_volume(surface_loop)
# The newline at the end is essential:
# If a GEO file doesn't end in a newline, Gmsh will report a syntax
# error.
self.add_comment("\n")
return vol
def _add_torus_extrude_circle(
self, irad, orad, lcar=None, R=numpy.eye(3), x0=numpy.array([0.0, 0.0, 0.0])
):
"""Create Gmsh code for the torus under the coordinate transformation
.. math::
\\hat{x} = R x + x_0.
:param irad: inner radius of the torus
:param orad: outer radius of the torus
"""
self.add_comment(76 * "-")
self.add_comment("Torus")
# Add circle
x0t = numpy.dot(R, numpy.array([0.0, orad, 0.0]))
Rc = numpy.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
c = self.add_circle(x0 + x0t, irad, lcar=lcar, R=numpy.dot(R, Rc))
rot_axis = [0.0, 0.0, 1.0]
rot_axis = numpy.dot(R, rot_axis)
point_on_rot_axis = [0.0, 0.0, 0.0]
point_on_rot_axis = numpy.dot(R, point_on_rot_axis) + x0
# Form the torus by extruding the circle three times by 2/3*pi. This
# works around the inability of Gmsh to extrude by pi or more. The
# Extrude() macro returns an array; the first [0] entry in the array is
# the entity that has been extruded at the far end. This can be used
# for the following Extrude() step. The second [1] entry of the array
# is the surface that was created by the extrusion. The third [2-end]
# is a list of all the planes of the lateral surface.
previous = c.plane_surface
all_volumes = []
num_steps = 3
for _ in range(num_steps):
top, vol, _ = self.extrude(
previous,
rotation_axis=rot_axis,
point_on_axis=point_on_rot_axis,
angle="2*Pi/{}".format(num_steps),
)
previous = top
all_volumes.append(vol)
if self._gmsh_major() == 3:
# This actually returns the volume, but the gmsh 4 version doesn't have that
# feature. Hence, for compatibility, also ditch it here.
self.add_compound_volume(all_volumes)
else:
assert self._gmsh_major() == 4
self.add_raw_code(
"Compound Volume{{{}}};".format(",".join(v.id for v in all_volumes))
)
self.add_comment(76 * "-" + "\n")
return
def add_pipe(
self,
outer_radius,
inner_radius,
length,
R=numpy.eye(3),
x0=numpy.array([0.0, 0.0, 0.0]),
lcar=None,
variant="rectangle_rotation",
):
if variant == "rectangle_rotation":
return self._add_pipe_by_rectangle_rotation(
outer_radius, inner_radius, length, R=R, x0=x0, lcar=lcar
)
assert variant == "circle_extrusion"
return self._add_pipe_by_circle_extrusion(
outer_radius, inner_radius, length, R=R, x0=x0, lcar=lcar
)
def _add_pipe_by_rectangle_rotation(
self,
outer_radius,
inner_radius,
length,
R=numpy.eye(3),
x0=numpy.array([0.0, 0.0, 0.0]),
lcar=None,
):
"""Hollow cylinder.
Define a rectangle, extrude it by rotation.
"""
self.add_comment("Define rectangle.")
X = numpy.array(
[
[0.0, outer_radius, -0.5 * length],
[0.0, outer_radius, +0.5 * length],
[0.0, inner_radius, +0.5 * length],
[0.0, inner_radius, -0.5 * length],
]
)
# Apply transformation.
X = [numpy.dot(R, x) + x0 for x in X]
# Create points set.
p = [self.add_point(x, lcar=lcar) for x in X]
# Define edges.
e = [
self.add_line(p[0], p[1]),
self.add_line(p[1], p[2]),
self.add_line(p[2], p[3]),
self.add_line(p[3], p[0]),
]
rot_axis = [0.0, 0.0, 1.0]
rot_axis = numpy.dot(R, rot_axis)
point_on_rot_axis = [0.0, 0.0, 0.0]
point_on_rot_axis = numpy.dot(R, point_on_rot_axis) + x0
# Extrude all edges three times by 2*Pi/3.
previous = e
angle = "2*Pi/3"
all_surfaces = []
# com = []
self.add_comment("Extrude in 3 steps.")
for i in range(3):
self.add_comment("Step {}".format(i + 1))
for k, p in enumerate(previous):
# ts1[] = Extrude {{0,0,1}, {0,0,0}, 2*Pi/3}{Line{tc1};};
top, surf, _ = self.extrude(
p,
rotation_axis=rot_axis,
point_on_axis=point_on_rot_axis,
angle=angle,
)
# if k==0:
# com.append(surf)
# else:
# all_names.appends(surf)
all_surfaces.append(surf)
previous[k] = top
#
# cs = CompoundSurface(com)
# Now just add surface loop and volume.
# all_surfaces = all_names + [cs]
surface_loop = self.add_surface_loop(all_surfaces)
vol = self.add_volume(surface_loop)
return vol
def _add_pipe_by_circle_extrusion(
self,
outer_radius,
inner_radius,
length,
R=numpy.eye(3),
x0=numpy.array([0.0, 0.0, 0.0]),
lcar=None,
):
"""Hollow cylinder.
Define a ring, extrude it by translation.
"""
# Define ring which to Extrude by translation.
Rc = numpy.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]])
c_inner = self.add_circle(
x0, inner_radius, lcar=lcar, R=numpy.dot(R, Rc), make_surface=False
)
circ = self.add_circle(
x0, outer_radius, lcar=lcar, R=numpy.dot(R, Rc), holes=[c_inner.line_loop]
)
# Now Extrude the ring surface.
_, vol, _ = self.extrude(