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

Permanent magnet branch merge #296

Merged
merged 226 commits into from
May 26, 2023
Merged

Permanent magnet branch merge #296

merged 226 commits into from
May 26, 2023

Conversation

akaptano
Copy link
Contributor

@akaptano akaptano commented Mar 8, 2023

Hi all,

The permanent magnet branch is looking pretty ready for a merge into the main branch. Some known issues/idiosyncrasies:

  1. I am not quite saving the PermanentMagnetGrid class correctly with the JSON/Optimizable stuff, so I hope to get some feedback on that.
  2. I have made an examples/4_Paper_Reproductions/ folder to hold some of the full-scale, high-resolution permanent magnet run files and files for reproducing my papers. I know this is not typical, but I am wondering if there is a solution other than keeping these files outside the SIMSOPT repo/on another branch, since I worry that they will stagnate relative to the rest of the SIMSOPT code (which develops fast!)
  3. The unit tests for the magnet functionality are relatively thin. This is something I'm happy to address in more detail when I get the chance.

Looking forward to any other thoughts, and thanks for the help!

ejpaul and others added 30 commits January 8, 2022 13:12
… Working on building the grid for the dipoles.
…but learning how to use the code so hopefully is worth it. Still some weirdness going on with the surfaces. Next steps: fix and plot the three surfaces (plasma, inner PM surface, outer PM surface). Then build uniform (r,z) bricks and just loop through and remove any that dont fit between the surfaces. Save list of the centers of the bricks.
…oordinates. Got the grid working between the two toroidal surfaces. Two toroidal surfaces currently being generated by extend_via_normal times some offset value, defaulting to ~10 cm.
…ned up the code and the permanent magnet class, moved it to a new directory.
…al vector that keeps all the points in the same original toroidal plane. Added the MwPGP algorithm from another python project. Fixed the overall geometric factor for creating the A_obj matrix in the optimization. Still having issues fully getting the grid points inside the two toroidal surfaces -- there seems to be small errors when phi approaches 0.25 (pi / 2) and going to investigate this more soon. For now, next step is to solve the convex version with MwPGP and then do some sanity checks, poincare plots, etc.
…ixed some errors in the PM optimization, one of which was that the dipole maxima was not being computed (at all) and then I was using the maxima instead of the maxima **2 in a number of places. Still some work to get the optimization fully working but appears to be doing somewhat reasonable things so far. Things looking pretty good. Next step is to build a MagneticField class for the permanent magnets.
…eticField class. Will debug this thoroughly next week.
…ncare plotter. However, looks like the computation is too slow currently. Working on speeding this up. Also added option to truncate small elements of the linear least squares matrix in the permanent magnet optimization problem.
…s in ATA instead of in |ATA| so all the negative terms would be chopped. Changed _optimize so that all the optimization-specific parameters do not get saved to the permanent magnet object. This is so we can rerun the optimization with different parameters but the same underlying magnet configuration. Added a file (that is in progress) for scanning the hyperparameters.
…nished and not even clear this will work but figure it is worth spending a full day or two to try and see if can get significant speedups with this.
…de a complete mess right now. Need numpy-like operations in the C++ code so added the xtensor-blas library as a submodule. I think I did this basically right but not quite compiling correctly.
…ause I rewrote the dipole field calculation in c++ using simple for loops. Seems to work (added some unit tests) but still appears to be excruciatingly slow in the poincare plot calculations. Not sure what to be done about that. Moved the Dipolefield class to magneticfieldclasses.py, like the Dommaschk class. Will delete the older dipolefield.py file on next commit.
…opefully will be worth the time spent when it is working. So far have written a minimal projected gradient version which is at least running without errors (when openmp off). Lots of work still to do.
…t almost certainly not optimized. Will optimize and clean this up dramatically in the next few days. Also did some linting fixes. The issues I was seeing earlier were related to (1) initializing/setting xtensors inside openmp loops, or (2) doing the same on shared variables (like doubles) so just need to declare these variables as private.
…Now prints out all the errors every 10 iterations. Looks like it works well so far, but have only tested with the various regularizers set to zero.
…ll allows one to switch between the python and c++ versions (for now). Seems to be working well with the L0 norm, although the L0 value is not printed correctly yet. Committing this version now in case I go ahead and cut the python routine + otherwise significantly reduce the python code.
…ld be private. Unfortunately poincare plots still not looking right despite the low Bn errors after optimization.
…o the symmetries. Also found some phis that were missing factors of 2pi... now the optimization is performing much stronger (and hopefully better). The poincare plots appear to be showing something now.
…al angle 2pi values. Fixed B and dB dipole calculations to correctly use private variables. Still having some weird issues, and can no longer compile the code with python setup.py develop (or pip) for some reason. Going to save my progress and try compiling with the main branch.
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.

I have a few comments.

  1. Can you please introduce type hints and minimize the if-else checks on the arguments?
  2. Check the alignment of docstrings. Don't indent them further than the indent of """.
  3. This PR and Non-XSIMD implementation of Biot Savart kernel for PowerPC #292 don't go together. We need non-XSIMD implementation for the XSIMD-based C++ code added here. First, let's merge this and then we can update Non-XSIMD implementation of Biot Savart kernel for PowerPC #292.

