Skip to content

Commit

Permalink
Fix issues found by flake8.
Browse files Browse the repository at this point in the history
+ Avoid ambiguity of numpy import.
+ Correct formatting of comments, dictionary, key-value arguments.
  • Loading branch information
RyanWollaeger committed Jul 19, 2021
1 parent 452282c commit f0eacbc
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 82 deletions.
60 changes: 32 additions & 28 deletions src/mesh/python/mesh_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# ------------------------------------------------------------------------------------------------ #
import numpy as np


# ------------------------------------------------------------------------------------------------ #
# base class (to show required member data)
class base_mesh:
Expand All @@ -12,17 +13,18 @@ class base_mesh:
This contains no methods for creating the data members.
'''
def __init__(self):
#-- required data
# -- required data
self.ndim = 0 # number of dimensions
self.num_nodes = 0 # total number of nodes
self.coordinates_per_node = np.array([]) # coordinate array indexed by node
self.num_cells = 0 # total number of cells
self.num_faces = 0 # total number of oriented faces
self.num_faces_per_cell = np.array([], dtype = int) # number of faces per cell
self.num_nodes_per_face = np.array([], dtype = int) # number of nodes per face
self.faces_per_cell = np.array([[]], dtype = int) # face indexes per cell
self.nodes_per_face = np.array([[]], dtype = int) # node indexes per face
self.nodes_per_side = [np.array([[]], dtype = int)] # list of arrays of node per bdy face
self.num_faces_per_cell = np.array([], dtype=int) # number of faces per cell
self.num_nodes_per_face = np.array([], dtype=int) # number of nodes per face
self.faces_per_cell = np.array([[]], dtype=int) # face indexes per cell
self.nodes_per_face = np.array([[]], dtype=int) # node indexes per face
self.nodes_per_side = [np.array([[]], dtype=int)] # list of arrays of node per bdy face


# ------------------------------------------------------------------------------------------------ #
# orthogonal 2D mesh type
Expand All @@ -34,21 +36,21 @@ class orth_2d_mesh(base_mesh):
'''
def __init__(self, bounds_per_dim, num_cells_per_dim):

#-- short-cuts
# -- short-cuts
nx = num_cells_per_dim[0]
ny = num_cells_per_dim[1]

#-- number of dimensions
# -- number of dimensions
ndim = len(num_cells_per_dim)
self.ndim = ndim
assert (ndim == 2), 'ndim != 2, exiting...'
assert (len(bounds_per_dim) == ndim), 'len(bounds_per_dim) != ndim, exiting...'

#-- create grid arrays along each dimension
# -- create grid arrays along each dimension
grid_per_dim = [np.linspace(bounds_per_dim[i][0], bounds_per_dim[i][1],
num_cells_per_dim[i] + 1) for i in range(ndim)]

#-- create node indices
# -- create node indices
num_nodes = (nx + 1) * (ny + 1)
self.num_nodes = num_nodes
self.coordinates_per_node = np.zeros((num_nodes, ndim))
Expand All @@ -59,28 +61,28 @@ def __init__(self, bounds_per_dim, num_cells_per_dim):
self.coordinates_per_node[node, 0] = grid_per_dim[0][i]
self.coordinates_per_node[node, 1] = grid_per_dim[1][j]

#-- set total number of cells and faces
# -- set total number of cells and faces
num_cells = nx * ny
self.num_cells = num_cells
self.num_faces = 4 * num_cells

#-- constants for this mesh
self.num_faces_per_cell = 4 * np.ones((num_cells), dtype = int)
self.num_nodes_per_face = 2 * np.ones((4 * num_cells), dtype = int)
# -- constants for this mesh
self.num_faces_per_cell = 4 * np.ones((num_cells), dtype=int)
self.num_nodes_per_face = 2 * np.ones((4 * num_cells), dtype=int)

#-- set nodes per face and faces per cell
self.faces_per_cell = np.zeros((num_cells, 4), dtype = int)
self.nodes_per_face = np.zeros((4 * num_cells, 2), dtype = int)
# -- set nodes per face and faces per cell
self.faces_per_cell = np.zeros((num_cells, 4), dtype=int)
self.nodes_per_face = np.zeros((4 * num_cells, 2), dtype=int)
for j in range(ny):
for i in range(nx):
#-- cell index
# -- cell index
cell = i + nx * j
#-- faces per cell
# -- faces per cell
self.faces_per_cell[cell, 0] = 4 * cell
self.faces_per_cell[cell, 1] = 4 * cell + 1
self.faces_per_cell[cell, 2] = 4 * cell + 2
self.faces_per_cell[cell, 3] = 4 * cell + 3
#-- nodes per face (per cell)
# -- nodes per face (per cell)
self.nodes_per_face[4 * cell, 0] = i + j * (nx + 1)
self.nodes_per_face[4 * cell, 1] = i + 1 + j * (nx + 1)
self.nodes_per_face[4 * cell + 1, 0] = i + 1 + j * (nx + 1)
Expand All @@ -90,27 +92,28 @@ def __init__(self, bounds_per_dim, num_cells_per_dim):
self.nodes_per_face[4 * cell + 3, 0] = i + (j + 1) * (nx + 1)
self.nodes_per_face[4 * cell + 3, 1] = i + j * (nx + 1)

#-- enumerate boundary nodes per face ("side")
#-- faces at x extrema
nodes_per_side_xlow = np.zeros((ny, 2), dtype = int)
nodes_per_side_xhig = np.zeros((ny, 2), dtype = int)
# -- enumerate boundary nodes per face ("side")
# -- faces at x extrema
nodes_per_side_xlow = np.zeros((ny, 2), dtype=int)
nodes_per_side_xhig = np.zeros((ny, 2), dtype=int)
for j in range(ny):
nodes_per_side_xlow[ny - 1 - j, 0] = (j + 1) * (nx + 1)
nodes_per_side_xlow[ny - 1 - j, 1] = j * (nx + 1)
nodes_per_side_xhig[j, 0] = j * (nx + 1) + nx
nodes_per_side_xhig[j, 1] = (j + 1) * (nx + 1) + nx
#-- faces at y extrema
nodes_per_side_ylow = np.zeros((nx, 2), dtype = int)
nodes_per_side_yhig = np.zeros((nx, 2), dtype = int)
# -- faces at y extrema
nodes_per_side_ylow = np.zeros((nx, 2), dtype=int)
nodes_per_side_yhig = np.zeros((nx, 2), dtype=int)
for i in range(nx):
nodes_per_side_ylow[i, 0] = i
nodes_per_side_ylow[i, 1] = i + 1
nodes_per_side_yhig[nx - 1 - i, 0] = i + 1 + ny * (nx + 1)
nodes_per_side_yhig[nx - 1 - i, 1] = i + ny * (nx + 1)
#-- compile into one side face array list (expeced as counter-clockwise in 2D)
# -- compile into one side face array list (expeced as counter-clockwise in 2D)
self.nodes_per_side = [nodes_per_side_ylow, nodes_per_side_xhig,
nodes_per_side_yhig, nodes_per_side_xlow]


# ------------------------------------------------------------------------------------------------ #
# orthogonal 3D mesh type
class orth_3d_mesh(base_mesh):
Expand All @@ -122,6 +125,7 @@ class orth_3d_mesh(base_mesh):
def __init__(self, bounds_per_dim, num_cells_per_dim):
assert (False), 'orth_3d_mesh type not yet implemented...'


# ------------------------------------------------------------------------------------------------ #
# end of mesh_types.py
# ------------------------------------------------------------------------------------------------ #
109 changes: 55 additions & 54 deletions src/mesh/python/x3d_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,54 @@
# -------------------------------------------*-python-*------------------------------------------- #
# file src/mesh/python/x3d_generator.py
# ------------------------------------------------------------------------------------------------ #
from mesh_types import *
import mesh_types
import numpy as np
import argparse

#-- mesh class dictionary
mesh_type_dict = {'orth_2d_mesh':orth_2d_mesh}
# -- mesh class dictionary
mesh_type_dict = {'orth_2d_mesh': mesh_types.orth_2d_mesh}

# ------------------------------------------------------------------------------------------------ #
#-- create argument parser
# -- create argument parser

parser = argparse.ArgumentParser(description = 'Generate X3D mesh file.')
parser.add_argument('-mt', '--mesh_type', type = str, default = 'orth_2d_mesh',
help = 'Select mesh type.')
parser.add_argument('-nd', '--num_per_dim', type = int, nargs = '+', default = [1, 1],
help = 'Number of cells per dimension.')
parser.add_argument('-bd', '--bnd_per_dim', type = float, nargs = '+', default = [0.0, 1.0, 0.0, 1.0],
help = 'Length per dimension.')
parser = argparse.ArgumentParser(description='Generate X3D mesh file.')
parser.add_argument('-mt', '--mesh_type', type=str, default='orth_2d_mesh',
help='Select mesh type.')
parser.add_argument('-nd', '--num_per_dim', type=int, nargs='+', default=[1, 1],
help='Number of cells per dimension.')
parser.add_argument('-bd', '--bnd_per_dim', type=float, nargs='+', default=[0.0, 1.0, 0.0, 1.0],
help='Length per dimension.')

#-- parse arguments from command line
# -- parse arguments from command line
args = parser.parse_args()

# ------------------------------------------------------------------------------------------------ #
#-- create the relevant mesh object
# -- create the relevant mesh object

#-- number of spatial dimensions should be length of num_per_dim
# -- number of spatial dimensions should be length of num_per_dim
ndim = len(args.num_per_dim)

#-- sanity check input
# -- sanity check input
assert (ndim > 0), 'len(args.num_per_dim) <= 0'
assert (ndim < 4), 'len(args.num_per_dim) >= 4'
assert (len(args.bnd_per_dim) == 2 * ndim), 'len(args.bnd_per_dim) != 2 * ndim'

#-- de-serialize spatial bound list
# -- de-serialize spatial bound list
bnd_per_dim = [[args.bnd_per_dim[2 * i], args.bnd_per_dim[2 * i + 1]] for i in range(ndim)]

