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

Last change on this file since 19105 was 19105, checked in by Mathieu Morlighem, 10 years ago

merged trunk-jpl and trunk for revision 19103

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