# Annotation of /trunk/doc/examples/cookbook/cblib.py

Revision 3892 - (hide annotations)
Tue Apr 10 08:57:23 2012 UTC (6 years, 10 months ago) by jfenwick
File MIME type: text/x-python
File size: 2538 byte(s)
```Merged changes across from the attempt2 branch.
This version builds and passes python2 tests.
It also passes most python3 tests.

```
 1 gross 2931 ######################################################## 2 # 3 # Copyright (c) 2009-2010 by University of Queensland 4 # Earth Systems Science Computational Center (ESSCC) 5 6 # 7 # Primary Business: Queensland, Australia 8 # Licensed under the Open Software License version 3.0 9 10 # 11 ######################################################## 12 13 __copyright__="""Copyright (c) 2009-2010 by University of Queensland 14 Earth Systems Science Computational Center (ESSCC) 15 http://www.uq.edu.au/esscc 16 Primary Business: Queensland, Australia""" 17 __license__="""Licensed under the Open Software License version 3.0 18 19 __url__= 20 21 """ 22 Additional routines using matplotlib for cookbook examples. 23 Author: Antony Hallam antony.hallam@uqconnect.edu.au 24 """ 25 gross 2948 from esys.escript import inf,sup 26 from esys.escript.pdetools import Locator 27 gross 2931 import numpy as np 28 gross 2948 import pylab as pl 29 gross 2931 30 def toXYTuple(coords): 31 gross 2948 """ 32 extracts the X and Y coordinates as two ```numpy`` arrays from the escript coordinates ```coords``` as produced by a ``.getX`` call. 33 """ 34 gross 2931 coords = np.array(coords.toListOfTuples()) #convert to array 35 coordX = coords[:,0]; coordY = coords[:,1] #X and Y components. 36 return coordX,coordY 37 gross 2948 38 def toRegGrid(u, nx=50, ny=50): 39 """ 40 returns a nx x ny grid representation of the escript object u 41 """ 42 xx=u.getDomain().getX() 43 x=u.getFunctionSpace().getX() 44 coordX, coordY = toXYTuple(x) 45 utemp = u.toListOfTuples() 46 # create regular grid 47 xi = np.linspace(inf(xx[0]),sup(xx[0]),nx) 48 yi = np.linspace(inf(xx[1]),sup(xx[1]),ny) 49 # interpolate u to grid 50 zi = pl.matplotlib.mlab.griddata(coordX,coordY,utemp,xi,yi) 51 return xi, yi, zi 52 53 def subsample(u, nx=50, ny=50): 54 """ 55 subsamples ```u``` over an ```nx``` x ```ny``` grid 56 and returns ``numpy`` arrays for the values and locations 57 used for subsampling. 58 """ 59 xx=u.getDomain().getX() # points of the domain 60 x0=inf(xx[0]) 61 y0=inf(xx[1]) 62 dx = (sup(xx[0])-x0)/nx # x spacing 63 dy = (sup(xx[1])-y0)/ny # y spacing 64 grid = [ ] 65 jfenwick 3892 for j in range(0,ny-1): 66 for i in range(0,nx-1): 67 gross 2948 grid.append([x0+dx/2+dx*i,y0+dy/2+dy*j]) 68 uLoc = Locator(u.getFunctionSpace(),grid) 69 subu= uLoc(u) # get data of u at sample points closests to grid points 70 usublocs = uLoc.getX() #returns actual locations from data 71 return np.array(usublocs), np.array(subu) 72