#-- instantiate the class for the mesh type selected
# -- instantiate the class for the mesh type selected
mesh = mesh_type_dict[args.mesh_type](bnd_per_dim, args.num_per_dim)

# ------------------------------------------------------------------------------------------------ #
#-- write out the main mesh file in x3d format
# -- write out the main mesh file in x3d format

#-- open writable file
# -- open writable file
fo = open('x3d.mesh.in', 'w')

#-- write header block
#-- for x3d:
#-- nodes_per_face = max number of nodes per face
#-- faces_per_cell = max number of faces per cell
# -- write header block
# -- for x3d:
# -- nodes_per_face = max number of nodes per face
# -- faces_per_cell = max number of faces per cell
fo.write('ascii\n')
fo.write('header\n')
fo.write(' process 1\n')
Expand All @@ -66,91 +67,92 @@
fo.write(' cell_data_fields 2\n')
fo.write('end_header\n')

#-- space
# -- space
fo.write('\n')

#-- write trivial material block
# -- write trivial material block
fo.write('matnames\n')
fo.write(' 1 0\n')
fo.write('end_matnames\n')

#-- space
# -- space
fo.write('\n')

#-- write trivial material eos block
# -- write trivial material eos block
fo.write('mateos\n')
fo.write(' 1 -1\n')
fo.write('end_mateos\n')

#-- space
# -- space
fo.write('\n')

#-- write trival material opc block
# -- write trival material opc block
fo.write('matopc\n')
fo.write(' 1 -1\n')
fo.write('end_matopc\n')

#-- space
# -- space
fo.write('\n')

#-- write node coordinate block
# -- write node coordinate block
fo.write('nodes\n')
for node in range(mesh.num_nodes):
crds = [0.0, 0.0, 0.0]
for idim in range(mesh.ndim): crds[idim] = mesh.coordinates_per_node[node, idim]
for idim in range(mesh.ndim):
crds[idim] = mesh.coordinates_per_node[node, idim]
fo.write('{0:10d} {1:.15e} {2:.15e} {3:.15e}\n'.format(node + 1, crds[0], crds[1], crds[2]))
fo.write('end_nodes\n')

#-- space
# -- space
fo.write('\n')

#-- write face block (excludes extra columns used for parallel meshes, for now)
# -- write face block (excludes extra columns used for parallel meshes, for now)
fo.write('faces\n')
for cell in range(mesh.num_cells):
for j in range(mesh.num_faces_per_cell[cell]):
#-- get the full face index for the cell and local face index
# -- get the full face index for the cell and local face index
face = mesh.faces_per_cell[cell, j]
#-- get the number of nodes for this face
# -- get the number of nodes for this face
nnpf = mesh.num_nodes_per_face[face]
#-- build face string
# -- build face string
face_str = '{0:10d}{1:10d}'.format(face + 1, nnpf)
#-- append node indices to face string
# -- append node indices to face string
for node in mesh.nodes_per_face[face, :]:
face_str += '{0:10d}'.format(node + 1)
#-- write face line
# -- write face line
fo.write(face_str + '\n')
fo.write('end_faces\n')

#-- space
# -- space
fo.write('\n')

#-- write cell block
# -- write cell block
fo.write('cells\n')
for cell in range(mesh.num_cells):
#-- get the number of faces for this cell
# -- get the number of faces for this cell
nfpc = mesh.num_faces_per_cell[cell]
#-- build cell string
# -- build cell string
cell_str = '{0:10d}{1:10d}'.format(cell + 1, nfpc)
#-- append face indices to cell string
# -- append face indices to cell string
for face in mesh.faces_per_cell[cell, :]:
cell_str += '{0:10d}'.format(face + 1)
#-- write cell line
# -- write cell line
fo.write(cell_str + '\n')
fo.write('end_cells\n')

#-- space
# -- space
fo.write('\n')

#-- unused AMR-type/parallel data
# -- unused AMR-type/parallel data
fo.write('slaved_nodes 0\n')
fo.write('end_slaved_nodes\n')
fo.write('ghost_nodes 0\n')
fo.write('end_ghost_nodes\n')

#-- space
# -- space
fo.write('\n')

#-- unused data per cell
# -- unused data per cell
fo.write('cell_data\n')
fo.write('matid\n')
fo.write(' 0\n')
Expand All @@ -160,18 +162,17 @@
fo.write('end_partelm\n')
fo.write('end_cell_data\n')

#-- close main mesh file
# -- close main mesh file
fo.close()

# ------------------------------------------------------------------------------------------------ #
#-- write out the mesh boundary node lists to file (for boundary conditions)
# -- write out the mesh boundary node lists to file (for boundary conditions)

#-- assume two boundaries per dimension
# -- assume two boundaries per dimension
# -- x3d only needs a unique node list
for i in range(2 * mesh.ndim):
np.savetxt('x3d.mesh.bdy' + str(i + 1) + '.in', np.unique(mesh.nodes_per_side[i] + 1), fmt='%d')

#-- x3d only needs a unique node list

# ------------------------------------------------------------------------------------------------ #
# end of x3d_generator.py
# ------------------------------------------------------------------------------------------------ #

0 comments on commit f0eacbc

Please sign in to comment.