Skip to content

Commit

Permalink
Make "single_region" option work for non-orthogonal grids
Browse files Browse the repository at this point in the history
  • Loading branch information
johnomotani committed Nov 7, 2024
1 parent 6bf8e26 commit b23d387
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
12 changes: 7 additions & 5 deletions hypnotoad/cases/tokamak.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,6 @@ def inside_wall(point: Point2D):
)
# Select just the single region `this_region_name`
all_regions = {this_region_name: leg_regions[this_region_name]}

# Delete any connections, because we only have a single region
connections = []
else:
# Create a new dictionary, which will contain all regions
# including core and legs
Expand All @@ -795,8 +792,13 @@ def inside_wall(point: Point2D):
self.regions = self.createRegionObjects(all_regions, segments)

# Make the connections between regions
for connection in connections:
self.makeConnection(*connection)
if self.user_options.single_region is not None:
print("making connections for single_region")
for connection in connections:
self.makeSingleRegionConnection(*connection)
else:
for connection in connections:
self.makeConnection(*connection)

def describeSingleNull(self):
"""
Expand Down
26 changes: 26 additions & 0 deletions hypnotoad/core/equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -3910,6 +3910,32 @@ def makeConnection(self, lowerRegion, lowerSegment, upperRegion, upperSegment):
lRegion.connections[lowerSegment]["upper"] = (upperRegion, upperSegment)
uRegion.connections[upperSegment]["lower"] = (lowerRegion, lowerSegment)

def makeSingleRegionConnection(
self, lowerRegion, lowerSegment, upperRegion, upperSegment
):
"""
Make fake connections for a `single_region` grid, if an edge of the region
should be connected in a full grid.
"""
# Needs to be OrderedDict so that Mesh can iterate through it in consistent order
if not isinstance(self.regions, OrderedDict):
raise ValueError("self.regions should be OrderedDict")

single_region_name = [*self.regions.keys()][0]
single_region = self.regions[single_region_name]
if lowerRegion == single_region_name:
if single_region.connections[lowerSegment]["upper"] is not None:
raise ValueError(
"single_region.connections['upper'] should not have been set already"
)
single_region.connections[lowerSegment]["upper"] = ("fake", upperSegment)
elif upperRegion == single_region_name:
if single_region.connections[upperSegment]["lower"] is not None:
raise ValueError(
"single_region.connections['lower'] should not have been set already"
)
single_region.connections[upperSegment]["lower"] = ("fake", lowerSegment)

def handleMultiLocationArray(getResult):
@functools.wraps(getResult)
# Define a function which handles MultiLocationArray arguments
Expand Down
14 changes: 8 additions & 6 deletions hypnotoad/core/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -1383,15 +1383,15 @@ def calcHy(self):
)
hy.centre[i, :] = d[2::2] - d[:-2:2]
hy.ylow[i, 1:-1] = d[3:-1:2] - d[1:-3:2]
if self.connections["lower"] is not None:
if self.connections["lower"] not in (None, "fake"):
cbelow = self.getNeighbour("lower").contours[2 * i + 1]
dbelow = cbelow.get_distance(psi=self.equilibriumRegion.psi)
hy.ylow[i, 0] = d[1] - d[0] + dbelow[-1] - dbelow[-2]
else:
# no region below, so estimate distance to point before '0' as the same
# as from '0' to '1'
hy.ylow[i, 0] = 2.0 * (d[1] - d[0])
if self.connections["upper"] is not None:
if self.connections["upper"] not in (None, "fake"):
cabove = self.getNeighbour("upper").contours[2 * i + 1]
dabove = cabove.get_distance(psi=self.equilibriumRegion.psi)
hy.ylow[i, -1] = d[-1] - d[-2] + dabove[1] - dabove[0]
Expand All @@ -1411,15 +1411,15 @@ def calcHy(self):
)
hy.xlow[i, :] = d[2::2] - d[:-2:2]
hy.corners[i, 1:-1] = d[3:-1:2] - d[1:-3:2]
if self.connections["lower"] is not None:
if self.connections["lower"] not in (None, "fake"):
cbelow = self.getNeighbour("lower").contours[2 * i]
dbelow = cbelow.get_distance(psi=self.equilibriumRegion.psi)
hy.corners[i, 0] = d[1] - d[0] + dbelow[-1] - dbelow[-2]
else:
# no region below, so estimate distance to point before '0' as the same
# as from '0' to '1'
hy.corners[i, 0] = 2.0 * (d[1] - d[0])
if self.connections["upper"] is not None:
if self.connections["upper"] not in (None, "fake"):
cabove = self.getNeighbour("upper").contours[2 * i]
dabove = cabove.get_distance(psi=self.equilibriumRegion.psi)
hy.corners[i, -1] = d[-1] - d[-2] + dabove[1] - dabove[0]
Expand Down Expand Up @@ -2456,7 +2456,7 @@ def _find_intersection(

if upper_wall:
if lower_wall:
starti = len(contour // 2)
starti = len(contour) // 2
else:
starti = 0

Expand Down Expand Up @@ -2692,7 +2692,9 @@ def __init__(self, equilibrium, settings):
region = equilibrium.regions[eq_reg]
c = region.connections[i]
for key, val in c.items():
if val is not None:
if val is not None and val[0] == "fake":
self.connections[region_id][key] = "fake"
elif val is not None:
self.connections[region_id][key] = self.region_lookup[val]
else:
self.connections[region_id][key] = None
Expand Down

0 comments on commit b23d387

Please sign in to comment.