/[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 4007 - (show annotations)
Tue Oct 2 02:12:01 2012 UTC (7 years, 1 month ago) by caltinay
File MIME type: text/x-python
File size: 3409 byte(s)
Doco updates.
Data source paddings now take x,y not xy,z as Z padding can be done by setting
vertical extents.

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

  ViewVC Help
Powered by ViewVC 1.1.26