source: issm/trunk-jpl/src/m/classes/qmu/uniform_uncertain.py@ 25045

Last change on this file since 25045 was 25045, checked in by jdquinn, 5 years ago

BUG: ‘nuv’ -> ‘uuv’

File size: 9.5 KB
Line 
1import numpy as np
2
3from MatlabArray import *
4from MatlabFuncs import *
5from fielddisplay import fielddisplay
6from pairoptions import pairoptions
7from partition_npart import *
8from qmupart2npart import qmupart2npart
9
10
11class uniform_uncertain(object):
12 '''
13 UNIFORM_UNCERTAIN class definition
14
15 Usage:
16 uuv = uniform_uncertain(
17 'descriptor', descriptor,
18 'lower', lower,
19 'upper', upper,
20 'partition', partition
21 )
22
23 where uuv is the uniform_uncertain object returned by the constructor,
24 lower and upper are the pdf distribution bounds, and partition is the
25 partition vector for distributed variables. Can be a partition vector
26 over elements or vertices.
27
28 Example:
29 md.qmu.variables.rheology = uniform_uncertain(
30 'descriptor', 'RheologyBBar',
31 'lower', 1e8,
32 'upper', 1e9
33 )
34 md.qmu.variables.rheology = uniform_uncertain(
35 'descriptor', 'RheologyBBar',
36 'lower', 1e8,
37 'upper', 1e9,
38 'partition', vpartition
39 )
40 '''
41 def __init__(self):
42 self.descriptor = ''
43 self.lower = -np.Inf
44 self.upper = np.Inf
45 self.partition = []
46 self.nsteps = 0
47
48 @staticmethod
49 def uniform_uncertain(*args):
50 nargin = len(args)
51
52 # create a default object
53 if nargin == 0:
54 return uniform_uncertain()
55
56 # copy the object
57 elif nargin == 1:
58 if isinstance(args[0], uniform_uncertain):
59 uuv = args[0]
60 else:
61 raise RuntimeError('Object ' + str(args[0]) + ' is a ' + str(type(args[0])) + ' class object, not "uniform_uncertain".')
62
63 # create the object from the input
64 else:
65 uuv = uniform_uncertain()
66
67 #recover options:
68 options = pairoptions(*args)
69
70 #initialize fields:
71 uuv.descriptor = options.getfieldvalue('descriptor')
72 uuv.lower = options.getfieldvalue('lower')
73 uuv.upper = options.getfieldvalue('upper')
74
75 #if the variable is scaled, a partition vector should have been
76 #supplied, and that partition vector should have as many partitions as
77 #the lower and upper vectors:
78 if uuv.isscaled():
79 uuv.partition = options.getfieldvalue('partition')
80 uuv.nsteps = options.getfieldvalue('nsteps', 1)
81 npart = qmupart2npart(uuv.partition)
82 if npart != uuv.upper.shape[0]:
83 raise RuntimeError("uniform_uncertain constructor: for the scaled variable %s the upper field is not currently a vector of values for all the partitions described in the partition vector" % uuv.descriptor)
84 if npart != uuv.lower.shape[0]:
85 raise RuntimeError("uniform_uncertain constructor: for the scaled variable %s the lower field is not currently a vector of values for all the partitions described in the partition vector" % uuv.descriptor)
86 if uuv.nsteps != uuv.upper.shape[1]:
87 raise RuntimeError("uniform_uncertain constructor: for the scaled variable %s the col size of the upper field should be identical to the number of time steps" % uuv.descriptor)
88 if uuv.nsteps != uuv.lower.shape[1]:
89 raise RuntimeError("uniform_uncertain constructor: for the scaled variable %s the col size of the lower field should be identical to the number of time steps" % uuv.descriptor)
90
91 return [uuv] # Always return a list, so we have something akin to a MATLAB single row matrix
92
93 def __repr__(self): #{{{
94 string = ' uniform uncertain variable: '
95 string = "%s\n%s" % (string, fielddisplay(self, 'descriptor', 'name tag'))
96 string = "%s\n%s" % (string, fielddisplay(self, 'lower', 'pdf lower bound'))
97 string = "%s\n%s" % (string, fielddisplay(self, 'upper', 'pdf upper bound'))
98 if self.partition != []:
99 string = "%s\n%s" % (string, fielddisplay(self, 'partition', 'partition vector defining where sampling will occur'))
100 string = "%s\n%s" % (string, fielddisplay(self, 'nsteps', 'number of time steps'))
101
102 return string
103 #}}}
104
105 def __len__(self): #{{{
106 if type(self.lower) in [list, np.ndarray]:
107 return len(self.lower)
108 else:
109 return 1
110 #}}}
111
112 def checkconsistency(self, md, solution, analyses): #{{{
113 md = checkfield(md, 'field', self.upper, 'fieldname', 'uniform_uncertain.upper', 'NaN', 1, 'Inf', 1, '>', self.lower, 'numel', len(self.lower))
114 md = checkfield(md, 'field', self.lower, 'fieldname', 'uniform_uncertain.upper', 'NaN', 1, 'Inf', 1, '<', self.upper, 'numel', len(self.upper))
115 if self.isscaled():
116 if self.partition == []:
117 raise RuntimeError("uniform_uncertain is a scaled variable, but it's missing a partition vector")
118 #better have a partition vector that has as many partitions as
119 #upper and lower's size:
120 if self.upper.shape[0] != partition_npart(self.partititon):
121 raise RuntimeError("uniform_uncertain error message: row size of upper and partition size should be identical")
122 if self.lower.shape[0] != partition_npart(self.partition):
123 raise RuntimeError("uniform_uncertain error message: row size of lower and partition size should be identical")
124 #we need as steps in upper and lower as there are time steps
125 if self.stddev.shape[1] != self.nsteps:
126 raise RuntimeError("uniform_uncertain error message: col size of upper and partition size should be identical")
127 if self.mean.shape[1] != self.nsteps:
128 raise RuntimeError("uniform_uncertain error message: col size of lower and partition size should be identical")
129 md = checkfield(md, 'field', self.partition, 'fieldname', 'uniform_uncertain.partition', 'NaN', 1, 'Inf', 1, '>=', -1, 'numel', [md.mesh.numberofvertices, md.mesh.numberofvertices])
130 if self.partition.shape[1] > 1:
131 raise RuntimeError("uniform_uncertain error message: partition should be a column vector")
132 partcheck = np.unique(self.partition)
133 partmin = min(partcheck)
134 partmax = max(partcheck)
135 if partmax < -1:
136 raise RuntimeError("uniform_uncertain error message: partition vector's min value should be -1 (for no partition), or start at 0")
137 nmax = max(md.mesh.numberofelements, md.mesh.numberofvertices)
138 if partmax > nmax:
139 raise RuntimeError("uniform_uncertain error message: partition vector's values cannot go over the number of vertices or elements")
140 #}}}
141
142 #virtual functions needed by qmu processing algorithms:
143 #implemented:
144
145 @staticmethod
146 def prop_desc(uuv, dstr): #{{{
147 desc = ['' for i in range(np.size(uuv))]
148 for i in range(np.size(uuv)):
149 if uuv[i].descriptor != '' or type(uuv[i].descriptor) != str:
150 desc[i] = str(uuv[i].descriptor)
151 elif dstr != '':
152 desc[i] = str(dstr) + str(string_dim(uuv, i, 'vector'))
153 else:
154 desc[i] = 'uuv' + str(string_dim(uuv, i, 'vector'))
155
156 desc = allempty(desc)
157
158 return desc
159 #}}}
160
161 @staticmethod
162 def prop_lower(uuv): #{{{
163 lower = np.zeros(np.size(uuv))
164 for i in range(np.size(uuv)):
165 lower[i] = uuv[i].lower
166
167 lower = allequal(lower, -np.Inf)
168
169 return lower
170 #}}}
171
172 @staticmethod
173 def prop_upper(uuv): #{{{
174 upper = np.zeros(np.size(uuv))
175 for i in range(np.size(uuv)):
176 upper[i] = uuv[i].upper
177
178 #upper = allequal(upper, np.Inf)
179
180 return upper
181 #}}}
182
183 @staticmethod
184 def prop_stddev(uuv): #{{{
185 stddev = []
186 return stddev
187 #}}}
188
189 @staticmethod
190 def prop_mean(uuv): #{{{
191 mean = []
192 return mean
193 #}}}
194
195 @staticmethod
196 def prop_initpt(uuv): #{{{
197 initpt = []
198 return initpt
199 #}}}
200
201 @staticmethod
202 def prop_initst(uuv): #{{{
203 initst = []
204 return initst
205 #}}}
206
207 @staticmethod
208 def prop_stype(uuv): #{{{
209 stype = []
210 return stype
211 #}}}
212
213 @staticmethod
214 def prop_scale(uuv): #{{{
215 scale = []
216 return scale
217 #}}}
218
219 @staticmethod
220 def prop_abscissas(hbu): #{{{
221 abscissas = []
222 return abscissas
223 #}}}
224
225 @staticmethod
226 def prop_counts(hbu): #{{{
227 counts = []
228 return counts
229 #}}}
230
231 @staticmethod
232 def prop_pairs_per_variable(hbu): #{{{
233 pairs_per_variable = []
234 return pairs_per_variable
235 #}}}
236
237 #new methods:
238 def isscaled(self): #{{{
239 if strncmp(self.descriptor, 'scaled_', 7):
240 return True
241 else:
242 return False
243 #}}}
244
245 @staticmethod
246 def dakota_write(fidi, dvar): #{{{
247 # possible namespace pollution, the above import seems not to work
248 from vlist_write import vlist_write
249 # # collect only the variables of the appropriate class
250 # uuv = [struc_class(i, 'uniform_uncertain', 'uuv') for i in dvar]
251 uuv = deepcopy(dvar)
252 fields = fieldnames(uuv)
253 for field in fields:
254 if getattr(uuv, field)[0].__class__.__name__ != 'uniform_uncertain':
255 delattr(uuv, field)
256 if len(uuv) > 0:
257 vlist_write(fidi, 'uniform_uncertain', 'uuv', uuv)
258 #}}}
Note: See TracBrowser for help on using the repository browser.