1 | /*
|
---|
2 | * \file Inputs.c
|
---|
3 | * \brief: implementation of the Inputs class, derived from DataSet class
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*Headers: {{{*/
|
---|
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 "./Inputs.h"
|
---|
14 | #include "../classes/objects/Inputs/Input.h"
|
---|
15 | #include "../shared/shared.h"
|
---|
16 | #include "../EnumDefinitions/EnumDefinitions.h"
|
---|
17 |
|
---|
18 | using namespace std;
|
---|
19 | /*}}}*/
|
---|
20 |
|
---|
21 | /*Object constructors and destructor*/
|
---|
22 | /*FUNCTION Inputs::Inputs(){{{*/
|
---|
23 | Inputs::Inputs(){
|
---|
24 | return;
|
---|
25 | }
|
---|
26 | /*}}}*/
|
---|
27 | /*FUNCTION Inputs::~Inputs(){{{*/
|
---|
28 | Inputs::~Inputs(){
|
---|
29 | return;
|
---|
30 | }
|
---|
31 | /*}}}*/
|
---|
32 |
|
---|
33 | /*Object management*/
|
---|
34 | /*FUNCTION Inputs::GetInputValue(bool* pvalue,int enum-type){{{*/
|
---|
35 | void Inputs::GetInputValue(bool* pvalue,int enum_type){
|
---|
36 |
|
---|
37 | vector<Object*>::iterator object;
|
---|
38 | Input* input=NULL;
|
---|
39 | bool found=false;
|
---|
40 |
|
---|
41 | /*Go through inputs and check whether any input with the same name is already in: */
|
---|
42 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
43 |
|
---|
44 | input=dynamic_cast<Input*>(*object);
|
---|
45 | if (input->InstanceEnum()==enum_type){
|
---|
46 | found=true;
|
---|
47 | break;
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (!found){
|
---|
52 | /*we could not find an input with the correct enum type. No defaults values were provided,
|
---|
53 | * error out: */
|
---|
54 | _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
|
---|
55 | }
|
---|
56 |
|
---|
57 | /*Ok, we have an input if we made it here, request the input to return the value: */
|
---|
58 | input->GetInputValue(pvalue);
|
---|
59 |
|
---|
60 | }
|
---|
61 | /*}}}*/
|
---|
62 | /*FUNCTION Inputs::GetInputValue(int* pvalue,int enum-type){{{*/
|
---|
63 | void Inputs::GetInputValue(int* pvalue,int enum_type){
|
---|
64 |
|
---|
65 | vector<Object*>::iterator object;
|
---|
66 | Input* input=NULL;
|
---|
67 | bool found=false;
|
---|
68 |
|
---|
69 | /*Go through inputs and check whether any input with the same name is already in: */
|
---|
70 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
71 |
|
---|
72 | input=dynamic_cast<Input*>(*object);
|
---|
73 | if (input->InstanceEnum()==enum_type){
|
---|
74 | found=true;
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (!found){
|
---|
80 | /*we could not find an input with the correct enum type. No defaults values were provided,
|
---|
81 | * error out: */
|
---|
82 | _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*Ok, we have an input if we made it here, request the input to return the value: */
|
---|
86 | input->GetInputValue(pvalue);
|
---|
87 |
|
---|
88 | }
|
---|
89 | /*}}}*/
|
---|
90 | /*FUNCTION Inputs::GetInputValue(IssmDouble* pvalue,int enum-type){{{*/
|
---|
91 | void Inputs::GetInputValue(IssmDouble* pvalue,int enum_type){
|
---|
92 |
|
---|
93 | vector<Object*>::iterator object;
|
---|
94 | Input* input=NULL;
|
---|
95 | bool found=false;
|
---|
96 |
|
---|
97 | /*Go through inputs and check whether any input with the same name is already in: */
|
---|
98 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
99 |
|
---|
100 | input=dynamic_cast<Input*>(*object);
|
---|
101 | if (input->InstanceEnum()==enum_type){
|
---|
102 | found=true;
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (!found){
|
---|
108 | /*we could not find an input with the correct enum type. No defaults values were provided,
|
---|
109 | * error out: */
|
---|
110 | _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
|
---|
111 | }
|
---|
112 |
|
---|
113 | /*Ok, we have an input if we made it here, request the input to return the value: */
|
---|
114 | input->GetInputValue(pvalue);
|
---|
115 |
|
---|
116 | }
|
---|
117 | /*}}}*/
|
---|
118 | /*FUNCTION Inputs::GetInputAverage{{{*/
|
---|
119 | void Inputs::GetInputAverage(IssmDouble* pvalue,int enum_type){
|
---|
120 |
|
---|
121 | vector<Object*>::iterator object;
|
---|
122 | Input* input=NULL;
|
---|
123 | bool found=false;
|
---|
124 |
|
---|
125 | /*Go through inputs and check whether any input with the same name is already in: */
|
---|
126 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
127 |
|
---|
128 | input=dynamic_cast<Input*>(*object);
|
---|
129 | if (input->InstanceEnum()==enum_type){
|
---|
130 | found=true;
|
---|
131 | break;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (!found){
|
---|
136 | /*we could not find an input with the correct enum type. No defaults values were provided,
|
---|
137 | * error out: */
|
---|
138 | _error_("could not find input with enum type " << enum_type << " (" << EnumToStringx(enum_type) << ")");
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*Ok, we have an input if we made it here, request the input to return the value: */
|
---|
142 | input->GetInputAverage(pvalue);
|
---|
143 |
|
---|
144 | }
|
---|
145 | /*}}}*/
|
---|
146 | /*FUNCTION Inputs::AddInput{{{*/
|
---|
147 | int Inputs::AddInput(Input* in_input){
|
---|
148 |
|
---|
149 | /*First, go through dataset of inputs and check whether any input
|
---|
150 | * with the same name is already in. If so, erase the corresponding
|
---|
151 | * object before adding this new one: */
|
---|
152 | vector<Object*>::iterator object;
|
---|
153 | Input* input=NULL;
|
---|
154 |
|
---|
155 | /*In debugging mode, check that the input is not a NULL pointer*/
|
---|
156 | _assert_(in_input);
|
---|
157 |
|
---|
158 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
159 |
|
---|
160 | input=dynamic_cast<Input*>(*object);
|
---|
161 |
|
---|
162 | if (input->InstanceEnum()==in_input->InstanceEnum()){
|
---|
163 | this->DeleteObject(input);
|
---|
164 | break;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | this->AddObject(in_input);
|
---|
168 |
|
---|
169 | return 1;
|
---|
170 | }
|
---|
171 | /*}}}*/
|
---|
172 | /*FUNCTION Inputs::ChangeEnum{{{*/
|
---|
173 | void Inputs::ChangeEnum(int oldenumtype,int newenumtype){
|
---|
174 |
|
---|
175 | /*Go through dataset of inputs and look for input with
|
---|
176 | * same enum as input enum, once found, just change its name */
|
---|
177 | vector<Object*>::iterator object;
|
---|
178 | Input* input=NULL;
|
---|
179 |
|
---|
180 | /*Delete existing input of newenumtype if it exists*/
|
---|
181 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
182 | input=dynamic_cast<Input*>(*object);
|
---|
183 |
|
---|
184 | if (input->InstanceEnum()==newenumtype){
|
---|
185 | this->DeleteObject(input);
|
---|
186 | break;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | /*Change enum_type of input of oldenumtype*/
|
---|
191 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
192 |
|
---|
193 | input=dynamic_cast<Input*>(*object);
|
---|
194 |
|
---|
195 | if (input->InstanceEnum()==oldenumtype){
|
---|
196 | input->ChangeEnum(newenumtype);
|
---|
197 | break;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 | /*}}}*/
|
---|
202 | /*FUNCTION Inputs::ConstrainMin{{{*/
|
---|
203 | void Inputs::ConstrainMin(int constrain_enum, IssmDouble minimum){
|
---|
204 |
|
---|
205 | /*Find x and y inputs: */
|
---|
206 | Input* constrain_input=dynamic_cast<Input*>(this->GetInput(constrain_enum));
|
---|
207 |
|
---|
208 | /*some checks: */
|
---|
209 | if(!constrain_input) _error_("input " << EnumToStringx(constrain_enum) << " could not be found!");
|
---|
210 |
|
---|
211 | /*Apply ContrainMin: */
|
---|
212 | constrain_input->ConstrainMin(minimum);
|
---|
213 | }
|
---|
214 | /*}}}*/
|
---|
215 | /*FUNCTION Inputs::InfinityNorm{{{*/
|
---|
216 | IssmDouble Inputs::InfinityNorm(int enumtype){
|
---|
217 |
|
---|
218 | /*Output*/
|
---|
219 | IssmDouble norm;
|
---|
220 |
|
---|
221 | /*Get input*/
|
---|
222 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
---|
223 |
|
---|
224 | /*Apply ContrainMin: */
|
---|
225 | if (input){
|
---|
226 | norm=input->InfinityNorm();
|
---|
227 | }
|
---|
228 | else{
|
---|
229 | norm=0;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /*Return output*/
|
---|
233 | return norm;
|
---|
234 | }
|
---|
235 | /*}}}*/
|
---|
236 | /*FUNCTION Inputs::Max{{{*/
|
---|
237 | IssmDouble Inputs::Max(int enumtype){
|
---|
238 |
|
---|
239 | /*Output*/
|
---|
240 | IssmDouble max;
|
---|
241 |
|
---|
242 | /*Get input*/
|
---|
243 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
---|
244 |
|
---|
245 | /*Apply ContrainMin: */
|
---|
246 | if (input){
|
---|
247 | max=input->Max();
|
---|
248 | }
|
---|
249 | else{
|
---|
250 | _error_("Input " << EnumToStringx(enumtype) << " not found");
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*Return output*/
|
---|
254 | return max;
|
---|
255 | }
|
---|
256 | /*}}}*/
|
---|
257 | /*FUNCTION Inputs::MaxAbs{{{*/
|
---|
258 | IssmDouble Inputs::MaxAbs(int enumtype){
|
---|
259 |
|
---|
260 | /*Output*/
|
---|
261 | IssmDouble max;
|
---|
262 |
|
---|
263 | /*Get input*/
|
---|
264 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
---|
265 |
|
---|
266 | /*Apply ContrainMin: */
|
---|
267 | if (input){
|
---|
268 | max=input->MaxAbs();
|
---|
269 | }
|
---|
270 | else{
|
---|
271 | _error_("Input " << EnumToStringx(enumtype) << " not found");
|
---|
272 | }
|
---|
273 |
|
---|
274 | /*Return output*/
|
---|
275 | return max;
|
---|
276 | }
|
---|
277 | /*}}}*/
|
---|
278 | /*FUNCTION Inputs::Min{{{*/
|
---|
279 | IssmDouble Inputs::Min(int enumtype){
|
---|
280 |
|
---|
281 | /*Output*/
|
---|
282 | IssmDouble min;
|
---|
283 |
|
---|
284 | /*Get input*/
|
---|
285 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
---|
286 |
|
---|
287 | /*Apply ContrainMin: */
|
---|
288 | if (input){
|
---|
289 | min=input->Min();
|
---|
290 | }
|
---|
291 | else{
|
---|
292 | _error_("Input " << EnumToStringx(enumtype) << " not found");
|
---|
293 | }
|
---|
294 |
|
---|
295 | /*Return output*/
|
---|
296 | return min;
|
---|
297 | }
|
---|
298 | /*}}}*/
|
---|
299 | /*FUNCTION Inputs::MinAbs{{{*/
|
---|
300 | IssmDouble Inputs::MinAbs(int enumtype){
|
---|
301 |
|
---|
302 | /*Output*/
|
---|
303 | IssmDouble min;
|
---|
304 |
|
---|
305 | /*Get input*/
|
---|
306 | Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
|
---|
307 |
|
---|
308 | /*Apply ContrainMin: */
|
---|
309 | if (input){
|
---|
310 | min=input->MinAbs();
|
---|
311 | }
|
---|
312 | else{
|
---|
313 | _error_("Input " << EnumToStringx(enumtype) << " not found");
|
---|
314 | }
|
---|
315 |
|
---|
316 | /*Return output*/
|
---|
317 | return min;
|
---|
318 | }
|
---|
319 | /*}}}*/
|
---|
320 | /*FUNCTION Inputs::GetInput{{{*/
|
---|
321 | Input* Inputs::GetInput(int enum_name){
|
---|
322 |
|
---|
323 | vector<Object*>::iterator object;
|
---|
324 | Input* input=NULL;
|
---|
325 |
|
---|
326 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
327 |
|
---|
328 | input=dynamic_cast<Input*>(*object);
|
---|
329 |
|
---|
330 | if (input->InstanceEnum()==enum_name){
|
---|
331 | return input;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | return NULL;
|
---|
335 | }
|
---|
336 | /*}}}*/
|
---|
337 | /*FUNCTION Inputs::DeleteInput{{{*/
|
---|
338 | int Inputs::DeleteInput(int enum_type){
|
---|
339 |
|
---|
340 | vector<Object*>::iterator object;
|
---|
341 | Input* input=NULL;
|
---|
342 |
|
---|
343 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
344 |
|
---|
345 | input=dynamic_cast<Input*>(*object);
|
---|
346 |
|
---|
347 | if (input->InstanceEnum()==enum_type){
|
---|
348 | this->DeleteObject(input);
|
---|
349 | break;
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | return 1;
|
---|
354 |
|
---|
355 | }
|
---|
356 | /*}}}*/
|
---|
357 | /*FUNCTION Inputs::DuplicateInput{{{*/
|
---|
358 | void Inputs::DuplicateInput(int original_enum,int new_enum){
|
---|
359 |
|
---|
360 | /*Make a copy of the original input: */
|
---|
361 | Input* original=dynamic_cast<Input*>(this->GetInput(original_enum));
|
---|
362 | if(!original)_error_("could not find input with enum: " << EnumToStringx(original_enum));
|
---|
363 | Input* copy=dynamic_cast<Input*>(original->copy());
|
---|
364 |
|
---|
365 | /*Change copy enum to reinitialized_enum: */
|
---|
366 | copy->ChangeEnum(new_enum);
|
---|
367 |
|
---|
368 | /*Add copy into inputs, it will wipe off the one already there: */
|
---|
369 | this->AddInput(dynamic_cast<Input*>(copy));
|
---|
370 | }
|
---|
371 | /*}}}*/
|
---|
372 | /*FUNCTION Inputs::SpawnTriaInputs{{{*/
|
---|
373 | Inputs* Inputs::SpawnTriaInputs(int* indices){
|
---|
374 |
|
---|
375 | /*Intermediary*/
|
---|
376 | vector<Object*>::iterator object;
|
---|
377 | Input* inputin=NULL;
|
---|
378 | Input* inputout=NULL;
|
---|
379 |
|
---|
380 | /*Output*/
|
---|
381 | Inputs* newinputs=new Inputs();
|
---|
382 |
|
---|
383 | /*Go through inputs and call Spawn function*/
|
---|
384 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
385 |
|
---|
386 | /*Create new input*/
|
---|
387 | inputin=dynamic_cast<Input*>(*object);
|
---|
388 | inputout=inputin->SpawnTriaInput(indices);
|
---|
389 |
|
---|
390 | /*Add input to new inputs*/
|
---|
391 | newinputs->AddObject(inputout);
|
---|
392 | }
|
---|
393 |
|
---|
394 | /*Assign output pointer*/
|
---|
395 | return newinputs;
|
---|
396 | }
|
---|
397 | /*}}}*/
|
---|
398 | /*FUNCTION Inputs::AXPY{{{*/
|
---|
399 | void Inputs::AXPY(int MeshYEnum, IssmDouble scalar, int MeshXEnum){
|
---|
400 |
|
---|
401 | /*Find x and y inputs: */
|
---|
402 | Input* xinput=dynamic_cast<Input*>(this->GetInput(MeshXEnum));
|
---|
403 | Input* yinput=dynamic_cast<Input*>(this->GetInput(MeshYEnum));
|
---|
404 |
|
---|
405 | /*some checks: */
|
---|
406 | if(!xinput) _error_("input " << EnumToStringx(MeshXEnum) << " could not be found!");
|
---|
407 | if(!yinput) _error_("input " << EnumToStringx(MeshYEnum) << " could not be found!");
|
---|
408 |
|
---|
409 | /*Apply AXPY: */
|
---|
410 | yinput->AXPY(xinput,scalar);
|
---|
411 | }
|
---|
412 | /*}}}*/
|
---|
413 | /*FUNCTION Inputs::Configure{{{*/
|
---|
414 | void Inputs::Configure(Parameters* parameters){
|
---|
415 |
|
---|
416 | vector<Object*>::iterator object;
|
---|
417 | Input* input=NULL;
|
---|
418 |
|
---|
419 | for ( object=objects.begin() ; object < objects.end(); object++ ){
|
---|
420 |
|
---|
421 | input=dynamic_cast<Input*>(*object);
|
---|
422 | input->Configure(parameters);
|
---|
423 |
|
---|
424 | }
|
---|
425 |
|
---|
426 | }
|
---|
427 | /*}}}*/
|
---|