source: branches/1.4/examples/zoo/mesh/mesh.py @ 5682

Last change on this file since 5682 was 5682, checked in by ldelgass, 9 years ago

merge new Python examples from 1.3 branch (uq sync)

File size: 4.3 KB
Line 
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
11import Rappture
12import sys
13import numpy as np
14
15# uncomment these for debugging
16# sys.stderr = open('mesh.err', 'w')
17# sys.stdout = open('mesh.out', 'w')
18
19rx = Rappture.PyXml(sys.argv[1])
20
21meshtype = rx["input.choice(mesh).current"].value
22contour = rx["input.boolean(contour).current"].value
23
24if contour == "yes":
25    view = 'contour'
26else:
27    view = 'heightmap'
28
29mesh = rx['output.mesh']
30mesh['dim'] = 2
31mesh['units'] = "m"
32mesh['hide'] = "yes"
33
34x = np.linspace(0, 1, num=50, endpoint=True)
35y = np.linspace(0, 1, num=50, endpoint=True)
36
37
38if 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
60if 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
69if meshtype == 'irregular':
70    mesh['about.label'] = "irregular grid mesh"
71    mesh['grid.xcoords'] = x
72    mesh['grid.ycoords'] = y
73
74if 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
81if 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
86if 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
103if 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
109if 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
116if meshtype == 'vtkmesh':
117    mesh['about.label'] = 'vtk mesh'
118    mesh.put('vtk', 'mesh.vtk', type='file')
119
120if 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
126f = rx['output.field(substrate)']
127f['about.label'] = "Substrate Surface"
128f['about.view'] = view
129f['component.mesh'] = mesh.name
130f.put('component.values', 'substrate_data.txt', type='file')
131
132f = rx['output.field(particle)']
133f['about.label'] = "Particle Surface"
134f['about.view'] = view
135f['component.mesh'] = mesh.name
136f.put('component.values', 'particle_data.txt', type='file')
137
138rx['output.string.about.label'] = "Mesh XML definition"
139rx['output.string.current'] = mesh.xml()
140
141rx.close()
Note: See TracBrowser for help on using the repository browser.