diff --git a/code/BaseMesh.py b/code/BaseMesh.py index 2b72ee80..a197b8a4 100644 --- a/code/BaseMesh.py +++ b/code/BaseMesh.py @@ -128,7 +128,7 @@ class BaseMesh(object): def nN(): doc = "Total number of nodes" - fget = lambda self: self.n + 1 + fget = lambda self: np.prod(self.n + 1) return locals() nN = property(**nN()) diff --git a/code/TensorMesh.py b/code/TensorMesh.py index 9e116f70..5f5e44b3 100644 --- a/code/TensorMesh.py +++ b/code/TensorMesh.py @@ -23,7 +23,7 @@ class TensorMesh(BaseMesh, TensorView): def __init__(self, h, x0=None): super(TensorMesh, self).__init__(np.array([len(x) for x in h]), x0) - assert len(h) == len(x0), "Dimension mismatch. x0 != len(h)" + assert len(h) == len(self.x0), "Dimension mismatch. x0 != len(h)" for i, h_i in enumerate(h): assert type(h_i) == np.ndarray, ("h[%i] is not a numpy array." % i) diff --git a/code/TensorView.py b/code/TensorView.py index 41e4b6b1..ab5c09cf 100644 --- a/code/TensorView.py +++ b/code/TensorView.py @@ -12,28 +12,36 @@ class TensorView(object): def __init__(self): pass - def plotImage(self, I): + def plotImage(self, I, imageType='CC', figNum=1): - fig = plt.figure(1) + assert type(I) == np.ndarray, "I must be a numpy array" + assert imageType in ["CC", "N"], "imageType must be 'CC' or 'N'" + + if imageType == 'CC': + assert I.size == self.nC, "Incorrect dimensions for CC." + elif imageType == 'N': + assert I.size == self.nN, "Incorrect dimensions for N." + + fig = plt.figure(figNum) fig.clf() ax = plt.subplot(111) if self.dim == 1: - if np.size(I) == self.n[0]: - print 'cell-centered image' - xx = self.gridCC - ax.plot(xx, I, 'ro') - elif np.size(I) == self.n[0]+1: - print 'nodal image' - xx = self.gridN - ax.plot(xx, I, 'bs') + if imageType == 'CC': + ax.plot(self.vectorCCx, I, 'ro') + elif imageType == 'N': + ax.plot(self.vectorNx, I, 'bs') elif self.dim == 2: - print "assume cell-centered image" - x = self.vectorNx - y = self.vectorNy - fh = ax.pcolormesh(x,y,I.reshape(self.n,order='F').T) - lx = plt.xlabel("x") - ly = plt.xlabel("y") + if imageType == 'CC': + C = I[:].reshape(self.n, order='F') + elif imageType == 'N': + C = I[:].reshape(self.n+1, order='F') + C = 0.25*(C[:-1, :-1] + C[1:, :-1] + C[:-1, 1:] + C[1:, 1:]) + + fh = ax.pcolormesh(self.vectorNx, self.vectorNy, C.T) + plt.xlabel("x") + plt.ylabel("y") fig.colorbar(fh) + fig.show()