Skip to content

Commit

Permalink
Updated the torques to be computed with respect to the coil barycente…
Browse files Browse the repository at this point in the history
…r. rerunning some examples. Almost have a working QH example with fixed coils but need to tune the force weight a bit.
  • Loading branch information
akaptano committed Oct 29, 2024
1 parent 8a2e57b commit aea2f37
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 171 deletions.
23 changes: 19 additions & 4 deletions examples/3_Advanced/QA_reactorscale_fixed_orientations.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def initialize_coils_QA(TEST_DIR, s):
import warnings

keep_inds = []
dists = np.zeros(len(base_curves))
for ii in range(len(base_curves)):
counter = 0
for i in range(base_curves[0].gamma().shape[0]):
Expand All @@ -174,9 +175,19 @@ def initialize_coils_QA(TEST_DIR, s):
'of a TF coil. Deleting these PSCs now.')
counter += 1
break
dists[ii] = np.min(np.linalg.norm(base_curves[ii].gamma(), axis=-1))
if counter == 0:
keep_inds.append(ii)

# Chop off the dipole coils in the tight inboard side
# since these coils often have very high forces and prevent the TF
# coils from moving around much
dists = dists[keep_inds]
argdists = np.argsort(dists)
keep_inds = np.array(keep_inds)[argdists]
for i in range(4):
keep_inds = np.delete(keep_inds, [0])

print(keep_inds)
base_curves = np.array(base_curves)[keep_inds]

Expand Down Expand Up @@ -279,7 +290,11 @@ def pointData_forces_torques(coils, allcoils, aprimes, bprimes, nturns_list):
CS_THRESHOLD = 1.5
CS_WEIGHT = 1e2
# Weight for the Coil Coil forces term
FORCE_WEIGHT = Weight(1e-21) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
# FORCE_WEIGHT = Weight(1e-19) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
# FORCE_WEIGHT2 = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
# TORQUE_WEIGHT = Weight(1e-24) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
# TORQUE_WEIGHT2 = Weight(1e-24) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
FORCE_WEIGHT = Weight(1e-20) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
FORCE_WEIGHT2 = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
TORQUE_WEIGHT = Weight(1e-24) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
TORQUE_WEIGHT2 = Weight(1e-24) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
Expand Down Expand Up @@ -365,9 +380,9 @@ def pointData_forces_torques(coils, allcoils, aprimes, bprimes, nturns_list):
regularization_list2 = np.zeros(len(coils_TF)) * regularization_rect(a, b)
# Jforce = MixedLpCurveForce(coils, coils_TF, regularization_list, regularization_list2) # [SquaredMeanForce2(c, coils) for c in (base_coils)]
# Jforce = MixedSquaredMeanForce(coils, coils_TF)
Jforce = sum([LpCurveForce(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 40) for i, c in enumerate(base_coils + base_coils_TF)])
Jforce = sum([LpCurveForce(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 100) for i, c in enumerate(base_coils + base_coils_TF)])
Jforce2 = sum([SquaredMeanForce(c, coils + coils_TF) for c in (base_coils + base_coils_TF)])
Jtorque = sum([LpCurveTorque(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 40) for i, c in enumerate(base_coils + base_coils_TF)])
Jtorque = sum([LpCurveTorque(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 100) for i, c in enumerate(base_coils + base_coils_TF)])
Jtorque2 = sum([SquaredMeanTorque(c, coils + coils_TF) for c in (base_coils + base_coils_TF)])

# Jtorque = SquaredMeanTorque2(coils, coils_TF) # [SquaredMeanForce2(c, coils) for c in (base_coils)]
Expand Down Expand Up @@ -541,7 +556,7 @@ def fun(dofs):
for i in range(1, n_saves + 1):
print('Iteration ' + str(i) + ' / ' + str(n_saves))
res = minimize(fun, dofs, jac=True, method='L-BFGS-B',
options={'maxiter': MAXITER, 'maxcor': 400}, tol=1e-15)
options={'maxiter': MAXITER, 'maxcor': 100}, tol=1e-15)
dofs = res.x

dipole_currents = [c.current.get_value() for c in bs.coils]
Expand Down
6 changes: 3 additions & 3 deletions examples/3_Advanced/QA_single_TFcoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def initialize_coils_QA(TEST_DIR, s):
aa = 0.05
bb = 0.05

