Advertisement
shihab2196

C-H_Bond_Breaking_Location_PAO_cummulative_count

Mar 2nd, 2025
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Author: Shihab Ahmed
  4. Created on Sat Mar  1 02:51:27 2025
  5. """
  6.  
  7. import magnolia.bondfile_parser as bfp
  8.  
  9.  
  10. dirr = r"C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Base_Oil\PAO4\25_PAO4_200_O2_Soria\Production\1600\Sim-1"
  11. filename = r"\bonds.reaxc"
  12.  
  13. atom_connectivity = bfp.parsebondfile(dirr+filename, mtypes=True)
  14. #%%
  15. import matplotlib.pyplot as plt
  16. import numpy as np
  17.  
  18. # Load connectivity and atomic type data
  19. neighbors = atom_connectivity['neighbours']  # Adjacency list for each step
  20. atypes    = atom_connectivity['atypes']      # Maps atom ID to atomic type (H=1, C=2, O=3, etc.)
  21.  
  22. # Step 1: Classify Carbons at Step 0
  23. carbon_types = {}  # Store classification of each carbon
  24. initial_graph = neighbors[0]  # Get the first step connectivity
  25.  
  26. for parent, children in initial_graph.items():
  27.     if atypes[parent] == 2:  # Only classify Carbon (C=2)
  28.         bonded_carbons = [x for x in children if atypes[x] == 2]  # Count bonded carbons
  29.         num_carbons = len(bonded_carbons)
  30.  
  31.         if num_carbons == 1:
  32.             carbon_types[parent] = "Primary"
  33.         elif num_carbons == 2:
  34.             carbon_types[parent] = "Secondary"
  35.         elif num_carbons == 3:
  36.             carbon_types[parent] = "Tertiary"
  37.  
  38. N_primary = list(carbon_types.values()).count('Primary')
  39. N_secondary = list(carbon_types.values()).count('Secondary')
  40. N_tertiary = list(carbon_types.values()).count('Tertiary')
  41. count = dict()
  42. count['Primary'] = N_primary
  43. count['Secondary'] = N_secondary
  44. count['Tertiary'] = N_tertiary
  45.  
  46. # Step 2: Count Hydrogens per Carbon at Any Step
  47. def count_hydrogens(neigh, atypes):
  48.     """Counts hydrogen atoms bonded to each carbon."""
  49.     hydrogen_counts = {}
  50.  
  51.     for atom, bonded_atoms in neigh.items():
  52.         if atypes[atom] == 2:  # Only count hydrogens around Carbon
  53.             hydrogen_count = sum(1 for neighbor in bonded_atoms if atypes[neighbor] == 1)  # Count H
  54.             hydrogen_counts[atom] = hydrogen_count
  55.  
  56.     return hydrogen_counts
  57.  
  58. # Initialize hydrogen count at step 0
  59. previous_hydrogens = count_hydrogens(neighbors[0], atypes)  
  60.  
  61. # Step 3: Track Incremental Hydrogen Loss Over Time
  62. H_loss_trend = {"Primary": [], "Secondary": [], "Tertiary": []}
  63.  
  64. for step, current_graph in neighbors.items():  # Loop through steps
  65.     current_hydrogens = count_hydrogens(current_graph, atypes)
  66.    
  67.     # Track total H loss per carbon type at this step
  68.     step_loss = {"Primary": 0, "Secondary": 0, "Tertiary": 0}
  69.  
  70.     for atom in current_hydrogens:
  71.         if atom in previous_hydrogens:  # Ensure atom was present in the previous step
  72.             h_lost = previous_hydrogens[atom] - current_hydrogens[atom]  # Compare with the last step
  73.  
  74.             if h_lost > 0:  # If hydrogens were lost
  75.                 c_type = carbon_types.get(atom, None)
  76.                 if c_type:
  77.                     step_loss[c_type] += h_lost  # Sum H loss per step per type
  78.  
  79.     # Append total H loss per step
  80.     for c_type in H_loss_trend:
  81.         H_loss_trend[c_type].append(step_loss[c_type])
  82.    
  83.     # Update previous_hydrogens to current for the next iteration
  84.     # previous_hydrogens = current_hydrogens  
  85.  
  86. # Step 4: Plot Incremental Hydrogen Loss Over Time
  87. steps = np.array(list(neighbors.keys()))
  88. time = steps*0.25/1000
  89.  
  90. start = 325
  91. end = None
  92.  
  93. fig, ax = plt.subplots(dpi=350)
  94.  
  95. for key, value in H_loss_trend.items():
  96.     value = np.array(value)
  97.     x     = time[time>325] - time[time>325].min()
  98.     y     = value[time>325]
  99.     ax.plot(x,y,label=key)
  100.  
  101. plt.xlabel("Time (ps)")
  102. plt.ylabel("H Atoms Loss")
  103. plt.legend()
  104. plt.title("Incremental H Loss from Different\nCarbon Types Over Time\n", fontsize=12)
  105. plt.show()
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement