Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-28 07:46:08

0001 #!/usr/bin/env python3
0002 
0003 # Copyright (c) 2025 ACTS-Project
0004 # This file is part of ACTS.
0005 # See LICENSE for details.
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     # Test Config creation with defaults
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     # Test Config customization
0042     print("\n๐Ÿ”ง Testing Config customization:")
0043     try:
0044         custom_config = acts.ToroidField.Config()
0045         custom_config.barrel.R_in = 5.2 * 1000  # Convert to mm
0046         custom_config.barrel.R_out = 9.8 * 1000  # Convert to mm
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     # Test ToroidField creation
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         # Create field
0081         config = acts.ToroidField.Config()
0082         field = acts.ToroidField(config)
0083 
0084         # Create magnetic field context and cache
0085         ctx = acts.MagneticFieldContext()
0086         cache = field.makeCache(ctx)
0087         print("โœ… Magnetic field context and cache created")
0088 
0089         # Test field calculation at various points
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             # Calculate field magnitude
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         # Test BarrelConfig
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         # Test EctConfig
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         # Test LayoutConfig
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     # Summary
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())