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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 147 by jgs, Fri Aug 12 01:45:47 2005 UTC revision 148 by jgs, Tue Aug 23 01:24:31 2005 UTC
# Line 1  Line 1 
1  # $Id$  # $Id$
2  from escript.escript import *  from escript.escript import *
3  from escript.modelframe import Model  from escript.modelframe import Model,ParameterSet
4  from math import log  from math import log
5    
6  class Sequencer(Model):  class Sequencer(Model):
     """@brief runs through time until t_end is reached.  
   
            @param t_end (in) -  model is terminate when t_end is passed (default Model.UNDEF_DT)  
            @param dt_max (in) -  maximum time step size (default Model.UNDEF_DT)  
            @param t (out) -  current time  
   
7      """      """
8      def __init__(self,debug=False):      Runs through time until t_end is reached.
9        """
10        def __init__(self,t=0.,t_end=Model.UNDEF_DT,dt_max=Model.UNDEF_DT,debug=False):
11            """
12               @param t_end: - model is terminated when t_end is passed  (exposed in writeXML)
13               @type t_end: float
14               @param dt_max: - maximum time step size
15               @type dt_max: float
16               @param t: - initial time
17               @type t: float
18    
19             """
20          Model.__init__(self,debug=debug)          Model.__init__(self,debug=debug)
21          self.declareParameter(t=0., \          self.declareParameter(t=t, \
22                                t_end=Model.UNDEF_DT,  \                                t_end=t_end,  \
23                                dt_max=Model.UNDEF_DT)                                dt_max=dt_max)
24    
25      def doInitialization(self):      def doInitialization(self):
26        self.__t_old=self.t          """
27                @brief initialize time integration
28            """
29            self.__t_old = self.t
30    
31      def doStepPreprocessing(self,dt):      def doStepPreprocessing(self, dt):
32        self.t=self.__t_old+dt          self.t = self.__t_old+dt
33    
34      def doStepPostprocessing(self,dt):      def doStepPostprocessing(self, dt):
35        self.__t_old=self.t          self.__t_old = self.t
36    
37      def finalize(self):      def finalize(self):
38        """true when t has reached t_end"""          """
39        return self.t>=self.t_end          true when t has reached t_end
40            """
41      def getSafeTimeStepSize(self,dt):          return self.t >= self.t_end
       """returns dt_max"""  
       return self.dt_max  
42    
43  class GausseanProfile(Model):      def getSafeTimeStepSize(self, dt):
44      """@brief generates a gaussean profile at center x_c, width width and height A over a domain          """
45            returns dt_max
46            """
47            return self.dt_max
48    
49             @param domain (in) - domain  class GaussianProfile(ParameterSet):
50             @param x_c (in)  - center of the Gaussean profile (default [0.,0.,0.])      """
51             @param A (in)  - height of the profile. A maybe a vector. (default 1.)      Generates a gaussian profile at center x_c, width width and height A
52             @param width (in) - width of the profile (default 0.1)      over a domain
            @param r (in) -  radius of the circle (default = 0)  
            @param out (callable) - profile  
