/[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 3775 - (show annotations)
Wed Jan 18 07:26:34 2012 UTC (7 years, 8 months ago) by jfenwick
File MIME type: text/x-python
File size: 6709 byte(s)
Weipa updated

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 from weipacpp import visitInitialize, visitPublishData
23
24 def interpolateEscriptData(domain, data):
25 """
26 esys.weipa does not support the function spaces Solution and
27 ReducedSolution. This function interpolates Data defined on those function
28 spaces to compatible alternatives.
29 """
30 from esys.escript import Solution, ReducedSolution
31 from esys.escript import ContinuousFunction, ReducedContinuousFunction
32 from esys.escript.util import interpolate
33 new_data={}
34 for n,d in list(data.items()):
35 if not d.isEmpty():
36 fs=d.getFunctionSpace()
37 if domain is None:
38 domain=fs.getDomain()
39 elif domain != fs.getDomain():
40 raise ValueError("weipa: All Data must be on the same domain!")
41 if fs == Solution(domain):
42 new_data[n]=interpolate(d, ContinuousFunction(domain))
43 elif fs == ReducedSolution(domain):
44 new_data[n]=interpolate(d, ReducedContinuousFunction(domain))
45 else:
46 new_data[n]=d
47 return domain,new_data
48
49 def createDataset(domain=None, **data):
50 """
51 Creates and returns an esys.weipa dataset consisting of a Domain and Data
52 objects. The returned object provides methods to access and export data.
53 """
54 from weipacpp import EscriptDataset
55 dataset=EscriptDataset()
56 domain,new_data=interpolateEscriptData(domain, data)
57 dataset.setDomain(domain)
58 for n,d in sorted(new_data.items()):
59 #TODO: data units are not supported here yet
60 dataset.addData(d, n, "")
61 return dataset
62
63 def saveSilo(filename, domain=None, write_meshdata=False, **data):
64 """
65 Writes `Data` objects and their mesh to a file using the SILO file format.
66
67 Example::
68
69 temp=Scalar(..)
70 v=Vector(..)
71 saveSilo("solution.silo", temperature=temp, velocity=v)
72
73 ``temp`` and ``v`` are written to "solution.silo" where ``temp`` is named
74 "temperature" and ``v`` is named "velocity".
75
76 :param filename: name of the output file ('.silo' is added if required)
77 :type filename: ``str``
78 :param domain: domain of the `Data` objects. If not specified, the domain
79 of the given `Data` objects is used.
80 :type domain: `escript.Domain`
81 :param write_meshdata: whether to save mesh-related data such as element
82 identifiers, ownership etc. This is mainly useful
83 for debugging.
84 :type write_meshdata: ``bool``
85 :keyword <name>: writes the assigned value to the Silo file using <name> as
86 identifier
87 :note: All data objects have to be defined on the same domain but they may
88 be defined on separate `FunctionSpace` s.
89 """
90
91 dataset = createDataset(domain, **data)
92 dataset.setSaveMeshData(write_meshdata)
93 return dataset.saveSilo(filename)
94
95 def saveVTK(filename, domain=None, metadata='', metadata_schema=None, write_meshdata=False, **data):
96 """
97 Writes `Data` objects and their mesh to a file using the VTK XML file
98 format.
99
100 Example::
101
102 temp=Scalar(..)
103 v=Vector(..)
104 saveVTK("solution.vtu", temperature=temp, velocity=v)
105
106 ``temp`` and ``v`` are written to "solution.vtu" where ``temp`` is named
107 "temperature" and ``v`` is named "velocity".
108
109 Meta tags, e.g. a timeStamp, can be added to the file, for instance::
110
111 tmp=Scalar(..)
112 v=Vector(..)
113 saveVTK("solution.vtu", temperature=tmp, velocity=v,
114 metadata="<timeStamp>1.234</timeStamp>",
115 metadata_schema={"gml":"http://www.opengis.net/gml"})
116
117 The argument ``metadata_schema`` allows the definition of name spaces with
118 a schema used in the definition of meta tags.
119
120 :param filename: name of the output file ('.vtu' is added if required)
121 :type filename: ``str``
122 :param domain: domain of the `Data` objects. If not specified, the domain
123 of the given `Data` objects is used.
124 :type domain: `escript.Domain`
125 :keyword <name>: writes the assigned value to the VTK file using <name> as
126 identifier
127 :param metadata: additional XML meta data which are inserted into the VTK
128 file. The meta data are marked by the tag ``<MetaData>``.
129 :type metadata: ``str``
130 :param metadata_schema: assigns schemas to namespaces which have been used
131 to define meta data.
132 :type metadata_schema: ``dict`` with ``metadata_schema[<namespace>]=<URI>``
133 to assign the scheme ``<URI>`` to the name space
134 ``<namespace>``.
135 :param write_meshdata: whether to save mesh-related data such as element
136 identifiers, ownership etc. This is mainly useful
137 for debugging.
138 :type write_meshdata: ``bool``
139 :note: All data objects have to be defined on the same domain. They may not
140 be in the same `FunctionSpace` but not all combinations of
141 `FunctionSpace` s can be written to a single VTK file.
142 Typically, data on the boundary and on the interior cannot be mixed.
143 """
144
145 dataset = createDataset(domain, **data)
146 ss=''
147 ms=''
148 if not metadata is None:
149 ms=metadata
150 if not metadata_schema is None:
151 if hasattr(metadata_schema, 'items'):
152 for i,p in list(metadata_schema.items()):
153 ss="%s xmlns:%s=\"%s\""%(ss, i, p)
154 else:
155 ss=metadata_schema
156 dataset.setMetadataSchemaString(ss.strip(), ms.strip())
157 dataset.setSaveMeshData(write_meshdata)
158 return dataset.saveVTK(filename)
159
160 def _saveVTK(filename, domain=None, metadata='', metadata_schema=None, data={}):
161 """
162 This is only here to support the deprecated domain C++ member saveVTK().
163 """
164 return saveVTK(filename, domain, metadata, metadata_schema, **data)
165

  ViewVC Help
Powered by ViewVC 1.1.26