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 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.iteritems()): |
59 |
#TODO: data units are not supported here yet |
60 |
dataset.addData(d, n, "") |
61 |
return dataset |
62 |
|
63 |
def saveSilo(filename, domain=None, **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 |
:keyword <name>: writes the assigned value to the Silo file using <name> as |
82 |
identifier |
83 |
:note: All data objects have to be defined on the same domain but they may |
84 |
be defined on separate `FunctionSpace` s. |
85 |
""" |
86 |
|
87 |
dataset = createDataset(domain, **data) |
88 |
dataset.saveSilo(filename) |
89 |
|
90 |
def saveVTK(filename, domain=None, metadata='', metadata_schema=None, **data): |
91 |
""" |
92 |
Writes `Data` objects and their mesh to a file using the VTK XML file |
93 |
format. |
94 |
|
95 |
Example:: |
96 |
|
97 |
temp=Scalar(..) |
98 |
v=Vector(..) |
99 |
saveVTK("solution.vtu", temperature=temp, velocity=v) |
100 |
|
101 |
``temp`` and ``v`` are written to "solution.vtu" where ``temp`` is named |
102 |
"temperature" and ``v`` is named "velocity". |
103 |
|
104 |
Meta tags, e.g. a timeStamp, can be added to the file, for instance:: |
105 |
|
106 |
tmp=Scalar(..) |
107 |
v=Vector(..) |
108 |
saveVTK("solution.vtu", temperature=tmp, velocity=v, |
109 |
metadata="<timeStamp>1.234</timeStamp>", |
110 |
metadata_schema={"gml":"http://www.opengis.net/gml"}) |
111 |
|
112 |
The argument ``metadata_schema`` allows the definition of name spaces with |
113 |
a schema used in the definition of meta tags. |
114 |
|
115 |
:param filename: name of the output file ('.vtu' is added if required) |
116 |
:type filename: ``str`` |
117 |
:param domain: domain of the `Data` objects. If not specified, the domain |
118 |
of the given `Data` objects is used. |
119 |
:type domain: `escript.Domain` |
120 |
:keyword <name>: writes the assigned value to the VTK file using <name> as |
121 |
identifier |
122 |
:param metadata: additional XML meta data which are inserted into the VTK |
123 |
file. The meta data are marked by the tag ``<MetaData>``. |
124 |
:type metadata: ``str`` |
125 |
:param metadata_schema: assigns schemas to namespaces which have been used |
126 |
to define meta data. |
127 |
:type metadata_schema: ``dict`` with ``metadata_schema[<namespace>]=<URI>`` |
128 |
to assign the scheme ``<URI>`` to the name space |
129 |
``<namespace>``. |
130 |
:note: All data objects have to be defined on the same domain. They may not |
131 |
be in the same `FunctionSpace` but not all combinations of |
132 |
`FunctionSpace` s can be written to a single VTK file. |
133 |
Typically, data on the boundary and on the interior cannot be mixed. |
134 |
""" |
135 |
|
136 |
dataset = createDataset(domain, **data) |
137 |
ss='' |
138 |
ms='' |
139 |
if not metadata is None: |
140 |
ms=metadata |
141 |
if not metadata_schema is None: |
142 |
if hasattr(metadata_schema, 'items'): |
143 |
for i,p in metadata_schema.items(): |
144 |
ss="%s xmlns:%s=\"%s\""%(ss, i, p) |
145 |
else: |
146 |
ss=metadata_schema |
147 |
dataset.setMetadataSchemaString(ss.strip(), ms.strip()) |
148 |
dataset.saveVTK(filename) |
149 |
|
150 |
def _saveVTK(filename, domain=None, metadata='', metadata_schema=None, data={}): |
151 |
""" |
152 |
This is only here to support the deprecated domain C++ member saveVTK(). |
153 |
""" |
154 |
saveVTK(filename, domain, metadata, metadata_schema, **data) |
155 |
|