53    
54        @param domain: (in) - domain
55        @param x_c: (in)  - center of the Gaussian profile (default [0.,0.,0.])
56        @param A: (in)  - height of the profile. A maybe a vector. (default 1.)
57        @param width: (in) - width of the profile (default 0.1)
58        @param r: (in) -  radius of the circle (default = 0)
59        @param out: (callable) - profile
60    
61         In the case that the spatial dimension is two, The third component of x_c is dropped      In the case that the spatial dimension is two, The third component of
62        x_c is dropped
63      """      """
64      def __init__(self,debug=False):      def __init__(self,debug=False):
65          Model.__init__(self,debug=debug)          ParameterSet.__init__(self,debug=debug)
66          self.declareParameter(domain=None,          self.declareParameter(domain=None,
67                                x_c=numarray.zeros([3]),                                x_c=numarray.zeros([3]),
68                                A=1.,                                A=1.,
# Line 56  class GausseanProfile(Model): Line 70  class GausseanProfile(Model):
70                                r=0)                                r=0)
71    
72      def out(self):      def out(self):
73          x=self.domain.getX()          x = self.domain.getX()
74          dim=self.domain.getDim()          dim = self.domain.getDim()
75          l=length(x-self.x_c[:dim])          l = length(x-self.x_c[:dim])
76          m=(l-self.r).whereNegative()          m = (l-self.r).whereNegative()
77    
78          return (m+(1.-m)*exp(-log(2.)*(l/self.width)**2))*self.A          return (m+(1.-m)*exp(-log(2.)*(l/self.width)**2))*self.A
79    
80  class InterpolateOverBox(Model):  class InterpolateOverBox(ParameterSet):
81      """      """
82             @brief returns values at each time. The values are defined through given values at time node.      Returns values at each time. The values are defined through given values
83        at time node.
            @param domain (in) - domain  
            @param left_bottom_front (in) - coordinates of left,bottom,front corner of the box  
            @param right_top_back (in) - coordinates of the right, top, back corner of the box  
            @param value_left_bottom_front (in) - value at left,bottom,front corner  
            @param value_right_bottom_front (in) - value at right, bottom, front corner  
            @param value_left_top_front (in) - value at left,top,front corner  
            @param value_right_top_front (in) - value at right,top,front corner  
            @param value_left_bottom_back (in) - value at  left,bottom,back corner  
            @param value_right_bottom_back (in) - value at right,bottom,back corner  
            @param value_left_top_back (in) - value at left,top,back  corner  
            @param value_right_top_back (in) - value at right,top,back corner  
            @param out (callable) - values at doamin locations by bilinear interpolation. for two dimensional domains back values are ignored.  
