1 | /*!\file Misfit.cpp
|
---|
2 | * \brief: Misfit object
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*Headers:*/
|
---|
6 | /*{{{*/
|
---|
7 | #ifdef HAVE_CONFIG_H
|
---|
8 | #include <config.h>
|
---|
9 | #else
|
---|
10 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "./classes.h"
|
---|
14 | #include "./ExternalResults/ExternalResult.h"
|
---|
15 | #include "./ExternalResults/Results.h"
|
---|
16 | #include "../datastructures/datastructures.h"
|
---|
17 | #include "./Elements/Element.h"
|
---|
18 | #include "./Elements/Elements.h"
|
---|
19 | #include "./FemModel.h"
|
---|
20 | #include "../modules/SurfaceAreax/SurfaceAreax.h"
|
---|
21 | #include "../classes/Params/Parameters.h"
|
---|
22 | #include "../classes/Inputs/Input.h"
|
---|
23 | #include "../classes/gauss/Gauss.h"
|
---|
24 | /*}}}*/
|
---|
25 |
|
---|
26 | /*Misfit constructors, destructors :*/
|
---|
27 | Misfit::Misfit(){/*{{{*/
|
---|
28 |
|
---|
29 | this->definitionenum = -1;
|
---|
30 | this->name = NULL;
|
---|
31 | this->model_enum = UNDEF;
|
---|
32 | this->observation_enum = UNDEF;
|
---|
33 | this->weights_enum = UNDEF;
|
---|
34 | this->timeinterpolation=NULL;
|
---|
35 | this->local=1;
|
---|
36 | this->misfit=0;
|
---|
37 | this->lock=0;
|
---|
38 |
|
---|
39 | }
|
---|
40 | /*}}}*/
|
---|
41 | Misfit::Misfit(char* in_name, int in_definitionenum, int in_model_enum, int in_observation_enum, char* in_timeinterpolation, int in_local, int in_weights_enum){/*{{{*/
|
---|
42 |
|
---|
43 | this->definitionenum=in_definitionenum;
|
---|
44 |
|
---|
45 | this->name = xNew<char>(strlen(in_name)+1);
|
---|
46 | xMemCpy<char>(this->name,in_name,strlen(in_name)+1);
|
---|
47 |
|
---|
48 | this->timeinterpolation = xNew<char>(strlen(in_timeinterpolation)+1);
|
---|
49 | xMemCpy<char>(this->timeinterpolation,in_timeinterpolation,strlen(in_timeinterpolation)+1);
|
---|
50 |
|
---|
51 | this->model_enum=in_model_enum;
|
---|
52 | this->observation_enum=in_observation_enum;
|
---|
53 | this->weights_enum=in_weights_enum;
|
---|
54 | this->local=in_local;
|
---|
55 |
|
---|
56 | this->misfit=0;
|
---|
57 | this->lock=0;
|
---|
58 | }
|
---|
59 | /*}}}*/
|
---|
60 | Misfit::~Misfit(){/*{{{*/
|
---|
61 | if(this->name)xDelete(this->name);
|
---|
62 | if(this->timeinterpolation)xDelete(this->timeinterpolation);
|
---|
63 | this->misfit=0;
|
---|
64 | this->lock=0;
|
---|
65 | }
|
---|
66 | /*}}}*/
|
---|
67 | /*Object virtual function resolutoin: */
|
---|
68 | Object* Misfit::copy() {/*{{{*/
|
---|
69 | Misfit* mf = new Misfit(this->name,this->definitionenum, this->model_enum,this->observation_enum,this->timeinterpolation,this->local,this->weights_enum);
|
---|
70 | mf->misfit=this->misfit;
|
---|
71 | mf->lock=this->lock;
|
---|
72 | return (Object*) mf;
|
---|
73 | }
|
---|
74 | /*}}}*/
|
---|
75 | void Misfit::DeepEcho(void){/*{{{*/
|
---|
76 | this->Echo();
|
---|
77 | }
|
---|
78 | /*}}}*/
|
---|
79 | void Misfit::Echo(void){/*{{{*/
|
---|
80 | _printf_(" Misfit: " << name << " " << this->definitionenum << "\n");
|
---|
81 | _printf_(" model_enum: " << model_enum << " " << EnumToStringx(model_enum) << "\n");
|
---|
82 | _printf_(" observation_enum: " << observation_enum << " " << EnumToStringx(observation_enum) << "\n");
|
---|
83 | _printf_(" weights_enum: " << weights_enum << " " << EnumToStringx(weights_enum) << "\n");
|
---|
84 | _printf_(" timeinterpolation: " << timeinterpolation << "\n");
|
---|
85 | _printf_(" local: " << local << "\n");
|
---|
86 | }
|
---|
87 | /*}}}*/
|
---|
88 | int Misfit::Id(void){/*{{{*/
|
---|
89 | return -1;
|
---|
90 | }
|
---|
91 | /*}}}*/
|
---|
92 | void Misfit::Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
|
---|
93 | _error_("not implemented yet!");
|
---|
94 | }
|
---|
95 | /*}}}*/
|
---|
96 | int Misfit::ObjectEnum(void){/*{{{*/
|
---|
97 | return MisfitEnum;
|
---|
98 | }
|
---|
99 | /*}}}*/
|
---|
100 | /*Definition virtual function resolutoin: */
|
---|
101 | int Misfit::DefinitionEnum(){/*{{{*/
|
---|
102 | return this->definitionenum;
|
---|
103 | }
|
---|
104 | /*}}}*/
|
---|
105 | char* Misfit::Name(){/*{{{*/
|
---|
106 | char* name2=xNew<char>(strlen(this->name)+1);
|
---|
107 | xMemCpy(name2,this->name,strlen(this->name)+1);
|
---|
108 |
|
---|
109 | return name2;
|
---|
110 | }
|
---|
111 | /*}}}*/
|
---|
112 | IssmDouble Misfit::Response(FemModel* femmodel){/*{{{*/
|
---|
113 |
|
---|
114 | /*diverse: */
|
---|
115 | IssmDouble time,starttime,finaltime;
|
---|
116 | IssmDouble dt;
|
---|
117 |
|
---|
118 | /*recover time parameters: */
|
---|
119 | femmodel->parameters->FindParam(&starttime,TimesteppingStartTimeEnum);
|
---|
120 | femmodel->parameters->FindParam(&finaltime,TimesteppingFinalTimeEnum);
|
---|
121 | femmodel->parameters->FindParam(&time,TimeEnum);
|
---|
122 | femmodel->parameters->FindParam(&dt,TimesteppingTimeStepEnum);
|
---|
123 |
|
---|
124 | if (this->local==1){ /*area integration using elements: {{{*/
|
---|
125 |
|
---|
126 | int i;
|
---|
127 | IssmDouble misfit_t=0.;
|
---|
128 | IssmDouble all_misfit_t=0.;
|
---|
129 | IssmDouble area_t=0.;
|
---|
130 | IssmDouble all_area_t;
|
---|
131 |
|
---|
132 | /*If we are locked, return time average: */
|
---|
133 | if(this->lock)return misfit/(time-starttime);
|
---|
134 |
|
---|
135 | for(i=0;i<femmodel->elements->Size();i++){
|
---|
136 | Element* element=(Element*)femmodel->elements->GetObjectByOffset(i);
|
---|
137 | misfit_t+=element->Misfit(model_enum,observation_enum,weights_enum);
|
---|
138 | area_t+=element->MisfitArea(weights_enum);
|
---|
139 | }
|
---|
140 |
|
---|
141 | ISSM_MPI_Allreduce ( (void*)&misfit_t,(void*)&all_misfit_t,1,ISSM_MPI_DOUBLE,ISSM_MPI_SUM,IssmComm::GetComm());
|
---|
142 | ISSM_MPI_Allreduce ( (void*)&area_t,(void*)&all_area_t,1,ISSM_MPI_DOUBLE,ISSM_MPI_SUM,IssmComm::GetComm());
|
---|
143 | area_t=all_area_t;
|
---|
144 | misfit_t=all_misfit_t;
|
---|
145 |
|
---|
146 | /*Divide by surface area if not nill!: */
|
---|
147 | if (area_t!=0) misfit_t=misfit_t/area_t;
|
---|
148 |
|
---|
149 | /*Add this time's contribution to curent misfit: */
|
---|
150 | misfit+=dt*misfit_t;
|
---|
151 |
|
---|
152 | /*Do we lock? i.e. are we at final_time? :*/
|
---|
153 | if(time==finaltime)this->lock=1;
|
---|
154 |
|
---|
155 | /*What we return is the value of misfit / time if transient*/
|
---|
156 | if(time!=0.) return misfit/(time-starttime);
|
---|
157 | return misfit;
|
---|
158 | } /*}}}*/
|
---|
159 | else if (this->local==2){ /*vertex by vertex computation: {{{*/
|
---|
160 |
|
---|
161 | IssmDouble* model = NULL;
|
---|
162 | IssmDouble* observation= NULL;
|
---|
163 | IssmDouble* weights= NULL;
|
---|
164 | int msize,osize,wsize;
|
---|
165 |
|
---|
166 | /*Are we transient?:*/
|
---|
167 | if (time==0){
|
---|
168 | IssmDouble misfit_t=0.;
|
---|
169 |
|
---|
170 | /*get global vectors: */
|
---|
171 | GetVectorFromInputsx(&model,&msize,femmodel,model_enum);
|
---|
172 | GetVectorFromInputsx(&observation,&osize,femmodel,observation_enum);_assert_(msize==osize);
|
---|
173 | GetVectorFromInputsx(&weights,&wsize,femmodel,weights_enum); _assert_(wsize==msize);
|
---|
174 |
|
---|
175 | int count=0;
|
---|
176 | for (int i=0;i<msize;i++){
|
---|
177 | misfit_t += pow(model[i]-observation[i],2)*weights[i];
|
---|
178 | if (weights[i]!=0)count++;
|
---|
179 | }
|
---|
180 | misfit=sqrt(misfit_t/count);
|
---|
181 |
|
---|
182 | /*Free ressources:*/
|
---|
183 | xDelete<IssmDouble>(model);
|
---|
184 | xDelete<IssmDouble>(observation);
|
---|
185 | xDelete<IssmDouble>(weights);
|
---|
186 |
|
---|
187 | /*return value: */
|
---|
188 | return misfit;
|
---|
189 | }
|
---|
190 | else{
|
---|
191 |
|
---|
192 | IssmDouble misfit_t=0.;
|
---|
193 | IssmDouble all_misfit_t=0.;
|
---|
194 |
|
---|
195 | /*If we are locked, return time average: */
|
---|
196 | if(this->lock)return misfit/(time-starttime);
|
---|
197 |
|
---|
198 | /*get global vectors: */
|
---|
199 | GetVectorFromInputsx(&model,&msize,femmodel,model_enum);
|
---|
200 | GetVectorFromInputsx(&observation,&osize,femmodel,observation_enum);_assert_(msize==osize);
|
---|
201 | GetVectorFromInputsx(&weights,&wsize,femmodel,weights_enum); _assert_(wsize==msize);
|
---|
202 |
|
---|
203 | int count=0;
|
---|
204 | for (int i=0;i<msize;i++){
|
---|
205 | misfit_t += pow(model[i]-observation[i],2)*weights[i];
|
---|
206 | if (weights[i]!=0)count++;
|
---|
207 | }
|
---|
208 | misfit=sqrt(misfit_t/count);
|
---|
209 |
|
---|
210 | /*Add this time's contribution to curent misfit: */
|
---|
211 | misfit=sqrt(misfit_t)/count;
|
---|
212 | misfit+=dt*misfit_t;
|
---|
213 |
|
---|
214 | /*Do we lock? i.e. are we at final_time? :*/
|
---|
215 | if(time==finaltime)this->lock=1;
|
---|
216 |
|
---|
217 | /*Free ressources:*/
|
---|
218 | xDelete<IssmDouble>(model);
|
---|
219 | xDelete<IssmDouble>(observation);
|
---|
220 | xDelete<IssmDouble>(weights);
|
---|
221 |
|
---|
222 | /*What we return is the value of misfit / time: */
|
---|
223 | return misfit/(time-starttime);
|
---|
224 | }
|
---|
225 |
|
---|
226 | } /*}}}*/
|
---|
227 | else{ /*global computation: {{{ */
|
---|
228 |
|
---|
229 | IssmDouble model, observation;
|
---|
230 |
|
---|
231 | /*If we are locked, return time average: */
|
---|
232 | if(this->lock)return misfit/(time-starttime);
|
---|
233 |
|
---|
234 | /*First, the global model response: */
|
---|
235 | model=OutputDefinitionsResponsex(femmodel,this->model_enum);
|
---|
236 | /*Now, the observation is buried inside the elements, go fish it in the first element (cludgy, needs fixing): */
|
---|
237 | Element* element=(Element*)femmodel->elements->GetObjectByOffset(0); _assert_(element);
|
---|
238 | Input* input = element->GetInput(observation_enum); _assert_(input);
|
---|
239 | input->GetInputAverage(&observation);
|
---|
240 |
|
---|
241 | /*Add this time's contribution to curent misfit: */
|
---|
242 | misfit+=dt*(model-observation);
|
---|
243 |
|
---|
244 | /*Do we lock? i.e. are we at final_time? :*/
|
---|
245 | if(time==finaltime)this->lock=1;
|
---|
246 |
|
---|
247 | /*What we return is the value of misfit / time: */
|
---|
248 | return misfit/(time-starttime);
|
---|
249 | } /*}}}*/
|
---|
250 |
|
---|
251 | }
|
---|
252 | /*}}}*/
|
---|