|
34 | 34 | import h5py |
35 | 35 | import numpy as np |
36 | 36 |
|
37 | | -import pyro.mesh.boundary as bnd |
38 | | -from pyro.mesh.array_indexer import ArrayIndexer, ArrayIndexerFC |
39 | | -from pyro.util import msg |
| 37 | +# import pyro.mesh.boundary as bnd |
| 38 | +# from pyro.mesh.array_indexer import ArrayIndexer, ArrayIndexerFC |
| 39 | +# from pyro.util import msg |
40 | 40 |
|
41 | 41 |
|
42 | 42 | class Grid2d: |
@@ -885,5 +885,174 @@ def do_demo(): |
885 | 885 | mydata.pretty_print("a") |
886 | 886 |
|
887 | 887 |
|
888 | | -if __name__ == "__main__": |
889 | | - do_demo() |
| 888 | +# **c checking |
| 889 | +class PolarGrid(Grid2d): |
| 890 | + """ |
| 891 | + the 2-d grid class. The grid object will contain the coordinate |
| 892 | + information (at various centerings). |
| 893 | +
|
| 894 | + A basic (1-d) representation of the layout is:: |
| 895 | +
|
| 896 | + | | | X | | | | X | | | |
| 897 | + +--*--+- // -+--*--X--*--+--*--+- // -+--*--+--*--X--*--+- // -+--*--+ |
| 898 | + 0 ng-1 ng ng+1 ... ng+nx-1 ng+nx 2ng+nx-1 |
| 899 | +
|
| 900 | + ilo ihi |
| 901 | +
|
| 902 | + |<- ng guardcells->|<---- nx interior zones ----->|<- ng guardcells->| |
| 903 | +
|
| 904 | + The '*' marks the data locations. |
| 905 | + """ |
| 906 | + |
| 907 | + # pylint: disable=too-many-instance-attributes |
| 908 | + |
| 909 | + def __init__(self, nx, ny, ng=1, |
| 910 | + xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0): |
| 911 | + |
| 912 | + """ |
| 913 | + Create a PolarGrid object. |
| 914 | +
|
| 915 | + The only data that we require is the number of points that |
| 916 | + make up the mesh in each direction. Optionally we take the |
| 917 | + extrema of the domain (default is [0,1]x[0,1]) and number of |
| 918 | + ghost cells (default is 1). |
| 919 | +
|
| 920 | + Note that the Grid2d object only defines the discretization, |
| 921 | + it does not know about the boundary conditions, as these can |
| 922 | + vary depending on the variable. |
| 923 | +
|
| 924 | + Parameters |
| 925 | + ---------- |
| 926 | + nx : int |
| 927 | + Number of zones in the r-direction |
| 928 | + ny : int |
| 929 | + Number of zones in the theta-direction |
| 930 | + ng : int, optional |
| 931 | + Number of ghost cells |
| 932 | + xmin : float, optional |
| 933 | + Physical coordinate at the lower x boundary |
| 934 | + xmax : float, optional |
| 935 | + Physical coordinate at the upper x boundary |
| 936 | + ymin : float, optional |
| 937 | + Physical coordinate at the lower y boundary |
| 938 | + ymax : float, optional |
| 939 | + Physical coordinate at the upper y boundary |
| 940 | + """ |
| 941 | + |
| 942 | + # pylint: disable=too-many-arguments |
| 943 | + |
| 944 | + # size of grid |
| 945 | + self.nx = int(nx) |
| 946 | + self.ny = int(ny) |
| 947 | + self.ng = int(ng) |
| 948 | + |
| 949 | + self.qx = int(2*ng + nx) |
| 950 | + self.qy = int(2*ng + ny) |
| 951 | + |
| 952 | + # domain extrema |
| 953 | + self.xmin = xmin |
| 954 | + self.xmax = xmax |
| 955 | + |
| 956 | + self.ymin = ymin |
| 957 | + self.ymax = ymax |
| 958 | + |
| 959 | + # compute the indices of the block interior (excluding guardcells) |
| 960 | + self.ilo = self.ng |
| 961 | + self.ihi = self.ng + self.nx-1 |
| 962 | + |
| 963 | + self.jlo = self.ng |
| 964 | + self.jhi = self.ng + self.ny-1 |
| 965 | + |
| 966 | + # center of the grid (for convenience) |
| 967 | + self.ic = self.ilo + self.nx//2 - 1 |
| 968 | + self.jc = self.jlo + self.ny//2 - 1 |
| 969 | + |
| 970 | + # define the coordinate information at the left, center, and right |
| 971 | + # zone coordinates |
| 972 | + self.dx = (xmax - xmin)/nx |
| 973 | + |
| 974 | + self.xl = (np.arange(self.qx) - ng)*self.dx + xmin |
| 975 | + self.xr = (np.arange(self.qx) + 1.0 - ng)*self.dx + xmin |
| 976 | + self.x = 0.5*(self.xl + self.xr) |
| 977 | + |
| 978 | + self.dy = (ymax - ymin)/ny |
| 979 | + |
| 980 | + self.yl = (np.arange(self.qy) - ng)*self.dy + ymin |
| 981 | + self.yr = (np.arange(self.qy) + 1.0 - ng)*self.dy + ymin |
| 982 | + self.y = 0.5*(self.yl + self.yr) |
| 983 | + |
| 984 | + # 2-d versions of the zone coordinates (replace with meshgrid?) |
| 985 | + x2d = np.repeat(self.x, self.qy) |
| 986 | + x2d.shape = (self.qx, self.qy) |
| 987 | + self.x2d = x2d |
| 988 | + |
| 989 | + y2d = np.repeat(self.y, self.qx) |
| 990 | + y2d.shape = (self.qy, self.qx) |
| 991 | + y2d = np.transpose(y2d) |
| 992 | + self.y2d = y2d |
| 993 | + |
| 994 | + |
| 995 | + def face_area(self): |
| 996 | + """ |
| 997 | + Return an array of the face areas. |
| 998 | + The shape of the returned array is (ni, nj). |
| 999 | + """ |
| 1000 | + tr = lambda arr: arr.transpose(1, 2, 0) |
| 1001 | + x = self.cell_vertices()[:,0] |
| 1002 | + y = self.cell_vertices()[0,:] |
| 1003 | + r0 = x[:-1, :-1] |
| 1004 | + r1 = x[+1:, :-1] |
| 1005 | + t0 = y[:-1, :-1] |
| 1006 | + t1 = y[+1:, +1:] |
| 1007 | + |
| 1008 | + # ** the area of the arc |
| 1009 | + |
| 1010 | + 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))) |
| 1011 | + 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))) |
| 1012 | + return tr(np.array([area_i, area_j])) |
| 1013 | + |
| 1014 | + def cell_volumes(self): |
| 1015 | + """ |
| 1016 | + Return an array of the cell volume data for the given coordinate box |
| 1017 | + The shape of the returned array is (ni, nj). |
| 1018 | + """ |
| 1019 | + |
| 1020 | + x = self.cell_vertices()[:,0] |
| 1021 | + y = self.cell_vertices()[0,:] |
| 1022 | + |
| 1023 | + r0 = x[:-1, :-1] |
| 1024 | + r1 = x[+1:, :-1] |
| 1025 | + t0 = y[:-1, :-1] |
| 1026 | + t1 = y[+1:, +1:] |
| 1027 | + |
| 1028 | + return (r1 ** 3 - r0 ** 3) * (np.cos(t1) - np.cos(t0)) * (-2.0 * np.pi) / 3.0 |
| 1029 | + # return r1 |
| 1030 | + |
| 1031 | + def cell_vertices(self): |
| 1032 | + """ |
| 1033 | + Return the coordinates of cell vertices |
| 1034 | + The arrays range from 0 to 1 for now |
| 1035 | + """ |
| 1036 | + # if self.ng == 0: |
| 1037 | + # xv = np.linspace(0, 1, self.nx + 1)[:-1] |
| 1038 | + # yv = np.linspace(0, 1, self.ny + 1)[:-1] |
| 1039 | + # else: |
| 1040 | + # xv = np.linspace(0, 1, self.nx + 1) |
| 1041 | + # yv = np.linspace(0, 1, self.ny + 1) |
| 1042 | + |
| 1043 | + xv = np.linspace(0, 1, self.nx + 1)[:-1] |
| 1044 | + yv = np.linspace(0, 1, self.ny + 1)[:-1] |
| 1045 | + |
| 1046 | + |
| 1047 | + tr = lambda arr: arr.transpose(1, 2, 0) |
| 1048 | + x, y = np.meshgrid(xv, yv, indexing="ij") |
| 1049 | + return tr(np.array([x, y])) |
| 1050 | + |
| 1051 | + |
| 1052 | + |
| 1053 | + |
| 1054 | + |
| 1055 | + |
| 1056 | + |
| 1057 | +# if __name__ == "__main__": |
| 1058 | +# do_demo() |
0 commit comments