/[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 3344 - (show annotations)
Thu Nov 11 23:26:52 2010 UTC (8 years, 10 months ago) by caltinay
File MIME type: text/x-python
File size: 5778 byte(s)
Phew!
-escript, finley, and dudley now uses weipa's saveVTK implementation
-moved tests from finley to weipa accordingly; dudley still to do
-rebaselined all test files
-fixed a few issues in weipa.saveVTK, e.g. saving metadata without schema
-added a deprecation warning to esys.escript.util.saveVTK
-todo: change doco, tests and other places to use weipa.saveVTK


1
2 ########################################################
3 #
4 # Copyright (c) 2003-2010 by University of Queensland
5 # Earth Systems Science Computational Center (ESSCC)
6 # http://www.uq.edu.au/esscc
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 ########################################################
13
14 __copyright__="""Copyright (c) 2003-2010 by University of Queensland
15 Earth Systems Science Computational Center (ESSCC)
16 http://www.uq.edu.au/esscc
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 def interpolateEscriptData(domain, data):
23 """
24 esys.weipa does not support the function spaces Solution and
25 ReducedSolution. This function interpolates Data defined on those function
26 spaces to compatible alternatives.
27 """
28 from esys.escript import Solution, ReducedSolution
29 from esys.escript import ContinuousFunction, ReducedContinuousFunction
30 from esys.escript.util import interpolate
31 new_data={}
32 for n,d in data.items():
33 if not d.isEmpty():
34 fs=d.getFunctionSpace()
35 if domain is None:
36 domain=fs.getDomain()
37 elif domain != fs.getDomain():
38 raise ValueError("weipa: All Data must be on the same domain!")
39 if fs == Solution(domain):
40 new_data[n]=interpolate(d, ContinuousFunction(domain))
41 elif fs == ReducedSolution(domain):
42 new_data[n]=interpolate(d, ReducedContinuousFunction(domain))
43 else:
44 new_data[n]=d
45 return domain,new_data
46
47 def createDataset(domain=None, **data):
48 """
49 Creates and returns an esys.weipa dataset consisting of a Domain and Data
50 objects. The returned object provides methods to access and export data.
51 """
52 from weipacpp import EscriptDataset
53 dataset=EscriptDataset()
54 domain,new_data=interpolateEscriptData(domain, data)
55 dataset.setDomain(domain)
56 for n,d in new_data.items():
57 #TODO: data units are not supported here yet
58 dataset.addData(d, n, "")
59 return dataset
60
61 def saveSilo(filename, domain=None, **data):
62 """
63 Writes `Data` objects and their mesh to a file using the SILO file format.
64
65 Example::
66
67 temp=Scalar(..)
68 v=Vector(..)
69 saveSilo("solution.silo", temperature=temp, velocity=v)
70
71 ``temp`` and ``v`` are written to "solution.silo" where ``temp`` is named
72 "temperature" and ``v`` is named "velocity".
73
74 :param filename: name of the output file ('.silo' is added if required)
75 :type filename: ``str``
76 :param domain: domain of the `Data` objects. If not specified, the domain
77 of the given `Data` objects is used.
78 :type domain: `escript.Domain`
79 :keyword <name>: writes the assigned value to the Silo file using <name> as
80 identifier
81 :note: All data objects have to be defined on the same domain but they may
82 be defined on separate `FunctionSpace` s.
83 """
84
85 dataset = createDataset(domain, **data)
86 dataset.saveSilo(filename)
87
88 def saveVTK(filename, domain=None, metadata='', metadata_schema=None, **data):
89 """
90 Writes `Data` objects and their mesh to a file using the VTK XML file
91 format.
92
93 Example::
94
95 temp=Scalar(..)
96 v=Vector(..)
97 saveVTK("solution.vtu", temperature=temp, velocity=v)
98
99 ``temp`` and ``v`` are written to "solution.vtu" where ``temp`` is named
100 "temperature" and ``v`` is named "velocity".
101
102 Meta tags, e.g. a timeStamp, can be added to the file, for instance::
103
104 tmp=Scalar(..)
105 v=Vector(..)
106 saveVTK("solution.vtu", temperature=tmp, velocity=v,
107 metadata="<timeStamp>1.234</timeStamp>",
108 metadata_schema={"gml":"http://www.opengis.net/gml"})
109
110 The argument ``metadata_schema`` allows the definition of name spaces with
111 a schema used in the definition of meta tags.
112
113 :param filename: name of the output file ('.vtu' is added if required)
114 :type filename: ``str``
115 :param domain: domain of the `Data` objects. If not specified, the domain
116 of the given `Data` objects is used.
117 :type domain: `escript.Domain`
118 :keyword <name>: writes the assigned value to the VTK file using <name> as
119 identifier
120 :param metadata: additional XML meta data which are inserted into the VTK
121 file. The meta data are marked by the tag ``<MetaData>``.
122 :type metadata: ``str``
123 :param metadata_schema: assigns schemas to namespaces which have been used
124 to define meta data.
125 :type metadata_schema: ``dict`` with ``metadata_schema[<namespace>]=<URI>``
126 to assign the scheme ``<URI>`` to the name space
127 ``<namespace>``.
128 :note: All data objects have to be defined on the same domain. They may not
129 be in the same `FunctionSpace` but not all combinations of
130 `FunctionSpace` s can be written to a single VTK file.
131 Typically, data on the boundary and on the interior cannot be mixed.
132 """
133
134 dataset = createDataset(domain, **data)
135 ss=''
136 ms=''
137 if not metadata is None:
138 ms=metadata
139 if not metadata_schema is None:
140 if hasattr(metadata_schema, 'items'):
141 for i,p in metadata_schema.items():
142 ss="%s xmlns:%s=\"%s\""%(ss, i, p)
143 else:
144 ss=metadata_schema
145 dataset.setMetadataSchemaString(ss.strip(), ms.strip())
146 dataset.saveVTK(filename)
147

  ViewVC Help
Powered by ViewVC 1.1.26