/[escript]/trunk/modellib/py_src/input.py
ViewVC logotype

Contents of /trunk/modellib/py_src/input.py

Parent Directory Parent Directory | Revision Log Revision Log


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