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

Strain optimization #346

Merged
merged 48 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
2273bf8
first commit for implementing accurate self field
phuslage May 26, 2023
5ab839b
First commit on coil_forces branch
phuslage Jun 6, 2023
1b0a95c
initial commit
phuslage Jun 6, 2023
98ab424
initial commit for strain optimization
phuslage Jun 6, 2023
b96555d
class file for frenet frame finite build
phuslage Jun 6, 2023
3e2c0c5
make basic example work
phuslage Jun 6, 2023
ca39c78
cleanups & comments
phuslage Jun 13, 2023
051abb3
fix typo
phuslage Jun 13, 2023
1e748b4
Initial refactoring.
ejpaul Aug 2, 2023
bf70cfa
Cleanup and refactoring.
ejpaul Aug 4, 2023
d8debdd
Refactoring of strain optimization classes.
ejpaul Aug 6, 2023
d09d909
Adding more documentation.
ejpaul Aug 7, 2023
818d041
Autopep.
ejpaul Aug 7, 2023
87e8e7e
Tests now passing.
ejpaul Aug 8, 2023
72632ae
Autopep.
ejpaul Aug 8, 2023
9926c78
Added unit tests for strains.
ejpaul Aug 8, 2023
acd058f
Added unit tests for strains.
ejpaul Aug 8, 2023
51674cf
Added simple example.
ejpaul Aug 8, 2023
e9f154f
Removed old example.
ejpaul Aug 8, 2023
bff6c2c
Small cleanup.
ejpaul Aug 8, 2023
ee25256
Fix to test.
ejpaul Aug 8, 2023
7ebe834
Delete biotsavart_mod.py
phuslage Aug 16, 2023
1d790b9
Delete self_field.py
phuslage Aug 16, 2023
645add2
minor changes
phuslage Sep 1, 2023
b1c9c59
more minor changes
phuslage Sep 1, 2023
f698fc8
Fix some docs formatting. Renamed strain examples
landreman Sep 2, 2023
8d3e607
Add strain_optimization example to CI
landreman Sep 2, 2023
d04ec4c
Revert changes to CMakeLists.txt.
ejpaul Sep 9, 2023
6c8c9e3
Merge branch 'strain_optimization_cu' of https://github.com/hiddenSym…
ejpaul Sep 9, 2023
6f7e6c6
Added Paz-Soldan reference to example.
ejpaul Sep 9, 2023
d3cffb3
Now using in_github_actions.
ejpaul Sep 9, 2023
3728c0b
Removed strain_plot.py.
ejpaul Sep 9, 2023
a3152d5
Removed unneeded imports.
ejpaul Sep 9, 2023
e565c92
Removed unneeded imports.
ejpaul Sep 9, 2023
7641765
Remove unused imports.
ejpaul Sep 9, 2023
77c1787
Remove unused commits.
ejpaul Sep 9, 2023
50eee3f
Responding to comments.
ejpaul Sep 9, 2023
468ed74
Autopep.
ejpaul Sep 9, 2023
c41f5af
Fixing build errors.
ejpaul Sep 9, 2023
054ecd8
Adding required import.
ejpaul Sep 9, 2023
9a1c51f
Fix typo.
ejpaul Sep 9, 2023
077462e
Including in_github_actions.
ejpaul Sep 9, 2023
705053a
Tweak to test to change coverage.
ejpaul Sep 9, 2023
2e3d79e
Merge branch 'master' of https://github.com/hiddenSymmetries/simsopt
ejpaul Sep 12, 2023
882d7e7
Add unit test to minimize strain for circular coil.
ejpaul Sep 14, 2023
d131aed
Autopep.
ejpaul Sep 14, 2023
26ef5ef
Merged in master.
ejpaul Sep 30, 2023
60329b3
Tidy up docs for strain optimization. Renamed StrainOpt -> CoilStrain.
landreman Sep 30, 2023
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ else()
unset(COMPILER_SUPPORTS_MARCH_NATIVE CACHE)
CHECK_CXX_COMPILER_FLAG(-march=native COMPILER_SUPPORTS_MARCH_NATIVE)
if(COMPILER_SUPPORTS_MARCH_NATIVE)
set(CMAKE_CXX_FLAGS "-O3 -march=native -mfma -ffp-contract=fast")
ejpaul marked this conversation as resolved.
Show resolved Hide resolved
set(CMAKE_CXX_FLAGS "-O3 -mfma -ffp-contract=fast")
elseif(${CMAKE_HOST_SYSTEM_PROCESSOR} STREQUAL "arm64")
set(CMAKE_CXX_FLAGS "-O3 -mcpu=apple-a14 -mfma -ffp-contract=fast")
else()
Expand Down
61 changes: 61 additions & 0 deletions examples/2_Intermediate/strain_optimization_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""
This script performs an optimization of the HTS tape winding angle
with respect to binormal curvature and torsional strain cost functions.
The orientation of the tape is defined wrt the Frener-Serret Frame
"""
ejpaul marked this conversation as resolved.
Show resolved Hide resolved

import numpy as np
import os
import numpy as np
from scipy.optimize import minimize
from simsopt.geo import StrainOpt, LPTorsionalStrainPenalty, LPBinormalCurvatureStrainPenalty
from simsopt.geo import FrameRotation, FramedCurveFrenet, CurveXYZFourier
from simsopt.field import load_coils_from_makegrid_file
ejpaul marked this conversation as resolved.
Show resolved Hide resolved
from simsopt.configs import get_hsx_data
import matplotlib.pyplot as plt

ci = "CI" in os.environ and os.environ['CI'].lower() in ['1', 'true']
MAXITER = 50 if ci else 400
ejpaul marked this conversation as resolved.
Show resolved Hide resolved

curves, currents, ma = get_hsx_data(Nt_coils=10, ppp=10)
curve = curves[1]
scale_factor = 0.1
curve_scaled = CurveXYZFourier(curve.quadpoints, curve.order)
curve_scaled.x = curve.x * scale_factor # scale coil to magnify the strains
rot_order = 10 # order of the Fourier expression for the rotation of the filament pack
width = 1e-3 # tape width

curve_scaled.fix_all() # fix curve DOFs -> only optimize winding angle
rotation = FrameRotation(curve_scaled.quadpoints, rot_order)

framedcurve = FramedCurveFrenet(curve_scaled, rotation)

tor_threshold = 0.02 # Threshold for strain parameters
cur_threshold = 0.02

Jtor = LPTorsionalStrainPenalty(framedcurve, p=2, threshold=tor_threshold)
Jbin = LPBinormalCurvatureStrainPenalty(framedcurve, p=2, threshold=cur_threshold)

strain = StrainOpt(framedcurve, width)
JF = Jtor + Jbin


def fun(dofs):
JF.x = dofs
J = JF.J()
grad = JF.dJ()
outstr = f"Max torsional strain={np.max(strain.torsional_strain()):.1e}, Max curvature strain={np.max(strain.binormal_curvature_strain()):.1e}"
print(outstr)
return J, grad


f = fun
dofs = JF.x

print("""
################################################################################
### Run the optimisation #######################################################
################################################################################
""")
res = minimize(fun, dofs, jac=True, method='L-BFGS-B',
options={'maxiter': MAXITER, 'maxcor': 10, 'gtol': 1e-20, 'ftol': 1e-20}, tol=1e-20)
52 changes: 52 additions & 0 deletions examples/3_Advanced/strain_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
ejpaul marked this conversation as resolved.
Show resolved Hide resolved
This script performs a stage two coil optimization
with additional terms for torsion and binormal curvature in the cost function.
We use a multifilament approach that initializes the coils using the Frener-Serret Frame
"""

