SMASIF

WPT Dataset Generation Sept 20

Sep 19th, 2024
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.58 KB | None | 0 0
  1. import torch
  2. import pandas as pd
  3. from scipy.special import ellipk, ellipe
  4. import os
  5. import time
  6. import math  # Import math for pi
  7.  
  8. # Constants
  9. mu_0 = 4 * math.pi * 1e-7  # Permeability of free space (H/m)
  10.  
  11. # Check if CUDA is available and set the device
  12. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  13. print(f"Using device: {device}")
  14.  
  15. # Define the updated Rx coil dimensions (in mm)
  16. rx_total_width = 26.61  # Total available width for Rx segments
  17. rx_total_length = 33.34  # Total available length for Rx segments
  18.  
  19. # Define the Tx coil dimensions (5 times larger than Rx)
  20. tx_total_width = rx_total_width * 5  # Total available width for Tx
  21. tx_total_length = rx_total_length * 5  # Total available length for Tx
  22.  
  23. # Define the range for Trace Width, Thickness, Gap, and Center Area Percentage
  24. trace_widths = torch.arange(0.06, 0.82, 0.04).to(device)  # Move to GPU
  25. trace_thicknesses = torch.arange(0.06, 0.82, 0.04).to(device)  # Move to GPU
  26. gaps = torch.arange(0.05, 1, 0.05).to(device)  # Move to GPU
  27. center_area_percentages = torch.arange(0.15, 0.30, 0.05).to(device)  # Move to GPU
  28.  
  29. # Set constant distance between coils (z = 20 mm)
  30. z = torch.tensor(20.0, device=device)  # Constant distance in mm
  31.  
  32. # Initialize a counter to track the number of iterations
  33. counter = 0
  34.  
  35. # Total number of iterations for progress tracking
  36. total_iterations = len(trace_widths) * len(trace_thicknesses) * len(gaps) * len(center_area_percentages)
  37.  
  38. # Check if there's an existing partial dataset
  39. output_file = r'C:\Users\Andrew\PycharmProjects\pythonProject\Data\mutual_inductance_dataset_updated_9th_sept.csv'
  40. if os.path.exists(output_file):
  41.     df_existing = pd.read_csv(output_file)
  42.     counter = len(df_existing)
  43.     dataset = df_existing.to_dict('records')
  44.     print(f"Resuming from {counter} configurations.")
  45. else:
  46.     dataset = []
  47.  
  48. # Function to calculate mutual inductance between filaments
  49. def calculate_mutual_inductance_for_filaments(a, b, c, d, z):
  50.     R = torch.sqrt((a + c) ** 2 + (b + d) ** 2 + z ** 2)
  51.     if R == 0:
  52.         return torch.tensor(0.0, device=device)
  53.  
  54.     k_sq = 4 * a * c / ((a + c) ** 2 + (b + d) ** 2 + z ** 2)
  55.     if k_sq < 0 or k_sq > 1:
  56.         return torch.tensor(0.0, device=device)
  57.  
  58.     try:
  59.         K_k = ellipk(k_sq.cpu().numpy())
  60.         E_k = ellipe(k_sq.cpu().numpy())
  61.     except ValueError:
  62.         return torch.tensor(0.0, device=device)
  63.  
  64.     sqrt_ac = torch.sqrt(a * c)
  65.     M = (mu_0 / math.pi) * sqrt_ac * ((1 - k_sq / 2) * K_k - E_k) / R
  66.  
  67.     if torch.isnan(M) or torch.isinf(M):
  68.         return torch.tensor(0.0, device=device)
  69.  
  70.     return M
  71.  
  72. # Function to calculate the mutual inductance between segments
  73. def calculate_mutual_inductance_for_segment(segment_length_rx, segment_width_rx, segment_length_tx, segment_width_tx,
  74.                                             N_rx, Tw_rx, T_rx, gap_length, z):
  75.     if N_rx <= 0:
  76.         return torch.tensor(0.0, device=device)
  77.  
  78.     P_T_rx = max(int(Tw_rx / 0.025), 1)  # Finer division
  79.     Q_T_rx = max(int(T_rx / 0.01), 1)    # Finer division
  80.  
  81.     M_total = torch.tensor(0.0, device=device)
  82.  
  83.     for q_T_rx in range(1, 2 * Q_T_rx + 2):
  84.         for p_T_rx in range(1, 2 * P_T_rx + 2):
  85.             for q_T_tx in range(1, 2 * Q_T_rx + 2):
  86.                 for p_T_tx in range(1, 2 * P_T_rx + 2):
  87.                     a_rx = (segment_length_rx - p_T_rx * (Tw_rx + gap_length)) / N_rx
  88.                     b_rx = (segment_width_rx - q_T_rx * (T_rx + gap_length)) / N_rx
  89.  
  90.                     a_tx = (segment_length_tx - p_T_tx * (Tw_rx + gap_length)) / N_rx
  91.                     b_tx = (segment_width_tx - q_T_tx * (T_rx + gap_length)) / N_rx
  92.  
  93.                     # Ensure filament dimensions are positive
  94.                     if min(a_rx, b_rx, a_tx, b_tx) <= 0:
  95.                         continue
  96.  
  97.                     M_ij = calculate_mutual_inductance_for_filaments(a_rx, b_rx, a_tx, b_tx, z)
  98.                     M_total += M_ij
  99.  
  100.     # Direct summation approach, no normalization factor
  101.     return M_total
  102.  
  103. # Iterate over all combinations of trace width, thickness, gap, and center area percentage
  104. start_time = time.time()  # Start the timer
  105. for center_area_percentage in center_area_percentages:
  106.     for Tw_rx in trace_widths:
  107.         for T_rx in trace_thicknesses:
  108.             for gap_length in gaps:
  109.                 cap_area_length = rx_total_length * center_area_percentage
  110.                 cap_area_width = rx_total_width * center_area_percentage
  111.  
  112.                 coil_area_length_rx = rx_total_length - cap_area_length
  113.                 coil_area_width_rx = rx_total_width - cap_area_width
  114.  
  115.                 coil_area_length_tx = tx_total_length - (cap_area_length * 5)
  116.                 coil_area_width_tx = tx_total_width - (cap_area_width * 5)
  117.  
  118.                 available_length_rx = coil_area_length_rx - gap_length
  119.                 available_length_tx = coil_area_length_tx - gap_length
  120.  
  121.                 # Ensure available lengths are positive
  122.                 if available_length_rx > 0 and available_length_tx > 0:
  123.                     segment_length_rx_L = available_length_rx / 2
  124.                     segment_length_rx_LC = available_length_rx / 2
  125.                     segment_length_tx = available_length_tx / 2
  126.  
  127.                     turn_pitch_rx_L = Tw_rx + gap_length
  128.                     N_rx_L = int(segment_length_rx_L / turn_pitch_rx_L)
  129.                     turn_pitch_rx_LC = Tw_rx + gap_length
  130.                     N_rx_LC = int(segment_length_rx_LC / turn_pitch_rx_LC)
  131.                     N_tx = int(segment_length_tx / turn_pitch_rx_L)
  132.  
  133.                     # Ensure segment lengths and number of turns are positive
  134.                     if segment_length_rx_L > 0 and segment_length_rx_LC > 0 and segment_length_tx > 0 \
  135.                             and N_rx_L > 0 and N_rx_LC > 0 and N_tx > 0:
  136.  
  137.                         M1_L = calculate_mutual_inductance_for_segment(segment_length_rx_L, coil_area_width_rx,
  138.                                                                        segment_length_tx, coil_area_width_tx, N_rx_L, Tw_rx,
  139.                                                                        T_rx, gap_length, z)
  140.                         M1_LC = calculate_mutual_inductance_for_segment(segment_length_rx_LC, coil_area_width_rx,
  141.                                                                         segment_length_tx, coil_area_width_tx, N_rx_LC, Tw_rx,
  142.                                                                         T_rx, gap_length, z)
  143.  
  144.                         M_final = M1_L + M1_LC
  145.  
  146.                         dataset.append({
  147.                             'Trace Width (Rx L mm)': Tw_rx.item(),
  148.                             'Trace Thickness (Rx L mm)': T_rx.item(),
  149.                             'Number of Turns (Rx L)': N_rx_L,
  150.                             'Trace Width (Rx L+C mm)': Tw_rx.item(),
  151.                             'Trace Thickness (Rx L+C mm)': T_rx.item(),
  152.                             'Number of Turns (Rx L+C)': N_rx_LC,
  153.                             'Gap Between Segments (mm)': gap_length.item(),
  154.                             'Center Area Percentage': center_area_percentage.item(),
  155.                             'Capacitor Area Length (mm)': cap_area_length,
  156.                             'Capacitor Area Width (mm)': cap_area_width,
  157.                             'Rx Coil Area Length (mm)': coil_area_length_rx,
  158.                             'Rx Coil Area Width (mm)': coil_area_width_rx,
  159.                             'Tx Coil Area Length (mm)': coil_area_length_tx,
  160.                             'Tx Coil Area Width (mm)': coil_area_width_tx,
  161.                             'Mutual Inductance Segment L (H)': M1_L.item(),
  162.                             'Mutual Inductance Segment L+C (H)': M1_LC.item(),
  163.                             'Final Mutual Inductance (H)': M_final.item()
  164.                         })
  165.  
  166.                         counter += 1
  167.                         if counter % 100 == 0 or counter == total_iterations:
  168.                             end_time = time.time()  # End the timer
  169.                             elapsed_time = end_time - start_time  # Calculate the elapsed time
  170.                             print(f"Processed {counter}/{total_iterations} configurations. Time for last 100 configurations: {elapsed_time:.2f} seconds.")
  171.                             start_time = time.time()  # Reset the timer
  172.  
  173.                             # Save progress
  174.                             pd.DataFrame(dataset).to_csv(output_file, index=False)
  175.  
  176. # Final save to ensure no data is lost
  177. pd.DataFrame(dataset).to_csv(output_file, index=False)
  178. print(f"Final dataset saved with {counter} configurations.")
  179.  
Advertisement
Add Comment
Please, Sign In to add comment