/[escript]/branches/subworld2/escriptcore/py_src/splitworld.py
ViewVC logotype

Contents of /branches/subworld2/escriptcore/py_src/splitworld.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5505 - (show annotations)
Wed Mar 4 23:05:47 2015 UTC (3 years, 11 months ago) by jfenwick
File MIME type: text/x-python
File size: 5043 byte(s)
Changes which were sitting in my trunk dir
1 ##############################################################################
2 #
3 # Copyright (c) 2014-2015 by University of Queensland
4 # http://www.uq.edu.au
5 #
6 # Primary Business: Queensland, Australia
7 # Licensed under the Open Software License version 3.0
8 # http://www.opensource.org/licenses/osl-3.0.php
9 #
10 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
11 # Development 2012-2013 by School of Earth Sciences
12 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
13 #
14 ##############################################################################
15
16 __copyright__="""Copyright (c) 2014-2015 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 __author__="Joel Fenwick"
24
25 """
26 This module contains the Python side of the SplitWorld functionality.
27
28 :var __author__: name of author
29 :var __copyright__: copyrights
30 :var __license__: licence agreement
31 :var __url__: url entry point on documentation
32 :var __version__: version
33 :var __date__: date of the version
34 """
35
36 import warnings
37 warnings.simplefilter('default', category=DeprecationWarning)
38
39 from . import escriptcpp as escore
40
41 class Job(object):
42 """
43 Describes a sequence of work to be carried out in a subworld.
44 The instances of this class used in the subworlds will
45 be constructed by the system.
46 The majority of the work done by the Job will be in the
47 *overloaded* work() method.
48 To do specific work, this class should be subclassed and the work()
49 (and possibly __init__ methods overloaded).
50 The majority of the work done by the job will be in the *overloaded* work() method.
51 The work() method should retreive values from the outside using importValue() and pass values to
52 the rest of the system using exportValue().
53 The rest of the methods should be considered off limits.
54 """
55
56 def __init__(self, *args, **kwargs):
57 """
58 It ignores all of its parameters, except, it requires the following as keyword arguments
59 :var domain: Domain to be used as the basis for all ``Data`` and PDEs in this Job.
60 :var jobid: sequence number of this job. The first job has id=1
61 :type jobid: Positive ``int``
62 """
63 self.domain=kwargs["domain"]
64 self.jobid=kwargs["jobid"]
65 self.wantedvalues=[] # names of shared values this job wishes to import
66 self.importedvalues={} # name:values of which this jobs wants to use
67 self.exportedvalues={} # name:values exported by this job
68
69
70 def wantValue(self, name):
71 """
72 Register your interest in importing a variable with the given name
73 """
74 self.wantedvalues.append(name)
75
76 def setImportValue(self, name, v):
77 """
78 Use to make a value available to the job (ie called from outside the job)
79 :var name: label used to identify this import
80 :type name: ``str``
81 :var v: value to be imported
82 :type v: ?
83 """
84 self.importedvalues[name]=v
85
86
87 def getExportValue(self, name):
88 """
89 get value exported by work() [called from outside the job]
90 """
91 if name in self.exportedvalues:
92 return self.exportedvalues[name]
93 else:
94 return None
95
96 def exportValue(self, name, v):
97 """
98 Make value v available to other Jobs under the label name.
99 name must have already been registered with the SplitWorld instance.
100 For use inside the work() method.
101 :var name: registered label for exported value
102 :type name: ``str``
103 """
104 self.exportedvalues[name]=v
105
106 def importValue(self, name):
107 """
108 For use inside the work() method.
109 :var name: label for imported value.
110 :type name: ``str``
111 """
112 if name in self.importedvalues:
113 return self.importedvalues[name]
114 else:
115 raise KeyError("Attempt to import variable \'"+name+"\' which is not available to this job.")
116 return None
117
118 def clearExports(self):
119 """
120 Remove exported values from the map
121 """
122 self.exportedvalues.clear()
123
124 def clearImports(self):
125 """
126 Remove imported values from their map
127 """
128 self.importedvalues.clear()
129
130 def requestImport(self, name):
131 """
132 Adds name to the list of imports
133 """
134 if (not isinstance(name, str)) or len(name)==0:
135 raise ValueError("Imports must be identified with non-empty strings")
136 if not name in self.wantedvalues:
137 self.wantedvalues+=name
138
139 def work(self):
140 """
141 Need to be overloaded for the job to actually do anthing.
142 A return value of True, indicates this job thinks it is done.
143 A return value of False indicates work still to be done
144 """
145 return True
146
147 class FunctionJob(Job):
148 """
149 Takes a python function (with no params) to be called as the work method
150 """
151 def __init__(self, fn):
152 super(FunctionJob, self).__init__()
153 self.__fn__ = fn
154
155 def work(self):
156 self.__fn__()
157 return True

  ViewVC Help
Powered by ViewVC 1.1.26