-
Notifications
You must be signed in to change notification settings - Fork 6
/
ControlParam.cpp
252 lines (217 loc) · 6.61 KB
/
ControlParam.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*!\file ControlParam.c
* \brief: implementation of the ControlParam object
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
#endif
#include "../classes.h"
#include "shared/shared.h"
/*ControlParam constructors and destructor*/
ControlParam::ControlParam(){/*{{{*/
return;
}
/*}}}*/
ControlParam::ControlParam(IssmDouble* in_value, IssmDouble* in_minvalue, IssmDouble* in_maxvalue, int in_enum_type, int in_M,int in_N){/*{{{*/
this->enum_type=in_enum_type;
this->M=in_M;
this->N=in_N;
/*Sanity check, can't hurt*/
if(this->N<1) _error_("Parameter is empty");
if(this->M<1) _error_("Parameter is empty");
if(this->M>2) _error_("Cannot handle more than 2 rows (as a TransientParam)");
/*Assign value*/
this->value=xNew<IssmDouble>(M*N);
xMemCpy<IssmDouble>(value,in_value,M*N);
/*Assign other fields*/
this->minvalue=xNew<IssmDouble>(N);
xMemCpy<IssmDouble>(minvalue,in_minvalue,N);
this->maxvalue=xNew<IssmDouble>(N);
xMemCpy<IssmDouble>(maxvalue,in_maxvalue,N);
this->gradient=xNewZeroInit<IssmDouble>(N);
}
/*}}}*/
ControlParam::~ControlParam(){/*{{{*/
xDelete<IssmDouble>(value);
xDelete<IssmDouble>(minvalue);
xDelete<IssmDouble>(maxvalue);
xDelete<IssmDouble>(gradient);
return;
}
/*}}}*/
/*Object virtual functions definitions:*/
Param* ControlParam::copy() {/*{{{*/
ControlParam* output = new ControlParam();
output->enum_type=this->enum_type;
output->M=this->M;
output->N=this->N;
if(value){
output->value=xNew<IssmDouble>(this->M*this->N);
xMemCpy<IssmDouble>(output->value,this->value,this->M*this->N);
}
if(minvalue){
output->minvalue=xNew<IssmDouble>(this->N);
xMemCpy<IssmDouble>(output->minvalue,this->minvalue,this->N);
}
if(maxvalue){
output->maxvalue=xNew<IssmDouble>(this->N);
xMemCpy<IssmDouble>(output->maxvalue,this->maxvalue,this->N);
}
if(gradient){
output->gradient=xNew<IssmDouble>(this->N);
xMemCpy<IssmDouble>(output->gradient,this->gradient,this->N);
}
return output;
}
/*}}}*/
void ControlParam::DeepEcho(void){/*{{{*/
_printf_(setw(22)<<" ControlParam "<<setw(35)<<left<<EnumToStringx(this->enum_type)<<"\n ");
if (value) _printf_("---value: ");
for(int i=0;i<this->M;i++) _printf_(" "<< this->value[i]);
_printf_("]\n");
if (minvalue) _printf_("---minvalue: ");
for(int i=0;i<this->M;i++) _printf_(" "<< this->minvalue[i]);
_printf_("]\n");
if (maxvalue) _printf_("---maxvalue: ");
for(int i=0;i<this->M;i++) _printf_(" "<< this->maxvalue[i]);
_printf_("]\n");
if (gradient) _printf_("---gradient: " << this->gradient << "\n");
}
/*}}}*/
void ControlParam::Echo(void){/*{{{*/
this->DeepEcho();
}
/*}}}*/
int ControlParam::Id(void){ return -1; }/*{{{*/
/*}}}*/
void ControlParam::Marshall(MarshallHandle* marshallhandle){ /*{{{*/
int object_enum = ControlParamEnum;
marshallhandle->call(object_enum);
marshallhandle->call(this->enum_type);
marshallhandle->call(this->M);
marshallhandle->call(this->N);
marshallhandle->call(this->value,this->M*this->N);
marshallhandle->call(this->minvalue,this->N);
marshallhandle->call(this->maxvalue,this->N);
marshallhandle->call(this->gradient,this->N);
}
/*}}}*/
int ControlParam::ObjectEnum(void){/*{{{*/
return ControlParamEnum;
}
/*}}}*/
void ControlParam::GetParameterValue(IssmDouble** poutput,int* pN, const char* data){/*{{{*/
IssmDouble* output=xNew<IssmDouble>(N);
if(strcmp(data,"value")==0){
xMemCpy<IssmDouble>(output,value,N);
}
else if (strcmp(data,"lowerbound")==0){
xMemCpy<IssmDouble>(output,minvalue,N);
}
else if (strcmp(data,"upperbound")==0){
xMemCpy<IssmDouble>(output,maxvalue,N);
}
else if (strcmp(data,"gradient")==0){
xMemCpy<IssmDouble>(output,gradient,N);
}
else{
_error_("Data " << data << " not supported yet");
}
/*Assign output pointers:*/
if(pN) *pN=N;
*poutput=output;
}
/*}}}*/
void ControlParam::GetParameterValue(IssmDouble* poutput){/*{{{*/
/*Copy entire vector if M==1, or first row if M==2*/
if(M==1){
xMemCpy<IssmDouble>(poutput,value,N);
return;
}
_error_("STOP");
}
/*}}}*/
void ControlParam::GetParameterValue(IssmDouble* poutput, IssmDouble time){/*{{{*/
if(M==1){
*poutput = value[0];
return;
}
IssmDouble *timesteps = &this->value[1*this->N+0];
IssmDouble output;
bool found;
/*Ok, we have the time, go through the timesteps, and figure out which interval we
*fall within. Then interpolate the values on this interval: */
if(time<timesteps[0]){
/*get values for the first time: */
output=this->value[0];
found=true;
}
else if(time>timesteps[this->N-1]){
/*get values for the last time: */
output=this->value[this->N-1];
found=true;
}
else{
/*Find which interval we fall within: */
for(int i=0;i<this->N;i++){
if(time==timesteps[i]){
/*We are right on one step time: */
output=this->value[i];
found=true;
break; //we are done with the time interpolation.
}
else{
if(timesteps[i]<time && time<timesteps[i+1]){
/*ok, we have the interval ]i:i+1[. Interpolate linearly for now: */
IssmDouble deltat=timesteps[i+1]-timesteps[i];
IssmDouble alpha=(time-timesteps[i])/deltat;
output=(1.0-alpha)*this->value[i] + alpha*this->value[i+1];
found=true;
break;
}
else continue; //keep looking on the next interval
}
}
}
if(!found)_error_("did not find time interval on which to interpolate values");
//_printf_("for time = "<<time/31536000.<<" yr, melt = "<<output*31536000.<<" m/yr\n");
*poutput=output;
}
/*}}}*/
void ControlParam::GetParameterValue(IssmDouble** poutput, int* pN){/*{{{*/
/*This method should be specific to VectorParams, only one tow required*/
_assert_(N>0);
_assert_(M==1);
IssmDouble* output=xNew<IssmDouble>(N);
xMemCpy<IssmDouble>(output,value,N);
/*Assign output pointers:*/
if(pN) *pN=N;
*poutput=output;
}
/*}}}*/
void ControlParam::SetValue(IssmDouble* poutput,int in_M, int in_N){/*{{{*/
_assert_(in_N==this->N);
_assert_(in_M==1);
xMemCpy<IssmDouble>(this->value,poutput,in_N);
}
/*}}}*/
void ControlParam::SetGradient(IssmDouble* poutput,int in_M, int in_N){/*{{{*/
_assert_(in_M==1);
xMemCpy<IssmDouble>(this->gradient,poutput,in_N);
}
/*}}}*/
void ControlParam::GetVectorFromControl(Vector<IssmDouble>* vector,int control_index,int in_N,const char* data,int offset){/*{{{*/
/*Get list of ids for this element and this control*/
_assert_(in_N==this->N);
int* idlist = xNew<int>(this->N);
for(int i=0;i<this->N;i++) idlist[i] = offset+i;
/*Get data*/
IssmDouble* values = NULL;
GetParameterValue(&values, NULL, data);
/*Enter data in vector*/
vector->SetValues(this->N,idlist,values,INS_VAL);
/*Clean up*/
xDelete<int>(idlist);
xDelete<IssmDouble>(values);
}/*}}}*/