Advertisement
shihab2196

base_oil_C-C_bond_breaking_count

Dec 20th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.27 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Author: Shihab Ahmed
  4. Created on Tue Dec 19 22:38:15 2023
  5. """
  6.  
  7. import magnolia.bondfile_parser as bfp
  8. import networkx as nx
  9. from networkx.algorithms import isomorphism
  10. from networkx.drawing.nx_agraph import graphviz_layout
  11. import sys
  12. import matplotlib.pyplot as plt
  13. import magnolia.molecular_analysis as man
  14. import pandas as pd
  15. import seaborn as sns
  16. import numpy as np
  17.  
  18. def map_isomorphic_nodes_to_unique_ids(graphs,reference_graph):
  19.     """
  20.    Maps each node in a list of isomorphic graphs to a unique ID based on node position.
  21.  
  22.    :param graphs: List of NetworkX graphs that are isomorphic to each other.
  23.    :return: Dictionary with node IDs as keys and unique IDs (1-30) as values.
  24.    """
  25.     if not graphs:
  26.         return {}
  27.  
  28.     node_to_unique_id = {node: unique_id for unique_id, node in enumerate(reference_graph.nodes, start=1)}
  29.  
  30.     # Map nodes of the other graphs
  31.     for graph in graphs[1:]:
  32.         iso_matcher = isomorphism.GraphMatcher(reference_graph, graph)
  33.         for iso_map in iso_matcher.isomorphisms_iter():
  34.             # Apply the unique ID from the reference node to the corresponding node in this graph
  35.             for ref_node, node in iso_map.items():
  36.                 node_to_unique_id[node] = node_to_unique_id[ref_node]
  37.             break  # Consider only the first mapping
  38.  
  39.     return node_to_unique_id
  40. #%%
  41. sim_dir = ['Sim-1','Sim-2','Sim-3']
  42. baseoil = "PAO4"
  43. ref_flag = True
  44. count = {}
  45.  
  46. for sim in sim_dir:
  47.     directory = r"C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Base_Oil\{}\25_{}_200_O2_Soria\Production\1600\{}".format(baseoil,baseoil,sim)
  48.     bondfilepath = directory+'\\bonds.reaxc'
  49.     cutoff = 0.45
  50.     bonddata = bfp.parsebondfile(bondfilepath,cutoff=cutoff, bo=True)
  51.    
  52.     neighbors = bonddata['neighbours']
  53.     atypes    = bonddata['atypes']
  54.     bondorders= bonddata['bondorders']
  55.     atomsymbols = 'HCO'
  56.    
  57.    
  58.     ## getting a list of baseoil
  59.     oils = []
  60.     firrstneigh = neighbors[0]
  61.     main_graph = nx.Graph(firrstneigh)
  62.     molecules = bfp.get_molecules(firrstneigh)
  63.     for molecule in molecules:
  64.         molGraph = main_graph.subgraph(molecule).copy()
  65.         if len(molecule)==92:
  66.             nodes_to_remove=[node for node in molGraph.nodes if atypes[node]==1]
  67.             molGraph.remove_nodes_from(nodes_to_remove)
  68.             oils.append(molGraph)
  69.    
  70.     if ref_flag:
  71.         reference_graph = oils[0].copy()
  72.         ref_flag = False
  73.    
  74.     carbon_enum = map_isomorphic_nodes_to_unique_ids(oils,reference_graph)
  75.     for ii, values in enumerate(oils):
  76.         oil = values.copy()
  77.         labels = {node:carbon_enum[node] for node in oil.nodes}
  78.         node_color = ['red' if labels[node]==11 else 'lightblue' for node in oil]
  79.         pos = graphviz_layout(oil, prog='neato')
  80.         nx.draw(oil,with_labels=True,labels=labels,node_color=node_color,pos=pos)
  81.         plt.title(f'Carbon Skeleton: {ii+1}')
  82.         plt.savefig(r'C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Post Processing\lammps_processing\python_outputs\graphs\carbon_skeleton_{}_{}'.format(ii+1,sim),dpi=500,bbox_inches='tight')
  83.         plt.show()
  84.    
  85.     if not count:
  86.         for u,v in oil.edges:
  87.             uu = carbon_enum[u]
  88.             vv = carbon_enum[v]
  89.             pair = tuple(sorted((uu,vv)))
  90.             count[pair]=0
  91.    
  92.     bo_cutoff = 0.45
  93.     for step in bondorders:
  94.         for atom1 in bondorders[step]:
  95.             if atypes[atom1]!=2: continue
  96.             for atom2 in bondorders[step][atom1]:
  97.                 if atypes[atom2]!=2: continue
  98.                 bond_order = bondorders[step][atom1].get(atom2,0)
  99.                 if bond_order<bo_cutoff:
  100.                     uu = carbon_enum[atom1]
  101.                     vv = carbon_enum[atom2]
  102.                     pair = tuple(sorted((uu,vv)))
  103.                     if pair in count:
  104.                         count[pair]+=1
  105. #%%
  106. special= 4
  107.  
  108. sorted_items = sorted(count.keys(), key=lambda x: count[x], reverse=True)
  109. top_counts = list(sorted_items)[:special]
  110. colors = ['red' if k in top_counts else 'tab:blue' for k in count]
  111.    
  112. plt.bar(list(map(str,count.keys())),count.values(),color=colors)
  113. plt.xticks(rotation=90,fontsize=7)
  114. plt.savefig(r'C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Post Processing\lammps_processing\python_outputs\figures\carbon_bond_break_count',dpi=500,bbox_inches='tight')
  115. plt.show()
  116.  
  117. top_nodes = list({e for tup in top_counts for e in tup})
  118.  
  119. labels = {node:carbon_enum[node] for node in oil.nodes}
  120. node_color = ['red' if labels[node] in top_nodes else 'tab:blue' for node in oil]
  121. edge_colors = ['red' if tuple(sorted((labels[x],labels[y]))) in top_counts else 'k' for x,y in oil.edges]
  122. edge_widths = [3 if tuple(sorted((labels[x],labels[y]))) in top_counts else 1 for x,y in oil.edges]
  123.  
  124. plt.figure(figsize=[10,6])
  125. pos = graphviz_layout(oil, prog='neato')
  126. nx.draw(oil,with_labels=True,labels=labels,node_color=node_color,pos=pos)
  127. nx.draw_networkx_edges(oil, pos, edge_color=edge_colors,width=edge_widths)
  128. plt.savefig(r'C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Post Processing\lammps_processing\python_outputs\graphs\specieal_carbon_skeleton_{}'.format(baseoil),dpi=500,bbox_inches='tight')
  129. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement