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 plt.xlabel('x')
101 plt.ylabel('y')
102 plt.title(component_label)
103 plt.tight_layout()
104 plt.savefig(output_fname)
105 plt.close()
106
107# Main script execution
108if __name__ == "__main__":
109 if len(sys.argv) != 3:
110 print("Usage: python visualize.py <stress_file> <component: xx|yy|zz|xy|xz|yz>")
111 sys.exit(1)
112
113 filename = sys.argv[1]
114 component_key = sys.argv[2].lower()
115
116 if component_key not in STRESS_COMPONENTS:
117 print("Component must be one of: xx, yy, zz, xy, xz, yz")
118 sys.exit(1)
119
120 component_index, component_label = STRESS_COMPONENTS[component_key]
121
122 base, _ = os.path.splitext(filename)
123 plt_fname = f"{base}_{component_key}.pdf"
124
125 data = read_file(filename)
126
127 # Convert stress to GPa
128 for row in data:
129 row[component_index] *= 160.0
130
131 plot_contour(data, plt_fname, component_index, component_label)
132
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