/[escript]/trunk/doc/examples/cookbook/cblib.py
ViewVC logotype

Contents of /trunk/doc/examples/cookbook/cblib.py

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26