import os
from pathlib import Path
import numpy as np
from scipy.optimize import minimize
from simsopt.field import BiotSavart
from simsopt.field import Current, Coil, apply_symmetries_to_curves, apply_symmetries_to_currents
from simsopt.geo import curves_to_vtk, create_equally_spaced_curves
from simsopt.geo import CurveLength, CurveCurveDistance, CurveSurfaceDistance
from simsopt.geo import SurfaceRZFourier
from simsopt.geo import ArclengthVariation
from simsopt.objectives import SquaredFlux
from simsopt.objectives import QuadraticPenalty
from simsopt.geo.strain_optimization_classes import StrainOpt
from simsopt.geo import create_multifilament_grid, ZeroRotation, FramedCurveFrenet, FrameRotation
# from exportcoils import export_coils, import_coils, import_coils_fb, export_coils_fb
landreman marked this conversation as resolved.
Show resolved Hide resolved
from simsopt.configs import get_ncsx_data
import matplotlib.pyplot as plt

curves, currents, ma = get_ncsx_data()
curve = curves[0]

# Set up the winding pack
rot_order = 5 # order of the Fourier expression for the rotation of the filament pack

width = 12
landreman marked this conversation as resolved.
Show resolved Hide resolved

rotation = FrameRotation(curve.quadpoints, rot_order)
framedcurve = FramedCurveFrenet(curve, rotation)

strain = StrainOpt(framedcurve, width=width)

tor = strain.binormal_curvature_strain()
ejpaul marked this conversation as resolved.
Show resolved Hide resolved

plt.figure()
plt.plot(tor)
ejpaul marked this conversation as resolved.
Show resolved Hide resolved
plt.show()

# tor = strain.torsional_strain()
ejpaul marked this conversation as resolved.
Show resolved Hide resolved
tor_frame = framedcurve.frame_torsion()
tor_curve = curve.torsion()
dl = curve.incremental_arclength()
ejpaul marked this conversation as resolved.
Show resolved Hide resolved

plt.figure()
plt.plot(tor_frame)
plt.plot(tor_curve)
plt.show()
ejpaul marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions src/simsopt/field/magneticfieldclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ def __init__(self, phi_str):
self.phi_str = phi_str
self.phi_parsed = parse_expr(phi_str)
R, Z, Phi = sp.symbols('R Z phi')
self.Blambdify = sp.lambdify((R, Z, Phi), [self.phi_parsed.diff(R)+1e-30*Phi*R*Z,\
self.phi_parsed.diff(Phi)/R+1e-30*Phi*R*Z,\
self.Blambdify = sp.lambdify((R, Z, Phi), [self.phi_parsed.diff(R)+1e-30*Phi*R*Z, \
self.phi_parsed.diff(Phi)/R+1e-30*Phi*R*Z, \
self.phi_parsed.diff(Z)+1e-30*Phi*R*Z])
self.dBlambdify_by_dX = sp.lambdify(
(R, Z, Phi),
Expand Down
1 change: 1 addition & 0 deletions src/simsopt/field/self_field.py
ejpaul marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# self field calculation
3 changes: 2 additions & 1 deletion src/simsopt/geo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .curvexyzfourier import *
from .curveperturbed import *
from .curveobjectives import *

from .framedcurve import *
from .finitebuild import *
from .plotting import *

Expand All @@ -21,6 +21,7 @@
from .surfacerzfourier import *
from .surfacexyzfourier import *
from .surfacexyztensorfourier import *
from .strain_optimization import *

__all__ = (curve.__all__ + curvehelical.__all__ +
curverzfourier.__all__ + curvexyzfourier.__all__ +
ejpaul marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading
Loading