Skip to content
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

Adjusted cartesian mesh generation for 2D meshes #66

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 90 additions & 18 deletions cgnsutilities/cgns_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,27 +493,77 @@
)

# ------------ Options for 'simpleCart' mode --------------------
p_sub = subparsers.add_parser("simpleCart", help="Generates a background cartesian mesh")
p_sub = subparsers.add_parser(

Check warning on line 496 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L496

Added line #L496 was not covered by tests
"simpleCart",
help="""Generates a background cartesian mesh containing two
regions: one internal region with uniform spacing dh
and an extended region where mesh sizes grow following
a geometric progression. The internal region is
defined by the bounding box of the mesh present in gridFile.""",
formatter_class=argparse.RawTextHelpFormatter,
)
p_sub.add_argument("gridFile", help="Name of input CGNS file")
p_sub.add_argument("dh", help="Uniform spacing size", type=float)
p_sub.add_argument("hExtra", help="Extension in each dimension", type=float)
p_sub.add_argument("nExtra", help="Number of nodes to use for extension", type=int)
p_sub.add_argument("sym", help="Normal for possible sym plane", type=str)
p_sub.add_argument(

Check warning on line 506 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L506

Added line #L506 was not covered by tests
"dh",
help="""Uniform spacing size. If a single number is provided,
the same spacing is applied to all three dimensions (x,y,z).
But the user can also provide a set of three spacings
separated by commas, for example: 0.1,0.2,0.3.""",
type=str,
)
p_sub.add_argument("hExtra", help="Length of the extended region", type=float)
p_sub.add_argument("nExtra", help="Number of nodes of the extended region", type=int)
p_sub.add_argument(

Check warning on line 516 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L514-L516

Added lines #L514 - L516 were not covered by tests
"sym",
help="""Normal for possible sym planes.
Possible options are xmin, ymin, zmin, xmax, ymax, zmax.
The user can also define a combination of them using commas,
for example: zmin,zmax. This is useful to generate 1-cell wide
meshes for 2D cases (remember to set dh to the span length
(mesh width) and mgcycle to 0).
""",
type=str,
)
p_sub.add_argument("mgcycle", help="Minimum MG cycle to enforce", type=int)
p_sub.add_argument("outFile", help="Name of output CGNS file")

# ------------ Options for 'explicitCart' mode --------------------
p_sub = subparsers.add_parser("explicitCart", help="Generates a background cartesian mesh")
p_sub.add_argument("xmin", type=float, help="min x coordinate")
p_sub.add_argument("ymin", type=float, help="min y coordinate")
p_sub.add_argument("zmin", type=float, help="min z coordinate")
p_sub.add_argument("xmax", type=float, help="max x coordinate")
p_sub.add_argument("ymax", type=float, help="max y coordinate")
p_sub.add_argument("zmax", type=float, help="max z coordinate")
p_sub.add_argument("dh", help="Uniform spacing size", type=float)
p_sub.add_argument("hExtra", help="Extension in each dimension", type=float)
p_sub.add_argument("nExtra", help="Number of nodes to use for extension", type=int)
p_sub.add_argument("sym", help="Normal for possible sym plane", type=str)
p_sub = subparsers.add_parser(

Check warning on line 531 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L531

Added line #L531 was not covered by tests
"explicitCart",
help="""Generates a background cartesian mesh containing two
regions: one internal region with uniform spacing dh
and an extended region where mesh sizes grow following
a geometric progression. The internal region is
defined by the x,y,z coordinates given as explicit arguments.""",
formatter_class=argparse.RawTextHelpFormatter,
)
p_sub.add_argument("xmin", type=float, help="min x coordinate of the internal region")
p_sub.add_argument("ymin", type=float, help="min y coordinate of the internal region")
p_sub.add_argument("zmin", type=float, help="min z coordinate of the internal region")
p_sub.add_argument("xmax", type=float, help="max x coordinate of the internal region")
p_sub.add_argument("ymax", type=float, help="max y coordinate of the internal region")
p_sub.add_argument("zmax", type=float, help="max z coordinate of the internal region")
p_sub.add_argument(

Check warning on line 546 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L540-L546

Added lines #L540 - L546 were not covered by tests
"dh",
help="""Uniform spacing size. If a single number is provided,
the same spacing is applied to all three dimensions (x,y,z).
But the user can also provide a set of three spacings
separated by commas, for example: 0.1,0.2,0.3.""",
type=str,
)
p_sub.add_argument("hExtra", help="Length of the extended region", type=float)
p_sub.add_argument("nExtra", help="Number of nodes of the extended region", type=int)
p_sub.add_argument(

Check warning on line 556 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L554-L556

Added lines #L554 - L556 were not covered by tests
"sym",
help="""Normal for possible sym planes.
Possible options are xmin, ymin, zmin, xmax, ymax, zmax.
The user can also define a combination of them using commas,
for example: zmin,zmax. This is useful to generate 1-cell wide
meshes for 2D cases (remember to set dh to the span length
(mesh width) and mgcycle to 0).
""",
type=str,
)
p_sub.add_argument("mgcycle", help="Minimum MG cycle to enforce", type=int)
p_sub.add_argument("outFile", help="Name of output CGNS file")

Expand Down Expand Up @@ -784,7 +834,18 @@
xMin = [args.xmin, args.ymin, args.zmin]
xMax = [args.xmax, args.ymax, args.zmax]

simpleCart(xMin, xMax, args.dh, args.hExtra, args.nExtra, args.sym, args.mgcycle, args.outFile)
# Change dh to a list of integers, since we had
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment mentions int, but the type casting below is float.

# to define it as a string in arg_parser to avoid
# issues with variable number of inputs.
# (The user could give a single dh, or a set of three values)
dh = list(map(float, args.dh.split(",")))
if len(dh) == 1:
dh = dh[0]

Check warning on line 843 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L841-L843

Added lines #L841 - L843 were not covered by tests

# A similar procedure is necessary for the symmetry planes argument
sym = args.sym.split(",")

Check warning on line 846 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L846

Added line #L846 was not covered by tests

simpleCart(xMin, xMax, dh, args.hExtra, args.nExtra, sym, args.mgcycle, args.outFile)

Check warning on line 848 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L848

Added line #L848 was not covered by tests

sys.exit(0)

Expand Down Expand Up @@ -922,7 +983,18 @@
sys.exit(0)

elif args.mode == "simpleCart":
curGrid.simpleCart(args.dh, args.hExtra, args.nExtra, args.sym, args.mgcycle, args.outFile)
# Change dh to a list of integers, since we had
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

# to define it as a string in arg_parser to avoid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the new code here is a duplication from explicitCart. Refactoring this into a function processing the cart inputs would be great.

# issues with variable number of inputs.
# (The user could give a single dh, or a set of three values)
dh = list(map(float, args.dh.split(",")))
if len(dh) == 1:
dh = dh[0]

Check warning on line 992 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L990-L992

Added lines #L990 - L992 were not covered by tests

# A similar procedure is necessary for the symmetry planes argument
sym = args.sym.split(",")

Check warning on line 995 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L995

Added line #L995 was not covered by tests

curGrid.simpleCart(dh, args.hExtra, args.nExtra, sym, args.mgcycle, args.outFile)

Check warning on line 997 in cgnsutilities/cgns_utils.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgns_utils.py#L997

Added line #L997 was not covered by tests
sys.exit(0)

elif args.mode == "translate":
Expand Down
24 changes: 13 additions & 11 deletions cgnsutilities/cgnsutilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,10 @@
print("Running Cartesian grid generator")

# Preallocate arrays
extensions = np.zeros((2, 3), order="F")
nNodes = np.zeros(3, order="F")
weightGR = np.zeros(3, order="F")
numBins = np.zeros(3, order="F")
extensions = np.zeros((2, 3), dtype=float)
nNodes = np.zeros(3, dtype=int)
weightGR = np.zeros(3, dtype=float)
numBins = np.zeros(3, dtype=int)

Check warning on line 697 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L694-L697

Added lines #L694 - L697 were not covered by tests

# Read four lines of the cartesian specs file
with open(cartFile, "r") as f:
Expand Down Expand Up @@ -844,8 +844,7 @@
# Define tangent bunching law
def tanDist(Sp1, Sp2, N):
"""
This is the tangential spacing developed by Ney Secco.
This bunching law is coarse at the ends and fine at the middle
The tangential spacing is coarse at the ends and fine at the middle
of the interval, just like shown below:
| | | | || | | | |

Expand Down Expand Up @@ -988,7 +987,7 @@
x0bin = xmin + dxBin * binIndex
xfbin = xmin + dxBin * (binIndex + 1)
# Find cells that touch this interval and get their edges
bol = -(((S[:-1] < x0bin) * (S[1:] < x0bin)) + ((S[:-1] > xfbin) * (S[1:] > xfbin)))
bol = ~(((S[:-1] < x0bin) * (S[1:] < x0bin)) + ((S[:-1] > xfbin) * (S[1:] > xfbin)))

Check warning on line 990 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L990

Added line #L990 was not covered by tests
bolEdges = E[bol]
# print bol
# Compute edge mismatch and increment variable
Expand All @@ -1013,7 +1012,10 @@

# Optimize
res = minimize(
func, x_start, method="Nelder-Mead", options={"maxiter": 2000, "disp": True, "xtol": 1e-8, "ftol": 1e-8}
func,
x_start,
method="Nelder-Mead",
options={"maxiter": 2000, "disp": True, "xatol": 1e-8, "fatol": 1e-8},
)

# Split variables
Expand Down Expand Up @@ -1042,15 +1044,15 @@
if nNodes[0] > 3:
gx = max((Sx[1] - Sx[0]) / (Sx[2] - Sx[1]), (Sx[-1] - Sx[-2]) / (Sx[-2] - Sx[-3]))
else:
gx = None
gx = 0.0

Check warning on line 1047 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L1047

Added line #L1047 was not covered by tests
if nNodes[1] > 3:
gy = max((Sy[1] - Sy[0]) / (Sy[2] - Sy[1]), (Sy[-1] - Sy[-2]) / (Sy[-2] - Sy[-3]))
else:
gy = None
gy = 0.0

Check warning on line 1051 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L1051

Added line #L1051 was not covered by tests
if nNodes[2] > 3:
gz = max((Sz[1] - Sz[0]) / (Sz[2] - Sz[1]), (Sz[-1] - Sz[-2]) / (Sz[-2] - Sz[-3]))
else:
gz = None
gz = 0.0

Check warning on line 1055 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L1055

Added line #L1055 was not covered by tests

# Print growth ratios
print("")
Expand Down