MDStressLab++
Loading...
Searching...
No Matches
visualize.py
Go to the documentation of this file.
32
33import sys
34import os
35import numpy as np
36import matplotlib.pyplot as plt
37from scipy.interpolate import griddata
38
39
41STRESS_COMPONENTS = {
42 'xx': (3, r'$\sigma_{xx}$'),
43 'yy': (4, r'$\sigma_{yy}$'),
44 'zz': (5, r'$\sigma_{zz}$'),
45 'xy': (6, r'$\sigma_{xy}$'),
46 'xz': (7, r'$\sigma_{xz}$'),
47 'yz': (8, r'$\sigma_{yz}$'),
48}
49
50
61def read_file(filename):
62 data = []
63 with open(filename, 'r') as f:
64 lines = f.readlines()
65 for line in lines[2:]: # Skip count and metadata header
66 line = line.strip()
67 if not line:
68 continue
69 try:
70 parts = list(map(float, line.split()))
71 if len(parts) >= 9:
72 data.append(parts[:9])
73 except ValueError:
74 continue
75 return data
76
77
86def plot_contour(data, output_fname, component_index, component_label):
87 x = [row[0] for row in data]
88 y = [row[1] for row in data] # using x-y plane for plotting
89 sig = [row[component_index] for row in data]
90
91 xi = np.linspace(min(x), max(x), 300)
92 yi = np.linspace(min(y), max(y), 300)
93 xi, yi = np.meshgrid(xi, yi)
94
95 sig_grid = griddata((x, y), sig, (xi, yi), method='linear')
96
97 plt.figure(figsize=(6, 5))
98 contour = plt.contourf(xi, yi, sig_grid, levels=100, cmap='RdBu_r')
99 #plt.colorbar(contour, label='Stress (GPa)')
100 cbar = plt.colorbar(contour, orientation='horizontal', pad=0.15) # pad adjusts distance from plot
101 cbar.set_label('Stress (GPa)')
102 plt.xlabel('x')
103 plt.ylabel('y')
104 plt.title(component_label)
105 plt.gca().set_aspect('equal', adjustable='box')
106 plt.tight_layout()
107 plt.savefig(output_fname)
108 plt.close()
109
110# Main script execution
111if __name__ == "__main__":
112 if len(sys.argv) != 3:
113 print("Usage: python visualize.py <stress_file> <component: xx|yy|zz|xy|xz|yz>")
114 sys.exit(1)
115
116 filename = sys.argv[1]
117 component_key = sys.argv[2].lower()
118
119 if component_key not in STRESS_COMPONENTS:
120 print("Component must be one of: xx, yy, zz, xy, xz, yz")
121 sys.exit(1)
122
123 component_index, component_label = STRESS_COMPONENTS[component_key]
124
125 base, _ = os.path.splitext(filename)
126 plt_fname = f"{base}_{component_key}.pdf"
127
128 data = read_file(filename)
129
130 # Convert stress to GPa
131 for row in data:
132 row[component_index] *= 160.0
133
134 plot_contour(data, plt_fname, component_index, component_label)
135
plot_contour(data, output_fname, component_index, component_label)
Plots a 2D contour of the selected stress component.
Definition visualize.py:86
read_file(filename)
Reads a .stress file containing atomistic stress data.
Definition visualize.py:61