/[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 6651 - (show annotations)
Wed Feb 7 02:12:08 2018 UTC (2 years, 3 months ago) by jfenwick
File MIME type: text/x-python
File size: 2839 byte(s)
Make everyone sad by touching all the files

Copyright dates update

1 ##############################################################################
2 #
3 # Copyright (c) 2009-2018 by The University of Queensland
4 # http://www.uq.edu.au
5 #
6 # Primary Business: Queensland, Australia
7 # Licensed under the Apache License, version 2.0
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 # Development 2012-2013 by School of Earth Sciences
12 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 #
14 ##############################################################################
15
16 from __future__ import division, print_function
17 __copyright__="""Copyright (c) 2009-2018 by The University of Queensland
18 http://www.uq.edu.au
19 Primary Business: Queensland, Australia"""
20 __license__="""Licensed under the Apache License, version 2.0
21 http://www.apache.org/licenses/LICENSE-2.0"""
22 __url__="https://launchpad.net/escript-finley"
23
24 """
25 Additional routines using matplotlib for cookbook examples.
26 Author: Antony Hallam antony.hallam@uqconnect.edu.au
27 """
28 from esys.escript import inf,sup
29 from esys.escript.pdetools import Locator
30 import numpy as np
31 import pylab as pl
32
33 try:
34 from mpl_toolkits.natgrid import _natgrid
35 HAVE_NATGRID=True
36 except ImportError:
37 HAVE_NATGRID=False
38
39 def toXYTuple(coords):
40 """
41 extracts the X and Y coordinates as two ```numpy`` arrays from the escript coordinates ```coords``` as produced by a ``.getX`` call.
42 """
43 coords = np.array(coords.toListOfTuples()) #convert to array
44 coordX = coords[:,0]; coordY = coords[:,1] #X and Y components.
45 return coordX,coordY
46
47 def toRegGrid(u, nx=50, ny=50):
48 """
49 returns a nx x ny grid representation of the escript object u
50 """
51 xx=u.getDomain().getX()
52 x=u.getFunctionSpace().getX()
53 coordX, coordY = toXYTuple(x)
54 utemp = u.toListOfTuples()
55 # create regular grid
56 xi = np.linspace(inf(xx[0]),sup(xx[0]),nx)
57 yi = np.linspace(inf(xx[1]),sup(xx[1]),ny)
58
59 # interpolate u to grid
60 zi = pl.matplotlib.mlab.griddata(coordX,coordY,utemp,xi,yi, interp='linear')
61 return xi, yi, zi
62
63 def subsample(u, nx=50, ny=50):
64 """
65 subsamples ```u``` over an ```nx``` x ```ny``` grid
66 and returns ``numpy`` arrays for the values and locations
67 used for subsampling.
68 """
69 xx=u.getDomain().getX() # points of the domain
70 x0=inf(xx[0])
71 y0=inf(xx[1])
72 dx = (sup(xx[0])-x0)/nx # x spacing
73 dy = (sup(xx[1])-y0)/ny # y spacing
74 grid = [ ]
75 for j in range(0,ny-1):
76 for i in range(0,nx-1):
77 grid.append([x0+dx/2+dx*i,y0+dy/2+dy*j])
78 uLoc = Locator(u.getFunctionSpace(),grid)
79 subu= uLoc(u) # get data of u at sample points closests to grid points
80 usublocs = uLoc.getX() #returns actual locations from data
81 return np.array(usublocs), np.array(subu)
82

  ViewVC Help
Powered by ViewVC 1.1.26