示例#1
0
            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))
            plt.close()
        else:
            ax.set_xlim(args.xlim)
            ax.set_ylim(args.ylim)
示例#2
0
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')
示例#3
0
        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]
                kw['color'] = next(colorcycle)

            cprofiles.plot_cprofiles(ax=ax, mirror=True,
                                     func=lambda x: x2wp(x, y=y),
                                     tlist=t, **kw)
            if i == 0:
                j = cprofiles.where_tlist(t, [])
                if len(j) > 0:
                    j = j[0]
                    idx, = np.where(cprofiles.ss[j] == 'aus1')
                    idx = idx[0]
                    zmax = 2*cprofiles.zz[j][-1] - cprofiles.zz[j][idx]
                    cmax = cprofiles.cc[j][idx]

            if args.tracking:
                ax.plot(pos['aus1.sn'], x2wp(ci['aus1.cin'], y=y), 'k:')

            ax.set_ylim(*args.ylim)
            ax.set_xlim(*args.xlim)

            if len(t) == 1:
                ax.text(.01, .85, s='{:g} s'.format(t[0]),