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

Overhaul radial distance utility #158

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

Conversation

connoramoreno
Copy link
Collaborator

@connoramoreno connoramoreno commented Sep 19, 2024

Overhauls the radial distance utility. Changes include:

  • Leveraging recent updates to the magnet workflow
  • Code restructuring
  • General code improvements
  • Addition of a function to handle all radial distance utility functionality, removing the need to call several functions when using the utility
  • Addition of a function to enforce helical symmetry in a matrix whose rows and columns correspond to regularly spaced angular arrays
  • Addition of a function to smooth a matrix using Gaussian filtering without increasing the value of matrix elements

@connoramoreno
Copy link
Collaborator Author

I'm beginning to wonder if it might be worth converting this script to an object-oriented version

parastell/radial_distance_utils.py Outdated Show resolved Hide resolved
parastell/radial_distance_utils.py Outdated Show resolved Hide resolved
parastell/radial_distance_utils.py Outdated Show resolved Hide resolved
parastell/radial_distance_utils.py Outdated Show resolved Hide resolved
Copy link
Member

@gonuke gonuke left a comment

Choose a reason for hiding this comment

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

I'm really impressed by the application of best practices here @connoramoreno - nice cleanup! I have a few more comments to continue the journey...

parastell/utils.py Outdated Show resolved Hide resolved
def enforce_helical_symmetry(matrix):
"""Ensures that a matrix is helically symmetric according to stellarator
geometry by overwriting certain matrix elements. Assumes regular spacing
between angles defining matrix.
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this assume even more than regular spacing? Doesn't it also assume:

  1. that the matrix represents a full period, and
  2. that there is a rib occurring at the mid-period
  3. that the first rib is at the beginning of a period, ie. a place where poloidal symmetry is expected

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good points. I thought I had written it in a way that (2) wouldn't matter but it seems that's not quite true. I think it's reasonable to assume (1) and (3), but I'd like to modify it to accommodate cases when (2) is not true.

Copy link
Member

Choose a reason for hiding this comment

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

Can we update this docstring to clarify the additional assumptions?

parastell/utils.py Outdated Show resolved Hide resolved
parastell/utils.py Outdated Show resolved Hide resolved
parastell/utils.py Show resolved Hide resolved
parastell/magnet_coils.py Outdated Show resolved Hide resolved
parastell/magnet_coils.py Outdated Show resolved Hide resolved
Examples/radial_distance_example.py Outdated Show resolved Hide resolved
Examples/radial_distance_example.py Outdated Show resolved Hide resolved
Examples/radial_distance_example.py Outdated Show resolved Hide resolved
Copy link
Member

@gonuke gonuke left a comment

Choose a reason for hiding this comment

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

A little final polish...

Comment on lines +433 to +438
if (min_tor_ang >= lower_bound or min_tor_ang <= upper_bound) or (
max_tor_ang >= lower_bound or max_tor_ang <= upper_bound
):
in_toroidal_extent = True
else:
in_toroidal_extent = False
Copy link
Member

Choose a reason for hiding this comment

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

Just learned about chainging comparisons!

Suggested change
if (min_tor_ang >= lower_bound or min_tor_ang <= upper_bound) or (
max_tor_ang >= lower_bound or max_tor_ang <= upper_bound
):
in_toroidal_extent = True
else:
in_toroidal_extent = False
in_toroidal_extent = ( (lower_bound <= min_tor_ang <= upper_bound) or
(lower_bound <= max_tor_ang <= upper_bound) )

def enforce_helical_symmetry(matrix):
"""Ensures that a matrix is helically symmetric according to stellarator
geometry by overwriting certain matrix elements. Assumes regular spacing
between angles defining matrix.
Copy link
Member

Choose a reason for hiding this comment

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

Can we update this docstring to clarify the additional assumptions?

Comment on lines +34 to +35
num_rows = matrix.shape[0]
num_columns = matrix.shape[1]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
num_rows = matrix.shape[0]
num_columns = matrix.shape[1]
num_rows, num_columns = matrix.shape

Comment on lines +38 to +39
for idx in range(num_rows):
matrix[idx, -1] = matrix[idx, 0]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for idx in range(num_rows):
matrix[idx, -1] = matrix[idx, 0]
matrix[:,-1] = matrix[:,0]

Comment on lines +53 to +56
for idx, value in enumerate(
flattened_matrix[: int(len(flattened_matrix) / 2)], start=1
):
flattened_matrix[-idx] = value
Copy link
Member

Choose a reason for hiding this comment

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

Is this what it's doing?

Suggested change
for idx, value in enumerate(
flattened_matrix[: int(len(flattened_matrix) / 2)], start=1
):
flattened_matrix[-idx] = value
half_period = flattened_matrix[0:int(len(flattened_matrix) / 2)]
flattened_matrix = np.concatenate([half_period, np.flip(half_period)])

Returns:
smoothed_matrix (2-D iterable of float): smoothed matrix.
"""
previous_iteration = matrix
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
previous_iteration = matrix
previous_matrix = matrix

smoothed_matrix = np.minimum(
previous_iteration,
gaussian_filter(
previous_iteration,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
previous_iteration,
previous_matrix,

mode="wrap",
),
)
previous_iteration = smoothed_matrix
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
previous_iteration = smoothed_matrix
previous_matrix = smoothed_matrix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants