File indexing completed on 2026-03-28 07:46:08
0001
0002
0003
0004
0005
0006
0007 """
0008 Simple test script for ToroidField Python bindings
0009 Tests the magnetic field functionality without complex dependencies
0010 """
0011
0012 import sys
0013
0014
0015 def test_toroidal_field_basic():
0016 """Test basic import and configuration"""
0017 print("=" * 60)
0018 print("Testing ToroidField Python Bindings")
0019 print("=" * 60)
0020
0021 import acts
0022
0023 print("โ
Successfully imported acts.ToroidField")
0024
0025
0026 print("\n๐ Testing Config with defaults:")
0027 try:
0028 config = acts.ToroidField.Config()
0029 print(f" Barrel R_in: {config.barrel.R_in / 1000:.1f} m")
0030 print(f" Barrel R_out: {config.barrel.R_out / 1000:.1f} m")
0031 print(f" Barrel c: {config.barrel.c / 1000:.1f} m")
0032 print(f" Barrel b: {config.barrel.b / 1000:.3f} m")
0033 print(f" Barrel I: {config.barrel.I} A")
0034 print(f" Barrel Nturns: {config.barrel.Nturns}")
0035 print(f" Number of coils: {config.layout.nCoils}")
0036 print("โ
Config creation successful")
0037 except Exception as e:
0038 print(f"โ Config creation failed: {e}")
0039 return False
0040
0041
0042 print("\n๐ง Testing Config customization:")
0043 try:
0044 custom_config = acts.ToroidField.Config()
0045 custom_config.barrel.R_in = 5.2 * 1000
0046 custom_config.barrel.R_out = 9.8 * 1000
0047 custom_config.barrel.I = 18000.0
0048 custom_config.layout.nCoils = 10
0049
0050 print(f" Customized Barrel R_in: {custom_config.barrel.R_in / 1000:.1f} m")
0051 print(f" Customized Barrel R_out: {custom_config.barrel.R_out / 1000:.1f} m")
0052 print(f" Customized Barrel I: {custom_config.barrel.I} A")
0053 print(f" Customized number of coils: {custom_config.layout.nCoils}")
0054 print("โ
Config customization successful")
0055 except Exception as e:
0056 print(f"โ Config customization failed: {e}")
0057 return False
0058
0059
0060 print("\n๐งฒ Testing ToroidField creation:")
0061 try:
0062 field = acts.ToroidField(config)
0063 print("โ
ToroidField creation successful")
0064 except Exception as e:
0065 print(f"โ ToroidField creation failed: {e}")
0066 return False
0067
0068 return True
0069
0070
0071 def test_toroidal_field_calculation():
0072 """Test magnetic field calculation"""
0073 print("\n" + "=" * 60)
0074 print("Testing Magnetic Field Calculation")
0075 print("=" * 60)
0076
0077 try:
0078 import acts
0079
0080
0081 config = acts.ToroidField.Config()
0082 field = acts.ToroidField(config)
0083
0084
0085 ctx = acts.MagneticFieldContext()
0086 cache = field.makeCache(ctx)
0087 print("โ
Magnetic field context and cache created")
0088
0089
0090 test_points = [
0091 (6000.0, 0.0, 0.0, "Barrel region"),
0092 (0.0, 7000.0, 1000.0, "Barrel region (rotated)"),
0093 (2000.0, 0.0, 15000.0, "Endcap region"),
0094 (0.0, 3000.0, -12000.0, "Negative endcap"),
0095 (0.0, 0.0, 0.0, "Origin"),
0096 ]
0097
0098 print(f"\n๐ฏ Testing field calculation at {len(test_points)} points:")
0099 for x, y, z, description in test_points:
0100 position = acts.Vector3(x, y, z)
0101 field_value = field.getField(position, cache)
0102
0103
0104 magnitude = (
0105 field_value[0] ** 2 + field_value[1] ** 2 + field_value[2] ** 2
0106 ) ** 0.5
0107
0108 print(f" {description}:")
0109 print(f" Position: ({x/1000:.1f}, {y/1000:.1f}, {z/1000:.1f}) m")
0110 print(
0111 f" Field: ({field_value[0]:.2e}, {field_value[1]:.2e}, {field_value[2]:.2e}) T"
0112 )
0113 print(f" Magnitude: {magnitude:.2e} T")
0114
0115 print("โ
Field calculation successful")
0116 return True
0117
0118 except Exception as e:
0119 print(f"โ Field calculation failed: {e}")
0120 return False
0121
0122
0123 def test_configuration_classes():
0124 """Test individual configuration classes"""
0125 print("\n" + "=" * 60)
0126 print("Testing Configuration Classes")
0127 print("=" * 60)
0128
0129 try:
0130 import acts
0131
0132
0133 print("\n๐บ Testing BarrelConfig:")
0134 barrel_config = acts.ToroidField.BarrelConfig()
0135 print(f" Default R_in: {barrel_config.R_in / 1000:.1f} m")
0136 print(f" Default R_out: {barrel_config.R_out / 1000:.1f} m")
0137 print(f" Default current: {barrel_config.I} A")
0138 print(f" Default turns: {barrel_config.Nturns}")
0139
0140
0141 print("\n๐ Testing EctConfig:")
0142 ect_config = acts.ToroidField.EctConfig()
0143 print(f" Default R_in: {ect_config.R_in / 1000:.3f} m")
0144 print(f" Default R_out: {ect_config.R_out / 1000:.2f} m")
0145 print(f" Default current: {ect_config.I} A")
0146 print(f" Default turns: {ect_config.Nturns}")
0147
0148
0149 print("\n๐ Testing LayoutConfig:")
0150 layout_config = acts.ToroidField.LayoutConfig()
0151 print(f" Default nCoils: {layout_config.nCoils}")
0152 print(f" Default theta0: {layout_config.theta0:.4f} rad")
0153 print(f" Default thetaStep: {layout_config.thetaStep:.4f} rad")
0154
0155 print("โ
Configuration classes test successful")
0156 return True
0157
0158 except Exception as e:
0159 print(f"โ Configuration classes test failed: {e}")
0160 return False
0161
0162
0163 def main():
0164 """Run all tests"""
0165 print("๐ Starting ToroidField Python Binding Tests")
0166 print("=" * 80)
0167
0168 tests = [
0169 ("Basic functionality", test_toroidal_field_basic),
0170 ("Field calculation", test_toroidal_field_calculation),
0171 ("Configuration classes", test_configuration_classes),
0172 ]
0173
0174 results = []
0175 for test_name, test_func in tests:
0176 print(f"\n๐งช Running test: {test_name}")
0177 try:
0178 result = test_func()
0179 results.append(result)
0180 if result:
0181 print(f"โ
{test_name}: PASSED")
0182 else:
0183 print(f"โ {test_name}: FAILED")
0184 except Exception as e:
0185 print(f"๐ฅ {test_name}: ERROR - {e}")
0186 results.append(False)
0187
0188
0189 print("\n" + "=" * 80)
0190 print("๐ Test Summary")
0191 print("=" * 80)
0192
0193 passed = sum(results)
0194 total = len(results)
0195
0196 for i, (test_name, _) in enumerate(tests):
0197 status = "โ
PASSED" if results[i] else "โ FAILED"
0198 print(f" {test_name}: {status}")
0199
0200 print(f"\nOverall: {passed}/{total} tests passed")
0201
0202 if passed == total:
0203 print("๐ All tests passed! ToroidField is working correctly.")
0204 return 0
0205 else:
0206 print("โ ๏ธ Some tests failed. Check the output above for details.")
0207 return 1
0208
0209
0210 if __name__ == "__main__":
0211 sys.exit(main())