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

Last change on this file since 12466 was 12466, checked in by utke, 13 years ago

type rename plus one generic include to make the typedef'ed name known

File size: 9.7 KB
Line 
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 <vector>
14#include <functional>
15#include <algorithm>
16#include <iostream>
17
18#include "./DataSet.h"
19#include "../shared/shared.h"
20#include "../include/include.h"
21#include "../EnumDefinitions/EnumDefinitions.h"
22
23using namespace std;
24/*}}}*/
25
26/*Object constructors and destructor*/
27/*FUNCTION Inputs::Inputs(){{{*/
28Inputs::Inputs(){
29 return;
30}
31/*}}}*/
32/*FUNCTION Inputs::~Inputs(){{{*/
33Inputs::~Inputs(){
34 return;
35}
36/*}}}*/
37
38/*Object management*/
39/*FUNCTION Inputs::GetInputValue(bool* pvalue,int enum-type){{{*/
40void Inputs::GetInputValue(bool* pvalue,int enum_type){
41
42 vector<Object*>::iterator object;
43 Input* input=NULL;
44 bool found=false;
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);
50 if (input->InstanceEnum()==enum_type){
51 found=true;
52 break;
53 }
54 }
55
56 if (!found){
57 /*we could not find an input with the correct enum type. No defaults values were provided,
58 * error out: */
59 _error_("could not find input with enum type %i (%s)",enum_type,EnumToStringx(enum_type));
60 }
61
62 /*Ok, we have an input if we made it here, request the input to return the value: */
63 input->GetInputValue(pvalue);
64
65}
66/*}}}*/
67/*FUNCTION Inputs::GetInputValue(int* pvalue,int enum-type){{{*/
68void Inputs::GetInputValue(int* pvalue,int enum_type){
69
70 vector<Object*>::iterator object;
71 Input* input=NULL;
72 bool found=false;
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);
78 if (input->InstanceEnum()==enum_type){
79 found=true;
80 break;
81 }
82 }
83
84 if (!found){
85 /*we could not find an input with the correct enum type. No defaults values were provided,
86 * error out: */
87 _error_("could not find input with enum type %i (%s)",enum_type,EnumToStringx(enum_type));
88 }
89
90 /*Ok, we have an input if we made it here, request the input to return the value: */
91 input->GetInputValue(pvalue);
92
93}
94/*}}}*/
95/*FUNCTION Inputs::GetInputValue(IssmDouble* pvalue,int enum-type){{{*/
96void Inputs::GetInputValue(IssmDouble* pvalue,int enum_type){
97
98 vector<Object*>::iterator object;
99 Input* input=NULL;
100 bool found=false;
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);
106 if (input->InstanceEnum()==enum_type){
107 found=true;
108 break;
109 }
110 }
111
112 if (!found){
113 /*we could not find an input with the correct enum type. No defaults values were provided,
114 * error out: */
115 _error_("could not find input with enum type %i (%s)",enum_type,EnumToStringx(enum_type));
116 }
117
118 /*Ok, we have an input if we made it here, request the input to return the value: */
119 input->GetInputValue(pvalue);
120
121}
122/*}}}*/
123/*FUNCTION Inputs::GetInputAverage{{{*/
124void Inputs::GetInputAverage(IssmDouble* pvalue,int enum_type){
125
126 vector<Object*>::iterator object;
127 Input* input=NULL;
128 bool found=false;
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);
134 if (input->InstanceEnum()==enum_type){
135 found=true;
136 break;
137 }
138 }
139
140 if (!found){
141 /*we could not find an input with the correct enum type. No defaults values were provided,
142 * error out: */
143 _error_("could not find input with enum type %i (%s)",enum_type,EnumToStringx(enum_type));
144 }
145
146 /*Ok, we have an input if we made it here, request the input to return the value: */
147 input->GetInputAverage(pvalue);
148
149}
150/*}}}*/
151/*FUNCTION Inputs::AddInput{{{*/
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
160 /*In debugging mode, check that the input is not a NULL pointer*/
161 _assert_(in_input);
162
163 for ( object=objects.begin() ; object < objects.end(); object++ ){
164
165 input=(Input*)(*object);
166
167 if (input->InstanceEnum()==in_input->InstanceEnum()){
168 this->DeleteObject(input);
169 break;
170 }
171 }
172 this->AddObject(in_input);
173
174 return 1;
175}
176/*}}}*/
177/*FUNCTION Inputs::ChangeEnum{{{*/
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
189 if (input->InstanceEnum()==newenumtype){
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
200 if (input->InstanceEnum()==oldenumtype){
201 input->ChangeEnum(newenumtype);
202 break;
203 }
204 }
205}
206/*}}}*/
207/*FUNCTION Inputs::ConstrainMin{{{*/
208void Inputs::ConstrainMin(int constrain_enum, IssmDouble minimum){
209
210 Input* constrain_input=NULL;
211 /*Find x and y inputs: */
212 constrain_input=(Input*)this->GetInput(constrain_enum);
213
214 /*some checks: */
215 if(!constrain_input) _error_(" input %s could not be found!",EnumToStringx(constrain_enum));
216
217 /*Apply ContrainMin: */
218 constrain_input->ConstrainMin(minimum);
219}
220/*}}}*/
221/*FUNCTION Inputs::InfinityNorm{{{*/
222IssmDouble Inputs::InfinityNorm(int enumtype){
223
224 /*Output*/
225 IssmDouble norm;
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/*}}}*/
242/*FUNCTION Inputs::Max{{{*/
243IssmDouble Inputs::Max(int enumtype){
244
245 /*Output*/
246 IssmDouble max;
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{
256 _error_("Input %s not found",EnumToStringx(enumtype));
257 }
258
259 /*Return output*/
260 return max;
261}
262/*}}}*/
263/*FUNCTION Inputs::MaxAbs{{{*/
264IssmDouble Inputs::MaxAbs(int enumtype){
265
266 /*Output*/
267 IssmDouble max;
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{
277 _error_("Input %s not found",EnumToStringx(enumtype));
278 }
279
280 /*Return output*/
281 return max;
282}
283/*}}}*/
284/*FUNCTION Inputs::Min{{{*/
285IssmDouble Inputs::Min(int enumtype){
286
287 /*Output*/
288 IssmDouble min;
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{
298 _error_("Input %s not found",EnumToStringx(enumtype));
299 }
300
301 /*Return output*/
302 return min;
303}
304/*}}}*/
305/*FUNCTION Inputs::MinAbs{{{*/
306IssmDouble Inputs::MinAbs(int enumtype){
307
308 /*Output*/
309 IssmDouble min;
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{
319 _error_("Input %s not found",EnumToStringx(enumtype));
320 }
321
322 /*Return output*/
323 return min;
324}
325/*}}}*/
326/*FUNCTION Inputs::GetInput{{{*/
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
336 if (input->InstanceEnum()==enum_name){
337 return input;
338 }
339 }
340 return NULL;
341}
342/*}}}*/
343/*FUNCTION Inputs::DeleteInput{{{*/
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
353 if (input->InstanceEnum()==enum_type){
354 this->DeleteObject(input);
355 break;
356 }
357 }
358
359 return 1;
360
361}
362/*}}}*/
363/*FUNCTION Inputs::DuplicateInput{{{*/
364void Inputs::DuplicateInput(int original_enum,int new_enum){
365
366 Input* original=NULL;
367 Input* copy=NULL;
368
369 /*Make a copy of the original input: */
370 original=(Input*)this->GetInput(original_enum);
371 if(!original)_error_("could not find input with enum: %s",EnumToStringx(original_enum));
372 copy=(Input*)original->copy();
373
374 /*Change copy enum to reinitialized_enum: */
375 copy->ChangeEnum(new_enum);
376
377 /*Add copy into inputs, it will wipe off the one already there: */
378 this->AddInput((Input*)copy);
379}
380/*}}}*/
381/*FUNCTION Inputs::SpawnTriaInputs{{{*/
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/*}}}*/
407/*FUNCTION Inputs::AXPY{{{*/
408void Inputs::AXPY(int MeshYEnum, IssmDouble scalar, int MeshXEnum){
409
410 Input* xinput=NULL;
411 Input* yinput=NULL;
412
413 /*Find x and y inputs: */
414 xinput=(Input*)this->GetInput(MeshXEnum);
415 yinput=(Input*)this->GetInput(MeshYEnum);
416
417 /*some checks: */
418 if(!xinput) _error_(" input %s could not be found!",EnumToStringx(MeshXEnum));
419 if(!yinput) _error_(" input %s could not be found!",EnumToStringx(MeshYEnum));
420
421 /*Apply AXPY: */
422 yinput->AXPY(xinput,scalar);
423}
424/*}}}*/
425/*FUNCTION Inputs::Configure{{{*/
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.