/[escript]/branches/3.4.1/modellib/py_src/input.py
ViewVC logotype

Annotation of /branches/3.4.1/modellib/py_src/input.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2061 - (hide annotations)
Wed Nov 19 03:40:21 2008 UTC (10 years, 5 months ago) by jfenwick
Original Path: trunk/modellib/py_src/input.py
File MIME type: text/x-python
File size: 20478 byte(s)
Fixing some warnings from epydoc.
Disabled c++ signatures in python docstrings.
Removed references to Bruce in epydoc and the users guide.
1 ksteube 1809
2     ########################################################
3 ksteube 1312 #
4 ksteube 1809 # Copyright (c) 2003-2008 by University of Queensland
5     # Earth Systems Science Computational Center (ESSCC)
6     # http://www.uq.edu.au/esscc
7 ksteube 1312 #
8 ksteube 1809 # 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 ksteube 1312 #
12 ksteube 1809 ########################################################
13 elspeth 628
14 ksteube 1809 __copyright__="""Copyright (c) 2003-2008 by University of Queensland
15     Earth Systems Science Computational Center (ESSCC)
16     http://www.uq.edu.au/esscc
17     Primary Business: Queensland, Australia"""
18 elspeth 628 __license__="""Licensed under the Open Software License version 3.0
19 ksteube 1809 http://www.opensource.org/licenses/osl-3.0.php"""
20     __url__="http://www.uq.edu.au/esscc/escript-finley"
21 elspeth 628
22 jgs 149 from esys.escript import *
23     from esys.escript.modelframe import Model,ParameterSet
24 gross 927 from esys.escript.linearPDEs import LinearPDE
25 jgs 147 from math import log
26 jgs 127
27 jgs 147 class Sequencer(Model):
28 jgs 148 """
29 gross 814 Runs through time until t_end is reached.
30 gross 720
31 gross 814 @ivar t_end: model is terminated when t_end is passed, default 1 (in).
32     @type t_end: C{float}
33     @ivar dt_max: maximum time step size, default L{Model.UNDEF_DT} (in)
34     @type dt_max: C{float}
35     @ivar t: current time stamp (in/out). By default it is initialized with zero.
36     @type t: C{float}
37    
38 jgs 148 """
39 gross 918 def __init__(self,**kwargs):
40 jgs 148 """
41 jgs 149 """
42 gross 918 super(Sequencer,self).__init__(**kwargs)
43 gross 423 self.declareParameter(t=0.,
44     t_end=1.,
45     dt_max=Model.UNDEF_DT)
46 jgs 127
47 jgs 147 def doInitialization(self):
48 jgs 148 """
49 jgs 149 initialize time integration
50 jgs 148 """
51     self.__t_old = self.t
52 jgs 147
53 jgs 148 def doStepPreprocessing(self, dt):
54     self.t = self.__t_old+dt
55 jgs 147
56 jgs 148 def doStepPostprocessing(self, dt):
57     self.__t_old = self.t
58 jgs 147
59     def finalize(self):
60 jgs 148 """
61 gross 814 returns true when L{t} has reached L{t_end}
62 jgs 148 """
63     return self.t >= self.t_end
64 jgs 147
65 jgs 148 def getSafeTimeStepSize(self, dt):
66     """
67 gross 814 returns L{dt_max}
68 jgs 148 """
69     return self.dt_max
70 jgs 147
71 jgs 148 class GaussianProfile(ParameterSet):
72     """
73 jgs 149 Generates a Gaussian profile at center x_c, width width and height A
74 jgs 148 over a domain
75 jgs 127
76 gross 814 @ivar domain: domain
77     @ivar x_c: center of the Gaussian profile (default [0.,0.,0.])
78     @ivar A: (in) height of the profile. A maybe a vector. (default 1.)
79     @ivar width: (in) width of the profile (default 0.1)
80     @ivar r: (in) radius of the circle (default = 0)
81 jgs 127
82 jgs 148 In the case that the spatial dimension is two, The third component of
83 gross 814 x_c is dropped.
84 jgs 127 """
85 gross 918 def __init__(self,**kwargs):
86     super(GaussianProfile, self).__init__(**kwargs)
87 jgs 147 self.declareParameter(domain=None,
88     x_c=numarray.zeros([3]),
89     A=1.,
90     width=0.1,
91     r=0)
92 jgs 127
93     def out(self):
94 jgs 149 """
95     Generate the Gaussian profile
96 gross 814
97     Link against this method to get the output of this model.
98 jgs 149 """
99 jgs 148 x = self.domain.getX()
100     dim = self.domain.getDim()
101     l = length(x-self.x_c[:dim])
102 gross 323 m = whereNegative(l-self.r)
103 jgs 148
104 jgs 127 return (m+(1.-m)*exp(-log(2.)*(l/self.width)**2))*self.A
105    
106 jgs 148 class InterpolateOverBox(ParameterSet):
107 jgs 147 """
108 jgs 148 Returns values at each time. The values are defined through given values
109 gross 814 at time node. For two dimensional domains back values are ignored.
110 jgs 147
111 gross 814 @ivar domain: domain
112     @ivar value_left_bottom_front: (in) value at left,bottom,front corner
113     @ivar value_right_bottom_front: (in) value at right, bottom, front corner
114     @ivar value_left_top_front: (in) value at left,top,front corner
115     @ivar value_right_top_front: (in) value at right,top,front corner
116     @ivar value_left_bottom_back: (in) value at left,bottom,back corner
117     @ivar value_right_bottom_back: (in) value at right,bottom,back corner
118     @ivar value_left_top_back: (in) value at left,top,back corner
119     @ivar value_right_top_back: (in) value at right,top,back corner
120 jgs 147 """
121    
122 gross 918 def __init__(self, **kwargs):
123 gross 911 super(InterpolateOverBox, self).__init__(self)
124 jgs 147 self.declareParameter(domain=None,
125     value_left_bottom_front=0.,
126     value_right_bottom_front=0.,
127     value_left_top_front=0.,
128     value_right_top_front=0.,
129     value_left_bottom_back=0.,
130     value_right_bottom_back=0.,
131     value_left_top_back=0.,
132     value_right_top_back=0.)
133    
134    
135     def out(self):
136 gross 814 """
137     values at domain locations by bilinear interpolation of the given values.
138    
139     Link against this method to get the output of this model.
140     """
141 jgs 148 x = self.domain.getX()
142     if self.domain.getDim() == 2:
143 gross 819 x0,x1=x[0],x[1]
144     left_bottom_front0,right_top_back0=inf(x0),sup(x0)
145 gross 820 left_bottom_front1,right_top_back1=inf(x1),sup(x1)
146     f_right = (x0 - left_bottom_front0)/(right_top_back0 -left_bottom_front0)
147 jgs 148 f_left = 1. - f_right
148 gross 820 f_top = (x1 - left_bottom_front1)/(right_top_back1 - left_bottom_front1)
149 jgs 148 f_bottom = 1. - f_top
150 gross 816 out = f_left * f_bottom * self.value_left_bottom_front \
151     + f_right * f_bottom * self.value_right_bottom_front \
152     + f_left * f_top * self.value_left_top_front \
153     + f_right * f_top * self.value_right_top_front
154 jgs 147 else:
155 gross 819 x0,x1,x2=x[0],x[1],x[2]
156     left_bottom_front0,right_top_back0=inf(x0),sup(x0)
157 gross 820 left_bottom_front1,right_top_back1=inf(x1),sup(x1)
158     left_bottom_front2,right_top_back2=inf(x2),sup(x2)
159     f_right = (x0 - left_bottom_front0)/(right_top_back0 - left_bottom_front0)
160 jgs 148 f_left = 1. - f_right
161 gross 820 f_top = (x1 - left_bottom_front1)/(right_top_back1 - left_bottom_front1)
162 jgs 148 f_bottom = 1. - f_top
163 gross 820 f_back = (x2 - left_bottom_front1)/(right_top_back2 - left_bottom_front2)
164 jgs 148 f_front = 1. - f_back
165 gross 816 out = f_left * f_bottom * f_front * self.value_left_bottom_front\
166     + f_right * f_bottom * f_front * self.value_right_bottom_front\
167     + f_left * f_top * f_front * self.value_left_top_front\
168     + f_right * f_top * f_front * self.value_right_top_front\
169     + f_left * f_bottom * f_back * self.value_left_bottom_back\
170     + f_right * f_bottom * f_back * self.value_right_bottom_back\
171     + f_left * f_top * f_back * self.value_left_top_back\
172     + f_right * f_top * f_back * self.value_right_top_back
173 jgs 147 return out
174    
175    
176 jgs 148 class InterpolatedTimeProfile(ParameterSet):
177     """
178    
179     Returns values at each time. The values are defined through given
180     values at time node.
181 jgs 147
182 jgs 148 value[i] defines the value at time nodes[i]. Between nodes linear
183     interpolation is used.
184    
185     For time t<nodes[0], value[0] is used and for t>nodes[l], values[l]
186     is used where l=len(nodes)-1.
187 jgs 147
188 gross 814 @ivar t: (in) current time
189     @ivar node: (in) list of time nodes
190     @ivar values: (in) list of values at time nodes
191 jgs 148 """
192 jgs 127
193 gross 918 def __init__(self,**kwargs):
194     super( InterpolatedTimeProfile, self).__init__(**kwargs)
195 jgs 147 self.declareParameter(t=0., \
196     nodes=[0.,1.],\
197     values=[1.,1.])
198     def out(self):
199 gross 814 """
200     current value
201    
202     Link against this method to get the output of this model.
203     """
204 jgs 148 l = len(self.nodes) - 1
205     t = self.t
206     if t <= self.nodes[0]:
207     return self.values[0]
208 jgs 147 else:
209     for i in range(1,l):
210 jgs 148 if t < self.nodes[i]:
211     m = (self.values[i-1] - self.values[i])/\
212     (self.nodes[i-1] - self.nodes[i])
213     return m*(t-self.nodes[i-1]) + self.values[i-1]
214 jgs 147 return self.values[l]
215 jgs 127
216 gross 903 class ScalarDistributionFromTags(ParameterSet):
217 jgs 148 """
218 gross 953 creates a scalar distribution on a domain from tags, If tag_map is given
219     the tags can be given a names and tag_map is used to map it into domain tags.
220 gross 903
221     @ivar domain: domain
222     @type domain: L{esys.escript.Domain}
223     @ivar default: default value
224     @ivar tag0: tag 0
225     @type tag0: C{int}
226     @ivar value0: value for tag 0
227     @type value0: C{float}
228     @ivar tag1: tag 1
229     @type tag1: C{int}
230     @ivar value1: value for tag 1
231     @type value1: C{float}
232     @ivar tag2: tag 2
233     @type tag2: C{int}
234     @ivar value2: value for tag 2
235     @type value2: C{float}
236     @ivar tag3: tag 3
237     @type tag3: C{int}
238     @ivar value3: value for tag 3
239     @type value3: C{float}
240     @ivar tag4: tag 4
241     @type tag4: C{int}
242     @ivar value4: value for tag 4
243     @type value4: C{float}
244     @ivar tag5: tag 5
245     @type tag5: C{int}
246     @ivar value5: value for tag 5
247     @type value5: C{float}
248     @ivar tag6: tag 6
249     @type tag6: C{int}
250     @ivar value6: value for tag 6
251     @type value6: C{float}
252     @ivar tag7: tag 7
253     @type tag7: C{int}
254     @ivar value7: value for tag 7
255     @type value7: C{float}
256     @ivar tag8: tag 8
257     @type tag8: C{int}
258     @ivar value8: value for tag 8
259     @type value8: C{float}
260     @ivar tag9: tag 9
261     @type tag9: C{int}
262     @ivar value9: value for tag 9
263     @type value9: C{float}
264     """
265 gross 918 def __init__(self,**kwargs):
266     super(ScalarDistributionFromTags, self).__init__(**kwargs)
267 gross 903 self.declareParameter(domain=None,
268     default=0.,
269     tag0=None,
270     value0=0.,
271     tag1=None,
272     value1=0.,
273     tag2=None,
274     value2=0.,
275     tag3=None,
276     value3=0.,
277     tag4=None,
278     value4=0.,
279     tag5=None,
280     value5=0.,
281     tag6=None,
282     value6=0.,
283     tag7=None,
284     value7=0.,
285     tag8=None,
286     value8=0.,
287     tag9=None,
288     value9=0.)
289    
290    
291     def out(self):
292     """
293     returns a L{esys.escript.Data} object
294     Link against this method to get the output of this model.
295     """
296     d=Scalar(self.default,Function(self.domain))
297 gross 1098 if not self.tag0 == None: d.setTaggedValue(self.tag0,self.value0)
298     if not self.tag1 == None: d.setTaggedValue(self.tag1,self.value1)
299     if not self.tag2 == None: d.setTaggedValue(self.tag2,self.value2)
300     if not self.tag3 == None: d.setTaggedValue(self.tag3,self.value3)
301     if not self.tag4 == None: d.setTaggedValue(self.tag4,self.value4)
302     if not self.tag5 == None: d.setTaggedValue(self.tag5,self.value5)
303     if not self.tag6 == None: d.setTaggedValue(self.tag6,self.value6)
304     if not self.tag7 == None: d.setTaggedValue(self.tag7,self.value7)
305     if not self.tag8 == None: d.setTaggedValue(self.tag8,self.value8)
306     if not self.tag9 == None: d.setTaggedValue(self.tag9,self.value9)
307 gross 903 return d
308    
309 gross 927 class SmoothScalarDistributionFromTags(ParameterSet):
310     """
311     creates a smooth scalar distribution on a domain from region tags
312    
313     @ivar domain: domain
314     @type domain: L{esys.escript.Domain}
315     @ivar default: default value
316     @ivar tag0: tag 0
317     @type tag0: C{int}
318     @ivar value0: value for tag 0
319     @type value0: C{float}
320     @ivar tag1: tag 1
321     @type tag1: C{int}
322     @ivar value1: value for tag 1
323     @type value1: C{float}
324     @ivar tag2: tag 2
325     @type tag2: C{int}
326     @ivar value2: value for tag 2
327     @type value2: C{float}
328     @ivar tag3: tag 3
329     @type tag3: C{int}
330     @ivar value3: value for tag 3
331     @type value3: C{float}
332     @ivar tag4: tag 4
333     @type tag4: C{int}
334     @ivar value4: value for tag 4
335     @type value4: C{float}
336     @ivar tag5: tag 5
337     @type tag5: C{int}
338     @ivar value5: value for tag 5
339     @type value5: C{float}
340     @ivar tag6: tag 6
341     @type tag6: C{int}
342     @ivar value6: value for tag 6
343     @type value6: C{float}
344     @ivar tag7: tag 7
345     @type tag7: C{int}
346     @ivar value7: value for tag 7
347     @type value7: C{float}
348     @ivar tag8: tag 8
349     @type tag8: C{int}
350     @ivar value8: value for tag 8
351     @type value8: C{float}
352     @ivar tag9: tag 9
353     @type tag9: C{int}
354     @ivar value9: value for tag 9
355     @type value9: C{float}
356     """
357     def __init__(self,**kwargs):
358     super(SmoothScalarDistributionFromTags, self).__init__(**kwargs)
359     self.declareParameter(domain=None,
360     default=0.,
361     tag0=None,
362     value0=0.,
363     tag1=None,
364     value1=0.,
365     tag2=None,
366     value2=0.,
367     tag3=None,
368     value3=0.,
369     tag4=None,
370     value4=0.,
371     tag5=None,
372     value5=0.,
373     tag6=None,
374     value6=0.,
375     tag7=None,
376     value7=0.,
377     tag8=None,
378     value8=0.,
379     tag9=None,
380     value9=0.)
381    
382    
383     def __update(self,tag,tag_value,value):
384     if self.__pde==None:
385     self.__pde=LinearPDE(self.domain,numSolutions=1)
386     mask=Scalar(0.,Function(self.domain))
387 gross 1098 mask.setTaggedValue(tag,1.)
388 gross 927 self.__pde.setValue(Y=mask)
389     mask=wherePositive(abs(self.__pde.getRightHandSide()))
390     value*=(1.-mask)
391     value+=tag_value*mask
392     return value
393    
394     def out(self):
395     """
396     returns a L{esys.escript.Data} object
397     Link against this method to get the output of this model.
398     """
399     d=Scalar(self.default,Solution(self.domain))
400     self.__pde=None
401     if not self.tag0 == None: d=self.__update(self.tag0,self.value0,d)
402     if not self.tag1 == None: d=self.__update(self.tag1,self.value1,d)
403     if not self.tag2 == None: d=self.__update(self.tag2,self.value2,d)
404     if not self.tag3 == None: d=self.__update(self.tag3,self.value3,d)
405     if not self.tag4 == None: d=self.__update(self.tag4,self.value4,d)
406     if not self.tag5 == None: d=self.__update(self.tag5,self.value5,d)
407     if not self.tag6 == None: d=self.__update(self.tag6,self.value6,d)
408     if not self.tag7 == None: d=self.__update(self.tag7,self.value7,d)
409     if not self.tag8 == None: d=self.__update(self.tag8,self.value8,d)
410     if not self.tag9 == None: d=self.__update(self.tag9,self.value9,d)
411     return d
412    
413 gross 903 class LinearCombination(ParameterSet):
414     """
415 jgs 148 Returns a linear combination of the f0*v0+f1*v1+f2*v2+f3*v3+f4*v4
416 jgs 147
417 gross 819 @ivar f0: numerical object or None, default=None (in)
418     @ivar v0: numerical object or None, default=None (in)
419     @ivar f1: numerical object or None, default=None (in)
420     @ivar v1: numerical object or None, default=None (in)
421     @ivar f2: numerical object or None, default=None (in)
422     @ivar v2: numerical object or None, default=None (in)
423     @ivar f3: numerical object or None, default=None (in)
424     @ivar v3: numerical object or None, default=None (in)
425     @ivar f4: numerical object or None, default=None (in)
426     @ivar v4: numerical object or None, default=None (in)
427 jgs 148 """
428 gross 918 def __init__(self,**kwargs):
429     super(LinearCombination, self).__init__(**kwargs)
430 jgs 148 self.declareParameter(f0=None, \
431     v0=None, \
432     f1=None, \
433     v1=None, \
434     f2=None, \
435     v2=None, \
436     f3=None, \
437     v3=None, \
438     f4=None, \
439     v4=None)
440 jgs 147
441 jgs 148 def out(self):
442 gross 814 """
443     returns f0*v0+f1*v1+f2*v2+f3*v3+f4*v4.
444     Link against this method to get the output of this model.
445     """
446 jgs 148 if not self.f0 == None and not self.v0 == None:
447     fv0 = self.f0*self.v0
448     else:
449     fv0 = None
450 jgs 147
451 jgs 148 if not self.f1 == None and not self.v1 == None:
452     fv1 = self.f1*self.v1
453     else:
454     fv1 = None
455 jgs 147
456 jgs 148 if not self.f2 == None and not self.v2 == None:
457     fv2 = f2*v2
458     else:
459     fv2 = None
460 jgs 147
461 jgs 148 if not self.f3 == None and not self.v3 == None:
462     fv3 = self.f3*self.v3
463     else:
464     fv3 = None
465 jgs 147
466 jgs 148 if not self.f4 == None and not self.v4 == None:
467     fv4 = self.f4*self.v4
468     else:
469     fv4 = None
470 jgs 147
471 jgs 148 if fv0 == None:
472     out = 0.
473     else:
474     out = fv0
475     if not fv1 == None:
476     out += fv1
477     if not fv2 == None:
478     out += fv2
479     if not fv3 == None:
480     out += fv3
481     return out
482 jgs 147
483 gross 904 class MergeConstraints(ParameterSet):
484     """
485     Returns a linear combination of the f0*v0+f1*v1+f2*v2+f3*v3+f4*v4
486     """
487 gross 918 def __init__(self,**kwargs):
488     super(MergeConstraints, self).__init__(**kwargs)
489 gross 904 self.declareParameter(location_of_constraint0=None, \
490     value_of_constraint0=None, \
491     location_of_constraint1=None, \
492     value_of_constraint1=None, \
493     location_of_constraint2=None, \
494     value_of_constraint2=None, \
495     location_of_constraint3=None, \
496     value_of_constraint3=None, \
497     location_of_constraint4=None, \
498     value_of_constraint4=None)
499     def location_of_constraint(self):
500     """
501     return the values used to constrain a solution
502    
503     @return: the mask marking the locations of the constraints
504 gross 906 @rtype: L{escript.Scalar}
505 gross 904 """
506     out_loc=0
507 gross 906 if not self.location_of_constraint0 == None:
508 gross 904 out_loc=wherePositive(out_loc+wherePositive(self.location_of_constraint0))
509 gross 906 if not self.location_of_constraint1 == None:
510 gross 904 out_loc=wherePositive(out_loc+wherePositive(self.location_of_constraint1))
511 gross 906 if not self.location_of_constraint2 == None:
512 gross 904 out_loc=wherePositive(out_loc+wherePositive(self.location_of_constraint2))
513 gross 906 if not self.location_of_constraint3 == None:
514 gross 904 out_loc=wherePositive(out_loc+wherePositive(self.location_of_constraint3))
515     return out_loc
516    
517     def value_of_constraint(self):
518     """
519     return the values used to constrain a solution
520    
521     @return: values to be used at the locations of the constraints. If
522 jfenwick 2061 C{value} is not given C{None} is rerturned.
523 gross 906 @rtype: L{escript.Scalar}
524 gross 904 """
525     out_loc=0
526     out=0
527 gross 906 if not self.location_of_constraint0 == None:
528 gross 904 tmp=wherePositive(self.location_of_constraint0)
529 gross 906 out=out*(1.-tmp)+self.value_of_constraint0*tmp
530 gross 904 out_loc=wherePositive(out_loc+tmp)
531 gross 906 if not self.location_of_constraint1 == None:
532 gross 904 tmp=wherePositive(self.location_of_constraint1)
533 gross 906 out=out*(1.-tmp)+self.value_of_constraint1*tmp
534 gross 904 out_loc=wherePositive(out_loc+tmp)
535 gross 906 if not self.location_of_constraint2 == None:
536 gross 904 tmp=wherePositive(self.location_of_constraint2)
537 gross 906 out=out*(1.-tmp)+self.value_of_constraint2*tmp
538 gross 904 out_loc=wherePositive(out_loc+tmp)
539 gross 906 if not self.location_of_constraint3 == None:
540 gross 904 tmp=wherePositive(self.location_of_constraint3)
541 gross 906 out=out*(1.-tmp)+self.value_of_constraint3*tmp
542 gross 904 out_loc=wherePositive(out_loc+tmp)
543     return out
544 jgs 148 # vim: expandtab shiftwidth=4:

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26