1 | import numpy as np
|
---|
2 | from project3d import project3d
|
---|
3 | from pairoptions import *
|
---|
4 | from collections import OrderedDict
|
---|
5 | from fielddisplay import fielddisplay
|
---|
6 | from checkfield import checkfield
|
---|
7 | from WriteData import WriteData
|
---|
8 |
|
---|
9 |
|
---|
10 | class misfit(object):
|
---|
11 | """
|
---|
12 | MISFIT class definition
|
---|
13 |
|
---|
14 | Usage:
|
---|
15 | misfit=misfit()
|
---|
16 | misfit=misfit(name='SurfaceAltimetry',
|
---|
17 | definitionstring='Outputdefinition1',
|
---|
18 | model_string='Surface',
|
---|
19 | observation_string='SurfaceObservations',
|
---|
20 | observation=md.geometry.surface,
|
---|
21 | timeinterpolation='nearestneighbor',
|
---|
22 | local=1,
|
---|
23 | weights=np.ones((md.mesh.numberofvertices,1)),
|
---|
24 | weights_string='WeightsSurfaceObservations')
|
---|
25 | """
|
---|
26 |
|
---|
27 | def __init__(self, name = None, definitionstring = None, model_string = None, observation = None, observation_string = None, timeinterpolation = None, local = None, weights = None, weights_string = None, cumulated = None):
|
---|
28 | # {{{
|
---|
29 | self.name = name if name is not None else ''
|
---|
30 |
|
---|
31 | #string that identifies this output definition uniquely, from 'Outputdefinition[1-100]'
|
---|
32 | self.definitionstring = definitionstring if definitionstring is not None else ''
|
---|
33 |
|
---|
34 | #string for field that is modeled
|
---|
35 | self.model_string = model_string if model_string is not None else ''
|
---|
36 |
|
---|
37 | #observed field that we compare the model against
|
---|
38 | self.observation = observation if observation is not None else float('NaN')
|
---|
39 |
|
---|
40 | #string for observed field.
|
---|
41 | self.observation_string = observation_string if observation_string is not None else ''
|
---|
42 |
|
---|
43 | self.timeinterpolation = timeinterpolation if timeinterpolation is not None else 'nearestneighbor'
|
---|
44 |
|
---|
45 | self.local = local if local is not None else 1
|
---|
46 |
|
---|
47 | #weight coefficients for every vertex
|
---|
48 | self.weights = weights if weights is not None else float('NaN')
|
---|
49 |
|
---|
50 | #string to identify this particular set of weights
|
---|
51 | self.weights_string = weights_string if weights_string is not None else ''
|
---|
52 |
|
---|
53 | #do we cumulate misfit through time?
|
---|
54 | self.cumulated = cumulated if cumulated is not None else float('NaN')
|
---|
55 | #}}}
|
---|
56 |
|
---|
57 | def __repr__(self): # {{{
|
---|
58 | string=' Misfit:'
|
---|
59 |
|
---|
60 | string="%s\n%s"%(string,fielddisplay(self,'name','identifier for this misfit response'))
|
---|
61 | string="%s\n%s"%(string,fielddisplay(self,'definitionstring','string that identifies this output definition uniquely, from "Outputdefinition[1-10]"'))
|
---|
62 | string="%s\n%s"%(string,fielddisplay(self,'model_string','string for field that is modeled'))
|
---|
63 | string="%s\n%s"%(string,fielddisplay(self,'observation','observed field that we compare the model against'))
|
---|
64 | string="%s\n%s"%(string,fielddisplay(self,'observation_string','observation string'))
|
---|
65 | string="%s\n%s"%(string,fielddisplay(self,'local','is the response local to the elements, or global? (default is 1)'))
|
---|
66 | string="%s\n%s"%(string,fielddisplay(self,'timeinterpolation','interpolation routine used to interpolate misfit between two time steps (default is "nearestneighbor"'))
|
---|
67 | string="%s\n%s"%(string,fielddisplay(self,'weights','weights (at vertices) to apply to the misfit'))
|
---|
68 | string="%s\n%s"%(string,fielddisplay(self,'weights_string','string for weights for identification purposes'))
|
---|
69 | return string
|
---|
70 | #}}}
|
---|
71 |
|
---|
72 | def extrude(self,md): # {{{
|
---|
73 | if not np.any(np.isnan(self.weights)):
|
---|
74 | self.weights = project3d(md,'vector',self.weights,'type','node')
|
---|
75 | if not np.any(np.isnan(self.observation)):
|
---|
76 | self.observation = project3d(md,'vector',self.observation,'type','node')
|
---|
77 | return self
|
---|
78 | #}}}
|
---|
79 |
|
---|
80 | def checkconsistency(self,md,solution,analyses): # {{{
|
---|
81 | if type(self.name) != str:
|
---|
82 | raise TypeError('misfit error message: "name" field should be a string!')
|
---|
83 |
|
---|
84 | OutputdefinitionStringArray = []
|
---|
85 | for i in range(100):
|
---|
86 | OutputdefinitionStringArray.append('Outputdefinition' + str(i))
|
---|
87 |
|
---|
88 | md = checkfield(md,'fieldname','self.definitionstring','field',self.definitionstring,'values',OutputdefinitionStringArray)
|
---|
89 | if type(self.timeinterpolation) != str:
|
---|
90 | raise TypeError('misfit error message: "timeinterpolation" field should be a string!')
|
---|
91 |
|
---|
92 | md = checkfield(md,'fieldname','self.observation','field',self.observation,'timeseries',1,'NaN',1,'Inf',1)
|
---|
93 | md = checkfield(md,'fieldname','self.timeinterpolation','field',self.timeinterpolation,'values',['nearestneighbor'])
|
---|
94 | md = checkfield(md,'fieldname','self.weights','field',self.weights,'timeseries',1,'NaN',1,'Inf',1)
|
---|
95 |
|
---|
96 | return md
|
---|
97 | # }}}
|
---|
98 |
|
---|
99 | def marshall(self,prefix,md,fid): # {{{
|
---|
100 | WriteData(fid,prefix,'data',self.name,'name','md.misfit.name','format','String')
|
---|
101 | WriteData(fid,prefix,'data',self.definitionstring,'name','md.misfit.definitionstring','format','String')
|
---|
102 | WriteData(fid,prefix,'data',self.model_string,'name','md.misfit.model_string','format','String')
|
---|
103 | WriteData(fid,prefix,'data',self.observation,'name','md.misfit.observation','format','DoubleMat','mattype',1,'timeserieslength',md.mesh.numberofvertices+1,'yts',md.constants.yts)
|
---|
104 | WriteData(fid,prefix,'data',self.observation_string,'name','md.misfit.observation_string','format','String')
|
---|
105 | WriteData(fid,prefix,'data',self.local,'name','md.misfit.local','format','Integer')
|
---|
106 | WriteData(fid,prefix,'data',self.timeinterpolation,'name','md.misfit.timeinterpolation','format','String')
|
---|
107 | WriteData(fid,prefix,'data',self.weights,'name','md.misfit.weights','format','DoubleMat','mattype',1,'timeserieslength',md.mesh.numberofvertices+1,'yts',md.constants.yts)
|
---|
108 | WriteData(fid,prefix,'data',self.weights_string,'name','md.misfit.weights_string','format','String')
|
---|
109 | # }}}
|
---|