src/simsopt/geo/permanent_magnet_grid.py Outdated Show resolved Hide resolved
src/simsopt/geo/permanent_magnet_grid.py Outdated Show resolved Hide resolved
src/simsopt/geo/permanent_magnet_grid.py Outdated Show resolved Hide resolved
src/simsopt/geo/permanent_magnet_grid.py Outdated Show resolved Hide resolved
src/simsopt/geo/permanent_magnet_grid.py Show resolved Hide resolved
src/simsopt/util/famus_helpers.py Show resolved Hide resolved
src/simsopt/util/famus_helpers.py Outdated Show resolved Hide resolved
src/simsopt/util/permanent_magnet_helper_functions.py Outdated Show resolved Hide resolved
src/simsopt/util/polarization_project.py Outdated Show resolved Hide resolved
@mbkumar
Copy link
Collaborator

mbkumar commented Apr 28, 2023

I'll take one more pass at this PR sometime this weekend.

examples/2_Intermediate/permanent_magnet_MUSE.py Outdated Show resolved Hide resolved
examples/2_Intermediate/permanent_magnet_NCSX.py Outdated Show resolved Hide resolved
examples/2_Intermediate/permanent_magnet_PM4Stell.py Outdated Show resolved Hide resolved
examples/2_Intermediate/permanent_magnet_QA.py Outdated Show resolved Hide resolved
examples/2_Intermediate/permanent_magnet_MUSE.py Outdated Show resolved Hide resolved
tests/field/test_magneticfields.py Outdated Show resolved Hide resolved
tests/field/test_magneticfields.py Outdated Show resolved Hide resolved
tests/field/test_magneticfields.py Outdated Show resolved Hide resolved
tests/field/test_magneticfields.py Outdated Show resolved Hide resolved
src/simsoptpp/dipole_field.h Outdated Show resolved Hide resolved
@mbkumar
Copy link
Collaborator

mbkumar commented May 9, 2023

@akaptano

Recently, simsopt port without XSIMD dependency is merged into master. A new test is added to check against this. Make sure your C++ code can work without XSIMD. If required I can take a stab at this.

@mbkumar
Copy link
Collaborator

mbkumar commented May 11, 2023

@akaptano

Integrated tests (aka examples) are taking more than 3 hours. Can you find a way to reduce the execution time? We limit the iteration numbers and adjust the stopping criteria to speed up the execution in CI. You can leave notes and commented-out code in the examples for users to run at full scale.

akaptano added 7 commits May 19, 2023 18:02
…d Bharat in latest round. Added some comments. Refactored the PermanentMagnetGrid so that if initialized from famus, dont need the rz_inner_surface and outer surface objects, which makes more sense. Converted some of the if statements to type casts. Going to merge with latest main and recheck all the tests now.
…d the resolution in test_pm_grid. Going to push this now and see if it works in the online CI.
…ne suddenly. Lets see what online CI does now.
…. Hoping this all passes now and runs much quicker. Required downsampling to be implemented in the two ways to load FAMUS files, since optimizing big grids from FAMUS looked like the main bottleneck.
@akaptano
Copy link
Contributor Author

Hi all, I think I have addressed the various comments/changes and things are passing now. Let me know if there are last changes and thanks for your help!

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.

Hi Alan,

Please go throw my comments and make the required changes. The changes needed will help in long-term maintainability.

examples/2_Intermediate/permanent_magnet_MUSE.py Outdated Show resolved Hide resolved
examples/2_Intermediate/permanent_magnet_NCSX.py Outdated Show resolved Hide resolved
examples/2_Intermediate/permanent_magnet_NCSX.py Outdated Show resolved Hide resolved
src/simsopt/util/permanent_magnet_helper_functions.py Outdated Show resolved Hide resolved
src/simsopt/util/permanent_magnet_helper_functions.py Outdated Show resolved Hide resolved
src/simsopt/util/permanent_magnet_helper_functions.py Outdated Show resolved Hide resolved
src/simsopt/util/polarization_project.py Outdated Show resolved Hide resolved
tests/geo/test_pm_grid.py Outdated Show resolved Hide resolved
@akaptano
Copy link
Contributor Author

Pushing up your request changes shortly, thanks!

…ed changing the initialization of the PermanentMagnetGrid to using geo_setup functions as classmethods, adding comments, using pathlib wherever possible, among other various comments. Updated all the examples and tests to work with the new grid initialization.
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.

Hello Alan,

Thanks for making the required changes. I am almost ready to approve this PR. I have a few requests in a couple of places. Could you please address them?

Thank you,
Bharat

examples/2_Intermediate/permanent_magnet_PM4Stell.py Outdated Show resolved Hide resolved
src/simsopt/solve/permanent_magnet_optimization.py Outdated Show resolved Hide resolved
src/simsopt/solve/permanent_magnet_optimization.py Outdated Show resolved Hide resolved
src/simsopt/util/permanent_magnet_helper_functions.py Outdated Show resolved Hide resolved
@akaptano
Copy link
Contributor Author

Pushing up these changes in a moment, thanks!

@akaptano
Copy link
Contributor Author

Looks like only a docker error, related to VMEC installation (maybe I broke something on my recent push to VMEC2000/ that added config files for Perlmutter?) remains. Let me know last steps and thanks for the help!

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.

Hello Alan,

Thanks for the changes. I am signing off on this PR.

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.

Hello Alan,

Thanks for the changes. I am signing off on this PR.

@mbkumar mbkumar merged commit 023f7e5 into master May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants