def __init__(self, basename, time, **kwargs): # Required arguments self.basename = basename self.time = time # Optional arguments self.directory = kwargs.pop('directory', 'C_profiles') self.xlim = kwargs.pop('xlim', None) self.ylim = kwargs.pop('ylim', None) self.title = kwargs.pop('title', None) self.mirror = kwargs.pop('mirror', True) self.callback = kwargs.pop('callback', None) # Instantiate CProfiles and load carbon profiles self.cprofiles = CProfiles(self.basename, self.directory) self.cprofiles.load_cprofiles() self.cprofiles.load_time() self.time_idx = self.cprofiles.where_tlist(self.time, appendto=[]) # Matplotlib Figure object self.fig, self.ax = plt.subplots(**kwargs) # Plotting options self.ax.set_xlabel(u'Posição ' + r'($\mu m$)') self.ax.set_ylabel(u'Teor de carbono (% peso)') self.ax.set_xlim(self.xlim) self.ax.set_ylim(self.ylim) if self.title: self.ax.set_title(self.title) i = self.time_idx[0] z, c, t = self.cprofiles.get_cprofile(i, self.mirror, self.x2wp) self.line, = self.ax.plot(z, c, 'k-') axis = '' if self.xlim is None: axis += 'x' if self.ylim is None: axis += 'y' axis = axis.replace('xy', 'both') if axis: self.ax.autoscale(True, axis=axis, tight=True) self.ax.autoscale(False) self.time_text = self.ax.text(0.02, 0.98, '', va='top', transform=self.ax.transAxes) self.ani = None self.artists = [] # Run callback function if self.callback is not None: self.artists += self.callback(self.ax)
class CPartitionAnimation(object): # Site fraction of substitutional elements in the ductile cast iron yalloy = dict(Cu=3.55354266E-3, Mn=2.05516602E-3, Si=5.02504411E-2, Fe=9.4414085022e-1) def __init__(self, basename, time, **kwargs): # Required arguments self.basename = basename self.time = time # Optional arguments self.directory = kwargs.pop('directory', 'C_profiles') self.xlim = kwargs.pop('xlim', None) self.ylim = kwargs.pop('ylim', None) self.title = kwargs.pop('title', None) self.mirror = kwargs.pop('mirror', True) self.callback = kwargs.pop('callback', None) # Instantiate CProfiles and load carbon profiles self.cprofiles = CProfiles(self.basename, self.directory) self.cprofiles.load_cprofiles() self.cprofiles.load_time() self.time_idx = self.cprofiles.where_tlist(self.time, appendto=[]) # Matplotlib Figure object self.fig, self.ax = plt.subplots(**kwargs) # Plotting options self.ax.set_xlabel(u'Posição ' + r'($\mu m$)') self.ax.set_ylabel(u'Teor de carbono (% peso)') self.ax.set_xlim(self.xlim) self.ax.set_ylim(self.ylim) if self.title: self.ax.set_title(self.title) i = self.time_idx[0] z, c, t = self.cprofiles.get_cprofile(i, self.mirror, self.x2wp) self.line, = self.ax.plot(z, c, 'k-') axis = '' if self.xlim is None: axis += 'x' if self.ylim is None: axis += 'y' axis = axis.replace('xy', 'both') if axis: self.ax.autoscale(True, axis=axis, tight=True) self.ax.autoscale(False) self.time_text = self.ax.text(0.02, 0.98, '', va='top', transform=self.ax.transAxes) self.ani = None self.artists = [] # Run callback function if self.callback is not None: self.artists += self.callback(self.ax) def x2wp(self, x): return x2wp(x, y=self.yalloy) def initialize_plot(self): self.tprevious = 0 self.line.set_data([], []) self.time_text.set_text('') return [self.line, self.time_text] + self.artists def plot_step(self, *args): z, c, t = args[0] spf = t - self.tprevious # seconds per frame self.tprevious = t self.line.set_data(z, c) string = 't = {:g} s'.format(t) string += '\n' string += '{:g} s / quadro'.format(spf) self.time_text.set_text(string) return [self.line, self.time_text] + self.artists def frames(self): for i in self.time_idx: z, c, t = self.cprofiles.get_cprofile(i, self.mirror, self.x2wp) yield z, c, t def animate(self, **kwargs): kw = dict(interval=25, blit=True) kw.update(kwargs) self.ani = animation.FuncAnimation(fig=self.fig, func=self.plot_step, frames=self.frames, init_func=self.initialize_plot, **kw) return self.ani def save_animation(self, fout=None, directory='', **kwargs): if self.ani is not None: try: if fout is None: fout = os.path.join(directory, self.cprofiles.basename) + '.mp4' self.ani.save(fout, **kwargs) except: raise else: print('Animation successfully saved in "{}"'.format(fout)) else: print('Run animate() first')
# y, c0, and WBs for the high manganese, high carbon region (cell boundary) y = dict(Si=0.034356055114394574, Mn=0.003731497480722358, Cu=0.0027562013887263755) y['Fe'] = 1. - sum(y.values()) c0 = x2wp(3.8553489675740495e-2, y=y) WBs = 1.5424 ########################################################### labels = [('aus1', r'$\gamma$', 1), ('aus2', r'$\gamma$', 1), ('fer1', r'$\alpha_{b}$', 1), ('fer2', r'$\alpha_{b}$', 1), ('fer3', r'$\alpha_{b}$', 1)] p0 = CProfiles('bainite_FoFo_375', '../C_profiles') ax0.axhline(c0, ls=':', color='k', lw=1) ax0.axhline(WBs, ls=':', color='k', lw=1) ax0.text(-1.1, c0, r'c$_0$', ha='left', va='bottom', size=10) ax0.text(-1.15, WBs, 'WBs', ha='left', va='bottom', size=10) p0.plot_cprofiles(tlist=[1, 10, 100, 300], ax=ax0, func=lambda x: x2wp(x, y=y), mirror=True, lw=1) ax0.set_xlim(-1.16, 1.16) ax0.set_ylim(-.02, 1.9) ax0.set_title('Bainite (cell boundary)', y=1.06, size=12) add_label(ax0, 'a)', py=0)
labels += [('mart', r"$\alpha' + \theta$", 1)] else: labels += [('mart', r"$\alpha'$", 1)] else: labels = [('aus1', r'$\gamma_1$', -1), ('aus2', r'$\gamma_2$', -1), ('aust', r'$\gamma$', -1), ('fer1', r'$\alpha_{b1}$', 1), ('fer2', r'$\alpha_{b2}$', 1)] if 'CCEpara' in basename or 'CCEortho' in basename or 'mu' in basename: labels += [('mart', r"$\alpha' + \theta$", -1)] else: labels += [('mart', r"$\alpha'$", -1)] try: fig, ax = plt.subplots(figsize=args.figsize) cprofiles = CProfiles(basename, 'C_profiles') last_t = None for t in args.time: j = cprofiles.where_tlist([t], []) if len(j) > 0: j = j[0] strct = cprofiles.ss[j] cprofiles.plot_cprofiles( ax=ax, mirror=args.mirror, func=lambda x: x2mu(x, strct, args.mu, args.sol), tlist=[t]) last_t = t except Exception: print('Failed to plot "{}"'.format(basename))
'font.size': 13, 'mathtext.fontset': 'stix' }) y = dict(Cu=3.55354266E-3, Mn=2.05516602E-3, Si=5.02504411E-2, Fe=9.4414085022e-1) parser = argparse.ArgumentParser() parser.add_argument('basenames', nargs='+') parser.add_argument('-S', '--save', action='store_true', help='Save plot') args = parser.parse_args() for basename in args.basenames: cprofiles = CProfiles(basename) print(cprofiles.basename) ax = cprofiles.plot_colormap(mirror=True, func=lambda x: x2wp(x, y=y), vmin=0, vmax=1.8) ax.set_xlabel(u'Position (μm)') ax.set_ylabel('Time (s)') ax.set_title(cprofiles.basename) if args.save: plt.savefig(os.path.join('img', cprofiles.basename + '.png'), dpi=150) plt.close()
labels = [('aus1', r'$\gamma_1$', -1), ('aus2', r'$\gamma_2$', -1), ('aust', r'$\gamma$', -1), ('fer1', r'$\alpha_{b1}$', 1), ('fer2', r'$\alpha_{b2}$', 1)] if 'CCEpara' in basename or 'CCEortho' in basename or 'mu' in basename: labels += [('mart', r"$\alpha' + \theta$", -1)] else: labels += [('mart', r"$\alpha'$", -1)] tracked_interfaces = [('aus1.sn', 'aus1.cin'), ('aus2.s0', 'aus2.ci0'), ('aus2.sn', 'aus2.cin')] try: fig, ax = plt.subplots(figsize=args.figsize) cprofiles = CProfiles(basename) cprofiles.plot_cprofiles(ax=ax, tlist=args.time, mirror=args.mirror, func=lambda x: x2wp(x, y=y)) except Exception: print('Failed to plot "{}"'.format(basename)) plt.close() else: ax.set_xlim(*args.xlim) ax.set_ylim(*args.ylim) ax.set_xlabel(u'Posição (μm)') ax.set_ylabel('Teor de carbono (%)') ax.legend(loc=dictloc[args.loc], fancybox=False)
labels += [('mart', r"$\alpha' + \theta$", 1)] else: labels += [('mart', r"$\alpha'$", 1)] else: labels = [('aus1', r'$\gamma_1$', -1), ('aus2', r'$\gamma_2$', -1), ('aust', r'$\gamma$', -1), ('fer1', r'$\alpha_{b1}$', 1), ('fer2', r'$\alpha_{b2}$', 1)] if 'CCEpara' in basename or 'CCEortho' in basename or 'mu' in basename: labels += [('mart', r"$\alpha' + \theta$", -1)] else: labels += [('mart', r"$\alpha'$", -1)] cprofiles = CProfiles(basename, 'C_profiles') if args.tracking: pos = pd.read_csv( 'pos_extremities/{}.txt'.format(cprofiles.basename), sep=' ') ci = pd.read_csv( 'C_extremities/{}.txt'.format(cprofiles.basename), sep=' ') fig, axes = plt.subplots(nrow, ncol, figsize=(4*ncol, 3*nrow)) # plt.subplots_adjust(wspace=.5, hspace=.5) axes = axes.ravel() for i, (t, ax) in enumerate(zip(args.time, axes)): kw = dict(lw=1) if not isinstance(t, list): t = [t]