84    
85        @param domain: (in) - domain
86        @param left_bottom_front: (in) - coordinates of left,bottom,front corner of the box
87        @param right_top_back: (in) - coordinates of the right, top, back corner of the box
88        @param value_left_bottom_front: (in) - value at left,bottom,front corner
89        @param value_right_bottom_front: (in) - value at right, bottom, front corner
90        @param value_left_top_front: (in) - value at left,top,front corner
91        @param value_right_top_front: (in) - value at right,top,front corner
92        @param value_left_bottom_back: (in) - value at  left,bottom,back corner
93        @param value_right_bottom_back: (in) - value at right,bottom,back corner
94        @param value_left_top_back: (in) - value at left,top,back  corner
95        @param value_right_top_back: (in) - value at right,top,back corner
96        @param out: (callable) - values at doamin locations by bilinear interpolation. for two dimensional domains back values are ignored.
97      """      """
98    
99      def __init__(self,debug=False):      def __init__(self, debug=False):
100          Model.__init__(self,debug=debug)          ParameterSet.__init__(self, debug=debug)
101          self.declareParameter(domain=None,          self.declareParameter(domain=None,
102                                left_bottom_front=[0.,0.,0.],                                left_bottom_front=[0.,0.,0.],
103                                right_top_back=[1.,1.,1.],                                right_top_back=[1.,1.,1.],
# Line 97  class InterpolateOverBox(Model): Line 112  class InterpolateOverBox(Model):
112    
113    
114      def out(self):      def out(self):
115          x=self.domain.getX()          x = self.domain.getX()
116          if self.domain.getDim()==2:          if self.domain.getDim() == 2:
117           f_right=(x[0]-self.left_bottom_front[0])/(self.right_top_back[0]-self.left_bottom_front[0])              f_right = (x[0] - self.left_bottom_front[0])/\
118           f_left=1.-f_right           (self.right_top_back[0] - self.left_bottom_front[0])
119           f_top=(x[1]-self.left_bottom_front[1])/(self.right_top_back[1]-self.left_bottom_front[1])              f_left = 1. - f_right
120           f_bottom=1.-f_top              f_top = (x[1] - self.left_bottom_front[1])/\
121           out=self.value_left_bottom_front * f_left * f_bottom \           (self.right_top_back[1] - self.left_bottom_front[1])
122              +self.value_right_bottom_front* f_right * f_bottom \              f_bottom = 1. - f_top
123              +self.value_left_top_front    * f_left * f_top \              out = self.value_left_bottom_front * f_left * f_bottom \
124              +self.value_right_top_front   * f_right * f_top                  + self.value_right_bottom_front* f_right * f_bottom \
125                    + self.value_left_top_front    * f_left * f_top \
126          else:                  + self.value_right_top_front   * f_right * f_top
127           f_right=(x[0]-self.left_bottom_front[0])/(self.right_top_back[0]-self.left_bottom_front[0])          else:
128           f_left=1.-f_right              f_right = (x[0] - self.left_bottom_front[0])/\
129           f_top=(x[1]-self.left_bottom_front[1])/(self.right_top_back[1]-self.left_bottom_front[1])                      (self.right_top_back[0] - self.left_bottom_front[0])
130           f_bottom=1.-f_top              f_left = 1. - f_right
131           f_back=(x[2]-self.left_bottom_front[1])/(self.right_top_back[2]-self.left_bottom_front[2])              f_top = (x[1] - self.left_bottom_front[1])/\
132           f_front=1.-f_back                      (self.right_top_back[1] - self.left_bottom_front[1])
133           out=self.value_left_bottom_front * f_left * f_bottom * f_front \              f_bottom = 1. - f_top
134              +self.value_right_bottom_front* f_right * f_bottom * f_front \              f_back = (x[2] - self.left_bottom_front[1])/\
135              +self.value_left_top_front    * f_left * f_top * f_front \                      (self.right_top_back[2] - self.left_bottom_front[2])
136              +self.value_right_top_front   * f_right * f_top * f_front \              f_front = 1. - f_back
137              +self.value_left_bottom_back  * f_left * f_bottom * f_back \              out = self.value_left_bottom_front * f_left * f_bottom * f_front \
138              +self.value_right_bottom_back * f_right * f_bottom * f_back \                  + self.value_right_bottom_front* f_right * f_bottom * f_front \
139              +self.value_left_top_back     * f_left * f_top * f_back \                  + self.value_left_top_front    * f_left * f_top * f_front \
140              +self.value_right_top_back    * f_right * f_top * f_back                  + self.value_right_top_front   * f_right * f_top * f_front \
141                    + self.value_left_bottom_back  * f_left * f_bottom * f_back \
142                    + self.value_right_bottom_back * f_right * f_bottom * f_back \
143                    + self.value_left_top_back     * f_left * f_top * f_back \
144                    + self.value_right_top_back    * f_right * f_top * f_back
145          return out          return out
146    
147    
148  class InterpolatedTimeProfile(Model):  class InterpolatedTimeProfile(ParameterSet):
149         """@brief returns values at each time. The values are defined through given values at time node.         """
               
          value[i] defines the value at time nodes[i]. Between nodes linear interpolation is used.  
          for time t<nodes[0] value[0] is used and for t>nodes[l] values[l] is used where l=len(nodes)-1.  
   
   
           @param t (in) - current time  
           @param node (in) - list of time nodes  
           @param values (in) - list of values at time nodes  
           @param out (callable) - current value  
150    
151           Returns values at each time. The values are defined through given
152           values at time node.
153                
154           value[i] defines the value at time nodes[i]. Between nodes linear
155           interpolation is used.
156    
157          """         For time t<nodes[0], value[0] is used and for t>nodes[l], values[l]
158           is used where l=len(nodes)-1.
159    
160           @param t: (in) - current time
161           @param node: (in) - list of time nodes
162           @param values: (in) - list of values at time nodes
163           @param out: (callable) - current value
164           """
165    
166         def __init__(self,debug=False):         def __init__(self,debug=False):
167             Model.__init__(self,debug=debug)             ParameterSet.__init__(self,debug=debug)
168             self.declareParameter(t=0., \             self.declareParameter(t=0., \
169                                   nodes=[0.,1.],\                                   nodes=[0.,1.],\
170                                   values=[1.,1.])                                   values=[1.,1.])
171         def out(self):         def out(self):
172             l=len(self.nodes)-1             l = len(self.nodes) - 1
173             t=self.t             t = self.t
174             if t<=self.nodes[0]:             if t <= self.nodes[0]:
175                  return self.values[0]                 return self.values[0]
176             else:             else:
177                 for i in range(1,l):                 for i in range(1,l):
178                    if t<self.nodes[i]:                    if t < self.nodes[i]:
179                      m=(self.values[i-1]-self.values[i])/(self.nodes[i-1]-self.nodes[i])                        m = (self.values[i-1] - self.values[i])/\
180                      return m*(t-self.nodes[i-1])+self.values[i-1]                              (self.nodes[i-1] - self.nodes[i])
181                          return m*(t-self.nodes[i-1]) + self.values[i-1]
182                 return self.values[l]                 return self.values[l]
183    
184  class LinearCombination(Model):  class LinearCombination(Model):
185         """@brief returns a linear combination of the f0*v0+f1*v1+f2*v2+f3*v3+f4*v4      """
186        Returns a linear combination of the f0*v0+f1*v1+f2*v2+f3*v3+f4*v4
187                            
188           @param f0 (in) numerical object or None (default: None)      @param f0: (in) numerical object or None (default: None)
189           @param v0 (in) numerical object or None (default: None)      @param v0: (in) numerical object or None (default: None)
190           @param f1 (in) numerical object or None (default: None)      @param f1: (in) numerical object or None (default: None)
191           @param v1 (in) numerical object or None (default: None)      @param v1: (in) numerical object or None (default: None)
192           @param f2 (in) numerical object or None (default: None)      @param f2: (in) numerical object or None (default: None)
193           @param v2 (in) numerical object or None (default: None)      @param v2: (in) numerical object or None (default: None)
194           @param f3 (in) numerical object or None (default: None)      @param f3: (in) numerical object or None (default: None)
195           @param v3 (in) numerical object or None (default: None)      @param v3: (in) numerical object or None (default: None)
196           @param f4 (in) numerical object or None (default: None)      @param f4: (in) numerical object or None (default: None)
197           @param v4 (in) numerical object or None (default: None)      @param v4: (in) numerical object or None (default: None)
198           @param out (callable) - current value      @param out: (callable) - current value
199        """
200        def __init__(self,debug=False):
201            Model.__init__(self,debug=debug)
202            self.declareParameter(f0=None, \
203                                  v0=None, \
204                                  f1=None, \
205                                  v1=None, \
206                                  f2=None, \
207                                  v2=None, \
208                                  f3=None, \
209                                  v3=None, \
210                                  f4=None, \
211                                  v4=None)
212    
213        def out(self):
214            if not self.f0 == None and not self.v0 == None:
215                fv0 = self.f0*self.v0
216            else:
217                fv0 = None
218    
219          """          if not self.f1 == None and not self.v1 == None:
220         def __init__(self,debug=False):              fv1 = self.f1*self.v1
221             Model.__init__(self,debug=debug)          else:
222             self.declareParameter(f0=None, \              fv1 = None
                                  v0=None, \  
                                  f1=None, \  
                                  v1=None, \  
                                  f2=None, \  
                                  v2=None, \  
                                  f3=None, \  
                                  v3=None, \  
                                  f4=None, \  
                                  v4=None)  
        def out(self):  
            if not self.f0==None and not self.v0==None:  
                 fv0=self.f0*self.v0  
            else:  
                 fv0=None  
223    
224             if not self.f1==None and not self.v1==None:          if not self.f2 == None and not self.v2 == None:
225                  fv1=self.f1*self.v1              fv2 = f2*v2
226             else:          else:
227                  fv1=None              fv2 = None
228    
229             if not self.f2==None and not self.v2==None:          if not self.f3 == None and not self.v3 == None:
230                  fv2=f2*v2              fv3 = self.f3*self.v3
231             else:          else:
232                  fv2=None              fv3 = None
233    
234             if not self.f3==None and not self.v3==None:          if not self.f4 == None and not self.v4 == None:
235                  fv3=self.f3*self.v3              fv4 = self.f4*self.v4
236             else:          else:
237                  fv3=None              fv4 = None
238    
239             if not self.f4==None and not self.v4==None:          if fv0 == None:
240                  fv4=self.f4*self.v4               out = 0.
241             else:          else:
242                  fv4=None               out = fv0
243            if not fv1 == None:
244                out += fv1
245            if not fv2 == None:
246                out += fv2
247            if not fv3 == None:
248                out += fv3
249            return out
250    
251             if fv0==None:  # vim: expandtab shiftwidth=4:
               out=0.  
            else:  
               out=fv0  
            if not fv1==None: out+=fv1  
            if not fv2==None: out+=fv2  
            if not fv3==None: out+=fv3  
            return out  

Legend:
Removed from v.147  
changed lines
  Added in v.148

  ViewVC Help
Powered by ViewVC 1.1.26