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

Leg only grid #187

Open
wants to merge 5 commits into
base: master
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
1 change: 1 addition & 0 deletions doc/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Release history
of the grid with the wall. This enables immersed boundary conditions.
- Wall coordinates are written to output grid as `closed_wall_R` and `closed_wall_Z`
(#176)
- Build grid for only a divertor leg region (#187).

0.5.2 (13th March 2023)
-------------------------
Expand Down
38 changes: 31 additions & 7 deletions hypnotoad/cases/tokamak.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ class TokamakEquilibrium(Equilibrium):
doc="Reverse the sign of toroidal magnetic field Bt.",
value_type=bool,
),
single_region=WithMeta(
None,
doc="Select only a single region of the equilibrium to mesh. Currently "
"this must be a divertor leg region.",
value_type=[NoneType, str],
),
start_at_upper_outer=WithMeta(
False,
doc=(
Expand Down Expand Up @@ -767,17 +773,32 @@ def inside_wall(point: Point2D):
# Specifications for a double null (connected or disconnected)
leg_regions, core_regions, segments, connections = self.describeDoubleNull()

# Create a new dictionary, which will contain all regions
# including core and legs
all_regions = leg_regions.copy()
all_regions.update(self.coreRegionToRegion(core_regions))
if self.user_options.single_region is not None:
this_region_name = self.user_options.single_region
if this_region_name not in leg_regions:
raise ValueError(
f"single_region option only supports leg regions so far. Region "
f"{this_region_name} not found in leg_regions {leg_regions.keys()}."
)
# Select just the single region `this_region_name`
all_regions = {this_region_name: leg_regions[this_region_name]}
else:
# Create a new dictionary, which will contain all regions
# including core and legs
all_regions = leg_regions.copy()
all_regions.update(self.coreRegionToRegion(core_regions))

# Create the regions in an OrderedDict, assign to self.regions
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 Expand Up @@ -1635,7 +1656,10 @@ def createRegionObjects(self, all_regions, segments):
# The region objects need to be sorted, so that the
# BoutMesh generator can use jyseps indices to introduce branch cuts

if "inner_lower_divertor" in region_objects:
if self.user_options.single_region is not None:
# Only a single region present
ordering = [self.user_options.single_region]
elif "inner_lower_divertor" in region_objects:
if not self.user_options.start_at_upper_outer:
ordering = [
"inner_lower_divertor",
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
Loading
Loading