Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-07-16 08:08:22

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