def test_minimum_distance_taylor_test(): nfp = 2 (coils, currents, ma, eta_bar) = get_24_coil_data(nfp=nfp) currents = len(coils) * [1e4] stellarator = CoilCollection(coils, currents, nfp, True) coils = stellarator.coils np.random.seed(2) J = MinimumDistance(coils, 0.1) coil_dofs = stellarator.get_dofs() h = np.random.rand(len(coil_dofs)).reshape(coil_dofs.shape) dJ = stellarator.reduce_coefficient_derivatives(J.dJ_by_dcoefficients()) deriv = np.sum(dJ * h) err = 1e8 for i in range(1, 5): eps = 0.1**i stellarator.set_dofs(coil_dofs + eps * h) Jp = J.J() stellarator.set_dofs(coil_dofs - eps * h) Jm = J.J() deriv_est = 0.5 * (Jp - Jm) / eps err_new = np.linalg.norm(deriv_est - deriv) print("err_new %s" % (err_new)) assert err_new < 0.55**2 * err err = err_new print("deriv_est %s" % (deriv_est))
def test_biot_savart_same_results_as_matlab(): num_coils = 6 nfp = 2 coils, currents, ma, eta_bar = get_24_coil_data(nfp=nfp, ppp=20) currents = num_coils * [1e4] coil_collection = CoilCollection(coils, currents, nfp, True) bs = BiotSavart(coil_collection.coils, coil_collection.currents) points = np.asarray([ [1.079860105000000, 0., 0.], [1.078778093231020, 0.041861502907184, -0.006392709264512], ]) matlab_res = np.asarray([ [0, -0.044495549447737, 0.005009283509639], [0.002147564148695, -0.044454924339257, 0.004992777089330], ]) bs.compute(points, use_cpp=True) print(bs.B, "\n", matlab_res) assert np.allclose(bs.B, matlab_res) J = SquaredMagneticFieldNormOnCurve(ma, bs) J0 = J.J() assert abs(0.5 * J0 - 0.007179654002556) < 1e-10 J = SquaredMagneticFieldGradientNormOnCurve(ma, bs) J0 = J.J() assert abs(0.5 * J0 - 0.014329772542444) < 1e-10 if __name__ == "__main__": ax = None for i in range(0, len(coils)): ax = coils[i].plot(ax=ax, show=False) ma.plot(ax=ax)
def test_magnetic_field_objective_by_dcurvecoeffs(gradient): nfp = 2 (coils, _, ma, _) = get_24_coil_data(nfp=nfp, ppp=20) currents = len(coils) * [1e4] stellerator = CoilCollection(coils, currents, nfp, True) bs = BiotSavart(stellerator.coils, stellerator.currents) if gradient: J = SquaredMagneticFieldGradientNormOnCurve(ma, bs) else: J = SquaredMagneticFieldNormOnCurve(ma, bs) J0 = J.J() curve_dofs = ma.get_dofs() h = 1e-1 * np.random.rand(len(curve_dofs)).reshape(curve_dofs.shape) dJ = J.dJ_by_dcurvecoefficients() deriv = np.sum(dJ * h) err = 1e6 for i in range(5, 10): eps = 0.5**i ma.set_dofs(curve_dofs + eps * h) Jh = J.J() deriv_est = (Jh - J0) / eps err_new = np.linalg.norm(deriv_est - deriv) print("err_new %s" % (err_new)) assert err_new < 0.55 * err err = err_new
def test_taylor_test_coil_currents(objective): num_coils = 6 nfp = 2 (coils, _, ma, _) = get_24_coil_data(nfp=nfp, ppp=20) currents = len(coils) * [1e4] stellerator = CoilCollection(coils, currents, nfp, True) bs = BiotSavart(stellerator.coils, stellerator.currents) bs.set_points(ma.gamma) qsf = QuasiSymmetricField(-2.25, ma) J = BiotSavartQuasiSymmetricFieldDifference(qsf, bs) x0 = stellerator.get_currents() h = 1e4 * np.random.rand(len(currents)) if objective == "l2": J0 = J.J_L2() dJ = stellerator.reduce_current_derivatives(J.dJ_L2_by_dcoilcurrents()) else: J0 = J.J_H1() dJ = stellerator.reduce_current_derivatives(J.dJ_H1_by_dcoilcurrents()) assert len(dJ) == len(h) deriv = np.sum(dJ * h) err = 1e6 eps = 0.1 while err > 1e-7: eps *= 0.5 stellerator.set_currents(x0 + eps * h) bs.clear_cached_properties() Jh = J.J_L2() if objective == "l2" else J.J_H1() deriv_est = (Jh - J0) / eps err_new = np.linalg.norm(deriv_est - deriv) print("err_new %s" % (err_new)) assert err_new < 0.55 * err err = err_new assert eps < 1e-2
def test_quasi_symmetric_difference_same_results_as_matlab(): num_coils = 6 nfp = 2 coils, currents, ma, eta_bar = get_24_coil_data(nfp=nfp, ppp=20) currents = num_coils * [1e4] coil_collection = CoilCollection(coils, currents, nfp, True) bs = BiotSavart(coil_collection.coils, coil_collection.currents) bs.set_points(ma.gamma) qsf = QuasiSymmetricField(-2.25, ma) J = BiotSavartQuasiSymmetricFieldDifference(qsf, bs) print(0.5 * J.J_L2()) print(0.5 * J.J_H1()) assert abs(3.486875802492926 - 0.5 * J.J_L2()) < 1e-5 assert abs(8.296004257157044 - 0.5 * J.J_H1()) < 1e-5
def test_taylor_test_ma_coeffs(objective): num_coils = 6 nfp = 2 (coils, _, ma, _) = get_24_coil_data(nfp=nfp, ppp=20) currents = len(coils) * [1e4] stellerator = CoilCollection(coils, currents, nfp, True) bs = BiotSavart(stellerator.coils, stellerator.currents) bs.set_points(ma.gamma) qsf = QuasiSymmetricField(-2.25, ma) J = BiotSavartQuasiSymmetricFieldDifference(qsf, bs) ma_dofs = ma.get_dofs() np.random.seed(1) h = np.random.rand(len(ma_dofs)).reshape(ma_dofs.shape) if objective == "l2": J0 = J.J_L2() dJ = J.dJ_L2_by_dmagneticaxiscoefficients() else: J0 = J.J_H1() dJ = J.dJ_H1_by_dmagneticaxiscoefficients() assert len(dJ) == len(h) deriv = np.sum(dJ * h) err = 1e6 eps = 0.04 while err > 1e-11: eps *= 0.5 deriv_est = 0 shifts = [-2, -1, +1, +2] weights = [+1 / 12, -2 / 3, +2 / 3, -1 / 12] for i in range(4): ma.set_dofs(ma_dofs + shifts[i] * eps * h) bs.set_points(ma.gamma) qsf.clear_cached_properties() deriv_est += weights[i] * (J.J_L2() if objective == "l2" else J.J_H1()) deriv_est *= 1 / eps err_new = np.linalg.norm(deriv_est - deriv) / np.linalg.norm(deriv) print("err_new %s" % (err_new)) assert err_new < (0.55)**4 * err err = err_new assert eps < 1e-3
def test_magnetic_field_in_ncsx_is_correct(): nfp = 3 (coils, ma, currents) = get_ncsx_data(Nt=25, ppp=20) stellarator = CoilCollection(coils, currents, nfp, True) dir_path = os.path.dirname(os.path.realpath(__file__)) filepath = os.path.join(dir_path, "..", "pyplasmaopt", "data", "ncsx", "mgrid_c09r00_modularOnly.nc") ds = nc.Dataset(filepath) Br = ds['br_001'][:, :, :] + ds['br_002'][:, :, :] + ds['br_003'][:, :, :] Bp = ds['bp_001'][:, :, :] + ds['bp_002'][:, :, :] + ds['bp_003'][:, :, :] Bz = ds['bz_001'][:, :, :] + ds['bz_002'][:, :, :] + ds['bz_003'][:, :, :] Rs = np.linspace(0.436, 2.436, 5, endpoint=True) phis = np.linspace(0, 2 * np.pi / 3, 4, endpoint=False) Zs = np.linspace(-1, 1, 9, endpoint=True) gammas = [coil.gamma for coil in stellarator.coils] dgamma_by_dphis = [ coil.dgamma_by_dphi[:, 0, :] for coil in stellarator.coils ] currents = stellarator.currents avg_rel_err = 0 max_rel_err = 0 for i, phi in enumerate(phis): for j, Z in enumerate(Zs): for k, R in enumerate(Rs): x = R * np.cos(phi) y = R * np.sin(phi) Bxyz = np.zeros((1, 3)) xyz = np.asarray([[x, y, Z]]) cpp.biot_savart_B_only(xyz, gammas, dgamma_by_dphis, currents, Bxyz) trueBx = np.cos(phi) * Br[i, j, k] - np.sin(phi) * Bp[i, j, k] trueBy = np.sin(phi) * Br[i, j, k] + np.cos(phi) * Bp[i, j, k] trueBz = Bz[i, j, k] trueB = np.asarray([[trueBx, trueBy, trueBz]]) err = np.linalg.norm(Bxyz - trueB) / np.linalg.norm(trueB) avg_rel_err += err max_rel_err = max(max_rel_err, err) assert err < 2e-2 print('avg_rel_err', avg_rel_err / ((len(phis) * len(Zs) * len(Rs)))) print('max_rel_err', max_rel_err)
def test_poincareplot(nparticles=12, nperiods=20): nfp = 2 coils, currents, ma, eta_bar = get_24_coil_data(nfp=nfp, ppp=20, at_optimum=True) currents = [ 1e5 * x for x in [ -2.271314992875459, -2.223774477156286, -2.091959078815509, -1.917569373937265, -2.115225147955706, -2.025410501731495 ] ] # coils, currents, ma, eta_bar = get_24_coil_data(nfp=nfp, ppp=20, at_optimum=False) # currents = 6 * [1] coil_collection = CoilCollection(coils, currents, nfp, True) bs = BiotSavart(coil_collection.coils, coil_collection.currents) rphiz, xyz, _, _ = compute_field_lines(bs, nperiods=nperiods, batch_size=8, magnetic_axis_radius=1.1, max_thickness=0.6, delta=0.02) nparticles = rphiz.shape[0] try: import mayavi.mlab as mlab if not __name__ == "__main__": mlab.options.offscreen = True for coil in coil_collection.coils: mlab.plot3d(coil.gamma[:, 0], coil.gamma[:, 1], coil.gamma[:, 2]) colors = [(0.298, 0.447, 0.690), (0.866, 0.517, 0.321), (0.333, 0.658, 0.407), (0.768, 0.305, 0.321), (0.505, 0.447, 0.701), (0.576, 0.470, 0.376), (0.854, 0.545, 0.764), (0.549, 0.549, 0.549), (0.800, 0.725, 0.454), (0.392, 0.709, 0.803)] counter = 0 for i in range(0, nparticles, nparticles // 5): mlab.plot3d(xyz[i, :, 0], xyz[i, :, 1], xyz[i, :, 2], tube_radius=0.005, color=colors[counter % len(colors)]) counter += 1 mlab.view(azimuth=0, elevation=0) if __name__ == "__main__": mlab.show() else: mlab.savefig("/tmp/poincare-particle.png", magnification=4) mlab.close() except ModuleNotFoundError: pass import matplotlib.pyplot as plt plt.figure() for i in range(nparticles): plt.scatter(rphiz[i, range(0, nperiods * 100, 100), 0], rphiz[i, range(0, nperiods * 100, 100), 2], s=0.1) # plt.show() plt.savefig("/tmp/poincare.png", dpi=300) plt.close() assert True # not quite sure how to test this, so we just check that it runs.