/[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 3981 - (show annotations)
Fri Sep 21 02:47:54 2012 UTC (7 years, 2 months ago) by jfenwick
File MIME type: text/x-python
File size: 3314 byte(s)
First pass of updating copyright notices
1
2 ##############################################################################
3 #
4 # Copyright (c) 2003-2012 by University of Queensland
5 # http://www.uq.edu.au
6 #
7 # Primary Business: Queensland, Australia
8 # Licensed under the Open Software License version 3.0
9 # http://www.opensource.org/licenses/osl-3.0.php
10 #
11 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development since 2012 by School of Earth Sciences
13 #
14 ##############################################################################
15
16 __copyright__="""Copyright (c) 2003-2012 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 __all__ = ['Mapping', 'BoundedRangeMapping', 'ScalingMapping']
24
25 from esys.escript import inf, sup, log, tanh
26
27 class Mapping(object):
28 """
29 An abstract mapping class.
30 """
31
32 def __init__(self, *args):
33 pass
34
35 def __call__(self, m):
36 """
37 short for getValue(m).
38 """
39 return self.getValue(m)
40
41 def getValue(self, m):
42 """
43 returns the value of the mapping for m
44 """
45 raise NotImplementedError
46
47 def getDerivative(self, m):
48 """
49 returns the value for the derivative of the mapping for m
50 """
51 raise NotImplementedError
52
53 def getInverse(self, s):
54 """
55 returns the value of the inverse of the mapping for s
56 """
57 raise NotImplementedError
58
59
60 class BoundedRangeMapping(Mapping):
61 """
62 Maps an unbounded parameter to a bounded range. The mapping is smooth and
63 continuous.
64 """
65
66 def __init__(self, s_min=0, s_max=1):
67 if not s_min < s_max:
68 raise ValueError("value for s_min must be less than the value for s_max.")
69 self.s_min=s_min
70 self.s_max=s_max
71
72 def getValue(self, m):
73 """
74 returns the value of the mapping for m
75 """
76 return (self.s_max+self.s_min)/2. + (self.s_max-self.s_min)/2. * tanh(m)
77
78 def getDerivative(self, m):
79 """
80 returns the value for the derivative of the mapping for m
81 """
82 return ((self.s_max-self.s_min)/2.) * (1.-tanh(m)**2.)
83
84 def getInverse(self, s):
85 """
86 returns the value of the inverse of the mapping for s
87 """
88 if not (inf(s) > self.s_min and sup(s) < self.s_max):
89 raise ValueError("s is out of range [%f,%f]"%(inf(s),sup(s)))
90
91 return 1./2. * log( (s-self.s_min)/(self.s_max-s) )
92
93
94 class ScalingMapping(Mapping):
95 """
96 Maps a parameter by scaling it with a constant.
97 """
98
99 def __init__(self, alpha=1):
100 if alpha==0:
101 raise ValueError("Scaling parameter must not be 0.")
102 self.__alpha=alpha
103
104 def getValue(self, m):
105 """
106 returns the value of the mapping for m
107 """
108 return self.__alpha*m
109
110 def getDerivative(self, m):
111 """
112 returns the value for the derivative of the mapping for m
113 """
114 return self.__alpha
115
116 def getInverse(self, s):
117 """
118 returns the value of the inverse of the mapping for s
119 """
120 return s/self.__alpha
121

  ViewVC Help
Powered by ViewVC 1.1.26