cs205-lecture-examples

Example codes used during Harvard CS205 lectures
git clone https://git.0xfab.ch/cs205-lecture-examples.git
Log | Files | Refs | README | LICENSE

plot.py (1768B)


      1 # File       : plot.py
      2 # Description: Plot temporal evolution of diffusing quantity
      3 # Copyright 2022 Harvard University. All Rights Reserved.
      4 import numpy as np
      5 import matplotlib.pyplot as plt
      6 
      7 def plot(ax, fname, title, dx, N, *, start=0.0, binary=False):
      8     if binary:
      9         data = np.fromfile(fname, dtype=float)
     10         nsteps = data.size // N
     11         data = data.reshape((nsteps, N))
     12     else:
     13         data = np.loadtxt(fname)
     14     N = data.shape[1]
     15     for step in range(data.shape[0]):
     16         x = start + (np.arange(N) + 0.5) * dx
     17         ax.plot(x, data[step, :])
     18     ax.text(0.03, 0.9, title, fontsize=6)
     19 
     20 
     21 def main():
     22     N = 4 * 128
     23     dx = 1.0 / N
     24 
     25     fig, ax = plt.subplots(4, 2, sharex=True)
     26     plot(ax[0][0], 'u.dat', 'POSIX', dx, N)
     27     plot(ax[1][0], 'u_mpi.bin', 'MPI (binary)', dx, N, binary=True)
     28     plot(ax[2][0], 'u_mpi.dat', 'MPI (ascii)', dx, N)
     29     plot(ax[3][0], 'u_mpi_ordered.dat', 'MPI (ascii, ordered)', dx, N)
     30     plot(ax[0][1], 'u_rank0.dat', 'MPI (rank 0)', dx, N, start=0 * 128 * dx)
     31     plot(ax[1][1], 'u_rank1.dat', 'MPI (rank 1)', dx, N, start=1 * 128 * dx)
     32     plot(ax[2][1], 'u_rank2.dat', 'MPI (rank 2)', dx, N, start=2 * 128 * dx)
     33     plot(ax[3][1], 'u_rank3.dat', 'MPI (rank 3)', dx, N, start=3 * 128 * dx)
     34     for a in ax.flatten():
     35         a.set_xlim([0, 1])
     36         a.set_ylim([-0.05, 1.05])
     37     ax[3][0].set_xlabel(r'$x$')
     38     ax[3][1].set_xlabel(r'$x$')
     39     for i in range(4):
     40         ax[i][0].set_ylabel(r'$u(x,t)$')
     41     plt.savefig('IO.png', dpi=600, bbox_inches='tight')
     42     plt.close()
     43 
     44     fig, ax = plt.subplots()
     45     plot(ax, 'u.dat', '', dx, N)
     46     plt.xlabel(r'$x$')
     47     plt.ylabel(r'$u(x,t)$')
     48     plt.savefig('u.png', dpi=600, bbox_inches='tight')
     49     plt.close()
     50 
     51 if __name__ == "__main__":
     52     main()