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

Virtual casing #209

Merged
merged 57 commits into from
May 9, 2022
Merged

Virtual casing #209

merged 57 commits into from
May 9, 2022

Conversation

landreman
Copy link
Contributor

This PR provides an interface to the virtual_casing package. This will be used to do coil optimization for finite-beta plasmas, replacing the BNORM code in stellopt. The new functions in simsopt adds lots of functionality to virtual_casing: providing an interface to Vmec, more tests, ability to save and load results, resample the results to the resolution desired for the stage-2 optimization, plot results, and suggest resolution parameters that minimize grid anisotropy on the surface. The virtual_casing module has also been added to the simsopt containers.

Typical usage:

    from simsopt.mhd.virtual_casing import VirtualCasing
    VirtualCasing.from_vmec('wout_li383.nc', nphi=300)

@landreman landreman requested a review from mbkumar March 21, 2022 10:23
@codecov
Copy link

codecov bot commented Mar 21, 2022

Codecov Report

Merging #209 (d09a68c) into master (f56cc17) will increase coverage by 0.21%.
The diff coverage is 98.40%.

@@            Coverage Diff             @@
##           master     #209      +/-   ##
==========================================
+ Coverage   91.01%   91.23%   +0.21%     
==========================================
  Files          60       62       +2     
  Lines        7900     8131     +231     
==========================================
+ Hits         7190     7418     +228     
- Misses        710      713       +3     
Flag Coverage Δ
unittests 91.23% <98.40%> (+0.21%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/simsopt/mhd/vmec_diagnostics.py 99.03% <94.73%> (-0.18%) ⬇️
src/simsopt/field/coil.py 98.86% <97.50%> (+0.53%) ⬆️
src/simsopt/mhd/virtual_casing.py 98.80% <98.80%> (ø)
src/simsopt/geo/surface.py 88.81% <100.00%> (+0.18%) ⬆️
src/simsopt/mhd/vmec.py 93.02% <100.00%> (+0.08%) ⬆️
src/simsopt/util/fourier_interpolation.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f56cc17...d09a68c. Read the comment docs.

# Only the phi resolution needs to be specified. The theta resolution
# is computed automatically to minimize anisotropy of the grid.
vc = VirtualCasing.from_vmec(filename, nphi=180)

Copy link
Collaborator

Choose a reason for hiding this comment

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

This example would be confusing to the users because it appears that nothing is happening after instantiating the VirtualCasing class. My suggestion would to be print some of the important quantities and add comments stating how those quantities are used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A bit more is done now in this example.

directory, basefile = os.path.split(vmec.output_file)
filename = os.path.join(directory, 'vcasing' + basefile[4:])
logger.debug(f'New filename: {filename}')
vc.save(filename)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd not call VirtualCasing.save automatically and instead ask the users to explicitly make a call to save the data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd prefer to keep saving as the default. The reason is that 99% of the time, the use case is to run virtual casing once, save the result, and load in the result many times for stage-2 coil optimizations with different parameters. If a user ever wants to not save a file, they can set filename=None when calling VirtualCasing.from_vmec().

src/simsopt/mhd/virtual_casing.py Outdated Show resolved Hide resolved
index += 1
"""

Btotal_normal = np.sum(B3d * unit_normal, axis=2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Btotal_normal is not used anywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. That line has been removed.

src/simsopt/mhd/virtual_casing.py Outdated Show resolved Hide resolved
newvc.__setattr__(variable, newvar)
return newvc

def plot(self, show=True):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Saving the fig is not enabled. Also, the approach used in the plot method is archaic. Instead, OO-approach should be used to make it more convenient for end-users.

Please refer to https://matplotlib.org/stable/tutorials/introductory/lifecycle.html

Copy link
Collaborator

Choose a reason for hiding this comment

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

My suggestion is to make plot return fig, axes objects so users can further customize the plots. Then they can decide whether to save or show them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This has been addressed.

@florianwechsung
Copy link
Contributor

Discussion in Princeton:

  • interpolation should take angle shift into account
  • vmec object should return sum of currents
  • only optimize for ncoil-1 currents, set the last one to I_total - \sum_remaining I_i

@jimlobsien
Copy link

Here is a list of what I think should be implemented in simsopt concerning the virtual casing principle:

  • Component of the internal currents on the boundary (work in progress)
  • Volumetric internal currents on a mesh surrounding the plasma boundary

Use-cases for the later (approximating finite beta equilibrium):

  • Poincare plot of coils plus internal currents
  • Derivation of a current distribution on a current carrying surface
  • Derivation of properties of the approximated finite beta magnetic field
  • Coil optimization with these properties

@florianwechsung
Copy link
Contributor

I think the functionality is all here now. Remaining todo is to fix the docstrings in the virtual_casing class and in the w7x example

@florianwechsung
Copy link
Contributor

Docs updated. @landreman @mbkumar I think this is ready for review.

@landreman
Copy link
Contributor Author

Thanks for all your work on this, @florianwechsung . I'm happy for it to be merged now.

Copy link
Collaborator

@mbkumar mbkumar left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@mbkumar mbkumar merged commit 5bd9e63 into master May 9, 2022
@florianwechsung florianwechsung deleted the ml/virtual_casing branch June 3, 2022 21:50
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.

4 participants