Advertisement
shihab2196

first_C-C_bond_breaking_three_simulations

Jan 29th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.58 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Author: Shihab Ahmed
  4. Created on Sun Jan 28 20:00:46 2024
  5. """
  6.  
  7. import magnolia.bondfile_parser as bfp
  8. import networkx as nx
  9. import sys
  10. import matplotlib.pyplot as plt
  11. import magnolia.molecular_analysis as man
  12. import numpy as np
  13. import seaborn as sns
  14. import random
  15. from collections import Counter
  16.  
  17. baseoil = "PAO4"
  18. simdirr = ['Sim-1','Sim-2','Sim-3']
  19. ref_flag = True
  20. bonds_df = {}
  21.  
  22. for sim in simdirr:
  23.     print(f'Simulations: {sim}')
  24.     bonds_df[sim] = []
  25.     directory = r"C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Base_Oil\{}\25_{}_200_O2_Soria\Production\1600\{}".format(baseoil,baseoil,sim)
  26.     bondfilepath = directory+'\\bonds.reaxc'
  27.     cutoff = 0.35
  28.     bonddata = bfp.parsebondfile(bondfilepath,cutoff=cutoff, mtypes=True,bo=True)
  29.     #%% mapping edges of all base oil to reference base oil (C-C bonds only)
  30.     neighbors = bonddata['neighbours']
  31.     atypes    = bonddata['atypes']
  32.     mtypes    = bonddata['mtypes']
  33.     bondorders= bonddata['bondorders']
  34.     atomsymbols = 'HCO'
  35.    
  36.     n=25
  37.     skipts = (1600-300)/4
  38.     tick_fontsize  = 8
  39.     label_fontsize = 15
  40.     vmin   = 0
  41.     vmax   = 2
  42.     timestep = 0.25
  43.  
  44.  
  45.    
  46.     for i in range(n):
  47.         firststep_neigh     = list(neighbors.values())[0]
  48.         firststep_graph     = nx.Graph(firststep_neigh)
  49.         # (i+1)th base oil molecule (1-25)
  50.         molecule            = [k for k,v in mtypes.items() if v==i+1]
  51.        
  52.         # molecule graph
  53.         subgraph = firststep_graph.subgraph(molecule)
  54.         H_nodes  = [x for x in molecule if atypes[x]==1]
  55.         backbone = subgraph.copy()
  56.         # backbone of molecule-{i+1}
  57.         backbone.remove_nodes_from(H_nodes)
  58.         center = nx.center(backbone)
  59.         #getting reference graph
  60.         if ref_flag:
  61.             ref_id = i+1
  62.             ref = backbone.copy()
  63.             ref_flag = False
  64.            
  65.         # graph matcher object
  66.         GM = nx.isomorphism.GraphMatcher(ref, backbone)
  67.         if GM.is_isomorphic():
  68.             mapped_edges = []
  69.             mapping = GM.mapping
  70.             for u,v in ref.edges():
  71.                 u_map,v_map = mapping[u], mapping[v]
  72.                 mapped_edge = (u_map,v_map)
  73.                 mapped_edges.append(mapped_edge)
  74.                 print(f'({u},{v})={mapped_edge}',end='\t')
  75.             print(f'\n========={i+1}=========')
  76.            
  77.         else:
  78.             raise ValueError(f'{i+1} is not isomorphic to {ref}')
  79.            
  80.            
  81.         df = bfp.bondorder_evolution(bondorders, mapped_edges, ts=timestep,
  82.                                      skipts=skipts,plot='no')
  83.         bonds_df[sim].append(df)
  84.        
  85.         ## Draw 1st graphs
  86.         if i==0:
  87.             node_color = []
  88.             for node in backbone.nodes:
  89.                 if atypes[node]==3: node_color.append('red')
  90.                 else: node_color.append('tab:blue')
  91.                
  92.             pos = nx.kamada_kawai_layout(backbone)
  93.             for key,value in pos.items():
  94.                 pos[key]=2*value
  95.                
  96.             plt.figure(figsize=[8,6])
  97.             node_size = 150
  98.             nx.draw(backbone,pos=pos,node_size=node_size,node_color=node_color)
  99.            
  100.             title = f'{i+1}'
  101.             plt.title(title,fontsize=30)
  102.            
  103.             for k, (u, v) in enumerate(ref.edges):
  104.                 u_map,v_map = mapping[u], mapping[v]
  105.                
  106.                 A = pos[u_map]
  107.                 B = pos[v_map]
  108.                 C = (A+B)/2 #+ np.array([-0.16,-0.06])
  109.                
  110.                 L8 = [mapping[x] for x in range(23,30+1)]+[mapping[11]]
  111.                 L10 = [mapping[x] for x in range(1,10+1)]+[mapping[11]]
  112.                 L11 = [mapping[x] for x in range(12,22+1)]+[mapping[11]]
  113.                
  114.                
  115.                 fontsize = 12
  116.                 if u_map in L8 and v_map in L8:
  117.                     # C+=np.array([0.1,0.1])
  118.                     shift = [0.0,0.0]
  119.                     pos_L8 = C+np.array(shift)
  120.                     plt.text(*pos_L8,f'{k+1}',color='red',fontsize=fontsize)
  121.                     nx.draw_networkx_nodes(backbone, pos=pos, node_size=node_size,
  122.                                            node_color='tab:red', edgecolors='black',
  123.                                            nodelist=[u,v])
  124.                 elif u_map in L10 and v_map in L10:
  125.                     shift = [0.01,-0.06]
  126.                     pos_L10=C+np.array(shift)
  127.                     plt.text(*pos_L10,f'{k+1}',color='darkgreen',fontsize=fontsize)
  128.                     nx.draw_networkx_nodes(backbone, pos=pos, node_size=node_size,
  129.                                            node_color='tab:green', edgecolors='black',
  130.                                            nodelist=[u,v])
  131.                 elif u_map in L11 and v_map in L11:
  132.                     shift = [-0.05,0.04]
  133.                     if {u,v}=={13,22}: shift = [0,0]
  134.                     pos_L11 = C+np.array(shift)
  135.                     plt.text(*pos_L11,f'{k+1}',color='blue',fontsize=fontsize)
  136.                     nx.draw_networkx_nodes(backbone, pos=pos, node_size=node_size,
  137.                                            node_color='tab:blue', edgecolors='black',
  138.                                            nodelist=[u,v])
  139.                 else:
  140.                     print('something is wrong',(u_map,v_map))
  141.             # color the center
  142.             nx.draw_networkx_nodes(backbone, pos=pos, node_size=node_size,
  143.                                    node_color='tab:grey', edgecolors='black',
  144.                                    nodelist=center)
  145.                    
  146.             plt.text(0.4,-1.0,'$L_{8}$',fontsize=fontsize+15)
  147.             plt.text(-0.9,-0.7,'$L_{11}$',fontsize=fontsize+15)
  148.             plt.text(0.3,1.3,'$L_{10}$',fontsize=fontsize+15)
  149.             rand = random.randint(0,1000000)
  150.             savedir = r'C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Post Processing\lammps_processing\python_outputs\graphs'
  151.             plt.savefig(savedir+f'\\marking_edge_PAO4_{rand}.png', dpi=500,
  152.                         bbox_inches='tight')
  153.             plt.show()
  154.     #%%---first bond breaking----
  155.    
  156. label_fontsize = 15
  157. tick_fontsize = 15
  158. print(f'Bond Order Cutoff: {cutoff}')
  159. first_break = {x:0 for x in range(1,29+1)}
  160. for sim in simdirr:
  161.     print(sim)
  162.     for i,df in enumerate(bonds_df[sim]):
  163.         bond_break_times = []
  164.         for mol_id in df.index:
  165.             bond = df.loc[mol_id] # panda series
  166.            
  167.             # strict condition
  168.             change = bond<cutoff
  169.             broke = change[change].index
  170.             if not broke.empty:
  171.                 bond_break_times.append(broke.min())
  172.             else:
  173.                 bond_break_times.append(np.inf)
  174.         minn = min(bond_break_times)
  175.         bond_id = bond_break_times.index(minn)+1
  176.         print(f'Mol-{i+1}: First break of bond {bond_id} at {minn}')        
  177.         if minn!=np.inf:
  178.             first_break[bond_id]+=1
  179.  
  180. plt.style.use('default')
  181. fig, ax = plt.subplots(figsize=[8,6])
  182. ax.bar(first_break.keys(),first_break.values(),color='#FFD54F',
  183.         edgecolor='black')
  184. ax.set_xlabel('Bond index',fontsize=label_fontsize)
  185. ax.set_ylabel('C-C bond dissociation count',fontsize=label_fontsize)
  186. ax.tick_params(axis='both', labelsize=tick_fontsize)
  187.  
  188. rand = random.randint(0,1000000)
  189. savedir2 = r'C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Post Processing\lammps_processing\python_outputs\figures'
  190. fig.savefig(savedir2+'\\bond_disso_count_{}.png'.format(rand),dpi=500,bbox_inches='tight')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement