source: issm/trunk/src/c/classes/Misfit.h@ 21341

Last change on this file since 21341 was 21341, checked in by Mathieu Morlighem, 9 years ago

merged trunk-jpl and trunk for revision 21337

File size: 6.1 KB
Line 
1/*!\file Misfit.h
2 * \brief: header file for Misfit object
3 */
4
5#ifndef _MISFIT_H_
6#define _MISFIT_H_
7
8/*Headers:*/
9/*{{{*/
10#include "./Definition.h"
11#include "../datastructures/datastructures.h"
12#include "./Elements/Element.h"
13#include "./Elements/Elements.h"
14#include "./FemModel.h"
15#include "../modules/SurfaceAreax/SurfaceAreax.h"
16#include "../classes/Params/Parameters.h"
17#include "../classes/Inputs/Input.h"
18#include "../classes/gauss/Gauss.h"
19/*}}}*/
20IssmDouble OutputDefinitionsResponsex(FemModel* femmodel,int output_enum);
21
22class Misfit: public Object, public Definition{
23
24 public:
25
26 int definitionenum;
27 bool local;
28 int model_enum;
29 char* name;
30 int observation_enum;
31 char* timeinterpolation;
32 int weights_enum;
33
34 int lock; // if lock is on, we just return the value stored in "misfit". this is used so we don't compute misfit past the final_time
35 IssmDouble misfit; //value carried over in time.
36
37 /*Misfit constructors, destructors :*/
38 Misfit(){/*{{{*/
39
40 this->definitionenum = -1;
41 this->name = NULL;
42 this->model_enum = UNDEF;
43 this->observation_enum = UNDEF;
44 this->weights_enum = UNDEF;
45 this->timeinterpolation=NULL;
46 this->local=true;
47 this->misfit=0;
48 this->lock=0;
49
50 }
51 /*}}}*/
52 Misfit(char* in_name, int in_definitionenum, int in_model_enum, int in_observation_enum, char* in_timeinterpolation, bool in_local, int in_weights_enum){/*{{{*/
53
54 this->definitionenum=in_definitionenum;
55
56 this->name = xNew<char>(strlen(in_name)+1);
57 xMemCpy<char>(this->name,in_name,strlen(in_name)+1);
58
59 this->timeinterpolation = xNew<char>(strlen(in_timeinterpolation)+1);
60 xMemCpy<char>(this->timeinterpolation,in_timeinterpolation,strlen(in_timeinterpolation)+1);
61
62 this->model_enum=in_model_enum;
63 this->observation_enum=in_observation_enum;
64 this->weights_enum=in_weights_enum;
65 this->local=in_local;
66
67 this->misfit=0;
68 this->lock=0;
69 }
70 /*}}}*/
71 ~Misfit(){/*{{{*/
72 if(this->name)xDelete(this->name);
73 if(this->timeinterpolation)xDelete(this->timeinterpolation);
74 this->misfit=0;
75 this->lock=0;
76 }
77 /*}}}*/
78 /*Object virtual function resolutoin: */
79 Object* copy() {/*{{{*/
80 Misfit* mf = new Misfit(this->name,this->definitionenum, this->model_enum,this->observation_enum,this->timeinterpolation,this->local,this->weights_enum);
81 mf->misfit=this->misfit;
82 mf->lock=this->lock;
83 return (Object*) mf;
84 }
85 /*}}}*/
86 void DeepEcho(void){/*{{{*/
87 this->Echo();
88 }
89 /*}}}*/
90 void Echo(void){/*{{{*/
91 _printf_(" Misfit: " << name << " " << this->definitionenum << "\n");
92 _printf_(" model_enum: " << model_enum << " " << EnumToStringx(model_enum) << "\n");
93 _printf_(" observation_enum: " << observation_enum << " " << EnumToStringx(observation_enum) << "\n");
94 _printf_(" weights_enum: " << weights_enum << " " << EnumToStringx(weights_enum) << "\n");
95 _printf_(" timeinterpolation: " << timeinterpolation << "\n");
96 _printf_(" local: " << local << "\n");
97 }
98 /*}}}*/
99 int Id(void){/*{{{*/
100 return -1;
101 }
102 /*}}}*/
103 void Marshall(char** pmarshalled_data,int* pmarshalled_data_size, int marshall_direction){/*{{{*/
104 _error_("not implemented yet!");
105 }
106 /*}}}*/
107 int ObjectEnum(void){/*{{{*/
108 return MisfitEnum;
109 }
110 /*}}}*/
111 /*Definition virtual function resolutoin: */
112 int DefinitionEnum(){/*{{{*/
113 return this->definitionenum;
114 }
115 /*}}}*/
116 char* Name(){/*{{{*/
117 char* name2=xNew<char>(strlen(this->name)+1);
118 xMemCpy(name2,this->name,strlen(this->name)+1);
119
120 return name2;
121 }
122 /*}}}*/
123 IssmDouble Response(FemModel* femmodel){/*{{{*/
124
125 /*diverse: */
126 IssmDouble time,starttime,finaltime;
127 IssmDouble dt;
128
129 /*recover time parameters: */
130 femmodel->parameters->FindParam(&starttime,TimesteppingStartTimeEnum);
131 femmodel->parameters->FindParam(&finaltime,TimesteppingFinalTimeEnum);
132 femmodel->parameters->FindParam(&time,TimeEnum);
133 femmodel->parameters->FindParam(&dt,TimesteppingTimeStepEnum);
134
135 if (this->local){ /*local computation: {{{*/
136
137 int i;
138 IssmDouble misfit_t=0.;
139 IssmDouble all_misfit_t=0.;
140 IssmDouble area_t=0.;
141 IssmDouble all_area_t;
142
143
144 /*If we are locked, return time average: */
145 if(this->lock)return misfit/(time-starttime);
146
147 for(i=0;i<femmodel->elements->Size();i++){
148 Element* element=(Element*)femmodel->elements->GetObjectByOffset(i);
149 misfit_t+=element->Misfit(model_enum,observation_enum,weights_enum);
150 area_t+=element->MisfitArea(weights_enum);
151 }
152
153 ISSM_MPI_Allreduce ( (void*)&misfit_t,(void*)&all_misfit_t,1,ISSM_MPI_DOUBLE,ISSM_MPI_SUM,IssmComm::GetComm());
154 ISSM_MPI_Allreduce ( (void*)&area_t,(void*)&all_area_t,1,ISSM_MPI_DOUBLE,ISSM_MPI_SUM,IssmComm::GetComm());
155 area_t=all_area_t;
156 misfit_t=all_misfit_t;
157
158 /*Divide by surface area if not nill!: */
159 if (area_t!=0) misfit_t=misfit_t/area_t;
160
161 /*Add this time's contribution to curent misfit: */
162 misfit+=dt*misfit_t;
163
164 /*Do we lock? i.e. are we at final_time? :*/
165 if(time==finaltime)this->lock=1;
166
167 /*What we return is the value of misfit / time: */
168 return misfit/(time-starttime);
169 } /*}}}*/
170 else{ /*global computation: {{{ */
171
172 IssmDouble model, observation;
173
174 /*If we are locked, return time average: */
175 if(this->lock)return misfit/(time-starttime);
176
177 /*First, the global model response: */
178 model=OutputDefinitionsResponsex(femmodel,this->model_enum);
179 /*Now, the observation is buried inside the elements, go fish it in the first element (cludgy, needs fixing): */
180 Element* element=(Element*)femmodel->elements->GetObjectByOffset(0); _assert_(element);
181 Input* input = element->GetInput(observation_enum); _assert_(input);
182 input->GetInputAverage(&observation);
183
184 /*Add this time's contribution to curent misfit: */
185 misfit+=dt*(model-observation);
186
187 /*Do we lock? i.e. are we at final_time? :*/
188 if(time==finaltime)this->lock=1;
189
190 /*What we return is the value of misfit / time: */
191 return misfit/(time-starttime);
192 } /*}}}*/
193
194 }
195 /*}}}*/
196};
197
198#endif /* _MISFIT_H_ */
Note: See TracBrowser for help on using the repository browser.