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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3892 - (hide annotations)
Tue Apr 10 08:57:23 2012 UTC (7 years, 5 months ago) by jfenwick
File MIME type: text/x-python
File size: 6711 byte(s)
Merged changes across from the attempt2 branch.
This version builds and passes python2 tests.
It also passes most python3 tests.



1 caltinay 2812
2     ########################################################
3     #
4 jfenwick 2881 # Copyright (c) 2003-2010 by University of Queensland
5 caltinay 2812 # 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 jfenwick 2881 __copyright__="""Copyright (c) 2003-2010 by University of Queensland
15 caltinay 2812 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 jfenwick 3892 from .weipacpp import visitInitialize, visitPublishData
23 caltinay 3364
24 caltinay 3344 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 jfenwick 3775 for n,d in list(data.items()):
35 caltinay 3344 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 jfenwick 3892 from .weipacpp import EscriptDataset
55 caltinay 3344 dataset=EscriptDataset()
56     domain,new_data=interpolateEscriptData(domain, data)
57     dataset.setDomain(domain)
58 jfenwick 3775 for n,d in sorted(new_data.items()):
59 caltinay 3344 #TODO: data units are not supported here yet
60     dataset.addData(d, n, "")
61     return dataset
62    
63 caltinay 3623 def saveSilo(filename, domain=None, write_meshdata=False, **data):
64 caltinay 3344 """
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 caltinay 3623 :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 caltinay 3344 :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 caltinay 3623 dataset.setSaveMeshData(write_meshdata)
93 caltinay 3627 return dataset.saveSilo(filename)
94 caltinay 3344
95 caltinay 3623 def saveVTK(filename, domain=None, metadata='', metadata_schema=None, write_meshdata=False, **data):
96 caltinay 3344 """
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 caltinay 3623 :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 caltinay 3344 :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 jfenwick 3775 for i,p in list(metadata_schema.items()):
153 caltinay 3344 ss="%s xmlns:%s=\"%s\""%(ss, i, p)
154     else:
155     ss=metadata_schema
156     dataset.setMetadataSchemaString(ss.strip(), ms.strip())
157 caltinay 3623 dataset.setSaveMeshData(write_meshdata)
158     return dataset.saveVTK(filename)
159 caltinay 3344
160 caltinay 3355 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 caltinay 3623 return saveVTK(filename, domain, metadata, metadata_schema, **data)
165 caltinay 3355

  ViewVC Help
Powered by ViewVC 1.1.26