Advertisement
shihab2196

first_CH_bond_breaking_carbon_types_bar_plot

Mar 2nd, 2025
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Author: Shihab Ahmed
  4. Created on Sat Mar  1 20:23:53 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. import networkx as nx
  18.  
  19. neighbors = atom_connectivity['neighbours']
  20. atypes    = atom_connectivity['atypes']
  21. mtypes    = atom_connectivity['mtypes']
  22. first_neighbors = neighbors[0]
  23.  
  24. def draw_backbone(molecule, carbons=[]):
  25.     firststep_neigh     = list(neighbors.values())[0]
  26.     firststep_graph     = nx.Graph(firststep_neigh)
  27.    
  28.     subgraph = firststep_graph.subgraph(molecule)
  29.     H_nodes  = [x for x in molecule if atypes[x]==1]
  30.     backbone = subgraph.copy()
  31.     backbone.remove_nodes_from(H_nodes)
  32.    
  33.     ## Draw graph
  34.     node_color = []
  35.     for node in backbone.nodes:
  36.         if node in carbons: node_color.append('black')
  37.         else: node_color.append('gray')
  38.        
  39.     pos = nx.kamada_kawai_layout(backbone)
  40.     plt.figure(figsize=[6,4])
  41.     nx.draw(backbone,pos=pos,node_size=150,node_color=node_color,
  42.             edgecolors='k')
  43.     plt.show()
  44.  
  45. def find_first_ch_bond_break(atom_connectivity):
  46.     neighbors = atom_connectivity['neighbours']  # Adjacency list per time step
  47.     atypes = atom_connectivity['atypes']  # Atom ID to atom type mapping
  48.     mtypes = atom_connectivity['mtypes']  # Atom ID to molecule ID mapping
  49.    
  50.     molecule_ch_bond_breaks = {}  # Store first break step for each molecule
  51.    
  52.     sorted_steps = sorted(neighbors.keys())  # Get sorted step keys
  53.    
  54.     for i in range(1, len(sorted_steps)):
  55.         step = sorted_steps[i]
  56.         prev_step = sorted_steps[i - 1]
  57.         adjacency = neighbors[step]
  58.         prev_adjacency = neighbors[prev_step]
  59.        
  60.         for atom_id, bonded_atoms in adjacency.items():
  61.             if atypes[atom_id] == 2:  # Carbon (C) atom
  62.                 molecule_id = mtypes[atom_id]  # Get molecule ID
  63.                
  64.                 # Count the number of Hydrogen (H) bonds
  65.                 initial_hydrogens = sum(1 for neighbor in bonded_atoms if atypes[neighbor] == 1)
  66.                 prev_bonded_atoms = prev_adjacency.get(atom_id, set())
  67.                 prev_hydrogens = sum(1 for neighbor in prev_bonded_atoms if atypes[neighbor] == 1)
  68.                
  69.                 if prev_hydrogens > initial_hydrogens:
  70.                     if molecule_id not in molecule_ch_bond_breaks:
  71.                         molecule_ch_bond_breaks[molecule_id] = atom_id #(step*0.25/1000-325, atom_id)
  72.    
  73.     return molecule_ch_bond_breaks
  74.    
  75.  
  76. for step, neigh in neighbors.items():
  77.     G = nx.Graph(neigh)
  78.     molecules = bfp.get_molecules(neigh)
  79.    
  80.     for molecule in molecules:
  81.         subgraph = G.subgraph(molecule)
  82.         tert_C = []
  83.         for atom in molecule:
  84.             if atypes[atom] == 2:
  85.                 children = subgraph.neighbors(atom)
  86.                 C_children = [x for x in children if atypes[x]==2]
  87.                 N_Carbon = len(C_children)
  88.                 if N_Carbon==3:
  89.                     tert_C.append(atom)
  90.         if N_Carbon==3: draw_backbone(molecule, tert_C)
  91.     break
  92.  
  93.  
  94. carbon_types = {}  # Store classification of each carbon
  95.  
  96. for parent, children in first_neighbors.items():
  97.     if atypes[parent] == 2:  # Only classify Carbon (C=2)
  98.         bonded_carbons = [x for x in children if atypes[x] == 2]
  99.         num_carbons = len(bonded_carbons)
  100.  
  101.         if num_carbons == 1:
  102.             carbon_types[parent] = "Primary"
  103.         elif num_carbons == 2:
  104.             carbon_types[parent] = "Secondary"
  105.         elif num_carbons == 3:
  106.             carbon_types[parent] = "Tertiary"
  107.  
  108. molecule_ch_bond_breaks = find_first_ch_bond_break(atom_connectivity)
  109. #%%
  110. degree = {}
  111. result = {'Primary':0, 'Secondary':0, 'Tertiary':0}
  112. for mid, aid in molecule_ch_bond_breaks.items():
  113.     degree[aid]=carbon_types[aid]
  114.     result[carbon_types[aid]]+=1
  115.    
  116. for ctype in result:
  117.     result[ctype] = result[ctype]
  118.  
  119. fig, ax = plt.subplots(dpi=350)
  120. ax.bar(result.keys(), result.values(), color='tab:red', edgecolor='k')
  121. ax.set_ylabel('# first C-H bond breaking')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement