1 | # ---------------------------------------------------------------------- |
---|
2 | # EXAMPLE: Rappture <mesh> elements |
---|
3 | # ====================================================================== |
---|
4 | # AUTHOR: Martin Hunt, Purdue University |
---|
5 | # Copyright (c) 2015 HUBzero Foundation, LLC |
---|
6 | # |
---|
7 | # See the file "license.terms" for information on usage and |
---|
8 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
9 | # ====================================================================== |
---|
10 | |
---|
11 | import Rappture |
---|
12 | import sys |
---|
13 | import numpy as np |
---|
14 | |
---|
15 | # uncomment these for debugging |
---|
16 | # sys.stderr = open('mesh.err', 'w') |
---|
17 | # sys.stdout = open('mesh.out', 'w') |
---|
18 | |
---|
19 | rx = Rappture.PyXml(sys.argv[1]) |
---|
20 | |
---|
21 | meshtype = rx["input.choice(mesh).current"].value |
---|
22 | contour = rx["input.boolean(contour).current"].value |
---|
23 | |
---|
24 | if contour == "yes": |
---|
25 | view = 'contour' |
---|
26 | else: |
---|
27 | view = 'heightmap' |
---|
28 | |
---|
29 | mesh = rx['output.mesh'] |
---|
30 | mesh['dim'] = 2 |
---|
31 | mesh['units'] = "m" |
---|
32 | mesh['hide'] = "yes" |
---|
33 | |
---|
34 | x = np.linspace(0, 1, num=50, endpoint=True) |
---|
35 | y = np.linspace(0, 1, num=50, endpoint=True) |
---|
36 | |
---|
37 | |
---|
38 | if meshtype == 'cloud': |
---|
39 | mesh['about.label'] = "cloud in unstructured mesh" |
---|
40 | |
---|
41 | # Warning! Unstructured grids require newlines after each |
---|
42 | # set of points!. |
---|
43 | |
---|
44 | # use mgrid. weird syntax, but compact |
---|
45 | # x, y = np.mgrid[0:1:50j, 0:1:50j] |
---|
46 | # pts = (x.ravel('F'), y.ravel('F')) |
---|
47 | |
---|
48 | # OR create x and y vectors then use one of these methods |
---|
49 | |
---|
50 | # Use list comprehension to create string |
---|
51 | pts = ' '.join(["%s %s\n" % (a, b) for b in y for a in x]) |
---|
52 | |
---|
53 | # Use meshgrid. Similar to mgrid() |
---|
54 | # X, Y = np.meshgrid(x, y) |
---|
55 | # pts = (X.ravel(), Y.ravel()) |
---|
56 | |
---|
57 | mesh['unstructured.points'] = pts |
---|
58 | mesh['unstructured.celltypes'] = "" |
---|
59 | |
---|
60 | if meshtype == 'regular': |
---|
61 | mesh['about.label'] = "uniform grid mesh" |
---|
62 | mesh['grid.xaxis.min'] = 0 |
---|
63 | mesh['grid.xaxis.max'] = 1 |
---|
64 | mesh['grid.xaxis.numpoints'] = 50 |
---|
65 | mesh['grid.yaxis.min'] = 0 |
---|
66 | mesh['grid.yaxis.max'] = 1 |
---|
67 | mesh['grid.yaxis.numpoints'] = 50 |
---|
68 | |
---|
69 | if meshtype == 'irregular': |
---|
70 | mesh['about.label'] = "irregular grid mesh" |
---|
71 | mesh['grid.xcoords'] = x |
---|
72 | mesh['grid.ycoords'] = y |
---|
73 | |
---|
74 | if meshtype == 'hybrid': |
---|
75 | mesh['about.label'] = "hybrid regular and irregular grid mesh" |
---|
76 | mesh['grid.xcoords'] = x |
---|
77 | mesh['grid.yaxis.min'] = 0 |
---|
78 | mesh['grid.yaxis.max'] = 1 |
---|
79 | mesh['grid.yaxis.numpoints'] = 50 |
---|
80 | |
---|
81 | if meshtype == 'triangular': |
---|
82 | mesh['about.label'] = "triangles in unstructured mesh" |
---|
83 | mesh.put('unstructured.points', 'points.txt', type='file') |
---|
84 | mesh.put('unstructured.triangles', 'triangles.txt', type='file') |
---|
85 | |
---|
86 | if meshtype == 'generic': |
---|
87 | mesh['about.label'] = "nodes and elements mesh" |
---|
88 | with open('points.txt') as f: |
---|
89 | data = f.read() |
---|
90 | for i, line in enumerate(data.split('\n')): |
---|
91 | if line == '': |
---|
92 | break |
---|
93 | x, y = line.split() |
---|
94 | mesh['node(%s)' % i] = "%s %s" % (x, y) |
---|
95 | with open('triangles.txt') as f: |
---|
96 | data = f.read() |
---|
97 | for i, line in enumerate(data.split('\n')): |
---|
98 | if line == '': |
---|
99 | break |
---|
100 | x, y, z = line.split() |
---|
101 | mesh['element(%s).nodes' % i] = "%s %s %s" % (x, y, z) |
---|
102 | |
---|
103 | if meshtype == 'unstructured': |
---|
104 | mesh['about.label'] = "Unstructured Grid" |
---|
105 | mesh['unstructured.celltypes'] = 'triangle' |
---|
106 | mesh.put('unstructured.points', 'points.txt', type='file') |
---|
107 | mesh.put('unstructured.cells', 'triangles.txt', type='file') |
---|
108 | |
---|
109 | if meshtype == 'cells': |
---|
110 | # FIXME. Copied from tcl example, but looks just like 'unstructured' |
---|
111 | mesh['about.label'] = "Unstructured Grid with Heterogeneous Cells" |
---|
112 | mesh['unstructured.celltypes'] = 'triangle' |
---|
113 | mesh.put('unstructured.points', 'points.txt', type='file') |
---|
114 | mesh.put('unstructured.cells', 'triangles.txt', type='file') |
---|
115 | |
---|
116 | if meshtype == 'vtkmesh': |
---|
117 | mesh['about.label'] = 'vtk mesh' |
---|
118 | mesh.put('vtk', 'mesh.vtk', type='file') |
---|
119 | |
---|
120 | if meshtype == 'vtkfield': |
---|
121 | f = rx['output.field(substrate)'] |
---|
122 | f['about.label'] = "Substrate Surface" |
---|
123 | f.put('component.vtk', 'file.vtk', type='file') |
---|
124 | rx.close() |
---|
125 | |
---|
126 | f = rx['output.field(substrate)'] |
---|
127 | f['about.label'] = "Substrate Surface" |
---|
128 | f['about.view'] = view |
---|
129 | f['component.mesh'] = mesh.name |
---|
130 | f.put('component.values', 'substrate_data.txt', type='file') |
---|
131 | |
---|
132 | f = rx['output.field(particle)'] |
---|
133 | f['about.label'] = "Particle Surface" |
---|
134 | f['about.view'] = view |
---|
135 | f['component.mesh'] = mesh.name |
---|
136 | f.put('component.values', 'particle_data.txt', type='file') |
---|
137 | |
---|
138 | rx['output.string.about.label'] = "Mesh XML definition" |
---|
139 | rx['output.string.current'] = mesh.xml() |
---|
140 | |
---|
141 | rx.close() |
---|