-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Included PolarGrid class. right now it assumes no ghost cells #136
base: main
Are you sure you want to change the base?
Changes from 6 commits
e0b136f
982e449
eb71efc
b716ca3
196e9df
f2367c1
7466128
bbe762d
f9afaa6
a8f9b65
c377f3b
f0f089b
7405339
d2d5712
df648dc
f98abf7
8e8e0d6
ffdce53
55c8377
d076961
8fa6b97
54ecf2d
6173abd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -885,5 +885,208 @@ def do_demo(): | |
mydata.pretty_print("a") | ||
|
||
|
||
# **c checking | ||
class PolarGrid(Grid2d): | ||
""" | ||
the 2-d grid class. The grid object will contain the coordinate | ||
information (at various centerings). | ||
|
||
A basic (1-d) representation of the layout is:: | ||
|
||
| | | X | | | | X | | | | ||
+--*--+- // -+--*--X--*--+--*--+- // -+--*--+--*--X--*--+- // -+--*--+ | ||
0 ng-1 ng ng+1 ... ng+nx-1 ng+nx 2ng+nx-1 | ||
|
||
ilo ihi | ||
|
||
|<- ng guardcells->|<---- nx interior zones ----->|<- ng guardcells->| | ||
|
||
The '*' marks the data locations. | ||
""" | ||
|
||
# pylint: disable=too-many-instance-attributes | ||
|
||
def __init__(self, nx, ny, ng=1, | ||
xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0): | ||
|
||
""" | ||
Create a PolarGrid object. | ||
|
||
The only data that we require is the number of points that | ||
make up the mesh in each direction. Optionally we take the | ||
extrema of the domain (default is [0,1]x[0,1]) and number of | ||
ghost cells (default is 1). | ||
|
||
Note that the Grid2d object only defines the discretization, | ||
it does not know about the boundary conditions, as these can | ||
vary depending on the variable. | ||
|
||
Parameters | ||
---------- | ||
nx : int | ||
Number of zones in the r-direction | ||
ny : int | ||
Number of zones in the theta-direction | ||
ng : int, optional | ||
Number of ghost cells | ||
xmin : float, optional | ||
Physical coordinate at the lower x boundary | ||
xmax : float, optional | ||
Physical coordinate at the upper x boundary | ||
ymin : float, optional | ||
Physical coordinate at the lower y boundary | ||
ymax : float, optional | ||
Physical coordinate at the upper y boundary | ||
""" | ||
|
||
# pylint: disable=too-many-arguments | ||
|
||
# size of grid | ||
self.nx = int(nx) | ||
self.ny = int(ny) | ||
self.ng = int(ng) | ||
|
||
self.qx = int(2*ng + nx) | ||
self.qy = int(2*ng + ny) | ||
|
||
# domain extrema | ||
self.xmin = xmin | ||
self.xmax = xmax | ||
|
||
self.ymin = ymin | ||
self.ymax = ymax | ||
|
||
# compute the indices of the block interior (excluding guardcells) | ||
self.ilo = self.ng | ||
self.ihi = self.ng + self.nx-1 | ||
|
||
self.jlo = self.ng | ||
self.jhi = self.ng + self.ny-1 | ||
|
||
# center of the grid (for convenience) | ||
self.ic = self.ilo + self.nx//2 - 1 | ||
self.jc = self.jlo + self.ny//2 - 1 | ||
|
||
# define the coordinate information at the left, center, and right | ||
# zone coordinates | ||
self.dx = (xmax - xmin)/nx | ||
|
||
self.xl = (np.arange(self.qx) - ng)*self.dx + xmin | ||
self.xr = (np.arange(self.qx) + 1.0 - ng)*self.dx + xmin | ||
self.x = 0.5*(self.xl + self.xr) | ||
|
||
self.dy = (ymax - ymin)/ny | ||
|
||
self.yl = (np.arange(self.qy) - ng)*self.dy + ymin | ||
self.yr = (np.arange(self.qy) + 1.0 - ng)*self.dy + ymin | ||
self.y = 0.5*(self.yl + self.yr) | ||
|
||
# 2-d versions of the zone coordinates (replace with meshgrid?) | ||
x2d = np.repeat(self.x, self.qy) | ||
x2d.shape = (self.qx, self.qy) | ||
self.x2d = x2d | ||
|
||
y2d = np.repeat(self.y, self.qx) | ||
y2d.shape = (self.qy, self.qx) | ||
y2d = np.transpose(y2d) | ||
self.y2d = y2d | ||
|
||
|
||
def face_area(self): | ||
""" | ||
Return an array of the face areas. | ||
The shape of the returned array is (ni, nj). | ||
""" | ||
tr = lambda arr: arr.transpose(1, 2, 0) | ||
x = self.cell_vertices()[:,0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use the data in the class instead of recomputing the vertices yourself? |
||
y = self.cell_vertices()[0,:] | ||
r0 = x[:-1, :-1] | ||
r1 = x[+1:, :-1] | ||
t0 = y[:-1, :-1] | ||
t1 = y[+1:, +1:] | ||
|
||
# ** the area of the arc | ||
|
||
area_i = np.pi * (r0 * np.sin(t0) + r0 * np.sin(t1)) * np.sqrt(np.square(r0 * np.sin(t1) - r0 * np.sin(t0)) + np.square(r0 * np.cos(t1) - r0 * np.cos(t0))) | ||
area_j = np.pi * (r0 * np.sin(t0) + r1 * np.sin(t0)) * np.sqrt(np.square(r1 * np.sin(t0) - r0 * np.sin(t0)) + np.square(r1 * np.cos(t0) - r0 * np.cos(t0))) | ||
return tr(np.array([area_i, area_j])) | ||
|
||
def area_x(self): | ||
""" | ||
Return an array of the face areas. | ||
The shape of the returned array is (ni, nj). | ||
""" | ||
tr = lambda arr: arr.transpose(1, 2, 0) | ||
x = self.cell_vertices()[:,0] | ||
y = self.cell_vertices()[0,:] | ||
r0 = x[:-1, :-1] | ||
r1 = x[+1:, :-1] | ||
t0 = y[:-1, :-1] | ||
t1 = y[+1:, +1:] | ||
|
||
area = r1 - r0 | ||
return area | ||
|
||
def area_y(self): | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be the area through a y face, which is just dr There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but y face is dtheta |
||
Return an array of the face areas. | ||
The shape of the returned array is (ni, nj). | ||
""" | ||
tr = lambda arr: arr.transpose(1, 2, 0) | ||
x = self.cell_vertices()[:,0] | ||
y = self.cell_vertices()[0,:] | ||
r0 = x[:-1, :-1] | ||
r1 = x[+1:, :-1] | ||
t0 = y[:-1, :-1] | ||
t1 = y[+1:, +1:] | ||
|
||
# ** the area of a part of an annulus | ||
|
||
area = 0.5 * (r1 ** 2 - r0 ** 2) * (t1 - t0) | ||
return area | ||
|
||
|
||
def cell_volumes(self): | ||
""" | ||
Return an array of the cell volume data for the given coordinate box | ||
The shape of the returned array is (ni, nj). | ||
""" | ||
|
||
x = self.cell_vertices()[:,0] | ||
y = self.cell_vertices()[0,:] | ||
|
||
r0 = x[:-1, :-1] | ||
r1 = x[+1:, :-1] | ||
t0 = y[:-1, :-1] | ||
t1 = y[+1:, +1:] | ||
|
||
return 0.5 * (r1 ** 2 - r0 ** 2) * (t1 - t0) * (r1 - r0) | ||
|
||
def cell_vertices(self): | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't this already what self.xl and self.xr provide? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not really understand what they are, and decided to write my own function. ;) |
||
Return the coordinates of cell vertices | ||
The arrays range from 0 to 1 for now | ||
""" | ||
# if self.ng == 0: | ||
# xv = np.linspace(0, 1, self.nx + 1)[:-1] | ||
# yv = np.linspace(0, 1, self.ny + 1)[:-1] | ||
# else: | ||
# xv = np.linspace(0, 1, self.nx + 1) | ||
# yv = np.linspace(0, 1, self.ny + 1) | ||
|
||
xv = np.linspace(0, 1, self.nx + 1)[:-1] | ||
yv = np.linspace(0, 1, self.ny + 1)[:-1] | ||
|
||
|
||
tr = lambda arr: arr.transpose(1, 2, 0) | ||
x, y = np.meshgrid(xv, yv, indexing="ij") | ||
return tr(np.array([x, y])) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
if __name__ == "__main__": | ||
do_demo() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need init, since it looks the same as the base class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left if for later if it will be needed to be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to call
super().__init__(nx, ny, ...)
in that case