Nx = 5
Nx = 3
Ny = Nx
Nz = Nx
# Create the initial coils:
Expand Down Expand Up @@ -218,9 +218,9 @@ def pointData_forces_torques(coils, allcoils, aprimes, bprimes, nturns_list):
CS_THRESHOLD = 1.5
CS_WEIGHT = 1e2
# Weight for the Coil Coil forces term
FORCE_WEIGHT = Weight(1e-19) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
FORCE_WEIGHT = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
FORCE_WEIGHT2 = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
TORQUE_WEIGHT = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
TORQUE_WEIGHT = Weight(1e-18) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
# Directory for output
OUT_DIR = ("./QA_singleTF_n{:d}_p{:.2e}_c{:.2e}_lw{:.2e}_lt{:.2e}_lkw{:.2e}" + \
"_cct{:.2e}_ccw{:.2e}_cst{:.2e}_csw{:.2e}_fw{:.2e}_fww{:2e}_tw{:.2e}/").format(
Expand Down
15 changes: 8 additions & 7 deletions examples/3_Advanced/QH_reactorscale_DA.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def initialize_coils_QH(TEST_DIR, s):
# Only need Jax flag for CurvePlanarFourier class
base_curves = create_equally_spaced_curves(
ncoils, s.nfp, stellsym=True,
R0=R0, R1=R1, order=order, numquadpoints=256,
R0=R0, R1=R1, order=order, numquadpoints=512,
jax_flag=True,
)

Expand Down Expand Up @@ -142,7 +142,7 @@ def initialize_coils_QH(TEST_DIR, s):
aa = 0.05
bb = 0.05

Nx = 7
Nx = 6
Ny = Nx
Nz = Nx
# Create the initial coils:
Expand Down Expand Up @@ -263,7 +263,7 @@ def pointData_forces_torques(coils, allcoils, aprimes, bprimes, nturns_list):
base_b_list = np.hstack((np.ones(len(base_coils)) * bb, np.ones(len(base_coils_TF)) * b))

LENGTH_WEIGHT = Weight(0.001)
LENGTH_TARGET = 70
LENGTH_TARGET = 80
LINK_WEIGHT = 1e3
CC_THRESHOLD = 0.8
CC_WEIGHT = 1e1
Expand All @@ -274,7 +274,7 @@ def pointData_forces_torques(coils, allcoils, aprimes, bprimes, nturns_list):
# FORCE_WEIGHT2 = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
# TORQUE_WEIGHT = Weight(1e-24) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
# TORQUE_WEIGHT2 = Weight(1e-24) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
FORCE_WEIGHT = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
FORCE_WEIGHT = Weight(1e-19) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
FORCE_WEIGHT2 = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
TORQUE_WEIGHT = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
TORQUE_WEIGHT2 = Weight(0.0) # Forces are in Newtons, and typical values are ~10^5, 10^6 Newtons
Expand Down Expand Up @@ -359,9 +359,10 @@ def pointData_forces_torques(coils, allcoils, aprimes, bprimes, nturns_list):
regularization_list2 = np.zeros(len(coils_TF)) * regularization_rect(a, b)
# Jforce = MixedLpCurveForce(coils, coils_TF, regularization_list, regularization_list2) # [SquaredMeanForce2(c, coils) for c in (base_coils)]
# Jforce = MixedSquaredMeanForce(coils, coils_TF)
Jforce = sum([LpCurveForce(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 40) for i, c in enumerate(base_coils + base_coils_TF)])
Jforce = sum([LpCurveForce(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 100) for i, c in enumerate(base_coils + base_coils_TF)])
Jforce2 = sum([SquaredMeanForce(c, coils + coils_TF) for c in (base_coils + base_coils_TF)])
Jtorque = sum([LpCurveTorque(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 40) for i, c in enumerate(base_coils + base_coils_TF)])
Jtorque = sum([LpCurveTorque(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 100) for i, c in enumerate(base_coils + base_coils_TF)])
# Jtorque = sum([LpCurveTorque(c, coils + coils_TF, regularization_rect(a_list[i], b_list[i]), p=2, threshold=1e5 * 100) for i, c in enumerate(base_coils + base_coils_TF)])
Jtorque2 = sum([SquaredMeanTorque(c, coils + coils_TF) for c in (base_coils + base_coils_TF)])

# Jtorque = SquaredMeanTorque2(coils, coils_TF) # [SquaredMeanForce2(c, coils) for c in (base_coils)]
Expand Down Expand Up @@ -531,7 +532,7 @@ def fun(dofs):
# print('dJtorques time = ', t2 - t1, ' s')

n_saves = 1
MAXITER = 400
MAXITER = 200
for i in range(1, n_saves + 1):
print('Iteration ' + str(i) + ' / ' + str(n_saves))
res = minimize(fun, dofs, jac=True, method='L-BFGS-B',
Expand Down
Loading

0 comments on commit aea2f37

Please sign in to comment.