示例#1
0
 def _plot_ke_history(self):
     from pysph.tools.pprocess import get_ke_history
     from matplotlib import pyplot as plt
     t, ke = get_ke_history(self.output_files, 'fluid')
     plt.clf()
     plt.plot(t, ke)
     plt.xlabel('t')
     plt.ylabel('Kinetic energy')
     fig = os.path.join(self.output_dir, "ke_history.png")
     plt.savefig(fig, dpi=300)
     return t, ke
示例#2
0
文件: rve.py 项目: nilsmeyerkit/pysph
    def post_process(self, info_fname):
        """Save fiber orientation tensor to csv file."""
        if len(self.output_files) == 0:
            return

        from pysph.tools.pprocess import get_ke_history
        from matplotlib import pyplot as plt

        t, ke = get_ke_history(self.output_files, "fluid")
        plt.clf()
        plt.plot(t, ke)
        plt.xlabel("t")
        plt.ylabel("Kinetic energy")
        fig = os.path.join(self.output_dir, "ke_history.png")
        plt.savefig(fig, dpi=300)

        # empty list for time
        t = []

        # empty lists for fiber orientation tensors
        A = []

        # iteration over all output files
        output_files = remove_irrelevant_files(self.output_files)
        for fname in output_files:
            data = load(fname)

            # extracting time
            t.append(data["solver_data"]["t"])

            if self.n > 0:
                # extrating all arrays.
                directions = []
                fiber = data["arrays"]["fibers"]
                startpoints = [i * (self.options.lf - 1) for i in range(0, self.n)]
                endpoints = [
                    i * (self.options.lf - 1) - 1 for i in range(1, self.n + 1)
                ]
                for start, end in zip(startpoints, endpoints):
                    px = np.mean(fiber.rxnext[start:end])
                    py = np.mean(fiber.rynext[start:end])
                    pz = np.mean(fiber.rznext[start:end])

                    n = np.array([px, py, pz])
                    norm = np.linalg.norm(n)
                    if norm == 0:
                        p = np.array([1, 0, 0])
                    else:
                        p = n / norm
                    directions.append(p)

                N = len(directions)
                a = np.zeros([3, 3])
                for p in directions:
                    for i in range(3):
                        for j in range(3):
                            a[i, j] += 1.0 / N * (p[i] * p[j])
                A.append(a.ravel())

        csv_file = os.path.join(self.output_dir, "A.csv")
        data = np.hstack((np.matrix(t).T, np.vstack(A)))
        np.savetxt(csv_file, data, delimiter=",")

        # plot results
        data = np.loadtxt(csv_file, delimiter=',')
        t = data[:, 0]
        plt.figure(figsize=(12, 3))
        for j, i in enumerate([0, 4, 8, 1]):
            plt.subplot("14"+str(j+1))
            p = plt.plot(self.options.G*t, data[:, i+1])
            plt.xlabel("Strains")
            plt.ylabel("Component %d" % j)
            if i % 2 == 0:
                plt.ylim([0, 1])
            else:
                plt.ylim([-1, 1])
        plt.tight_layout()
        plt.savefig(csv_file.replace('.csv', '.pdf'))
        try:
            from matplotlib2tikz import save as tikz_save
            tikz_save(csv_file.replace('.csv', '.tex'))
        except ImportError:
            print("Did not write tikz figure.")