/[escript]/trunk/weipa/py_src/__init__.py
ViewVC logotype

Contents of /trunk/weipa/py_src/__init__.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4398 - (show annotations)
Thu May 9 08:42:44 2013 UTC (7 years, 4 months ago) by jfenwick
File MIME type: text/x-python
File size: 9679 byte(s)
support for supressing doco subpackages. fiddling with one of the imports to try to reduce the number of places where Data appears.  Seriously it looks like we have 8 different Data classes
1
2 ##############################################################################
3 #
4 # Copyright (c) 2003-2013 by University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Open Software License version 3.0
9 # http://www.opensource.org/licenses/osl-3.0.php
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development since 2012 by School of Earth Sciences
13 #
14 ##############################################################################
15
16 __copyright__="""Copyright (c) 2003-2013 by University of Queensland
17 http://www.uq.edu.au
18 Primary Business: Queensland, Australia"""
19 __license__="""Licensed under the Open Software License version 3.0
20 http://www.opensource.org/licenses/osl-3.0.php"""
21 __url__="https://launchpad.net/escript-finley"
22
23 from .weipacpp import visitInitialize, visitPublishData
24
25 __nodocorecursion=['weipacpp']
26
27 def interpolateEscriptData(domain, data):
28 """
29 esys.weipa does not support the function spaces Solution and
30 ReducedSolution. This function interpolates Data defined on those function
31 spaces to compatible alternatives.
32 """
33 from esys.escript import Solution, ReducedSolution
34 from esys.escript import ContinuousFunction, ReducedContinuousFunction
35 from esys.escript.util import interpolate
36 new_data={}
37 for n,d in list(data.items()):
38 if not d.isEmpty():
39 fs=d.getFunctionSpace()
40 if domain is None:
41 domain=fs.getDomain()
42 elif domain != fs.getDomain():
43 raise ValueError("weipa: All Data must be on the same domain!")
44 if fs == Solution(domain):
45 new_data[n]=interpolate(d, ContinuousFunction(domain))
46 elif fs == ReducedSolution(domain):
47 new_data[n]=interpolate(d, ReducedContinuousFunction(domain))
48 else:
49 new_data[n]=d
50 return domain,new_data
51
52 def createDataset(domain=None, **data):
53 """
54 Creates and returns an esys.weipa dataset consisting of a Domain and Data
55 objects. The returned object provides methods to access and export data.
56 """
57 from .weipacpp import EscriptDataset
58 dataset=EscriptDataset()
59 domain,new_data=interpolateEscriptData(domain, data)
60 dataset.setDomain(domain)
61 for n,d in sorted(new_data.items()):
62 #TODO: data units are not supported here yet
63 dataset.addData(d, n, "")
64 return dataset
65
66 def saveSilo(filename, domain=None, write_meshdata=False, **data):
67 """
68 Writes `Data` objects and their mesh to a file using the SILO file format.
69
70 Example::
71
72 temp=Scalar(..)
73 v=Vector(..)
74 saveSilo("solution.silo", temperature=temp, velocity=v)
75
76 ``temp`` and ``v`` are written to "solution.silo" where ``temp`` is named
77 "temperature" and ``v`` is named "velocity".
78
79 :param filename: name of the output file ('.silo' is added if required)
80 :type filename: ``str``
81 :param domain: domain of the `Data` objects. If not specified, the domain
82 of the given `Data` objects is used.
83 :type domain: `escript.Domain`
84 :param write_meshdata: whether to save mesh-related data such as element
85 identifiers, ownership etc. This is mainly useful
86 for debugging.
87 :type write_meshdata: ``bool``
88 :keyword <name>: writes the assigned value to the Silo file using <name> as
89 identifier
90 :note: All data objects have to be defined on the same domain but they may
91 be defined on separate `FunctionSpace` s.
92 """
93
94 dataset = createDataset(domain, **data)
95 dataset.setSaveMeshData(write_meshdata)
96 return dataset.saveSilo(filename)
97
98 def saveVTK(filename, domain=None, metadata='', metadata_schema=None, write_meshdata=False, **data):
99 """
100 Writes `Data` objects and their mesh to a file using the VTK XML file
101 format.
102
103 Example::
104
105 temp=Scalar(..)
106 v=Vector(..)
107 saveVTK("solution.vtu", temperature=temp, velocity=v)
108
109 ``temp`` and ``v`` are written to "solution.vtu" where ``temp`` is named
110 "temperature" and ``v`` is named "velocity".
111
112 Meta tags, e.g. a timeStamp, can be added to the file, for instance::
113
114 tmp=Scalar(..)
115 v=Vector(..)
116 saveVTK("solution.vtu", temperature=tmp, velocity=v,
117 metadata="<timeStamp>1.234</timeStamp>",
118 metadata_schema={"gml":"http://www.opengis.net/gml"})
119
120 The argument ``metadata_schema`` allows the definition of name spaces with
121 a schema used in the definition of meta tags.
122
123 :param filename: name of the output file ('.vtu' is added if required)
124 :type filename: ``str``
125 :param domain: domain of the `Data` objects. If not specified, the domain
126 of the given `Data` objects is used.
127 :type domain: `escript.Domain`
128 :keyword <name>: writes the assigned value to the VTK file using <name> as
129 identifier
130 :param metadata: additional XML meta data which are inserted into the VTK
131 file. The meta data are marked by the tag ``<MetaData>``.
132 :type metadata: ``str``
133 :param metadata_schema: assigns schemas to namespaces which have been used
134 to define meta data.
135 :type metadata_schema: ``dict`` with ``metadata_schema[<namespace>]=<URI>``
136 to assign the scheme ``<URI>`` to the name space
137 ``<namespace>``.
138 :param write_meshdata: whether to save mesh-related data such as element
139 identifiers, ownership etc. This is mainly useful
140 for debugging.
141 :type write_meshdata: ``bool``
142 :note: All data objects have to be defined on the same domain. They may not
143 be in the same `FunctionSpace` but not all combinations of
144 `FunctionSpace` s can be written to a single VTK file.
145 Typically, data on the boundary and on the interior cannot be mixed.
146 """
147
148 dataset = createDataset(domain, **data)
149 ss=''
150 ms=''
151 if not metadata is None:
152 ms=metadata
153 if not metadata_schema is None:
154 if hasattr(metadata_schema, 'items'):
155 for i,p in list(metadata_schema.items()):
156 ss="%s xmlns:%s=\"%s\""%(ss, i, p)
157 else:
158 ss=metadata_schema
159 dataset.setMetadataSchemaString(ss.strip(), ms.strip())
160 dataset.setSaveMeshData(write_meshdata)
161 return dataset.saveVTK(filename)
162
163 def saveVoxet(filename, **data):
164 """
165 Writes `Data` objects to a file using the GOCAD Voxet file format as
166 separate properties on the same grid.
167 At the moment only Data on a `ripley` domain can be saved in this format.
168 Note that this function will produce one header file (ending in .vo) and
169 a separate property file for each `Data` object.
170
171 :param filename: name of the output file ('.vo' is added if required)
172 :type filename: ``str``
173 :note: All data objects have to be defined on the same ripley domain and
174 either defined on reduced Function or on a `FunctionSpace` that
175 allows interpolation to reduced Function.
176 """
177
178 from esys.escript import ReducedFunction
179 from esys.escript.util import interpolate
180 from esys.ripley.ripleycpp import DATATYPE_FLOAT32, BYTEORDER_BIG_ENDIAN
181
182 new_data={}
183 domain=None
184 for n,d in list(data.items()):
185 if d.isEmpty():
186 continue
187 fs=d.getFunctionSpace()
188 if domain is None:
189 domain=fs.getDomain()
190 elif domain != fs.getDomain():
191 raise ValueError("saveVoxet: All Data must be on the same domain!")
192
193 try:
194 nd=interpolate(d, ReducedFunction(domain))
195 except:
196 raise ValueError("saveVoxet: Unable to interpolate all Data to reduced Function!")
197 new_data[n]=nd
198
199 if filename[-3:]=='.vo':
200 fileprefix=filename[:-3]+"_"
201 else:
202 fileprefix=filename+"_"
203 filename=filename+'.vo'
204
205 origin, spacing, NE = domain.getGridParameters()
206 # flip vertical origin
207 origin=origin[:-1]+(-origin[-1],)
208 axis_max=NE[:-1]+(-NE[-1],)
209 midpoint=tuple([n/2 for n in NE])
210
211 if domain.getDim() == 2:
212 origin=origin+(0.,)
213 spacing=spacing+(1.,)
214 NE=NE+(1,)
215 midpoint=midpoint+(0,)
216 axis_max=axis_max+(0,)
217
218 mainvar=new_data.keys()[0]
219 f=open(filename,'w')
220 f.write("GOCAD Voxet 1\nHEADER {\nname: escriptdata\n")
221 f.write("sections: 3 1 1 %d 2 1 %d 3 1 %d\n"%midpoint)
222 f.write("painted: on\nascii: off\n*painted*variable: %s\n}"%mainvar)
223 f.write("""
224 GOCAD_ORIGINAL_COORDINATE_SYSTEM
225 NAME "gocad Local"
226 AXIS_NAME X Y Z
227 AXIS_UNIT m m m
228 ZPOSITIVE Depth
229 END_ORIGINAL_COORDINATE_SYSTEM\n""")
230
231 f.write("AXIS_O %0.2f %0.2f %0.2f\n"%origin)
232 f.write("AXIS_U %0.2f 0 0\n"%spacing[0])
233 f.write("AXIS_V 0 %0.2f 0\n"%spacing[1])
234 f.write("AXIS_W 0 0 %0.2f\n"%spacing[2])
235 f.write("AXIS_MIN 0 0 0\n")
236 f.write("AXIS_MAX %d %d %d\n"%axis_max)
237 f.write("AXIS_N %d %d %d\n"%NE)
238 f.write("\n")
239
240 num=0
241 for n,d in list(new_data.items()):
242 num=num+1
243 propfile=fileprefix+n
244 domain.writeBinaryGrid(d, propfile, BYTEORDER_BIG_ENDIAN, DATATYPE_FLOAT32)
245 f.write("\nPROPERTY %d %s\n"%(num, n))
246 f.write("PROPERTY_SUBCLASS %d QUANTITY Float\n"%num)
247 f.write("PROP_ESIZE %d 4\n"%num)
248 f.write("PROP_ETYPE %d IEEE\n"%num)
249 f.write("PROP_FORMAT %d RAW\n"%num)
250 f.write("PROP_OFFSET %d 0\n"%num)
251 f.write("PROP_FILE %d %s\n"%(num,propfile))
252 f.close()
253

  ViewVC Help
Powered by ViewVC 1.1.26