shihab2196

BASEOIL_reaction_pathway_figures_nx_draw

Jan 29th, 2024
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.11 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Author: Shihab Ahmed
  4. Created on Thu Jan 25 12:20:35 2024
  5. """
  6. ## update species pathway tracker
  7.  
  8. import magnolia.bondfile_parser as bfp
  9. import networkx as nx
  10. import sys
  11. import matplotlib.pyplot as plt
  12. import magnolia.molecular_analysis as man
  13. import numpy as np
  14. import random
  15. import os
  16.  
  17. baseoil = "PAO4"
  18. cutoff = 0.35
  19. bonddata = {}
  20. simdir = ['Sim-1','Sim-2','Sim-3']
  21. for sim in simdir:
  22.     directory = r"C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Base_Oil\{}\25_{}_200_O2_Soria\Production\1600\{}".format(baseoil,baseoil,sim)
  23.     bondfilepath = directory+'\\bonds.reaxc'
  24.     bonddata[sim] = bfp.parsebondfile(bondfilepath,cutoff=cutoff, mtypes=True,bo=True)
  25. #%% seeking from the last step
  26. sim = 'Sim-2'
  27.  
  28. neighbors = bonddata[sim]['neighbours']
  29. atypes    = bonddata[sim]['atypes']
  30. mtypes    = bonddata[sim]['mtypes']
  31. bondorders= bonddata[sim]['bondorders']
  32. atomsymbols = 'HCO'
  33.  
  34. seek = 'H2CO2'
  35. seek_molecules = set()
  36.  
  37. laststep_neigh     = list(neighbors.values())[-1]
  38. laststep           = list(neighbors.keys())[-1]
  39. laststep_molecules = list(bfp.get_molecules(laststep_neigh))
  40.  
  41. for molecule in laststep_molecules:
  42.     species = bfp.get_molecular_formula(molecule, atypes, atomsymbols)
  43.     if species==seek and frozenset(molecule) not in seek_molecules:
  44.         seek_molecules.add(frozenset(molecule))
  45.         print(molecule)
  46.         for atom1 in molecule:
  47.             for atom2 in molecule:
  48.                 if atom1>atom2:
  49.                     sym1 = atomsymbols[atypes[atom1]-1]
  50.                     sym2 = atomsymbols[atypes[atom2]-1]
  51.                     bb = bondorders[laststep][atom1].get(atom2,None)
  52.                     if bb: print((sym1,sym2),bb)
  53. #%% tracking a single seeking molecule in reveres step order
  54. def find_all_largest_consecutive_sequences(arr):
  55.     if not arr:
  56.         return []
  57.  
  58.     arr.sort()  # Sort the input list
  59.  
  60.     longest_sequences = []  # Initialize a list to store all longest sequences
  61.     current_sequence = [arr[0]]  # Initialize the current sequence with the first element
  62.  
  63.     for i in range(1, len(arr)):
  64.         if arr[i] == arr[i - 1] + 1:
  65.             current_sequence.append(arr[i])
  66.         else:
  67.             # Check if the current sequence is longer than or equal to the longest sequence(s)
  68.             if not longest_sequences or len(current_sequence) == len(longest_sequences[0]):
  69.                 longest_sequences.append(current_sequence.copy())
  70.             elif len(current_sequence) > len(longest_sequences[0]):
  71.                 longest_sequences = [current_sequence.copy()]
  72.             current_sequence = [arr[i]]
  73.  
  74.     # Check again after the loop ends in case the longest sequence(s) is/are at the end
  75.     if not longest_sequences or len(current_sequence) == len(longest_sequences[0]):
  76.         longest_sequences.append(current_sequence.copy())
  77.     elif len(current_sequence) > len(longest_sequences[0]):
  78.         longest_sequences = [current_sequence.copy()]
  79.  
  80.     return np.array(longest_sequences)
  81.  
  82. print(seek)
  83. for i in range(len(seek_molecules)):
  84.     if i!=1: continue
  85.  
  86.     track          = list(seek_molecules)[i]
  87.     exist_in_frame = []
  88.    
  89.     reversed_keys = list(neighbors.keys())[::-1]
  90.     total_frame   = len(reversed_keys)
  91.     for f, key in enumerate(reversed_keys):
  92.         frame = total_frame-f-1
  93.         neigh = neighbors[key]
  94.         molecules = bfp.get_molecules(neigh)
  95.         for molecule in molecules:
  96.             if track == molecule:
  97.                 exist_in_frame.append(frame)
  98.                 bingo = True
  99.            
  100.                
  101.     result = find_all_largest_consecutive_sequences(exist_in_frame)
  102.     appeared, life = result.shape
  103.     print(f"seeking id: {i}: appeared = {appeared}, life = {life}")
  104. #%%
  105. # working with single track
  106. parents = {}
  107. for frame, (step,neigh) in enumerate(neighbors.items()):
  108.     parents[frame]=[]
  109.     molecules = bfp.get_molecules(neigh)
  110.     for molecule in molecules:
  111.         if track & molecule:
  112.             parents[frame].append(molecule)
  113. #%%
  114. #draw molecule
  115. def draw_backbone(molecule,frame,show_H=False):
  116.     species = bfp.get_molecular_formula(molecule, atypes, atomsymbols)
  117.     step = list(neighbors.keys())[frame]
  118.     neigh = neighbors[step]
  119.     G     = nx.Graph(neigh)    
  120.     subgraph = G.subgraph(molecule)
  121.     H_nodes  = [x for x in molecule if atypes[x]==1]
  122.     backbone = subgraph.copy()
  123.        
  124.     if not show_H:
  125.         backbone.remove_nodes_from(H_nodes)
  126.    
  127.     ## Draw graph
  128.     node_color = []
  129.     node_size  = []
  130.     node_shape = []
  131.     for node in backbone.nodes:
  132.         if node in track:
  133.             # node_color.append('gold')
  134.             if atypes[node]==3:
  135.                 node_size.append(100)
  136.                 node_color.append('tab:red')
  137.                 node_shape.append('blue')
  138.             elif atypes[node]==2:
  139.                 node_color.append('tab:grey')
  140.                 node_size.append(180)
  141.                 node_shape.append('blue')
  142.             else:
  143.                 node_size.append(30)
  144.                 node_color.append('white')
  145.                 node_shape.append('blue')
  146.         elif atypes[node]==3:
  147.             node_color.append('tab:red')
  148.             node_size.append(100)
  149.             node_shape.append('k')
  150.         elif atypes[node]==2:
  151.             node_color.append('tab:grey')
  152.             node_size.append(180)
  153.             node_shape.append('k')
  154.         elif atypes[node]==1:
  155.             node_color.append('white')
  156.             node_size.append(30)
  157.             node_shape.append('k')
  158.         else:
  159.             node_color.append('gold')
  160.             node_size.append(350)
  161.             node_shape.append('k')
  162.    
  163.     edge_color = []
  164.     edge_thickness = []
  165.     db_cutoff  = 1.3 # double bond cutoff
  166.     for u,v in backbone.edges:
  167.         if bondorders[step][u][v]>db_cutoff:
  168.             edge_color.append('tab:red')
  169.             edge_thickness.append(3)
  170.         else:
  171.             edge_color.append('k')
  172.             edge_thickness.append(1.0)
  173.    
  174.     pos = nx.kamada_kawai_layout(backbone)
  175.     plt.figure(figsize=[10,8])
  176.    
  177.     nx.draw(backbone,pos=pos,node_size=node_size,node_color=node_color,
  178.             edgecolors=node_shape, edge_color=edge_color,
  179.             width=edge_thickness)
  180.     plt.title(f'frame-{frame}: {species}',fontsize=20)        
  181. #%% Analyze the parents
  182. save = r'C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Post Processing\lammps_processing\python_outputs\graphs'
  183. spec = bfp.get_molecular_formula(track, atypes, atomsymbols)
  184. dirr = save+f'\\{baseoil}_{spec}'
  185. if not os.path.exists(dirr):
  186.     os.makedirs(dirr)
  187.  
  188. check_list = []    
  189. for frame in range(5301):
  190.     if frame%100==0: print(frame)
  191.     parent = parents[frame]
  192.    
  193.        
  194.     for molecule in parent:
  195.         species = bfp.get_molecular_formula(molecule, atypes, atomsymbols)
  196.         if species not in check_list:
  197.             draw_backbone(molecule, frame,show_H=True)
  198.             rand  = random.randint(0,1000000)
  199.             plt.savefig(dirr+f'\\graph_{frame}_{species}.png',dpi=500,bbox_inches='tight')
  200.             plt.close()
  201.             check_list.append(species)
  202.    
Add Comment
Please, Sign In to add comment