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 6 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 floats, since we had
# 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 floats, since we had
# 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
25 changes: 14 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,8 @@
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)))
# Note that ~ is an inverse operation (eqv. to np.invert) inverting the boolean array
bol = ~(((S[:-1] < x0bin) * (S[1:] < x0bin)) + ((S[:-1] > xfbin) * (S[1:] > xfbin)))

Check warning on line 991 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L991

Added line #L991 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this change.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is essentially inverting a boolean array. This is a bit cryptic, and perhaps numpys invert would be more readable (see docs here).

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh ok yeah, it is a bit cryptic, you mean flipping the boolean values element-by-element? Could you just expand the comment in the code? Otherwise the PR is ready to go for me.

Copy link
Contributor

Choose a reason for hiding this comment

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

I added a comment on this, but not sure if this is useful. Please take a look. In any case, Ney suggests that this is not a useful function, and we should perhaps deprecate in favor of explicitCart and simpleCart.

bolEdges = E[bol]
# print bol
# Compute edge mismatch and increment variable
Expand All @@ -1013,7 +1013,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 +1045,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 1048 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L1048

Added line #L1048 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 1052 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L1052

Added line #L1052 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 1056 in cgnsutilities/cgnsutilities.py

View check run for this annotation

Codecov / codecov/patch

cgnsutilities/cgnsutilities.py#L1056

Added line #L1056 was not covered by tests

# Print growth ratios
print("")
Expand Down