-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shapes.py
executable file
·207 lines (166 loc) · 7.62 KB
/
Shapes.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
#! /usr/bin/env python3
#
# Base class for all geometry shapes in a finite element analysis
#
# Jan-2020, Pat Welch, pat@mousebrains.com
import numpy as np
from scipy.spatial.transform import Rotation as R
class Shapes(list):
def __init__(self, info:dict, msg:str) -> None:
list.__init__(self)
for key in info:
item = info[key]
shape = item["shape"] if "shape" in item else None
if shape is None: raise Exception("No shape specified for {}, {}".format(key, msg))
if shape == "cylinder":
self.append(Cylinder(key, item, msg))
else:
raise Exception("Unrecognized shape, {}, in {}, {}".format(shape, key, msg))
def dryVolume(self):
volume = 0
for item in self: volume += item.qDry * item.volume
return volume
def wetVolume(self):
volume = 0
for item in self: volume += (not item.qDry) * item.volume
return volume
class Base:
""" API specification """
def __init__(self, name:str) -> None:
self.name = name # Name of the shape
def grid(self, rotation:R=None, offset:np.array=None) -> np.array:
""" Return the grid points, possibly rotated and offset """
position = self.position
if rotation is not None:
# Rotate from body to grid
position = rotation.apply(position, inverse=True)
if offset is not None: position = np.add(position, offset)
return position
def integrate(self, force:np.array, rotation:R=None) -> np.array:
""" Return surface integral over force """
surface = self.surface
if rotation is not None:
surface = rotation.apply(surface, inverse=True)
# integral of dot product
return \
np.sum(surface[:,0] * force[:,0]) + \
np.sum(surface[:,1] * force[:,1]) + \
np.sum(surface[:,2] * force[:,2])
@staticmethod
def mkRotation(angles:list) -> R:
return R.from_euler("ZYX", angles, degrees=True)
@staticmethod
def strRotation(r:R) -> list:
return r.as_euler("ZYX", degrees=True)
# Plotting methods for a shape
@staticmethod
def plotShow(ax=None) -> None:
if ax is not None:
import matplotlib.pyplot as plt
plt.show()
def plotInit(self, ax=None):
if ax is None:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")
return ax
def plotScatter(self, ax, pos:np.array=None, sz=5):
if pos is None: pos = self.position
ax = self.plotInit(ax)
ax.scatter(pos[:,0], pos[:,1], pos[:,2], s=sz)
return ax
def plotWireFrame(self, ax, pos:np.array=None, rstride=1, cstride=1, color:str=None):
if pos is None: pos = self.position
(x,y,z) = self.wireFrame(pos)
ax = self.plotInit(ax)
ax.plot_wireframe(x, y, z, rstride=rstride, cstride=cstride, color=color)
return ax
def plot(self, ax=None):
return self.plotScatter(ax)
class Cylinder(Base):
""" Cylindrical part of a cylinder with outward pointing normals to surface """
def __init__(self, name:str, info:dict, msg:str) -> None:
Base.__init__(self, name + "::cylinder")
for key in ["length", "radius", "offset", "rotation", "nRadial", "nLength"]:
if key not in info:
raise Exception("Field {} not in {}, {}".format(key, name, msg))
self.__info = info
radius = info["radius"] # Radius of the cylinder
length = info["length"] # Length of cylinder
self.__radius = radius
self.qDry = info["qDry"] if "qDry" in info else False
self.volume = np.pi * radius * radius * length # Volume of cylinder
dTheta = (2 * np.pi) / info["nRadial"] # Step size in theta
dZ = length / info["nLength"] # Step size along axis
self.__area = dZ * self.__radius * dTheta # Area of each element
# Angle to center of each segment
theta = np.arange(dTheta/2, 2 * np.pi, dTheta) # (0, 2pi)
# z of center of each segment
zMax = info["length"] / 2
z = np.arange(-zMax + dZ/2, zMax, dZ) # (-zMax, zMax)
(gTheta, gZ) = np.meshgrid(theta, z) # Grid of all possible theta/z pairs
gTheta = gTheta.ravel() # Change from 2D to 1D without copying
gZ = gZ.ravel() # Change from 2D to 1D without copying
nx = np.cos(gTheta) # x component of outward pointing unit normal vector
ny = np.sin(gTheta) # y component of outward pointing unit normal vector
unitNorm = np.column_stack((nx, ny, np.zeros(nx.shape))) # Outward pointing unit normal
position = np.column_stack((radius * nx, radius * ny, gZ)) # center of each element
# Rotate the cylinder
self.__rotation = self.mkRotation(info["rotation"])
unitNorm = self.__rotation.apply(unitNorm) # Rotate outward pointing normals
position = self.__rotation.apply(position) # Rotate positions of centers
# Offset the cylinder, the unit norm does not change
self.__offset = info["offset"]
self.position = np.add(position, self.__offset) # Position of each surface element
self.surface = self.__area * unitNorm # outward pointing surface vector
def __repr__(self) -> str:
msg = self.name + \
" radius {} area {:.4g} n {} qDry {} volume {:.4g}".format(
self.__radius, self.__area, self.position.shape[0],
self.qDry, self.volume)
msg+= "\n" + self.name + \
" rotation {}".format(self.strRotation(self.__rotation))
msg+= "\n" + self.name + " offset {}".format(self.__offset)
msg+= "\n" + self.name + " area={}".format(self.integrate(self.surface / self.__area))
return msg
def wireFrame(self, position:np.array=None) -> tuple:
""" Return x,y,z for use in plotting a wire frame """
if position is None: position = self.position
nTheta = self.__info["nRadial"]
nZ = self.__info["nLength"]
x = position[:,0].reshape(nTheta, nZ)
y = position[:,1].reshape(nTheta, nZ)
z = position[:,2].reshape(nTheta, nZ)
return (x, y, z)
if __name__ == "__main__":
import yaml
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--glider", type=str, metavar="foo.yml", required=True,
help="geometry configuration YAML file")
parser.add_argument("--plot", action="store_true", help="Plot shapes")
parser.add_argument("--pitch", type=float, metavar="degrees", default=0,
help="Pitch angle of body")
parser.add_argument("--heading", type=float, metavar="degrees", default=0,
help="heading angle of body")
parser.add_argument("--roll", type=float, metavar="degrees", default=0,
help="Roll angle of body")
args = parser.parse_args()
print(args)
with open(args.glider, "r") as fp: info = yaml.safe_load(fp)
if "geometry" not in info: raise Exception("geometry not in " + args.glider)
shapes = Shapes(info["geometry"], "in " + args.glider)
ax = None
r = Base.mkRotation([args.heading, args.pitch, args.roll])
for shp in shapes:
print(shp)
grid = shp.grid(r)
print("Grid")
print(grid)
if args.plot:
ax = shp.plotWireFrame(ax, rstride=10, cstride=10, color="blue")
ax = shp.plotWireFrame(ax, grid, rstride=10, cstride=10, color="yellow")
Base.plotShow(ax)