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

Last change on this file since 14951 was 14951, checked in by Eric.Larour, 12 years ago

CHG: de-entangle dependencies between Containers and shared/Elements

File size: 10.0 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 "../EnumDefinitions/EnumDefinitions.h"
21#include "../classes/objects/Inputs/Input.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=dynamic_cast<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 " << 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=dynamic_cast<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 " << 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=dynamic_cast<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 " << 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=dynamic_cast<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 " << 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=dynamic_cast<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=dynamic_cast<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=dynamic_cast<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 /*Find x and y inputs: */
211 Input* constrain_input=dynamic_cast<Input*>(this->GetInput(constrain_enum));
212
213 /*some checks: */
214 if(!constrain_input) _error_("input " << EnumToStringx(constrain_enum) << " could not be found!");
215
216 /*Apply ContrainMin: */
217 constrain_input->ConstrainMin(minimum);
218}
219/*}}}*/
220/*FUNCTION Inputs::InfinityNorm{{{*/
221IssmDouble Inputs::InfinityNorm(int enumtype){
222
223 /*Output*/
224 IssmDouble norm;
225
226 /*Get input*/
227 Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
228
229 /*Apply ContrainMin: */
230 if (input){
231 norm=input->InfinityNorm();
232 }
233 else{
234 norm=0;
235 }
236
237 /*Return output*/
238 return norm;
239}
240/*}}}*/
241/*FUNCTION Inputs::Max{{{*/
242IssmDouble Inputs::Max(int enumtype){
243
244 /*Output*/
245 IssmDouble max;
246
247 /*Get input*/
248 Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
249
250 /*Apply ContrainMin: */
251 if (input){
252 max=input->Max();
253 }
254 else{
255 _error_("Input " << EnumToStringx(enumtype) << " not found");
256 }
257
258 /*Return output*/
259 return max;
260}
261/*}}}*/
262/*FUNCTION Inputs::MaxAbs{{{*/
263IssmDouble Inputs::MaxAbs(int enumtype){
264
265 /*Output*/
266 IssmDouble max;
267
268 /*Get input*/
269 Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
270
271 /*Apply ContrainMin: */
272 if (input){
273 max=input->MaxAbs();
274 }
275 else{
276 _error_("Input " << EnumToStringx(enumtype) << " not found");
277 }
278
279 /*Return output*/
280 return max;
281}
282/*}}}*/
283/*FUNCTION Inputs::Min{{{*/
284IssmDouble Inputs::Min(int enumtype){
285
286 /*Output*/
287 IssmDouble min;
288
289 /*Get input*/
290 Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
291
292 /*Apply ContrainMin: */
293 if (input){
294 min=input->Min();
295 }
296 else{
297 _error_("Input " << EnumToStringx(enumtype) << " not found");
298 }
299
300 /*Return output*/
301 return min;
302}
303/*}}}*/
304/*FUNCTION Inputs::MinAbs{{{*/
305IssmDouble Inputs::MinAbs(int enumtype){
306
307 /*Output*/
308 IssmDouble min;
309
310 /*Get input*/
311 Input* input=dynamic_cast<Input*>(this->GetInput(enumtype));
312
313 /*Apply ContrainMin: */
314 if (input){
315 min=input->MinAbs();
316 }
317 else{
318 _error_("Input " << EnumToStringx(enumtype) << " not found");
319 }
320
321 /*Return output*/
322 return min;
323}
324/*}}}*/
325/*FUNCTION Inputs::GetInput{{{*/
326Input* Inputs::GetInput(int enum_name){
327
328 vector<Object*>::iterator object;
329 Input* input=NULL;
330
331 for ( object=objects.begin() ; object < objects.end(); object++ ){
332
333 input=dynamic_cast<Input*>(*object);
334
335 if (input->InstanceEnum()==enum_name){
336 return input;
337 }
338 }
339 return NULL;
340}
341/*}}}*/
342/*FUNCTION Inputs::DeleteInput{{{*/
343int Inputs::DeleteInput(int enum_type){
344
345 vector<Object*>::iterator object;
346 Input* input=NULL;
347
348 for ( object=objects.begin() ; object < objects.end(); object++ ){
349
350 input=dynamic_cast<Input*>(*object);
351
352 if (input->InstanceEnum()==enum_type){
353 this->DeleteObject(input);
354 break;
355 }
356 }
357
358 return 1;
359
360}
361/*}}}*/
362/*FUNCTION Inputs::DuplicateInput{{{*/
363void Inputs::DuplicateInput(int original_enum,int new_enum){
364
365 /*Make a copy of the original input: */
366 Input* original=dynamic_cast<Input*>(this->GetInput(original_enum));
367 if(!original)_error_("could not find input with enum: " << EnumToStringx(original_enum));
368 Input* copy=dynamic_cast<Input*>(original->copy());
369
370 /*Change copy enum to reinitialized_enum: */
371 copy->ChangeEnum(new_enum);
372
373 /*Add copy into inputs, it will wipe off the one already there: */
374 this->AddInput(dynamic_cast<Input*>(copy));
375}
376/*}}}*/
377/*FUNCTION Inputs::SpawnTriaInputs{{{*/
378Inputs* Inputs::SpawnTriaInputs(int* indices){
379
380 /*Intermediary*/
381 vector<Object*>::iterator object;
382 Input* inputin=NULL;
383 Input* inputout=NULL;
384
385 /*Output*/
386 Inputs* newinputs=new Inputs();
387
388 /*Go through inputs and call Spawn function*/
389 for ( object=objects.begin() ; object < objects.end(); object++ ){
390
391 /*Create new input*/
392 inputin=dynamic_cast<Input*>(*object);
393 inputout=inputin->SpawnTriaInput(indices);
394
395 /*Add input to new inputs*/
396 newinputs->AddObject(inputout);
397 }
398
399 /*Assign output pointer*/
400 return newinputs;
401}
402/*}}}*/
403/*FUNCTION Inputs::AXPY{{{*/
404void Inputs::AXPY(int MeshYEnum, IssmDouble scalar, int MeshXEnum){
405
406 /*Find x and y inputs: */
407 Input* xinput=dynamic_cast<Input*>(this->GetInput(MeshXEnum));
408 Input* yinput=dynamic_cast<Input*>(this->GetInput(MeshYEnum));
409
410 /*some checks: */
411 if(!xinput) _error_("input " << EnumToStringx(MeshXEnum) << " could not be found!");
412 if(!yinput) _error_("input " << EnumToStringx(MeshYEnum) << " could not be found!");
413
414 /*Apply AXPY: */
415 yinput->AXPY(xinput,scalar);
416}
417/*}}}*/
418/*FUNCTION Inputs::Configure{{{*/
419void Inputs::Configure(Parameters* parameters){
420
421 vector<Object*>::iterator object;
422 Input* input=NULL;
423
424 for ( object=objects.begin() ; object < objects.end(); object++ ){
425
426 input=dynamic_cast<Input*>(*object);
427 input->Configure(parameters);
428
429 }
430
431}
432/*}}}*/
Note: See TracBrowser for help on using the repository browser.