1+ #!python
2+
3+ import numpy as np
4+ import matplotlib .pyplot as plt
5+ import sys , os
6+
7+ import cellconstructor as CC , cellconstructor .Units
8+
9+
10+ DESCRIPTION = '''
11+ This program plots the results of a SSCHA minimization
12+ saved with the Utilities.IOInfo class.
13+
14+ It plots both frequencies and the free energy minimization.
15+
16+ Many files can be specified, in this case, they are plotted one after the other,
17+ separated by vertical dashed lines.
18+
19+ Usage:
20+
21+ >>> sscha-plot-data.py file1 ...
22+
23+ The code looks for data file called file.dat and file.freqs,
24+ containing, respectively, the minimization data and the auxiliary frequencies.
25+
26+ These files are generated only by python-sscha >= 1.2.
27+
28+ '''
29+
30+
31+ def main ():
32+ print (DESCRIPTION )
33+
34+ if len (sys .argv ) < 2 :
35+ ERROR_MSG = '''
36+ Error, you need to specify at least one file.
37+ Exit failure!
38+ '''
39+ print (ERROR_MSG )
40+ raise ValueError (ERROR_MSG )
41+
42+ plt .rcParams ["font.family" ] = "Liberation Serif"
43+ LBL_FS = 12
44+ DPI = 120
45+ TITLE_FS = 15
46+
47+ freqs_files = [sys .argv [x ] + '.freqs' for x in range (1 , len (sys .argv ))]
48+ minim_files = [sys .argv [x ] + '.dat' for x in range (1 , len (sys .argv ))]
49+
50+ # Check if all the files exist
51+ for f , m in zip (freqs_files , minim_files ):
52+ assert os .path .exists (f ), 'Error, file {} not found.' .format (f )
53+ assert os .path .exists (m ), 'Error, file {} not found.' .format (m )
54+
55+ print ("Reading the input..." )
56+
57+ # Load all the data
58+ freqs_data = np .concatenate ([np .loadtxt (f ) for f in freqs_files ])
59+ minim_data = np .concatenate ([np .loadtxt (f ) for f in minim_files ])
60+
61+ print ("Plotting..." )
62+
63+ # Insert the x axis in the plotting data
64+ xsteps = np .arange (minim_data .shape [0 ])
65+ new_data = np .zeros (( len (xsteps ), 8 ), dtype = np .double )
66+ new_data [:,0 ] = xsteps
67+ new_data [:, 1 :] = minim_data
68+ minim_data = new_data
69+
70+ fig_data , axarr = plt .subplots (nrows = 2 , ncols = 2 , sharex = True , dpi = DPI )
71+
72+ # Plot the data
73+ axarr [0 ,0 ].fill_between (minim_data [:,0 ], minim_data [:,1 ] - minim_data [:, 2 ]* .5 ,
74+ minim_data [:, 1 ] + minim_data [:, 2 ] * .5 , color = "aquamarine" )
75+ axarr [0 ,0 ].plot (minim_data [:,0 ], minim_data [:,1 ], color = "k" )
76+ axarr [0 ,0 ].set_ylabel ("Free energy / unit cell [meV]" , fontsize = LBL_FS )
77+
78+
79+ axarr [0 ,1 ].fill_between (minim_data [:,0 ], minim_data [:,3 ] - minim_data [:, 4 ]* .5 ,
80+ minim_data [:, 3 ] + minim_data [:, 4 ] * .5 , color = "aquamarine" )
81+ axarr [0 ,1 ].plot (minim_data [:,0 ], minim_data [:,3 ], color = "k" )
82+ axarr [0 ,1 ].set_ylabel ("FC gradient" , fontsize = LBL_FS )
83+
84+ axarr [1 ,1 ].fill_between (minim_data [:,0 ], minim_data [:,5 ] - minim_data [:, 6 ]* .5 ,
85+ minim_data [:, 5 ] + minim_data [:, 6 ] * .5 , color = "aquamarine" )
86+ axarr [1 ,1 ].plot (minim_data [:,0 ], minim_data [:,5 ], color = "k" )
87+ axarr [1 ,1 ].set_ylabel ("Structure gradient" , fontsize = LBL_FS )
88+ axarr [1 ,1 ].set_xlabel ("Good minimization steps" , fontsize = LBL_FS )
89+
90+
91+ axarr [1 ,0 ].plot (minim_data [:,0 ], minim_data [:,7 ], color = "k" )
92+ axarr [1 ,0 ].set_ylabel ("Effective sample size" , fontsize = LBL_FS )
93+ axarr [1 ,0 ].set_xlabel ("Good minimization steps" , fontsize = LBL_FS )
94+ fig_data .tight_layout ()
95+
96+
97+ # Now plot the frequencies
98+ fig_freqs = plt .figure (dpi = DPI )
99+ ax = plt .gca ()
100+
101+ N_points , Nw = np .shape (freqs_data )
102+
103+ for i in range (Nw ):
104+ ax .plot (freqs_data [:, i ] * CC .Units .RY_TO_CM )
105+
106+ ax .set_xlabel ("Good minimization steps" , fontsize = LBL_FS )
107+ ax .set_ylabel ("Frequency [cm-1]" , fontsize = LBL_FS )
108+ ax .set_title ("Frequcency evolution" , fontsize = TITLE_FS )
109+ fig_freqs .tight_layout ()
110+
111+ plt .show ()
112+
113+ if __name__ == "__main__" :
114+ main ()
0 commit comments