/[escript]/trunk/downunder/py_src/mappings.py
ViewVC logotype

Contents of /trunk/downunder/py_src/mappings.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3947 - (show annotations)
Wed Aug 22 23:19:10 2012 UTC (7 years, 2 months ago) by caltinay
File MIME type: text/x-python
File size: 3252 byte(s)
Compiling and installing downunder module now. Adjusted import statements
accordingly. Added a gravity test run.

1
2 ########################################################
3 #
4 # Copyright (c) 2003-2012 by University of Queensland
5 # 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 __copyright__="""Copyright (c) 2003-2012 by University of Queensland
15 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 __all__ = ['Mapping', 'BoundedRangeMapping', 'ScalingMapping']
23
24 from esys.escript import inf, sup, log, tanh
25
26 class Mapping(object):
27 """
28 An abstract mapping class.
29 """
30
31 def __init__(self, *args):
32 pass
33
34 def __call__(self, m):
35 """
36 short for getValue(m).
37 """
38 return self.getValue(m)
39
40 def getValue(self, m):
41 """
42 returns the value of the mapping for m
43 """
44 raise NotImplementedError
45
46 def getDerivative(self, m):
47 """
48 returns the value for the derivative of the mapping for m
49 """
50 raise NotImplementedError
51
52 def getInverse(self, s):
53 """
54 returns the value of the inverse of the mapping for s
55 """
56 raise NotImplementedError
57
58
59 class BoundedRangeMapping(Mapping):
60 """
61 Maps an unbounded parameter to a bounded range. The mapping is smooth and
62 continuous.
63 """
64
65 def __init__(self, s_min=0, s_max=1):
66 if not s_min < s_max:
67 raise ValueError("value for s_min must be less than the value for s_max.")
68 self.s_min=s_min
69 self.s_max=s_max
70
71 def getValue(self, m):
72 """
73 returns the value of the mapping for m
74 """
75 return (self.s_max+self.s_min)/2. + (self.s_max-self.s_min)/2. * tanh(m)
76
77 def getDerivative(self, m):
78 """
79 returns the value for the derivative of the mapping for m
80 """
81 return ((self.s_max-self.s_min)/2.) * (1.-tanh(m)**2.)
82
83 def getInverse(self, s):
84 """
85 returns the value of the inverse of the mapping for s
86 """
87 if not (inf(s) > self.s_min and sup(s) < self.s_max):
88 raise ValueError("s is out of range [%f,%f]"%(inf(s),sup(s)))
89
90 return 1./2. * log( (s-self.s_min)/(self.s_max-s) )
91
92
93 class ScalingMapping(Mapping):
94 """
95 Maps a parameter by scaling it with a constant.
96 """
97
98 def __init__(self, alpha=1):
99 if alpha==0:
100 raise ValueError("Scaling parameter must not be 0.")
101 self.__alpha=alpha
102
103 def getValue(self, m):
104 """
105 returns the value of the mapping for m
106 """
107 return self.__alpha*m
108
109 def getDerivative(self, m):
110 """
111 returns the value for the derivative of the mapping for m
112 """
113 return self.__alpha
114
115 def getInverse(self, s):
116 """
117 returns the value of the inverse of the mapping for s
118 """
119 return s/self.__alpha
120

  ViewVC Help
Powered by ViewVC 1.1.26