Advertisement
shihab2196

draw_backbon_with_carbon_source_colored

Jan 26th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 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.  
  15. baseoil = "PAO4"
  16. directory = r"C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Base_Oil\{}\25_{}_200_O2_Soria\Production\1600\Sim-1".format(baseoil,baseoil)
  17. bondfilepath = directory+'\\bonds.reaxc'
  18. cutoff = 0.35
  19. bonddata = bfp.parsebondfile(bondfilepath,cutoff=cutoff, mtypes=True,bo=True)
  20. #%% seeking from the last step
  21. neighbors = bonddata['neighbours']
  22. atypes    = bonddata['atypes']
  23. mtypes    = bonddata['mtypes']
  24. bondorders= bonddata['bondorders']
  25. atomsymbols = 'HCO'
  26.  
  27. seek = 'H2CO'
  28. seek_molecules = set()
  29.  
  30. laststep_neigh     = list(neighbors.values())[-1]
  31. laststep           = list(neighbors.keys())[-1]
  32. laststep_molecules = list(bfp.get_molecules(laststep_neigh))
  33.  
  34. for molecule in laststep_molecules:
  35.     species = bfp.get_molecular_formula(molecule, atypes, atomsymbols)
  36.     if species==seek and frozenset(molecule) not in seek_molecules:
  37.         seek_molecules.add(frozenset(molecule))
  38.         print(molecule)
  39.         for atom1 in molecule:
  40.             for atom2 in molecule:
  41.                 if atom1>atom2:
  42.                     sym1 = atomsymbols[atypes[atom1]-1]
  43.                     sym2 = atomsymbols[atypes[atom2]-1]
  44.                     bb = bondorders[laststep][atom1].get(atom2,None)
  45.                     if bb: print((sym1,sym2),bb)
  46. #%% carbon skeleton
  47. def draw_backbone(molecule_type,carbons=[]):
  48.     firststep_neigh     = list(neighbors.values())[0]
  49.     firststep_graph     = nx.Graph(firststep_neigh)
  50.     molecule            = [k for k,v in mtypes.items() if v==molecule_type]
  51.    
  52.     subgraph = firststep_graph.subgraph(molecule)
  53.     H_nodes  = [x for x in molecule if atypes[x]==1]
  54.     backbone = subgraph.copy()
  55.     backbone.remove_nodes_from(H_nodes)
  56.     center   = nx.center(backbone)
  57.     print(f'center = {center}')
  58.    
  59.     ## Draw graph
  60.     node_color = []
  61.     for node in backbone.nodes:
  62.         if node in carbons: node_color.append('tab:red')
  63.         else: node_color.append('tab:blue')
  64.        
  65.     pos = nx.kamada_kawai_layout(backbone)
  66.     plt.figure(figsize=[6,4])
  67.     nx.draw(backbone,pos=pos,node_size=150,node_color=node_color)
  68. #%% Carbon Sources
  69. savedir = r'C:\Users\arup2\OneDrive - University of California Merced\Desktop\LAMMPS\Post Processing\lammps_processing\python_outputs\pathway'
  70. for i, track in enumerate(seek_molecules):
  71.     carbons = [x for x in track if atypes[x]==2]
  72.     molecule_type = mtypes[carbons[0]]
  73.     draw_backbone(molecule_type,carbons)
  74.     plt.title(f'{i}:  {seek}',fontsize=30)
  75.     plt.savefig(savedir+f'\\backbone_{i}.png', dpi=500, bbox_inches='tight')
  76.     # plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement