source: issm/trunk-jpl/src/c/Container/Inputs.cpp@ 12493

Last change on this file since 12493 was 12493, checked in by Mathieu Morlighem, 13 years ago

replaced all _error_ to _error2_

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